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. */
963 /* GNU extensions. */
966 /* C++0x extensions. */
968 case RID_UNDERLYING_TYPE
:
973 if (keyword
>= RID_FIRST_INT_N
974 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
975 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
981 /* Return true if the next token is a keyword for a decl-specifier. */
984 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
988 token
= cp_lexer_peek_token (lexer
);
989 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
992 /* Returns TRUE iff the token T begins a decltype type. */
995 token_is_decltype (cp_token
*t
)
997 return (t
->keyword
== RID_DECLTYPE
998 || t
->type
== CPP_DECLTYPE
);
1001 /* Returns TRUE iff the next token begins a decltype type. */
1004 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1006 cp_token
*t
= cp_lexer_peek_token (lexer
);
1007 return token_is_decltype (t
);
1010 /* Called when processing a token with tree_check_value; perform or defer the
1011 associated checks and return the value. */
1014 saved_checks_value (struct tree_check
*check_value
)
1016 /* Perform any access checks that were deferred. */
1017 vec
<deferred_access_check
, va_gc
> *checks
;
1018 deferred_access_check
*chk
;
1019 checks
= check_value
->checks
;
1023 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1024 perform_or_defer_access_check (chk
->binfo
,
1026 chk
->diag_decl
, tf_warning_or_error
);
1028 /* Return the stored value. */
1029 return check_value
->value
;
1032 /* Return a pointer to the Nth token in the token stream. If N is 1,
1033 then this is precisely equivalent to cp_lexer_peek_token (except
1034 that it is not inline). One would like to disallow that case, but
1035 there is one case (cp_parser_nth_token_starts_template_id) where
1036 the caller passes a variable for N and it might be 1. */
1039 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1043 /* N is 1-based, not zero-based. */
1046 if (cp_lexer_debugging_p (lexer
))
1047 fprintf (cp_lexer_debug_stream
,
1048 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1051 token
= lexer
->next_token
;
1052 gcc_assert (!n
|| token
!= &eof_token
);
1056 if (token
== lexer
->last_token
)
1062 if (!token
->purged_p
)
1066 if (cp_lexer_debugging_p (lexer
))
1068 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1069 putc ('\n', cp_lexer_debug_stream
);
1075 /* Return the next token, and advance the lexer's next_token pointer
1076 to point to the next non-purged token. */
1079 cp_lexer_consume_token (cp_lexer
* lexer
)
1081 cp_token
*token
= lexer
->next_token
;
1083 gcc_assert (token
!= &eof_token
);
1084 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1088 lexer
->next_token
++;
1089 if (lexer
->next_token
== lexer
->last_token
)
1091 lexer
->next_token
= &eof_token
;
1096 while (lexer
->next_token
->purged_p
);
1098 cp_lexer_set_source_position_from_token (token
);
1100 /* Provide debugging output. */
1101 if (cp_lexer_debugging_p (lexer
))
1103 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1104 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1105 putc ('\n', cp_lexer_debug_stream
);
1111 /* Permanently remove the next token from the token stream, and
1112 advance the next_token pointer to refer to the next non-purged
1116 cp_lexer_purge_token (cp_lexer
*lexer
)
1118 cp_token
*tok
= lexer
->next_token
;
1120 gcc_assert (tok
!= &eof_token
);
1121 tok
->purged_p
= true;
1122 tok
->location
= UNKNOWN_LOCATION
;
1123 tok
->u
.value
= NULL_TREE
;
1124 tok
->keyword
= RID_MAX
;
1129 if (tok
== lexer
->last_token
)
1135 while (tok
->purged_p
);
1136 lexer
->next_token
= tok
;
1139 /* Permanently remove all tokens after TOK, up to, but not
1140 including, the token that will be returned next by
1141 cp_lexer_peek_token. */
1144 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1146 cp_token
*peek
= lexer
->next_token
;
1148 if (peek
== &eof_token
)
1149 peek
= lexer
->last_token
;
1151 gcc_assert (tok
< peek
);
1153 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1155 tok
->purged_p
= true;
1156 tok
->location
= UNKNOWN_LOCATION
;
1157 tok
->u
.value
= NULL_TREE
;
1158 tok
->keyword
= RID_MAX
;
1162 /* Begin saving tokens. All tokens consumed after this point will be
1166 cp_lexer_save_tokens (cp_lexer
* lexer
)
1168 /* Provide debugging output. */
1169 if (cp_lexer_debugging_p (lexer
))
1170 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1172 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1175 /* Commit to the portion of the token stream most recently saved. */
1178 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1180 /* Provide debugging output. */
1181 if (cp_lexer_debugging_p (lexer
))
1182 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1184 lexer
->saved_tokens
.pop ();
1187 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1188 to the token stream. Stop saving tokens. */
1191 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer
))
1195 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1197 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1200 /* RAII wrapper around the above functions, with sanity checking. Creating
1201 a variable saves tokens, which are committed when the variable is
1202 destroyed unless they are explicitly rolled back by calling the rollback
1205 struct saved_token_sentinel
1210 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1212 len
= lexer
->saved_tokens
.length ();
1213 cp_lexer_save_tokens (lexer
);
1217 cp_lexer_rollback_tokens (lexer
);
1220 ~saved_token_sentinel()
1223 cp_lexer_commit_tokens (lexer
);
1224 gcc_assert (lexer
->saved_tokens
.length () == len
);
1228 /* Print a representation of the TOKEN on the STREAM. */
1231 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1233 /* We don't use cpp_type2name here because the parser defines
1234 a few tokens of its own. */
1235 static const char *const token_names
[] = {
1236 /* cpplib-defined token types */
1237 #define OP(e, s) #e,
1238 #define TK(e, s) #e,
1242 /* C++ parser token types - see "Manifest constants", above. */
1245 "NESTED_NAME_SPECIFIER",
1248 /* For some tokens, print the associated data. */
1249 switch (token
->type
)
1252 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1253 For example, `struct' is mapped to an INTEGER_CST. */
1254 if (!identifier_p (token
->u
.value
))
1258 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1265 case CPP_UTF8STRING
:
1266 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1270 print_generic_expr (stream
, token
->u
.value
);
1274 /* If we have a name for the token, print it out. Otherwise, we
1275 simply give the numeric code. */
1276 if (token
->type
< ARRAY_SIZE(token_names
))
1277 fputs (token_names
[token
->type
], stream
);
1279 fprintf (stream
, "[%d]", token
->type
);
1285 debug (cp_token
&ref
)
1287 cp_lexer_print_token (stderr
, &ref
);
1288 fprintf (stderr
, "\n");
1292 debug (cp_token
*ptr
)
1297 fprintf (stderr
, "<nil>\n");
1301 /* Start emitting debugging information. */
1304 cp_lexer_start_debugging (cp_lexer
* lexer
)
1306 if (!LEXER_DEBUGGING_ENABLED_P
)
1307 fatal_error (input_location
,
1308 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1310 lexer
->debugging_p
= true;
1311 cp_lexer_debug_stream
= stderr
;
1314 /* Stop emitting debugging information. */
1317 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1319 if (!LEXER_DEBUGGING_ENABLED_P
)
1320 fatal_error (input_location
,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1323 lexer
->debugging_p
= false;
1324 cp_lexer_debug_stream
= NULL
;
1327 /* Create a new cp_token_cache, representing a range of tokens. */
1329 static cp_token_cache
*
1330 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1332 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1333 cache
->first
= first
;
1338 /* Diagnose if #pragma omp declare simd isn't followed immediately
1339 by function declaration or definition. */
1342 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1344 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1346 error ("%<#pragma omp declare simd%> not immediately followed by "
1347 "function declaration or definition");
1348 parser
->omp_declare_simd
= NULL
;
1352 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1353 and put that into "omp declare simd" attribute. */
1356 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1358 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1360 if (fndecl
== error_mark_node
)
1362 parser
->omp_declare_simd
= NULL
;
1365 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1367 cp_ensure_no_omp_declare_simd (parser
);
1373 /* Diagnose if #pragma acc routine isn't followed immediately by function
1374 declaration or definition. */
1377 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1379 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1381 error_at (parser
->oacc_routine
->loc
,
1382 "%<#pragma acc routine%> not immediately followed by "
1383 "function declaration or definition");
1384 parser
->oacc_routine
= NULL
;
1388 /* Decl-specifiers. */
1390 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1393 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1395 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1400 /* Nothing other than the parser should be creating declarators;
1401 declarators are a semi-syntactic representation of C++ entities.
1402 Other parts of the front end that need to create entities (like
1403 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1405 static cp_declarator
*make_call_declarator
1406 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1407 static cp_declarator
*make_array_declarator
1408 (cp_declarator
*, tree
);
1409 static cp_declarator
*make_pointer_declarator
1410 (cp_cv_quals
, cp_declarator
*, tree
);
1411 static cp_declarator
*make_reference_declarator
1412 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1413 static cp_declarator
*make_ptrmem_declarator
1414 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1416 /* An erroneous declarator. */
1417 static cp_declarator
*cp_error_declarator
;
1419 /* The obstack on which declarators and related data structures are
1421 static struct obstack declarator_obstack
;
1423 /* Alloc BYTES from the declarator memory pool. */
1425 static inline void *
1426 alloc_declarator (size_t bytes
)
1428 return obstack_alloc (&declarator_obstack
, bytes
);
1431 /* Allocate a declarator of the indicated KIND. Clear fields that are
1432 common to all declarators. */
1434 static cp_declarator
*
1435 make_declarator (cp_declarator_kind kind
)
1437 cp_declarator
*declarator
;
1439 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1440 declarator
->kind
= kind
;
1441 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1442 declarator
->attributes
= NULL_TREE
;
1443 declarator
->std_attributes
= NULL_TREE
;
1444 declarator
->declarator
= NULL
;
1445 declarator
->parameter_pack_p
= false;
1446 declarator
->id_loc
= UNKNOWN_LOCATION
;
1451 /* Make a declarator for a generalized identifier. If
1452 QUALIFYING_SCOPE is non-NULL, the identifier is
1453 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1454 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1457 static cp_declarator
*
1458 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1459 special_function_kind sfk
, location_t id_location
)
1461 cp_declarator
*declarator
;
1463 /* It is valid to write:
1465 class C { void f(); };
1469 The standard is not clear about whether `typedef const C D' is
1470 legal; as of 2002-09-15 the committee is considering that
1471 question. EDG 3.0 allows that syntax. Therefore, we do as
1473 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1474 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1476 gcc_assert (identifier_p (unqualified_name
)
1477 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1478 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1480 declarator
= make_declarator (cdk_id
);
1481 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1482 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1483 declarator
->u
.id
.sfk
= sfk
;
1484 declarator
->id_loc
= id_location
;
1489 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1490 of modifiers such as const or volatile to apply to the pointer
1491 type, represented as identifiers. ATTRIBUTES represent the attributes that
1492 appertain to the pointer or reference. */
1495 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1498 cp_declarator
*declarator
;
1500 declarator
= make_declarator (cdk_pointer
);
1501 declarator
->declarator
= target
;
1502 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1503 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1506 declarator
->id_loc
= target
->id_loc
;
1507 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1508 target
->parameter_pack_p
= false;
1511 declarator
->parameter_pack_p
= false;
1513 declarator
->std_attributes
= attributes
;
1518 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1519 represent the attributes that appertain to the pointer or
1523 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1524 bool rvalue_ref
, tree attributes
)
1526 cp_declarator
*declarator
;
1528 declarator
= make_declarator (cdk_reference
);
1529 declarator
->declarator
= target
;
1530 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1531 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1534 declarator
->id_loc
= target
->id_loc
;
1535 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1536 target
->parameter_pack_p
= false;
1539 declarator
->parameter_pack_p
= false;
1541 declarator
->std_attributes
= attributes
;
1546 /* Like make_pointer_declarator -- but for a pointer to a non-static
1547 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1548 appertain to the pointer or reference. */
1551 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1552 cp_declarator
*pointee
,
1555 cp_declarator
*declarator
;
1557 declarator
= make_declarator (cdk_ptrmem
);
1558 declarator
->declarator
= pointee
;
1559 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1560 declarator
->u
.pointer
.class_type
= class_type
;
1564 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1565 pointee
->parameter_pack_p
= false;
1568 declarator
->parameter_pack_p
= false;
1570 declarator
->std_attributes
= attributes
;
1575 /* Make a declarator for the function given by TARGET, with the
1576 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1577 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1578 indicates what exceptions can be thrown. */
1581 make_call_declarator (cp_declarator
*target
,
1583 cp_cv_quals cv_qualifiers
,
1584 cp_virt_specifiers virt_specifiers
,
1585 cp_ref_qualifier ref_qualifier
,
1587 tree exception_specification
,
1588 tree late_return_type
,
1589 tree requires_clause
)
1591 cp_declarator
*declarator
;
1593 declarator
= make_declarator (cdk_function
);
1594 declarator
->declarator
= target
;
1595 declarator
->u
.function
.parameters
= parms
;
1596 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1597 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1598 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1599 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1600 declarator
->u
.function
.exception_specification
= exception_specification
;
1601 declarator
->u
.function
.late_return_type
= late_return_type
;
1602 declarator
->u
.function
.requires_clause
= requires_clause
;
1605 declarator
->id_loc
= target
->id_loc
;
1606 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1607 target
->parameter_pack_p
= false;
1610 declarator
->parameter_pack_p
= false;
1615 /* Make a declarator for an array of BOUNDS elements, each of which is
1616 defined by ELEMENT. */
1619 make_array_declarator (cp_declarator
*element
, tree bounds
)
1621 cp_declarator
*declarator
;
1623 declarator
= make_declarator (cdk_array
);
1624 declarator
->declarator
= element
;
1625 declarator
->u
.array
.bounds
= bounds
;
1628 declarator
->id_loc
= element
->id_loc
;
1629 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1630 element
->parameter_pack_p
= false;
1633 declarator
->parameter_pack_p
= false;
1638 /* Determine whether the declarator we've seen so far can be a
1639 parameter pack, when followed by an ellipsis. */
1641 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1643 if (declarator
&& declarator
->parameter_pack_p
)
1644 /* We already saw an ellipsis. */
1647 /* Search for a declarator name, or any other declarator that goes
1648 after the point where the ellipsis could appear in a parameter
1649 pack. If we find any of these, then this declarator can not be
1650 made into a parameter pack. */
1652 while (declarator
&& !found
)
1654 switch ((int)declarator
->kind
)
1666 declarator
= declarator
->declarator
;
1674 cp_parameter_declarator
*no_parameters
;
1676 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1677 DECLARATOR and DEFAULT_ARGUMENT. */
1679 cp_parameter_declarator
*
1680 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1681 cp_declarator
*declarator
,
1682 tree default_argument
,
1684 bool template_parameter_pack_p
= false)
1686 cp_parameter_declarator
*parameter
;
1688 parameter
= ((cp_parameter_declarator
*)
1689 alloc_declarator (sizeof (cp_parameter_declarator
)));
1690 parameter
->next
= NULL
;
1691 if (decl_specifiers
)
1692 parameter
->decl_specifiers
= *decl_specifiers
;
1694 clear_decl_specs (¶meter
->decl_specifiers
);
1695 parameter
->declarator
= declarator
;
1696 parameter
->default_argument
= default_argument
;
1697 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1698 parameter
->loc
= loc
;
1703 /* Returns true iff DECLARATOR is a declaration for a function. */
1706 function_declarator_p (const cp_declarator
*declarator
)
1710 if (declarator
->kind
== cdk_function
1711 && declarator
->declarator
->kind
== cdk_id
)
1713 if (declarator
->kind
== cdk_id
1714 || declarator
->kind
== cdk_decomp
1715 || declarator
->kind
== cdk_error
)
1717 declarator
= declarator
->declarator
;
1727 A cp_parser parses the token stream as specified by the C++
1728 grammar. Its job is purely parsing, not semantic analysis. For
1729 example, the parser breaks the token stream into declarators,
1730 expressions, statements, and other similar syntactic constructs.
1731 It does not check that the types of the expressions on either side
1732 of an assignment-statement are compatible, or that a function is
1733 not declared with a parameter of type `void'.
1735 The parser invokes routines elsewhere in the compiler to perform
1736 semantic analysis and to build up the abstract syntax tree for the
1739 The parser (and the template instantiation code, which is, in a
1740 way, a close relative of parsing) are the only parts of the
1741 compiler that should be calling push_scope and pop_scope, or
1742 related functions. The parser (and template instantiation code)
1743 keeps track of what scope is presently active; everything else
1744 should simply honor that. (The code that generates static
1745 initializers may also need to set the scope, in order to check
1746 access control correctly when emitting the initializers.)
1751 The parser is of the standard recursive-descent variety. Upcoming
1752 tokens in the token stream are examined in order to determine which
1753 production to use when parsing a non-terminal. Some C++ constructs
1754 require arbitrary look ahead to disambiguate. For example, it is
1755 impossible, in the general case, to tell whether a statement is an
1756 expression or declaration without scanning the entire statement.
1757 Therefore, the parser is capable of "parsing tentatively." When the
1758 parser is not sure what construct comes next, it enters this mode.
1759 Then, while we attempt to parse the construct, the parser queues up
1760 error messages, rather than issuing them immediately, and saves the
1761 tokens it consumes. If the construct is parsed successfully, the
1762 parser "commits", i.e., it issues any queued error messages and
1763 the tokens that were being preserved are permanently discarded.
1764 If, however, the construct is not parsed successfully, the parser
1765 rolls back its state completely so that it can resume parsing using
1766 a different alternative.
1771 The performance of the parser could probably be improved substantially.
1772 We could often eliminate the need to parse tentatively by looking ahead
1773 a little bit. In some places, this approach might not entirely eliminate
1774 the need to parse tentatively, but it might still speed up the average
1777 /* Flags that are passed to some parsing functions. These values can
1778 be bitwise-ored together. */
1783 CP_PARSER_FLAGS_NONE
= 0x0,
1784 /* The construct is optional. If it is not present, then no error
1785 should be issued. */
1786 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1787 /* When parsing a type-specifier, treat user-defined type-names
1788 as non-type identifiers. */
1789 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1790 /* When parsing a type-specifier, do not try to parse a class-specifier
1791 or enum-specifier. */
1792 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1793 /* When parsing a decl-specifier-seq, only allow type-specifier or
1795 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1796 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1797 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10,
1798 /* When parsing a decl-specifier-seq, allow missing typename. */
1799 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
= 0x20
1802 /* This type is used for parameters and variables which hold
1803 combinations of the above flags. */
1804 typedef int cp_parser_flags
;
1806 /* The different kinds of declarators we want to parse. */
1808 enum cp_parser_declarator_kind
1810 /* We want an abstract declarator. */
1811 CP_PARSER_DECLARATOR_ABSTRACT
,
1812 /* We want a named declarator. */
1813 CP_PARSER_DECLARATOR_NAMED
,
1814 /* We don't mind, but the name must be an unqualified-id. */
1815 CP_PARSER_DECLARATOR_EITHER
1818 /* The precedence values used to parse binary expressions. The minimum value
1819 of PREC must be 1, because zero is reserved to quickly discriminate
1820 binary operators from other tokens. */
1825 PREC_LOGICAL_OR_EXPRESSION
,
1826 PREC_LOGICAL_AND_EXPRESSION
,
1827 PREC_INCLUSIVE_OR_EXPRESSION
,
1828 PREC_EXCLUSIVE_OR_EXPRESSION
,
1829 PREC_AND_EXPRESSION
,
1830 PREC_EQUALITY_EXPRESSION
,
1831 PREC_RELATIONAL_EXPRESSION
,
1832 PREC_SHIFT_EXPRESSION
,
1833 PREC_ADDITIVE_EXPRESSION
,
1834 PREC_MULTIPLICATIVE_EXPRESSION
,
1836 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1839 /* A mapping from a token type to a corresponding tree node type, with a
1840 precedence value. */
1842 struct cp_parser_binary_operations_map_node
1844 /* The token type. */
1845 enum cpp_ttype token_type
;
1846 /* The corresponding tree code. */
1847 enum tree_code tree_type
;
1848 /* The precedence of this operator. */
1849 enum cp_parser_prec prec
;
1852 struct cp_parser_expression_stack_entry
1854 /* Left hand side of the binary operation we are currently
1857 /* Original tree code for left hand side, if it was a binary
1858 expression itself (used for -Wparentheses). */
1859 enum tree_code lhs_type
;
1860 /* Tree code for the binary operation we are parsing. */
1861 enum tree_code tree_type
;
1862 /* Precedence of the binary operation we are parsing. */
1863 enum cp_parser_prec prec
;
1864 /* Location of the binary operation we are parsing. */
1868 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1869 entries because precedence levels on the stack are monotonically
1871 typedef struct cp_parser_expression_stack_entry
1872 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1876 /* Constructors and destructors. */
1878 static cp_parser_context
*cp_parser_context_new
1879 (cp_parser_context
*);
1881 /* Class variables. */
1883 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1885 /* The operator-precedence table used by cp_parser_binary_expression.
1886 Transformed into an associative array (binops_by_token) by
1889 static const cp_parser_binary_operations_map_node binops
[] = {
1890 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1891 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1893 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1894 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1895 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1897 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1898 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1900 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1901 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1903 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1904 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1905 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1906 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1908 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1909 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1911 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1913 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1915 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1917 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1919 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1922 /* The same as binops, but initialized by cp_parser_new so that
1923 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1925 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1927 /* Constructors and destructors. */
1929 /* Construct a new context. The context below this one on the stack
1930 is given by NEXT. */
1932 static cp_parser_context
*
1933 cp_parser_context_new (cp_parser_context
* next
)
1935 cp_parser_context
*context
;
1937 /* Allocate the storage. */
1938 if (cp_parser_context_free_list
!= NULL
)
1940 /* Pull the first entry from the free list. */
1941 context
= cp_parser_context_free_list
;
1942 cp_parser_context_free_list
= context
->next
;
1943 memset (context
, 0, sizeof (*context
));
1946 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1948 /* No errors have occurred yet in this context. */
1949 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1950 /* If this is not the bottommost context, copy information that we
1951 need from the previous context. */
1954 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1955 expression, then we are parsing one in this context, too. */
1956 context
->object_type
= next
->object_type
;
1957 /* Thread the stack. */
1958 context
->next
= next
;
1964 /* Managing the unparsed function queues. */
1966 #define unparsed_funs_with_default_args \
1967 parser->unparsed_queues->last ().funs_with_default_args
1968 #define unparsed_funs_with_definitions \
1969 parser->unparsed_queues->last ().funs_with_definitions
1970 #define unparsed_nsdmis \
1971 parser->unparsed_queues->last ().nsdmis
1972 #define unparsed_classes \
1973 parser->unparsed_queues->last ().classes
1976 push_unparsed_function_queues (cp_parser
*parser
)
1978 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1979 vec_safe_push (parser
->unparsed_queues
, e
);
1983 pop_unparsed_function_queues (cp_parser
*parser
)
1985 release_tree_vector (unparsed_funs_with_definitions
);
1986 parser
->unparsed_queues
->pop ();
1991 /* Constructors and destructors. */
1993 static cp_parser
*cp_parser_new
1996 /* Routines to parse various constructs.
1998 Those that return `tree' will return the error_mark_node (rather
1999 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2000 Sometimes, they will return an ordinary node if error-recovery was
2001 attempted, even though a parse error occurred. So, to check
2002 whether or not a parse error occurred, you should always use
2003 cp_parser_error_occurred. If the construct is optional (indicated
2004 either by an `_opt' in the name of the function that does the
2005 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2006 the construct is not present. */
2008 /* Lexical conventions [gram.lex] */
2010 static cp_expr cp_parser_identifier
2012 static cp_expr cp_parser_string_literal
2013 (cp_parser
*, bool, bool, bool);
2014 static cp_expr cp_parser_userdef_char_literal
2016 static tree cp_parser_userdef_string_literal
2018 static cp_expr cp_parser_userdef_numeric_literal
2021 /* Basic concepts [gram.basic] */
2023 static void cp_parser_translation_unit (cp_parser
*);
2025 /* Expressions [gram.expr] */
2027 static cp_expr cp_parser_primary_expression
2028 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2029 static cp_expr cp_parser_id_expression
2030 (cp_parser
*, bool, bool, bool *, bool, bool);
2031 static cp_expr cp_parser_unqualified_id
2032 (cp_parser
*, bool, bool, bool, bool);
2033 static tree cp_parser_nested_name_specifier_opt
2034 (cp_parser
*, bool, bool, bool, bool, bool = false);
2035 static tree cp_parser_nested_name_specifier
2036 (cp_parser
*, bool, bool, bool, bool);
2037 static tree cp_parser_qualifying_entity
2038 (cp_parser
*, bool, bool, bool, bool, bool);
2039 static cp_expr cp_parser_postfix_expression
2040 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2041 static tree cp_parser_postfix_open_square_expression
2042 (cp_parser
*, tree
, bool, bool);
2043 static tree cp_parser_postfix_dot_deref_expression
2044 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2045 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2046 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2048 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2049 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2050 static void cp_parser_pseudo_destructor_name
2051 (cp_parser
*, tree
, tree
*, tree
*);
2052 static cp_expr cp_parser_unary_expression
2053 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2054 static enum tree_code cp_parser_unary_operator
2056 static tree cp_parser_has_attribute_expression
2058 static tree cp_parser_new_expression
2060 static vec
<tree
, va_gc
> *cp_parser_new_placement
2062 static tree cp_parser_new_type_id
2063 (cp_parser
*, tree
*);
2064 static cp_declarator
*cp_parser_new_declarator_opt
2066 static cp_declarator
*cp_parser_direct_new_declarator
2068 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2070 static tree cp_parser_delete_expression
2072 static cp_expr cp_parser_cast_expression
2073 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2074 static cp_expr cp_parser_binary_expression
2075 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2076 static tree cp_parser_question_colon_clause
2077 (cp_parser
*, cp_expr
);
2078 static cp_expr cp_parser_assignment_expression
2079 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2080 static enum tree_code cp_parser_assignment_operator_opt
2082 static cp_expr cp_parser_expression
2083 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2084 static cp_expr cp_parser_constant_expression
2085 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2086 static cp_expr cp_parser_builtin_offsetof
2088 static cp_expr cp_parser_lambda_expression
2090 static void cp_parser_lambda_introducer
2091 (cp_parser
*, tree
);
2092 static bool cp_parser_lambda_declarator_opt
2093 (cp_parser
*, tree
);
2094 static void cp_parser_lambda_body
2095 (cp_parser
*, tree
);
2097 /* Statements [gram.stmt.stmt] */
2099 static void cp_parser_statement
2100 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2101 static void cp_parser_label_for_labeled_statement
2102 (cp_parser
*, tree
);
2103 static tree cp_parser_expression_statement
2104 (cp_parser
*, tree
);
2105 static tree cp_parser_compound_statement
2106 (cp_parser
*, tree
, int, bool);
2107 static void cp_parser_statement_seq_opt
2108 (cp_parser
*, tree
);
2109 static tree cp_parser_selection_statement
2110 (cp_parser
*, bool *, vec
<tree
> *);
2111 static tree cp_parser_condition
2113 static tree cp_parser_iteration_statement
2114 (cp_parser
*, bool *, bool, unsigned short);
2115 static bool cp_parser_init_statement
2116 (cp_parser
*, tree
*decl
);
2117 static tree cp_parser_for
2118 (cp_parser
*, bool, unsigned short);
2119 static tree cp_parser_c_for
2120 (cp_parser
*, tree
, tree
, bool, unsigned short);
2121 static tree cp_parser_range_for
2122 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2123 static void do_range_for_auto_deduction
2125 static tree cp_parser_perform_range_for_lookup
2126 (tree
, tree
*, tree
*);
2127 static tree cp_parser_range_for_member_function
2129 static tree cp_parser_jump_statement
2131 static void cp_parser_declaration_statement
2134 static tree cp_parser_implicitly_scoped_statement
2135 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2136 static void cp_parser_already_scoped_statement
2137 (cp_parser
*, bool *, const token_indent_info
&);
2139 /* Declarations [gram.dcl.dcl] */
2141 static void cp_parser_declaration_seq_opt
2143 static void cp_parser_declaration
2145 static void cp_parser_toplevel_declaration
2147 static void cp_parser_block_declaration
2148 (cp_parser
*, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser
*, bool, tree
*);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser
*, cp_decl_specifier_seq
*);
2157 static tree cp_parser_type_specifier
2158 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2162 static tree cp_parser_type_name
2163 (cp_parser
*, bool);
2164 static tree cp_parser_nonclass_name
2165 (cp_parser
* parser
);
2166 static tree cp_parser_elaborated_type_specifier
2167 (cp_parser
*, bool, bool);
2168 static tree cp_parser_enum_specifier
2170 static void cp_parser_enumerator_list
2171 (cp_parser
*, tree
);
2172 static void cp_parser_enumerator_definition
2173 (cp_parser
*, tree
);
2174 static tree cp_parser_namespace_name
2176 static void cp_parser_namespace_definition
2178 static void cp_parser_namespace_body
2180 static tree cp_parser_qualified_namespace_specifier
2182 static void cp_parser_namespace_alias_definition
2184 static bool cp_parser_using_declaration
2185 (cp_parser
*, bool);
2186 static void cp_parser_using_directive
2188 static tree cp_parser_alias_declaration
2190 static void cp_parser_asm_definition
2192 static void cp_parser_linkage_specification
2194 static void cp_parser_static_assert
2195 (cp_parser
*, bool);
2196 static tree cp_parser_decltype
2198 static tree cp_parser_decomposition_declaration
2199 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2201 /* Declarators [gram.dcl.decl] */
2203 static tree cp_parser_init_declarator
2204 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*,
2205 vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*,
2206 location_t
*, tree
*);
2207 static cp_declarator
*cp_parser_declarator
2208 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool *,
2210 static cp_declarator
*cp_parser_direct_declarator
2211 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool, bool,
2213 static enum tree_code cp_parser_ptr_operator
2214 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2215 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2217 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2219 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2221 static tree cp_parser_tx_qualifier_opt
2223 static tree cp_parser_late_return_type_opt
2224 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2225 static tree cp_parser_declarator_id
2226 (cp_parser
*, bool);
2227 static tree cp_parser_type_id
2228 (cp_parser
*, cp_parser_flags
= CP_PARSER_FLAGS_NONE
, location_t
* = NULL
);
2229 static tree cp_parser_template_type_arg
2231 static tree
cp_parser_trailing_type_id (cp_parser
*);
2232 static tree cp_parser_type_id_1
2233 (cp_parser
*, cp_parser_flags
, bool, bool, location_t
*);
2234 static void cp_parser_type_specifier_seq
2235 (cp_parser
*, cp_parser_flags
, bool, bool, cp_decl_specifier_seq
*);
2236 static tree cp_parser_parameter_declaration_clause
2237 (cp_parser
*, cp_parser_flags
);
2238 static tree cp_parser_parameter_declaration_list
2239 (cp_parser
*, cp_parser_flags
);
2240 static cp_parameter_declarator
*cp_parser_parameter_declaration
2241 (cp_parser
*, cp_parser_flags
, bool, bool *);
2242 static tree cp_parser_default_argument
2243 (cp_parser
*, bool);
2244 static void cp_parser_function_body
2245 (cp_parser
*, bool);
2246 static tree cp_parser_initializer
2247 (cp_parser
*, bool *, bool *, bool = false);
2248 static cp_expr cp_parser_initializer_clause
2249 (cp_parser
*, bool *);
2250 static cp_expr cp_parser_braced_list
2251 (cp_parser
*, bool*);
2252 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2253 (cp_parser
*, bool *);
2255 static void cp_parser_ctor_initializer_opt_and_function_body
2256 (cp_parser
*, bool);
2258 static tree cp_parser_late_parsing_omp_declare_simd
2259 (cp_parser
*, tree
);
2261 static tree cp_parser_late_parsing_oacc_routine
2262 (cp_parser
*, tree
);
2264 static tree synthesize_implicit_template_parm
2265 (cp_parser
*, tree
);
2266 static tree finish_fully_implicit_template
2267 (cp_parser
*, tree
);
2268 static void abort_fully_implicit_template
2271 /* Classes [gram.class] */
2273 static tree cp_parser_class_name
2274 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2275 static tree cp_parser_class_specifier
2277 static tree cp_parser_class_head
2278 (cp_parser
*, bool *);
2279 static enum tag_types cp_parser_class_key
2281 static void cp_parser_type_parameter_key
2282 (cp_parser
* parser
);
2283 static void cp_parser_member_specification_opt
2285 static void cp_parser_member_declaration
2287 static tree cp_parser_pure_specifier
2289 static tree cp_parser_constant_initializer
2292 /* Derived classes [gram.class.derived] */
2294 static tree cp_parser_base_clause
2296 static tree cp_parser_base_specifier
2299 /* Special member functions [gram.special] */
2301 static tree cp_parser_conversion_function_id
2303 static tree cp_parser_conversion_type_id
2305 static cp_declarator
*cp_parser_conversion_declarator_opt
2307 static void cp_parser_ctor_initializer_opt
2309 static void cp_parser_mem_initializer_list
2311 static tree cp_parser_mem_initializer
2313 static tree cp_parser_mem_initializer_id
2316 /* Overloading [gram.over] */
2318 static cp_expr cp_parser_operator_function_id
2320 static cp_expr cp_parser_operator
2321 (cp_parser
*, location_t
);
2323 /* Templates [gram.temp] */
2325 static void cp_parser_template_declaration
2326 (cp_parser
*, bool);
2327 static tree cp_parser_template_parameter_list
2329 static tree cp_parser_template_parameter
2330 (cp_parser
*, bool *, bool *);
2331 static tree cp_parser_type_parameter
2332 (cp_parser
*, bool *);
2333 static tree cp_parser_template_id
2334 (cp_parser
*, bool, bool, enum tag_types
, bool);
2335 static tree cp_parser_template_name
2336 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2337 static tree cp_parser_template_argument_list
2339 static tree cp_parser_template_argument
2341 static void cp_parser_explicit_instantiation
2343 static void cp_parser_explicit_specialization
2346 /* Exception handling [gram.exception] */
2348 static tree cp_parser_try_block
2350 static void cp_parser_function_try_block
2352 static void cp_parser_handler_seq
2354 static void cp_parser_handler
2356 static tree cp_parser_exception_declaration
2358 static tree cp_parser_throw_expression
2360 static tree cp_parser_exception_specification_opt
2362 static tree cp_parser_type_id_list
2365 /* GNU Extensions */
2367 static tree cp_parser_asm_specification_opt
2369 static tree cp_parser_asm_operand_list
2371 static tree cp_parser_asm_clobber_list
2373 static tree cp_parser_asm_label_list
2375 static bool cp_next_tokens_can_be_attribute_p
2377 static bool cp_next_tokens_can_be_gnu_attribute_p
2379 static bool cp_next_tokens_can_be_std_attribute_p
2381 static bool cp_nth_tokens_can_be_std_attribute_p
2382 (cp_parser
*, size_t);
2383 static bool cp_nth_tokens_can_be_gnu_attribute_p
2384 (cp_parser
*, size_t);
2385 static bool cp_nth_tokens_can_be_attribute_p
2386 (cp_parser
*, size_t);
2387 static tree cp_parser_attributes_opt
2389 static tree cp_parser_gnu_attributes_opt
2391 static tree cp_parser_gnu_attribute_list
2392 (cp_parser
*, bool = false);
2393 static tree cp_parser_std_attribute
2394 (cp_parser
*, tree
);
2395 static tree cp_parser_std_attribute_spec
2397 static tree cp_parser_std_attribute_spec_seq
2399 static size_t cp_parser_skip_attributes_opt
2400 (cp_parser
*, size_t);
2401 static bool cp_parser_extension_opt
2402 (cp_parser
*, int *);
2403 static void cp_parser_label_declaration
2406 /* Concept Extensions */
2408 static tree cp_parser_requires_clause
2410 static tree cp_parser_requires_clause_opt
2412 static tree cp_parser_requires_expression
2414 static tree cp_parser_requirement_parameter_list
2416 static tree cp_parser_requirement_body
2418 static tree cp_parser_requirement_list
2420 static tree cp_parser_requirement
2422 static tree cp_parser_simple_requirement
2424 static tree cp_parser_compound_requirement
2426 static tree cp_parser_type_requirement
2428 static tree cp_parser_nested_requirement
2431 /* Transactional Memory Extensions */
2433 static tree cp_parser_transaction
2434 (cp_parser
*, cp_token
*);
2435 static tree cp_parser_transaction_expression
2436 (cp_parser
*, enum rid
);
2437 static void cp_parser_function_transaction
2438 (cp_parser
*, enum rid
);
2439 static tree cp_parser_transaction_cancel
2442 enum pragma_context
{
2449 static bool cp_parser_pragma
2450 (cp_parser
*, enum pragma_context
, bool *);
2452 /* Objective-C++ Productions */
2454 static tree cp_parser_objc_message_receiver
2456 static tree cp_parser_objc_message_args
2458 static tree cp_parser_objc_message_expression
2460 static cp_expr cp_parser_objc_encode_expression
2462 static tree cp_parser_objc_defs_expression
2464 static tree cp_parser_objc_protocol_expression
2466 static tree cp_parser_objc_selector_expression
2468 static cp_expr cp_parser_objc_expression
2470 static bool cp_parser_objc_selector_p
2472 static tree cp_parser_objc_selector
2474 static tree cp_parser_objc_protocol_refs_opt
2476 static void cp_parser_objc_declaration
2477 (cp_parser
*, tree
);
2478 static tree cp_parser_objc_statement
2480 static bool cp_parser_objc_valid_prefix_attributes
2481 (cp_parser
*, tree
*);
2482 static void cp_parser_objc_at_property_declaration
2484 static void cp_parser_objc_at_synthesize_declaration
2486 static void cp_parser_objc_at_dynamic_declaration
2488 static tree cp_parser_objc_struct_declaration
2491 /* Utility Routines */
2493 static cp_expr cp_parser_lookup_name
2494 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2495 static tree cp_parser_lookup_name_simple
2496 (cp_parser
*, tree
, location_t
);
2497 static tree cp_parser_maybe_treat_template_as_class
2499 static bool cp_parser_check_declarator_template_parameters
2500 (cp_parser
*, cp_declarator
*, location_t
);
2501 static bool cp_parser_check_template_parameters
2502 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2503 static cp_expr cp_parser_simple_cast_expression
2505 static tree cp_parser_global_scope_opt
2506 (cp_parser
*, bool);
2507 static bool cp_parser_constructor_declarator_p
2508 (cp_parser
*, bool);
2509 static tree cp_parser_function_definition_from_specifiers_and_declarator
2510 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2511 static tree cp_parser_function_definition_after_declarator
2512 (cp_parser
*, bool);
2513 static bool cp_parser_template_declaration_after_export
2514 (cp_parser
*, bool);
2515 static void cp_parser_perform_template_parameter_access_checks
2516 (vec
<deferred_access_check
, va_gc
> *);
2517 static tree cp_parser_single_declaration
2518 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2519 static cp_expr cp_parser_functional_cast
2520 (cp_parser
*, tree
);
2521 static tree cp_parser_save_member_function_body
2522 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2523 static tree cp_parser_save_nsdmi
2525 static tree cp_parser_enclosed_template_argument_list
2527 static void cp_parser_save_default_args
2528 (cp_parser
*, tree
);
2529 static void cp_parser_late_parsing_for_member
2530 (cp_parser
*, tree
);
2531 static tree cp_parser_late_parse_one_default_arg
2532 (cp_parser
*, tree
, tree
, tree
);
2533 static void cp_parser_late_parsing_nsdmi
2534 (cp_parser
*, tree
);
2535 static void cp_parser_late_parsing_default_args
2536 (cp_parser
*, tree
);
2537 static tree cp_parser_sizeof_operand
2538 (cp_parser
*, enum rid
);
2539 static cp_expr cp_parser_trait_expr
2540 (cp_parser
*, enum rid
);
2541 static bool cp_parser_declares_only_class_p
2543 static void cp_parser_set_storage_class
2544 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2545 static void cp_parser_set_decl_spec_type
2546 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2547 static void set_and_check_decl_spec_loc
2548 (cp_decl_specifier_seq
*decl_specs
,
2549 cp_decl_spec ds
, cp_token
*);
2550 static bool cp_parser_friend_p
2551 (const cp_decl_specifier_seq
*);
2552 static void cp_parser_required_error
2553 (cp_parser
*, required_token
, bool, location_t
);
2554 static cp_token
*cp_parser_require
2555 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2556 static cp_token
*cp_parser_require_keyword
2557 (cp_parser
*, enum rid
, required_token
);
2558 static bool cp_parser_token_starts_function_definition_p
2560 static bool cp_parser_next_token_starts_class_definition_p
2562 static bool cp_parser_next_token_ends_template_argument_p
2564 static bool cp_parser_nth_token_starts_template_argument_list_p
2565 (cp_parser
*, size_t);
2566 static enum tag_types cp_parser_token_is_class_key
2568 static enum tag_types cp_parser_token_is_type_parameter_key
2570 static void cp_parser_check_class_key
2571 (enum tag_types
, tree type
);
2572 static void cp_parser_check_access_in_redeclaration
2573 (tree type
, location_t location
);
2574 static bool cp_parser_optional_template_keyword
2576 static void cp_parser_pre_parsed_nested_name_specifier
2578 static bool cp_parser_cache_group
2579 (cp_parser
*, enum cpp_ttype
, unsigned);
2580 static tree cp_parser_cache_defarg
2581 (cp_parser
*parser
, bool nsdmi
);
2582 static void cp_parser_parse_tentatively
2584 static void cp_parser_commit_to_tentative_parse
2586 static void cp_parser_commit_to_topmost_tentative_parse
2588 static void cp_parser_abort_tentative_parse
2590 static bool cp_parser_parse_definitely
2592 static inline bool cp_parser_parsing_tentatively
2594 static bool cp_parser_uncommitted_to_tentative_parse_p
2596 static void cp_parser_error
2597 (cp_parser
*, const char *);
2598 static void cp_parser_name_lookup_error
2599 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2600 static bool cp_parser_simulate_error
2602 static bool cp_parser_check_type_definition
2604 static void cp_parser_check_for_definition_in_return_type
2605 (cp_declarator
*, tree
, location_t type_location
);
2606 static void cp_parser_check_for_invalid_template_id
2607 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2608 static bool cp_parser_non_integral_constant_expression
2609 (cp_parser
*, non_integral_constant
);
2610 static void cp_parser_diagnose_invalid_type_name
2611 (cp_parser
*, tree
, location_t
);
2612 static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 static int cp_parser_skip_to_closing_parenthesis
2615 (cp_parser
*, bool, bool, bool);
2616 static void cp_parser_skip_to_end_of_statement
2618 static void cp_parser_consume_semicolon_at_end_of_statement
2620 static void cp_parser_skip_to_end_of_block_or_statement
2622 static bool cp_parser_skip_to_closing_brace
2624 static void cp_parser_skip_to_end_of_template_parameter_list
2626 static void cp_parser_skip_to_pragma_eol
2627 (cp_parser
*, cp_token
*);
2628 static bool cp_parser_error_occurred
2630 static bool cp_parser_allow_gnu_extensions_p
2632 static bool cp_parser_is_pure_string_literal
2634 static bool cp_parser_is_string_literal
2636 static bool cp_parser_is_keyword
2637 (cp_token
*, enum rid
);
2638 static tree cp_parser_make_typename_type
2639 (cp_parser
*, tree
, location_t location
);
2640 static cp_declarator
* cp_parser_make_indirect_declarator
2641 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2642 static bool cp_parser_compound_literal_p
2644 static bool cp_parser_array_designator_p
2646 static bool cp_parser_init_statement_p
2648 static bool cp_parser_skip_to_closing_square_bracket
2651 /* Concept-related syntactic transformations */
2653 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2654 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2656 // -------------------------------------------------------------------------- //
2657 // Unevaluated Operand Guard
2659 // Implementation of an RAII helper for unevaluated operand parsing.
2660 cp_unevaluated::cp_unevaluated ()
2662 ++cp_unevaluated_operand
;
2663 ++c_inhibit_evaluation_warnings
;
2666 cp_unevaluated::~cp_unevaluated ()
2668 --c_inhibit_evaluation_warnings
;
2669 --cp_unevaluated_operand
;
2672 // -------------------------------------------------------------------------- //
2673 // Tentative Parsing
2675 /* Returns nonzero if we are parsing tentatively. */
2678 cp_parser_parsing_tentatively (cp_parser
* parser
)
2680 return parser
->context
->next
!= NULL
;
2683 /* Returns nonzero if TOKEN is a string literal. */
2686 cp_parser_is_pure_string_literal (cp_token
* token
)
2688 return (token
->type
== CPP_STRING
||
2689 token
->type
== CPP_STRING16
||
2690 token
->type
== CPP_STRING32
||
2691 token
->type
== CPP_WSTRING
||
2692 token
->type
== CPP_UTF8STRING
);
2695 /* Returns nonzero if TOKEN is a string literal
2696 of a user-defined string literal. */
2699 cp_parser_is_string_literal (cp_token
* token
)
2701 return (cp_parser_is_pure_string_literal (token
) ||
2702 token
->type
== CPP_STRING_USERDEF
||
2703 token
->type
== CPP_STRING16_USERDEF
||
2704 token
->type
== CPP_STRING32_USERDEF
||
2705 token
->type
== CPP_WSTRING_USERDEF
||
2706 token
->type
== CPP_UTF8STRING_USERDEF
);
2709 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2712 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2714 return token
->keyword
== keyword
;
2717 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2720 static enum pragma_kind
2721 cp_parser_pragma_kind (cp_token
*token
)
2723 if (token
->type
!= CPP_PRAGMA
)
2725 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2726 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2729 /* Helper function for cp_parser_error.
2730 Having peeked a token of kind TOK1_KIND that might signify
2731 a conflict marker, peek successor tokens to determine
2732 if we actually do have a conflict marker.
2733 Specifically, we consider a run of 7 '<', '=' or '>' characters
2734 at the start of a line as a conflict marker.
2735 These come through the lexer as three pairs and a single,
2736 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2737 If it returns true, *OUT_LOC is written to with the location/range
2741 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2742 location_t
*out_loc
)
2744 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2745 if (token2
->type
!= tok1_kind
)
2747 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2748 if (token3
->type
!= tok1_kind
)
2750 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2751 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2754 /* It must be at the start of the line. */
2755 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2756 if (LOCATION_COLUMN (start_loc
) != 1)
2759 /* We have a conflict marker. Construct a location of the form:
2762 with start == caret, finishing at the end of the marker. */
2763 location_t finish_loc
= get_finish (token4
->location
);
2764 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2769 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2773 get_matching_symbol (required_token token_desc
)
2780 case RT_CLOSE_BRACE
:
2782 case RT_CLOSE_PAREN
:
2787 /* Attempt to convert TOKEN_DESC from a required_token to an
2788 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2790 static enum cpp_ttype
2791 get_required_cpp_ttype (required_token token_desc
)
2796 return CPP_SEMICOLON
;
2798 return CPP_OPEN_PAREN
;
2799 case RT_CLOSE_BRACE
:
2800 return CPP_CLOSE_BRACE
;
2802 return CPP_OPEN_BRACE
;
2803 case RT_CLOSE_SQUARE
:
2804 return CPP_CLOSE_SQUARE
;
2805 case RT_OPEN_SQUARE
:
2806 return CPP_OPEN_SQUARE
;
2811 case RT_CLOSE_PAREN
:
2812 return CPP_CLOSE_PAREN
;
2815 /* Use CPP_EOF as a "no completions possible" code. */
2821 /* Subroutine of cp_parser_error and cp_parser_required_error.
2823 Issue a diagnostic of the form
2824 FILE:LINE: MESSAGE before TOKEN
2825 where TOKEN is the next token in the input stream. MESSAGE
2826 (specified by the caller) is usually of the form "expected
2829 This bypasses the check for tentative passing, and potentially
2830 adds material needed by cp_parser_required_error.
2832 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2833 suggesting insertion of the missing token.
2835 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2836 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2840 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2841 required_token missing_token_desc
,
2842 location_t matching_location
)
2844 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2845 /* This diagnostic makes more sense if it is tagged to the line
2846 of the token we just peeked at. */
2847 cp_lexer_set_source_position_from_token (token
);
2849 if (token
->type
== CPP_PRAGMA
)
2851 error_at (token
->location
,
2852 "%<#pragma%> is not allowed here");
2853 cp_parser_skip_to_pragma_eol (parser
, token
);
2857 /* If this is actually a conflict marker, report it as such. */
2858 if (token
->type
== CPP_LSHIFT
2859 || token
->type
== CPP_RSHIFT
2860 || token
->type
== CPP_EQ_EQ
)
2863 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2865 error_at (loc
, "version control conflict marker in file");
2866 expanded_location token_exploc
= expand_location (token
->location
);
2867 /* Consume tokens until the end of the source line. */
2870 cp_lexer_consume_token (parser
->lexer
);
2871 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2874 expanded_location next_exploc
= expand_location (next
->location
);
2875 if (next_exploc
.file
!= token_exploc
.file
)
2877 if (next_exploc
.line
!= token_exploc
.line
)
2884 gcc_rich_location
richloc (input_location
);
2886 bool added_matching_location
= false;
2888 if (missing_token_desc
!= RT_NONE
)
2890 /* Potentially supply a fix-it hint, suggesting to add the
2891 missing token immediately after the *previous* token.
2892 This may move the primary location within richloc. */
2893 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2894 location_t prev_token_loc
2895 = cp_lexer_previous_token (parser
->lexer
)->location
;
2896 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2898 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2899 Attempt to consolidate diagnostics by printing it as a
2900 secondary range within the main diagnostic. */
2901 if (matching_location
!= UNKNOWN_LOCATION
)
2902 added_matching_location
2903 = richloc
.add_location_if_nearby (matching_location
);
2906 /* Actually emit the error. */
2907 c_parse_error (gmsgid
,
2908 /* Because c_parser_error does not understand
2909 CPP_KEYWORD, keywords are treated like
2911 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2912 token
->u
.value
, token
->flags
, &richloc
);
2914 if (missing_token_desc
!= RT_NONE
)
2916 /* If we weren't able to consolidate matching_location, then
2917 print it as a secondary diagnostic. */
2918 if (matching_location
!= UNKNOWN_LOCATION
2919 && !added_matching_location
)
2920 inform (matching_location
, "to match this %qs",
2921 get_matching_symbol (missing_token_desc
));
2925 /* If not parsing tentatively, issue a diagnostic of the form
2926 FILE:LINE: MESSAGE before TOKEN
2927 where TOKEN is the next token in the input stream. MESSAGE
2928 (specified by the caller) is usually of the form "expected
2932 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2934 if (!cp_parser_simulate_error (parser
))
2935 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2938 /* Issue an error about name-lookup failing. NAME is the
2939 IDENTIFIER_NODE DECL is the result of
2940 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2941 the thing that we hoped to find. */
2944 cp_parser_name_lookup_error (cp_parser
* parser
,
2947 name_lookup_error desired
,
2948 location_t location
)
2950 /* If name lookup completely failed, tell the user that NAME was not
2952 if (decl
== error_mark_node
)
2954 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2955 error_at (location
, "%<%E::%E%> has not been declared",
2956 parser
->scope
, name
);
2957 else if (parser
->scope
== global_namespace
)
2958 error_at (location
, "%<::%E%> has not been declared", name
);
2959 else if (parser
->object_scope
2960 && !CLASS_TYPE_P (parser
->object_scope
))
2961 error_at (location
, "request for member %qE in non-class type %qT",
2962 name
, parser
->object_scope
);
2963 else if (parser
->object_scope
)
2964 error_at (location
, "%<%T::%E%> has not been declared",
2965 parser
->object_scope
, name
);
2967 error_at (location
, "%qE has not been declared", name
);
2969 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2974 error_at (location
, "%<%E::%E%> is not a type",
2975 parser
->scope
, name
);
2978 error_at (location
, "%<%E::%E%> is not a class or namespace",
2979 parser
->scope
, name
);
2983 "%<%E::%E%> is not a class, namespace, or enumeration",
2984 parser
->scope
, name
);
2991 else if (parser
->scope
== global_namespace
)
2996 error_at (location
, "%<::%E%> is not a type", name
);
2999 error_at (location
, "%<::%E%> is not a class or namespace", name
);
3003 "%<::%E%> is not a class, namespace, or enumeration",
3015 error_at (location
, "%qE is not a type", name
);
3018 error_at (location
, "%qE is not a class or namespace", name
);
3022 "%qE is not a class, namespace, or enumeration", name
);
3030 /* If we are parsing tentatively, remember that an error has occurred
3031 during this tentative parse. Returns true if the error was
3032 simulated; false if a message should be issued by the caller. */
3035 cp_parser_simulate_error (cp_parser
* parser
)
3037 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3039 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3045 /* This function is called when a type is defined. If type
3046 definitions are forbidden at this point, an error message is
3050 cp_parser_check_type_definition (cp_parser
* parser
)
3052 /* If types are forbidden here, issue a message. */
3053 if (parser
->type_definition_forbidden_message
)
3055 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3056 in the message need to be interpreted. */
3057 error (parser
->type_definition_forbidden_message
);
3063 /* This function is called when the DECLARATOR is processed. The TYPE
3064 was a type defined in the decl-specifiers. If it is invalid to
3065 define a type in the decl-specifiers for DECLARATOR, an error is
3066 issued. TYPE_LOCATION is the location of TYPE and is used
3067 for error reporting. */
3070 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3071 tree type
, location_t type_location
)
3073 /* [dcl.fct] forbids type definitions in return types.
3074 Unfortunately, it's not easy to know whether or not we are
3075 processing a return type until after the fact. */
3077 && (declarator
->kind
== cdk_pointer
3078 || declarator
->kind
== cdk_reference
3079 || declarator
->kind
== cdk_ptrmem
))
3080 declarator
= declarator
->declarator
;
3082 && declarator
->kind
== cdk_function
)
3084 error_at (type_location
,
3085 "new types may not be defined in a return type");
3086 inform (type_location
,
3087 "(perhaps a semicolon is missing after the definition of %qT)",
3092 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3093 "<" in any valid C++ program. If the next token is indeed "<",
3094 issue a message warning the user about what appears to be an
3095 invalid attempt to form a template-id. LOCATION is the location
3096 of the type-specifier (TYPE) */
3099 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3101 enum tag_types tag_type
,
3102 location_t location
)
3104 cp_token_position start
= 0;
3106 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3108 if (TREE_CODE (type
) == TYPE_DECL
)
3109 type
= TREE_TYPE (type
);
3110 if (TYPE_P (type
) && !template_placeholder_p (type
))
3111 error_at (location
, "%qT is not a template", type
);
3112 else if (identifier_p (type
))
3114 if (tag_type
!= none_type
)
3115 error_at (location
, "%qE is not a class template", type
);
3117 error_at (location
, "%qE is not a template", type
);
3120 error_at (location
, "invalid template-id");
3121 /* Remember the location of the invalid "<". */
3122 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3123 start
= cp_lexer_token_position (parser
->lexer
, true);
3124 /* Consume the "<". */
3125 cp_lexer_consume_token (parser
->lexer
);
3126 /* Parse the template arguments. */
3127 cp_parser_enclosed_template_argument_list (parser
);
3128 /* Permanently remove the invalid template arguments so that
3129 this error message is not issued again. */
3131 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3135 /* If parsing an integral constant-expression, issue an error message
3136 about the fact that THING appeared and return true. Otherwise,
3137 return false. In either case, set
3138 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3141 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3142 non_integral_constant thing
)
3144 parser
->non_integral_constant_expression_p
= true;
3145 if (parser
->integral_constant_expression_p
)
3147 if (!parser
->allow_non_integral_constant_expression_p
)
3149 const char *msg
= NULL
;
3153 pedwarn (input_location
, OPT_Wpedantic
,
3154 "ISO C++ forbids using a floating-point literal "
3155 "in a constant-expression");
3158 error ("a cast to a type other than an integral or "
3159 "enumeration type cannot appear in a "
3160 "constant-expression");
3163 error ("%<typeid%> operator "
3164 "cannot appear in a constant-expression");
3167 error ("non-constant compound literals "
3168 "cannot appear in a constant-expression");
3171 error ("a function call "
3172 "cannot appear in a constant-expression");
3175 error ("an increment "
3176 "cannot appear in a constant-expression");
3179 error ("an decrement "
3180 "cannot appear in a constant-expression");
3183 error ("an array reference "
3184 "cannot appear in a constant-expression");
3186 case NIC_ADDR_LABEL
:
3187 error ("the address of a label "
3188 "cannot appear in a constant-expression");
3190 case NIC_OVERLOADED
:
3191 error ("calls to overloaded operators "
3192 "cannot appear in a constant-expression");
3194 case NIC_ASSIGNMENT
:
3195 error ("an assignment cannot appear in a constant-expression");
3198 error ("a comma operator "
3199 "cannot appear in a constant-expression");
3201 case NIC_CONSTRUCTOR
:
3202 error ("a call to a constructor "
3203 "cannot appear in a constant-expression");
3205 case NIC_TRANSACTION
:
3206 error ("a transaction expression "
3207 "cannot appear in a constant-expression");
3213 msg
= "__FUNCTION__";
3215 case NIC_PRETTY_FUNC
:
3216 msg
= "__PRETTY_FUNCTION__";
3236 case NIC_PREINCREMENT
:
3239 case NIC_PREDECREMENT
:
3252 error ("%qs cannot appear in a constant-expression", msg
);
3259 /* Emit a diagnostic for an invalid type name. This function commits
3260 to the current active tentative parse, if any. (Otherwise, the
3261 problematic construct might be encountered again later, resulting
3262 in duplicate error messages.) LOCATION is the location of ID. */
3265 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3266 location_t location
)
3268 tree decl
, ambiguous_decls
;
3269 cp_parser_commit_to_tentative_parse (parser
);
3270 /* Try to lookup the identifier. */
3271 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3272 /*is_template=*/false,
3273 /*is_namespace=*/false,
3274 /*check_dependency=*/true,
3275 &ambiguous_decls
, location
);
3276 if (ambiguous_decls
)
3277 /* If the lookup was ambiguous, an error will already have
3280 /* If the lookup found a template-name, it means that the user forgot
3281 to specify an argument list. Emit a useful error message. */
3282 if (DECL_TYPE_TEMPLATE_P (decl
))
3284 auto_diagnostic_group d
;
3286 "invalid use of template-name %qE without an argument list",
3288 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3289 inform (location
, "class template argument deduction is only available "
3290 "with -std=c++17 or -std=gnu++17");
3291 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3293 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3294 error_at (location
, "invalid use of destructor %qD as a type", id
);
3295 else if (TREE_CODE (decl
) == TYPE_DECL
)
3296 /* Something like 'unsigned A a;' */
3297 error_at (location
, "invalid combination of multiple type-specifiers");
3298 else if (!parser
->scope
)
3300 /* Issue an error message. */
3301 auto_diagnostic_group d
;
3303 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3304 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3305 if (const char *suggestion
= hint
.suggestion ())
3307 gcc_rich_location
richloc (location
);
3308 richloc
.add_fixit_replace (suggestion
);
3310 "%qE does not name a type; did you mean %qs?",
3314 error_at (location
, "%qE does not name a type", id
);
3315 /* If we're in a template class, it's possible that the user was
3316 referring to a type from a base class. For example:
3318 template <typename T> struct A { typedef T X; };
3319 template <typename T> struct B : public A<T> { X x; };
3321 The user should have said "typename A<T>::X". */
3322 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3323 inform (location
, "C++11 %<constexpr%> only available with "
3324 "-std=c++11 or -std=gnu++11");
3325 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3326 inform (location
, "C++11 %<noexcept%> only available with "
3327 "-std=c++11 or -std=gnu++11");
3328 else if (cxx_dialect
< cxx11
3329 && TREE_CODE (id
) == IDENTIFIER_NODE
3330 && id_equal (id
, "thread_local"))
3331 inform (location
, "C++11 %<thread_local%> only available with "
3332 "-std=c++11 or -std=gnu++11");
3333 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3334 inform (location
, "%<concept%> only available with -fconcepts");
3335 else if (processing_template_decl
&& current_class_type
3336 && TYPE_BINFO (current_class_type
))
3340 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3344 tree base_type
= BINFO_TYPE (b
);
3345 if (CLASS_TYPE_P (base_type
)
3346 && dependent_type_p (base_type
))
3349 /* Go from a particular instantiation of the
3350 template (which will have an empty TYPE_FIELDs),
3351 to the main version. */
3352 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3353 for (field
= TYPE_FIELDS (base_type
);
3355 field
= DECL_CHAIN (field
))
3356 if (TREE_CODE (field
) == TYPE_DECL
3357 && DECL_NAME (field
) == id
)
3360 "(perhaps %<typename %T::%E%> was intended)",
3361 BINFO_TYPE (b
), id
);
3370 /* Here we diagnose qualified-ids where the scope is actually correct,
3371 but the identifier does not resolve to a valid type name. */
3372 else if (parser
->scope
!= error_mark_node
)
3374 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3376 auto_diagnostic_group d
;
3378 if (decl
== error_mark_node
)
3379 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3381 const char *suggestion
= hint
.suggestion ();
3382 gcc_rich_location
richloc (location_of (id
));
3384 richloc
.add_fixit_replace (suggestion
);
3385 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3389 "%qE in namespace %qE does not name a template"
3390 " type; did you mean %qs?",
3391 id
, parser
->scope
, suggestion
);
3394 "%qE in namespace %qE does not name a template type",
3397 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3401 "%qE in namespace %qE does not name a template"
3402 " type; did you mean %qs?",
3403 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3406 "%qE in namespace %qE does not name a template"
3408 TREE_OPERAND (id
, 0), parser
->scope
);
3414 "%qE in namespace %qE does not name a type"
3415 "; did you mean %qs?",
3416 id
, parser
->scope
, suggestion
);
3419 "%qE in namespace %qE does not name a type",
3423 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3425 else if (CLASS_TYPE_P (parser
->scope
)
3426 && constructor_name_p (id
, parser
->scope
))
3429 auto_diagnostic_group d
;
3430 error_at (location
, "%<%T::%E%> names the constructor, not"
3431 " the type", parser
->scope
, id
);
3432 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3433 error_at (location
, "and %qT has no template constructors",
3436 else if (TYPE_P (parser
->scope
)
3437 && dependent_scope_p (parser
->scope
))
3439 gcc_rich_location
richloc (location
);
3440 richloc
.add_fixit_insert_before ("typename ");
3441 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3443 "need %<typename%> before %<%T::%D::%E%> because "
3444 "%<%T::%D%> is a dependent scope",
3445 TYPE_CONTEXT (parser
->scope
),
3446 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3448 TYPE_CONTEXT (parser
->scope
),
3449 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3451 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3452 "%qT is a dependent scope",
3453 parser
->scope
, id
, parser
->scope
);
3455 else if (TYPE_P (parser
->scope
))
3457 auto_diagnostic_group d
;
3458 if (!COMPLETE_TYPE_P (parser
->scope
))
3459 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3461 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3462 error_at (location_of (id
),
3463 "%qE in %q#T does not name a template type",
3465 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3466 error_at (location_of (id
),
3467 "%qE in %q#T does not name a template type",
3468 TREE_OPERAND (id
, 0), parser
->scope
);
3470 error_at (location_of (id
),
3471 "%qE in %q#T does not name a type",
3474 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3481 /* Check for a common situation where a type-name should be present,
3482 but is not, and issue a sensible error message. Returns true if an
3483 invalid type-name was detected.
3485 The situation handled by this function are variable declarations of the
3486 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3487 Usually, `ID' should name a type, but if we got here it means that it
3488 does not. We try to emit the best possible error message depending on
3489 how exactly the id-expression looks like. */
3492 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3495 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3497 /* Avoid duplicate error about ambiguous lookup. */
3498 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3500 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3501 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3505 cp_parser_parse_tentatively (parser
);
3506 id
= cp_parser_id_expression (parser
,
3507 /*template_keyword_p=*/false,
3508 /*check_dependency_p=*/true,
3509 /*template_p=*/NULL
,
3510 /*declarator_p=*/false,
3511 /*optional_p=*/false);
3512 /* If the next token is a (, this is a function with no explicit return
3513 type, i.e. constructor, destructor or conversion op. */
3514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3515 || TREE_CODE (id
) == TYPE_DECL
)
3517 cp_parser_abort_tentative_parse (parser
);
3520 if (!cp_parser_parse_definitely (parser
))
3523 /* Emit a diagnostic for the invalid type. */
3524 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3526 /* If we aren't in the middle of a declarator (i.e. in a
3527 parameter-declaration-clause), skip to the end of the declaration;
3528 there's no point in trying to process it. */
3529 if (!parser
->in_declarator_p
)
3530 cp_parser_skip_to_end_of_block_or_statement (parser
);
3534 /* Consume tokens up to, and including, the next non-nested closing `)'.
3535 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3536 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3537 found an unnested token of that type. */
3540 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3545 unsigned paren_depth
= 0;
3546 unsigned brace_depth
= 0;
3547 unsigned square_depth
= 0;
3548 unsigned condop_depth
= 0;
3550 if (recovering
&& or_ttype
== CPP_EOF
3551 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3556 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3558 /* Have we found what we're looking for before the closing paren? */
3559 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3560 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3563 switch (token
->type
)
3565 case CPP_PRAGMA_EOL
:
3566 if (!parser
->lexer
->in_pragma
)
3570 /* If we've run out of tokens, then there is no closing `)'. */
3573 /* This is good for lambda expression capture-lists. */
3574 case CPP_OPEN_SQUARE
:
3577 case CPP_CLOSE_SQUARE
:
3578 if (!square_depth
--)
3583 /* This matches the processing in skip_to_end_of_statement. */
3588 case CPP_OPEN_BRACE
:
3591 case CPP_CLOSE_BRACE
:
3596 case CPP_OPEN_PAREN
:
3601 case CPP_CLOSE_PAREN
:
3602 if (!brace_depth
&& !paren_depth
--)
3605 cp_lexer_consume_token (parser
->lexer
);
3611 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3616 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3624 /* Consume the token. */
3625 cp_lexer_consume_token (parser
->lexer
);
3629 /* Consume tokens up to, and including, the next non-nested closing `)'.
3630 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3631 are doing error recovery. Returns -1 if OR_COMMA is true and we
3632 found an unnested token of that type. */
3635 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3640 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3641 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3642 ttype
, consume_paren
);
3645 /* Consume tokens until we reach the end of the current statement.
3646 Normally, that will be just before consuming a `;'. However, if a
3647 non-nested `}' comes first, then we stop before consuming that. */
3650 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3652 unsigned nesting_depth
= 0;
3654 /* Unwind generic function template scope if necessary. */
3655 if (parser
->fully_implicit_function_template_p
)
3656 abort_fully_implicit_template (parser
);
3660 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3662 switch (token
->type
)
3664 case CPP_PRAGMA_EOL
:
3665 if (!parser
->lexer
->in_pragma
)
3669 /* If we've run out of tokens, stop. */
3673 /* If the next token is a `;', we have reached the end of the
3679 case CPP_CLOSE_BRACE
:
3680 /* If this is a non-nested '}', stop before consuming it.
3681 That way, when confronted with something like:
3685 we stop before consuming the closing '}', even though we
3686 have not yet reached a `;'. */
3687 if (nesting_depth
== 0)
3690 /* If it is the closing '}' for a block that we have
3691 scanned, stop -- but only after consuming the token.
3697 we will stop after the body of the erroneously declared
3698 function, but before consuming the following `typedef'
3700 if (--nesting_depth
== 0)
3702 cp_lexer_consume_token (parser
->lexer
);
3707 case CPP_OPEN_BRACE
:
3715 /* Consume the token. */
3716 cp_lexer_consume_token (parser
->lexer
);
3720 /* This function is called at the end of a statement or declaration.
3721 If the next token is a semicolon, it is consumed; otherwise, error
3722 recovery is attempted. */
3725 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3727 /* Look for the trailing `;'. */
3728 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3730 /* If there is additional (erroneous) input, skip to the end of
3732 cp_parser_skip_to_end_of_statement (parser
);
3733 /* If the next token is now a `;', consume it. */
3734 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3735 cp_lexer_consume_token (parser
->lexer
);
3739 /* Skip tokens until we have consumed an entire block, or until we
3740 have consumed a non-nested `;'. */
3743 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3745 int nesting_depth
= 0;
3747 /* Unwind generic function template scope if necessary. */
3748 if (parser
->fully_implicit_function_template_p
)
3749 abort_fully_implicit_template (parser
);
3751 while (nesting_depth
>= 0)
3753 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3755 switch (token
->type
)
3757 case CPP_PRAGMA_EOL
:
3758 if (!parser
->lexer
->in_pragma
)
3762 /* If we've run out of tokens, stop. */
3766 /* Stop if this is an unnested ';'. */
3771 case CPP_CLOSE_BRACE
:
3772 /* Stop if this is an unnested '}', or closes the outermost
3775 if (nesting_depth
< 0)
3781 case CPP_OPEN_BRACE
:
3790 /* Consume the token. */
3791 cp_lexer_consume_token (parser
->lexer
);
3795 /* Skip tokens until a non-nested closing curly brace is the next
3796 token, or there are no more tokens. Return true in the first case,
3800 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3802 unsigned nesting_depth
= 0;
3806 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3808 switch (token
->type
)
3810 case CPP_PRAGMA_EOL
:
3811 if (!parser
->lexer
->in_pragma
)
3815 /* If we've run out of tokens, stop. */
3818 case CPP_CLOSE_BRACE
:
3819 /* If the next token is a non-nested `}', then we have reached
3820 the end of the current block. */
3821 if (nesting_depth
-- == 0)
3825 case CPP_OPEN_BRACE
:
3826 /* If it the next token is a `{', then we are entering a new
3827 block. Consume the entire block. */
3835 /* Consume the token. */
3836 cp_lexer_consume_token (parser
->lexer
);
3840 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3841 parameter is the PRAGMA token, allowing us to purge the entire pragma
3845 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3849 parser
->lexer
->in_pragma
= false;
3852 token
= cp_lexer_consume_token (parser
->lexer
);
3853 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3855 /* Ensure that the pragma is not parsed again. */
3856 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3859 /* Require pragma end of line, resyncing with it as necessary. The
3860 arguments are as for cp_parser_skip_to_pragma_eol. */
3863 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3865 parser
->lexer
->in_pragma
= false;
3866 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3867 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3870 /* This is a simple wrapper around make_typename_type. When the id is
3871 an unresolved identifier node, we can provide a superior diagnostic
3872 using cp_parser_diagnose_invalid_type_name. */
3875 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3876 location_t id_location
)
3879 if (identifier_p (id
))
3881 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3882 /*complain=*/tf_none
);
3883 if (result
== error_mark_node
)
3884 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3887 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3890 /* This is a wrapper around the
3891 make_{pointer,ptrmem,reference}_declarator functions that decides
3892 which one to call based on the CODE and CLASS_TYPE arguments. The
3893 CODE argument should be one of the values returned by
3894 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3895 appertain to the pointer or reference. */
3897 static cp_declarator
*
3898 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3899 cp_cv_quals cv_qualifiers
,
3900 cp_declarator
*target
,
3903 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3904 return cp_error_declarator
;
3906 if (code
== INDIRECT_REF
)
3907 if (class_type
== NULL_TREE
)
3908 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3910 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3911 target
, attributes
);
3912 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3913 return make_reference_declarator (cv_qualifiers
, target
,
3915 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3916 return make_reference_declarator (cv_qualifiers
, target
,
3921 /* Create a new C++ parser. */
3924 cp_parser_new (void)
3930 /* cp_lexer_new_main is called before doing GC allocation because
3931 cp_lexer_new_main might load a PCH file. */
3932 lexer
= cp_lexer_new_main ();
3934 /* Initialize the binops_by_token so that we can get the tree
3935 directly from the token. */
3936 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3937 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3939 parser
= ggc_cleared_alloc
<cp_parser
> ();
3940 parser
->lexer
= lexer
;
3941 parser
->context
= cp_parser_context_new (NULL
);
3943 /* For now, we always accept GNU extensions. */
3944 parser
->allow_gnu_extensions_p
= 1;
3946 /* The `>' token is a greater-than operator, not the end of a
3948 parser
->greater_than_is_operator_p
= true;
3950 parser
->default_arg_ok_p
= true;
3952 /* We are not parsing a constant-expression. */
3953 parser
->integral_constant_expression_p
= false;
3954 parser
->allow_non_integral_constant_expression_p
= false;
3955 parser
->non_integral_constant_expression_p
= false;
3957 /* Local variable names are not forbidden. */
3958 parser
->local_variables_forbidden_p
= 0;
3960 /* We are not processing an `extern "C"' declaration. */
3961 parser
->in_unbraced_linkage_specification_p
= false;
3963 /* We are not processing a declarator. */
3964 parser
->in_declarator_p
= false;
3966 /* We are not processing a template-argument-list. */
3967 parser
->in_template_argument_list_p
= false;
3969 /* We are not in an iteration statement. */
3970 parser
->in_statement
= 0;
3972 /* We are not in a switch statement. */
3973 parser
->in_switch_statement_p
= false;
3975 /* We are not parsing a type-id inside an expression. */
3976 parser
->in_type_id_in_expr_p
= false;
3978 /* String literals should be translated to the execution character set. */
3979 parser
->translate_strings_p
= true;
3981 /* We are not parsing a function body. */
3982 parser
->in_function_body
= false;
3984 /* We can correct until told otherwise. */
3985 parser
->colon_corrects_to_scope_p
= true;
3987 /* The unparsed function queue is empty. */
3988 push_unparsed_function_queues (parser
);
3990 /* There are no classes being defined. */
3991 parser
->num_classes_being_defined
= 0;
3993 /* No template parameters apply. */
3994 parser
->num_template_parameter_lists
= 0;
3996 /* Special parsing data structures. */
3997 parser
->omp_declare_simd
= NULL
;
3998 parser
->oacc_routine
= NULL
;
4000 /* Not declaring an implicit function template. */
4001 parser
->auto_is_implicit_function_template_parm_p
= false;
4002 parser
->fully_implicit_function_template_p
= false;
4003 parser
->implicit_template_parms
= 0;
4004 parser
->implicit_template_scope
= 0;
4006 /* Allow constrained-type-specifiers. */
4007 parser
->prevent_constrained_type_specifiers
= 0;
4009 /* We haven't yet seen an 'extern "C"'. */
4010 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
4015 /* Create a cp_lexer structure which will emit the tokens in CACHE
4016 and push it onto the parser's lexer stack. This is used for delayed
4017 parsing of in-class method bodies and default arguments, and should
4018 not be confused with tentative parsing. */
4020 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4022 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4023 lexer
->next
= parser
->lexer
;
4024 parser
->lexer
= lexer
;
4026 /* Move the current source position to that of the first token in the
4028 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4031 /* Pop the top lexer off the parser stack. This is never used for the
4032 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4034 cp_parser_pop_lexer (cp_parser
*parser
)
4036 cp_lexer
*lexer
= parser
->lexer
;
4037 parser
->lexer
= lexer
->next
;
4038 cp_lexer_destroy (lexer
);
4040 /* Put the current source position back where it was before this
4041 lexer was pushed. */
4042 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4045 /* Lexical conventions [gram.lex] */
4047 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4051 cp_parser_identifier (cp_parser
* parser
)
4055 /* Look for the identifier. */
4056 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4057 /* Return the value. */
4059 return cp_expr (token
->u
.value
, token
->location
);
4061 return error_mark_node
;
4064 /* Parse a sequence of adjacent string constants. Returns a
4065 TREE_STRING representing the combined, nul-terminated string
4066 constant. If TRANSLATE is true, translate the string to the
4067 execution character set. If WIDE_OK is true, a wide string is
4070 C++98 [lex.string] says that if a narrow string literal token is
4071 adjacent to a wide string literal token, the behavior is undefined.
4072 However, C99 6.4.5p4 says that this results in a wide string literal.
4073 We follow C99 here, for consistency with the C front end.
4075 This code is largely lifted from lex_string() in c-lex.c.
4077 FUTURE: ObjC++ will need to handle @-strings here. */
4079 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4080 bool lookup_udlit
= true)
4084 struct obstack str_ob
;
4085 struct obstack loc_ob
;
4086 cpp_string str
, istr
, *strs
;
4088 enum cpp_ttype type
, curr_type
;
4089 int have_suffix_p
= 0;
4091 tree suffix_id
= NULL_TREE
;
4092 bool curr_tok_is_userdef_p
= false;
4094 tok
= cp_lexer_peek_token (parser
->lexer
);
4095 if (!cp_parser_is_string_literal (tok
))
4097 cp_parser_error (parser
, "expected string-literal");
4098 return error_mark_node
;
4101 location_t loc
= tok
->location
;
4103 if (cpp_userdef_string_p (tok
->type
))
4105 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4106 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4107 curr_tok_is_userdef_p
= true;
4111 string_tree
= tok
->u
.value
;
4112 curr_type
= tok
->type
;
4116 /* Try to avoid the overhead of creating and destroying an obstack
4117 for the common case of just one string. */
4118 if (!cp_parser_is_string_literal
4119 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4121 cp_lexer_consume_token (parser
->lexer
);
4123 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4124 str
.len
= TREE_STRING_LENGTH (string_tree
);
4127 if (curr_tok_is_userdef_p
)
4129 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4131 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4134 curr_type
= tok
->type
;
4140 location_t last_tok_loc
= tok
->location
;
4141 gcc_obstack_init (&str_ob
);
4142 gcc_obstack_init (&loc_ob
);
4147 cp_lexer_consume_token (parser
->lexer
);
4149 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4150 str
.len
= TREE_STRING_LENGTH (string_tree
);
4152 if (curr_tok_is_userdef_p
)
4154 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4155 if (have_suffix_p
== 0)
4157 suffix_id
= curr_suffix_id
;
4160 else if (have_suffix_p
== 1
4161 && curr_suffix_id
!= suffix_id
)
4163 error ("inconsistent user-defined literal suffixes"
4164 " %qD and %qD in string literal",
4165 suffix_id
, curr_suffix_id
);
4168 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4171 curr_type
= tok
->type
;
4173 if (type
!= curr_type
)
4175 if (type
== CPP_STRING
)
4177 else if (curr_type
!= CPP_STRING
)
4179 rich_location
rich_loc (line_table
, tok
->location
);
4180 rich_loc
.add_range (last_tok_loc
);
4181 error_at (&rich_loc
,
4182 "unsupported non-standard concatenation "
4183 "of string literals");
4187 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4188 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4190 last_tok_loc
= tok
->location
;
4192 tok
= cp_lexer_peek_token (parser
->lexer
);
4193 if (cpp_userdef_string_p (tok
->type
))
4195 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4196 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4197 curr_tok_is_userdef_p
= true;
4201 string_tree
= tok
->u
.value
;
4202 curr_type
= tok
->type
;
4203 curr_tok_is_userdef_p
= false;
4206 while (cp_parser_is_string_literal (tok
));
4208 /* A string literal built by concatenation has its caret=start at
4209 the start of the initial string, and its finish at the finish of
4210 the final string literal. */
4211 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4213 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4216 if (type
!= CPP_STRING
&& !wide_ok
)
4218 cp_parser_error (parser
, "a wide string is invalid in this context");
4222 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4223 (parse_in
, strs
, count
, &istr
, type
))
4225 value
= build_string (istr
.len
, (const char *)istr
.text
);
4226 free (CONST_CAST (unsigned char *, istr
.text
));
4229 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4230 gcc_assert (g_string_concat_db
);
4231 g_string_concat_db
->record_string_concatenation (count
, locs
);
4238 case CPP_UTF8STRING
:
4239 TREE_TYPE (value
) = char_array_type_node
;
4242 TREE_TYPE (value
) = char16_array_type_node
;
4245 TREE_TYPE (value
) = char32_array_type_node
;
4248 TREE_TYPE (value
) = wchar_array_type_node
;
4252 value
= fix_string_type (value
);
4256 tree literal
= build_userdef_literal (suffix_id
, value
,
4257 OT_NONE
, NULL_TREE
);
4259 value
= cp_parser_userdef_string_literal (literal
);
4265 /* cpp_interpret_string has issued an error. */
4266 value
= error_mark_node
;
4270 obstack_free (&str_ob
, 0);
4271 obstack_free (&loc_ob
, 0);
4274 return cp_expr (value
, loc
);
4277 /* Look up a literal operator with the name and the exact arguments. */
4280 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4282 tree decl
= lookup_name (name
);
4283 if (!decl
|| !is_overloaded_fn (decl
))
4284 return error_mark_node
;
4286 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4290 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4296 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4297 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4299 tree tparm
= TREE_VALUE (parmtypes
);
4300 tree targ
= TREE_TYPE ((*args
)[ix
]);
4301 bool ptr
= TYPE_PTR_P (tparm
);
4302 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4303 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4305 || !same_type_p (TREE_TYPE (tparm
),
4311 && ix
== vec_safe_length (args
)
4312 /* May be this should be sufficient_parms_p instead,
4313 depending on how exactly should user-defined literals
4314 work in presence of default arguments on the literal
4315 operator parameters. */
4316 && parmtypes
== void_list_node
)
4321 return error_mark_node
;
4324 /* Parse a user-defined char constant. Returns a call to a user-defined
4325 literal operator taking the character as an argument. */
4328 cp_parser_userdef_char_literal (cp_parser
*parser
)
4330 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4331 tree literal
= token
->u
.value
;
4332 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4333 tree value
= USERDEF_LITERAL_VALUE (literal
);
4334 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4337 /* Build up a call to the user-defined operator */
4338 /* Lookup the name we got back from the id-expression. */
4339 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4340 vec_safe_push (args
, value
);
4341 decl
= lookup_literal_operator (name
, args
);
4342 if (!decl
|| decl
== error_mark_node
)
4344 error ("unable to find character literal operator %qD with %qT argument",
4345 name
, TREE_TYPE (value
));
4346 release_tree_vector (args
);
4347 return error_mark_node
;
4349 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4350 release_tree_vector (args
);
4354 /* A subroutine of cp_parser_userdef_numeric_literal to
4355 create a char... template parameter pack from a string node. */
4358 make_char_string_pack (tree value
)
4361 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4362 const char *str
= TREE_STRING_POINTER (value
);
4363 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4364 tree argvec
= make_tree_vec (1);
4366 /* Fill in CHARVEC with all of the parameters. */
4367 charvec
= make_tree_vec (len
);
4368 for (i
= 0; i
< len
; ++i
)
4370 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4371 cpp_string in
= { 3, s
};
4372 cpp_string out
= { 0, 0 };
4373 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4375 gcc_assert (out
.len
== 2);
4376 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4380 /* Build the argument packs. */
4381 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4383 TREE_VEC_ELT (argvec
, 0) = argpack
;
4388 /* A subroutine of cp_parser_userdef_numeric_literal to
4389 create a char... template parameter pack from a string node. */
4392 make_string_pack (tree value
)
4395 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4396 const unsigned char *str
4397 = (const unsigned char *) TREE_STRING_POINTER (value
);
4398 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4399 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4400 tree argvec
= make_tree_vec (2);
4402 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4403 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4405 /* First template parm is character type. */
4406 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4408 /* Fill in CHARVEC with all of the parameters. */
4409 charvec
= make_tree_vec (len
);
4410 for (int i
= 0; i
< len
; ++i
)
4411 TREE_VEC_ELT (charvec
, i
)
4412 = double_int_to_tree (str_char_type_node
,
4413 double_int::from_buffer (str
+ i
* sz
, sz
));
4415 /* Build the argument packs. */
4416 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4418 TREE_VEC_ELT (argvec
, 1) = argpack
;
4423 /* Parse a user-defined numeric constant. returns a call to a user-defined
4424 literal operator. */
4427 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4429 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4430 tree literal
= token
->u
.value
;
4431 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4432 tree value
= USERDEF_LITERAL_VALUE (literal
);
4433 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4434 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4435 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4437 vec
<tree
, va_gc
> *args
;
4439 /* Look for a literal operator taking the exact type of numeric argument
4440 as the literal value. */
4441 args
= make_tree_vector ();
4442 vec_safe_push (args
, value
);
4443 decl
= lookup_literal_operator (name
, args
);
4444 if (decl
&& decl
!= error_mark_node
)
4446 result
= finish_call_expr (decl
, &args
, false, true,
4447 tf_warning_or_error
);
4449 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4451 warning_at (token
->location
, OPT_Woverflow
,
4452 "integer literal exceeds range of %qT type",
4453 long_long_unsigned_type_node
);
4458 warning_at (token
->location
, OPT_Woverflow
,
4459 "floating literal exceeds range of %qT type",
4460 long_double_type_node
);
4461 else if (overflow
< 0)
4462 warning_at (token
->location
, OPT_Woverflow
,
4463 "floating literal truncated to zero");
4466 release_tree_vector (args
);
4469 release_tree_vector (args
);
4471 /* If the numeric argument didn't work, look for a raw literal
4472 operator taking a const char* argument consisting of the number
4473 in string format. */
4474 args
= make_tree_vector ();
4475 vec_safe_push (args
, num_string
);
4476 decl
= lookup_literal_operator (name
, args
);
4477 if (decl
&& decl
!= error_mark_node
)
4479 result
= finish_call_expr (decl
, &args
, false, true,
4480 tf_warning_or_error
);
4481 release_tree_vector (args
);
4484 release_tree_vector (args
);
4486 /* If the raw literal didn't work, look for a non-type template
4487 function with parameter pack char.... Call the function with
4488 template parameter characters representing the number. */
4489 args
= make_tree_vector ();
4490 decl
= lookup_literal_operator (name
, args
);
4491 if (decl
&& decl
!= error_mark_node
)
4493 tree tmpl_args
= make_char_string_pack (num_string
);
4494 if (tmpl_args
== NULL_TREE
)
4496 error ("failed to translate literal to execution character set %qT",
4498 return error_mark_node
;
4500 decl
= lookup_template_function (decl
, tmpl_args
);
4501 result
= finish_call_expr (decl
, &args
, false, true,
4502 tf_warning_or_error
);
4503 release_tree_vector (args
);
4507 release_tree_vector (args
);
4509 /* In C++14 the standard library defines complex number suffixes that
4510 conflict with GNU extensions. Prefer them if <complex> is #included. */
4511 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4512 bool i14
= (cxx_dialect
> cxx11
4513 && (id_equal (suffix_id
, "i")
4514 || id_equal (suffix_id
, "if")
4515 || id_equal (suffix_id
, "il")));
4516 diagnostic_t kind
= DK_ERROR
;
4521 tree cxlit
= lookup_qualified_name (std_node
,
4522 get_identifier ("complex_literals"),
4524 if (cxlit
== error_mark_node
)
4526 /* No <complex>, so pedwarn and use GNU semantics. */
4528 opt
= OPT_Wpedantic
;
4533 = emit_diagnostic (kind
, input_location
, opt
,
4534 "unable to find numeric literal operator %qD", name
);
4537 /* Don't inform either. */;
4540 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4541 "(from <complex>) to enable the C++14 user-defined literal "
4544 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4545 "GNU built-in suffix");
4548 inform (token
->location
, "use -fext-numeric-literals "
4549 "to enable more built-in suffixes");
4551 if (kind
== DK_ERROR
)
4552 value
= error_mark_node
;
4555 /* Use the built-in semantics. */
4557 if (id_equal (suffix_id
, "i"))
4559 if (TREE_CODE (value
) == INTEGER_CST
)
4560 type
= integer_type_node
;
4562 type
= double_type_node
;
4564 else if (id_equal (suffix_id
, "if"))
4565 type
= float_type_node
;
4566 else /* if (id_equal (suffix_id, "il")) */
4567 type
= long_double_type_node
;
4569 value
= build_complex (build_complex_type (type
),
4570 fold_convert (type
, integer_zero_node
),
4571 fold_convert (type
, value
));
4574 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4575 /* Avoid repeated diagnostics. */
4576 token
->u
.value
= value
;
4580 /* Parse a user-defined string constant. Returns a call to a user-defined
4581 literal operator taking a character pointer and the length of the string
4585 cp_parser_userdef_string_literal (tree literal
)
4587 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4588 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4589 tree value
= USERDEF_LITERAL_VALUE (literal
);
4590 int len
= TREE_STRING_LENGTH (value
)
4591 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4594 /* Build up a call to the user-defined operator. */
4595 /* Lookup the name we got back from the id-expression. */
4596 releasing_vec rargs
;
4597 vec
<tree
, va_gc
> *&args
= rargs
.get_ref();
4598 vec_safe_push (args
, value
);
4599 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4600 decl
= lookup_literal_operator (name
, args
);
4602 if (decl
&& decl
!= error_mark_node
)
4603 return finish_call_expr (decl
, &args
, false, true,
4604 tf_warning_or_error
);
4606 /* Look for a suitable template function, either (C++20) with a single
4607 parameter of class type, or (N3599) with typename parameter CharT and
4608 parameter pack CharT... */
4610 decl
= lookup_literal_operator (name
, args
);
4611 if (decl
&& decl
!= error_mark_node
)
4613 /* Use resolve_nondeduced_context to try to choose one form of template
4615 tree tmpl_args
= make_tree_vec (1);
4616 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4617 decl
= lookup_template_function (decl
, tmpl_args
);
4618 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4623 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4624 res
= resolve_nondeduced_context (decl
, tf_none
);
4628 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4629 TREE_OPERAND (decl
, 1) = tmpl_args
;
4630 return finish_call_expr (decl
, &args
, false, true,
4631 tf_warning_or_error
);
4634 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4635 name
, TREE_TYPE (value
), size_type_node
);
4636 return error_mark_node
;
4640 /* Basic concepts [gram.basic] */
4642 /* Parse a translation-unit.
4645 declaration-seq [opt] */
4648 cp_parser_translation_unit (cp_parser
* parser
)
4650 gcc_checking_assert (!cp_error_declarator
);
4652 /* Create the declarator obstack. */
4653 gcc_obstack_init (&declarator_obstack
);
4654 /* Create the error declarator. */
4655 cp_error_declarator
= make_declarator (cdk_error
);
4656 /* Create the empty parameter list. */
4657 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4659 /* Remember where the base of the declarator obstack lies. */
4660 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4662 bool implicit_extern_c
= false;
4666 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4668 /* If we're entering or exiting a region that's implicitly
4669 extern "C", modify the lang context appropriately. */
4670 if (implicit_extern_c
4671 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4673 implicit_extern_c
= !implicit_extern_c
;
4674 if (implicit_extern_c
)
4675 push_lang_context (lang_name_c
);
4677 pop_lang_context ();
4680 if (token
->type
== CPP_EOF
)
4683 if (token
->type
== CPP_CLOSE_BRACE
)
4685 cp_parser_error (parser
, "expected declaration");
4686 cp_lexer_consume_token (parser
->lexer
);
4687 /* If the next token is now a `;', consume it. */
4688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4689 cp_lexer_consume_token (parser
->lexer
);
4692 cp_parser_toplevel_declaration (parser
);
4695 /* Get rid of the token array; we don't need it any more. */
4696 cp_lexer_destroy (parser
->lexer
);
4697 parser
->lexer
= NULL
;
4699 /* The EOF should have reset this. */
4700 gcc_checking_assert (!implicit_extern_c
);
4702 /* Make sure the declarator obstack was fully cleaned up. */
4703 gcc_assert (obstack_next_free (&declarator_obstack
)
4704 == declarator_obstack_base
);
4707 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4708 decltype context. */
4710 static inline tsubst_flags_t
4711 complain_flags (bool decltype_p
)
4713 tsubst_flags_t complain
= tf_warning_or_error
;
4715 complain
|= tf_decltype
;
4719 /* We're about to parse a collection of statements. If we're currently
4720 parsing tentatively, set up a firewall so that any nested
4721 cp_parser_commit_to_tentative_parse won't affect the current context. */
4723 static cp_token_position
4724 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4726 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4729 cp_parser_parse_tentatively (parser
);
4730 cp_parser_commit_to_topmost_tentative_parse (parser
);
4731 return cp_lexer_token_position (parser
->lexer
, false);
4734 /* We've finished parsing the collection of statements. Wrap up the
4735 firewall and replace the relevant tokens with the parsed form. */
4738 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4744 /* Finish the firewall level. */
4745 cp_parser_parse_definitely (parser
);
4746 /* And remember the result of the parse for when we try again. */
4747 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4748 token
->type
= CPP_PREPARSED_EXPR
;
4749 token
->u
.value
= expr
;
4750 token
->keyword
= RID_MAX
;
4751 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4754 /* Like the above functions, but let the user modify the tokens. Used by
4755 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4756 later parses, so it makes sense to localize the effects of
4757 cp_parser_commit_to_tentative_parse. */
4759 struct tentative_firewall
4764 tentative_firewall (cp_parser
*p
): parser(p
)
4766 /* If we're currently parsing tentatively, start a committed level as a
4767 firewall and then an inner tentative parse. */
4768 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4770 cp_parser_parse_tentatively (parser
);
4771 cp_parser_commit_to_topmost_tentative_parse (parser
);
4772 cp_parser_parse_tentatively (parser
);
4776 ~tentative_firewall()
4780 /* Finish the inner tentative parse and the firewall, propagating any
4781 uncommitted error state to the outer tentative parse. */
4782 bool err
= cp_parser_error_occurred (parser
);
4783 cp_parser_parse_definitely (parser
);
4784 cp_parser_parse_definitely (parser
);
4786 cp_parser_simulate_error (parser
);
4791 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4792 This class is for tracking such a matching pair of symbols.
4793 In particular, it tracks the location of the first token,
4794 so that if the second token is missing, we can highlight the
4795 location of the first token when notifying the user about the
4798 template <typename traits_t
>
4802 /* token_pair's ctor. */
4803 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4805 /* If the next token is the opening symbol for this pair, consume it and
4807 Otherwise, issue an error and return false.
4808 In either case, record the location of the opening token. */
4810 bool require_open (cp_parser
*parser
)
4812 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4813 return cp_parser_require (parser
, traits_t::open_token_type
,
4814 traits_t::required_token_open
);
4817 /* Consume the next token from PARSER, recording its location as
4818 that of the opening token within the pair. */
4820 cp_token
* consume_open (cp_parser
*parser
)
4822 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4823 gcc_assert (tok
->type
== traits_t::open_token_type
);
4824 m_open_loc
= tok
->location
;
4828 /* If the next token is the closing symbol for this pair, consume it
4830 Otherwise, issue an error, highlighting the location of the
4831 corresponding opening token, and return NULL. */
4833 cp_token
*require_close (cp_parser
*parser
) const
4835 return cp_parser_require (parser
, traits_t::close_token_type
,
4836 traits_t::required_token_close
,
4841 location_t m_open_loc
;
4844 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4846 struct matching_paren_traits
4848 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4849 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4850 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4851 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4854 /* "matching_parens" is a token_pair<T> class for tracking matching
4855 pairs of parentheses. */
4857 typedef token_pair
<matching_paren_traits
> matching_parens
;
4859 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4861 struct matching_brace_traits
4863 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4864 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4865 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4866 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4869 /* "matching_braces" is a token_pair<T> class for tracking matching
4872 typedef token_pair
<matching_brace_traits
> matching_braces
;
4875 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4876 enclosing parentheses. */
4879 cp_parser_statement_expr (cp_parser
*parser
)
4881 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4883 /* Consume the '('. */
4884 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4885 matching_parens parens
;
4886 parens
.consume_open (parser
);
4887 /* Start the statement-expression. */
4888 tree expr
= begin_stmt_expr ();
4889 /* Parse the compound-statement. */
4890 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4892 expr
= finish_stmt_expr (expr
, false);
4893 /* Consume the ')'. */
4894 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4895 if (!parens
.require_close (parser
))
4896 cp_parser_skip_to_end_of_statement (parser
);
4898 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4899 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4900 return cp_expr (expr
, combined_loc
);
4903 /* Expressions [gram.expr] */
4905 /* Parse a fold-operator.
4908 - * / % ^ & | = < > << >>
4909 = -= *= /= %= ^= &= |= <<= >>=
4910 == != <= >= && || , .* ->*
4912 This returns the tree code corresponding to the matched operator
4913 as an int. When the current token matches a compound assignment
4914 opertor, the resulting tree code is the negative value of the
4915 non-assignment operator. */
4918 cp_parser_fold_operator (cp_token
*token
)
4920 switch (token
->type
)
4922 case CPP_PLUS
: return PLUS_EXPR
;
4923 case CPP_MINUS
: return MINUS_EXPR
;
4924 case CPP_MULT
: return MULT_EXPR
;
4925 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4926 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4927 case CPP_XOR
: return BIT_XOR_EXPR
;
4928 case CPP_AND
: return BIT_AND_EXPR
;
4929 case CPP_OR
: return BIT_IOR_EXPR
;
4930 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4931 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4933 case CPP_EQ
: return -NOP_EXPR
;
4934 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4935 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4936 case CPP_MULT_EQ
: return -MULT_EXPR
;
4937 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4938 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4939 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4940 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4941 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4942 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4943 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4945 case CPP_EQ_EQ
: return EQ_EXPR
;
4946 case CPP_NOT_EQ
: return NE_EXPR
;
4947 case CPP_LESS
: return LT_EXPR
;
4948 case CPP_GREATER
: return GT_EXPR
;
4949 case CPP_LESS_EQ
: return LE_EXPR
;
4950 case CPP_GREATER_EQ
: return GE_EXPR
;
4952 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4953 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4955 case CPP_COMMA
: return COMPOUND_EXPR
;
4957 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4958 case CPP_DEREF_STAR
: return MEMBER_REF
;
4960 default: return ERROR_MARK
;
4964 /* Returns true if CODE indicates a binary expression, which is not allowed in
4965 the LHS of a fold-expression. More codes will need to be added to use this
4966 function in other contexts. */
4969 is_binary_op (tree_code code
)
4974 case POINTER_PLUS_EXPR
:
4977 case TRUNC_DIV_EXPR
:
4978 case TRUNC_MOD_EXPR
:
4994 case TRUTH_ANDIF_EXPR
:
4995 case TRUTH_ORIF_EXPR
:
5008 /* If the next token is a suitable fold operator, consume it and return as
5009 the function above. */
5012 cp_parser_fold_operator (cp_parser
*parser
)
5014 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5015 int code
= cp_parser_fold_operator (token
);
5016 if (code
!= ERROR_MARK
)
5017 cp_lexer_consume_token (parser
->lexer
);
5021 /* Parse a fold-expression.
5024 ( ... folding-operator cast-expression)
5025 ( cast-expression folding-operator ... )
5026 ( cast-expression folding operator ... folding-operator cast-expression)
5028 Note that the '(' and ')' are matched in primary expression. */
5031 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5038 cp_lexer_consume_token (parser
->lexer
);
5039 int op
= cp_parser_fold_operator (parser
);
5040 if (op
== ERROR_MARK
)
5042 cp_parser_error (parser
, "expected binary operator");
5043 return error_mark_node
;
5046 tree expr
= cp_parser_cast_expression (parser
, false, false,
5048 if (expr
== error_mark_node
)
5049 return error_mark_node
;
5050 return finish_left_unary_fold_expr (expr
, op
);
5053 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5054 int op
= cp_parser_fold_operator (parser
);
5055 if (op
== ERROR_MARK
)
5057 cp_parser_error (parser
, "expected binary operator");
5058 return error_mark_node
;
5061 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5063 cp_parser_error (parser
, "expected ...");
5064 return error_mark_node
;
5066 cp_lexer_consume_token (parser
->lexer
);
5068 /* The operands of a fold-expression are cast-expressions, so binary or
5069 conditional expressions are not allowed. We check this here to avoid
5070 tentative parsing. */
5071 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5072 /* OK, the expression was parenthesized. */;
5073 else if (is_binary_op (TREE_CODE (expr1
)))
5074 error_at (location_of (expr1
),
5075 "binary expression in operand of fold-expression");
5076 else if (TREE_CODE (expr1
) == COND_EXPR
5077 || (REFERENCE_REF_P (expr1
)
5078 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5079 error_at (location_of (expr1
),
5080 "conditional expression in operand of fold-expression");
5083 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5084 return finish_right_unary_fold_expr (expr1
, op
);
5086 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5088 cp_parser_error (parser
, "mismatched operator in fold-expression");
5089 return error_mark_node
;
5091 cp_lexer_consume_token (parser
->lexer
);
5093 // Binary left or right fold.
5094 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5095 if (expr2
== error_mark_node
)
5096 return error_mark_node
;
5097 return finish_binary_fold_expr (expr1
, expr2
, op
);
5100 /* Parse a primary-expression.
5107 lambda-expression (C++11)
5112 ( compound-statement )
5113 __builtin_va_arg ( assignment-expression , type-id )
5114 __builtin_offsetof ( type-id , offsetof-expression )
5117 __has_nothrow_assign ( type-id )
5118 __has_nothrow_constructor ( type-id )
5119 __has_nothrow_copy ( type-id )
5120 __has_trivial_assign ( type-id )
5121 __has_trivial_constructor ( type-id )
5122 __has_trivial_copy ( type-id )
5123 __has_trivial_destructor ( type-id )
5124 __has_virtual_destructor ( type-id )
5125 __is_abstract ( type-id )
5126 __is_base_of ( type-id , type-id )
5127 __is_class ( type-id )
5128 __is_empty ( type-id )
5129 __is_enum ( type-id )
5130 __is_final ( type-id )
5131 __is_literal_type ( type-id )
5132 __is_pod ( type-id )
5133 __is_polymorphic ( type-id )
5134 __is_std_layout ( type-id )
5135 __is_trivial ( type-id )
5136 __is_union ( type-id )
5138 Objective-C++ Extension:
5146 ADDRESS_P is true iff this expression was immediately preceded by
5147 "&" and therefore might denote a pointer-to-member. CAST_P is true
5148 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5149 true iff this expression is a template argument.
5151 Returns a representation of the expression. Upon return, *IDK
5152 indicates what kind of id-expression (if any) was present. */
5155 cp_parser_primary_expression (cp_parser
*parser
,
5158 bool template_arg_p
,
5162 cp_token
*token
= NULL
;
5164 /* Assume the primary expression is not an id-expression. */
5165 *idk
= CP_ID_KIND_NONE
;
5167 /* Peek at the next token. */
5168 token
= cp_lexer_peek_token (parser
->lexer
);
5169 switch ((int) token
->type
)
5178 user-defined-literal */
5185 case CPP_PREPARSED_EXPR
:
5186 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5187 return cp_parser_userdef_numeric_literal (parser
);
5188 token
= cp_lexer_consume_token (parser
->lexer
);
5189 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5191 error_at (token
->location
,
5192 "fixed-point types not supported in C++");
5193 return error_mark_node
;
5195 /* Floating-point literals are only allowed in an integral
5196 constant expression if they are cast to an integral or
5197 enumeration type. */
5198 if (TREE_CODE (token
->u
.value
) == REAL_CST
5199 && parser
->integral_constant_expression_p
5202 /* CAST_P will be set even in invalid code like "int(2.7 +
5203 ...)". Therefore, we have to check that the next token
5204 is sure to end the cast. */
5207 cp_token
*next_token
;
5209 next_token
= cp_lexer_peek_token (parser
->lexer
);
5210 if (/* The comma at the end of an
5211 enumerator-definition. */
5212 next_token
->type
!= CPP_COMMA
5213 /* The curly brace at the end of an enum-specifier. */
5214 && next_token
->type
!= CPP_CLOSE_BRACE
5215 /* The end of a statement. */
5216 && next_token
->type
!= CPP_SEMICOLON
5217 /* The end of the cast-expression. */
5218 && next_token
->type
!= CPP_CLOSE_PAREN
5219 /* The end of an array bound. */
5220 && next_token
->type
!= CPP_CLOSE_SQUARE
5221 /* The closing ">" in a template-argument-list. */
5222 && (next_token
->type
!= CPP_GREATER
5223 || parser
->greater_than_is_operator_p
)
5224 /* C++0x only: A ">>" treated like two ">" tokens,
5225 in a template-argument-list. */
5226 && (next_token
->type
!= CPP_RSHIFT
5227 || (cxx_dialect
== cxx98
)
5228 || parser
->greater_than_is_operator_p
))
5232 /* If we are within a cast, then the constraint that the
5233 cast is to an integral or enumeration type will be
5234 checked at that point. If we are not within a cast, then
5235 this code is invalid. */
5237 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5239 return (cp_expr (token
->u
.value
, token
->location
)
5240 .maybe_add_location_wrapper ());
5242 case CPP_CHAR_USERDEF
:
5243 case CPP_CHAR16_USERDEF
:
5244 case CPP_CHAR32_USERDEF
:
5245 case CPP_WCHAR_USERDEF
:
5246 case CPP_UTF8CHAR_USERDEF
:
5247 return cp_parser_userdef_char_literal (parser
);
5253 case CPP_UTF8STRING
:
5254 case CPP_STRING_USERDEF
:
5255 case CPP_STRING16_USERDEF
:
5256 case CPP_STRING32_USERDEF
:
5257 case CPP_WSTRING_USERDEF
:
5258 case CPP_UTF8STRING_USERDEF
:
5259 /* ??? Should wide strings be allowed when parser->translate_strings_p
5260 is false (i.e. in attributes)? If not, we can kill the third
5261 argument to cp_parser_string_literal. */
5262 return (cp_parser_string_literal (parser
,
5263 parser
->translate_strings_p
,
5265 .maybe_add_location_wrapper ());
5267 case CPP_OPEN_PAREN
:
5268 /* If we see `( { ' then we are looking at the beginning of
5269 a GNU statement-expression. */
5270 if (cp_parser_allow_gnu_extensions_p (parser
)
5271 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5273 /* Statement-expressions are not allowed by the standard. */
5274 pedwarn (token
->location
, OPT_Wpedantic
,
5275 "ISO C++ forbids braced-groups within expressions");
5277 /* And they're not allowed outside of a function-body; you
5278 cannot, for example, write:
5280 int i = ({ int j = 3; j + 1; });
5282 at class or namespace scope. */
5283 if (!parser
->in_function_body
5284 || parser
->in_template_argument_list_p
)
5286 error_at (token
->location
,
5287 "statement-expressions are not allowed outside "
5288 "functions nor in template-argument lists");
5289 cp_parser_skip_to_end_of_block_or_statement (parser
);
5290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5291 cp_lexer_consume_token (parser
->lexer
);
5292 return error_mark_node
;
5295 return cp_parser_statement_expr (parser
);
5297 /* Otherwise it's a normal parenthesized expression. */
5300 bool saved_greater_than_is_operator_p
;
5302 location_t open_paren_loc
= token
->location
;
5304 /* Consume the `('. */
5305 matching_parens parens
;
5306 parens
.consume_open (parser
);
5307 /* Within a parenthesized expression, a `>' token is always
5308 the greater-than operator. */
5309 saved_greater_than_is_operator_p
5310 = parser
->greater_than_is_operator_p
;
5311 parser
->greater_than_is_operator_p
= true;
5313 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5314 /* Left fold expression. */
5317 /* Parse the parenthesized expression. */
5318 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5320 token
= cp_lexer_peek_token (parser
->lexer
);
5321 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5323 expr
= cp_parser_fold_expression (parser
, expr
);
5324 if (expr
!= error_mark_node
5325 && cxx_dialect
< cxx17
5326 && !in_system_header_at (input_location
))
5327 pedwarn (input_location
, 0, "fold-expressions only available "
5328 "with -std=c++17 or -std=gnu++17");
5331 /* Let the front end know that this expression was
5332 enclosed in parentheses. This matters in case, for
5333 example, the expression is of the form `A::B', since
5334 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5336 expr
= finish_parenthesized_expr (expr
);
5338 /* DR 705: Wrapping an unqualified name in parentheses
5339 suppresses arg-dependent lookup. We want to pass back
5340 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5341 (c++/37862), but none of the others. */
5342 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5343 *idk
= CP_ID_KIND_NONE
;
5345 /* The `>' token might be the end of a template-id or
5346 template-parameter-list now. */
5347 parser
->greater_than_is_operator_p
5348 = saved_greater_than_is_operator_p
;
5350 /* Consume the `)'. */
5351 token
= cp_lexer_peek_token (parser
->lexer
);
5352 location_t close_paren_loc
= token
->location
;
5353 expr
.set_range (open_paren_loc
, close_paren_loc
);
5354 if (!parens
.require_close (parser
)
5355 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5356 cp_parser_skip_to_end_of_statement (parser
);
5361 case CPP_OPEN_SQUARE
:
5363 if (c_dialect_objc ())
5365 /* We might have an Objective-C++ message. */
5366 cp_parser_parse_tentatively (parser
);
5367 tree msg
= cp_parser_objc_message_expression (parser
);
5368 /* If that works out, we're done ... */
5369 if (cp_parser_parse_definitely (parser
))
5371 /* ... else, fall though to see if it's a lambda. */
5373 cp_expr lam
= cp_parser_lambda_expression (parser
);
5374 /* Don't warn about a failed tentative parse. */
5375 if (cp_parser_error_occurred (parser
))
5376 return error_mark_node
;
5377 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5381 case CPP_OBJC_STRING
:
5382 if (c_dialect_objc ())
5383 /* We have an Objective-C++ string literal. */
5384 return cp_parser_objc_expression (parser
);
5385 cp_parser_error (parser
, "expected primary-expression");
5386 return error_mark_node
;
5389 switch (token
->keyword
)
5391 /* These two are the boolean literals. */
5393 cp_lexer_consume_token (parser
->lexer
);
5394 return cp_expr (boolean_true_node
, token
->location
);
5396 cp_lexer_consume_token (parser
->lexer
);
5397 return cp_expr (boolean_false_node
, token
->location
);
5399 /* The `__null' literal. */
5401 cp_lexer_consume_token (parser
->lexer
);
5402 return cp_expr (null_node
, token
->location
);
5404 /* The `nullptr' literal. */
5406 cp_lexer_consume_token (parser
->lexer
);
5407 return cp_expr (nullptr_node
, token
->location
);
5409 /* Recognize the `this' keyword. */
5411 cp_lexer_consume_token (parser
->lexer
);
5412 if (parser
->local_variables_forbidden_p
& THIS_FORBIDDEN
)
5414 error_at (token
->location
,
5415 "%<this%> may not be used in this context");
5416 return error_mark_node
;
5418 /* Pointers cannot appear in constant-expressions. */
5419 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5420 return error_mark_node
;
5421 return cp_expr (finish_this_expr (), token
->location
);
5423 /* The `operator' keyword can be the beginning of an
5428 case RID_FUNCTION_NAME
:
5429 case RID_PRETTY_FUNCTION_NAME
:
5430 case RID_C99_FUNCTION_NAME
:
5432 non_integral_constant name
;
5434 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5435 __func__ are the names of variables -- but they are
5436 treated specially. Therefore, they are handled here,
5437 rather than relying on the generic id-expression logic
5438 below. Grammatically, these names are id-expressions.
5440 Consume the token. */
5441 token
= cp_lexer_consume_token (parser
->lexer
);
5443 switch (token
->keyword
)
5445 case RID_FUNCTION_NAME
:
5446 name
= NIC_FUNC_NAME
;
5448 case RID_PRETTY_FUNCTION_NAME
:
5449 name
= NIC_PRETTY_FUNC
;
5451 case RID_C99_FUNCTION_NAME
:
5452 name
= NIC_C99_FUNC
;
5458 if (cp_parser_non_integral_constant_expression (parser
, name
))
5459 return error_mark_node
;
5461 /* Look up the name. */
5462 return finish_fname (token
->u
.value
);
5469 location_t type_location
;
5470 location_t start_loc
5471 = cp_lexer_peek_token (parser
->lexer
)->location
;
5472 /* The `__builtin_va_arg' construct is used to handle
5473 `va_arg'. Consume the `__builtin_va_arg' token. */
5474 cp_lexer_consume_token (parser
->lexer
);
5475 /* Look for the opening `('. */
5476 matching_parens parens
;
5477 parens
.require_open (parser
);
5478 /* Now, parse the assignment-expression. */
5479 expression
= cp_parser_assignment_expression (parser
);
5480 /* Look for the `,'. */
5481 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5482 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5483 /* Parse the type-id. */
5485 type_id_in_expr_sentinel
s (parser
);
5486 type
= cp_parser_type_id (parser
);
5488 /* Look for the closing `)'. */
5489 location_t finish_loc
5490 = cp_lexer_peek_token (parser
->lexer
)->location
;
5491 parens
.require_close (parser
);
5492 /* Using `va_arg' in a constant-expression is not
5494 if (cp_parser_non_integral_constant_expression (parser
,
5496 return error_mark_node
;
5497 /* Construct a location of the form:
5498 __builtin_va_arg (v, int)
5499 ~~~~~~~~~~~~~~~~~~~~~^~~~
5500 with the caret at the type, ranging from the start of the
5501 "__builtin_va_arg" token to the close paren. */
5502 location_t combined_loc
5503 = make_location (type_location
, start_loc
, finish_loc
);
5504 return build_x_va_arg (combined_loc
, expression
, type
);
5508 return cp_parser_builtin_offsetof (parser
);
5510 case RID_HAS_NOTHROW_ASSIGN
:
5511 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5512 case RID_HAS_NOTHROW_COPY
:
5513 case RID_HAS_TRIVIAL_ASSIGN
:
5514 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5515 case RID_HAS_TRIVIAL_COPY
:
5516 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5517 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5518 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5519 case RID_IS_ABSTRACT
:
5520 case RID_IS_AGGREGATE
:
5521 case RID_IS_BASE_OF
:
5526 case RID_IS_LITERAL_TYPE
:
5528 case RID_IS_POLYMORPHIC
:
5529 case RID_IS_SAME_AS
:
5530 case RID_IS_STD_LAYOUT
:
5531 case RID_IS_TRIVIAL
:
5532 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5533 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5534 case RID_IS_TRIVIALLY_COPYABLE
:
5536 case RID_IS_ASSIGNABLE
:
5537 case RID_IS_CONSTRUCTIBLE
:
5538 return cp_parser_trait_expr (parser
, token
->keyword
);
5542 return cp_parser_requires_expression (parser
);
5544 /* Objective-C++ expressions. */
5546 case RID_AT_PROTOCOL
:
5547 case RID_AT_SELECTOR
:
5548 return cp_parser_objc_expression (parser
);
5551 if (parser
->in_function_body
5552 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5555 error_at (token
->location
,
5556 "a template declaration cannot appear at block scope");
5557 cp_parser_skip_to_end_of_block_or_statement (parser
);
5558 return error_mark_node
;
5562 cp_parser_error (parser
, "expected primary-expression");
5563 return error_mark_node
;
5566 /* An id-expression can start with either an identifier, a
5567 `::' as the beginning of a qualified-id, or the "operator"
5571 case CPP_TEMPLATE_ID
:
5572 case CPP_NESTED_NAME_SPECIFIER
:
5575 cp_expr id_expression
;
5577 const char *error_msg
;
5580 cp_token
*id_expr_token
;
5582 /* Parse the id-expression. */
5584 = cp_parser_id_expression (parser
,
5585 /*template_keyword_p=*/false,
5586 /*check_dependency_p=*/true,
5588 /*declarator_p=*/false,
5589 /*optional_p=*/false);
5590 if (id_expression
== error_mark_node
)
5591 return error_mark_node
;
5592 id_expr_token
= token
;
5593 token
= cp_lexer_peek_token (parser
->lexer
);
5594 done
= (token
->type
!= CPP_OPEN_SQUARE
5595 && token
->type
!= CPP_OPEN_PAREN
5596 && token
->type
!= CPP_DOT
5597 && token
->type
!= CPP_DEREF
5598 && token
->type
!= CPP_PLUS_PLUS
5599 && token
->type
!= CPP_MINUS_MINUS
);
5600 /* If we have a template-id, then no further lookup is
5601 required. If the template-id was for a template-class, we
5602 will sometimes have a TYPE_DECL at this point. */
5603 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5604 || TREE_CODE (id_expression
) == TYPE_DECL
)
5605 decl
= id_expression
;
5606 /* Look up the name. */
5609 tree ambiguous_decls
;
5611 /* If we already know that this lookup is ambiguous, then
5612 we've already issued an error message; there's no reason
5614 if (id_expr_token
->type
== CPP_NAME
5615 && id_expr_token
->error_reported
)
5617 cp_parser_simulate_error (parser
);
5618 return error_mark_node
;
5621 decl
= cp_parser_lookup_name (parser
, id_expression
,
5624 /*is_namespace=*/false,
5625 /*check_dependency=*/true,
5627 id_expression
.get_location ());
5628 /* If the lookup was ambiguous, an error will already have
5630 if (ambiguous_decls
)
5631 return error_mark_node
;
5633 /* In Objective-C++, we may have an Objective-C 2.0
5634 dot-syntax for classes here. */
5635 if (c_dialect_objc ()
5636 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5637 && TREE_CODE (decl
) == TYPE_DECL
5638 && objc_is_class_name (decl
))
5641 cp_lexer_consume_token (parser
->lexer
);
5642 component
= cp_parser_identifier (parser
);
5643 if (component
== error_mark_node
)
5644 return error_mark_node
;
5646 tree result
= objc_build_class_component_ref (id_expression
,
5648 /* Build a location of the form:
5651 with caret at the start of the component name (at
5652 input_location), ranging from the start of the id_expression
5653 to the end of the component name. */
5654 location_t combined_loc
5655 = make_location (input_location
, id_expression
.get_start (),
5656 get_finish (input_location
));
5657 protected_set_expr_location (result
, combined_loc
);
5661 /* In Objective-C++, an instance variable (ivar) may be preferred
5662 to whatever cp_parser_lookup_name() found.
5663 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5664 rest of c-family, we have to do a little extra work to preserve
5665 any location information in cp_expr "decl". Given that
5666 objc_lookup_ivar is implemented in "c-family" and "objc", we
5667 have a trip through the pure "tree" type, rather than cp_expr.
5668 Naively copying it back to "decl" would implicitly give the
5669 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5670 store an EXPR_LOCATION. Hence we only update "decl" (and
5671 hence its location_t) if we get back a different tree node. */
5672 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5674 if (decl_tree
!= decl
.get_value ())
5675 decl
= cp_expr (decl_tree
);
5677 /* If name lookup gives us a SCOPE_REF, then the
5678 qualifying scope was dependent. */
5679 if (TREE_CODE (decl
) == SCOPE_REF
)
5681 /* At this point, we do not know if DECL is a valid
5682 integral constant expression. We assume that it is
5683 in fact such an expression, so that code like:
5685 template <int N> struct A {
5689 is accepted. At template-instantiation time, we
5690 will check that B<N>::i is actually a constant. */
5693 /* Check to see if DECL is a local variable in a context
5694 where that is forbidden. */
5695 if ((parser
->local_variables_forbidden_p
& LOCAL_VARS_FORBIDDEN
)
5696 && local_variable_p (decl
))
5698 error_at (id_expression
.get_location (),
5699 "local variable %qD may not appear in this context",
5701 return error_mark_node
;
5705 decl
= (finish_id_expression
5706 (id_expression
, decl
, parser
->scope
,
5708 parser
->integral_constant_expression_p
,
5709 parser
->allow_non_integral_constant_expression_p
,
5710 &parser
->non_integral_constant_expression_p
,
5711 template_p
, done
, address_p
,
5714 id_expression
.get_location ()));
5716 cp_parser_error (parser
, error_msg
);
5717 /* Build a location for an id-expression of the form:
5723 i.e. from the start of the first token to the end of the final
5724 token, with the caret at the start of the unqualified-id. */
5725 location_t caret_loc
= get_pure_location (id_expression
.get_location ());
5726 location_t start_loc
= get_start (id_expr_token
->location
);
5727 location_t finish_loc
= get_finish (id_expression
.get_location ());
5728 location_t combined_loc
5729 = make_location (caret_loc
, start_loc
, finish_loc
);
5731 decl
.set_location (combined_loc
);
5735 /* Anything else is an error. */
5737 cp_parser_error (parser
, "expected primary-expression");
5738 return error_mark_node
;
5742 static inline cp_expr
5743 cp_parser_primary_expression (cp_parser
*parser
,
5746 bool template_arg_p
,
5749 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5750 /*decltype*/false, idk
);
5753 /* Parse an id-expression.
5760 :: [opt] nested-name-specifier template [opt] unqualified-id
5762 :: operator-function-id
5765 Return a representation of the unqualified portion of the
5766 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5767 a `::' or nested-name-specifier.
5769 Often, if the id-expression was a qualified-id, the caller will
5770 want to make a SCOPE_REF to represent the qualified-id. This
5771 function does not do this in order to avoid wastefully creating
5772 SCOPE_REFs when they are not required.
5774 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5777 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5778 uninstantiated templates.
5780 If *TEMPLATE_P is non-NULL, it is set to true iff the
5781 `template' keyword is used to explicitly indicate that the entity
5782 named is a template.
5784 If DECLARATOR_P is true, the id-expression is appearing as part of
5785 a declarator, rather than as part of an expression. */
5788 cp_parser_id_expression (cp_parser
*parser
,
5789 bool template_keyword_p
,
5790 bool check_dependency_p
,
5795 bool global_scope_p
;
5796 bool nested_name_specifier_p
;
5798 /* Assume the `template' keyword was not used. */
5800 *template_p
= template_keyword_p
;
5802 /* Look for the optional `::' operator. */
5804 = (!template_keyword_p
5805 && (cp_parser_global_scope_opt (parser
,
5806 /*current_scope_valid_p=*/false)
5809 /* Look for the optional nested-name-specifier. */
5810 nested_name_specifier_p
5811 = (cp_parser_nested_name_specifier_opt (parser
,
5812 /*typename_keyword_p=*/false,
5819 /* If there is a nested-name-specifier, then we are looking at
5820 the first qualified-id production. */
5821 if (nested_name_specifier_p
)
5824 tree saved_object_scope
;
5825 tree saved_qualifying_scope
;
5826 cp_expr unqualified_id
;
5829 /* See if the next token is the `template' keyword. */
5831 template_p
= &is_template
;
5832 *template_p
= cp_parser_optional_template_keyword (parser
);
5833 /* Name lookup we do during the processing of the
5834 unqualified-id might obliterate SCOPE. */
5835 saved_scope
= parser
->scope
;
5836 saved_object_scope
= parser
->object_scope
;
5837 saved_qualifying_scope
= parser
->qualifying_scope
;
5838 /* Process the final unqualified-id. */
5839 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5842 /*optional_p=*/false);
5843 /* Restore the SAVED_SCOPE for our caller. */
5844 parser
->scope
= saved_scope
;
5845 parser
->object_scope
= saved_object_scope
;
5846 parser
->qualifying_scope
= saved_qualifying_scope
;
5848 return unqualified_id
;
5850 /* Otherwise, if we are in global scope, then we are looking at one
5851 of the other qualified-id productions. */
5852 else if (global_scope_p
)
5857 /* Peek at the next token. */
5858 token
= cp_lexer_peek_token (parser
->lexer
);
5860 /* If it's an identifier, and the next token is not a "<", then
5861 we can avoid the template-id case. This is an optimization
5862 for this common case. */
5863 if (token
->type
== CPP_NAME
5864 && !cp_parser_nth_token_starts_template_argument_list_p
5866 return cp_parser_identifier (parser
);
5868 cp_parser_parse_tentatively (parser
);
5869 /* Try a template-id. */
5870 id
= cp_parser_template_id (parser
,
5871 /*template_keyword_p=*/false,
5872 /*check_dependency_p=*/true,
5875 /* If that worked, we're done. */
5876 if (cp_parser_parse_definitely (parser
))
5879 /* Peek at the next token. (Changes in the token buffer may
5880 have invalidated the pointer obtained above.) */
5881 token
= cp_lexer_peek_token (parser
->lexer
);
5883 switch (token
->type
)
5886 return cp_parser_identifier (parser
);
5889 if (token
->keyword
== RID_OPERATOR
)
5890 return cp_parser_operator_function_id (parser
);
5894 cp_parser_error (parser
, "expected id-expression");
5895 return error_mark_node
;
5899 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5900 /*check_dependency_p=*/true,
5905 /* Parse an unqualified-id.
5909 operator-function-id
5910 conversion-function-id
5914 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5915 keyword, in a construct like `A::template ...'.
5917 Returns a representation of unqualified-id. For the `identifier'
5918 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5919 production a BIT_NOT_EXPR is returned; the operand of the
5920 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5921 other productions, see the documentation accompanying the
5922 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5923 names are looked up in uninstantiated templates. If DECLARATOR_P
5924 is true, the unqualified-id is appearing as part of a declarator,
5925 rather than as part of an expression. */
5928 cp_parser_unqualified_id (cp_parser
* parser
,
5929 bool template_keyword_p
,
5930 bool check_dependency_p
,
5936 /* Peek at the next token. */
5937 token
= cp_lexer_peek_token (parser
->lexer
);
5939 switch ((int) token
->type
)
5945 /* We don't know yet whether or not this will be a
5947 cp_parser_parse_tentatively (parser
);
5948 /* Try a template-id. */
5949 id
= cp_parser_template_id (parser
, template_keyword_p
,
5953 /* If it worked, we're done. */
5954 if (cp_parser_parse_definitely (parser
))
5956 /* Otherwise, it's an ordinary identifier. */
5957 return cp_parser_identifier (parser
);
5960 case CPP_TEMPLATE_ID
:
5961 return cp_parser_template_id (parser
, template_keyword_p
,
5969 tree qualifying_scope
;
5974 /* Consume the `~' token. */
5975 cp_lexer_consume_token (parser
->lexer
);
5976 /* Parse the class-name. The standard, as written, seems to
5979 template <typename T> struct S { ~S (); };
5980 template <typename T> S<T>::~S() {}
5982 is invalid, since `~' must be followed by a class-name, but
5983 `S<T>' is dependent, and so not known to be a class.
5984 That's not right; we need to look in uninstantiated
5985 templates. A further complication arises from:
5987 template <typename T> void f(T t) {
5991 Here, it is not possible to look up `T' in the scope of `T'
5992 itself. We must look in both the current scope, and the
5993 scope of the containing complete expression.
5995 Yet another issue is:
6004 The standard does not seem to say that the `S' in `~S'
6005 should refer to the type `S' and not the data member
6008 /* DR 244 says that we look up the name after the "~" in the
6009 same scope as we looked up the qualifying name. That idea
6010 isn't fully worked out; it's more complicated than that. */
6011 scope
= parser
->scope
;
6012 object_scope
= parser
->object_scope
;
6013 qualifying_scope
= parser
->qualifying_scope
;
6015 /* Check for invalid scopes. */
6016 if (scope
== error_mark_node
)
6018 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6019 cp_lexer_consume_token (parser
->lexer
);
6020 return error_mark_node
;
6022 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
6024 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6025 error_at (token
->location
,
6026 "scope %qT before %<~%> is not a class-name",
6028 cp_parser_simulate_error (parser
);
6029 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6030 cp_lexer_consume_token (parser
->lexer
);
6031 return error_mark_node
;
6033 gcc_assert (!scope
|| TYPE_P (scope
));
6035 /* If the name is of the form "X::~X" it's OK even if X is a
6037 token
= cp_lexer_peek_token (parser
->lexer
);
6039 && token
->type
== CPP_NAME
6040 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6042 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6043 || (CLASS_TYPE_P (scope
)
6044 && constructor_name_p (token
->u
.value
, scope
))))
6046 cp_lexer_consume_token (parser
->lexer
);
6047 return build_nt (BIT_NOT_EXPR
, scope
);
6050 /* ~auto means the destructor of whatever the object is. */
6051 if (cp_parser_is_keyword (token
, RID_AUTO
))
6053 if (cxx_dialect
< cxx14
)
6054 pedwarn (input_location
, 0,
6055 "%<~auto%> only available with "
6056 "-std=c++14 or -std=gnu++14");
6057 cp_lexer_consume_token (parser
->lexer
);
6058 return build_nt (BIT_NOT_EXPR
, make_auto ());
6061 /* If there was an explicit qualification (S::~T), first look
6062 in the scope given by the qualification (i.e., S).
6064 Note: in the calls to cp_parser_class_name below we pass
6065 typename_type so that lookup finds the injected-class-name
6066 rather than the constructor. */
6068 type_decl
= NULL_TREE
;
6071 cp_parser_parse_tentatively (parser
);
6072 type_decl
= cp_parser_class_name (parser
,
6073 /*typename_keyword_p=*/false,
6074 /*template_keyword_p=*/false,
6076 /*check_dependency=*/false,
6077 /*class_head_p=*/false,
6079 if (cp_parser_parse_definitely (parser
))
6082 /* In "N::S::~S", look in "N" as well. */
6083 if (!done
&& scope
&& qualifying_scope
)
6085 cp_parser_parse_tentatively (parser
);
6086 parser
->scope
= qualifying_scope
;
6087 parser
->object_scope
= NULL_TREE
;
6088 parser
->qualifying_scope
= NULL_TREE
;
6090 = cp_parser_class_name (parser
,
6091 /*typename_keyword_p=*/false,
6092 /*template_keyword_p=*/false,
6094 /*check_dependency=*/false,
6095 /*class_head_p=*/false,
6097 if (cp_parser_parse_definitely (parser
))
6100 /* In "p->S::~T", look in the scope given by "*p" as well. */
6101 else if (!done
&& object_scope
)
6103 cp_parser_parse_tentatively (parser
);
6104 parser
->scope
= object_scope
;
6105 parser
->object_scope
= NULL_TREE
;
6106 parser
->qualifying_scope
= NULL_TREE
;
6108 = cp_parser_class_name (parser
,
6109 /*typename_keyword_p=*/false,
6110 /*template_keyword_p=*/false,
6112 /*check_dependency=*/false,
6113 /*class_head_p=*/false,
6115 if (cp_parser_parse_definitely (parser
))
6118 /* Look in the surrounding context. */
6121 parser
->scope
= NULL_TREE
;
6122 parser
->object_scope
= NULL_TREE
;
6123 parser
->qualifying_scope
= NULL_TREE
;
6124 if (processing_template_decl
)
6125 cp_parser_parse_tentatively (parser
);
6127 = cp_parser_class_name (parser
,
6128 /*typename_keyword_p=*/false,
6129 /*template_keyword_p=*/false,
6131 /*check_dependency=*/false,
6132 /*class_head_p=*/false,
6134 if (processing_template_decl
6135 && ! cp_parser_parse_definitely (parser
))
6137 /* We couldn't find a type with this name. If we're parsing
6138 tentatively, fail and try something else. */
6139 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6141 cp_parser_simulate_error (parser
);
6142 return error_mark_node
;
6144 /* Otherwise, accept it and check for a match at instantiation
6146 type_decl
= cp_parser_identifier (parser
);
6147 if (type_decl
!= error_mark_node
)
6148 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6152 /* If an error occurred, assume that the name of the
6153 destructor is the same as the name of the qualifying
6154 class. That allows us to keep parsing after running
6155 into ill-formed destructor names. */
6156 if (type_decl
== error_mark_node
&& scope
)
6157 return build_nt (BIT_NOT_EXPR
, scope
);
6158 else if (type_decl
== error_mark_node
)
6159 return error_mark_node
;
6161 /* Check that destructor name and scope match. */
6162 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6164 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6165 error_at (token
->location
,
6166 "declaration of %<~%T%> as member of %qT",
6168 cp_parser_simulate_error (parser
);
6169 return error_mark_node
;
6174 A typedef-name that names a class shall not be used as the
6175 identifier in the declarator for a destructor declaration. */
6177 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6178 && !DECL_SELF_REFERENCE_P (type_decl
)
6179 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6180 error_at (token
->location
,
6181 "typedef-name %qD used as destructor declarator",
6184 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6188 if (token
->keyword
== RID_OPERATOR
)
6192 /* This could be a template-id, so we try that first. */
6193 cp_parser_parse_tentatively (parser
);
6194 /* Try a template-id. */
6195 id
= cp_parser_template_id (parser
, template_keyword_p
,
6196 /*check_dependency_p=*/true,
6199 /* If that worked, we're done. */
6200 if (cp_parser_parse_definitely (parser
))
6202 /* We still don't know whether we're looking at an
6203 operator-function-id or a conversion-function-id. */
6204 cp_parser_parse_tentatively (parser
);
6205 /* Try an operator-function-id. */
6206 id
= cp_parser_operator_function_id (parser
);
6207 /* If that didn't work, try a conversion-function-id. */
6208 if (!cp_parser_parse_definitely (parser
))
6209 id
= cp_parser_conversion_function_id (parser
);
6218 cp_parser_error (parser
, "expected unqualified-id");
6219 return error_mark_node
;
6223 /* Parse an (optional) nested-name-specifier.
6225 nested-name-specifier: [C++98]
6226 class-or-namespace-name :: nested-name-specifier [opt]
6227 class-or-namespace-name :: template nested-name-specifier [opt]
6229 nested-name-specifier: [C++0x]
6232 nested-name-specifier identifier ::
6233 nested-name-specifier template [opt] simple-template-id ::
6235 PARSER->SCOPE should be set appropriately before this function is
6236 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6237 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6240 Sets PARSER->SCOPE to the class (TYPE) or namespace
6241 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6242 it unchanged if there is no nested-name-specifier. Returns the new
6243 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6245 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6246 part of a declaration and/or decl-specifier. */
6249 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6250 bool typename_keyword_p
,
6251 bool check_dependency_p
,
6253 bool is_declaration
,
6254 bool template_keyword_p
/* = false */)
6256 bool success
= false;
6257 cp_token_position start
= 0;
6260 /* Remember where the nested-name-specifier starts. */
6261 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6263 start
= cp_lexer_token_position (parser
->lexer
, false);
6264 push_deferring_access_checks (dk_deferred
);
6271 tree saved_qualifying_scope
;
6273 /* Spot cases that cannot be the beginning of a
6274 nested-name-specifier. */
6275 token
= cp_lexer_peek_token (parser
->lexer
);
6277 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6278 the already parsed nested-name-specifier. */
6279 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6281 /* Grab the nested-name-specifier and continue the loop. */
6282 cp_parser_pre_parsed_nested_name_specifier (parser
);
6283 /* If we originally encountered this nested-name-specifier
6284 with IS_DECLARATION set to false, we will not have
6285 resolved TYPENAME_TYPEs, so we must do so here. */
6287 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6289 new_scope
= resolve_typename_type (parser
->scope
,
6290 /*only_current_p=*/false);
6291 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6292 parser
->scope
= new_scope
;
6298 /* Spot cases that cannot be the beginning of a
6299 nested-name-specifier. On the second and subsequent times
6300 through the loop, we look for the `template' keyword. */
6301 if (success
&& token
->keyword
== RID_TEMPLATE
)
6303 /* A template-id can start a nested-name-specifier. */
6304 else if (token
->type
== CPP_TEMPLATE_ID
)
6306 /* DR 743: decltype can be used in a nested-name-specifier. */
6307 else if (token_is_decltype (token
))
6311 /* If the next token is not an identifier, then it is
6312 definitely not a type-name or namespace-name. */
6313 if (token
->type
!= CPP_NAME
)
6315 /* If the following token is neither a `<' (to begin a
6316 template-id), nor a `::', then we are not looking at a
6317 nested-name-specifier. */
6318 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6320 if (token
->type
== CPP_COLON
6321 && parser
->colon_corrects_to_scope_p
6322 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6324 gcc_rich_location
richloc (token
->location
);
6325 richloc
.add_fixit_replace ("::");
6327 "found %<:%> in nested-name-specifier, "
6329 token
->type
= CPP_SCOPE
;
6332 if (token
->type
!= CPP_SCOPE
6333 && !cp_parser_nth_token_starts_template_argument_list_p
6338 /* The nested-name-specifier is optional, so we parse
6340 cp_parser_parse_tentatively (parser
);
6342 /* Look for the optional `template' keyword, if this isn't the
6343 first time through the loop. */
6345 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6347 /* Save the old scope since the name lookup we are about to do
6348 might destroy it. */
6349 old_scope
= parser
->scope
;
6350 saved_qualifying_scope
= parser
->qualifying_scope
;
6351 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6352 look up names in "X<T>::I" in order to determine that "Y" is
6353 a template. So, if we have a typename at this point, we make
6354 an effort to look through it. */
6356 && !typename_keyword_p
6358 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6359 parser
->scope
= resolve_typename_type (parser
->scope
,
6360 /*only_current_p=*/false);
6361 /* Parse the qualifying entity. */
6363 = cp_parser_qualifying_entity (parser
,
6369 /* Look for the `::' token. */
6370 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6372 /* If we found what we wanted, we keep going; otherwise, we're
6374 if (!cp_parser_parse_definitely (parser
))
6376 bool error_p
= false;
6378 /* Restore the OLD_SCOPE since it was valid before the
6379 failed attempt at finding the last
6380 class-or-namespace-name. */
6381 parser
->scope
= old_scope
;
6382 parser
->qualifying_scope
= saved_qualifying_scope
;
6384 /* If the next token is a decltype, and the one after that is a
6385 `::', then the decltype has failed to resolve to a class or
6386 enumeration type. Give this error even when parsing
6387 tentatively since it can't possibly be valid--and we're going
6388 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6389 won't get another chance.*/
6390 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6391 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6394 token
= cp_lexer_consume_token (parser
->lexer
);
6395 error_at (token
->location
, "decltype evaluates to %qT, "
6396 "which is not a class or enumeration type",
6397 token
->u
.tree_check_value
->value
);
6398 parser
->scope
= error_mark_node
;
6402 cp_lexer_consume_token (parser
->lexer
);
6405 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6406 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6408 /* If we have a non-type template-id followed by ::, it can't
6409 possibly be valid. */
6410 token
= cp_lexer_peek_token (parser
->lexer
);
6411 tree tid
= token
->u
.tree_check_value
->value
;
6412 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6413 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6415 tree tmpl
= NULL_TREE
;
6416 if (is_overloaded_fn (tid
))
6418 tree fns
= get_fns (tid
);
6419 if (OVL_SINGLE_P (fns
))
6420 tmpl
= OVL_FIRST (fns
);
6421 error_at (token
->location
, "function template-id %qD "
6422 "in nested-name-specifier", tid
);
6426 /* Variable template. */
6427 tmpl
= TREE_OPERAND (tid
, 0);
6428 gcc_assert (variable_template_p (tmpl
));
6429 error_at (token
->location
, "variable template-id %qD "
6430 "in nested-name-specifier", tid
);
6433 inform (DECL_SOURCE_LOCATION (tmpl
),
6434 "%qD declared here", tmpl
);
6436 parser
->scope
= error_mark_node
;
6440 cp_lexer_consume_token (parser
->lexer
);
6441 cp_lexer_consume_token (parser
->lexer
);
6445 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6447 /* If the next token is an identifier, and the one after
6448 that is a `::', then any valid interpretation would have
6449 found a class-or-namespace-name. */
6450 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6451 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6453 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6456 token
= cp_lexer_consume_token (parser
->lexer
);
6459 if (!token
->error_reported
)
6462 tree ambiguous_decls
;
6464 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6466 /*is_template=*/false,
6467 /*is_namespace=*/false,
6468 /*check_dependency=*/true,
6471 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6472 error_at (token
->location
,
6473 "%qD used without template arguments",
6475 else if (ambiguous_decls
)
6477 // cp_parser_lookup_name has the same diagnostic,
6478 // thus make sure to emit it at most once.
6479 if (cp_parser_uncommitted_to_tentative_parse_p
6482 error_at (token
->location
,
6483 "reference to %qD is ambiguous",
6485 print_candidates (ambiguous_decls
);
6487 decl
= error_mark_node
;
6491 if (cxx_dialect
!= cxx98
)
6492 cp_parser_name_lookup_error
6493 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6496 cp_parser_name_lookup_error
6497 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6501 parser
->scope
= error_mark_node
;
6503 /* Treat this as a successful nested-name-specifier
6508 If the name found is not a class-name (clause
6509 _class_) or namespace-name (_namespace.def_), the
6510 program is ill-formed. */
6513 cp_lexer_consume_token (parser
->lexer
);
6517 /* We've found one valid nested-name-specifier. */
6519 /* Name lookup always gives us a DECL. */
6520 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6521 new_scope
= TREE_TYPE (new_scope
);
6522 /* Uses of "template" must be followed by actual templates. */
6523 if (template_keyword_p
6524 && !(CLASS_TYPE_P (new_scope
)
6525 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6526 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6527 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6528 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6529 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6530 == TEMPLATE_ID_EXPR
)))
6531 permerror (input_location
, TYPE_P (new_scope
)
6532 ? G_("%qT is not a template")
6533 : G_("%qD is not a template"),
6535 /* If it is a class scope, try to complete it; we are about to
6536 be looking up names inside the class. */
6537 if (TYPE_P (new_scope
)
6538 /* Since checking types for dependency can be expensive,
6539 avoid doing it if the type is already complete. */
6540 && !COMPLETE_TYPE_P (new_scope
)
6541 /* Do not try to complete dependent types. */
6542 && !dependent_type_p (new_scope
))
6544 new_scope
= complete_type (new_scope
);
6545 /* If it is a typedef to current class, use the current
6546 class instead, as the typedef won't have any names inside
6548 if (!COMPLETE_TYPE_P (new_scope
)
6549 && currently_open_class (new_scope
))
6550 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6552 /* Make sure we look in the right scope the next time through
6554 parser
->scope
= new_scope
;
6557 /* If parsing tentatively, replace the sequence of tokens that makes
6558 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6559 token. That way, should we re-parse the token stream, we will
6560 not have to repeat the effort required to do the parse, nor will
6561 we issue duplicate error messages. */
6562 if (success
&& start
)
6566 token
= cp_lexer_token_at (parser
->lexer
, start
);
6567 /* Reset the contents of the START token. */
6568 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6569 /* Retrieve any deferred checks. Do not pop this access checks yet
6570 so the memory will not be reclaimed during token replacing below. */
6571 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6572 token
->u
.tree_check_value
->value
= parser
->scope
;
6573 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6574 token
->u
.tree_check_value
->qualifying_scope
=
6575 parser
->qualifying_scope
;
6576 token
->keyword
= RID_MAX
;
6578 /* Purge all subsequent tokens. */
6579 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6583 pop_to_parent_deferring_access_checks ();
6585 return success
? parser
->scope
: NULL_TREE
;
6588 /* Parse a nested-name-specifier. See
6589 cp_parser_nested_name_specifier_opt for details. This function
6590 behaves identically, except that it will an issue an error if no
6591 nested-name-specifier is present. */
6594 cp_parser_nested_name_specifier (cp_parser
*parser
,
6595 bool typename_keyword_p
,
6596 bool check_dependency_p
,
6598 bool is_declaration
)
6602 /* Look for the nested-name-specifier. */
6603 scope
= cp_parser_nested_name_specifier_opt (parser
,
6608 /* If it was not present, issue an error message. */
6611 cp_parser_error (parser
, "expected nested-name-specifier");
6612 parser
->scope
= NULL_TREE
;
6618 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6619 this is either a class-name or a namespace-name (which corresponds
6620 to the class-or-namespace-name production in the grammar). For
6621 C++0x, it can also be a type-name that refers to an enumeration
6622 type or a simple-template-id.
6624 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6625 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6626 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6627 TYPE_P is TRUE iff the next name should be taken as a class-name,
6628 even the same name is declared to be another entity in the same
6631 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6632 specified by the class-or-namespace-name. If neither is found the
6633 ERROR_MARK_NODE is returned. */
6636 cp_parser_qualifying_entity (cp_parser
*parser
,
6637 bool typename_keyword_p
,
6638 bool template_keyword_p
,
6639 bool check_dependency_p
,
6641 bool is_declaration
)
6644 tree saved_qualifying_scope
;
6645 tree saved_object_scope
;
6648 bool successful_parse_p
;
6650 /* DR 743: decltype can appear in a nested-name-specifier. */
6651 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6653 scope
= cp_parser_decltype (parser
);
6654 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6655 && !MAYBE_CLASS_TYPE_P (scope
))
6657 cp_parser_simulate_error (parser
);
6658 return error_mark_node
;
6660 if (TYPE_NAME (scope
))
6661 scope
= TYPE_NAME (scope
);
6665 /* Before we try to parse the class-name, we must save away the
6666 current PARSER->SCOPE since cp_parser_class_name will destroy
6668 saved_scope
= parser
->scope
;
6669 saved_qualifying_scope
= parser
->qualifying_scope
;
6670 saved_object_scope
= parser
->object_scope
;
6671 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6672 there is no need to look for a namespace-name. */
6673 only_class_p
= template_keyword_p
6674 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6676 cp_parser_parse_tentatively (parser
);
6677 scope
= cp_parser_class_name (parser
,
6680 type_p
? class_type
: none_type
,
6682 /*class_head_p=*/false,
6684 /*enum_ok=*/cxx_dialect
> cxx98
);
6685 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6686 /* If that didn't work, try for a namespace-name. */
6687 if (!only_class_p
&& !successful_parse_p
)
6689 /* Restore the saved scope. */
6690 parser
->scope
= saved_scope
;
6691 parser
->qualifying_scope
= saved_qualifying_scope
;
6692 parser
->object_scope
= saved_object_scope
;
6693 /* If we are not looking at an identifier followed by the scope
6694 resolution operator, then this is not part of a
6695 nested-name-specifier. (Note that this function is only used
6696 to parse the components of a nested-name-specifier.) */
6697 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6698 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6699 return error_mark_node
;
6700 scope
= cp_parser_namespace_name (parser
);
6706 /* Return true if we are looking at a compound-literal, false otherwise. */
6709 cp_parser_compound_literal_p (cp_parser
*parser
)
6711 cp_lexer_save_tokens (parser
->lexer
);
6713 /* Skip tokens until the next token is a closing parenthesis.
6714 If we find the closing `)', and the next token is a `{', then
6715 we are looking at a compound-literal. */
6716 bool compound_literal_p
6717 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6718 /*consume_paren=*/true)
6719 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6721 /* Roll back the tokens we skipped. */
6722 cp_lexer_rollback_tokens (parser
->lexer
);
6724 return compound_literal_p
;
6727 /* Return true if EXPR is the integer constant zero or a complex constant
6728 of zero, without any folding, but ignoring location wrappers. */
6731 literal_integer_zerop (const_tree expr
)
6733 return (location_wrapper_p (expr
)
6734 && integer_zerop (TREE_OPERAND (expr
, 0)));
6737 /* Parse a postfix-expression.
6741 postfix-expression [ expression ]
6742 postfix-expression ( expression-list [opt] )
6743 simple-type-specifier ( expression-list [opt] )
6744 typename :: [opt] nested-name-specifier identifier
6745 ( expression-list [opt] )
6746 typename :: [opt] nested-name-specifier template [opt] template-id
6747 ( expression-list [opt] )
6748 postfix-expression . template [opt] id-expression
6749 postfix-expression -> template [opt] id-expression
6750 postfix-expression . pseudo-destructor-name
6751 postfix-expression -> pseudo-destructor-name
6752 postfix-expression ++
6753 postfix-expression --
6754 dynamic_cast < type-id > ( expression )
6755 static_cast < type-id > ( expression )
6756 reinterpret_cast < type-id > ( expression )
6757 const_cast < type-id > ( expression )
6758 typeid ( expression )
6764 ( type-id ) { initializer-list , [opt] }
6766 This extension is a GNU version of the C99 compound-literal
6767 construct. (The C99 grammar uses `type-name' instead of `type-id',
6768 but they are essentially the same concept.)
6770 If ADDRESS_P is true, the postfix expression is the operand of the
6771 `&' operator. CAST_P is true if this expression is the target of a
6774 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6775 class member access expressions [expr.ref].
6777 Returns a representation of the expression. */
6780 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6781 bool member_access_only_p
, bool decltype_p
,
6782 cp_id_kind
* pidk_return
)
6787 cp_id_kind idk
= CP_ID_KIND_NONE
;
6788 cp_expr postfix_expression
= NULL_TREE
;
6789 bool is_member_access
= false;
6791 /* Peek at the next token. */
6792 token
= cp_lexer_peek_token (parser
->lexer
);
6793 loc
= token
->location
;
6794 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6796 /* Some of the productions are determined by keywords. */
6797 keyword
= token
->keyword
;
6807 const char *saved_message
;
6808 bool saved_in_type_id_in_expr_p
;
6810 /* All of these can be handled in the same way from the point
6811 of view of parsing. Begin by consuming the token
6812 identifying the cast. */
6813 cp_lexer_consume_token (parser
->lexer
);
6815 /* New types cannot be defined in the cast. */
6816 saved_message
= parser
->type_definition_forbidden_message
;
6817 parser
->type_definition_forbidden_message
6818 = G_("types may not be defined in casts");
6820 /* Look for the opening `<'. */
6821 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6822 /* Parse the type to which we are casting. */
6823 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6824 parser
->in_type_id_in_expr_p
= true;
6825 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
6827 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6828 /* Look for the closing `>'. */
6829 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6830 /* Restore the old message. */
6831 parser
->type_definition_forbidden_message
= saved_message
;
6833 bool saved_greater_than_is_operator_p
6834 = parser
->greater_than_is_operator_p
;
6835 parser
->greater_than_is_operator_p
= true;
6837 /* And the expression which is being cast. */
6838 matching_parens parens
;
6839 parens
.require_open (parser
);
6840 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6841 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6843 location_t end_loc
= close_paren
?
6844 close_paren
->location
: UNKNOWN_LOCATION
;
6846 parser
->greater_than_is_operator_p
6847 = saved_greater_than_is_operator_p
;
6849 /* Only type conversions to integral or enumeration types
6850 can be used in constant-expressions. */
6851 if (!cast_valid_in_integral_constant_expression_p (type
)
6852 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6854 postfix_expression
= error_mark_node
;
6862 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6866 = build_static_cast (type
, expression
, tf_warning_or_error
);
6870 = build_reinterpret_cast (type
, expression
,
6871 tf_warning_or_error
);
6875 = build_const_cast (type
, expression
, tf_warning_or_error
);
6881 /* Construct a location e.g. :
6882 reinterpret_cast <int *> (expr)
6883 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6884 ranging from the start of the "*_cast" token to the final closing
6885 paren, with the caret at the start. */
6886 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6887 postfix_expression
.set_location (cp_cast_loc
);
6894 const char *saved_message
;
6895 bool saved_in_type_id_in_expr_p
;
6897 /* Consume the `typeid' token. */
6898 cp_lexer_consume_token (parser
->lexer
);
6899 /* Look for the `(' token. */
6900 matching_parens parens
;
6901 parens
.require_open (parser
);
6902 /* Types cannot be defined in a `typeid' expression. */
6903 saved_message
= parser
->type_definition_forbidden_message
;
6904 parser
->type_definition_forbidden_message
6905 = G_("types may not be defined in a %<typeid%> expression");
6906 /* We can't be sure yet whether we're looking at a type-id or an
6908 cp_parser_parse_tentatively (parser
);
6909 /* Try a type-id first. */
6910 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6911 parser
->in_type_id_in_expr_p
= true;
6912 type
= cp_parser_type_id (parser
);
6913 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6914 /* Look for the `)' token. Otherwise, we can't be sure that
6915 we're not looking at an expression: consider `typeid (int
6916 (3))', for example. */
6917 cp_token
*close_paren
= parens
.require_close (parser
);
6918 /* If all went well, simply lookup the type-id. */
6919 if (cp_parser_parse_definitely (parser
))
6920 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6921 /* Otherwise, fall back to the expression variant. */
6926 /* Look for an expression. */
6927 expression
= cp_parser_expression (parser
, & idk
);
6928 /* Compute its typeid. */
6929 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6930 /* Look for the `)' token. */
6931 close_paren
= parens
.require_close (parser
);
6933 /* Restore the saved message. */
6934 parser
->type_definition_forbidden_message
= saved_message
;
6935 /* `typeid' may not appear in an integral constant expression. */
6936 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6937 postfix_expression
= error_mark_node
;
6939 /* Construct a location e.g. :
6942 ranging from the start of the "typeid" token to the final closing
6943 paren, with the caret at the start. */
6946 location_t typeid_loc
6947 = make_location (start_loc
, start_loc
, close_paren
->location
);
6948 postfix_expression
.set_location (typeid_loc
);
6949 postfix_expression
.maybe_add_location_wrapper ();
6957 /* The syntax permitted here is the same permitted for an
6958 elaborated-type-specifier. */
6959 ++parser
->prevent_constrained_type_specifiers
;
6960 type
= cp_parser_elaborated_type_specifier (parser
,
6961 /*is_friend=*/false,
6962 /*is_declaration=*/false);
6963 --parser
->prevent_constrained_type_specifiers
;
6964 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6969 case RID_BUILTIN_SHUFFLE
:
6970 case RID_BUILTIN_LAUNDER
:
6972 vec
<tree
, va_gc
> *vec
;
6976 cp_lexer_consume_token (parser
->lexer
);
6977 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6978 /*cast_p=*/false, /*allow_expansion_p=*/true,
6979 /*non_constant_p=*/NULL
);
6982 postfix_expression
= error_mark_node
;
6986 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6992 if (vec
->length () == 1)
6994 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6997 error_at (loc
, "wrong number of arguments to "
6998 "%<__builtin_addressof%>");
6999 postfix_expression
= error_mark_node
;
7003 case RID_BUILTIN_LAUNDER
:
7004 if (vec
->length () == 1)
7005 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
7006 tf_warning_or_error
);
7009 error_at (loc
, "wrong number of arguments to "
7010 "%<__builtin_launder%>");
7011 postfix_expression
= error_mark_node
;
7015 case RID_BUILTIN_SHUFFLE
:
7016 if (vec
->length () == 2)
7018 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
7019 (*vec
)[1], tf_warning_or_error
);
7020 else if (vec
->length () == 3)
7022 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
7023 (*vec
)[2], tf_warning_or_error
);
7026 error_at (loc
, "wrong number of arguments to "
7027 "%<__builtin_shuffle%>");
7028 postfix_expression
= error_mark_node
;
7038 case RID_BUILTIN_CONVERTVECTOR
:
7042 /* Consume the `__builtin_convertvector' token. */
7043 cp_lexer_consume_token (parser
->lexer
);
7044 /* Look for the opening `('. */
7045 matching_parens parens
;
7046 parens
.require_open (parser
);
7047 /* Now, parse the assignment-expression. */
7048 expression
= cp_parser_assignment_expression (parser
);
7049 /* Look for the `,'. */
7050 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7051 location_t type_location
7052 = cp_lexer_peek_token (parser
->lexer
)->location
;
7053 /* Parse the type-id. */
7055 type_id_in_expr_sentinel
s (parser
);
7056 type
= cp_parser_type_id (parser
);
7058 /* Look for the closing `)'. */
7059 parens
.require_close (parser
);
7060 return cp_build_vec_convert (expression
, type_location
, type
,
7061 tf_warning_or_error
);
7068 /* If the next thing is a simple-type-specifier, we may be
7069 looking at a functional cast. We could also be looking at
7070 an id-expression. So, we try the functional cast, and if
7071 that doesn't work we fall back to the primary-expression. */
7072 cp_parser_parse_tentatively (parser
);
7073 /* Look for the simple-type-specifier. */
7074 ++parser
->prevent_constrained_type_specifiers
;
7075 type
= cp_parser_simple_type_specifier (parser
,
7076 /*decl_specs=*/NULL
,
7077 CP_PARSER_FLAGS_NONE
);
7078 --parser
->prevent_constrained_type_specifiers
;
7079 /* Parse the cast itself. */
7080 if (!cp_parser_error_occurred (parser
))
7082 = cp_parser_functional_cast (parser
, type
);
7083 /* If that worked, we're done. */
7084 if (cp_parser_parse_definitely (parser
))
7087 /* If the functional-cast didn't work out, try a
7088 compound-literal. */
7089 if (cp_parser_allow_gnu_extensions_p (parser
)
7090 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7092 cp_expr initializer
= NULL_TREE
;
7094 cp_parser_parse_tentatively (parser
);
7096 matching_parens parens
;
7097 parens
.consume_open (parser
);
7099 /* Avoid calling cp_parser_type_id pointlessly, see comment
7100 in cp_parser_cast_expression about c++/29234. */
7101 if (!cp_parser_compound_literal_p (parser
))
7102 cp_parser_simulate_error (parser
);
7105 /* Parse the type. */
7106 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7107 parser
->in_type_id_in_expr_p
= true;
7108 type
= cp_parser_type_id (parser
);
7109 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7110 parens
.require_close (parser
);
7113 /* If things aren't going well, there's no need to
7115 if (!cp_parser_error_occurred (parser
))
7117 bool non_constant_p
;
7118 /* Parse the brace-enclosed initializer list. */
7119 initializer
= cp_parser_braced_list (parser
,
7122 /* If that worked, we're definitely looking at a
7123 compound-literal expression. */
7124 if (cp_parser_parse_definitely (parser
))
7126 /* Warn the user that a compound literal is not
7127 allowed in standard C++. */
7128 pedwarn (input_location
, OPT_Wpedantic
,
7129 "ISO C++ forbids compound-literals");
7130 /* For simplicity, we disallow compound literals in
7131 constant-expressions. We could
7132 allow compound literals of integer type, whose
7133 initializer was a constant, in constant
7134 expressions. Permitting that usage, as a further
7135 extension, would not change the meaning of any
7136 currently accepted programs. (Of course, as
7137 compound literals are not part of ISO C++, the
7138 standard has nothing to say.) */
7139 if (cp_parser_non_integral_constant_expression (parser
,
7142 postfix_expression
= error_mark_node
;
7145 /* Form the representation of the compound-literal. */
7147 = finish_compound_literal (type
, initializer
,
7148 tf_warning_or_error
, fcl_c99
);
7149 postfix_expression
.set_location (initializer
.get_location ());
7154 /* It must be a primary-expression. */
7156 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7157 /*template_arg_p=*/false,
7164 /* Note that we don't need to worry about calling build_cplus_new on a
7165 class-valued CALL_EXPR in decltype when it isn't the end of the
7166 postfix-expression; unary_complex_lvalue will take care of that for
7169 /* Keep looping until the postfix-expression is complete. */
7172 if (idk
== CP_ID_KIND_UNQUALIFIED
7173 && identifier_p (postfix_expression
)
7174 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7175 /* It is not a Koenig lookup function call. */
7177 = unqualified_name_lookup_error (postfix_expression
);
7179 /* Peek at the next token. */
7180 token
= cp_lexer_peek_token (parser
->lexer
);
7182 switch (token
->type
)
7184 case CPP_OPEN_SQUARE
:
7185 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7187 cp_parser_error (parser
,
7188 "two consecutive %<[%> shall "
7189 "only introduce an attribute");
7190 return error_mark_node
;
7193 = cp_parser_postfix_open_square_expression (parser
,
7197 postfix_expression
.set_range (start_loc
,
7198 postfix_expression
.get_location ());
7200 idk
= CP_ID_KIND_NONE
;
7201 is_member_access
= false;
7204 case CPP_OPEN_PAREN
:
7205 /* postfix-expression ( expression-list [opt] ) */
7208 bool is_builtin_constant_p
;
7209 bool saved_integral_constant_expression_p
= false;
7210 bool saved_non_integral_constant_expression_p
= false;
7211 tsubst_flags_t complain
= complain_flags (decltype_p
);
7212 vec
<tree
, va_gc
> *args
;
7213 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7215 is_member_access
= false;
7217 tree stripped_expression
7218 = tree_strip_any_location_wrapper (postfix_expression
);
7219 is_builtin_constant_p
7220 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression
);
7221 if (is_builtin_constant_p
)
7223 /* The whole point of __builtin_constant_p is to allow
7224 non-constant expressions to appear as arguments. */
7225 saved_integral_constant_expression_p
7226 = parser
->integral_constant_expression_p
;
7227 saved_non_integral_constant_expression_p
7228 = parser
->non_integral_constant_expression_p
;
7229 parser
->integral_constant_expression_p
= false;
7231 args
= (cp_parser_parenthesized_expression_list
7233 /*cast_p=*/false, /*allow_expansion_p=*/true,
7234 /*non_constant_p=*/NULL
,
7235 /*close_paren_loc=*/&close_paren_loc
,
7236 /*wrap_locations_p=*/true));
7237 if (is_builtin_constant_p
)
7239 parser
->integral_constant_expression_p
7240 = saved_integral_constant_expression_p
;
7241 parser
->non_integral_constant_expression_p
7242 = saved_non_integral_constant_expression_p
;
7247 postfix_expression
= error_mark_node
;
7251 /* Function calls are not permitted in
7252 constant-expressions. */
7253 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7254 && cp_parser_non_integral_constant_expression (parser
,
7257 postfix_expression
= error_mark_node
;
7258 release_tree_vector (args
);
7263 if (idk
== CP_ID_KIND_UNQUALIFIED
7264 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7266 if (identifier_p (postfix_expression
)
7267 /* In C++2A, we may need to perform ADL for a template
7269 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7270 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7272 if (!args
->is_empty ())
7275 if (!any_type_dependent_arguments_p (args
))
7277 = perform_koenig_lookup (postfix_expression
, args
,
7282 = unqualified_fn_lookup_error (postfix_expression
);
7284 /* We do not perform argument-dependent lookup if
7285 normal lookup finds a non-function, in accordance
7286 with the expected resolution of DR 218. */
7287 else if (!args
->is_empty ()
7288 && is_overloaded_fn (postfix_expression
))
7290 /* We only need to look at the first function,
7291 because all the fns share the attribute we're
7292 concerned with (all member fns or all local
7294 tree fn
= get_first_fn (postfix_expression
);
7295 fn
= STRIP_TEMPLATE (fn
);
7297 /* Do not do argument dependent lookup if regular
7298 lookup finds a member function or a block-scope
7299 function declaration. [basic.lookup.argdep]/3 */
7300 if (!((TREE_CODE (fn
) == USING_DECL
&& DECL_DEPENDENT_P (fn
))
7301 || DECL_FUNCTION_MEMBER_P (fn
)
7302 || DECL_LOCAL_FUNCTION_P (fn
)))
7305 if (!any_type_dependent_arguments_p (args
))
7307 = perform_koenig_lookup (postfix_expression
, args
,
7313 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7315 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7316 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7318 if (processing_template_decl
7319 && (type_dependent_object_expression_p (instance
)
7320 || (!BASELINK_P (fn
)
7321 && TREE_CODE (fn
) != FIELD_DECL
)
7322 || type_dependent_expression_p (fn
)
7323 || any_type_dependent_arguments_p (args
)))
7325 maybe_generic_this_capture (instance
, fn
);
7327 = build_min_nt_call_vec (postfix_expression
, args
);
7328 release_tree_vector (args
);
7332 if (BASELINK_P (fn
))
7335 = (build_new_method_call
7336 (instance
, fn
, &args
, NULL_TREE
,
7337 (idk
== CP_ID_KIND_QUALIFIED
7338 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7345 = finish_call_expr (postfix_expression
, &args
,
7346 /*disallow_virtual=*/false,
7350 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7351 || TREE_CODE (postfix_expression
) == MEMBER_REF
7352 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7353 postfix_expression
= (build_offset_ref_call_from_tree
7354 (postfix_expression
, &args
,
7356 else if (idk
== CP_ID_KIND_QUALIFIED
)
7357 /* A call to a static class member, or a namespace-scope
7360 = finish_call_expr (postfix_expression
, &args
,
7361 /*disallow_virtual=*/true,
7365 /* All other function calls. */
7367 = finish_call_expr (postfix_expression
, &args
,
7368 /*disallow_virtual=*/false,
7372 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7374 location_t combined_loc
= make_location (token
->location
,
7377 postfix_expression
.set_location (combined_loc
);
7380 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7381 idk
= CP_ID_KIND_NONE
;
7383 release_tree_vector (args
);
7389 /* postfix-expression . template [opt] id-expression
7390 postfix-expression . pseudo-destructor-name
7391 postfix-expression -> template [opt] id-expression
7392 postfix-expression -> pseudo-destructor-name */
7394 /* Consume the `.' or `->' operator. */
7395 cp_lexer_consume_token (parser
->lexer
);
7398 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7402 is_member_access
= true;
7406 /* postfix-expression ++ */
7407 /* Consume the `++' token. */
7408 cp_lexer_consume_token (parser
->lexer
);
7409 /* Generate a representation for the complete expression. */
7411 = finish_increment_expr (postfix_expression
,
7412 POSTINCREMENT_EXPR
);
7413 /* Increments may not appear in constant-expressions. */
7414 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7415 postfix_expression
= error_mark_node
;
7416 idk
= CP_ID_KIND_NONE
;
7417 is_member_access
= false;
7420 case CPP_MINUS_MINUS
:
7421 /* postfix-expression -- */
7422 /* Consume the `--' token. */
7423 cp_lexer_consume_token (parser
->lexer
);
7424 /* Generate a representation for the complete expression. */
7426 = finish_increment_expr (postfix_expression
,
7427 POSTDECREMENT_EXPR
);
7428 /* Decrements may not appear in constant-expressions. */
7429 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7430 postfix_expression
= error_mark_node
;
7431 idk
= CP_ID_KIND_NONE
;
7432 is_member_access
= false;
7436 if (pidk_return
!= NULL
)
7437 * pidk_return
= idk
;
7438 if (member_access_only_p
)
7439 return is_member_access
7440 ? postfix_expression
7441 : cp_expr (error_mark_node
);
7443 return postfix_expression
;
7447 /* We should never get here. */
7449 return error_mark_node
;
7452 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7453 by cp_parser_builtin_offsetof. We're looking for
7455 postfix-expression [ expression ]
7456 postfix-expression [ braced-init-list ] (C++11)
7458 FOR_OFFSETOF is set if we're being called in that context, which
7459 changes how we deal with integer constant expressions. */
7462 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7463 tree postfix_expression
,
7467 tree index
= NULL_TREE
;
7468 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7469 bool saved_greater_than_is_operator_p
;
7471 /* Consume the `[' token. */
7472 cp_lexer_consume_token (parser
->lexer
);
7474 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7475 parser
->greater_than_is_operator_p
= true;
7477 /* Parse the index expression. */
7478 /* ??? For offsetof, there is a question of what to allow here. If
7479 offsetof is not being used in an integral constant expression context,
7480 then we *could* get the right answer by computing the value at runtime.
7481 If we are in an integral constant expression context, then we might
7482 could accept any constant expression; hard to say without analysis.
7483 Rather than open the barn door too wide right away, allow only integer
7484 constant expressions here. */
7486 index
= cp_parser_constant_expression (parser
);
7489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7491 bool expr_nonconst_p
;
7492 cp_lexer_set_source_position (parser
->lexer
);
7493 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7494 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7497 index
= cp_parser_expression (parser
);
7500 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7502 /* Look for the closing `]'. */
7503 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7505 /* Build the ARRAY_REF. */
7506 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7509 /* When not doing offsetof, array references are not permitted in
7510 constant-expressions. */
7512 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7513 postfix_expression
= error_mark_node
;
7515 return postfix_expression
;
7518 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7519 dereference of incomplete type, returns true if error_mark_node should
7520 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7521 and *DEPENDENT_P. */
7524 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7527 /* In a template, be permissive by treating an object expression
7528 of incomplete type as dependent (after a pedwarn). */
7529 diagnostic_t kind
= (processing_template_decl
7530 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7532 switch (TREE_CODE (*postfix_expression
))
7535 case REINTERPRET_CAST_EXPR
:
7536 case CONST_CAST_EXPR
:
7537 case STATIC_CAST_EXPR
:
7538 case DYNAMIC_CAST_EXPR
:
7539 case IMPLICIT_CONV_EXPR
:
7540 case VIEW_CONVERT_EXPR
:
7541 case NON_LVALUE_EXPR
:
7545 /* Don't emit any diagnostic for OVERLOADs. */
7549 /* Avoid clobbering e.g. DECLs. */
7550 if (!EXPR_P (*postfix_expression
))
7555 if (kind
== DK_IGNORED
)
7558 location_t exploc
= location_of (*postfix_expression
);
7559 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7560 if (!MAYBE_CLASS_TYPE_P (*scope
))
7562 if (kind
== DK_ERROR
)
7563 *scope
= *postfix_expression
= error_mark_node
;
7564 else if (processing_template_decl
)
7566 *dependent_p
= true;
7567 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7572 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7573 by cp_parser_builtin_offsetof. We're looking for
7575 postfix-expression . template [opt] id-expression
7576 postfix-expression . pseudo-destructor-name
7577 postfix-expression -> template [opt] id-expression
7578 postfix-expression -> pseudo-destructor-name
7580 FOR_OFFSETOF is set if we're being called in that context. That sorta
7581 limits what of the above we'll actually accept, but nevermind.
7582 TOKEN_TYPE is the "." or "->" token, which will already have been
7583 removed from the stream. */
7586 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7587 enum cpp_ttype token_type
,
7588 cp_expr postfix_expression
,
7589 bool for_offsetof
, cp_id_kind
*idk
,
7590 location_t location
)
7594 bool pseudo_destructor_p
;
7595 tree scope
= NULL_TREE
;
7596 location_t start_loc
= postfix_expression
.get_start ();
7598 /* If this is a `->' operator, dereference the pointer. */
7599 if (token_type
== CPP_DEREF
)
7600 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7601 tf_warning_or_error
);
7602 /* Check to see whether or not the expression is type-dependent and
7603 not the current instantiation. */
7604 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7605 /* The identifier following the `->' or `.' is not qualified. */
7606 parser
->scope
= NULL_TREE
;
7607 parser
->qualifying_scope
= NULL_TREE
;
7608 parser
->object_scope
= NULL_TREE
;
7609 *idk
= CP_ID_KIND_NONE
;
7611 /* Enter the scope corresponding to the type of the object
7612 given by the POSTFIX_EXPRESSION. */
7615 scope
= TREE_TYPE (postfix_expression
);
7616 /* According to the standard, no expression should ever have
7617 reference type. Unfortunately, we do not currently match
7618 the standard in this respect in that our internal representation
7619 of an expression may have reference type even when the standard
7620 says it does not. Therefore, we have to manually obtain the
7621 underlying type here. */
7622 scope
= non_reference (scope
);
7623 /* The type of the POSTFIX_EXPRESSION must be complete. */
7624 /* Unlike the object expression in other contexts, *this is not
7625 required to be of complete type for purposes of class member
7626 access (5.2.5) outside the member function body. */
7627 if (postfix_expression
!= current_class_ref
7628 && scope
!= error_mark_node
7629 && !currently_open_class (scope
))
7631 scope
= complete_type (scope
);
7632 if (!COMPLETE_TYPE_P (scope
)
7633 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7635 return error_mark_node
;
7640 /* Let the name lookup machinery know that we are processing a
7641 class member access expression. */
7642 parser
->context
->object_type
= scope
;
7643 /* If something went wrong, we want to be able to discern that case,
7644 as opposed to the case where there was no SCOPE due to the type
7645 of expression being dependent. */
7647 scope
= error_mark_node
;
7648 /* If the SCOPE was erroneous, make the various semantic analysis
7649 functions exit quickly -- and without issuing additional error
7651 if (scope
== error_mark_node
)
7652 postfix_expression
= error_mark_node
;
7657 /* Tell cp_parser_lookup_name that there was an object, even though it's
7659 parser
->context
->object_type
= unknown_type_node
;
7661 /* Assume this expression is not a pseudo-destructor access. */
7662 pseudo_destructor_p
= false;
7664 /* If the SCOPE is a scalar type, then, if this is a valid program,
7665 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7666 is type dependent, it can be pseudo-destructor-name or something else.
7667 Try to parse it as pseudo-destructor-name first. */
7668 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7673 cp_parser_parse_tentatively (parser
);
7674 /* Parse the pseudo-destructor-name. */
7676 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7679 && (cp_parser_error_occurred (parser
)
7680 || !SCALAR_TYPE_P (type
)))
7681 cp_parser_abort_tentative_parse (parser
);
7682 else if (cp_parser_parse_definitely (parser
))
7684 pseudo_destructor_p
= true;
7686 = finish_pseudo_destructor_expr (postfix_expression
,
7691 if (!pseudo_destructor_p
)
7693 /* If the SCOPE is not a scalar type, we are looking at an
7694 ordinary class member access expression, rather than a
7695 pseudo-destructor-name. */
7697 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7698 /* Parse the id-expression. */
7699 name
= (cp_parser_id_expression
7701 cp_parser_optional_template_keyword (parser
),
7702 /*check_dependency_p=*/true,
7704 /*declarator_p=*/false,
7705 /*optional_p=*/false));
7706 /* In general, build a SCOPE_REF if the member name is qualified.
7707 However, if the name was not dependent and has already been
7708 resolved; there is no need to build the SCOPE_REF. For example;
7710 struct X { void f(); };
7711 template <typename T> void f(T* t) { t->X::f(); }
7713 Even though "t" is dependent, "X::f" is not and has been resolved
7714 to a BASELINK; there is no need to include scope information. */
7716 /* But we do need to remember that there was an explicit scope for
7717 virtual function calls. */
7719 *idk
= CP_ID_KIND_QUALIFIED
;
7721 /* If the name is a template-id that names a type, we will get a
7722 TYPE_DECL here. That is invalid code. */
7723 if (TREE_CODE (name
) == TYPE_DECL
)
7725 error_at (token
->location
, "invalid use of %qD", name
);
7726 postfix_expression
= error_mark_node
;
7730 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7732 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7734 error_at (token
->location
, "%<%D::%D%> is not a class member",
7735 parser
->scope
, name
);
7736 postfix_expression
= error_mark_node
;
7739 name
= build_qualified_name (/*type=*/NULL_TREE
,
7743 parser
->scope
= NULL_TREE
;
7744 parser
->qualifying_scope
= NULL_TREE
;
7745 parser
->object_scope
= NULL_TREE
;
7747 if (parser
->scope
&& name
&& BASELINK_P (name
))
7748 adjust_result_of_qualified_name_lookup
7749 (name
, parser
->scope
, scope
);
7751 = finish_class_member_access_expr (postfix_expression
, name
,
7753 tf_warning_or_error
);
7754 /* Build a location e.g.:
7757 where the caret is at the deref token, ranging from
7758 the start of postfix_expression to the end of the access expr. */
7760 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7761 location_t combined_loc
7762 = make_location (input_location
, start_loc
, end_loc
);
7763 protected_set_expr_location (postfix_expression
, combined_loc
);
7767 /* We no longer need to look up names in the scope of the object on
7768 the left-hand side of the `.' or `->' operator. */
7769 parser
->context
->object_type
= NULL_TREE
;
7771 /* Outside of offsetof, these operators may not appear in
7772 constant-expressions. */
7774 && (cp_parser_non_integral_constant_expression
7775 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7776 postfix_expression
= error_mark_node
;
7778 return postfix_expression
;
7781 /* Parse a parenthesized expression-list.
7784 assignment-expression
7785 expression-list, assignment-expression
7790 identifier, expression-list
7792 CAST_P is true if this expression is the target of a cast.
7794 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7797 WRAP_LOCATIONS_P is true if expressions within this list for which
7798 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7799 their source locations.
7801 Returns a vector of trees. Each element is a representation of an
7802 assignment-expression. NULL is returned if the ( and or ) are
7803 missing. An empty, but allocated, vector is returned on no
7804 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7805 if we are parsing an attribute list for an attribute that wants a
7806 plain identifier argument, normal_attr for an attribute that wants
7807 an expression, or non_attr if we aren't parsing an attribute list. If
7808 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7809 not all of the expressions in the list were constant.
7810 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7811 will be written to with the location of the closing parenthesis. If
7812 an error occurs, it may or may not be written to. */
7814 static vec
<tree
, va_gc
> *
7815 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7816 int is_attribute_list
,
7818 bool allow_expansion_p
,
7819 bool *non_constant_p
,
7820 location_t
*close_paren_loc
,
7821 bool wrap_locations_p
)
7823 vec
<tree
, va_gc
> *expression_list
;
7824 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7825 tree identifier
= NULL_TREE
;
7826 bool saved_greater_than_is_operator_p
;
7828 /* Assume all the expressions will be constant. */
7830 *non_constant_p
= false;
7832 matching_parens parens
;
7833 if (!parens
.require_open (parser
))
7836 expression_list
= make_tree_vector ();
7838 /* Within a parenthesized expression, a `>' token is always
7839 the greater-than operator. */
7840 saved_greater_than_is_operator_p
7841 = parser
->greater_than_is_operator_p
;
7842 parser
->greater_than_is_operator_p
= true;
7844 cp_expr
expr (NULL_TREE
);
7846 /* Consume expressions until there are no more. */
7847 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7850 /* At the beginning of attribute lists, check to see if the
7851 next token is an identifier. */
7852 if (is_attribute_list
== id_attr
7853 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7857 /* Consume the identifier. */
7858 token
= cp_lexer_consume_token (parser
->lexer
);
7859 /* Save the identifier. */
7860 identifier
= token
->u
.value
;
7864 bool expr_non_constant_p
;
7866 /* Parse the next assignment-expression. */
7867 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7869 /* A braced-init-list. */
7870 cp_lexer_set_source_position (parser
->lexer
);
7871 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7872 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7873 if (non_constant_p
&& expr_non_constant_p
)
7874 *non_constant_p
= true;
7876 else if (non_constant_p
)
7878 expr
= (cp_parser_constant_expression
7879 (parser
, /*allow_non_constant_p=*/true,
7880 &expr_non_constant_p
));
7881 if (expr_non_constant_p
)
7882 *non_constant_p
= true;
7885 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7889 expr
= instantiate_non_dependent_expr (expr
);
7891 /* If we have an ellipsis, then this is an expression
7893 if (allow_expansion_p
7894 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7896 /* Consume the `...'. */
7897 cp_lexer_consume_token (parser
->lexer
);
7899 /* Build the argument pack. */
7900 expr
= make_pack_expansion (expr
);
7903 if (wrap_locations_p
)
7904 expr
.maybe_add_location_wrapper ();
7906 /* Add it to the list. We add error_mark_node
7907 expressions to the list, so that we can still tell if
7908 the correct form for a parenthesized expression-list
7909 is found. That gives better errors. */
7910 vec_safe_push (expression_list
, expr
.get_value ());
7912 if (expr
== error_mark_node
)
7916 /* After the first item, attribute lists look the same as
7917 expression lists. */
7918 is_attribute_list
= non_attr
;
7921 /* If the next token isn't a `,', then we are done. */
7922 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7925 /* Otherwise, consume the `,' and keep going. */
7926 cp_lexer_consume_token (parser
->lexer
);
7929 if (close_paren_loc
)
7930 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7932 if (!parens
.require_close (parser
))
7937 /* We try and resync to an unnested comma, as that will give the
7938 user better diagnostics. */
7939 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7940 /*recovering=*/true,
7942 /*consume_paren=*/true);
7947 parser
->greater_than_is_operator_p
7948 = saved_greater_than_is_operator_p
;
7953 parser
->greater_than_is_operator_p
7954 = saved_greater_than_is_operator_p
;
7957 vec_safe_insert (expression_list
, 0, identifier
);
7959 return expression_list
;
7962 /* Parse a pseudo-destructor-name.
7964 pseudo-destructor-name:
7965 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7966 :: [opt] nested-name-specifier template template-id :: ~ type-name
7967 :: [opt] nested-name-specifier [opt] ~ type-name
7969 If either of the first two productions is used, sets *SCOPE to the
7970 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7971 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7972 or ERROR_MARK_NODE if the parse fails. */
7975 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7980 bool nested_name_specifier_p
;
7983 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7984 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7985 && !type_dependent_expression_p (object
))
7987 if (cxx_dialect
< cxx14
)
7988 pedwarn (input_location
, 0,
7989 "%<~auto%> only available with "
7990 "-std=c++14 or -std=gnu++14");
7991 cp_lexer_consume_token (parser
->lexer
);
7992 cp_lexer_consume_token (parser
->lexer
);
7994 *type
= TREE_TYPE (object
);
7998 /* Assume that things will not work out. */
7999 *type
= error_mark_node
;
8001 /* Look for the optional `::' operator. */
8002 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
8003 /* Look for the optional nested-name-specifier. */
8004 nested_name_specifier_p
8005 = (cp_parser_nested_name_specifier_opt (parser
,
8006 /*typename_keyword_p=*/false,
8007 /*check_dependency_p=*/true,
8009 /*is_declaration=*/false)
8011 /* Now, if we saw a nested-name-specifier, we might be doing the
8012 second production. */
8013 if (nested_name_specifier_p
8014 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8016 /* Consume the `template' keyword. */
8017 cp_lexer_consume_token (parser
->lexer
);
8018 /* Parse the template-id. */
8019 cp_parser_template_id (parser
,
8020 /*template_keyword_p=*/true,
8021 /*check_dependency_p=*/false,
8023 /*is_declaration=*/true);
8024 /* Look for the `::' token. */
8025 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8027 /* If the next token is not a `~', then there might be some
8028 additional qualification. */
8029 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
8031 /* At this point, we're looking for "type-name :: ~". The type-name
8032 must not be a class-name, since this is a pseudo-destructor. So,
8033 it must be either an enum-name, or a typedef-name -- both of which
8034 are just identifiers. So, we peek ahead to check that the "::"
8035 and "~" tokens are present; if they are not, then we can avoid
8036 calling type_name. */
8037 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
8038 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
8039 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
8041 cp_parser_error (parser
, "non-scalar type");
8045 /* Look for the type-name. */
8046 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8047 if (*scope
== error_mark_node
)
8050 /* Look for the `::' token. */
8051 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8056 /* Look for the `~'. */
8057 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
8059 /* Once we see the ~, this has to be a pseudo-destructor. */
8060 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
8061 cp_parser_commit_to_topmost_tentative_parse (parser
);
8063 /* Look for the type-name again. We are not responsible for
8064 checking that it matches the first type-name. */
8065 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8068 /* Parse a unary-expression.
8074 unary-operator cast-expression
8075 sizeof unary-expression
8077 alignof ( type-id ) [C++0x]
8084 __extension__ cast-expression
8085 __alignof__ unary-expression
8086 __alignof__ ( type-id )
8087 alignof unary-expression [C++0x]
8088 __real__ cast-expression
8089 __imag__ cast-expression
8091 sizeof ( type-id ) { initializer-list , [opt] }
8092 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8093 __alignof__ ( type-id ) { initializer-list , [opt] }
8095 ADDRESS_P is true iff the unary-expression is appearing as the
8096 operand of the `&' operator. CAST_P is true if this expression is
8097 the target of a cast.
8099 Returns a representation of the expression. */
8102 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8103 bool address_p
, bool cast_p
, bool decltype_p
)
8106 enum tree_code unary_operator
;
8108 /* Peek at the next token. */
8109 token
= cp_lexer_peek_token (parser
->lexer
);
8110 /* Some keywords give away the kind of expression. */
8111 if (token
->type
== CPP_KEYWORD
)
8113 enum rid keyword
= token
->keyword
;
8122 location_t start_loc
= token
->location
;
8124 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8125 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8127 /* Consume the token. */
8128 cp_lexer_consume_token (parser
->lexer
);
8129 /* Parse the operand. */
8130 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8132 if (TYPE_P (operand
))
8133 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8137 /* ISO C++ defines alignof only with types, not with
8138 expressions. So pedwarn if alignof is used with a non-
8139 type expression. However, __alignof__ is ok. */
8141 pedwarn (token
->location
, OPT_Wpedantic
,
8142 "ISO C++ does not allow %<alignof%> "
8145 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8147 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8148 SIZEOF_EXPR with the original operand. */
8149 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8151 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8153 if (!processing_template_decl
&& TYPE_P (operand
))
8155 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8156 build1 (NOP_EXPR
, operand
,
8158 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8161 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8162 TREE_SIDE_EFFECTS (ret
) = 0;
8163 TREE_READONLY (ret
) = 1;
8167 /* Construct a location e.g. :
8170 with start == caret at the start of the "alignof"/"sizeof"
8171 token, with the endpoint at the final closing paren. */
8172 location_t finish_loc
8173 = cp_lexer_previous_token (parser
->lexer
)->location
;
8174 location_t compound_loc
8175 = make_location (start_loc
, start_loc
, finish_loc
);
8177 cp_expr
ret_expr (ret
);
8178 ret_expr
.set_location (compound_loc
);
8179 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8183 case RID_BUILTIN_HAS_ATTRIBUTE
:
8184 return cp_parser_has_attribute_expression (parser
);
8187 return cp_parser_new_expression (parser
);
8190 return cp_parser_delete_expression (parser
);
8194 /* The saved value of the PEDANTIC flag. */
8198 /* Save away the PEDANTIC flag. */
8199 cp_parser_extension_opt (parser
, &saved_pedantic
);
8200 /* Parse the cast-expression. */
8201 expr
= cp_parser_simple_cast_expression (parser
);
8202 /* Restore the PEDANTIC flag. */
8203 pedantic
= saved_pedantic
;
8213 /* Consume the `__real__' or `__imag__' token. */
8214 cp_lexer_consume_token (parser
->lexer
);
8215 /* Parse the cast-expression. */
8216 expression
= cp_parser_simple_cast_expression (parser
);
8217 /* Create the complete representation. */
8218 return build_x_unary_op (token
->location
,
8219 (keyword
== RID_REALPART
8220 ? REALPART_EXPR
: IMAGPART_EXPR
),
8222 tf_warning_or_error
);
8226 case RID_TRANSACTION_ATOMIC
:
8227 case RID_TRANSACTION_RELAXED
:
8228 return cp_parser_transaction_expression (parser
, keyword
);
8233 const char *saved_message
;
8234 bool saved_integral_constant_expression_p
;
8235 bool saved_non_integral_constant_expression_p
;
8236 bool saved_greater_than_is_operator_p
;
8238 location_t start_loc
= token
->location
;
8240 cp_lexer_consume_token (parser
->lexer
);
8241 matching_parens parens
;
8242 parens
.require_open (parser
);
8244 saved_message
= parser
->type_definition_forbidden_message
;
8245 parser
->type_definition_forbidden_message
8246 = G_("types may not be defined in %<noexcept%> expressions");
8248 saved_integral_constant_expression_p
8249 = parser
->integral_constant_expression_p
;
8250 saved_non_integral_constant_expression_p
8251 = parser
->non_integral_constant_expression_p
;
8252 parser
->integral_constant_expression_p
= false;
8254 saved_greater_than_is_operator_p
8255 = parser
->greater_than_is_operator_p
;
8256 parser
->greater_than_is_operator_p
= true;
8258 ++cp_unevaluated_operand
;
8259 ++c_inhibit_evaluation_warnings
;
8260 ++cp_noexcept_operand
;
8261 expr
= cp_parser_expression (parser
);
8262 --cp_noexcept_operand
;
8263 --c_inhibit_evaluation_warnings
;
8264 --cp_unevaluated_operand
;
8266 parser
->greater_than_is_operator_p
8267 = saved_greater_than_is_operator_p
;
8269 parser
->integral_constant_expression_p
8270 = saved_integral_constant_expression_p
;
8271 parser
->non_integral_constant_expression_p
8272 = saved_non_integral_constant_expression_p
;
8274 parser
->type_definition_forbidden_message
= saved_message
;
8276 location_t finish_loc
8277 = cp_lexer_peek_token (parser
->lexer
)->location
;
8278 parens
.require_close (parser
);
8280 /* Construct a location of the form:
8283 with start == caret, finishing at the close-paren. */
8284 location_t noexcept_loc
8285 = make_location (start_loc
, start_loc
, finish_loc
);
8287 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8296 /* Look for the `:: new' and `:: delete', which also signal the
8297 beginning of a new-expression, or delete-expression,
8298 respectively. If the next token is `::', then it might be one of
8300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8304 /* See if the token after the `::' is one of the keywords in
8305 which we're interested. */
8306 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8307 /* If it's `new', we have a new-expression. */
8308 if (keyword
== RID_NEW
)
8309 return cp_parser_new_expression (parser
);
8310 /* Similarly, for `delete'. */
8311 else if (keyword
== RID_DELETE
)
8312 return cp_parser_delete_expression (parser
);
8315 /* Look for a unary operator. */
8316 unary_operator
= cp_parser_unary_operator (token
);
8317 /* The `++' and `--' operators can be handled similarly, even though
8318 they are not technically unary-operators in the grammar. */
8319 if (unary_operator
== ERROR_MARK
)
8321 if (token
->type
== CPP_PLUS_PLUS
)
8322 unary_operator
= PREINCREMENT_EXPR
;
8323 else if (token
->type
== CPP_MINUS_MINUS
)
8324 unary_operator
= PREDECREMENT_EXPR
;
8325 /* Handle the GNU address-of-label extension. */
8326 else if (cp_parser_allow_gnu_extensions_p (parser
)
8327 && token
->type
== CPP_AND_AND
)
8331 location_t start_loc
= token
->location
;
8333 /* Consume the '&&' token. */
8334 cp_lexer_consume_token (parser
->lexer
);
8335 /* Look for the identifier. */
8336 location_t finish_loc
8337 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8338 identifier
= cp_parser_identifier (parser
);
8339 /* Construct a location of the form:
8342 with caret==start at the "&&", finish at the end of the label. */
8343 location_t combined_loc
8344 = make_location (start_loc
, start_loc
, finish_loc
);
8345 /* Create an expression representing the address. */
8346 expression
= finish_label_address_expr (identifier
, combined_loc
);
8347 if (cp_parser_non_integral_constant_expression (parser
,
8349 expression
= error_mark_node
;
8353 if (unary_operator
!= ERROR_MARK
)
8355 cp_expr cast_expression
;
8356 cp_expr expression
= error_mark_node
;
8357 non_integral_constant non_constant_p
= NIC_NONE
;
8358 location_t loc
= token
->location
;
8359 tsubst_flags_t complain
= complain_flags (decltype_p
);
8361 /* Consume the operator token. */
8362 token
= cp_lexer_consume_token (parser
->lexer
);
8363 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8365 /* Parse the cast-expression. */
8367 = cp_parser_cast_expression (parser
,
8368 unary_operator
== ADDR_EXPR
,
8374 OP_TOKEN CAST_EXPRESSION
8375 ^~~~~~~~~~~~~~~~~~~~~~~~~
8376 with start==caret at the operator token, and
8377 extending to the end of the cast_expression. */
8378 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8380 /* Now, build an appropriate representation. */
8381 switch (unary_operator
)
8384 non_constant_p
= NIC_STAR
;
8385 expression
= build_x_indirect_ref (loc
, cast_expression
,
8388 /* TODO: build_x_indirect_ref does not always honor the
8389 location, so ensure it is set. */
8390 expression
.set_location (loc
);
8394 non_constant_p
= NIC_ADDR
;
8397 expression
= build_x_unary_op (loc
, unary_operator
,
8400 /* TODO: build_x_unary_op does not always honor the location,
8401 so ensure it is set. */
8402 expression
.set_location (loc
);
8405 case PREINCREMENT_EXPR
:
8406 case PREDECREMENT_EXPR
:
8407 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8408 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8411 /* Immediately fold negation of a constant, unless the constant is 0
8412 (since -0 == 0) or it would overflow. */
8413 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
)
8416 = tree_strip_any_location_wrapper (cast_expression
);
8417 if (CONSTANT_CLASS_P (stripped_expr
)
8418 && !integer_zerop (stripped_expr
)
8419 && !TREE_OVERFLOW (stripped_expr
))
8421 tree folded
= fold_build1 (unary_operator
,
8422 TREE_TYPE (stripped_expr
),
8424 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8426 expression
= maybe_wrap_with_location (folded
, loc
);
8432 case UNARY_PLUS_EXPR
:
8433 case TRUTH_NOT_EXPR
:
8434 expression
= finish_unary_op_expr (loc
, unary_operator
,
8435 cast_expression
, complain
);
8442 if (non_constant_p
!= NIC_NONE
8443 && cp_parser_non_integral_constant_expression (parser
,
8445 expression
= error_mark_node
;
8450 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8451 /*member_access_only_p=*/false,
8456 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8457 unary-operator, the corresponding tree code is returned. */
8459 static enum tree_code
8460 cp_parser_unary_operator (cp_token
* token
)
8462 switch (token
->type
)
8465 return INDIRECT_REF
;
8471 return UNARY_PLUS_EXPR
;
8477 return TRUTH_NOT_EXPR
;
8480 return BIT_NOT_EXPR
;
8487 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8488 Returns a representation of the expression. */
8491 cp_parser_has_attribute_expression (cp_parser
*parser
)
8493 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8495 /* Consume the __builtin_has_attribute token. */
8496 cp_lexer_consume_token (parser
->lexer
);
8498 matching_parens parens
;
8499 if (!parens
.require_open (parser
))
8500 return error_mark_node
;
8502 /* Types cannot be defined in a `sizeof' expression. Save away the
8504 const char *saved_message
= parser
->type_definition_forbidden_message
;
8505 /* And create the new one. */
8506 const int kwd
= RID_BUILTIN_HAS_ATTRIBUTE
;
8507 char *tmp
= concat ("types may not be defined in %<",
8508 IDENTIFIER_POINTER (ridpointers
[kwd
]),
8509 "%> expressions", NULL
);
8510 parser
->type_definition_forbidden_message
= tmp
;
8512 /* The restrictions on constant-expressions do not apply inside
8513 sizeof expressions. */
8514 bool saved_integral_constant_expression_p
8515 = parser
->integral_constant_expression_p
;
8516 bool saved_non_integral_constant_expression_p
8517 = parser
->non_integral_constant_expression_p
;
8518 parser
->integral_constant_expression_p
= false;
8520 /* Do not actually evaluate the expression. */
8521 ++cp_unevaluated_operand
;
8522 ++c_inhibit_evaluation_warnings
;
8524 tree oper
= NULL_TREE
;
8526 /* We can't be sure yet whether we're looking at a type-id or an
8528 cp_parser_parse_tentatively (parser
);
8530 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8531 parser
->in_type_id_in_expr_p
= true;
8532 /* Look for the type-id. */
8533 oper
= cp_parser_type_id (parser
);
8534 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8536 cp_parser_parse_definitely (parser
);
8538 /* If the type-id production did not work out, then we must be
8539 looking at the unary-expression production. */
8540 if (!oper
|| oper
== error_mark_node
)
8541 oper
= cp_parser_unary_expression (parser
);
8543 STRIP_ANY_LOCATION_WRAPPER (oper
);
8545 /* Go back to evaluating expressions. */
8546 --cp_unevaluated_operand
;
8547 --c_inhibit_evaluation_warnings
;
8549 /* Free the message we created. */
8551 /* And restore the old one. */
8552 parser
->type_definition_forbidden_message
= saved_message
;
8553 parser
->integral_constant_expression_p
8554 = saved_integral_constant_expression_p
;
8555 parser
->non_integral_constant_expression_p
8556 = saved_non_integral_constant_expression_p
;
8558 /* Consume the comma if it's there. */
8559 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
8561 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8562 /*consume_paren=*/true);
8563 return error_mark_node
;
8566 /* Parse the attribute specification. */
8568 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8569 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
8571 if (oper
!= error_mark_node
)
8573 /* Fold constant expressions used in attributes first. */
8574 cp_check_const_attributes (attr
);
8576 /* Finally, see if OPER has been declared with ATTR. */
8577 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
8580 parens
.require_close (parser
);
8584 error_at (atloc
, "expected identifier");
8585 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8588 /* Construct a location e.g. :
8589 __builtin_has_attribute (oper, attr)
8590 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8591 with start == caret at the start of the built-in token,
8592 and with the endpoint at the final closing paren. */
8593 location_t finish_loc
8594 = cp_lexer_previous_token (parser
->lexer
)->location
;
8595 location_t compound_loc
8596 = make_location (start_loc
, start_loc
, finish_loc
);
8598 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
8599 ret_expr
.set_location (compound_loc
);
8600 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8604 /* Parse a new-expression.
8607 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8608 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8610 Returns a representation of the expression. */
8613 cp_parser_new_expression (cp_parser
* parser
)
8615 bool global_scope_p
;
8616 vec
<tree
, va_gc
> *placement
;
8618 vec
<tree
, va_gc
> *initializer
;
8619 tree nelts
= NULL_TREE
;
8622 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8624 /* Look for the optional `::' operator. */
8626 = (cp_parser_global_scope_opt (parser
,
8627 /*current_scope_valid_p=*/false)
8629 /* Look for the `new' operator. */
8630 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8631 /* There's no easy way to tell a new-placement from the
8632 `( type-id )' construct. */
8633 cp_parser_parse_tentatively (parser
);
8634 /* Look for a new-placement. */
8635 placement
= cp_parser_new_placement (parser
);
8636 /* If that didn't work out, there's no new-placement. */
8637 if (!cp_parser_parse_definitely (parser
))
8639 if (placement
!= NULL
)
8640 release_tree_vector (placement
);
8644 /* If the next token is a `(', then we have a parenthesized
8646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8649 const char *saved_message
= parser
->type_definition_forbidden_message
;
8651 /* Consume the `('. */
8652 matching_parens parens
;
8653 parens
.consume_open (parser
);
8655 /* Parse the type-id. */
8656 parser
->type_definition_forbidden_message
8657 = G_("types may not be defined in a new-expression");
8659 type_id_in_expr_sentinel
s (parser
);
8660 type
= cp_parser_type_id (parser
);
8662 parser
->type_definition_forbidden_message
= saved_message
;
8664 /* Look for the closing `)'. */
8665 parens
.require_close (parser
);
8666 token
= cp_lexer_peek_token (parser
->lexer
);
8667 /* There should not be a direct-new-declarator in this production,
8668 but GCC used to allowed this, so we check and emit a sensible error
8669 message for this case. */
8670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8672 error_at (token
->location
,
8673 "array bound forbidden after parenthesized type-id");
8674 inform (token
->location
,
8675 "try removing the parentheses around the type-id");
8676 cp_parser_direct_new_declarator (parser
);
8679 /* Otherwise, there must be a new-type-id. */
8681 type
= cp_parser_new_type_id (parser
, &nelts
);
8683 /* If the next token is a `(' or '{', then we have a new-initializer. */
8684 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8685 if (token
->type
== CPP_OPEN_PAREN
8686 || token
->type
== CPP_OPEN_BRACE
)
8687 initializer
= cp_parser_new_initializer (parser
);
8691 /* A new-expression may not appear in an integral constant
8693 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8694 ret
= error_mark_node
;
8695 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8696 of a new-type-id or type-id of a new-expression, the new-expression shall
8697 contain a new-initializer of the form ( assignment-expression )".
8698 Additionally, consistently with the spirit of DR 1467, we want to accept
8699 'new auto { 2 }' too. */
8700 else if ((ret
= type_uses_auto (type
))
8701 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8702 && (vec_safe_length (initializer
) != 1
8703 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8704 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8706 error_at (token
->location
,
8707 "initialization of new-expression for type %<auto%> "
8708 "requires exactly one element");
8709 ret
= error_mark_node
;
8713 /* Construct a location e.g.:
8716 with caret == start at the start of the "new" token, and the end
8717 at the end of the final token we consumed. */
8718 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8719 location_t end_loc
= get_finish (end_tok
->location
);
8720 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8722 /* Create a representation of the new-expression. */
8723 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8724 tf_warning_or_error
);
8725 protected_set_expr_location (ret
, combined_loc
);
8728 if (placement
!= NULL
)
8729 release_tree_vector (placement
);
8730 if (initializer
!= NULL
)
8731 release_tree_vector (initializer
);
8736 /* Parse a new-placement.
8741 Returns the same representation as for an expression-list. */
8743 static vec
<tree
, va_gc
> *
8744 cp_parser_new_placement (cp_parser
* parser
)
8746 vec
<tree
, va_gc
> *expression_list
;
8748 /* Parse the expression-list. */
8749 expression_list
= (cp_parser_parenthesized_expression_list
8750 (parser
, non_attr
, /*cast_p=*/false,
8751 /*allow_expansion_p=*/true,
8752 /*non_constant_p=*/NULL
));
8754 if (expression_list
&& expression_list
->is_empty ())
8755 error ("expected expression-list or type-id");
8757 return expression_list
;
8760 /* Parse a new-type-id.
8763 type-specifier-seq new-declarator [opt]
8765 Returns the TYPE allocated. If the new-type-id indicates an array
8766 type, *NELTS is set to the number of elements in the last array
8767 bound; the TYPE will not include the last array bound. */
8770 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8772 cp_decl_specifier_seq type_specifier_seq
;
8773 cp_declarator
*new_declarator
;
8774 cp_declarator
*declarator
;
8775 cp_declarator
*outer_declarator
;
8776 const char *saved_message
;
8778 /* The type-specifier sequence must not contain type definitions.
8779 (It cannot contain declarations of new types either, but if they
8780 are not definitions we will catch that because they are not
8782 saved_message
= parser
->type_definition_forbidden_message
;
8783 parser
->type_definition_forbidden_message
8784 = G_("types may not be defined in a new-type-id");
8785 /* Parse the type-specifier-seq. */
8786 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
8787 /*is_declaration=*/false,
8788 /*is_trailing_return=*/false,
8789 &type_specifier_seq
);
8790 /* Restore the old message. */
8791 parser
->type_definition_forbidden_message
= saved_message
;
8793 if (type_specifier_seq
.type
== error_mark_node
)
8794 return error_mark_node
;
8796 /* Parse the new-declarator. */
8797 new_declarator
= cp_parser_new_declarator_opt (parser
);
8799 /* Determine the number of elements in the last array dimension, if
8802 /* Skip down to the last array dimension. */
8803 declarator
= new_declarator
;
8804 outer_declarator
= NULL
;
8805 while (declarator
&& (declarator
->kind
== cdk_pointer
8806 || declarator
->kind
== cdk_ptrmem
))
8808 outer_declarator
= declarator
;
8809 declarator
= declarator
->declarator
;
8812 && declarator
->kind
== cdk_array
8813 && declarator
->declarator
8814 && declarator
->declarator
->kind
== cdk_array
)
8816 outer_declarator
= declarator
;
8817 declarator
= declarator
->declarator
;
8820 if (declarator
&& declarator
->kind
== cdk_array
)
8822 *nelts
= declarator
->u
.array
.bounds
;
8823 if (*nelts
== error_mark_node
)
8824 *nelts
= integer_one_node
;
8826 if (outer_declarator
)
8827 outer_declarator
->declarator
= declarator
->declarator
;
8829 new_declarator
= NULL
;
8832 return groktypename (&type_specifier_seq
, new_declarator
, false);
8835 /* Parse an (optional) new-declarator.
8838 ptr-operator new-declarator [opt]
8839 direct-new-declarator
8841 Returns the declarator. */
8843 static cp_declarator
*
8844 cp_parser_new_declarator_opt (cp_parser
* parser
)
8846 enum tree_code code
;
8847 tree type
, std_attributes
= NULL_TREE
;
8848 cp_cv_quals cv_quals
;
8850 /* We don't know if there's a ptr-operator next, or not. */
8851 cp_parser_parse_tentatively (parser
);
8852 /* Look for a ptr-operator. */
8853 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8854 /* If that worked, look for more new-declarators. */
8855 if (cp_parser_parse_definitely (parser
))
8857 cp_declarator
*declarator
;
8859 /* Parse another optional declarator. */
8860 declarator
= cp_parser_new_declarator_opt (parser
);
8862 declarator
= cp_parser_make_indirect_declarator
8863 (code
, type
, cv_quals
, declarator
, std_attributes
);
8868 /* If the next token is a `[', there is a direct-new-declarator. */
8869 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8870 return cp_parser_direct_new_declarator (parser
);
8875 /* Parse a direct-new-declarator.
8877 direct-new-declarator:
8879 direct-new-declarator [constant-expression]
8883 static cp_declarator
*
8884 cp_parser_direct_new_declarator (cp_parser
* parser
)
8886 cp_declarator
*declarator
= NULL
;
8893 /* Look for the opening `['. */
8894 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8896 token
= cp_lexer_peek_token (parser
->lexer
);
8897 expression
= cp_parser_expression (parser
);
8898 /* The standard requires that the expression have integral
8899 type. DR 74 adds enumeration types. We believe that the
8900 real intent is that these expressions be handled like the
8901 expression in a `switch' condition, which also allows
8902 classes with a single conversion to integral or
8903 enumeration type. */
8904 if (!processing_template_decl
)
8907 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8912 error_at (token
->location
,
8913 "expression in new-declarator must have integral "
8914 "or enumeration type");
8915 expression
= error_mark_node
;
8919 /* Look for the closing `]'. */
8920 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8922 /* Add this bound to the declarator. */
8923 declarator
= make_array_declarator (declarator
, expression
);
8925 /* If the next token is not a `[', then there are no more
8927 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8934 /* Parse a new-initializer.
8937 ( expression-list [opt] )
8940 Returns a representation of the expression-list. */
8942 static vec
<tree
, va_gc
> *
8943 cp_parser_new_initializer (cp_parser
* parser
)
8945 vec
<tree
, va_gc
> *expression_list
;
8947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8950 bool expr_non_constant_p
;
8951 cp_lexer_set_source_position (parser
->lexer
);
8952 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8953 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8954 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8955 expression_list
= make_tree_vector_single (t
);
8958 expression_list
= (cp_parser_parenthesized_expression_list
8959 (parser
, non_attr
, /*cast_p=*/false,
8960 /*allow_expansion_p=*/true,
8961 /*non_constant_p=*/NULL
));
8963 return expression_list
;
8966 /* Parse a delete-expression.
8969 :: [opt] delete cast-expression
8970 :: [opt] delete [ ] cast-expression
8972 Returns a representation of the expression. */
8975 cp_parser_delete_expression (cp_parser
* parser
)
8977 bool global_scope_p
;
8981 /* Look for the optional `::' operator. */
8983 = (cp_parser_global_scope_opt (parser
,
8984 /*current_scope_valid_p=*/false)
8986 /* Look for the `delete' keyword. */
8987 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8988 /* See if the array syntax is in use. */
8989 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8991 /* Consume the `[' token. */
8992 cp_lexer_consume_token (parser
->lexer
);
8993 /* Look for the `]' token. */
8994 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8995 /* Remember that this is the `[]' construct. */
9001 /* Parse the cast-expression. */
9002 expression
= cp_parser_simple_cast_expression (parser
);
9004 /* A delete-expression may not appear in an integral constant
9006 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
9007 return error_mark_node
;
9009 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
9010 tf_warning_or_error
);
9013 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9014 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9018 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
9020 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9021 switch (token
->type
)
9027 case CPP_CLOSE_SQUARE
:
9028 case CPP_CLOSE_PAREN
:
9029 case CPP_CLOSE_BRACE
:
9030 case CPP_OPEN_BRACE
:
9034 case CPP_DEREF_STAR
:
9042 case CPP_GREATER_EQ
:
9063 case CPP_OPEN_PAREN
:
9064 /* In ((type ()) () the last () isn't a valid cast-expression,
9065 so the whole must be parsed as postfix-expression. */
9066 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
9069 case CPP_OPEN_SQUARE
:
9070 /* '[' may start a primary-expression in obj-c++ and in C++11,
9071 as a lambda-expression, eg, '(void)[]{}'. */
9072 if (cxx_dialect
>= cxx11
)
9074 return c_dialect_objc ();
9077 case CPP_MINUS_MINUS
:
9078 /* '++' and '--' may or may not start a cast-expression:
9080 struct T { void operator++(int); };
9081 void f() { (T())++; }
9094 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9095 in the order: const_cast, static_cast, reinterpret_cast.
9097 Don't suggest dynamic_cast.
9099 Return the first legal cast kind found, or NULL otherwise. */
9102 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9106 /* Reuse the parser logic by attempting to build the various kinds of
9107 cast, with "complain" disabled.
9108 Identify the first such cast that is valid. */
9110 /* Don't attempt to run such logic within template processing. */
9111 if (processing_template_decl
)
9114 /* First try const_cast. */
9115 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
9116 if (trial
!= error_mark_node
)
9117 return "const_cast";
9119 /* If that fails, try static_cast. */
9120 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
9121 if (trial
!= error_mark_node
)
9122 return "static_cast";
9124 /* Finally, try reinterpret_cast. */
9125 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
9126 if (trial
!= error_mark_node
)
9127 return "reinterpret_cast";
9129 /* No such cast possible. */
9133 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9134 suggesting how to convert a C-style cast of the form:
9138 to a C++-style cast.
9140 The primary range of RICHLOC is asssumed to be that of the original
9141 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9142 of the parens in the C-style cast. */
9145 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9146 location_t close_paren_loc
, tree orig_expr
,
9149 /* This function is non-trivial, so bail out now if the warning isn't
9150 going to be emitted. */
9151 if (!warn_old_style_cast
)
9154 /* Try to find a legal C++ cast, trying them in order:
9155 const_cast, static_cast, reinterpret_cast. */
9156 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9157 if (!cast_suggestion
)
9160 /* Replace the open paren with "CAST_SUGGESTION<". */
9162 pp_printf (&pp
, "%s<", cast_suggestion
);
9163 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9165 /* Replace the close paren with "> (". */
9166 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9168 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9169 rich_loc
->add_fixit_insert_after (")");
9173 /* Parse a cast-expression.
9177 ( type-id ) cast-expression
9179 ADDRESS_P is true iff the unary-expression is appearing as the
9180 operand of the `&' operator. CAST_P is true if this expression is
9181 the target of a cast.
9183 Returns a representation of the expression. */
9186 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9187 bool decltype_p
, cp_id_kind
* pidk
)
9189 /* If it's a `(', then we might be looking at a cast. */
9190 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9192 tree type
= NULL_TREE
;
9193 cp_expr
expr (NULL_TREE
);
9194 int cast_expression
= 0;
9195 const char *saved_message
;
9197 /* There's no way to know yet whether or not this is a cast.
9198 For example, `(int (3))' is a unary-expression, while `(int)
9199 3' is a cast. So, we resort to parsing tentatively. */
9200 cp_parser_parse_tentatively (parser
);
9201 /* Types may not be defined in a cast. */
9202 saved_message
= parser
->type_definition_forbidden_message
;
9203 parser
->type_definition_forbidden_message
9204 = G_("types may not be defined in casts");
9205 /* Consume the `('. */
9206 matching_parens parens
;
9207 cp_token
*open_paren
= parens
.consume_open (parser
);
9208 location_t open_paren_loc
= open_paren
->location
;
9209 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9211 /* A very tricky bit is that `(struct S) { 3 }' is a
9212 compound-literal (which we permit in C++ as an extension).
9213 But, that construct is not a cast-expression -- it is a
9214 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9215 is legal; if the compound-literal were a cast-expression,
9216 you'd need an extra set of parentheses.) But, if we parse
9217 the type-id, and it happens to be a class-specifier, then we
9218 will commit to the parse at that point, because we cannot
9219 undo the action that is done when creating a new class. So,
9220 then we cannot back up and do a postfix-expression.
9222 Another tricky case is the following (c++/29234):
9224 struct S { void operator () (); };
9231 As a type-id we parse the parenthesized S()() as a function
9232 returning a function, groktypename complains and we cannot
9233 back up in this case either.
9235 Therefore, we scan ahead to the closing `)', and check to see
9236 if the tokens after the `)' can start a cast-expression. Otherwise
9237 we are dealing with an unary-expression, a postfix-expression
9240 Yet another tricky case, in C++11, is the following (c++/54891):
9244 The issue is that usually, besides the case of lambda-expressions,
9245 the parenthesized type-id cannot be followed by '[', and, eg, we
9246 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9247 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9248 we don't commit, we try a cast-expression, then an unary-expression.
9250 Save tokens so that we can put them back. */
9251 cp_lexer_save_tokens (parser
->lexer
);
9253 /* We may be looking at a cast-expression. */
9254 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9255 /*consume_paren=*/true))
9257 = cp_parser_tokens_start_cast_expression (parser
);
9259 /* Roll back the tokens we skipped. */
9260 cp_lexer_rollback_tokens (parser
->lexer
);
9261 /* If we aren't looking at a cast-expression, simulate an error so
9262 that the call to cp_parser_error_occurred below returns true. */
9263 if (!cast_expression
)
9264 cp_parser_simulate_error (parser
);
9267 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9268 parser
->in_type_id_in_expr_p
= true;
9269 /* Look for the type-id. */
9270 type
= cp_parser_type_id (parser
);
9271 /* Look for the closing `)'. */
9272 cp_token
*close_paren
= parens
.require_close (parser
);
9274 close_paren_loc
= close_paren
->location
;
9275 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9278 /* Restore the saved message. */
9279 parser
->type_definition_forbidden_message
= saved_message
;
9281 /* At this point this can only be either a cast or a
9282 parenthesized ctor such as `(T ())' that looks like a cast to
9283 function returning T. */
9284 if (!cp_parser_error_occurred (parser
))
9286 /* Only commit if the cast-expression doesn't start with
9287 '++', '--', or '[' in C++11. */
9288 if (cast_expression
> 0)
9289 cp_parser_commit_to_topmost_tentative_parse (parser
);
9291 expr
= cp_parser_cast_expression (parser
,
9292 /*address_p=*/false,
9294 /*decltype_p=*/false,
9297 if (cp_parser_parse_definitely (parser
))
9299 /* Warn about old-style casts, if so requested. */
9300 if (warn_old_style_cast
9301 && !in_system_header_at (input_location
)
9302 && !VOID_TYPE_P (type
)
9303 && current_lang_name
!= lang_name_c
)
9305 gcc_rich_location
rich_loc (input_location
);
9306 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9308 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9309 "use of old-style cast to %q#T", type
);
9312 /* Only type conversions to integral or enumeration types
9313 can be used in constant-expressions. */
9314 if (!cast_valid_in_integral_constant_expression_p (type
)
9315 && cp_parser_non_integral_constant_expression (parser
,
9317 return error_mark_node
;
9319 /* Perform the cast. */
9323 with start==caret at the open paren, extending to the
9325 location_t cast_loc
= make_location (open_paren_loc
,
9327 expr
.get_finish ());
9328 expr
= build_c_cast (cast_loc
, type
, expr
);
9333 cp_parser_abort_tentative_parse (parser
);
9336 /* If we get here, then it's not a cast, so it must be a
9337 unary-expression. */
9338 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9339 cast_p
, decltype_p
);
9342 /* Parse a binary expression of the general form:
9346 pm-expression .* cast-expression
9347 pm-expression ->* cast-expression
9349 multiplicative-expression:
9351 multiplicative-expression * pm-expression
9352 multiplicative-expression / pm-expression
9353 multiplicative-expression % pm-expression
9355 additive-expression:
9356 multiplicative-expression
9357 additive-expression + multiplicative-expression
9358 additive-expression - multiplicative-expression
9362 shift-expression << additive-expression
9363 shift-expression >> additive-expression
9365 relational-expression:
9367 relational-expression < shift-expression
9368 relational-expression > shift-expression
9369 relational-expression <= shift-expression
9370 relational-expression >= shift-expression
9374 relational-expression:
9375 relational-expression <? shift-expression
9376 relational-expression >? shift-expression
9378 equality-expression:
9379 relational-expression
9380 equality-expression == relational-expression
9381 equality-expression != relational-expression
9385 and-expression & equality-expression
9387 exclusive-or-expression:
9389 exclusive-or-expression ^ and-expression
9391 inclusive-or-expression:
9392 exclusive-or-expression
9393 inclusive-or-expression | exclusive-or-expression
9395 logical-and-expression:
9396 inclusive-or-expression
9397 logical-and-expression && inclusive-or-expression
9399 logical-or-expression:
9400 logical-and-expression
9401 logical-or-expression || logical-and-expression
9403 All these are implemented with a single function like:
9406 simple-cast-expression
9407 binary-expression <token> binary-expression
9409 CAST_P is true if this expression is the target of a cast.
9411 The binops_by_token map is used to get the tree codes for each <token> type.
9412 binary-expressions are associated according to a precedence table. */
9414 #define TOKEN_PRECEDENCE(token) \
9415 (((token->type == CPP_GREATER \
9416 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9417 && !parser->greater_than_is_operator_p) \
9418 ? PREC_NOT_OPERATOR \
9419 : binops_by_token[token->type].prec)
9422 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9423 bool no_toplevel_fold_p
,
9425 enum cp_parser_prec prec
,
9428 cp_parser_expression_stack stack
;
9429 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9430 cp_parser_expression_stack_entry current
;
9433 enum tree_code rhs_type
;
9434 enum cp_parser_prec new_prec
, lookahead_prec
;
9437 /* Parse the first expression. */
9438 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9439 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9440 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9441 cast_p
, decltype_p
, pidk
);
9442 current
.prec
= prec
;
9444 if (cp_parser_error_occurred (parser
))
9445 return error_mark_node
;
9449 /* Get an operator token. */
9450 token
= cp_lexer_peek_token (parser
->lexer
);
9452 if (warn_cxx11_compat
9453 && token
->type
== CPP_RSHIFT
9454 && !parser
->greater_than_is_operator_p
)
9456 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9457 "%<>>%> operator is treated"
9458 " as two right angle brackets in C++11"))
9459 inform (token
->location
,
9460 "suggest parentheses around %<>>%> expression");
9463 new_prec
= TOKEN_PRECEDENCE (token
);
9464 if (new_prec
!= PREC_NOT_OPERATOR
9465 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9466 /* This is a fold-expression; handle it later. */
9467 new_prec
= PREC_NOT_OPERATOR
;
9469 /* Popping an entry off the stack means we completed a subexpression:
9470 - either we found a token which is not an operator (`>' where it is not
9471 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9472 will happen repeatedly;
9473 - or, we found an operator which has lower priority. This is the case
9474 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9476 if (new_prec
<= current
.prec
)
9485 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9486 current
.loc
= token
->location
;
9488 /* We used the operator token. */
9489 cp_lexer_consume_token (parser
->lexer
);
9491 /* For "false && x" or "true || x", x will never be executed;
9492 disable warnings while evaluating it. */
9493 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9494 c_inhibit_evaluation_warnings
+=
9495 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9496 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9497 c_inhibit_evaluation_warnings
+=
9498 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9500 /* Extract another operand. It may be the RHS of this expression
9501 or the LHS of a new, higher priority expression. */
9502 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9503 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9504 rhs
= cp_parser_simple_cast_expression (parser
);
9506 /* Get another operator token. Look up its precedence to avoid
9507 building a useless (immediately popped) stack entry for common
9508 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9509 token
= cp_lexer_peek_token (parser
->lexer
);
9510 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9511 if (lookahead_prec
!= PREC_NOT_OPERATOR
9512 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9513 lookahead_prec
= PREC_NOT_OPERATOR
;
9514 if (lookahead_prec
> new_prec
)
9516 /* ... and prepare to parse the RHS of the new, higher priority
9517 expression. Since precedence levels on the stack are
9518 monotonically increasing, we do not have to care about
9523 current
.lhs_type
= rhs_type
;
9524 current
.prec
= new_prec
;
9525 new_prec
= lookahead_prec
;
9529 lookahead_prec
= new_prec
;
9530 /* If the stack is not empty, we have parsed into LHS the right side
9531 (`4' in the example above) of an expression we had suspended.
9532 We can use the information on the stack to recover the LHS (`3')
9533 from the stack together with the tree code (`MULT_EXPR'), and
9534 the precedence of the higher level subexpression
9535 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9536 which will be used to actually build the additive expression. */
9538 rhs_type
= current
.lhs_type
;
9543 /* Undo the disabling of warnings done above. */
9544 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9545 c_inhibit_evaluation_warnings
-=
9546 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9547 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9548 c_inhibit_evaluation_warnings
-=
9549 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9551 if (warn_logical_not_paren
9552 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9553 && current
.lhs_type
== TRUTH_NOT_EXPR
9554 /* Avoid warning for !!x == y. */
9555 && (TREE_CODE (current
.lhs
) != NE_EXPR
9556 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9557 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9558 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9559 /* Avoid warning for !b == y where b is boolean. */
9560 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9561 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9563 /* Avoid warning for !!b == y where b is boolean. */
9564 && (!DECL_P (tree_strip_any_location_wrapper (current
.lhs
))
9565 || TREE_TYPE (current
.lhs
) == NULL_TREE
9566 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9567 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9568 current
.lhs
, maybe_constant_value (rhs
));
9572 location_t combined_loc
= make_location (current
.loc
,
9573 current
.lhs
.get_start (),
9576 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9577 ERROR_MARK for everything that is not a binary expression.
9578 This makes warn_about_parentheses miss some warnings that
9579 involve unary operators. For unary expressions we should
9580 pass the correct tree_code unless the unary expression was
9581 surrounded by parentheses.
9583 if (no_toplevel_fold_p
9584 && lookahead_prec
<= current
.prec
9587 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9588 current
.lhs
= error_mark_node
;
9592 = build_min (current
.tree_type
,
9593 TREE_CODE_CLASS (current
.tree_type
)
9595 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9596 current
.lhs
.get_value (), rhs
.get_value ());
9597 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9602 op_location_t
op_loc (current
.loc
, combined_loc
);
9603 current
.lhs
= build_x_binary_op (op_loc
, current
.tree_type
,
9604 current
.lhs
, current
.lhs_type
,
9605 rhs
, rhs_type
, &overload
,
9606 complain_flags (decltype_p
));
9607 /* TODO: build_x_binary_op doesn't always honor the location. */
9608 current
.lhs
.set_location (combined_loc
);
9610 current
.lhs_type
= current
.tree_type
;
9612 /* If the binary operator required the use of an overloaded operator,
9613 then this expression cannot be an integral constant-expression.
9614 An overloaded operator can be used even if both operands are
9615 otherwise permissible in an integral constant-expression if at
9616 least one of the operands is of enumeration type. */
9619 && cp_parser_non_integral_constant_expression (parser
,
9621 return error_mark_node
;
9628 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9629 bool no_toplevel_fold_p
,
9630 enum cp_parser_prec prec
,
9633 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9634 /*decltype*/false, prec
, pidk
);
9637 /* Parse the `? expression : assignment-expression' part of a
9638 conditional-expression. The LOGICAL_OR_EXPR is the
9639 logical-or-expression that started the conditional-expression.
9640 Returns a representation of the entire conditional-expression.
9642 This routine is used by cp_parser_assignment_expression.
9644 ? expression : assignment-expression
9648 ? : assignment-expression */
9651 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9653 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9654 cp_expr assignment_expr
;
9655 struct cp_token
*token
;
9656 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9658 /* Consume the `?' token. */
9659 cp_lexer_consume_token (parser
->lexer
);
9660 token
= cp_lexer_peek_token (parser
->lexer
);
9661 if (cp_parser_allow_gnu_extensions_p (parser
)
9662 && token
->type
== CPP_COLON
)
9664 pedwarn (token
->location
, OPT_Wpedantic
,
9665 "ISO C++ does not allow ?: with omitted middle operand");
9666 /* Implicit true clause. */
9668 c_inhibit_evaluation_warnings
+=
9669 folded_logical_or_expr
== truthvalue_true_node
;
9670 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9674 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9675 parser
->colon_corrects_to_scope_p
= false;
9676 /* Parse the expression. */
9677 c_inhibit_evaluation_warnings
+=
9678 folded_logical_or_expr
== truthvalue_false_node
;
9679 expr
= cp_parser_expression (parser
);
9680 c_inhibit_evaluation_warnings
+=
9681 ((folded_logical_or_expr
== truthvalue_true_node
)
9682 - (folded_logical_or_expr
== truthvalue_false_node
));
9683 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9686 /* The next token should be a `:'. */
9687 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9688 /* Parse the assignment-expression. */
9689 assignment_expr
= cp_parser_assignment_expression (parser
);
9690 c_inhibit_evaluation_warnings
-=
9691 folded_logical_or_expr
== truthvalue_true_node
;
9694 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9695 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9696 with the caret at the "?", ranging from the start of
9697 the logical_or_expr to the end of the assignment_expr. */
9698 loc
= make_location (loc
,
9699 logical_or_expr
.get_start (),
9700 assignment_expr
.get_finish ());
9702 /* Build the conditional-expression. */
9703 return build_x_conditional_expr (loc
, logical_or_expr
,
9706 tf_warning_or_error
);
9709 /* Parse an assignment-expression.
9711 assignment-expression:
9712 conditional-expression
9713 logical-or-expression assignment-operator assignment_expression
9716 CAST_P is true if this expression is the target of a cast.
9717 DECLTYPE_P is true if this expression is the operand of decltype.
9719 Returns a representation for the expression. */
9722 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9723 bool cast_p
, bool decltype_p
)
9727 /* If the next token is the `throw' keyword, then we're looking at
9728 a throw-expression. */
9729 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9730 expr
= cp_parser_throw_expression (parser
);
9731 /* Otherwise, it must be that we are looking at a
9732 logical-or-expression. */
9735 /* Parse the binary expressions (logical-or-expression). */
9736 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9738 PREC_NOT_OPERATOR
, pidk
);
9739 /* If the next token is a `?' then we're actually looking at a
9740 conditional-expression. */
9741 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9742 return cp_parser_question_colon_clause (parser
, expr
);
9745 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9747 /* If it's an assignment-operator, we're using the second
9749 enum tree_code assignment_operator
9750 = cp_parser_assignment_operator_opt (parser
);
9751 if (assignment_operator
!= ERROR_MARK
)
9753 bool non_constant_p
;
9755 /* Parse the right-hand side of the assignment. */
9756 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9759 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9760 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9762 /* An assignment may not appear in a
9763 constant-expression. */
9764 if (cp_parser_non_integral_constant_expression (parser
,
9766 return error_mark_node
;
9767 /* Build the assignment expression. Its default
9771 is the location of the '=' token as the
9772 caret, ranging from the start of the lhs to the
9774 loc
= make_location (loc
,
9777 expr
= build_x_modify_expr (loc
, expr
,
9778 assignment_operator
,
9780 complain_flags (decltype_p
));
9781 /* TODO: build_x_modify_expr doesn't honor the location,
9782 so we must set it here. */
9783 expr
.set_location (loc
);
9791 /* Parse an (optional) assignment-operator.
9793 assignment-operator: one of
9794 = *= /= %= += -= >>= <<= &= ^= |=
9798 assignment-operator: one of
9801 If the next token is an assignment operator, the corresponding tree
9802 code is returned, and the token is consumed. For example, for
9803 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9804 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9805 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9806 operator, ERROR_MARK is returned. */
9808 static enum tree_code
9809 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9814 /* Peek at the next token. */
9815 token
= cp_lexer_peek_token (parser
->lexer
);
9817 switch (token
->type
)
9828 op
= TRUNC_DIV_EXPR
;
9832 op
= TRUNC_MOD_EXPR
;
9864 /* Nothing else is an assignment operator. */
9868 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9869 if (op
!= ERROR_MARK
9870 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9873 /* If it was an assignment operator, consume it. */
9874 if (op
!= ERROR_MARK
)
9875 cp_lexer_consume_token (parser
->lexer
);
9880 /* Parse an expression.
9883 assignment-expression
9884 expression , assignment-expression
9886 CAST_P is true if this expression is the target of a cast.
9887 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9888 except possibly parenthesized or on the RHS of a comma (N3276).
9890 Returns a representation of the expression. */
9893 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9894 bool cast_p
, bool decltype_p
)
9896 cp_expr expression
= NULL_TREE
;
9897 location_t loc
= UNKNOWN_LOCATION
;
9901 cp_expr assignment_expression
;
9903 /* Parse the next assignment-expression. */
9904 assignment_expression
9905 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9907 /* We don't create a temporary for a call that is the immediate operand
9908 of decltype or on the RHS of a comma. But when we see a comma, we
9909 need to create a temporary for a call on the LHS. */
9910 if (decltype_p
&& !processing_template_decl
9911 && TREE_CODE (assignment_expression
) == CALL_EXPR
9912 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9913 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9914 assignment_expression
9915 = build_cplus_new (TREE_TYPE (assignment_expression
),
9916 assignment_expression
, tf_warning_or_error
);
9918 /* If this is the first assignment-expression, we can just
9921 expression
= assignment_expression
;
9924 /* Create a location with caret at the comma, ranging
9925 from the start of the LHS to the end of the RHS. */
9926 loc
= make_location (loc
,
9927 expression
.get_start (),
9928 assignment_expression
.get_finish ());
9929 expression
= build_x_compound_expr (loc
, expression
,
9930 assignment_expression
,
9931 complain_flags (decltype_p
));
9932 expression
.set_location (loc
);
9934 /* If the next token is not a comma, or we're in a fold-expression, then
9935 we are done with the expression. */
9936 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9937 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9939 /* Consume the `,'. */
9940 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9941 cp_lexer_consume_token (parser
->lexer
);
9942 /* A comma operator cannot appear in a constant-expression. */
9943 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9944 expression
= error_mark_node
;
9950 /* Parse a constant-expression.
9952 constant-expression:
9953 conditional-expression
9955 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9956 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9957 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9958 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9959 only parse a conditional-expression, otherwise parse an
9960 assignment-expression. See below for rationale. */
9963 cp_parser_constant_expression (cp_parser
* parser
,
9964 bool allow_non_constant_p
,
9965 bool *non_constant_p
,
9968 bool saved_integral_constant_expression_p
;
9969 bool saved_allow_non_integral_constant_expression_p
;
9970 bool saved_non_integral_constant_expression_p
;
9973 /* It might seem that we could simply parse the
9974 conditional-expression, and then check to see if it were
9975 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9976 one that the compiler can figure out is constant, possibly after
9977 doing some simplifications or optimizations. The standard has a
9978 precise definition of constant-expression, and we must honor
9979 that, even though it is somewhat more restrictive.
9985 is not a legal declaration, because `(2, 3)' is not a
9986 constant-expression. The `,' operator is forbidden in a
9987 constant-expression. However, GCC's constant-folding machinery
9988 will fold this operation to an INTEGER_CST for `3'. */
9990 /* Save the old settings. */
9991 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9992 saved_allow_non_integral_constant_expression_p
9993 = parser
->allow_non_integral_constant_expression_p
;
9994 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9995 /* We are now parsing a constant-expression. */
9996 parser
->integral_constant_expression_p
= true;
9997 parser
->allow_non_integral_constant_expression_p
9998 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9999 parser
->non_integral_constant_expression_p
= false;
10000 /* Although the grammar says "conditional-expression", when not STRICT_P,
10001 we parse an "assignment-expression", which also permits
10002 "throw-expression" and the use of assignment operators. In the case
10003 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10004 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10005 actually essential that we look for an assignment-expression.
10006 For example, cp_parser_initializer_clauses uses this function to
10007 determine whether a particular assignment-expression is in fact
10011 /* Parse the binary expressions (logical-or-expression). */
10012 expression
= cp_parser_binary_expression (parser
, false, false, false,
10013 PREC_NOT_OPERATOR
, NULL
);
10014 /* If the next token is a `?' then we're actually looking at
10015 a conditional-expression; otherwise we're done. */
10016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10017 expression
= cp_parser_question_colon_clause (parser
, expression
);
10020 expression
= cp_parser_assignment_expression (parser
);
10021 /* Restore the old settings. */
10022 parser
->integral_constant_expression_p
10023 = saved_integral_constant_expression_p
;
10024 parser
->allow_non_integral_constant_expression_p
10025 = saved_allow_non_integral_constant_expression_p
;
10026 if (cxx_dialect
>= cxx11
)
10028 /* Require an rvalue constant expression here; that's what our
10029 callers expect. Reference constant expressions are handled
10030 separately in e.g. cp_parser_template_argument. */
10031 tree decay
= expression
;
10032 if (TREE_TYPE (expression
)
10033 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
10034 decay
= build_address (expression
);
10035 bool is_const
= potential_rvalue_constant_expression (decay
);
10036 parser
->non_integral_constant_expression_p
= !is_const
;
10037 if (!is_const
&& !allow_non_constant_p
)
10038 require_potential_rvalue_constant_expression (decay
);
10040 if (allow_non_constant_p
)
10041 *non_constant_p
= parser
->non_integral_constant_expression_p
;
10042 parser
->non_integral_constant_expression_p
10043 = saved_non_integral_constant_expression_p
;
10048 /* Parse __builtin_offsetof.
10050 offsetof-expression:
10051 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10053 offsetof-member-designator:
10055 | offsetof-member-designator "." id-expression
10056 | offsetof-member-designator "[" expression "]"
10057 | offsetof-member-designator "->" id-expression */
10060 cp_parser_builtin_offsetof (cp_parser
*parser
)
10062 int save_ice_p
, save_non_ice_p
;
10067 location_t finish_loc
;
10069 /* We're about to accept non-integral-constant things, but will
10070 definitely yield an integral constant expression. Save and
10071 restore these values around our local parsing. */
10072 save_ice_p
= parser
->integral_constant_expression_p
;
10073 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10075 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10077 /* Consume the "__builtin_offsetof" token. */
10078 cp_lexer_consume_token (parser
->lexer
);
10079 /* Consume the opening `('. */
10080 matching_parens parens
;
10081 parens
.require_open (parser
);
10082 /* Parse the type-id. */
10083 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10085 const char *saved_message
= parser
->type_definition_forbidden_message
;
10086 parser
->type_definition_forbidden_message
10087 = G_("types may not be defined within __builtin_offsetof");
10088 type
= cp_parser_type_id (parser
);
10089 parser
->type_definition_forbidden_message
= saved_message
;
10091 /* Look for the `,'. */
10092 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10093 token
= cp_lexer_peek_token (parser
->lexer
);
10095 /* Build the (type *)null that begins the traditional offsetof macro. */
10097 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
10098 tf_warning_or_error
);
10100 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10101 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10102 true, &dummy
, token
->location
);
10105 token
= cp_lexer_peek_token (parser
->lexer
);
10106 switch (token
->type
)
10108 case CPP_OPEN_SQUARE
:
10109 /* offsetof-member-designator "[" expression "]" */
10110 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10115 /* offsetof-member-designator "->" identifier */
10116 expr
= grok_array_decl (token
->location
, expr
,
10117 integer_zero_node
, false);
10121 /* offsetof-member-designator "." identifier */
10122 cp_lexer_consume_token (parser
->lexer
);
10123 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10124 expr
, true, &dummy
,
10128 case CPP_CLOSE_PAREN
:
10129 /* Consume the ")" token. */
10130 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10131 cp_lexer_consume_token (parser
->lexer
);
10135 /* Error. We know the following require will fail, but
10136 that gives the proper error message. */
10137 parens
.require_close (parser
);
10138 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10139 expr
= error_mark_node
;
10145 /* Make a location of the form:
10146 __builtin_offsetof (struct s, f)
10147 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10148 with caret at the type-id, ranging from the start of the
10149 "_builtin_offsetof" token to the close paren. */
10150 loc
= make_location (loc
, start_loc
, finish_loc
);
10151 /* The result will be an INTEGER_CST, so we need to explicitly
10152 preserve the location. */
10153 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10156 parser
->integral_constant_expression_p
= save_ice_p
;
10157 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10159 expr
= expr
.maybe_add_location_wrapper ();
10163 /* Parse a trait expression.
10165 Returns a representation of the expression, the underlying type
10166 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10169 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10171 cp_trait_kind kind
;
10172 tree type1
, type2
= NULL_TREE
;
10173 bool binary
= false;
10174 bool variadic
= false;
10178 case RID_HAS_NOTHROW_ASSIGN
:
10179 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10181 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10182 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10184 case RID_HAS_NOTHROW_COPY
:
10185 kind
= CPTK_HAS_NOTHROW_COPY
;
10187 case RID_HAS_TRIVIAL_ASSIGN
:
10188 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10190 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10191 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10193 case RID_HAS_TRIVIAL_COPY
:
10194 kind
= CPTK_HAS_TRIVIAL_COPY
;
10196 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10197 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10199 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10200 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10202 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10203 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10205 case RID_IS_ABSTRACT
:
10206 kind
= CPTK_IS_ABSTRACT
;
10208 case RID_IS_AGGREGATE
:
10209 kind
= CPTK_IS_AGGREGATE
;
10211 case RID_IS_BASE_OF
:
10212 kind
= CPTK_IS_BASE_OF
;
10216 kind
= CPTK_IS_CLASS
;
10219 kind
= CPTK_IS_EMPTY
;
10222 kind
= CPTK_IS_ENUM
;
10225 kind
= CPTK_IS_FINAL
;
10227 case RID_IS_LITERAL_TYPE
:
10228 kind
= CPTK_IS_LITERAL_TYPE
;
10231 kind
= CPTK_IS_POD
;
10233 case RID_IS_POLYMORPHIC
:
10234 kind
= CPTK_IS_POLYMORPHIC
;
10236 case RID_IS_SAME_AS
:
10237 kind
= CPTK_IS_SAME_AS
;
10240 case RID_IS_STD_LAYOUT
:
10241 kind
= CPTK_IS_STD_LAYOUT
;
10243 case RID_IS_TRIVIAL
:
10244 kind
= CPTK_IS_TRIVIAL
;
10246 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10247 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10250 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10251 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10254 case RID_IS_TRIVIALLY_COPYABLE
:
10255 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10258 kind
= CPTK_IS_UNION
;
10260 case RID_UNDERLYING_TYPE
:
10261 kind
= CPTK_UNDERLYING_TYPE
;
10266 case RID_DIRECT_BASES
:
10267 kind
= CPTK_DIRECT_BASES
;
10269 case RID_IS_ASSIGNABLE
:
10270 kind
= CPTK_IS_ASSIGNABLE
;
10273 case RID_IS_CONSTRUCTIBLE
:
10274 kind
= CPTK_IS_CONSTRUCTIBLE
;
10278 gcc_unreachable ();
10281 /* Get location of initial token. */
10282 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10284 /* Consume the token. */
10285 cp_lexer_consume_token (parser
->lexer
);
10287 matching_parens parens
;
10288 parens
.require_open (parser
);
10291 type_id_in_expr_sentinel
s (parser
);
10292 type1
= cp_parser_type_id (parser
);
10295 if (type1
== error_mark_node
)
10296 return error_mark_node
;
10300 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10303 type_id_in_expr_sentinel
s (parser
);
10304 type2
= cp_parser_type_id (parser
);
10307 if (type2
== error_mark_node
)
10308 return error_mark_node
;
10312 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10314 cp_lexer_consume_token (parser
->lexer
);
10315 tree elt
= cp_parser_type_id (parser
);
10316 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10318 cp_lexer_consume_token (parser
->lexer
);
10319 elt
= make_pack_expansion (elt
);
10321 if (elt
== error_mark_node
)
10322 return error_mark_node
;
10323 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10327 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10328 parens
.require_close (parser
);
10330 /* Construct a location of the form:
10331 __is_trivially_copyable(_Tp)
10332 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10333 with start == caret, finishing at the close-paren. */
10334 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10336 /* Complete the trait expression, which may mean either processing
10337 the trait expr now or saving it for template instantiation. */
10340 case CPTK_UNDERLYING_TYPE
:
10341 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10343 return cp_expr (finish_bases (type1
, false), trait_loc
);
10344 case CPTK_DIRECT_BASES
:
10345 return cp_expr (finish_bases (type1
, true), trait_loc
);
10347 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10351 /* Parse a lambda expression.
10354 lambda-introducer lambda-declarator [opt] compound-statement
10356 Returns a representation of the expression. */
10359 cp_parser_lambda_expression (cp_parser
* parser
)
10361 tree lambda_expr
= build_lambda_expr ();
10364 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10365 cp_token_position start
= 0;
10367 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10369 if (cxx_dialect
>= cxx2a
)
10370 /* C++20 allows lambdas in unevaluated context. */;
10371 else if (cp_unevaluated_operand
)
10373 if (!token
->error_reported
)
10375 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10376 "lambda-expression in unevaluated context"
10377 " only available with -std=c++2a or -std=gnu++2a");
10378 token
->error_reported
= true;
10382 else if (parser
->in_template_argument_list_p
)
10384 if (!token
->error_reported
)
10386 error_at (token
->location
, "lambda-expression in template-argument"
10387 " only available with -std=c++2a or -std=gnu++2a");
10388 token
->error_reported
= true;
10393 /* We may be in the middle of deferred access check. Disable
10395 push_deferring_access_checks (dk_no_deferred
);
10397 cp_parser_lambda_introducer (parser
, lambda_expr
);
10398 if (cp_parser_error_occurred (parser
))
10399 return error_mark_node
;
10401 type
= begin_lambda_type (lambda_expr
);
10402 if (type
== error_mark_node
)
10403 return error_mark_node
;
10405 record_lambda_scope (lambda_expr
);
10407 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10408 determine_visibility (TYPE_NAME (type
));
10410 /* Now that we've started the type, add the capture fields for any
10411 explicit captures. */
10412 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10415 /* Inside the class, surrounding template-parameter-lists do not apply. */
10416 unsigned int saved_num_template_parameter_lists
10417 = parser
->num_template_parameter_lists
;
10418 unsigned char in_statement
= parser
->in_statement
;
10419 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10420 bool fully_implicit_function_template_p
10421 = parser
->fully_implicit_function_template_p
;
10422 tree implicit_template_parms
= parser
->implicit_template_parms
;
10423 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10424 bool auto_is_implicit_function_template_parm_p
10425 = parser
->auto_is_implicit_function_template_parm_p
;
10427 parser
->num_template_parameter_lists
= 0;
10428 parser
->in_statement
= 0;
10429 parser
->in_switch_statement_p
= false;
10430 parser
->fully_implicit_function_template_p
= false;
10431 parser
->implicit_template_parms
= 0;
10432 parser
->implicit_template_scope
= 0;
10433 parser
->auto_is_implicit_function_template_parm_p
= false;
10435 /* By virtue of defining a local class, a lambda expression has access to
10436 the private variables of enclosing classes. */
10438 if (cp_parser_start_tentative_firewall (parser
))
10441 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10443 if (ok
&& cp_parser_error_occurred (parser
))
10448 cp_parser_lambda_body (parser
, lambda_expr
);
10450 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10452 if (cp_parser_skip_to_closing_brace (parser
))
10453 cp_lexer_consume_token (parser
->lexer
);
10456 /* The capture list was built up in reverse order; fix that now. */
10457 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10458 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10461 maybe_add_lambda_conv_op (type
);
10463 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10465 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10466 parser
->in_statement
= in_statement
;
10467 parser
->in_switch_statement_p
= in_switch_statement_p
;
10468 parser
->fully_implicit_function_template_p
10469 = fully_implicit_function_template_p
;
10470 parser
->implicit_template_parms
= implicit_template_parms
;
10471 parser
->implicit_template_scope
= implicit_template_scope
;
10472 parser
->auto_is_implicit_function_template_parm_p
10473 = auto_is_implicit_function_template_parm_p
;
10476 /* This field is only used during parsing of the lambda. */
10477 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10479 /* This lambda shouldn't have any proxies left at this point. */
10480 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10481 /* And now that we're done, push proxies for an enclosing lambda. */
10482 insert_pending_capture_proxies ();
10484 /* Update the lambda expression to a range. */
10485 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10486 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10488 end_tok
->location
);
10491 lambda_expr
= build_lambda_object (lambda_expr
);
10493 lambda_expr
= error_mark_node
;
10495 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10497 pop_deferring_access_checks ();
10499 return lambda_expr
;
10502 /* Parse the beginning of a lambda expression.
10505 [ lambda-capture [opt] ]
10507 LAMBDA_EXPR is the current representation of the lambda expression. */
10510 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10512 /* Need commas after the first capture. */
10515 /* Eat the leading `['. */
10516 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10518 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10519 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10520 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10521 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10522 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10523 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10525 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10527 cp_lexer_consume_token (parser
->lexer
);
10530 if (!(at_function_scope_p () || parsing_nsdmi ()))
10531 error ("non-local lambda expression cannot have a capture-default");
10534 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10536 cp_token
* capture_token
;
10538 tree capture_init_expr
;
10539 cp_id_kind idk
= CP_ID_KIND_NONE
;
10540 bool explicit_init_p
= false;
10542 enum capture_kind_type
10547 enum capture_kind_type capture_kind
= BY_COPY
;
10549 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10551 error ("expected end of capture-list");
10558 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10560 /* Possibly capture `this'. */
10561 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10563 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10564 if (cxx_dialect
< cxx2a
10565 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10566 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10567 "with by-copy capture default");
10568 cp_lexer_consume_token (parser
->lexer
);
10569 add_capture (lambda_expr
,
10570 /*id=*/this_identifier
,
10571 /*initializer=*/finish_this_expr (),
10572 /*by_reference_p=*/true,
10577 /* Possibly capture `*this'. */
10578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10579 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10581 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10582 if (cxx_dialect
< cxx17
)
10583 pedwarn (loc
, 0, "%<*this%> capture only available with "
10584 "-std=c++17 or -std=gnu++17");
10585 cp_lexer_consume_token (parser
->lexer
);
10586 cp_lexer_consume_token (parser
->lexer
);
10587 add_capture (lambda_expr
,
10588 /*id=*/this_identifier
,
10589 /*initializer=*/finish_this_expr (),
10590 /*by_reference_p=*/false,
10595 bool init_pack_expansion
= false;
10596 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10598 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10599 if (cxx_dialect
< cxx2a
)
10600 pedwarn (loc
, 0, "pack init-capture only available with "
10601 "-std=c++2a or -std=gnu++2a");
10602 cp_lexer_consume_token (parser
->lexer
);
10603 init_pack_expansion
= true;
10606 /* Remember whether we want to capture as a reference or not. */
10607 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10609 capture_kind
= BY_REFERENCE
;
10610 cp_lexer_consume_token (parser
->lexer
);
10613 /* Get the identifier. */
10614 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10615 capture_id
= cp_parser_identifier (parser
);
10617 if (capture_id
== error_mark_node
)
10618 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10619 delimiters, but I modified this to stop on unnested ']' as well. It
10620 was already changed to stop on unnested '}', so the
10621 "closing_parenthesis" name is no more misleading with my change. */
10623 cp_parser_skip_to_closing_parenthesis (parser
,
10624 /*recovering=*/true,
10626 /*consume_paren=*/true);
10630 /* Find the initializer for this capture. */
10631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10632 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10633 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10635 bool direct
, non_constant
;
10636 /* An explicit initializer exists. */
10637 if (cxx_dialect
< cxx14
)
10638 pedwarn (input_location
, 0,
10639 "lambda capture initializers "
10640 "only available with -std=c++14 or -std=gnu++14");
10641 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10642 &non_constant
, true);
10643 explicit_init_p
= true;
10644 if (capture_init_expr
== NULL_TREE
)
10646 error ("empty initializer for lambda init-capture");
10647 capture_init_expr
= error_mark_node
;
10649 if (init_pack_expansion
)
10650 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10654 const char* error_msg
;
10656 /* Turn the identifier into an id-expression. */
10658 = cp_parser_lookup_name_simple (parser
, capture_id
,
10659 capture_token
->location
);
10661 if (capture_init_expr
== error_mark_node
)
10663 unqualified_name_lookup_error (capture_id
);
10666 else if (!VAR_P (capture_init_expr
)
10667 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10669 error_at (capture_token
->location
,
10670 "capture of non-variable %qE",
10671 capture_init_expr
);
10672 if (DECL_P (capture_init_expr
))
10673 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10674 "%q#D declared here", capture_init_expr
);
10677 if (VAR_P (capture_init_expr
)
10678 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10680 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10681 "%qD with non-automatic storage duration",
10682 capture_init_expr
))
10683 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10684 "%q#D declared here", capture_init_expr
);
10689 = finish_id_expression
10694 /*integral_constant_expression_p=*/false,
10695 /*allow_non_integral_constant_expression_p=*/false,
10696 /*non_integral_constant_expression_p=*/NULL
,
10697 /*template_p=*/false,
10699 /*address_p=*/false,
10700 /*template_arg_p=*/false,
10702 capture_token
->location
);
10704 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10706 cp_lexer_consume_token (parser
->lexer
);
10707 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10711 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10712 && !explicit_init_p
)
10714 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10715 && capture_kind
== BY_COPY
)
10716 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10717 "of %qD redundant with by-copy capture default",
10719 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10720 && capture_kind
== BY_REFERENCE
)
10721 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10722 "capture of %qD redundant with by-reference capture "
10723 "default", capture_id
);
10726 add_capture (lambda_expr
,
10729 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10732 /* If there is any qualification still in effect, clear it
10733 now; we will be starting fresh with the next capture. */
10734 parser
->scope
= NULL_TREE
;
10735 parser
->qualifying_scope
= NULL_TREE
;
10736 parser
->object_scope
= NULL_TREE
;
10739 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10742 /* Parse the (optional) middle of a lambda expression.
10745 < template-parameter-list [opt] >
10746 ( parameter-declaration-clause [opt] )
10747 attribute-specifier [opt]
10748 decl-specifier-seq [opt]
10749 exception-specification [opt]
10750 lambda-return-type-clause [opt]
10752 LAMBDA_EXPR is the current representation of the lambda expression. */
10755 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10757 /* 5.1.1.4 of the standard says:
10758 If a lambda-expression does not include a lambda-declarator, it is as if
10759 the lambda-declarator were ().
10760 This means an empty parameter list, no attributes, and no exception
10762 tree param_list
= void_list_node
;
10763 tree attributes
= NULL_TREE
;
10764 tree exception_spec
= NULL_TREE
;
10765 tree template_param_list
= NULL_TREE
;
10766 tree tx_qual
= NULL_TREE
;
10767 tree return_type
= NULL_TREE
;
10768 cp_decl_specifier_seq lambda_specs
;
10769 clear_decl_specs (&lambda_specs
);
10771 /* The template-parameter-list is optional, but must begin with
10772 an opening angle if present. */
10773 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10775 if (cxx_dialect
< cxx14
)
10776 pedwarn (parser
->lexer
->next_token
->location
, 0,
10777 "lambda templates are only available with "
10778 "-std=c++14 or -std=gnu++14");
10779 else if (cxx_dialect
< cxx2a
)
10780 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10781 "lambda templates are only available with "
10782 "-std=c++2a or -std=gnu++2a");
10784 cp_lexer_consume_token (parser
->lexer
);
10786 template_param_list
= cp_parser_template_parameter_list (parser
);
10788 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10790 /* We just processed one more parameter list. */
10791 ++parser
->num_template_parameter_lists
;
10794 /* The parameter-declaration-clause is optional (unless
10795 template-parameter-list was given), but must begin with an
10796 opening parenthesis if present. */
10797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10799 matching_parens parens
;
10800 parens
.consume_open (parser
);
10802 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10804 /* Parse parameters. */
10806 = cp_parser_parameter_declaration_clause
10807 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
);
10809 /* Default arguments shall not be specified in the
10810 parameter-declaration-clause of a lambda-declarator. */
10811 if (cxx_dialect
< cxx14
)
10812 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10813 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10814 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10815 "default argument specified for lambda parameter");
10817 parens
.require_close (parser
);
10819 /* In the decl-specifier-seq of the lambda-declarator, each
10820 decl-specifier shall either be mutable or constexpr. */
10821 int declares_class_or_enum
;
10822 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10823 cp_parser_decl_specifier_seq (parser
,
10824 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10825 &lambda_specs
, &declares_class_or_enum
);
10826 if (lambda_specs
.storage_class
== sc_mutable
)
10828 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10829 if (lambda_specs
.conflicting_specifiers_p
)
10830 error_at (lambda_specs
.locations
[ds_storage_class
],
10831 "duplicate %<mutable%>");
10834 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10836 /* Parse optional exception specification. */
10837 exception_spec
= cp_parser_exception_specification_opt (parser
);
10839 attributes
= cp_parser_std_attribute_spec_seq (parser
);
10841 /* Parse optional trailing return type. */
10842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10844 cp_lexer_consume_token (parser
->lexer
);
10845 return_type
= cp_parser_trailing_type_id (parser
);
10848 /* The function parameters must be in scope all the way until after the
10849 trailing-return-type in case of decltype. */
10850 pop_bindings_and_leave_scope ();
10852 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10853 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10855 /* Create the function call operator.
10857 Messing with declarators like this is no uglier than building up the
10858 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10861 cp_decl_specifier_seq return_type_specs
;
10862 cp_declarator
* declarator
;
10867 clear_decl_specs (&return_type_specs
);
10868 return_type_specs
.type
= make_auto ();
10870 if (lambda_specs
.locations
[ds_constexpr
])
10872 if (cxx_dialect
>= cxx17
)
10873 return_type_specs
.locations
[ds_constexpr
]
10874 = lambda_specs
.locations
[ds_constexpr
];
10876 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10877 "lambda only available with -std=c++17 or -std=gnu++17");
10880 p
= obstack_alloc (&declarator_obstack
, 0);
10882 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
10883 LAMBDA_EXPR_LOCATION (lambda_expr
));
10885 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10886 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10887 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10888 VIRT_SPEC_UNSPECIFIED
,
10893 /*requires_clause*/NULL_TREE
);
10894 declarator
->std_attributes
= attributes
;
10896 fco
= grokmethod (&return_type_specs
,
10899 if (fco
!= error_mark_node
)
10901 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10902 DECL_ARTIFICIAL (fco
) = 1;
10903 /* Give the object parameter a different name. */
10904 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10905 DECL_LAMBDA_FUNCTION (fco
) = 1;
10907 if (template_param_list
)
10909 fco
= finish_member_template_decl (fco
);
10910 finish_template_decl (template_param_list
);
10911 --parser
->num_template_parameter_lists
;
10913 else if (parser
->fully_implicit_function_template_p
)
10914 fco
= finish_fully_implicit_template (parser
, fco
);
10916 finish_member_declaration (fco
);
10918 obstack_free (&declarator_obstack
, p
);
10920 return (fco
!= error_mark_node
);
10924 /* Parse the body of a lambda expression, which is simply
10928 but which requires special handling.
10929 LAMBDA_EXPR is the current representation of the lambda expression. */
10932 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10934 bool nested
= (current_function_decl
!= NULL_TREE
);
10935 unsigned char local_variables_forbidden_p
10936 = parser
->local_variables_forbidden_p
;
10937 bool in_function_body
= parser
->in_function_body
;
10939 /* The body of a lambda-expression is not a subexpression of the enclosing
10944 push_function_context ();
10946 /* Still increment function_depth so that we don't GC in the
10947 middle of an expression. */
10950 vec
<tree
> omp_privatization_save
;
10951 save_omp_privatization_clauses (omp_privatization_save
);
10952 /* Clear this in case we're in the middle of a default argument. */
10953 parser
->local_variables_forbidden_p
= 0;
10954 parser
->in_function_body
= true;
10957 local_specialization_stack
s (lss_copy
);
10958 tree fco
= lambda_function (lambda_expr
);
10959 tree body
= start_lambda_function (fco
, lambda_expr
);
10960 matching_braces braces
;
10962 if (braces
.require_open (parser
))
10964 tree compound_stmt
= begin_compound_stmt (0);
10966 /* Originally C++11 required us to peek for 'return expr'; and
10967 process it specially here to deduce the return type. N3638
10968 removed the need for that. */
10970 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10971 cp_parser_label_declaration (parser
);
10972 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10973 braces
.require_close (parser
);
10975 finish_compound_stmt (compound_stmt
);
10978 finish_lambda_function (body
);
10981 restore_omp_privatization_clauses (omp_privatization_save
);
10982 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10983 parser
->in_function_body
= in_function_body
;
10985 pop_function_context();
10990 /* Statements [gram.stmt.stmt] */
10992 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10995 add_debug_begin_stmt (location_t loc
)
10997 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10999 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
11000 /* A concept is never expanded normally. */
11003 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
11004 SET_EXPR_LOCATION (stmt
, loc
);
11008 /* Parse a statement.
11012 expression-statement
11014 selection-statement
11015 iteration-statement
11017 declaration-statement
11024 attribute-specifier-seq (opt) expression-statement
11025 attribute-specifier-seq (opt) compound-statement
11026 attribute-specifier-seq (opt) selection-statement
11027 attribute-specifier-seq (opt) iteration-statement
11028 attribute-specifier-seq (opt) jump-statement
11029 declaration-statement
11030 attribute-specifier-seq (opt) try-block
11033 expression-statement
11041 IN_COMPOUND is true when the statement is nested inside a
11042 cp_parser_compound_statement; this matters for certain pragmas.
11044 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11045 is a (possibly labeled) if statement which is not enclosed in braces
11046 and has an else clause. This is used to implement -Wparentheses.
11048 CHAIN is a vector of if-else-if conditions. */
11051 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
11052 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
11053 location_t
*loc_after_labels
)
11055 tree statement
, std_attrs
= NULL_TREE
;
11057 location_t statement_location
, attrs_location
;
11062 /* There is no statement yet. */
11063 statement
= NULL_TREE
;
11065 saved_token_sentinel
saved_tokens (parser
->lexer
);
11066 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
11067 if (c_dialect_objc ())
11068 /* In obj-c++, seeing '[[' might be the either the beginning of
11069 c++11 attributes, or a nested objc-message-expression. So
11070 let's parse the c++11 attributes tentatively. */
11071 cp_parser_parse_tentatively (parser
);
11072 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11073 if (c_dialect_objc ())
11075 if (!cp_parser_parse_definitely (parser
))
11076 std_attrs
= NULL_TREE
;
11079 /* Peek at the next token. */
11080 token
= cp_lexer_peek_token (parser
->lexer
);
11081 /* Remember the location of the first token in the statement. */
11082 cp_token
*statement_token
= token
;
11083 statement_location
= token
->location
;
11084 add_debug_begin_stmt (statement_location
);
11085 /* If this is a keyword, then that will often determine what kind of
11086 statement we have. */
11087 if (token
->type
== CPP_KEYWORD
)
11089 enum rid keyword
= token
->keyword
;
11095 /* Looks like a labeled-statement with a case label.
11096 Parse the label, and then use tail recursion to parse
11098 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11099 in_compound
= false;
11104 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11105 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
11111 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11112 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
11119 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11120 statement
= cp_parser_jump_statement (parser
);
11123 /* Objective-C++ exception-handling constructs. */
11126 case RID_AT_FINALLY
:
11127 case RID_AT_SYNCHRONIZED
:
11129 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11130 statement
= cp_parser_objc_statement (parser
);
11134 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11135 statement
= cp_parser_try_block (parser
);
11138 case RID_NAMESPACE
:
11139 /* This must be a namespace alias definition. */
11140 if (std_attrs
!= NULL_TREE
)
11142 /* Attributes should be parsed as part of the the
11143 declaration, so let's un-parse them. */
11144 saved_tokens
.rollback();
11145 std_attrs
= NULL_TREE
;
11147 cp_parser_declaration_statement (parser
);
11150 case RID_TRANSACTION_ATOMIC
:
11151 case RID_TRANSACTION_RELAXED
:
11152 case RID_SYNCHRONIZED
:
11153 case RID_ATOMIC_NOEXCEPT
:
11154 case RID_ATOMIC_CANCEL
:
11155 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11156 statement
= cp_parser_transaction (parser
, token
);
11158 case RID_TRANSACTION_CANCEL
:
11159 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11160 statement
= cp_parser_transaction_cancel (parser
);
11164 /* It might be a keyword like `int' that can start a
11165 declaration-statement. */
11169 else if (token
->type
== CPP_NAME
)
11171 /* If the next token is a `:', then we are looking at a
11172 labeled-statement. */
11173 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11174 if (token
->type
== CPP_COLON
)
11176 /* Looks like a labeled-statement with an ordinary label.
11177 Parse the label, and then use tail recursion to parse
11180 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11181 in_compound
= false;
11185 /* Anything that starts with a `{' must be a compound-statement. */
11186 else if (token
->type
== CPP_OPEN_BRACE
)
11187 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11188 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11189 a statement all its own. */
11190 else if (token
->type
== CPP_PRAGMA
)
11192 /* Only certain OpenMP pragmas are attached to statements, and thus
11193 are considered statements themselves. All others are not. In
11194 the context of a compound, accept the pragma as a "statement" and
11195 return so that we can check for a close brace. Otherwise we
11196 require a real statement and must go back and read one. */
11198 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11199 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11203 else if (token
->type
== CPP_EOF
)
11205 cp_parser_error (parser
, "expected statement");
11209 /* Everything else must be a declaration-statement or an
11210 expression-statement. Try for the declaration-statement
11211 first, unless we are looking at a `;', in which case we know that
11212 we have an expression-statement. */
11215 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11217 if (std_attrs
!= NULL_TREE
)
11218 /* Attributes should be parsed as part of the declaration,
11219 so let's un-parse them. */
11220 saved_tokens
.rollback();
11222 cp_parser_parse_tentatively (parser
);
11223 /* Try to parse the declaration-statement. */
11224 cp_parser_declaration_statement (parser
);
11225 /* If that worked, we're done. */
11226 if (cp_parser_parse_definitely (parser
))
11228 /* It didn't work, restore the post-attribute position. */
11230 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11232 /* All preceding labels have been parsed at this point. */
11233 if (loc_after_labels
!= NULL
)
11234 *loc_after_labels
= statement_location
;
11236 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11238 /* Look for an expression-statement instead. */
11239 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11241 /* Handle [[fallthrough]];. */
11242 if (attribute_fallthrough_p (std_attrs
))
11244 /* The next token after the fallthrough attribute is ';'. */
11245 if (statement
== NULL_TREE
)
11247 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11248 statement
= build_call_expr_internal_loc (statement_location
,
11250 void_type_node
, 0);
11251 finish_expr_stmt (statement
);
11254 warning_at (statement_location
, OPT_Wattributes
,
11255 "%<fallthrough%> attribute not followed by %<;%>");
11256 std_attrs
= NULL_TREE
;
11260 /* Set the line number for the statement. */
11261 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11262 SET_EXPR_LOCATION (statement
, statement_location
);
11264 /* Allow "[[fallthrough]];", but warn otherwise. */
11265 if (std_attrs
!= NULL_TREE
)
11266 warning_at (attrs_location
,
11268 "attributes at the beginning of statement are ignored");
11271 /* Append ATTR to attribute list ATTRS. */
11274 attr_chainon (tree attrs
, tree attr
)
11276 if (attrs
== error_mark_node
)
11277 return error_mark_node
;
11278 if (attr
== error_mark_node
)
11279 return error_mark_node
;
11280 return chainon (attrs
, attr
);
11283 /* Parse the label for a labeled-statement, i.e.
11286 case constant-expression :
11290 case constant-expression ... constant-expression : statement
11292 When a label is parsed without errors, the label is added to the
11293 parse tree by the finish_* functions, so this function doesn't
11294 have to return the label. */
11297 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11300 tree label
= NULL_TREE
;
11301 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11303 /* The next token should be an identifier. */
11304 token
= cp_lexer_peek_token (parser
->lexer
);
11305 if (token
->type
!= CPP_NAME
11306 && token
->type
!= CPP_KEYWORD
)
11308 cp_parser_error (parser
, "expected labeled-statement");
11312 /* Remember whether this case or a user-defined label is allowed to fall
11314 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11316 parser
->colon_corrects_to_scope_p
= false;
11317 switch (token
->keyword
)
11321 tree expr
, expr_hi
;
11322 cp_token
*ellipsis
;
11324 /* Consume the `case' token. */
11325 cp_lexer_consume_token (parser
->lexer
);
11326 /* Parse the constant-expression. */
11327 expr
= cp_parser_constant_expression (parser
);
11328 if (check_for_bare_parameter_packs (expr
))
11329 expr
= error_mark_node
;
11331 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11332 if (ellipsis
->type
== CPP_ELLIPSIS
)
11334 /* Consume the `...' token. */
11335 cp_lexer_consume_token (parser
->lexer
);
11336 expr_hi
= cp_parser_constant_expression (parser
);
11337 if (check_for_bare_parameter_packs (expr_hi
))
11338 expr_hi
= error_mark_node
;
11340 /* We don't need to emit warnings here, as the common code
11341 will do this for us. */
11344 expr_hi
= NULL_TREE
;
11346 if (parser
->in_switch_statement_p
)
11348 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11349 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11351 label
= CASE_LABEL (l
);
11352 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11356 error_at (token
->location
,
11357 "case label %qE not within a switch statement",
11363 /* Consume the `default' token. */
11364 cp_lexer_consume_token (parser
->lexer
);
11366 if (parser
->in_switch_statement_p
)
11368 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11369 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11371 label
= CASE_LABEL (l
);
11372 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11376 error_at (token
->location
, "case label not within a switch statement");
11380 /* Anything else must be an ordinary label. */
11381 label
= finish_label_stmt (cp_parser_identifier (parser
));
11382 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11383 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11387 /* Require the `:' token. */
11388 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11390 /* An ordinary label may optionally be followed by attributes.
11391 However, this is only permitted if the attributes are then
11392 followed by a semicolon. This is because, for backward
11393 compatibility, when parsing
11394 lab: __attribute__ ((unused)) int i;
11395 we want the attribute to attach to "i", not "lab". */
11396 if (label
!= NULL_TREE
11397 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11400 cp_parser_parse_tentatively (parser
);
11401 attrs
= cp_parser_gnu_attributes_opt (parser
);
11402 if (attrs
== NULL_TREE
11403 /* And fallthrough always binds to the expression-statement. */
11404 || attribute_fallthrough_p (attrs
)
11405 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11406 cp_parser_abort_tentative_parse (parser
);
11407 else if (!cp_parser_parse_definitely (parser
))
11410 attributes
= attr_chainon (attributes
, attrs
);
11413 if (attributes
!= NULL_TREE
)
11414 cplus_decl_attributes (&label
, attributes
, 0);
11416 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11419 /* Parse an expression-statement.
11421 expression-statement:
11424 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11425 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11426 indicates whether this expression-statement is part of an
11427 expression statement. */
11430 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11432 tree statement
= NULL_TREE
;
11433 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11434 location_t loc
= token
->location
;
11436 /* There might be attribute fallthrough. */
11437 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11439 /* If the next token is a ';', then there is no expression
11441 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11443 statement
= cp_parser_expression (parser
);
11444 if (statement
== error_mark_node
11445 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11447 cp_parser_skip_to_end_of_block_or_statement (parser
);
11448 return error_mark_node
;
11452 /* Handle [[fallthrough]];. */
11453 if (attribute_fallthrough_p (attr
))
11455 /* The next token after the fallthrough attribute is ';'. */
11456 if (statement
== NULL_TREE
)
11457 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11458 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11459 void_type_node
, 0);
11461 warning_at (loc
, OPT_Wattributes
,
11462 "%<fallthrough%> attribute not followed by %<;%>");
11466 /* Allow "[[fallthrough]];", but warn otherwise. */
11467 if (attr
!= NULL_TREE
)
11468 warning_at (loc
, OPT_Wattributes
,
11469 "attributes at the beginning of statement are ignored");
11471 /* Give a helpful message for "A<T>::type t;" and the like. */
11472 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11473 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11475 if (TREE_CODE (statement
) == SCOPE_REF
)
11476 error_at (token
->location
, "need %<typename%> before %qE because "
11477 "%qT is a dependent scope",
11478 statement
, TREE_OPERAND (statement
, 0));
11479 else if (is_overloaded_fn (statement
)
11480 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11483 tree fn
= get_first_fn (statement
);
11484 error_at (token
->location
,
11485 "%<%T::%D%> names the constructor, not the type",
11486 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11490 /* Consume the final `;'. */
11491 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11493 if (in_statement_expr
11494 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11495 /* This is the final expression statement of a statement
11497 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11498 else if (statement
)
11499 statement
= finish_expr_stmt (statement
);
11504 /* Parse a compound-statement.
11506 compound-statement:
11507 { statement-seq [opt] }
11511 compound-statement:
11512 { label-declaration-seq [opt] statement-seq [opt] }
11514 label-declaration-seq:
11516 label-declaration-seq label-declaration
11518 Returns a tree representing the statement. */
11521 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11522 int bcs_flags
, bool function_body
)
11524 tree compound_stmt
;
11525 matching_braces braces
;
11527 /* Consume the `{'. */
11528 if (!braces
.require_open (parser
))
11529 return error_mark_node
;
11530 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11531 && !function_body
&& cxx_dialect
< cxx14
)
11532 pedwarn (input_location
, OPT_Wpedantic
,
11533 "compound-statement in %<constexpr%> function");
11534 /* Begin the compound-statement. */
11535 compound_stmt
= begin_compound_stmt (bcs_flags
);
11536 /* If the next keyword is `__label__' we have a label declaration. */
11537 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11538 cp_parser_label_declaration (parser
);
11539 /* Parse an (optional) statement-seq. */
11540 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11541 /* Finish the compound-statement. */
11542 finish_compound_stmt (compound_stmt
);
11543 /* Consume the `}'. */
11544 braces
.require_close (parser
);
11546 return compound_stmt
;
11549 /* Parse an (optional) statement-seq.
11553 statement-seq [opt] statement */
11556 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11558 /* Scan statements until there aren't any more. */
11561 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11563 /* If we are looking at a `}', then we have run out of
11564 statements; the same is true if we have reached the end
11565 of file, or have stumbled upon a stray '@end'. */
11566 if (token
->type
== CPP_CLOSE_BRACE
11567 || token
->type
== CPP_EOF
11568 || token
->type
== CPP_PRAGMA_EOL
11569 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11572 /* If we are in a compound statement and find 'else' then
11573 something went wrong. */
11574 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11576 if (parser
->in_statement
& IN_IF_STMT
)
11580 token
= cp_lexer_consume_token (parser
->lexer
);
11581 error_at (token
->location
, "%<else%> without a previous %<if%>");
11585 /* Parse the statement. */
11586 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11590 /* Return true if this is the C++20 version of range-based-for with
11594 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11598 /* Save tokens so that we can put them back. */
11599 cp_lexer_save_tokens (parser
->lexer
);
11601 /* There has to be an unnested ; followed by an unnested :. */
11602 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11603 /*recovering=*/false,
11605 /*consume_paren=*/false) != -1)
11608 /* We found the semicolon, eat it now. */
11609 cp_lexer_consume_token (parser
->lexer
);
11611 /* Now look for ':' that is not nested in () or {}. */
11612 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11613 /*recovering=*/false,
11615 /*consume_paren=*/false) == -1);
11618 /* Roll back the tokens we skipped. */
11619 cp_lexer_rollback_tokens (parser
->lexer
);
11624 /* Return true if we're looking at (init; cond), false otherwise. */
11627 cp_parser_init_statement_p (cp_parser
*parser
)
11629 /* Save tokens so that we can put them back. */
11630 cp_lexer_save_tokens (parser
->lexer
);
11632 /* Look for ';' that is not nested in () or {}. */
11633 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11634 /*recovering=*/false,
11636 /*consume_paren=*/false);
11638 /* Roll back the tokens we skipped. */
11639 cp_lexer_rollback_tokens (parser
->lexer
);
11644 /* Parse a selection-statement.
11646 selection-statement:
11647 if ( init-statement [opt] condition ) statement
11648 if ( init-statement [opt] condition ) statement else statement
11649 switch ( init-statement [opt] condition ) statement
11651 Returns the new IF_STMT or SWITCH_STMT.
11653 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11654 is a (possibly labeled) if statement which is not enclosed in
11655 braces and has an else clause. This is used to implement
11658 CHAIN is a vector of if-else-if conditions. This is used to implement
11659 -Wduplicated-cond. */
11662 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11667 token_indent_info guard_tinfo
;
11672 /* Peek at the next token. */
11673 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11674 guard_tinfo
= get_token_indent_info (token
);
11676 /* See what kind of keyword it is. */
11677 keyword
= token
->keyword
;
11687 if (keyword
== RID_IF
11688 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11692 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11693 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11694 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11695 "with -std=c++17 or -std=gnu++17");
11698 /* Look for the `('. */
11699 matching_parens parens
;
11700 if (!parens
.require_open (parser
))
11702 cp_parser_skip_to_end_of_statement (parser
);
11703 return error_mark_node
;
11706 /* Begin the selection-statement. */
11707 if (keyword
== RID_IF
)
11709 statement
= begin_if_stmt ();
11710 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11713 statement
= begin_switch_stmt ();
11715 /* Parse the optional init-statement. */
11716 if (cp_parser_init_statement_p (parser
))
11719 if (cxx_dialect
< cxx17
)
11720 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11721 "init-statement in selection statements only available "
11722 "with -std=c++17 or -std=gnu++17");
11723 cp_parser_init_statement (parser
, &decl
);
11726 /* Parse the condition. */
11727 condition
= cp_parser_condition (parser
);
11728 /* Look for the `)'. */
11729 if (!parens
.require_close (parser
))
11730 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11731 /*consume_paren=*/true);
11733 if (keyword
== RID_IF
)
11736 unsigned char in_statement
;
11738 /* Add the condition. */
11739 condition
= finish_if_stmt_cond (condition
, statement
);
11741 if (warn_duplicated_cond
)
11742 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11745 /* Parse the then-clause. */
11746 in_statement
= parser
->in_statement
;
11747 parser
->in_statement
|= IN_IF_STMT
;
11749 /* Outside a template, the non-selected branch of a constexpr
11750 if is a 'discarded statement', i.e. unevaluated. */
11751 bool was_discarded
= in_discarded_stmt
;
11752 bool discard_then
= (cx
&& !processing_template_decl
11753 && integer_zerop (condition
));
11756 in_discarded_stmt
= true;
11757 ++c_inhibit_evaluation_warnings
;
11760 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11763 parser
->in_statement
= in_statement
;
11765 finish_then_clause (statement
);
11769 THEN_CLAUSE (statement
) = NULL_TREE
;
11770 in_discarded_stmt
= was_discarded
;
11771 --c_inhibit_evaluation_warnings
;
11774 /* If the next token is `else', parse the else-clause. */
11775 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11778 bool discard_else
= (cx
&& !processing_template_decl
11779 && integer_nonzerop (condition
));
11782 in_discarded_stmt
= true;
11783 ++c_inhibit_evaluation_warnings
;
11787 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11788 /* Consume the `else' keyword. */
11789 cp_lexer_consume_token (parser
->lexer
);
11790 if (warn_duplicated_cond
)
11792 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11796 /* We've got "if (COND) else if (COND2)". Start
11797 the condition chain and add COND as the first
11799 chain
= new vec
<tree
> ();
11800 if (!CONSTANT_CLASS_P (condition
)
11801 && !TREE_SIDE_EFFECTS (condition
))
11803 /* Wrap it in a NOP_EXPR so that we can set the
11804 location of the condition. */
11805 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11807 SET_EXPR_LOCATION (e
, token
->location
);
11808 chain
->safe_push (e
);
11811 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11814 /* This is if-else without subsequent if. Zap the
11815 condition chain; we would have already warned at
11821 begin_else_clause (statement
);
11822 /* Parse the else-clause. */
11823 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11824 guard_tinfo
, chain
);
11826 finish_else_clause (statement
);
11828 /* If we are currently parsing a then-clause, then
11829 IF_P will not be NULL. We set it to true to
11830 indicate that this if statement has an else clause.
11831 This may trigger the Wparentheses warning below
11832 when we get back up to the parent if statement. */
11838 ELSE_CLAUSE (statement
) = NULL_TREE
;
11839 in_discarded_stmt
= was_discarded
;
11840 --c_inhibit_evaluation_warnings
;
11845 /* This if statement does not have an else clause. If
11846 NESTED_IF is true, then the then-clause has an if
11847 statement which does have an else clause. We warn
11848 about the potential ambiguity. */
11850 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11851 "suggest explicit braces to avoid ambiguous"
11853 if (warn_duplicated_cond
)
11855 /* We don't need the condition chain anymore. */
11861 /* Now we're all done with the if-statement. */
11862 finish_if_stmt (statement
);
11866 bool in_switch_statement_p
;
11867 unsigned char in_statement
;
11869 /* Add the condition. */
11870 finish_switch_cond (condition
, statement
);
11872 /* Parse the body of the switch-statement. */
11873 in_switch_statement_p
= parser
->in_switch_statement_p
;
11874 in_statement
= parser
->in_statement
;
11875 parser
->in_switch_statement_p
= true;
11876 parser
->in_statement
|= IN_SWITCH_STMT
;
11877 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11879 parser
->in_switch_statement_p
= in_switch_statement_p
;
11880 parser
->in_statement
= in_statement
;
11882 /* Now we're all done with the switch-statement. */
11883 finish_switch_stmt (statement
);
11891 cp_parser_error (parser
, "expected selection-statement");
11892 return error_mark_node
;
11896 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11897 If we have seen at least one decl-specifier, and the next token
11898 is not a parenthesis, then we must be looking at a declaration.
11899 (After "int (" we might be looking at a functional cast.) */
11902 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11903 bool any_specifiers_p
)
11905 if (any_specifiers_p
11906 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11907 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11908 && !cp_parser_error_occurred (parser
))
11909 cp_parser_commit_to_tentative_parse (parser
);
11912 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11913 The declarator shall not specify a function or an array. Returns
11914 TRUE if the declarator is valid, FALSE otherwise. */
11917 cp_parser_check_condition_declarator (cp_parser
* parser
,
11918 cp_declarator
*declarator
,
11921 if (declarator
== cp_error_declarator
11922 || function_declarator_p (declarator
)
11923 || declarator
->kind
== cdk_array
)
11925 if (declarator
== cp_error_declarator
)
11926 /* Already complained. */;
11927 else if (declarator
->kind
== cdk_array
)
11928 error_at (loc
, "condition declares an array");
11930 error_at (loc
, "condition declares a function");
11931 if (parser
->fully_implicit_function_template_p
)
11932 abort_fully_implicit_template (parser
);
11933 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11934 /*or_comma=*/false,
11935 /*consume_paren=*/false);
11942 /* Parse a condition.
11946 type-specifier-seq declarator = initializer-clause
11947 type-specifier-seq declarator braced-init-list
11952 type-specifier-seq declarator asm-specification [opt]
11953 attributes [opt] = assignment-expression
11955 Returns the expression that should be tested. */
11958 cp_parser_condition (cp_parser
* parser
)
11960 cp_decl_specifier_seq type_specifiers
;
11961 const char *saved_message
;
11962 int declares_class_or_enum
;
11964 /* Try the declaration first. */
11965 cp_parser_parse_tentatively (parser
);
11966 /* New types are not allowed in the type-specifier-seq for a
11968 saved_message
= parser
->type_definition_forbidden_message
;
11969 parser
->type_definition_forbidden_message
11970 = G_("types may not be defined in conditions");
11971 /* Parse the type-specifier-seq. */
11972 cp_parser_decl_specifier_seq (parser
,
11973 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11975 &declares_class_or_enum
);
11976 /* Restore the saved message. */
11977 parser
->type_definition_forbidden_message
= saved_message
;
11979 cp_parser_maybe_commit_to_declaration (parser
,
11980 type_specifiers
.any_specifiers_p
);
11982 /* If all is well, we might be looking at a declaration. */
11983 if (!cp_parser_error_occurred (parser
))
11986 tree asm_specification
;
11988 cp_declarator
*declarator
;
11989 tree initializer
= NULL_TREE
;
11990 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11992 /* Parse the declarator. */
11993 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11994 CP_PARSER_FLAGS_NONE
,
11995 /*ctor_dtor_or_conv_p=*/NULL
,
11996 /*parenthesized_p=*/NULL
,
11997 /*member_p=*/false,
11998 /*friend_p=*/false,
11999 /*static_p=*/false);
12000 /* Parse the attributes. */
12001 attributes
= cp_parser_attributes_opt (parser
);
12002 /* Parse the asm-specification. */
12003 asm_specification
= cp_parser_asm_specification_opt (parser
);
12004 /* If the next token is not an `=' or '{', then we might still be
12005 looking at an expression. For example:
12009 looks like a decl-specifier-seq and a declarator -- but then
12010 there is no `=', so this is an expression. */
12011 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
12012 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12013 cp_parser_simulate_error (parser
);
12015 /* If we did see an `=' or '{', then we are looking at a declaration
12017 if (cp_parser_parse_definitely (parser
))
12020 bool non_constant_p
= false;
12021 int flags
= LOOKUP_ONLYCONVERTING
;
12023 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
12024 return error_mark_node
;
12026 /* Create the declaration. */
12027 decl
= start_decl (declarator
, &type_specifiers
,
12028 /*initialized_p=*/true,
12029 attributes
, /*prefix_attributes=*/NULL_TREE
,
12032 /* Parse the initializer. */
12033 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12035 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
12036 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
12039 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12041 /* Consume the `='. */
12042 cp_lexer_consume_token (parser
->lexer
);
12043 initializer
= cp_parser_initializer_clause (parser
,
12048 cp_parser_error (parser
, "expected initializer");
12049 initializer
= error_mark_node
;
12051 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
12052 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12054 /* Process the initializer. */
12055 cp_finish_decl (decl
,
12056 initializer
, !non_constant_p
,
12061 pop_scope (pushed_scope
);
12063 return convert_from_reference (decl
);
12066 /* If we didn't even get past the declarator successfully, we are
12067 definitely not looking at a declaration. */
12069 cp_parser_abort_tentative_parse (parser
);
12071 /* Otherwise, we are looking at an expression. */
12072 return cp_parser_expression (parser
);
12075 /* Parses a for-statement or range-for-statement until the closing ')',
12079 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
12081 tree init
, scope
, decl
;
12084 /* Begin the for-statement. */
12085 scope
= begin_for_scope (&init
);
12087 /* Parse the initialization. */
12088 is_range_for
= cp_parser_init_statement (parser
, &decl
);
12091 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
12094 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
12098 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
12099 unsigned short unroll
)
12101 /* Normal for loop */
12102 tree condition
= NULL_TREE
;
12103 tree expression
= NULL_TREE
;
12106 stmt
= begin_for_stmt (scope
, init
);
12107 /* The init-statement has already been parsed in
12108 cp_parser_init_statement, so no work is needed here. */
12109 finish_init_stmt (stmt
);
12111 /* If there's a condition, process it. */
12112 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12113 condition
= cp_parser_condition (parser
);
12116 cp_parser_error (parser
, "missing loop condition in loop with "
12117 "%<GCC ivdep%> pragma");
12118 condition
= error_mark_node
;
12122 cp_parser_error (parser
, "missing loop condition in loop with "
12123 "%<GCC unroll%> pragma");
12124 condition
= error_mark_node
;
12126 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
12127 /* Look for the `;'. */
12128 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12130 /* If there's an expression, process it. */
12131 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
12132 expression
= cp_parser_expression (parser
);
12133 finish_for_expr (expression
, stmt
);
12138 /* Tries to parse a range-based for-statement:
12141 decl-specifier-seq declarator : expression
12143 The decl-specifier-seq declarator and the `:' are already parsed by
12144 cp_parser_init_statement. If processing_template_decl it returns a
12145 newly created RANGE_FOR_STMT; if not, it is converted to a
12146 regular FOR_STMT. */
12149 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
12150 bool ivdep
, unsigned short unroll
, bool is_omp
)
12152 tree stmt
, range_expr
;
12153 auto_vec
<cxx_binding
*, 16> bindings
;
12154 auto_vec
<tree
, 16> names
;
12155 tree decomp_first_name
= NULL_TREE
;
12156 unsigned int decomp_cnt
= 0;
12158 /* Get the range declaration momentarily out of the way so that
12159 the range expression doesn't clash with it. */
12160 if (range_decl
!= error_mark_node
)
12162 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
12164 tree v
= DECL_VALUE_EXPR (range_decl
);
12165 /* For decomposition declaration get all of the corresponding
12166 declarations out of the way. */
12167 if (TREE_CODE (v
) == ARRAY_REF
12168 && VAR_P (TREE_OPERAND (v
, 0))
12169 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
12171 tree d
= range_decl
;
12172 range_decl
= TREE_OPERAND (v
, 0);
12173 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
12174 decomp_first_name
= d
;
12175 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
12177 tree name
= DECL_NAME (d
);
12178 names
.safe_push (name
);
12179 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12180 IDENTIFIER_BINDING (name
)
12181 = IDENTIFIER_BINDING (name
)->previous
;
12185 if (names
.is_empty ())
12187 tree name
= DECL_NAME (range_decl
);
12188 names
.safe_push (name
);
12189 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12190 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
12194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12196 bool expr_non_constant_p
;
12197 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12200 range_expr
= cp_parser_expression (parser
);
12202 /* Put the range declaration(s) back into scope. */
12203 for (unsigned int i
= 0; i
< names
.length (); i
++)
12205 cxx_binding
*binding
= bindings
[i
];
12206 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12207 IDENTIFIER_BINDING (names
[i
]) = binding
;
12210 /* finish_omp_for has its own code for the following, so just
12211 return the range_expr instead. */
12215 /* If in template, STMT is converted to a normal for-statement
12216 at instantiation. If not, it is done just ahead. */
12217 if (processing_template_decl
)
12219 if (check_for_bare_parameter_packs (range_expr
))
12220 range_expr
= error_mark_node
;
12221 stmt
= begin_range_for_stmt (scope
, init
);
12223 RANGE_FOR_IVDEP (stmt
) = 1;
12225 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12226 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12227 if (!type_dependent_expression_p (range_expr
)
12228 /* do_auto_deduction doesn't mess with template init-lists. */
12229 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12230 do_range_for_auto_deduction (range_decl
, range_expr
);
12234 stmt
= begin_for_stmt (scope
, init
);
12235 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12236 decomp_first_name
, decomp_cnt
, ivdep
,
12242 /* Subroutine of cp_convert_range_for: given the initializer expression,
12243 builds up the range temporary. */
12246 build_range_temp (tree range_expr
)
12248 tree range_type
, range_temp
;
12250 /* Find out the type deduced by the declaration
12251 `auto &&__range = range_expr'. */
12252 range_type
= cp_build_reference_type (make_auto (), true);
12253 range_type
= do_auto_deduction (range_type
, range_expr
,
12254 type_uses_auto (range_type
));
12256 /* Create the __range variable. */
12257 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12259 TREE_USED (range_temp
) = 1;
12260 DECL_ARTIFICIAL (range_temp
) = 1;
12265 /* Used by cp_parser_range_for in template context: we aren't going to
12266 do a full conversion yet, but we still need to resolve auto in the
12267 type of the for-range-declaration if present. This is basically
12268 a shortcut version of cp_convert_range_for. */
12271 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12273 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12276 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12277 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12278 iter_type
= (cp_parser_perform_range_for_lookup
12279 (range_temp
, &begin_dummy
, &end_dummy
));
12282 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12284 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12286 tf_warning_or_error
);
12287 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12288 iter_decl
, auto_node
);
12293 /* Converts a range-based for-statement into a normal
12294 for-statement, as per the definition.
12296 for (RANGE_DECL : RANGE_EXPR)
12299 should be equivalent to:
12302 auto &&__range = RANGE_EXPR;
12303 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12307 RANGE_DECL = *__begin;
12312 If RANGE_EXPR is an array:
12313 BEGIN_EXPR = __range
12314 END_EXPR = __range + ARRAY_SIZE(__range)
12315 Else if RANGE_EXPR has a member 'begin' or 'end':
12316 BEGIN_EXPR = __range.begin()
12317 END_EXPR = __range.end()
12319 BEGIN_EXPR = begin(__range)
12320 END_EXPR = end(__range);
12322 If __range has a member 'begin' but not 'end', or vice versa, we must
12323 still use the second alternative (it will surely fail, however).
12324 When calling begin()/end() in the third alternative we must use
12325 argument dependent lookup, but always considering 'std' as an associated
12329 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12330 tree decomp_first_name
, unsigned int decomp_cnt
,
12331 bool ivdep
, unsigned short unroll
)
12334 tree iter_type
, begin_expr
, end_expr
;
12335 tree condition
, expression
;
12337 range_expr
= mark_lvalue_use (range_expr
);
12339 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12340 /* If an error happened previously do nothing or else a lot of
12341 unhelpful errors would be issued. */
12342 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12347 if (VAR_P (range_expr
)
12348 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12349 /* Can't bind a reference to an array of runtime bound. */
12350 range_temp
= range_expr
;
12353 range_temp
= build_range_temp (range_expr
);
12354 pushdecl (range_temp
);
12355 cp_finish_decl (range_temp
, range_expr
,
12356 /*is_constant_init*/false, NULL_TREE
,
12357 LOOKUP_ONLYCONVERTING
);
12358 range_temp
= convert_from_reference (range_temp
);
12360 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12361 &begin_expr
, &end_expr
);
12364 /* The new for initialization statement. */
12365 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12367 TREE_USED (begin
) = 1;
12368 DECL_ARTIFICIAL (begin
) = 1;
12370 cp_finish_decl (begin
, begin_expr
,
12371 /*is_constant_init*/false, NULL_TREE
,
12372 LOOKUP_ONLYCONVERTING
);
12374 if (cxx_dialect
>= cxx17
)
12375 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12376 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12377 TREE_USED (end
) = 1;
12378 DECL_ARTIFICIAL (end
) = 1;
12380 cp_finish_decl (end
, end_expr
,
12381 /*is_constant_init*/false, NULL_TREE
,
12382 LOOKUP_ONLYCONVERTING
);
12384 finish_init_stmt (statement
);
12386 /* The new for condition. */
12387 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12390 NULL
, tf_warning_or_error
);
12391 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12393 /* The new increment expression. */
12394 expression
= finish_unary_op_expr (input_location
,
12395 PREINCREMENT_EXPR
, begin
,
12396 tf_warning_or_error
);
12397 finish_for_expr (expression
, statement
);
12399 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12400 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12402 /* The declaration is initialized with *__begin inside the loop body. */
12403 cp_finish_decl (range_decl
,
12404 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12405 tf_warning_or_error
),
12406 /*is_constant_init*/false, NULL_TREE
,
12407 LOOKUP_ONLYCONVERTING
);
12408 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12409 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12414 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12415 We need to solve both at the same time because the method used
12416 depends on the existence of members begin or end.
12417 Returns the type deduced for the iterator expression. */
12420 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12422 if (error_operand_p (range
))
12424 *begin
= *end
= error_mark_node
;
12425 return error_mark_node
;
12428 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12430 error ("range-based %<for%> expression of type %qT "
12431 "has incomplete type", TREE_TYPE (range
));
12432 *begin
= *end
= error_mark_node
;
12433 return error_mark_node
;
12435 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12437 /* If RANGE is an array, we will use pointer arithmetic. */
12438 *begin
= decay_conversion (range
, tf_warning_or_error
);
12439 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12441 array_type_nelts_top (TREE_TYPE (range
)),
12443 return TREE_TYPE (*begin
);
12447 /* If it is not an array, we must do a bit of magic. */
12448 tree id_begin
, id_end
;
12449 tree member_begin
, member_end
;
12451 *begin
= *end
= error_mark_node
;
12453 id_begin
= get_identifier ("begin");
12454 id_end
= get_identifier ("end");
12455 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12456 /*protect=*/2, /*want_type=*/false,
12457 tf_warning_or_error
);
12458 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12459 /*protect=*/2, /*want_type=*/false,
12460 tf_warning_or_error
);
12462 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12464 /* Use the member functions. */
12465 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12466 *end
= cp_parser_range_for_member_function (range
, id_end
);
12470 /* Use global functions with ADL. */
12471 vec
<tree
, va_gc
> *vec
;
12472 vec
= make_tree_vector ();
12474 vec_safe_push (vec
, range
);
12476 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12477 tf_warning_or_error
);
12478 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12479 tf_warning_or_error
);
12480 member_end
= perform_koenig_lookup (id_end
, vec
,
12481 tf_warning_or_error
);
12482 *end
= finish_call_expr (member_end
, &vec
, false, true,
12483 tf_warning_or_error
);
12485 release_tree_vector (vec
);
12488 /* Last common checks. */
12489 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12491 /* If one of the expressions is an error do no more checks. */
12492 *begin
= *end
= error_mark_node
;
12493 return error_mark_node
;
12495 else if (type_dependent_expression_p (*begin
)
12496 || type_dependent_expression_p (*end
))
12497 /* Can happen, when, eg, in a template context, Koenig lookup
12498 can't resolve begin/end (c++/58503). */
12502 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12503 /* The unqualified type of the __begin and __end temporaries should
12504 be the same, as required by the multiple auto declaration. */
12505 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12507 if (cxx_dialect
>= cxx17
12508 && (build_x_binary_op (input_location
, NE_EXPR
,
12509 *begin
, ERROR_MARK
,
12512 != error_mark_node
))
12513 /* P0184R0 allows __begin and __end to have different types,
12514 but make sure they are comparable so we can give a better
12517 error ("inconsistent begin/end types in range-based %<for%> "
12518 "statement: %qT and %qT",
12519 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12526 /* Helper function for cp_parser_perform_range_for_lookup.
12527 Builds a tree for RANGE.IDENTIFIER(). */
12530 cp_parser_range_for_member_function (tree range
, tree identifier
)
12533 vec
<tree
, va_gc
> *vec
;
12535 member
= finish_class_member_access_expr (range
, identifier
,
12536 false, tf_warning_or_error
);
12537 if (member
== error_mark_node
)
12538 return error_mark_node
;
12540 vec
= make_tree_vector ();
12541 res
= finish_call_expr (member
, &vec
,
12542 /*disallow_virtual=*/false,
12543 /*koenig_p=*/false,
12544 tf_warning_or_error
);
12545 release_tree_vector (vec
);
12549 /* Parse an iteration-statement.
12551 iteration-statement:
12552 while ( condition ) statement
12553 do statement while ( expression ) ;
12554 for ( init-statement condition [opt] ; expression [opt] )
12557 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12560 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12561 unsigned short unroll
)
12566 unsigned char in_statement
;
12567 token_indent_info guard_tinfo
;
12569 /* Peek at the next token. */
12570 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12572 return error_mark_node
;
12574 guard_tinfo
= get_token_indent_info (token
);
12576 /* Remember whether or not we are already within an iteration
12578 in_statement
= parser
->in_statement
;
12580 /* See what kind of keyword it is. */
12581 keyword
= token
->keyword
;
12588 /* Begin the while-statement. */
12589 statement
= begin_while_stmt ();
12590 /* Look for the `('. */
12591 matching_parens parens
;
12592 parens
.require_open (parser
);
12593 /* Parse the condition. */
12594 condition
= cp_parser_condition (parser
);
12595 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12596 /* Look for the `)'. */
12597 parens
.require_close (parser
);
12598 /* Parse the dependent statement. */
12599 parser
->in_statement
= IN_ITERATION_STMT
;
12600 bool prev
= note_iteration_stmt_body_start ();
12601 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12602 note_iteration_stmt_body_end (prev
);
12603 parser
->in_statement
= in_statement
;
12604 /* We're done with the while-statement. */
12605 finish_while_stmt (statement
);
12613 /* Begin the do-statement. */
12614 statement
= begin_do_stmt ();
12615 /* Parse the body of the do-statement. */
12616 parser
->in_statement
= IN_ITERATION_STMT
;
12617 bool prev
= note_iteration_stmt_body_start ();
12618 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12619 note_iteration_stmt_body_end (prev
);
12620 parser
->in_statement
= in_statement
;
12621 finish_do_body (statement
);
12622 /* Look for the `while' keyword. */
12623 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12624 /* Look for the `('. */
12625 matching_parens parens
;
12626 parens
.require_open (parser
);
12627 /* Parse the expression. */
12628 expression
= cp_parser_expression (parser
);
12629 /* We're done with the do-statement. */
12630 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12631 /* Look for the `)'. */
12632 parens
.require_close (parser
);
12633 /* Look for the `;'. */
12634 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12640 /* Look for the `('. */
12641 matching_parens parens
;
12642 parens
.require_open (parser
);
12644 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12646 /* Look for the `)'. */
12647 parens
.require_close (parser
);
12649 /* Parse the body of the for-statement. */
12650 parser
->in_statement
= IN_ITERATION_STMT
;
12651 bool prev
= note_iteration_stmt_body_start ();
12652 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12653 note_iteration_stmt_body_end (prev
);
12654 parser
->in_statement
= in_statement
;
12656 /* We're done with the for-statement. */
12657 finish_for_stmt (statement
);
12662 cp_parser_error (parser
, "expected iteration-statement");
12663 statement
= error_mark_node
;
12670 /* Parse a init-statement or the declarator of a range-based-for.
12671 Returns true if a range-based-for declaration is seen.
12674 expression-statement
12675 simple-declaration */
12678 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12680 /* If the next token is a `;', then we have an empty
12681 expression-statement. Grammatically, this is also a
12682 simple-declaration, but an invalid one, because it does not
12683 declare anything. Therefore, if we did not handle this case
12684 specially, we would issue an error message about an invalid
12686 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12688 bool is_range_for
= false;
12689 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12691 /* Try to parse the init-statement. */
12692 if (cp_parser_range_based_for_with_init_p (parser
))
12695 cp_parser_parse_tentatively (parser
);
12696 /* Parse the declaration. */
12697 cp_parser_simple_declaration (parser
,
12698 /*function_definition_allowed_p=*/false,
12700 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12701 if (!cp_parser_parse_definitely (parser
))
12702 /* That didn't work, try to parse it as an expression-statement. */
12703 cp_parser_expression_statement (parser
, NULL_TREE
);
12705 if (cxx_dialect
< cxx2a
)
12707 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12708 "range-based %<for%> loops with initializer only "
12709 "available with -std=c++2a or -std=gnu++2a");
12710 *decl
= error_mark_node
;
12714 /* A colon is used in range-based for. */
12715 parser
->colon_corrects_to_scope_p
= false;
12717 /* We're going to speculatively look for a declaration, falling back
12718 to an expression, if necessary. */
12719 cp_parser_parse_tentatively (parser
);
12720 /* Parse the declaration. */
12721 cp_parser_simple_declaration (parser
,
12722 /*function_definition_allowed_p=*/false,
12724 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12725 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12727 /* It is a range-for, consume the ':'. */
12728 cp_lexer_consume_token (parser
->lexer
);
12729 is_range_for
= true;
12730 if (cxx_dialect
< cxx11
)
12731 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12732 "range-based %<for%> loops only available with "
12733 "-std=c++11 or -std=gnu++11");
12736 /* The ';' is not consumed yet because we told
12737 cp_parser_simple_declaration not to. */
12738 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12740 if (cp_parser_parse_definitely (parser
))
12741 return is_range_for
;
12742 /* If the tentative parse failed, then we shall need to look for an
12743 expression-statement. */
12745 /* If we are here, it is an expression-statement. */
12746 cp_parser_expression_statement (parser
, NULL_TREE
);
12750 /* Parse a jump-statement.
12755 return expression [opt] ;
12756 return braced-init-list ;
12762 goto * expression ;
12764 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12767 cp_parser_jump_statement (cp_parser
* parser
)
12769 tree statement
= error_mark_node
;
12772 unsigned char in_statement
;
12774 /* Peek at the next token. */
12775 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12777 return error_mark_node
;
12779 /* See what kind of keyword it is. */
12780 keyword
= token
->keyword
;
12784 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12785 switch (in_statement
)
12788 error_at (token
->location
, "break statement not within loop or switch");
12791 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12792 || in_statement
== IN_ITERATION_STMT
);
12793 statement
= finish_break_stmt ();
12794 if (in_statement
== IN_ITERATION_STMT
)
12795 break_maybe_infinite_loop ();
12798 error_at (token
->location
, "invalid exit from OpenMP structured block");
12801 error_at (token
->location
, "break statement used with OpenMP for loop");
12804 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12808 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12811 error_at (token
->location
, "continue statement not within a loop");
12813 /* Fall through. */
12814 case IN_ITERATION_STMT
:
12816 statement
= finish_continue_stmt ();
12819 error_at (token
->location
, "invalid exit from OpenMP structured block");
12822 gcc_unreachable ();
12824 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12830 bool expr_non_constant_p
;
12832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12834 cp_lexer_set_source_position (parser
->lexer
);
12835 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12836 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12838 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12839 expr
= cp_parser_expression (parser
);
12841 /* If the next token is a `;', then there is no
12844 /* Build the return-statement. */
12845 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12846 /* Don't deduce from a discarded return statement. */;
12848 statement
= finish_return_stmt (expr
);
12849 /* Look for the final `;'. */
12850 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12855 if (parser
->in_function_body
12856 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12858 error ("%<goto%> in %<constexpr%> function");
12859 cp_function_chain
->invalid_constexpr
= true;
12862 /* Create the goto-statement. */
12863 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12865 /* Issue a warning about this use of a GNU extension. */
12866 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12867 /* Consume the '*' token. */
12868 cp_lexer_consume_token (parser
->lexer
);
12869 /* Parse the dependent expression. */
12870 finish_goto_stmt (cp_parser_expression (parser
));
12873 finish_goto_stmt (cp_parser_identifier (parser
));
12874 /* Look for the final `;'. */
12875 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12879 cp_parser_error (parser
, "expected jump-statement");
12886 /* Parse a declaration-statement.
12888 declaration-statement:
12889 block-declaration */
12892 cp_parser_declaration_statement (cp_parser
* parser
)
12896 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12897 p
= obstack_alloc (&declarator_obstack
, 0);
12899 /* Parse the block-declaration. */
12900 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12902 /* Free any declarators allocated. */
12903 obstack_free (&declarator_obstack
, p
);
12906 /* Some dependent statements (like `if (cond) statement'), are
12907 implicitly in their own scope. In other words, if the statement is
12908 a single statement (as opposed to a compound-statement), it is
12909 none-the-less treated as if it were enclosed in braces. Any
12910 declarations appearing in the dependent statement are out of scope
12911 after control passes that point. This function parses a statement,
12912 but ensures that is in its own scope, even if it is not a
12913 compound-statement.
12915 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12916 is a (possibly labeled) if statement which is not enclosed in
12917 braces and has an else clause. This is used to implement
12920 CHAIN is a vector of if-else-if conditions. This is used to implement
12923 Returns the new statement. */
12926 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12927 const token_indent_info
&guard_tinfo
,
12931 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12932 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12933 token_indent_info body_tinfo
12934 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12939 /* Mark if () ; with a special NOP_EXPR. */
12940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12942 cp_lexer_consume_token (parser
->lexer
);
12943 statement
= add_stmt (build_empty_stmt (body_loc
));
12945 if (guard_tinfo
.keyword
== RID_IF
12946 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12947 warning_at (body_loc
, OPT_Wempty_body
,
12948 "suggest braces around empty body in an %<if%> statement");
12949 else if (guard_tinfo
.keyword
== RID_ELSE
)
12950 warning_at (body_loc
, OPT_Wempty_body
,
12951 "suggest braces around empty body in an %<else%> statement");
12953 /* if a compound is opened, we simply parse the statement directly. */
12954 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12955 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12956 /* If the token is not a `{', then we must take special action. */
12959 /* Create a compound-statement. */
12960 statement
= begin_compound_stmt (0);
12961 /* Parse the dependent-statement. */
12962 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12963 &body_loc_after_labels
);
12964 /* Finish the dummy compound-statement. */
12965 finish_compound_stmt (statement
);
12968 token_indent_info next_tinfo
12969 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12970 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12972 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12973 && next_tinfo
.type
!= CPP_SEMICOLON
)
12974 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12975 guard_tinfo
.location
, guard_tinfo
.keyword
);
12977 /* Return the statement. */
12981 /* For some dependent statements (like `while (cond) statement'), we
12982 have already created a scope. Therefore, even if the dependent
12983 statement is a compound-statement, we do not want to create another
12987 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12988 const token_indent_info
&guard_tinfo
)
12990 /* If the token is a `{', then we must take special action. */
12991 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12993 token_indent_info body_tinfo
12994 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12995 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12997 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12998 &loc_after_labels
);
12999 token_indent_info next_tinfo
13000 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13001 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13003 if (loc_after_labels
!= UNKNOWN_LOCATION
13004 && next_tinfo
.type
!= CPP_SEMICOLON
)
13005 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
13006 guard_tinfo
.location
,
13007 guard_tinfo
.keyword
);
13011 /* Avoid calling cp_parser_compound_statement, so that we
13012 don't create a new scope. Do everything else by hand. */
13013 matching_braces braces
;
13014 braces
.require_open (parser
);
13015 /* If the next keyword is `__label__' we have a label declaration. */
13016 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
13017 cp_parser_label_declaration (parser
);
13018 /* Parse an (optional) statement-seq. */
13019 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
13020 braces
.require_close (parser
);
13024 /* Declarations [gram.dcl.dcl] */
13026 /* Parse an optional declaration-sequence.
13030 declaration-seq declaration */
13033 cp_parser_declaration_seq_opt (cp_parser
* parser
)
13037 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13039 if (token
->type
== CPP_CLOSE_BRACE
13040 || token
->type
== CPP_EOF
)
13043 cp_parser_toplevel_declaration (parser
);
13047 /* Parse a declaration.
13051 function-definition
13052 template-declaration
13053 explicit-instantiation
13054 explicit-specialization
13055 linkage-specification
13056 namespace-definition
13064 __extension__ declaration */
13067 cp_parser_declaration (cp_parser
* parser
)
13071 int saved_pedantic
;
13073 tree attributes
= NULL_TREE
;
13075 /* Check for the `__extension__' keyword. */
13076 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13078 /* Parse the qualified declaration. */
13079 cp_parser_declaration (parser
);
13080 /* Restore the PEDANTIC flag. */
13081 pedantic
= saved_pedantic
;
13086 /* Try to figure out what kind of declaration is present. */
13087 token1
= *cp_lexer_peek_token (parser
->lexer
);
13089 if (token1
.type
!= CPP_EOF
)
13090 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
13093 token2
.type
= CPP_EOF
;
13094 token2
.keyword
= RID_MAX
;
13097 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13098 p
= obstack_alloc (&declarator_obstack
, 0);
13100 /* If the next token is `extern' and the following token is a string
13101 literal, then we have a linkage specification. */
13102 if (token1
.keyword
== RID_EXTERN
13103 && cp_parser_is_pure_string_literal (&token2
))
13104 cp_parser_linkage_specification (parser
);
13105 /* If the next token is `template', then we have either a template
13106 declaration, an explicit instantiation, or an explicit
13108 else if (token1
.keyword
== RID_TEMPLATE
)
13110 /* `template <>' indicates a template specialization. */
13111 if (token2
.type
== CPP_LESS
13112 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13113 cp_parser_explicit_specialization (parser
);
13114 /* `template <' indicates a template declaration. */
13115 else if (token2
.type
== CPP_LESS
)
13116 cp_parser_template_declaration (parser
, /*member_p=*/false);
13117 /* Anything else must be an explicit instantiation. */
13119 cp_parser_explicit_instantiation (parser
);
13121 /* If the next token is `export', then we have a template
13123 else if (token1
.keyword
== RID_EXPORT
)
13124 cp_parser_template_declaration (parser
, /*member_p=*/false);
13125 /* If the next token is `extern', 'static' or 'inline' and the one
13126 after that is `template', we have a GNU extended explicit
13127 instantiation directive. */
13128 else if (cp_parser_allow_gnu_extensions_p (parser
)
13129 && (token1
.keyword
== RID_EXTERN
13130 || token1
.keyword
== RID_STATIC
13131 || token1
.keyword
== RID_INLINE
)
13132 && token2
.keyword
== RID_TEMPLATE
)
13133 cp_parser_explicit_instantiation (parser
);
13134 /* If the next token is `namespace', check for a named or unnamed
13135 namespace definition. */
13136 else if (token1
.keyword
== RID_NAMESPACE
13137 && (/* A named namespace definition. */
13138 (token2
.type
== CPP_NAME
13139 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13141 || (token2
.type
== CPP_OPEN_SQUARE
13142 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13143 == CPP_OPEN_SQUARE
)
13144 /* An unnamed namespace definition. */
13145 || token2
.type
== CPP_OPEN_BRACE
13146 || token2
.keyword
== RID_ATTRIBUTE
))
13147 cp_parser_namespace_definition (parser
);
13148 /* An inline (associated) namespace definition. */
13149 else if (token1
.keyword
== RID_INLINE
13150 && token2
.keyword
== RID_NAMESPACE
)
13151 cp_parser_namespace_definition (parser
);
13152 /* Objective-C++ declaration/definition. */
13153 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
13154 cp_parser_objc_declaration (parser
, NULL_TREE
);
13155 else if (c_dialect_objc ()
13156 && token1
.keyword
== RID_ATTRIBUTE
13157 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
13158 cp_parser_objc_declaration (parser
, attributes
);
13159 /* At this point we may have a template declared by a concept
13161 else if (flag_concepts
13162 && cp_parser_template_declaration_after_export (parser
,
13163 /*member_p=*/false))
13166 /* Try to parse a block-declaration, or a function-definition. */
13167 cp_parser_block_declaration (parser
, /*statement_p=*/false);
13169 /* Free any declarators allocated. */
13170 obstack_free (&declarator_obstack
, p
);
13173 /* Parse a namespace-scope declaration. */
13176 cp_parser_toplevel_declaration (cp_parser
* parser
)
13178 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13180 if (token
->type
== CPP_PRAGMA
)
13181 /* A top-level declaration can consist solely of a #pragma. A
13182 nested declaration cannot, so this is done here and not in
13183 cp_parser_declaration. (A #pragma at block scope is
13184 handled in cp_parser_statement.) */
13185 cp_parser_pragma (parser
, pragma_external
, NULL
);
13186 else if (token
->type
== CPP_SEMICOLON
)
13188 /* A declaration consisting of a single semicolon is
13189 invalid. Allow it unless we're being pedantic. */
13190 cp_lexer_consume_token (parser
->lexer
);
13191 if (!in_system_header_at (input_location
))
13192 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
13195 /* Parse the declaration itself. */
13196 cp_parser_declaration (parser
);
13199 /* Parse a block-declaration.
13204 namespace-alias-definition
13211 __extension__ block-declaration
13216 static_assert-declaration
13218 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13219 part of a declaration-statement. */
13222 cp_parser_block_declaration (cp_parser
*parser
,
13226 int saved_pedantic
;
13228 /* Check for the `__extension__' keyword. */
13229 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13231 /* Parse the qualified declaration. */
13232 cp_parser_block_declaration (parser
, statement_p
);
13233 /* Restore the PEDANTIC flag. */
13234 pedantic
= saved_pedantic
;
13239 /* Peek at the next token to figure out which kind of declaration is
13241 token1
= cp_lexer_peek_token (parser
->lexer
);
13243 /* If the next keyword is `asm', we have an asm-definition. */
13244 if (token1
->keyword
== RID_ASM
)
13247 cp_parser_commit_to_tentative_parse (parser
);
13248 cp_parser_asm_definition (parser
);
13250 /* If the next keyword is `namespace', we have a
13251 namespace-alias-definition. */
13252 else if (token1
->keyword
== RID_NAMESPACE
)
13253 cp_parser_namespace_alias_definition (parser
);
13254 /* If the next keyword is `using', we have a
13255 using-declaration, a using-directive, or an alias-declaration. */
13256 else if (token1
->keyword
== RID_USING
)
13261 cp_parser_commit_to_tentative_parse (parser
);
13262 /* If the token after `using' is `namespace', then we have a
13263 using-directive. */
13264 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13265 if (token2
->keyword
== RID_NAMESPACE
)
13266 cp_parser_using_directive (parser
);
13267 /* If the second token after 'using' is '=', then we have an
13268 alias-declaration. */
13269 else if (cxx_dialect
>= cxx11
13270 && token2
->type
== CPP_NAME
13271 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13272 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13273 cp_parser_alias_declaration (parser
);
13274 /* Otherwise, it's a using-declaration. */
13276 cp_parser_using_declaration (parser
,
13277 /*access_declaration_p=*/false);
13279 /* If the next keyword is `__label__' we have a misplaced label
13281 else if (token1
->keyword
== RID_LABEL
)
13283 cp_lexer_consume_token (parser
->lexer
);
13284 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13285 cp_parser_skip_to_end_of_statement (parser
);
13286 /* If the next token is now a `;', consume it. */
13287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13288 cp_lexer_consume_token (parser
->lexer
);
13290 /* If the next token is `static_assert' we have a static assertion. */
13291 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13292 cp_parser_static_assert (parser
, /*member_p=*/false);
13293 /* Anything else must be a simple-declaration. */
13295 cp_parser_simple_declaration (parser
, !statement_p
,
13296 /*maybe_range_for_decl*/NULL
);
13299 /* Parse a simple-declaration.
13301 simple-declaration:
13302 decl-specifier-seq [opt] init-declarator-list [opt] ;
13303 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13304 brace-or-equal-initializer ;
13306 init-declarator-list:
13308 init-declarator-list , init-declarator
13310 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13311 function-definition as a simple-declaration.
13313 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13314 parsed declaration if it is an uninitialized single declarator not followed
13315 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13316 if present, will not be consumed. */
13319 cp_parser_simple_declaration (cp_parser
* parser
,
13320 bool function_definition_allowed_p
,
13321 tree
*maybe_range_for_decl
)
13323 cp_decl_specifier_seq decl_specifiers
;
13324 int declares_class_or_enum
;
13325 bool saw_declarator
;
13326 location_t comma_loc
= UNKNOWN_LOCATION
;
13327 location_t init_loc
= UNKNOWN_LOCATION
;
13329 if (maybe_range_for_decl
)
13330 *maybe_range_for_decl
= NULL_TREE
;
13332 /* Defer access checks until we know what is being declared; the
13333 checks for names appearing in the decl-specifier-seq should be
13334 done as if we were in the scope of the thing being declared. */
13335 push_deferring_access_checks (dk_deferred
);
13337 /* Parse the decl-specifier-seq. We have to keep track of whether
13338 or not the decl-specifier-seq declares a named class or
13339 enumeration type, since that is the only case in which the
13340 init-declarator-list is allowed to be empty.
13344 In a simple-declaration, the optional init-declarator-list can be
13345 omitted only when declaring a class or enumeration, that is when
13346 the decl-specifier-seq contains either a class-specifier, an
13347 elaborated-type-specifier, or an enum-specifier. */
13348 cp_parser_decl_specifier_seq (parser
,
13349 CP_PARSER_FLAGS_OPTIONAL
,
13351 &declares_class_or_enum
);
13352 /* We no longer need to defer access checks. */
13353 stop_deferring_access_checks ();
13355 /* In a block scope, a valid declaration must always have a
13356 decl-specifier-seq. By not trying to parse declarators, we can
13357 resolve the declaration/expression ambiguity more quickly. */
13358 if (!function_definition_allowed_p
13359 && !decl_specifiers
.any_specifiers_p
)
13361 cp_parser_error (parser
, "expected declaration");
13365 /* If the next two tokens are both identifiers, the code is
13366 erroneous. The usual cause of this situation is code like:
13370 where "T" should name a type -- but does not. */
13371 if (!decl_specifiers
.any_type_specifiers_p
13372 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13374 /* If parsing tentatively, we should commit; we really are
13375 looking at a declaration. */
13376 cp_parser_commit_to_tentative_parse (parser
);
13381 cp_parser_maybe_commit_to_declaration (parser
,
13382 decl_specifiers
.any_specifiers_p
);
13384 /* Look for C++17 decomposition declaration. */
13385 for (size_t n
= 1; ; n
++)
13386 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13387 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13389 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13390 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13391 && decl_specifiers
.any_specifiers_p
)
13394 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13395 maybe_range_for_decl
,
13398 /* The next token should be either a `,' or a `;'. */
13399 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13400 /* If it's a `;', we are done. */
13401 if (token
->type
== CPP_SEMICOLON
)
13403 else if (maybe_range_for_decl
)
13405 if (*maybe_range_for_decl
== NULL_TREE
)
13406 *maybe_range_for_decl
= error_mark_node
;
13409 /* Anything else is an error. */
13412 /* If we have already issued an error message we don't need
13413 to issue another one. */
13414 if ((decl
!= error_mark_node
13415 && DECL_INITIAL (decl
) != error_mark_node
)
13416 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13417 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13418 /* Skip tokens until we reach the end of the statement. */
13419 cp_parser_skip_to_end_of_statement (parser
);
13420 /* If the next token is now a `;', consume it. */
13421 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13422 cp_lexer_consume_token (parser
->lexer
);
13430 bool auto_specifier_p
;
13431 /* NULL_TREE if both variable and function declaration are allowed,
13432 error_mark_node if function declaration are not allowed and
13433 a FUNCTION_DECL that should be diagnosed if it is followed by
13434 variable declarations. */
13435 tree auto_function_declaration
;
13437 last_type
= NULL_TREE
;
13439 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13440 auto_function_declaration
= NULL_TREE
;
13442 /* Keep going until we hit the `;' at the end of the simple
13444 saw_declarator
= false;
13445 while (cp_lexer_next_token_is_not (parser
->lexer
,
13449 bool function_definition_p
;
13451 tree auto_result
= NULL_TREE
;
13453 if (saw_declarator
)
13455 /* If we are processing next declarator, comma is expected */
13456 token
= cp_lexer_peek_token (parser
->lexer
);
13457 gcc_assert (token
->type
== CPP_COMMA
);
13458 cp_lexer_consume_token (parser
->lexer
);
13459 if (maybe_range_for_decl
)
13461 *maybe_range_for_decl
= error_mark_node
;
13462 if (comma_loc
== UNKNOWN_LOCATION
)
13463 comma_loc
= token
->location
;
13467 saw_declarator
= true;
13469 /* Parse the init-declarator. */
13470 decl
= cp_parser_init_declarator (parser
,
13471 CP_PARSER_FLAGS_NONE
,
13474 function_definition_allowed_p
,
13475 /*member_p=*/false,
13476 declares_class_or_enum
,
13477 &function_definition_p
,
13478 maybe_range_for_decl
,
13481 /* If an error occurred while parsing tentatively, exit quickly.
13482 (That usually happens when in the body of a function; each
13483 statement is treated as a declaration-statement until proven
13485 if (cp_parser_error_occurred (parser
))
13488 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13490 /* If the init-declarator-list contains more than one
13491 init-declarator, they shall all form declarations of
13493 if (auto_function_declaration
== NULL_TREE
)
13494 auto_function_declaration
13495 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13496 else if (TREE_CODE (decl
) == FUNCTION_DECL
13497 || auto_function_declaration
!= error_mark_node
)
13499 error_at (decl_specifiers
.locations
[ds_type_spec
],
13500 "non-variable %qD in declaration with more than one "
13501 "declarator with placeholder type",
13502 TREE_CODE (decl
) == FUNCTION_DECL
13503 ? decl
: auto_function_declaration
);
13504 auto_function_declaration
= error_mark_node
;
13509 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13512 && last_type
!= error_mark_node
13513 && !same_type_p (auto_result
, last_type
))
13515 /* If the list of declarators contains more than one declarator,
13516 the type of each declared variable is determined as described
13517 above. If the type deduced for the template parameter U is not
13518 the same in each deduction, the program is ill-formed. */
13519 error_at (decl_specifiers
.locations
[ds_type_spec
],
13520 "inconsistent deduction for %qT: %qT and then %qT",
13521 decl_specifiers
.type
, last_type
, auto_result
);
13522 last_type
= error_mark_node
;
13525 last_type
= auto_result
;
13528 /* Handle function definitions specially. */
13529 if (function_definition_p
)
13531 /* If the next token is a `,', then we are probably
13532 processing something like:
13536 which is erroneous. */
13537 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13539 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13540 error_at (token
->location
,
13542 " declarations and function-definitions is forbidden");
13544 /* Otherwise, we're done with the list of declarators. */
13547 pop_deferring_access_checks ();
13551 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13552 *maybe_range_for_decl
= decl
;
13553 /* The next token should be either a `,' or a `;'. */
13554 token
= cp_lexer_peek_token (parser
->lexer
);
13555 /* If it's a `,', there are more declarators to come. */
13556 if (token
->type
== CPP_COMMA
)
13557 /* will be consumed next time around */;
13558 /* If it's a `;', we are done. */
13559 else if (token
->type
== CPP_SEMICOLON
)
13561 else if (maybe_range_for_decl
)
13563 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13564 permerror (decl_specifiers
.locations
[ds_type_spec
],
13565 "types may not be defined in a for-range-declaration");
13568 /* Anything else is an error. */
13571 /* If we have already issued an error message we don't need
13572 to issue another one. */
13573 if ((decl
!= error_mark_node
13574 && DECL_INITIAL (decl
) != error_mark_node
)
13575 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13576 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13577 /* Skip tokens until we reach the end of the statement. */
13578 cp_parser_skip_to_end_of_statement (parser
);
13579 /* If the next token is now a `;', consume it. */
13580 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13581 cp_lexer_consume_token (parser
->lexer
);
13584 /* After the first time around, a function-definition is not
13585 allowed -- even if it was OK at first. For example:
13590 function_definition_allowed_p
= false;
13593 /* Issue an error message if no declarators are present, and the
13594 decl-specifier-seq does not itself declare a class or
13595 enumeration: [dcl.dcl]/3. */
13596 if (!saw_declarator
)
13598 if (cp_parser_declares_only_class_p (parser
))
13600 if (!declares_class_or_enum
13601 && decl_specifiers
.type
13602 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13603 /* Ensure an error is issued anyway when finish_decltype_type,
13604 called via cp_parser_decl_specifier_seq, returns a class or
13605 an enumeration (c++/51786). */
13606 decl_specifiers
.type
= NULL_TREE
;
13607 shadow_tag (&decl_specifiers
);
13609 /* Perform any deferred access checks. */
13610 perform_deferred_access_checks (tf_warning_or_error
);
13613 /* Consume the `;'. */
13615 if (!maybe_range_for_decl
)
13616 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13617 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13619 if (init_loc
!= UNKNOWN_LOCATION
)
13620 error_at (init_loc
, "initializer in range-based %<for%> loop");
13621 if (comma_loc
!= UNKNOWN_LOCATION
)
13622 error_at (comma_loc
,
13623 "multiple declarations in range-based %<for%> loop");
13627 pop_deferring_access_checks ();
13630 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13631 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13635 cp_parser_decomposition_declaration (cp_parser
*parser
,
13636 cp_decl_specifier_seq
*decl_specifiers
,
13637 tree
*maybe_range_for_decl
,
13638 location_t
*init_loc
)
13640 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13641 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13642 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13644 /* Parse the identifier-list. */
13645 auto_vec
<cp_expr
, 10> v
;
13646 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13649 cp_expr e
= cp_parser_identifier (parser
);
13650 if (e
.get_value () == error_mark_node
)
13653 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13655 cp_lexer_consume_token (parser
->lexer
);
13658 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13659 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13661 end_loc
= UNKNOWN_LOCATION
;
13662 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13664 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13665 cp_lexer_consume_token (parser
->lexer
);
13668 cp_parser_skip_to_end_of_statement (parser
);
13669 return error_mark_node
;
13673 if (cxx_dialect
< cxx17
)
13674 pedwarn (loc
, 0, "structured bindings only available with "
13675 "-std=c++17 or -std=gnu++17");
13678 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13679 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13680 declarator
->id_loc
= loc
;
13681 if (ref_qual
!= REF_QUAL_NONE
)
13682 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13683 ref_qual
== REF_QUAL_RVALUE
,
13685 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13686 NULL_TREE
, decl_specifiers
->attributes
,
13688 tree orig_decl
= decl
;
13692 cp_decl_specifier_seq decl_specs
;
13693 clear_decl_specs (&decl_specs
);
13694 decl_specs
.type
= make_auto ();
13696 FOR_EACH_VEC_ELT (v
, i
, e
)
13699 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
13700 sfk_none
, e
.get_location ());
13703 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13704 declarator
->id_loc
= e
.get_location ();
13706 tree elt_pushed_scope
;
13707 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13708 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13709 if (decl2
== error_mark_node
)
13710 decl
= error_mark_node
;
13711 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13713 /* Ensure we've diagnosed redeclaration if we aren't creating
13715 gcc_assert (errorcount
);
13716 decl
= error_mark_node
;
13720 if (elt_pushed_scope
)
13721 pop_scope (elt_pushed_scope
);
13726 error_at (loc
, "empty structured binding declaration");
13727 decl
= error_mark_node
;
13730 if (maybe_range_for_decl
== NULL
13731 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13733 bool non_constant_p
= false, is_direct_init
= false;
13734 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13735 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13737 if (initializer
== NULL_TREE
13738 || (TREE_CODE (initializer
) == TREE_LIST
13739 && TREE_CHAIN (initializer
))
13741 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13742 && CONSTRUCTOR_NELTS (initializer
) != 1))
13744 error_at (loc
, "invalid initializer for structured binding "
13746 initializer
= error_mark_node
;
13749 if (decl
!= error_mark_node
)
13751 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13752 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13753 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13754 cp_finish_decomp (decl
, prev
, v
.length ());
13757 else if (decl
!= error_mark_node
)
13759 *maybe_range_for_decl
= prev
;
13760 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13761 the underlying DECL. */
13762 cp_finish_decomp (decl
, prev
, v
.length ());
13766 pop_scope (pushed_scope
);
13768 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13770 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13771 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13777 /* Parse a decl-specifier-seq.
13779 decl-specifier-seq:
13780 decl-specifier-seq [opt] decl-specifier
13781 decl-specifier attribute-specifier-seq [opt] (C++11)
13784 storage-class-specifier
13795 Concepts Extension:
13800 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13802 The parser flags FLAGS is used to control type-specifier parsing.
13804 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13807 1: one of the decl-specifiers is an elaborated-type-specifier
13808 (i.e., a type declaration)
13809 2: one of the decl-specifiers is an enum-specifier or a
13810 class-specifier (i.e., a type definition)
13815 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13816 cp_parser_flags flags
,
13817 cp_decl_specifier_seq
*decl_specs
,
13818 int* declares_class_or_enum
)
13820 bool constructor_possible_p
= !parser
->in_declarator_p
;
13821 bool found_decl_spec
= false;
13822 cp_token
*start_token
= NULL
;
13825 /* Clear DECL_SPECS. */
13826 clear_decl_specs (decl_specs
);
13828 /* Assume no class or enumeration type is declared. */
13829 *declares_class_or_enum
= 0;
13831 /* Keep reading specifiers until there are no more to read. */
13834 bool constructor_p
;
13838 /* Peek at the next token. */
13839 token
= cp_lexer_peek_token (parser
->lexer
);
13841 /* Save the first token of the decl spec list for error
13844 start_token
= token
;
13845 /* Handle attributes. */
13846 if (cp_next_tokens_can_be_attribute_p (parser
))
13848 /* Parse the attributes. */
13849 tree attrs
= cp_parser_attributes_opt (parser
);
13851 /* In a sequence of declaration specifiers, c++11 attributes
13852 appertain to the type that precede them. In that case
13855 The attribute-specifier-seq affects the type only for
13856 the declaration it appears in, not other declarations
13857 involving the same type.
13859 But for now let's force the user to position the
13860 attribute either at the beginning of the declaration or
13861 after the declarator-id, which would clearly mean that it
13862 applies to the declarator. */
13863 if (cxx11_attribute_p (attrs
))
13865 if (!found_decl_spec
)
13866 /* The c++11 attribute is at the beginning of the
13867 declaration. It appertains to the entity being
13871 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13873 /* This is an attribute following a
13874 class-specifier. */
13875 if (decl_specs
->type_definition_p
)
13876 warn_misplaced_attr_for_class_type (token
->location
,
13882 decl_specs
->std_attributes
13883 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13884 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13885 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13891 decl_specs
->attributes
13892 = attr_chainon (decl_specs
->attributes
, attrs
);
13893 if (decl_specs
->locations
[ds_attribute
] == 0)
13894 decl_specs
->locations
[ds_attribute
] = token
->location
;
13897 /* Assume we will find a decl-specifier keyword. */
13898 found_decl_spec
= true;
13899 /* If the next token is an appropriate keyword, we can simply
13900 add it to the list. */
13901 switch (token
->keyword
)
13907 if (!at_class_scope_p ())
13909 gcc_rich_location
richloc (token
->location
);
13910 richloc
.add_fixit_remove ();
13911 error_at (&richloc
, "%<friend%> used outside of class");
13912 cp_lexer_purge_token (parser
->lexer
);
13917 /* Consume the token. */
13918 cp_lexer_consume_token (parser
->lexer
);
13922 case RID_CONSTEXPR
:
13924 cp_lexer_consume_token (parser
->lexer
);
13929 cp_lexer_consume_token (parser
->lexer
);
13932 /* function-specifier:
13939 cp_parser_function_specifier_opt (parser
, decl_specs
);
13946 /* Consume the token. */
13947 cp_lexer_consume_token (parser
->lexer
);
13948 /* A constructor declarator cannot appear in a typedef. */
13949 constructor_possible_p
= false;
13950 /* The "typedef" keyword can only occur in a declaration; we
13951 may as well commit at this point. */
13952 cp_parser_commit_to_tentative_parse (parser
);
13954 if (decl_specs
->storage_class
!= sc_none
)
13955 decl_specs
->conflicting_specifiers_p
= true;
13958 /* storage-class-specifier:
13968 if (cxx_dialect
== cxx98
)
13970 /* Consume the token. */
13971 cp_lexer_consume_token (parser
->lexer
);
13973 /* Complain about `auto' as a storage specifier, if
13974 we're complaining about C++0x compatibility. */
13975 gcc_rich_location
richloc (token
->location
);
13976 richloc
.add_fixit_remove ();
13977 warning_at (&richloc
, OPT_Wc__11_compat
,
13978 "%<auto%> changes meaning in C++11; "
13979 "please remove it");
13981 /* Set the storage class anyway. */
13982 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13986 /* C++0x auto type-specifier. */
13987 found_decl_spec
= false;
13994 /* Consume the token. */
13995 cp_lexer_consume_token (parser
->lexer
);
13996 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
14000 /* Consume the token. */
14002 cp_lexer_consume_token (parser
->lexer
);
14006 /* We did not yet find a decl-specifier yet. */
14007 found_decl_spec
= false;
14011 if (found_decl_spec
14012 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
14013 && token
->keyword
!= RID_CONSTEXPR
)
14014 error ("decl-specifier invalid in condition");
14016 if (found_decl_spec
14017 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14018 && token
->keyword
!= RID_MUTABLE
14019 && token
->keyword
!= RID_CONSTEXPR
)
14020 error_at (token
->location
, "%qD invalid in lambda",
14021 ridpointers
[token
->keyword
]);
14024 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14026 /* Constructors are a special case. The `S' in `S()' is not a
14027 decl-specifier; it is the beginning of the declarator. */
14029 = (!found_decl_spec
14030 && constructor_possible_p
14031 && (cp_parser_constructor_declarator_p
14032 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
14034 /* If we don't have a DECL_SPEC yet, then we must be looking at
14035 a type-specifier. */
14036 if (!found_decl_spec
&& !constructor_p
)
14038 int decl_spec_declares_class_or_enum
;
14039 bool is_cv_qualifier
;
14043 = cp_parser_type_specifier (parser
, flags
,
14045 /*is_declaration=*/true,
14046 &decl_spec_declares_class_or_enum
,
14048 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
14050 /* If this type-specifier referenced a user-defined type
14051 (a typedef, class-name, etc.), then we can't allow any
14052 more such type-specifiers henceforth.
14056 The longest sequence of decl-specifiers that could
14057 possibly be a type name is taken as the
14058 decl-specifier-seq of a declaration. The sequence shall
14059 be self-consistent as described below.
14063 As a general rule, at most one type-specifier is allowed
14064 in the complete decl-specifier-seq of a declaration. The
14065 only exceptions are the following:
14067 -- const or volatile can be combined with any other
14070 -- signed or unsigned can be combined with char, long,
14078 void g (const int Pc);
14080 Here, Pc is *not* part of the decl-specifier seq; it's
14081 the declarator. Therefore, once we see a type-specifier
14082 (other than a cv-qualifier), we forbid any additional
14083 user-defined types. We *do* still allow things like `int
14084 int' to be considered a decl-specifier-seq, and issue the
14085 error message later. */
14086 if (type_spec
&& !is_cv_qualifier
)
14087 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
14088 /* A constructor declarator cannot follow a type-specifier. */
14091 constructor_possible_p
= false;
14092 found_decl_spec
= true;
14093 if (!is_cv_qualifier
)
14094 decl_specs
->any_type_specifiers_p
= true;
14096 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
14097 error_at (token
->location
, "type-specifier invalid in lambda");
14101 /* If we still do not have a DECL_SPEC, then there are no more
14102 decl-specifiers. */
14103 if (!found_decl_spec
)
14106 decl_specs
->any_specifiers_p
= true;
14107 /* After we see one decl-specifier, further decl-specifiers are
14108 always optional. */
14109 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
14112 /* Don't allow a friend specifier with a class definition. */
14113 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
14114 && (*declares_class_or_enum
& 2))
14115 error_at (decl_specs
->locations
[ds_friend
],
14116 "class definition may not be declared a friend");
14119 /* Parse an (optional) storage-class-specifier.
14121 storage-class-specifier:
14130 storage-class-specifier:
14133 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14136 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
14138 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
14141 if (cxx_dialect
!= cxx98
)
14143 /* Fall through for C++98. */
14144 gcc_fallthrough ();
14151 /* Consume the token. */
14152 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14159 /* Parse an (optional) function-specifier.
14161 function-specifier:
14167 explicit(constant-expression)
14169 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14170 Updates DECL_SPECS, if it is non-NULL. */
14173 cp_parser_function_specifier_opt (cp_parser
* parser
,
14174 cp_decl_specifier_seq
*decl_specs
)
14176 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14177 switch (token
->keyword
)
14180 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
14184 /* 14.5.2.3 [temp.mem]
14186 A member function template shall not be virtual. */
14187 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14188 && current_class_type
)
14189 error_at (token
->location
, "templates may not be %<virtual%>");
14191 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
14196 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14197 /* If we see '(', it's C++20 explicit(bool). */
14199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14201 matching_parens parens
;
14202 parens
.consume_open (parser
);
14204 /* New types are not allowed in an explicit-specifier. */
14205 const char *saved_message
14206 = parser
->type_definition_forbidden_message
;
14207 parser
->type_definition_forbidden_message
14208 = G_("types may not be defined in explicit-specifier");
14210 if (cxx_dialect
< cxx2a
)
14211 pedwarn (token
->location
, 0,
14212 "%<explicit(bool)%> only available with -std=c++2a "
14213 "or -std=gnu++2a");
14215 /* Parse the constant-expression. */
14216 expr
= cp_parser_constant_expression (parser
);
14218 /* Restore the saved message. */
14219 parser
->type_definition_forbidden_message
= saved_message
;
14220 parens
.require_close (parser
);
14223 /* The explicit-specifier explicit without a constant-expression is
14224 equivalent to the explicit-specifier explicit(true). */
14225 expr
= boolean_true_node
;
14228 "the constant-expression, if supplied, shall be a contextually
14229 converted constant expression of type bool." */
14230 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14231 /* We could evaluate it -- mark the decl as appropriate. */
14232 if (expr
== boolean_true_node
)
14233 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14234 else if (expr
== boolean_false_node
)
14235 /* Don't mark the decl as explicit. */;
14236 else if (decl_specs
)
14237 /* The expression was value-dependent. Remember it so that we can
14238 substitute it later. */
14239 decl_specs
->explicit_specifier
= expr
;
14247 /* Consume the token. */
14248 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14251 /* Parse a linkage-specification.
14253 linkage-specification:
14254 extern string-literal { declaration-seq [opt] }
14255 extern string-literal declaration */
14258 cp_parser_linkage_specification (cp_parser
* parser
)
14262 /* Look for the `extern' keyword. */
14263 cp_token
*extern_token
14264 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14266 /* Look for the string-literal. */
14267 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14268 linkage
= cp_parser_string_literal (parser
, false, false);
14270 /* Transform the literal into an identifier. If the literal is a
14271 wide-character string, or contains embedded NULs, then we can't
14272 handle it as the user wants. */
14273 if (strlen (TREE_STRING_POINTER (linkage
))
14274 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14276 cp_parser_error (parser
, "invalid linkage-specification");
14277 /* Assume C++ linkage. */
14278 linkage
= lang_name_cplusplus
;
14281 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14283 /* We're now using the new linkage. */
14284 push_lang_context (linkage
);
14286 /* Preserve the location of the the innermost linkage specification,
14287 tracking the locations of nested specifications via a local. */
14288 location_t saved_location
14289 = parser
->innermost_linkage_specification_location
;
14290 /* Construct a location ranging from the start of the "extern" to
14291 the end of the string-literal, with the caret at the start, e.g.:
14295 parser
->innermost_linkage_specification_location
14296 = make_location (extern_token
->location
,
14297 extern_token
->location
,
14298 get_finish (string_token
->location
));
14300 /* If the next token is a `{', then we're using the first
14302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14304 cp_ensure_no_omp_declare_simd (parser
);
14305 cp_ensure_no_oacc_routine (parser
);
14307 /* Consume the `{' token. */
14308 matching_braces braces
;
14309 braces
.consume_open (parser
);
14310 /* Parse the declarations. */
14311 cp_parser_declaration_seq_opt (parser
);
14312 /* Look for the closing `}'. */
14313 braces
.require_close (parser
);
14315 /* Otherwise, there's just one declaration. */
14318 bool saved_in_unbraced_linkage_specification_p
;
14320 saved_in_unbraced_linkage_specification_p
14321 = parser
->in_unbraced_linkage_specification_p
;
14322 parser
->in_unbraced_linkage_specification_p
= true;
14323 cp_parser_declaration (parser
);
14324 parser
->in_unbraced_linkage_specification_p
14325 = saved_in_unbraced_linkage_specification_p
;
14328 /* We're done with the linkage-specification. */
14329 pop_lang_context ();
14331 /* Restore location of parent linkage specification, if any. */
14332 parser
->innermost_linkage_specification_location
= saved_location
;
14335 /* Parse a static_assert-declaration.
14337 static_assert-declaration:
14338 static_assert ( constant-expression , string-literal ) ;
14339 static_assert ( constant-expression ) ; (C++17)
14341 If MEMBER_P, this static_assert is a class member. */
14344 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14347 location_t token_loc
;
14351 /* Peek at the `static_assert' token so we can keep track of exactly
14352 where the static assertion started. */
14353 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14355 /* Look for the `static_assert' keyword. */
14356 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14360 /* We know we are in a static assertion; commit to any tentative
14362 if (cp_parser_parsing_tentatively (parser
))
14363 cp_parser_commit_to_tentative_parse (parser
);
14365 /* Parse the `(' starting the static assertion condition. */
14366 matching_parens parens
;
14367 parens
.require_open (parser
);
14369 /* Parse the constant-expression. Allow a non-constant expression
14370 here in order to give better diagnostics in finish_static_assert. */
14372 cp_parser_constant_expression (parser
,
14373 /*allow_non_constant_p=*/true,
14374 /*non_constant_p=*/&dummy
);
14376 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14378 if (cxx_dialect
< cxx17
)
14379 pedwarn (input_location
, OPT_Wpedantic
,
14380 "static_assert without a message "
14381 "only available with -std=c++17 or -std=gnu++17");
14383 cp_lexer_consume_token (parser
->lexer
);
14384 message
= build_string (1, "");
14385 TREE_TYPE (message
) = char_array_type_node
;
14386 fix_string_type (message
);
14390 /* Parse the separating `,'. */
14391 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14393 /* Parse the string-literal message. */
14394 message
= cp_parser_string_literal (parser
,
14395 /*translate=*/false,
14398 /* A `)' completes the static assertion. */
14399 if (!parens
.require_close (parser
))
14400 cp_parser_skip_to_closing_parenthesis (parser
,
14401 /*recovering=*/true,
14402 /*or_comma=*/false,
14403 /*consume_paren=*/true);
14406 /* A semicolon terminates the declaration. */
14407 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14409 /* Get the location for the static assertion. Use that of the
14410 condition if available, otherwise, use that of the "static_assert"
14412 location_t assert_loc
= condition
.get_location ();
14413 if (assert_loc
== UNKNOWN_LOCATION
)
14414 assert_loc
= token_loc
;
14416 /* Complete the static assertion, which may mean either processing
14417 the static assert now or saving it for template instantiation. */
14418 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14421 /* Parse the expression in decltype ( expression ). */
14424 cp_parser_decltype_expr (cp_parser
*parser
,
14425 bool &id_expression_or_member_access_p
)
14427 cp_token
*id_expr_start_token
;
14430 /* Since we're going to preserve any side-effects from this parse, set up a
14431 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14432 in the expression. */
14433 tentative_firewall
firewall (parser
);
14435 /* First, try parsing an id-expression. */
14436 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14437 cp_parser_parse_tentatively (parser
);
14438 expr
= cp_parser_id_expression (parser
,
14439 /*template_keyword_p=*/false,
14440 /*check_dependency_p=*/true,
14441 /*template_p=*/NULL
,
14442 /*declarator_p=*/false,
14443 /*optional_p=*/false);
14445 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14447 bool non_integral_constant_expression_p
= false;
14448 tree id_expression
= expr
;
14450 const char *error_msg
;
14452 if (identifier_p (expr
))
14453 /* Lookup the name we got back from the id-expression. */
14454 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14455 id_expr_start_token
->location
);
14457 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14458 /* A template without args is not a complete id-expression. */
14459 expr
= error_mark_node
;
14462 && expr
!= error_mark_node
14463 && TREE_CODE (expr
) != TYPE_DECL
14464 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14465 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14466 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14468 /* Complete lookup of the id-expression. */
14469 expr
= (finish_id_expression
14470 (id_expression
, expr
, parser
->scope
, &idk
,
14471 /*integral_constant_expression_p=*/false,
14472 /*allow_non_integral_constant_expression_p=*/true,
14473 &non_integral_constant_expression_p
,
14474 /*template_p=*/false,
14476 /*address_p=*/false,
14477 /*template_arg_p=*/false,
14479 id_expr_start_token
->location
));
14481 if (expr
== error_mark_node
)
14482 /* We found an id-expression, but it was something that we
14483 should not have found. This is an error, not something
14484 we can recover from, so note that we found an
14485 id-expression and we'll recover as gracefully as
14487 id_expression_or_member_access_p
= true;
14491 && expr
!= error_mark_node
14492 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14493 /* We have an id-expression. */
14494 id_expression_or_member_access_p
= true;
14497 if (!id_expression_or_member_access_p
)
14499 /* Abort the id-expression parse. */
14500 cp_parser_abort_tentative_parse (parser
);
14502 /* Parsing tentatively, again. */
14503 cp_parser_parse_tentatively (parser
);
14505 /* Parse a class member access. */
14506 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14507 /*cast_p=*/false, /*decltype*/true,
14508 /*member_access_only_p=*/true, NULL
);
14511 && expr
!= error_mark_node
14512 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14513 /* We have an id-expression. */
14514 id_expression_or_member_access_p
= true;
14517 if (id_expression_or_member_access_p
)
14518 /* We have parsed the complete id-expression or member access. */
14519 cp_parser_parse_definitely (parser
);
14522 /* Abort our attempt to parse an id-expression or member access
14524 cp_parser_abort_tentative_parse (parser
);
14526 /* Commit to the tentative_firewall so we get syntax errors. */
14527 cp_parser_commit_to_tentative_parse (parser
);
14529 /* Parse a full expression. */
14530 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14531 /*decltype_p=*/true);
14537 /* Parse a `decltype' type. Returns the type.
14539 simple-type-specifier:
14540 decltype ( expression )
14542 decltype ( auto ) */
14545 cp_parser_decltype (cp_parser
*parser
)
14547 bool id_expression_or_member_access_p
= false;
14548 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14550 if (start_token
->type
== CPP_DECLTYPE
)
14552 /* Already parsed. */
14553 cp_lexer_consume_token (parser
->lexer
);
14554 return saved_checks_value (start_token
->u
.tree_check_value
);
14557 /* Look for the `decltype' token. */
14558 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14559 return error_mark_node
;
14561 /* Parse the opening `('. */
14562 matching_parens parens
;
14563 if (!parens
.require_open (parser
))
14564 return error_mark_node
;
14566 push_deferring_access_checks (dk_deferred
);
14568 tree expr
= NULL_TREE
;
14570 if (cxx_dialect
>= cxx14
14571 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14572 /* decltype (auto) */
14573 cp_lexer_consume_token (parser
->lexer
);
14576 /* decltype (expression) */
14578 /* Types cannot be defined in a `decltype' expression. Save away the
14579 old message and set the new one. */
14580 const char *saved_message
= parser
->type_definition_forbidden_message
;
14581 parser
->type_definition_forbidden_message
14582 = G_("types may not be defined in %<decltype%> expressions");
14584 /* The restrictions on constant-expressions do not apply inside
14585 decltype expressions. */
14586 bool saved_integral_constant_expression_p
14587 = parser
->integral_constant_expression_p
;
14588 bool saved_non_integral_constant_expression_p
14589 = parser
->non_integral_constant_expression_p
;
14590 parser
->integral_constant_expression_p
= false;
14592 /* Within a parenthesized expression, a `>' token is always
14593 the greater-than operator. */
14594 bool saved_greater_than_is_operator_p
14595 = parser
->greater_than_is_operator_p
;
14596 parser
->greater_than_is_operator_p
= true;
14598 /* Do not actually evaluate the expression. */
14599 ++cp_unevaluated_operand
;
14601 /* Do not warn about problems with the expression. */
14602 ++c_inhibit_evaluation_warnings
;
14604 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14605 STRIP_ANY_LOCATION_WRAPPER (expr
);
14607 /* Go back to evaluating expressions. */
14608 --cp_unevaluated_operand
;
14609 --c_inhibit_evaluation_warnings
;
14611 /* The `>' token might be the end of a template-id or
14612 template-parameter-list now. */
14613 parser
->greater_than_is_operator_p
14614 = saved_greater_than_is_operator_p
;
14616 /* Restore the old message and the integral constant expression
14618 parser
->type_definition_forbidden_message
= saved_message
;
14619 parser
->integral_constant_expression_p
14620 = saved_integral_constant_expression_p
;
14621 parser
->non_integral_constant_expression_p
14622 = saved_non_integral_constant_expression_p
;
14625 /* Parse to the closing `)'. */
14626 if (!parens
.require_close (parser
))
14628 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14629 /*consume_paren=*/true);
14630 pop_deferring_access_checks ();
14631 return error_mark_node
;
14637 expr
= make_decltype_auto ();
14638 AUTO_IS_DECLTYPE (expr
) = true;
14641 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14642 tf_warning_or_error
);
14644 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14646 start_token
->type
= CPP_DECLTYPE
;
14647 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14648 start_token
->u
.tree_check_value
->value
= expr
;
14649 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14650 start_token
->keyword
= RID_MAX
;
14651 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14653 pop_to_parent_deferring_access_checks ();
14658 /* Special member functions [gram.special] */
14660 /* Parse a conversion-function-id.
14662 conversion-function-id:
14663 operator conversion-type-id
14665 Returns an IDENTIFIER_NODE representing the operator. */
14668 cp_parser_conversion_function_id (cp_parser
* parser
)
14672 tree saved_qualifying_scope
;
14673 tree saved_object_scope
;
14674 tree pushed_scope
= NULL_TREE
;
14676 /* Look for the `operator' token. */
14677 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14678 return error_mark_node
;
14679 /* When we parse the conversion-type-id, the current scope will be
14680 reset. However, we need that information in able to look up the
14681 conversion function later, so we save it here. */
14682 saved_scope
= parser
->scope
;
14683 saved_qualifying_scope
= parser
->qualifying_scope
;
14684 saved_object_scope
= parser
->object_scope
;
14685 /* We must enter the scope of the class so that the names of
14686 entities declared within the class are available in the
14687 conversion-type-id. For example, consider:
14694 S::operator I() { ... }
14696 In order to see that `I' is a type-name in the definition, we
14697 must be in the scope of `S'. */
14699 pushed_scope
= push_scope (saved_scope
);
14700 /* Parse the conversion-type-id. */
14701 type
= cp_parser_conversion_type_id (parser
);
14702 /* Leave the scope of the class, if any. */
14704 pop_scope (pushed_scope
);
14705 /* Restore the saved scope. */
14706 parser
->scope
= saved_scope
;
14707 parser
->qualifying_scope
= saved_qualifying_scope
;
14708 parser
->object_scope
= saved_object_scope
;
14709 /* If the TYPE is invalid, indicate failure. */
14710 if (type
== error_mark_node
)
14711 return error_mark_node
;
14712 return make_conv_op_name (type
);
14715 /* Parse a conversion-type-id:
14717 conversion-type-id:
14718 type-specifier-seq conversion-declarator [opt]
14720 Returns the TYPE specified. */
14723 cp_parser_conversion_type_id (cp_parser
* parser
)
14726 cp_decl_specifier_seq type_specifiers
;
14727 cp_declarator
*declarator
;
14728 tree type_specified
;
14729 const char *saved_message
;
14731 /* Parse the attributes. */
14732 attributes
= cp_parser_attributes_opt (parser
);
14734 saved_message
= parser
->type_definition_forbidden_message
;
14735 parser
->type_definition_forbidden_message
14736 = G_("types may not be defined in a conversion-type-id");
14738 /* Parse the type-specifiers. */
14739 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
14740 /*is_declaration=*/false,
14741 /*is_trailing_return=*/false,
14744 parser
->type_definition_forbidden_message
= saved_message
;
14746 /* If that didn't work, stop. */
14747 if (type_specifiers
.type
== error_mark_node
)
14748 return error_mark_node
;
14749 /* Parse the conversion-declarator. */
14750 declarator
= cp_parser_conversion_declarator_opt (parser
);
14752 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14753 /*initialized=*/0, &attributes
);
14755 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14757 /* Don't give this error when parsing tentatively. This happens to
14758 work because we always parse this definitively once. */
14759 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14760 && type_uses_auto (type_specified
))
14762 if (cxx_dialect
< cxx14
)
14764 error ("invalid use of %<auto%> in conversion operator");
14765 return error_mark_node
;
14767 else if (template_parm_scope_p ())
14768 warning (0, "use of %<auto%> in member template "
14769 "conversion operator can never be deduced");
14772 return type_specified
;
14775 /* Parse an (optional) conversion-declarator.
14777 conversion-declarator:
14778 ptr-operator conversion-declarator [opt]
14782 static cp_declarator
*
14783 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14785 enum tree_code code
;
14786 tree class_type
, std_attributes
= NULL_TREE
;
14787 cp_cv_quals cv_quals
;
14789 /* We don't know if there's a ptr-operator next, or not. */
14790 cp_parser_parse_tentatively (parser
);
14791 /* Try the ptr-operator. */
14792 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14794 /* If it worked, look for more conversion-declarators. */
14795 if (cp_parser_parse_definitely (parser
))
14797 cp_declarator
*declarator
;
14799 /* Parse another optional declarator. */
14800 declarator
= cp_parser_conversion_declarator_opt (parser
);
14802 declarator
= cp_parser_make_indirect_declarator
14803 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14811 /* Parse an (optional) ctor-initializer.
14814 : mem-initializer-list */
14817 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14819 /* If the next token is not a `:', then there is no
14820 ctor-initializer. */
14821 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14823 /* Do default initialization of any bases and members. */
14824 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14825 finish_mem_initializers (NULL_TREE
);
14829 /* Consume the `:' token. */
14830 cp_lexer_consume_token (parser
->lexer
);
14831 /* And the mem-initializer-list. */
14832 cp_parser_mem_initializer_list (parser
);
14835 /* Parse a mem-initializer-list.
14837 mem-initializer-list:
14838 mem-initializer ... [opt]
14839 mem-initializer ... [opt] , mem-initializer-list */
14842 cp_parser_mem_initializer_list (cp_parser
* parser
)
14844 tree mem_initializer_list
= NULL_TREE
;
14845 tree target_ctor
= error_mark_node
;
14846 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14848 /* Let the semantic analysis code know that we are starting the
14849 mem-initializer-list. */
14850 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14851 error_at (token
->location
,
14852 "only constructors take member initializers");
14854 /* Loop through the list. */
14857 tree mem_initializer
;
14859 token
= cp_lexer_peek_token (parser
->lexer
);
14860 /* Parse the mem-initializer. */
14861 mem_initializer
= cp_parser_mem_initializer (parser
);
14862 /* If the next token is a `...', we're expanding member initializers. */
14863 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14865 || (mem_initializer
!= error_mark_node
14866 && check_for_bare_parameter_packs (TREE_PURPOSE
14867 (mem_initializer
))))
14869 /* Consume the `...'. */
14871 cp_lexer_consume_token (parser
->lexer
);
14873 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14874 can be expanded but members cannot. */
14875 if (mem_initializer
!= error_mark_node
14876 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14878 error_at (token
->location
,
14879 "cannot expand initializer for member %qD",
14880 TREE_PURPOSE (mem_initializer
));
14881 mem_initializer
= error_mark_node
;
14884 /* Construct the pack expansion type. */
14885 if (mem_initializer
!= error_mark_node
)
14886 mem_initializer
= make_pack_expansion (mem_initializer
);
14888 if (target_ctor
!= error_mark_node
14889 && mem_initializer
!= error_mark_node
)
14891 error ("mem-initializer for %qD follows constructor delegation",
14892 TREE_PURPOSE (mem_initializer
));
14893 mem_initializer
= error_mark_node
;
14895 /* Look for a target constructor. */
14896 if (mem_initializer
!= error_mark_node
14897 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14898 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14900 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14901 if (mem_initializer_list
)
14903 error ("constructor delegation follows mem-initializer for %qD",
14904 TREE_PURPOSE (mem_initializer_list
));
14905 mem_initializer
= error_mark_node
;
14907 target_ctor
= mem_initializer
;
14909 /* Add it to the list, unless it was erroneous. */
14910 if (mem_initializer
!= error_mark_node
)
14912 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14913 mem_initializer_list
= mem_initializer
;
14915 /* If the next token is not a `,', we're done. */
14916 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14918 /* Consume the `,' token. */
14919 cp_lexer_consume_token (parser
->lexer
);
14922 /* Perform semantic analysis. */
14923 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14924 finish_mem_initializers (mem_initializer_list
);
14927 /* Parse a mem-initializer.
14930 mem-initializer-id ( expression-list [opt] )
14931 mem-initializer-id braced-init-list
14936 ( expression-list [opt] )
14938 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14939 class) or FIELD_DECL (for a non-static data member) to initialize;
14940 the TREE_VALUE is the expression-list. An empty initialization
14941 list is represented by void_list_node. */
14944 cp_parser_mem_initializer (cp_parser
* parser
)
14946 tree mem_initializer_id
;
14947 tree expression_list
;
14949 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14951 /* Find out what is being initialized. */
14952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14954 permerror (token
->location
,
14955 "anachronistic old-style base class initializer");
14956 mem_initializer_id
= NULL_TREE
;
14960 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14961 if (mem_initializer_id
== error_mark_node
)
14962 return mem_initializer_id
;
14964 member
= expand_member_init (mem_initializer_id
);
14965 if (member
&& !DECL_P (member
))
14966 in_base_initializer
= 1;
14968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14970 bool expr_non_constant_p
;
14971 cp_lexer_set_source_position (parser
->lexer
);
14972 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14973 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14974 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14975 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14979 vec
<tree
, va_gc
> *vec
;
14980 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14982 /*allow_expansion_p=*/true,
14983 /*non_constant_p=*/NULL
,
14984 /*close_paren_loc=*/NULL
,
14985 /*wrap_locations_p=*/true);
14987 return error_mark_node
;
14988 expression_list
= build_tree_list_vec (vec
);
14989 release_tree_vector (vec
);
14992 if (expression_list
== error_mark_node
)
14993 return error_mark_node
;
14994 if (!expression_list
)
14995 expression_list
= void_type_node
;
14997 in_base_initializer
= 0;
14999 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
15002 /* Parse a mem-initializer-id.
15004 mem-initializer-id:
15005 :: [opt] nested-name-specifier [opt] class-name
15006 decltype-specifier (C++11)
15009 Returns a TYPE indicating the class to be initialized for the first
15010 production (and the second in C++11). Returns an IDENTIFIER_NODE
15011 indicating the data member to be initialized for the last production. */
15014 cp_parser_mem_initializer_id (cp_parser
* parser
)
15016 bool global_scope_p
;
15017 bool nested_name_specifier_p
;
15018 bool template_p
= false;
15021 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15023 /* `typename' is not allowed in this context ([temp.res]). */
15024 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
15026 error_at (token
->location
,
15027 "keyword %<typename%> not allowed in this context (a qualified "
15028 "member initializer is implicitly a type)");
15029 cp_lexer_consume_token (parser
->lexer
);
15031 /* Look for the optional `::' operator. */
15033 = (cp_parser_global_scope_opt (parser
,
15034 /*current_scope_valid_p=*/false)
15036 /* Look for the optional nested-name-specifier. The simplest way to
15041 The keyword `typename' is not permitted in a base-specifier or
15042 mem-initializer; in these contexts a qualified name that
15043 depends on a template-parameter is implicitly assumed to be a
15046 is to assume that we have seen the `typename' keyword at this
15048 nested_name_specifier_p
15049 = (cp_parser_nested_name_specifier_opt (parser
,
15050 /*typename_keyword_p=*/true,
15051 /*check_dependency_p=*/true,
15053 /*is_declaration=*/true)
15055 if (nested_name_specifier_p
)
15056 template_p
= cp_parser_optional_template_keyword (parser
);
15057 /* If there is a `::' operator or a nested-name-specifier, then we
15058 are definitely looking for a class-name. */
15059 if (global_scope_p
|| nested_name_specifier_p
)
15060 return cp_parser_class_name (parser
,
15061 /*typename_keyword_p=*/true,
15062 /*template_keyword_p=*/template_p
,
15064 /*check_dependency_p=*/true,
15065 /*class_head_p=*/false,
15066 /*is_declaration=*/true);
15067 /* Otherwise, we could also be looking for an ordinary identifier. */
15068 cp_parser_parse_tentatively (parser
);
15069 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
15070 /* Try a decltype-specifier. */
15071 id
= cp_parser_decltype (parser
);
15073 /* Otherwise, try a class-name. */
15074 id
= cp_parser_class_name (parser
,
15075 /*typename_keyword_p=*/true,
15076 /*template_keyword_p=*/false,
15078 /*check_dependency_p=*/true,
15079 /*class_head_p=*/false,
15080 /*is_declaration=*/true);
15081 /* If we found one, we're done. */
15082 if (cp_parser_parse_definitely (parser
))
15084 /* Otherwise, look for an ordinary identifier. */
15085 return cp_parser_identifier (parser
);
15088 /* Overloading [gram.over] */
15090 /* Parse an operator-function-id.
15092 operator-function-id:
15095 Returns an IDENTIFIER_NODE for the operator which is a
15096 human-readable spelling of the identifier, e.g., `operator +'. */
15099 cp_parser_operator_function_id (cp_parser
* parser
)
15101 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15102 /* Look for the `operator' keyword. */
15103 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15104 return error_mark_node
;
15105 /* And then the name of the operator itself. */
15106 return cp_parser_operator (parser
, start_loc
);
15109 /* Return an identifier node for a user-defined literal operator.
15110 The suffix identifier is chained to the operator name identifier. */
15113 cp_literal_operator_id (const char* name
)
15116 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
15117 + strlen (name
) + 10);
15118 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
15119 identifier
= get_identifier (buffer
);
15124 /* Parse an operator.
15127 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15128 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15129 || ++ -- , ->* -> () []
15136 Returns an IDENTIFIER_NODE for the operator which is a
15137 human-readable spelling of the identifier, e.g., `operator +'. */
15140 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
15142 tree id
= NULL_TREE
;
15146 /* Peek at the next token. */
15147 token
= cp_lexer_peek_token (parser
->lexer
);
15149 location_t end_loc
= token
->location
;
15151 /* Figure out which operator we have. */
15152 enum tree_code op
= ERROR_MARK
;
15153 bool assop
= false;
15154 bool consumed
= false;
15155 switch (token
->type
)
15159 /* The keyword should be either `new' or `delete'. */
15160 if (token
->keyword
== RID_NEW
)
15162 else if (token
->keyword
== RID_DELETE
)
15167 /* Consume the `new' or `delete' token. */
15168 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
15170 /* Peek at the next token. */
15171 token
= cp_lexer_peek_token (parser
->lexer
);
15172 /* If it's a `[' token then this is the array variant of the
15174 if (token
->type
== CPP_OPEN_SQUARE
)
15176 /* Consume the `[' token. */
15177 cp_lexer_consume_token (parser
->lexer
);
15178 /* Look for the `]' token. */
15179 if (cp_token
*close_token
15180 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15181 end_loc
= close_token
->location
;
15182 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
15201 op
= TRUNC_DIV_EXPR
;
15205 op
= TRUNC_MOD_EXPR
;
15225 op
= TRUTH_NOT_EXPR
;
15258 op
= TRUNC_DIV_EXPR
;
15263 op
= TRUNC_MOD_EXPR
;
15289 case CPP_LSHIFT_EQ
:
15294 case CPP_RSHIFT_EQ
:
15311 case CPP_GREATER_EQ
:
15316 op
= TRUTH_ANDIF_EXPR
;
15320 op
= TRUTH_ORIF_EXPR
;
15323 case CPP_PLUS_PLUS
:
15324 op
= POSTINCREMENT_EXPR
;
15327 case CPP_MINUS_MINUS
:
15328 op
= PREDECREMENT_EXPR
;
15332 op
= COMPOUND_EXPR
;
15335 case CPP_DEREF_STAR
:
15340 op
= COMPONENT_REF
;
15343 case CPP_OPEN_PAREN
:
15345 /* Consume the `('. */
15346 matching_parens parens
;
15347 parens
.consume_open (parser
);
15348 /* Look for the matching `)'. */
15349 token
= parens
.require_close (parser
);
15351 end_loc
= token
->location
;
15357 case CPP_OPEN_SQUARE
:
15358 /* Consume the `['. */
15359 cp_lexer_consume_token (parser
->lexer
);
15360 /* Look for the matching `]'. */
15361 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15363 end_loc
= token
->location
;
15368 case CPP_UTF8STRING
:
15369 case CPP_UTF8STRING_USERDEF
:
15376 case CPP_STRING_USERDEF
:
15377 case CPP_WSTRING_USERDEF
:
15378 case CPP_STRING16_USERDEF
:
15379 case CPP_STRING32_USERDEF
:
15385 if (cxx_dialect
== cxx98
)
15386 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15388 /* Consume the string. */
15389 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15390 /*wide_ok=*/true, /*lookup_udlit=*/false);
15391 if (str
== error_mark_node
)
15392 return error_mark_node
;
15393 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15395 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
15396 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
15397 end_loc
= str
.get_location ();
15402 /* Look for the suffix identifier. */
15403 token
= cp_lexer_peek_token (parser
->lexer
);
15404 if (token
->type
== CPP_NAME
)
15406 id
= cp_parser_identifier (parser
);
15407 end_loc
= token
->location
;
15409 else if (token
->type
== CPP_KEYWORD
)
15411 error ("unexpected keyword;"
15412 " remove space between quotes and suffix identifier");
15413 return error_mark_node
;
15417 error ("expected suffix identifier");
15418 return error_mark_node
;
15421 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15422 (TREE_TYPE (TREE_TYPE (string_tree
))));
15423 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15426 error ("expected empty string after %<operator%> keyword");
15427 return error_mark_node
;
15429 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15432 error ("invalid encoding prefix in literal operator");
15433 return error_mark_node
;
15435 if (id
!= error_mark_node
)
15437 const char *name
= IDENTIFIER_POINTER (id
);
15438 id
= cp_literal_operator_id (name
);
15440 /* Generate a location of the form:
15441 "" _suffix_identifier
15442 ^~~~~~~~~~~~~~~~~~~~~
15443 with caret == start at the start token, finish at the end of the
15444 suffix identifier. */
15445 location_t finish_loc
15446 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15447 location_t combined_loc
15448 = make_location (start_loc
, start_loc
, finish_loc
);
15449 return cp_expr (id
, combined_loc
);
15453 /* Anything else is an error. */
15457 /* If we have selected an identifier, we need to consume the
15459 if (op
!= ERROR_MARK
)
15461 id
= ovl_op_identifier (assop
, op
);
15463 cp_lexer_consume_token (parser
->lexer
);
15465 /* Otherwise, no valid operator name was present. */
15468 cp_parser_error (parser
, "expected operator");
15469 id
= error_mark_node
;
15472 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15473 return cp_expr (id
, start_loc
);
15476 /* Parse a template-declaration.
15478 template-declaration:
15479 export [opt] template < template-parameter-list > declaration
15481 If MEMBER_P is TRUE, this template-declaration occurs within a
15484 The grammar rule given by the standard isn't correct. What
15485 is really meant is:
15487 template-declaration:
15488 export [opt] template-parameter-list-seq
15489 decl-specifier-seq [opt] init-declarator [opt] ;
15490 export [opt] template-parameter-list-seq
15491 function-definition
15493 template-parameter-list-seq:
15494 template-parameter-list-seq [opt]
15495 template < template-parameter-list >
15497 Concept Extensions:
15499 template-parameter-list-seq:
15500 template < template-parameter-list > requires-clause [opt]
15503 requires logical-or-expression */
15506 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15508 /* Check for `export'. */
15509 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15511 /* Consume the `export' token. */
15512 cp_lexer_consume_token (parser
->lexer
);
15513 /* Warn that we do not support `export'. */
15514 warning (0, "keyword %<export%> not implemented, and will be ignored");
15517 cp_parser_template_declaration_after_export (parser
, member_p
);
15520 /* Parse a template-parameter-list.
15522 template-parameter-list:
15524 template-parameter-list , template-parameter
15526 Returns a TREE_LIST. Each node represents a template parameter.
15527 The nodes are connected via their TREE_CHAINs. */
15530 cp_parser_template_parameter_list (cp_parser
* parser
)
15532 tree parameter_list
= NULL_TREE
;
15534 /* Don't create wrapper nodes within a template-parameter-list,
15535 since we don't want to have different types based on the
15536 spelling location of constants and decls within them. */
15537 auto_suppress_location_wrappers sentinel
;
15539 begin_template_parm_list ();
15541 /* The loop below parses the template parms. We first need to know
15542 the total number of template parms to be able to compute proper
15543 canonical types of each dependent type. So after the loop, when
15544 we know the total number of template parms,
15545 end_template_parm_list computes the proper canonical types and
15546 fixes up the dependent types accordingly. */
15551 bool is_parameter_pack
;
15552 location_t parm_loc
;
15554 /* Parse the template-parameter. */
15555 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15556 parameter
= cp_parser_template_parameter (parser
,
15558 &is_parameter_pack
);
15559 /* Add it to the list. */
15560 if (parameter
!= error_mark_node
)
15561 parameter_list
= process_template_parm (parameter_list
,
15565 is_parameter_pack
);
15568 tree err_parm
= build_tree_list (parameter
, parameter
);
15569 parameter_list
= chainon (parameter_list
, err_parm
);
15572 /* If the next token is not a `,', we're done. */
15573 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15575 /* Otherwise, consume the `,' token. */
15576 cp_lexer_consume_token (parser
->lexer
);
15579 return end_template_parm_list (parameter_list
);
15582 /* Parse a introduction-list.
15585 introduced-parameter
15586 introduction-list , introduced-parameter
15588 introduced-parameter:
15589 ...[opt] identifier
15591 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15592 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15593 WILDCARD_DECL will also have DECL_NAME set and token location in
15594 DECL_SOURCE_LOCATION. */
15597 cp_parser_introduction_list (cp_parser
*parser
)
15599 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15603 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15605 cp_lexer_consume_token (parser
->lexer
);
15607 tree identifier
= cp_parser_identifier (parser
);
15608 if (identifier
== error_mark_node
)
15611 /* Build placeholder. */
15612 tree parm
= build_nt (WILDCARD_DECL
);
15613 DECL_SOURCE_LOCATION (parm
)
15614 = cp_lexer_peek_token (parser
->lexer
)->location
;
15615 DECL_NAME (parm
) = identifier
;
15616 WILDCARD_PACK_P (parm
) = is_pack
;
15617 vec_safe_push (introduction_vec
, parm
);
15619 /* If the next token is not a `,', we're done. */
15620 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15622 /* Otherwise, consume the `,' token. */
15623 cp_lexer_consume_token (parser
->lexer
);
15626 /* Convert the vec into a TREE_VEC. */
15627 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15630 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15631 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15633 release_tree_vector (introduction_vec
);
15634 return introduction_list
;
15637 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15638 is an abstract declarator. */
15640 static inline cp_declarator
*
15641 get_id_declarator (cp_declarator
*declarator
)
15643 cp_declarator
*d
= declarator
;
15644 while (d
&& d
->kind
!= cdk_id
)
15649 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15650 is an abstract declarator. */
15653 get_unqualified_id (cp_declarator
*declarator
)
15655 declarator
= get_id_declarator (declarator
);
15657 return declarator
->u
.id
.unqualified_name
;
15662 /* Returns true if DECL represents a constrained-parameter. */
15665 is_constrained_parameter (tree decl
)
15668 && TREE_CODE (decl
) == TYPE_DECL
15669 && CONSTRAINED_PARM_CONCEPT (decl
)
15670 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15673 /* Returns true if PARM declares a constrained-parameter. */
15676 is_constrained_parameter (cp_parameter_declarator
*parm
)
15678 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15681 /* Check that the type parameter is only a declarator-id, and that its
15682 type is not cv-qualified. */
15685 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15686 cp_parameter_declarator
*parm
)
15688 if (!parm
->declarator
)
15691 if (parm
->declarator
->kind
!= cdk_id
)
15693 cp_parser_error (parser
, "invalid constrained type parameter");
15697 /* Don't allow cv-qualified type parameters. */
15698 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15699 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15701 cp_parser_error (parser
, "cv-qualified type parameter");
15708 /* Finish parsing/processing a template type parameter and checking
15709 various restrictions. */
15712 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15714 cp_parameter_declarator
* parmdecl
)
15716 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15717 return finish_template_type_parm (class_type_node
, id
);
15719 return error_mark_node
;
15723 finish_constrained_template_template_parm (tree proto
, tree id
)
15725 /* FIXME: This should probably be copied, and we may need to adjust
15726 the template parameter depths. */
15727 tree saved_parms
= current_template_parms
;
15728 begin_template_parm_list ();
15729 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15730 end_template_parm_list ();
15732 tree parm
= finish_template_template_parm (class_type_node
, id
);
15733 current_template_parms
= saved_parms
;
15738 /* Finish parsing/processing a template template parameter by borrowing
15739 the template parameter list from the prototype parameter. */
15742 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15745 cp_parameter_declarator
*parmdecl
)
15747 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15748 return error_mark_node
;
15749 return finish_constrained_template_template_parm (proto
, id
);
15752 /* Create a new non-type template parameter from the given PARM
15756 constrained_non_type_template_parm (bool *is_non_type
,
15757 cp_parameter_declarator
*parm
)
15759 *is_non_type
= true;
15760 cp_declarator
*decl
= parm
->declarator
;
15761 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15762 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15763 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15766 /* Build a constrained template parameter based on the PARMDECL
15767 declarator. The type of PARMDECL is the constrained type, which
15768 refers to the prototype template parameter that ultimately
15769 specifies the type of the declared parameter. */
15772 finish_constrained_parameter (cp_parser
*parser
,
15773 cp_parameter_declarator
*parmdecl
,
15775 bool *is_parameter_pack
)
15777 tree decl
= parmdecl
->decl_specifiers
.type
;
15778 tree id
= get_unqualified_id (parmdecl
->declarator
);
15779 tree def
= parmdecl
->default_argument
;
15780 tree proto
= DECL_INITIAL (decl
);
15782 /* A template parameter constrained by a variadic concept shall also
15783 be declared as a template parameter pack. */
15784 bool is_variadic
= template_parameter_pack_p (proto
);
15785 if (is_variadic
&& !*is_parameter_pack
)
15786 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15788 /* Build the parameter. Return an error if the declarator was invalid. */
15790 if (TREE_CODE (proto
) == TYPE_DECL
)
15791 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15792 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15793 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15796 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15797 if (parm
== error_mark_node
)
15798 return error_mark_node
;
15800 /* Finish the parameter decl and create a node attaching the
15801 default argument and constraint. */
15802 parm
= build_tree_list (def
, parm
);
15803 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15808 /* Returns true if the parsed type actually represents the declaration
15809 of a type template-parameter. */
15812 declares_constrained_type_template_parameter (tree type
)
15814 return (is_constrained_parameter (type
)
15815 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15819 /* Returns true if the parsed type actually represents the declaration of
15820 a template template-parameter. */
15823 declares_constrained_template_template_parameter (tree type
)
15825 return (is_constrained_parameter (type
)
15826 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15829 /* Parse a default argument for a type template-parameter.
15830 Note that diagnostics are handled in cp_parser_template_parameter. */
15833 cp_parser_default_type_template_argument (cp_parser
*parser
)
15835 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15837 /* Consume the `=' token. */
15838 cp_lexer_consume_token (parser
->lexer
);
15840 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15842 /* Parse the default-argument. */
15843 push_deferring_access_checks (dk_no_deferred
);
15844 tree default_argument
= cp_parser_type_id (parser
,
15845 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15847 pop_deferring_access_checks ();
15849 if (flag_concepts
&& type_uses_auto (default_argument
))
15851 error_at (token
->location
,
15852 "invalid use of %<auto%> in default template argument");
15853 return error_mark_node
;
15856 return default_argument
;
15859 /* Parse a default argument for a template template-parameter. */
15862 cp_parser_default_template_template_argument (cp_parser
*parser
)
15864 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15868 /* Consume the `='. */
15869 cp_lexer_consume_token (parser
->lexer
);
15870 /* Parse the id-expression. */
15871 push_deferring_access_checks (dk_no_deferred
);
15872 /* save token before parsing the id-expression, for error
15874 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15875 tree default_argument
15876 = cp_parser_id_expression (parser
,
15877 /*template_keyword_p=*/false,
15878 /*check_dependency_p=*/true,
15879 /*template_p=*/&is_template
,
15880 /*declarator_p=*/false,
15881 /*optional_p=*/false);
15882 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15883 /* If the id-expression was a template-id that refers to
15884 a template-class, we already have the declaration here,
15885 so no further lookup is needed. */
15888 /* Look up the name. */
15890 = cp_parser_lookup_name (parser
, default_argument
,
15892 /*is_template=*/is_template
,
15893 /*is_namespace=*/false,
15894 /*check_dependency=*/true,
15895 /*ambiguous_decls=*/NULL
,
15897 /* See if the default argument is valid. */
15898 default_argument
= check_template_template_default_arg (default_argument
);
15899 pop_deferring_access_checks ();
15900 return default_argument
;
15903 /* Parse a template-parameter.
15905 template-parameter:
15907 parameter-declaration
15909 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15910 the parameter. The TREE_PURPOSE is the default value, if any.
15911 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15912 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15913 set to true iff this parameter is a parameter pack. */
15916 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15917 bool *is_parameter_pack
)
15920 cp_parameter_declarator
*parameter_declarator
;
15923 /* Assume it is a type parameter or a template parameter. */
15924 *is_non_type
= false;
15925 /* Assume it not a parameter pack. */
15926 *is_parameter_pack
= false;
15927 /* Peek at the next token. */
15928 token
= cp_lexer_peek_token (parser
->lexer
);
15929 /* If it is `template', we have a type-parameter. */
15930 if (token
->keyword
== RID_TEMPLATE
)
15931 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15932 /* If it is `class' or `typename' we do not know yet whether it is a
15933 type parameter or a non-type parameter. Consider:
15935 template <typename T, typename T::X X> ...
15939 template <class C, class D*> ...
15941 Here, the first parameter is a type parameter, and the second is
15942 a non-type parameter. We can tell by looking at the token after
15943 the identifier -- if it is a `,', `=', or `>' then we have a type
15945 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15947 /* Peek at the token after `class' or `typename'. */
15948 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15949 /* If it's an ellipsis, we have a template type parameter
15951 if (token
->type
== CPP_ELLIPSIS
)
15952 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15953 /* If it's an identifier, skip it. */
15954 if (token
->type
== CPP_NAME
)
15955 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15956 /* Now, see if the token looks like the end of a template
15958 if (token
->type
== CPP_COMMA
15959 || token
->type
== CPP_EQ
15960 || token
->type
== CPP_GREATER
)
15961 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15964 /* Otherwise, it is a non-type parameter or a constrained parameter.
15968 When parsing a default template-argument for a non-type
15969 template-parameter, the first non-nested `>' is taken as the end
15970 of the template parameter-list rather than a greater-than
15972 parameter_declarator
15973 = cp_parser_parameter_declaration (parser
,
15974 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15975 /*template_parm_p=*/true,
15976 /*parenthesized_p=*/NULL
);
15978 if (!parameter_declarator
)
15979 return error_mark_node
;
15981 /* If the parameter declaration is marked as a parameter pack, set
15982 *IS_PARAMETER_PACK to notify the caller. */
15983 if (parameter_declarator
->template_parameter_pack_p
)
15984 *is_parameter_pack
= true;
15986 if (parameter_declarator
->default_argument
)
15988 /* Can happen in some cases of erroneous input (c++/34892). */
15989 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15990 /* Consume the `...' for better error recovery. */
15991 cp_lexer_consume_token (parser
->lexer
);
15994 // The parameter may have been constrained.
15995 if (is_constrained_parameter (parameter_declarator
))
15996 return finish_constrained_parameter (parser
,
15997 parameter_declarator
,
15999 is_parameter_pack
);
16001 // Now we're sure that the parameter is a non-type parameter.
16002 *is_non_type
= true;
16004 parm
= grokdeclarator (parameter_declarator
->declarator
,
16005 ¶meter_declarator
->decl_specifiers
,
16006 TPARM
, /*initialized=*/0,
16007 /*attrlist=*/NULL
);
16008 if (parm
== error_mark_node
)
16009 return error_mark_node
;
16011 return build_tree_list (parameter_declarator
->default_argument
, parm
);
16014 /* Parse a type-parameter.
16017 class identifier [opt]
16018 class identifier [opt] = type-id
16019 typename identifier [opt]
16020 typename identifier [opt] = type-id
16021 template < template-parameter-list > class identifier [opt]
16022 template < template-parameter-list > class identifier [opt]
16025 GNU Extension (variadic templates):
16028 class ... identifier [opt]
16029 typename ... identifier [opt]
16031 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16032 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16033 the declaration of the parameter.
16035 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16038 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
16043 /* Look for a keyword to tell us what kind of parameter this is. */
16044 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
16046 return error_mark_node
;
16048 switch (token
->keyword
)
16054 tree default_argument
;
16056 /* If the next token is an ellipsis, we have a template
16058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16060 /* Consume the `...' token. */
16061 cp_lexer_consume_token (parser
->lexer
);
16062 maybe_warn_variadic_templates ();
16064 *is_parameter_pack
= true;
16067 /* If the next token is an identifier, then it names the
16069 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16070 identifier
= cp_parser_identifier (parser
);
16072 identifier
= NULL_TREE
;
16074 /* Create the parameter. */
16075 parameter
= finish_template_type_parm (class_type_node
, identifier
);
16077 /* If the next token is an `=', we have a default argument. */
16078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16081 = cp_parser_default_type_template_argument (parser
);
16083 /* Template parameter packs cannot have default
16085 if (*is_parameter_pack
)
16088 error_at (token
->location
,
16089 "template parameter pack %qD cannot have a "
16090 "default argument", identifier
);
16092 error_at (token
->location
,
16093 "template parameter packs cannot have "
16094 "default arguments");
16095 default_argument
= NULL_TREE
;
16097 else if (check_for_bare_parameter_packs (default_argument
))
16098 default_argument
= error_mark_node
;
16101 default_argument
= NULL_TREE
;
16103 /* Create the combined representation of the parameter and the
16104 default argument. */
16105 parameter
= build_tree_list (default_argument
, parameter
);
16112 tree default_argument
;
16114 /* Look for the `<'. */
16115 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16116 /* Parse the template-parameter-list. */
16117 cp_parser_template_parameter_list (parser
);
16118 /* Look for the `>'. */
16119 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16121 // If template requirements are present, parse them.
16124 tree reqs
= get_shorthand_constraints (current_template_parms
);
16125 if (tree r
= cp_parser_requires_clause_opt (parser
))
16126 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
16127 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
16130 /* Look for the `class' or 'typename' keywords. */
16131 cp_parser_type_parameter_key (parser
);
16132 /* If the next token is an ellipsis, we have a template
16134 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16136 /* Consume the `...' token. */
16137 cp_lexer_consume_token (parser
->lexer
);
16138 maybe_warn_variadic_templates ();
16140 *is_parameter_pack
= true;
16142 /* If the next token is an `=', then there is a
16143 default-argument. If the next token is a `>', we are at
16144 the end of the parameter-list. If the next token is a `,',
16145 then we are at the end of this parameter. */
16146 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16147 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
16148 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16150 identifier
= cp_parser_identifier (parser
);
16151 /* Treat invalid names as if the parameter were nameless. */
16152 if (identifier
== error_mark_node
)
16153 identifier
= NULL_TREE
;
16156 identifier
= NULL_TREE
;
16158 /* Create the template parameter. */
16159 parameter
= finish_template_template_parm (class_type_node
,
16162 /* If the next token is an `=', then there is a
16163 default-argument. */
16164 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16167 = cp_parser_default_template_template_argument (parser
);
16169 /* Template parameter packs cannot have default
16171 if (*is_parameter_pack
)
16174 error_at (token
->location
,
16175 "template parameter pack %qD cannot "
16176 "have a default argument",
16179 error_at (token
->location
, "template parameter packs cannot "
16180 "have default arguments");
16181 default_argument
= NULL_TREE
;
16185 default_argument
= NULL_TREE
;
16187 /* Create the combined representation of the parameter and the
16188 default argument. */
16189 parameter
= build_tree_list (default_argument
, parameter
);
16194 gcc_unreachable ();
16201 /* Parse a template-id.
16204 template-name < template-argument-list [opt] >
16206 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16207 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16208 returned. Otherwise, if the template-name names a function, or set
16209 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16210 names a class, returns a TYPE_DECL for the specialization.
16212 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16213 uninstantiated templates. */
16216 cp_parser_template_id (cp_parser
*parser
,
16217 bool template_keyword_p
,
16218 bool check_dependency_p
,
16219 enum tag_types tag_type
,
16220 bool is_declaration
)
16225 cp_token_position start_of_id
= 0;
16226 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
16227 bool is_identifier
;
16229 /* If the next token corresponds to a template-id, there is no need
16231 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16232 if (token
->type
== CPP_TEMPLATE_ID
)
16234 cp_lexer_consume_token (parser
->lexer
);
16235 return saved_checks_value (token
->u
.tree_check_value
);
16238 /* Avoid performing name lookup if there is no possibility of
16239 finding a template-id. */
16240 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
16241 || (token
->type
== CPP_NAME
16242 && !cp_parser_nth_token_starts_template_argument_list_p
16245 cp_parser_error (parser
, "expected template-id");
16246 return error_mark_node
;
16249 /* Remember where the template-id starts. */
16250 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
16251 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
16253 push_deferring_access_checks (dk_deferred
);
16255 /* Parse the template-name. */
16256 is_identifier
= false;
16257 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16258 check_dependency_p
,
16263 /* Push any access checks inside the firewall we're about to create. */
16264 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
16265 pop_deferring_access_checks ();
16266 if (templ
== error_mark_node
|| is_identifier
)
16269 /* Since we're going to preserve any side-effects from this parse, set up a
16270 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16271 in the template arguments. */
16272 tentative_firewall
firewall (parser
);
16273 reopen_deferring_access_checks (checks
);
16275 /* If we find the sequence `[:' after a template-name, it's probably
16276 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16277 parse correctly the argument list. */
16278 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16279 == CPP_OPEN_SQUARE
)
16280 && next_token
->flags
& DIGRAPH
16281 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16283 && !(next_token_2
->flags
& PREV_WHITE
))
16285 cp_parser_parse_tentatively (parser
);
16286 /* Change `:' into `::'. */
16287 next_token_2
->type
= CPP_SCOPE
;
16288 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16290 cp_lexer_consume_token (parser
->lexer
);
16292 /* Parse the arguments. */
16293 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16294 if (!cp_parser_parse_definitely (parser
))
16296 /* If we couldn't parse an argument list, then we revert our changes
16297 and return simply an error. Maybe this is not a template-id
16299 next_token_2
->type
= CPP_COLON
;
16300 cp_parser_error (parser
, "expected %<<%>");
16301 pop_deferring_access_checks ();
16302 return error_mark_node
;
16304 /* Otherwise, emit an error about the invalid digraph, but continue
16305 parsing because we got our argument list. */
16306 if (permerror (next_token
->location
,
16307 "%<<::%> cannot begin a template-argument list"))
16309 static bool hint
= false;
16310 inform (next_token
->location
,
16311 "%<<:%> is an alternate spelling for %<[%>."
16312 " Insert whitespace between %<<%> and %<::%>");
16313 if (!hint
&& !flag_permissive
)
16315 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16316 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16317 "accept your code)");
16324 /* Look for the `<' that starts the template-argument-list. */
16325 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16327 pop_deferring_access_checks ();
16328 return error_mark_node
;
16330 /* Parse the arguments. */
16331 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16333 if ((cxx_dialect
> cxx17
)
16334 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16335 && !template_keyword_p
16336 && (cp_parser_error_occurred (parser
)
16337 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16339 /* This didn't go well. */
16340 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16342 /* C++2A says that "function-name < a;" is now ill-formed. */
16343 if (cp_parser_error_occurred (parser
))
16345 error_at (token
->location
, "invalid template-argument-list");
16346 inform (token
->location
, "function name as the left hand "
16347 "operand of %<<%> is ill-formed in C++2a; wrap the "
16348 "function name in %<()%>");
16351 /* We expect "f<targs>" to be followed by "(args)". */
16352 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16353 "expected %<(%> after template-argument-list");
16355 /* Purge all subsequent tokens. */
16356 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16359 cp_parser_simulate_error (parser
);
16360 pop_deferring_access_checks ();
16361 return error_mark_node
;
16365 /* Set the location to be of the form:
16366 template-name < template-argument-list [opt] >
16367 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16368 with caret == start at the start of the template-name,
16369 ranging until the closing '>'. */
16370 location_t finish_loc
16371 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
16372 location_t combined_loc
16373 = make_location (token
->location
, token
->location
, finish_loc
);
16375 /* Check for concepts autos where they don't belong. We could
16376 identify types in some cases of idnetifier TEMPL, looking ahead
16377 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16378 types. We reject them in functions, but if what we have is an
16379 identifier, even with none_type we can't conclude it's NOT a
16380 type, we have to wait for template substitution. */
16381 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16382 template_id
= error_mark_node
;
16383 /* Build a representation of the specialization. */
16384 else if (identifier_p (templ
))
16385 template_id
= build_min_nt_loc (combined_loc
,
16388 else if (DECL_TYPE_TEMPLATE_P (templ
)
16389 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16391 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16392 template (rather than some instantiation thereof) only if
16393 is not nested within some other construct. For example, in
16394 "template <typename T> void f(T) { A<T>::", A<T> is just an
16395 instantiation of A. */
16396 bool entering_scope
16397 = (template_parm_scope_p ()
16398 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16400 = finish_template_type (templ
, arguments
, entering_scope
);
16402 /* A template-like identifier may be a partial concept id. */
16403 else if (flag_concepts
16404 && (template_id
= (cp_parser_maybe_partial_concept_id
16405 (parser
, templ
, arguments
))))
16406 return template_id
;
16407 else if (variable_template_p (templ
))
16409 template_id
= lookup_template_variable (templ
, arguments
);
16410 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16411 SET_EXPR_LOCATION (template_id
, combined_loc
);
16415 /* If it's not a class-template or a template-template, it should be
16416 a function-template. */
16417 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16418 || TREE_CODE (templ
) == OVERLOAD
16419 || TREE_CODE (templ
) == FUNCTION_DECL
16420 || BASELINK_P (templ
)));
16422 template_id
= lookup_template_function (templ
, arguments
);
16423 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16424 SET_EXPR_LOCATION (template_id
, combined_loc
);
16427 /* If parsing tentatively, replace the sequence of tokens that makes
16428 up the template-id with a CPP_TEMPLATE_ID token. That way,
16429 should we re-parse the token stream, we will not have to repeat
16430 the effort required to do the parse, nor will we issue duplicate
16431 error messages about problems during instantiation of the
16434 /* Don't do this if we had a parse error in a declarator; re-parsing
16435 might succeed if a name changes meaning (60361). */
16436 && !(cp_parser_error_occurred (parser
)
16437 && cp_parser_parsing_tentatively (parser
)
16438 && parser
->in_declarator_p
))
16440 /* Reset the contents of the START_OF_ID token. */
16441 token
->type
= CPP_TEMPLATE_ID
;
16442 token
->location
= combined_loc
;
16444 /* Retrieve any deferred checks. Do not pop this access checks yet
16445 so the memory will not be reclaimed during token replacing below. */
16446 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16447 token
->u
.tree_check_value
->value
= template_id
;
16448 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16449 token
->keyword
= RID_MAX
;
16451 /* Purge all subsequent tokens. */
16452 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16454 /* ??? Can we actually assume that, if template_id ==
16455 error_mark_node, we will have issued a diagnostic to the
16456 user, as opposed to simply marking the tentative parse as
16458 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16459 error_at (token
->location
, "parse error in template argument list");
16462 pop_to_parent_deferring_access_checks ();
16463 return template_id
;
16466 /* Parse a template-name.
16471 The standard should actually say:
16475 operator-function-id
16477 A defect report has been filed about this issue.
16479 A conversion-function-id cannot be a template name because they cannot
16480 be part of a template-id. In fact, looking at this code:
16482 a.operator K<int>()
16484 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16485 It is impossible to call a templated conversion-function-id with an
16486 explicit argument list, since the only allowed template parameter is
16487 the type to which it is converting.
16489 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16490 `template' keyword, in a construction like:
16494 In that case `f' is taken to be a template-name, even though there
16495 is no way of knowing for sure.
16497 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16498 name refers to a set of overloaded functions, at least one of which
16499 is a template, or an IDENTIFIER_NODE with the name of the template,
16500 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16501 names are looked up inside uninstantiated templates. */
16504 cp_parser_template_name (cp_parser
* parser
,
16505 bool template_keyword_p
,
16506 bool check_dependency_p
,
16507 bool is_declaration
,
16508 enum tag_types tag_type
,
16509 bool *is_identifier
)
16513 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16515 /* If the next token is `operator', then we have either an
16516 operator-function-id or a conversion-function-id. */
16517 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16519 /* We don't know whether we're looking at an
16520 operator-function-id or a conversion-function-id. */
16521 cp_parser_parse_tentatively (parser
);
16522 /* Try an operator-function-id. */
16523 identifier
= cp_parser_operator_function_id (parser
);
16524 /* If that didn't work, try a conversion-function-id. */
16525 if (!cp_parser_parse_definitely (parser
))
16527 cp_parser_error (parser
, "expected template-name");
16528 return error_mark_node
;
16531 /* Look for the identifier. */
16533 identifier
= cp_parser_identifier (parser
);
16535 /* If we didn't find an identifier, we don't have a template-id. */
16536 if (identifier
== error_mark_node
)
16537 return error_mark_node
;
16539 /* If the name immediately followed the `template' keyword, then it
16540 is a template-name. However, if the next token is not `<', then
16541 we do not treat it as a template-name, since it is not being used
16542 as part of a template-id. This enables us to handle constructs
16545 template <typename T> struct S { S(); };
16546 template <typename T> S<T>::S();
16548 correctly. We would treat `S' as a template -- if it were `S<T>'
16549 -- but we do not if there is no `<'. */
16551 if (processing_template_decl
16552 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16554 /* In a declaration, in a dependent context, we pretend that the
16555 "template" keyword was present in order to improve error
16556 recovery. For example, given:
16558 template <typename T> void f(T::X<int>);
16560 we want to treat "X<int>" as a template-id. */
16562 && !template_keyword_p
16563 && parser
->scope
&& TYPE_P (parser
->scope
)
16564 && check_dependency_p
16565 && dependent_scope_p (parser
->scope
)
16566 /* Do not do this for dtors (or ctors), since they never
16567 need the template keyword before their name. */
16568 && !constructor_name_p (identifier
, parser
->scope
))
16570 cp_token_position start
= 0;
16572 /* Explain what went wrong. */
16573 error_at (token
->location
, "non-template %qD used as template",
16575 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16576 parser
->scope
, identifier
);
16577 /* If parsing tentatively, find the location of the "<" token. */
16578 if (cp_parser_simulate_error (parser
))
16579 start
= cp_lexer_token_position (parser
->lexer
, true);
16580 /* Parse the template arguments so that we can issue error
16581 messages about them. */
16582 cp_lexer_consume_token (parser
->lexer
);
16583 cp_parser_enclosed_template_argument_list (parser
);
16584 /* Skip tokens until we find a good place from which to
16585 continue parsing. */
16586 cp_parser_skip_to_closing_parenthesis (parser
,
16587 /*recovering=*/true,
16589 /*consume_paren=*/false);
16590 /* If parsing tentatively, permanently remove the
16591 template argument list. That will prevent duplicate
16592 error messages from being issued about the missing
16593 "template" keyword. */
16595 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16597 *is_identifier
= true;
16598 parser
->context
->object_type
= NULL_TREE
;
16602 /* If the "template" keyword is present, then there is generally
16603 no point in doing name-lookup, so we just return IDENTIFIER.
16604 But, if the qualifying scope is non-dependent then we can
16605 (and must) do name-lookup normally. */
16606 if (template_keyword_p
)
16608 tree scope
= (parser
->scope
? parser
->scope
16609 : parser
->context
->object_type
);
16610 if (scope
&& TYPE_P (scope
)
16611 && (!CLASS_TYPE_P (scope
)
16612 || (check_dependency_p
&& dependent_type_p (scope
))))
16614 /* We're optimizing away the call to cp_parser_lookup_name, but
16615 we still need to do this. */
16616 parser
->context
->object_type
= NULL_TREE
;
16622 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16623 const bool scoped_p
= ((parser
->scope
? parser
->scope
16624 : parser
->context
->object_type
) != NULL_TREE
);
16626 /* Look up the name. */
16627 decl
= cp_parser_lookup_name (parser
, identifier
,
16629 /*is_template=*/true,
16630 /*is_namespace=*/false,
16631 check_dependency_p
,
16632 /*ambiguous_decls=*/NULL
,
16635 decl
= strip_using_decl (decl
);
16637 /* If DECL is a template, then the name was a template-name. */
16638 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16640 if (TREE_DEPRECATED (decl
)
16641 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16642 warn_deprecated_use (decl
, NULL_TREE
);
16646 /* The standard does not explicitly indicate whether a name that
16647 names a set of overloaded declarations, some of which are
16648 templates, is a template-name. However, such a name should
16649 be a template-name; otherwise, there is no way to form a
16650 template-id for the overloaded templates. */
16651 bool found
= false;
16653 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16654 !found
&& iter
; ++iter
)
16655 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16659 && (cxx_dialect
> cxx17
)
16661 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
)
16662 && tag_type
== none_type
)
16664 /* [temp.names] says "A name is also considered to refer to a template
16665 if it is an unqualified-id followed by a < and name lookup finds
16666 either one or more functions or finds nothing." */
16668 /* The "more functions" case. Just use the OVERLOAD as normally.
16669 We don't use is_overloaded_fn here to avoid considering
16671 if (TREE_CODE (decl
) == OVERLOAD
16672 /* Name lookup found one function. */
16673 || TREE_CODE (decl
) == FUNCTION_DECL
)
16675 /* Name lookup found nothing. */
16676 else if (decl
== error_mark_node
)
16682 /* The name does not name a template. */
16683 cp_parser_error (parser
, "expected template-name");
16684 return error_mark_node
;
16691 /* Parse a template-argument-list.
16693 template-argument-list:
16694 template-argument ... [opt]
16695 template-argument-list , template-argument ... [opt]
16697 Returns a TREE_VEC containing the arguments. */
16700 cp_parser_template_argument_list (cp_parser
* parser
)
16702 tree fixed_args
[10];
16703 unsigned n_args
= 0;
16704 unsigned alloced
= 10;
16705 tree
*arg_ary
= fixed_args
;
16707 bool saved_in_template_argument_list_p
;
16709 bool saved_non_ice_p
;
16711 /* Don't create location wrapper nodes within a template-argument-list. */
16712 auto_suppress_location_wrappers sentinel
;
16714 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16715 parser
->in_template_argument_list_p
= true;
16716 /* Even if the template-id appears in an integral
16717 constant-expression, the contents of the argument list do
16719 saved_ice_p
= parser
->integral_constant_expression_p
;
16720 parser
->integral_constant_expression_p
= false;
16721 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16722 parser
->non_integral_constant_expression_p
= false;
16724 /* Parse the arguments. */
16730 /* Consume the comma. */
16731 cp_lexer_consume_token (parser
->lexer
);
16733 /* Parse the template-argument. */
16734 argument
= cp_parser_template_argument (parser
);
16736 /* If the next token is an ellipsis, we're expanding a template
16738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16740 if (argument
== error_mark_node
)
16742 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16743 error_at (token
->location
,
16744 "expected parameter pack before %<...%>");
16746 /* Consume the `...' token. */
16747 cp_lexer_consume_token (parser
->lexer
);
16749 /* Make the argument into a TYPE_PACK_EXPANSION or
16750 EXPR_PACK_EXPANSION. */
16751 argument
= make_pack_expansion (argument
);
16754 if (n_args
== alloced
)
16758 if (arg_ary
== fixed_args
)
16760 arg_ary
= XNEWVEC (tree
, alloced
);
16761 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16764 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16766 arg_ary
[n_args
++] = argument
;
16768 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16770 vec
= make_tree_vec (n_args
);
16773 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16775 if (arg_ary
!= fixed_args
)
16777 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16778 parser
->integral_constant_expression_p
= saved_ice_p
;
16779 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16781 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16785 /* Parse a template-argument.
16788 assignment-expression
16792 The representation is that of an assignment-expression, type-id, or
16793 id-expression -- except that the qualified id-expression is
16794 evaluated, so that the value returned is either a DECL or an
16797 Although the standard says "assignment-expression", it forbids
16798 throw-expressions or assignments in the template argument.
16799 Therefore, we use "conditional-expression" instead. */
16802 cp_parser_template_argument (cp_parser
* parser
)
16807 bool maybe_type_id
= false;
16808 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16809 location_t loc
= 0;
16812 /* There's really no way to know what we're looking at, so we just
16813 try each alternative in order.
16817 In a template-argument, an ambiguity between a type-id and an
16818 expression is resolved to a type-id, regardless of the form of
16819 the corresponding template-parameter.
16821 Therefore, we try a type-id first. */
16822 cp_parser_parse_tentatively (parser
);
16823 argument
= cp_parser_template_type_arg (parser
);
16824 /* If there was no error parsing the type-id but the next token is a
16825 '>>', our behavior depends on which dialect of C++ we're
16826 parsing. In C++98, we probably found a typo for '> >'. But there
16827 are type-id which are also valid expressions. For instance:
16829 struct X { int operator >> (int); };
16830 template <int V> struct Foo {};
16833 Here 'X()' is a valid type-id of a function type, but the user just
16834 wanted to write the expression "X() >> 5". Thus, we remember that we
16835 found a valid type-id, but we still try to parse the argument as an
16836 expression to see what happens.
16838 In C++0x, the '>>' will be considered two separate '>'
16840 if (!cp_parser_error_occurred (parser
)
16841 && cxx_dialect
== cxx98
16842 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16844 maybe_type_id
= true;
16845 cp_parser_abort_tentative_parse (parser
);
16849 /* If the next token isn't a `,' or a `>', then this argument wasn't
16850 really finished. This means that the argument is not a valid
16852 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16853 cp_parser_error (parser
, "expected template-argument");
16854 /* If that worked, we're done. */
16855 if (cp_parser_parse_definitely (parser
))
16858 /* We're still not sure what the argument will be. */
16859 cp_parser_parse_tentatively (parser
);
16860 /* Try a template. */
16861 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16862 argument
= cp_parser_id_expression (parser
,
16863 /*template_keyword_p=*/false,
16864 /*check_dependency_p=*/true,
16866 /*declarator_p=*/false,
16867 /*optional_p=*/false);
16868 /* If the next token isn't a `,' or a `>', then this argument wasn't
16869 really finished. */
16870 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16871 cp_parser_error (parser
, "expected template-argument");
16872 if (!cp_parser_error_occurred (parser
))
16874 /* Figure out what is being referred to. If the id-expression
16875 was for a class template specialization, then we will have a
16876 TYPE_DECL at this point. There is no need to do name lookup
16877 at this point in that case. */
16878 if (TREE_CODE (argument
) != TYPE_DECL
)
16879 argument
= cp_parser_lookup_name (parser
, argument
,
16881 /*is_template=*/template_p
,
16882 /*is_namespace=*/false,
16883 /*check_dependency=*/true,
16884 /*ambiguous_decls=*/NULL
,
16885 argument_start_token
->location
);
16886 /* Handle a constrained-type-specifier for a non-type template
16888 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16890 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16891 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16892 cp_parser_error (parser
, "expected template-name");
16894 if (cp_parser_parse_definitely (parser
))
16896 if (TREE_DEPRECATED (argument
))
16897 warn_deprecated_use (argument
, NULL_TREE
);
16900 /* It must be a non-type argument. In C++17 any constant-expression is
16902 if (cxx_dialect
> cxx14
)
16905 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16907 -- an integral constant-expression of integral or enumeration
16910 -- the name of a non-type template-parameter; or
16912 -- the name of an object or function with external linkage...
16914 -- the address of an object or function with external linkage...
16916 -- a pointer to member... */
16917 /* Look for a non-type template parameter. */
16918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16920 cp_parser_parse_tentatively (parser
);
16921 argument
= cp_parser_primary_expression (parser
,
16922 /*address_p=*/false,
16924 /*template_arg_p=*/true,
16926 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16927 || !cp_parser_next_token_ends_template_argument_p (parser
))
16928 cp_parser_simulate_error (parser
);
16929 if (cp_parser_parse_definitely (parser
))
16933 /* If the next token is "&", the argument must be the address of an
16934 object or function with external linkage. */
16935 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16938 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16939 cp_lexer_consume_token (parser
->lexer
);
16941 /* See if we might have an id-expression. */
16942 token
= cp_lexer_peek_token (parser
->lexer
);
16943 if (token
->type
== CPP_NAME
16944 || token
->keyword
== RID_OPERATOR
16945 || token
->type
== CPP_SCOPE
16946 || token
->type
== CPP_TEMPLATE_ID
16947 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16949 cp_parser_parse_tentatively (parser
);
16950 argument
= cp_parser_primary_expression (parser
,
16953 /*template_arg_p=*/true,
16955 if (cp_parser_error_occurred (parser
)
16956 || !cp_parser_next_token_ends_template_argument_p (parser
))
16957 cp_parser_abort_tentative_parse (parser
);
16962 if (INDIRECT_REF_P (argument
))
16964 /* Strip the dereference temporarily. */
16965 gcc_assert (REFERENCE_REF_P (argument
));
16966 argument
= TREE_OPERAND (argument
, 0);
16969 /* If we're in a template, we represent a qualified-id referring
16970 to a static data member as a SCOPE_REF even if the scope isn't
16971 dependent so that we can check access control later. */
16973 if (TREE_CODE (probe
) == SCOPE_REF
)
16974 probe
= TREE_OPERAND (probe
, 1);
16977 /* A variable without external linkage might still be a
16978 valid constant-expression, so no error is issued here
16979 if the external-linkage check fails. */
16980 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16981 cp_parser_simulate_error (parser
);
16983 else if (is_overloaded_fn (argument
))
16984 /* All overloaded functions are allowed; if the external
16985 linkage test does not pass, an error will be issued
16989 && (TREE_CODE (argument
) == OFFSET_REF
16990 || TREE_CODE (argument
) == SCOPE_REF
))
16991 /* A pointer-to-member. */
16993 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16996 cp_parser_simulate_error (parser
);
16998 if (cp_parser_parse_definitely (parser
))
17001 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
17002 tf_warning_or_error
);
17004 argument
= convert_from_reference (argument
);
17009 /* If the argument started with "&", there are no other valid
17010 alternatives at this point. */
17013 cp_parser_error (parser
, "invalid non-type template argument");
17014 return error_mark_node
;
17018 /* If the argument wasn't successfully parsed as a type-id followed
17019 by '>>', the argument can only be a constant expression now.
17020 Otherwise, we try parsing the constant-expression tentatively,
17021 because the argument could really be a type-id. */
17023 cp_parser_parse_tentatively (parser
);
17025 if (cxx_dialect
<= cxx14
)
17026 argument
= cp_parser_constant_expression (parser
);
17029 /* In C++20, we can encounter a braced-init-list. */
17030 if (cxx_dialect
>= cxx2a
17031 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17033 bool expr_non_constant_p
;
17034 return cp_parser_braced_list (parser
, &expr_non_constant_p
);
17037 /* With C++17 generalized non-type template arguments we need to handle
17038 lvalue constant expressions, too. */
17039 argument
= cp_parser_assignment_expression (parser
);
17040 require_potential_constant_expression (argument
);
17043 if (!maybe_type_id
)
17045 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17046 cp_parser_error (parser
, "expected template-argument");
17047 if (cp_parser_parse_definitely (parser
))
17049 /* We did our best to parse the argument as a non type-id, but that
17050 was the only alternative that matched (albeit with a '>' after
17051 it). We can assume it's just a typo from the user, and a
17052 diagnostic will then be issued. */
17053 return cp_parser_template_type_arg (parser
);
17056 /* Parse an explicit-instantiation.
17058 explicit-instantiation:
17059 template declaration
17061 Although the standard says `declaration', what it really means is:
17063 explicit-instantiation:
17064 template decl-specifier-seq [opt] declarator [opt] ;
17066 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17067 supposed to be allowed. A defect report has been filed about this
17072 explicit-instantiation:
17073 storage-class-specifier template
17074 decl-specifier-seq [opt] declarator [opt] ;
17075 function-specifier template
17076 decl-specifier-seq [opt] declarator [opt] ; */
17079 cp_parser_explicit_instantiation (cp_parser
* parser
)
17081 int declares_class_or_enum
;
17082 cp_decl_specifier_seq decl_specifiers
;
17083 tree extension_specifier
= NULL_TREE
;
17085 timevar_push (TV_TEMPLATE_INST
);
17087 /* Look for an (optional) storage-class-specifier or
17088 function-specifier. */
17089 if (cp_parser_allow_gnu_extensions_p (parser
))
17091 extension_specifier
17092 = cp_parser_storage_class_specifier_opt (parser
);
17093 if (!extension_specifier
)
17094 extension_specifier
17095 = cp_parser_function_specifier_opt (parser
,
17096 /*decl_specs=*/NULL
);
17099 /* Look for the `template' keyword. */
17100 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17101 /* Let the front end know that we are processing an explicit
17103 begin_explicit_instantiation ();
17104 /* [temp.explicit] says that we are supposed to ignore access
17105 control while processing explicit instantiation directives. */
17106 push_deferring_access_checks (dk_no_check
);
17107 /* Parse a decl-specifier-seq. */
17108 cp_parser_decl_specifier_seq (parser
,
17109 CP_PARSER_FLAGS_OPTIONAL
,
17111 &declares_class_or_enum
);
17112 /* If there was exactly one decl-specifier, and it declared a class,
17113 and there's no declarator, then we have an explicit type
17115 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
17119 type
= check_tag_decl (&decl_specifiers
,
17120 /*explicit_type_instantiation_p=*/true);
17121 /* Turn access control back on for names used during
17122 template instantiation. */
17123 pop_deferring_access_checks ();
17125 do_type_instantiation (type
, extension_specifier
,
17126 /*complain=*/tf_error
);
17130 cp_declarator
*declarator
;
17133 /* Parse the declarator. */
17135 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17136 CP_PARSER_FLAGS_NONE
,
17137 /*ctor_dtor_or_conv_p=*/NULL
,
17138 /*parenthesized_p=*/NULL
,
17139 /*member_p=*/false,
17140 /*friend_p=*/false,
17141 /*static_p=*/false);
17142 if (declares_class_or_enum
& 2)
17143 cp_parser_check_for_definition_in_return_type (declarator
,
17144 decl_specifiers
.type
,
17145 decl_specifiers
.locations
[ds_type_spec
]);
17146 if (declarator
!= cp_error_declarator
)
17148 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
17149 permerror (decl_specifiers
.locations
[ds_inline
],
17150 "explicit instantiation shall not use"
17151 " %<inline%> specifier");
17152 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
17153 permerror (decl_specifiers
.locations
[ds_constexpr
],
17154 "explicit instantiation shall not use"
17155 " %<constexpr%> specifier");
17157 decl
= grokdeclarator (declarator
, &decl_specifiers
,
17158 NORMAL
, 0, &decl_specifiers
.attributes
);
17159 /* Turn access control back on for names used during
17160 template instantiation. */
17161 pop_deferring_access_checks ();
17162 /* Do the explicit instantiation. */
17163 do_decl_instantiation (decl
, extension_specifier
);
17167 pop_deferring_access_checks ();
17168 /* Skip the body of the explicit instantiation. */
17169 cp_parser_skip_to_end_of_statement (parser
);
17172 /* We're done with the instantiation. */
17173 end_explicit_instantiation ();
17175 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17177 timevar_pop (TV_TEMPLATE_INST
);
17180 /* Parse an explicit-specialization.
17182 explicit-specialization:
17183 template < > declaration
17185 Although the standard says `declaration', what it really means is:
17187 explicit-specialization:
17188 template <> decl-specifier [opt] init-declarator [opt] ;
17189 template <> function-definition
17190 template <> explicit-specialization
17191 template <> template-declaration */
17194 cp_parser_explicit_specialization (cp_parser
* parser
)
17196 bool need_lang_pop
;
17197 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17199 /* Look for the `template' keyword. */
17200 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17201 /* Look for the `<'. */
17202 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
17203 /* Look for the `>'. */
17204 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
17205 /* We have processed another parameter list. */
17206 ++parser
->num_template_parameter_lists
;
17209 A template ... explicit specialization ... shall not have C
17211 if (current_lang_name
== lang_name_c
)
17213 error_at (token
->location
, "template specialization with C linkage");
17214 maybe_show_extern_c_location ();
17215 /* Give it C++ linkage to avoid confusing other parts of the
17217 push_lang_context (lang_name_cplusplus
);
17218 need_lang_pop
= true;
17221 need_lang_pop
= false;
17222 /* Let the front end know that we are beginning a specialization. */
17223 if (!begin_specialization ())
17225 end_specialization ();
17229 /* If the next keyword is `template', we need to figure out whether
17230 or not we're looking a template-declaration. */
17231 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17233 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17234 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
17235 cp_parser_template_declaration_after_export (parser
,
17236 /*member_p=*/false);
17238 cp_parser_explicit_specialization (parser
);
17241 /* Parse the dependent declaration. */
17242 cp_parser_single_declaration (parser
,
17244 /*member_p=*/false,
17245 /*explicit_specialization_p=*/true,
17246 /*friend_p=*/NULL
);
17247 /* We're done with the specialization. */
17248 end_specialization ();
17249 /* For the erroneous case of a template with C linkage, we pushed an
17250 implicit C++ linkage scope; exit that scope now. */
17252 pop_lang_context ();
17253 /* We're done with this parameter list. */
17254 --parser
->num_template_parameter_lists
;
17257 /* Parse a type-specifier.
17260 simple-type-specifier
17263 elaborated-type-specifier
17271 Returns a representation of the type-specifier. For a
17272 class-specifier, enum-specifier, or elaborated-type-specifier, a
17273 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17275 The parser flags FLAGS is used to control type-specifier parsing.
17277 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17278 in a decl-specifier-seq.
17280 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17281 class-specifier, enum-specifier, or elaborated-type-specifier, then
17282 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17283 if a type is declared; 2 if it is defined. Otherwise, it is set to
17286 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17287 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17288 is set to FALSE. */
17291 cp_parser_type_specifier (cp_parser
* parser
,
17292 cp_parser_flags flags
,
17293 cp_decl_specifier_seq
*decl_specs
,
17294 bool is_declaration
,
17295 int* declares_class_or_enum
,
17296 bool* is_cv_qualifier
)
17298 tree type_spec
= NULL_TREE
;
17301 cp_decl_spec ds
= ds_last
;
17303 /* Assume this type-specifier does not declare a new type. */
17304 if (declares_class_or_enum
)
17305 *declares_class_or_enum
= 0;
17306 /* And that it does not specify a cv-qualifier. */
17307 if (is_cv_qualifier
)
17308 *is_cv_qualifier
= false;
17309 /* Peek at the next token. */
17310 token
= cp_lexer_peek_token (parser
->lexer
);
17312 /* If we're looking at a keyword, we can use that to guide the
17313 production we choose. */
17314 keyword
= token
->keyword
;
17318 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17319 goto elaborated_type_specifier
;
17321 /* Look for the enum-specifier. */
17322 type_spec
= cp_parser_enum_specifier (parser
);
17323 /* If that worked, we're done. */
17326 if (declares_class_or_enum
)
17327 *declares_class_or_enum
= 2;
17329 cp_parser_set_decl_spec_type (decl_specs
,
17332 /*type_definition_p=*/true);
17336 goto elaborated_type_specifier
;
17338 /* Any of these indicate either a class-specifier, or an
17339 elaborated-type-specifier. */
17343 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17344 goto elaborated_type_specifier
;
17346 /* Parse tentatively so that we can back up if we don't find a
17347 class-specifier. */
17348 cp_parser_parse_tentatively (parser
);
17349 /* Look for the class-specifier. */
17350 type_spec
= cp_parser_class_specifier (parser
);
17351 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17352 /* If that worked, we're done. */
17353 if (cp_parser_parse_definitely (parser
))
17355 if (declares_class_or_enum
)
17356 *declares_class_or_enum
= 2;
17358 cp_parser_set_decl_spec_type (decl_specs
,
17361 /*type_definition_p=*/true);
17365 /* Fall through. */
17366 elaborated_type_specifier
:
17367 /* We're declaring (not defining) a class or enum. */
17368 if (declares_class_or_enum
)
17369 *declares_class_or_enum
= 1;
17371 /* Fall through. */
17373 /* Look for an elaborated-type-specifier. */
17375 = (cp_parser_elaborated_type_specifier
17377 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17380 cp_parser_set_decl_spec_type (decl_specs
,
17383 /*type_definition_p=*/false);
17388 if (is_cv_qualifier
)
17389 *is_cv_qualifier
= true;
17394 if (is_cv_qualifier
)
17395 *is_cv_qualifier
= true;
17400 if (is_cv_qualifier
)
17401 *is_cv_qualifier
= true;
17405 /* The `__complex__' keyword is a GNU extension. */
17413 /* Handle simple keywords. */
17418 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17419 decl_specs
->any_specifiers_p
= true;
17421 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17424 /* If we do not already have a type-specifier, assume we are looking
17425 at a simple-type-specifier. */
17426 type_spec
= cp_parser_simple_type_specifier (parser
,
17430 /* If we didn't find a type-specifier, and a type-specifier was not
17431 optional in this context, issue an error message. */
17432 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17434 cp_parser_error (parser
, "expected type specifier");
17435 return error_mark_node
;
17441 /* Parse a simple-type-specifier.
17443 simple-type-specifier:
17444 :: [opt] nested-name-specifier [opt] type-name
17445 :: [opt] nested-name-specifier template template-id
17460 simple-type-specifier:
17462 decltype ( expression )
17465 __underlying_type ( type-id )
17469 nested-name-specifier(opt) template-name
17473 simple-type-specifier:
17475 __typeof__ unary-expression
17476 __typeof__ ( type-id )
17477 __typeof__ ( type-id ) { initializer-list , [opt] }
17479 Concepts Extension:
17481 simple-type-specifier:
17482 constrained-type-specifier
17484 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17485 appropriately updated. */
17488 cp_parser_simple_type_specifier (cp_parser
* parser
,
17489 cp_decl_specifier_seq
*decl_specs
,
17490 cp_parser_flags flags
)
17492 tree type
= NULL_TREE
;
17496 /* Peek at the next token. */
17497 token
= cp_lexer_peek_token (parser
->lexer
);
17499 /* If we're looking at a keyword, things are easy. */
17500 switch (token
->keyword
)
17504 decl_specs
->explicit_char_p
= true;
17505 type
= char_type_node
;
17508 type
= char16_type_node
;
17511 type
= char32_type_node
;
17514 type
= wchar_type_node
;
17517 type
= boolean_type_node
;
17520 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17521 type
= short_integer_type_node
;
17525 decl_specs
->explicit_int_p
= true;
17526 type
= integer_type_node
;
17532 idx
= token
->keyword
- RID_INT_N_0
;
17533 if (! int_n_enabled_p
[idx
])
17537 decl_specs
->explicit_intN_p
= true;
17538 decl_specs
->int_n_idx
= idx
;
17540 type
= int_n_trees
[idx
].signed_type
;
17544 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17545 type
= long_integer_type_node
;
17548 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17549 type
= integer_type_node
;
17552 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17553 type
= unsigned_type_node
;
17556 type
= float_type_node
;
17559 type
= double_type_node
;
17562 type
= void_type_node
;
17566 maybe_warn_cpp0x (CPP0X_AUTO
);
17567 if (parser
->auto_is_implicit_function_template_parm_p
)
17569 /* The 'auto' might be the placeholder return type for a function decl
17570 with trailing return type. */
17571 bool have_trailing_return_fn_decl
= false;
17573 cp_parser_parse_tentatively (parser
);
17574 cp_lexer_consume_token (parser
->lexer
);
17575 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17576 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17577 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17578 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17580 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17582 cp_lexer_consume_token (parser
->lexer
);
17583 cp_parser_skip_to_closing_parenthesis (parser
,
17584 /*recovering*/false,
17586 /*consume_paren*/true);
17590 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17592 have_trailing_return_fn_decl
= true;
17596 cp_lexer_consume_token (parser
->lexer
);
17598 cp_parser_abort_tentative_parse (parser
);
17600 if (have_trailing_return_fn_decl
)
17602 type
= make_auto ();
17606 if (cxx_dialect
>= cxx14
)
17608 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17609 type
= TREE_TYPE (type
);
17612 type
= error_mark_node
;
17614 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17616 if (cxx_dialect
< cxx14
)
17617 error_at (token
->location
,
17618 "use of %<auto%> in lambda parameter declaration "
17619 "only available with "
17620 "-std=c++14 or -std=gnu++14");
17622 else if (cxx_dialect
< cxx14
)
17623 error_at (token
->location
,
17624 "use of %<auto%> in parameter declaration "
17625 "only available with "
17626 "-std=c++14 or -std=gnu++14");
17627 else if (!flag_concepts
)
17628 pedwarn (token
->location
, 0,
17629 "use of %<auto%> in parameter declaration "
17630 "only available with -fconcepts");
17633 type
= make_auto ();
17637 /* Since DR 743, decltype can either be a simple-type-specifier by
17638 itself or begin a nested-name-specifier. Parsing it will replace
17639 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17640 handling below decide what to do. */
17641 cp_parser_decltype (parser
);
17642 cp_lexer_set_token_position (parser
->lexer
, token
);
17646 /* Consume the `typeof' token. */
17647 cp_lexer_consume_token (parser
->lexer
);
17648 /* Parse the operand to `typeof'. */
17649 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17650 /* If it is not already a TYPE, take its type. */
17651 if (!TYPE_P (type
))
17652 type
= finish_typeof (type
);
17655 cp_parser_set_decl_spec_type (decl_specs
, type
,
17657 /*type_definition_p=*/false);
17661 case RID_UNDERLYING_TYPE
:
17662 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17664 cp_parser_set_decl_spec_type (decl_specs
, type
,
17666 /*type_definition_p=*/false);
17671 case RID_DIRECT_BASES
:
17672 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17674 cp_parser_set_decl_spec_type (decl_specs
, type
,
17676 /*type_definition_p=*/false);
17682 /* If token is an already-parsed decltype not followed by ::,
17683 it's a simple-type-specifier. */
17684 if (token
->type
== CPP_DECLTYPE
17685 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17687 type
= saved_checks_value (token
->u
.tree_check_value
);
17690 cp_parser_set_decl_spec_type (decl_specs
, type
,
17692 /*type_definition_p=*/false);
17693 /* Remember that we are handling a decltype in order to
17694 implement the resolution of DR 1510 when the argument
17695 isn't instantiation dependent. */
17696 decl_specs
->decltype_p
= true;
17698 cp_lexer_consume_token (parser
->lexer
);
17702 /* If the type-specifier was for a built-in type, we're done. */
17705 /* Record the type. */
17707 && (token
->keyword
!= RID_SIGNED
17708 && token
->keyword
!= RID_UNSIGNED
17709 && token
->keyword
!= RID_SHORT
17710 && token
->keyword
!= RID_LONG
))
17711 cp_parser_set_decl_spec_type (decl_specs
,
17714 /*type_definition_p=*/false);
17716 decl_specs
->any_specifiers_p
= true;
17718 /* Consume the token. */
17719 cp_lexer_consume_token (parser
->lexer
);
17721 if (type
== error_mark_node
)
17722 return error_mark_node
;
17724 /* There is no valid C++ program where a non-template type is
17725 followed by a "<". That usually indicates that the user thought
17726 that the type was a template. */
17727 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17730 return TYPE_NAME (type
);
17733 /* The type-specifier must be a user-defined type. */
17734 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17738 const bool typename_p
= (cxx_dialect
>= cxx2a
17739 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
17741 /* Don't gobble tokens or issue error messages if this is an
17742 optional type-specifier. */
17743 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17744 cp_parser_parse_tentatively (parser
);
17746 token
= cp_lexer_peek_token (parser
->lexer
);
17748 /* Look for the optional `::' operator. */
17750 = (cp_parser_global_scope_opt (parser
,
17751 /*current_scope_valid_p=*/false)
17753 /* Look for the nested-name specifier. */
17755 = (cp_parser_nested_name_specifier_opt (parser
,
17756 /*typename_keyword_p=*/false,
17757 /*check_dependency_p=*/true,
17759 /*is_declaration=*/false)
17761 /* If we have seen a nested-name-specifier, and the next token
17762 is `template', then we are using the template-id production. */
17764 && cp_parser_optional_template_keyword (parser
))
17766 /* Look for the template-id. */
17767 type
= cp_parser_template_id (parser
,
17768 /*template_keyword_p=*/true,
17769 /*check_dependency_p=*/true,
17771 /*is_declaration=*/false);
17772 /* If the template-id did not name a type, we are out of
17774 if (TREE_CODE (type
) != TYPE_DECL
)
17776 /* ...unless we pretend we have seen 'typename'. */
17778 type
= cp_parser_make_typename_type (parser
, type
,
17782 cp_parser_error (parser
, "expected template-id for type");
17787 /* Otherwise, look for a type-name. */
17789 type
= cp_parser_type_name (parser
, (qualified_p
&& typename_p
));
17791 /* Keep track of all name-lookups performed in class scopes. */
17795 && TREE_CODE (type
) == TYPE_DECL
17796 && identifier_p (DECL_NAME (type
)))
17797 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17798 /* If it didn't work out, we don't have a TYPE. */
17799 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17800 && !cp_parser_parse_definitely (parser
))
17802 if (!type
&& cxx_dialect
>= cxx17
)
17804 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17805 cp_parser_parse_tentatively (parser
);
17807 cp_parser_global_scope_opt (parser
,
17808 /*current_scope_valid_p=*/false);
17809 cp_parser_nested_name_specifier_opt (parser
,
17810 /*typename_keyword_p=*/false,
17811 /*check_dependency_p=*/true,
17813 /*is_declaration=*/false);
17814 tree name
= cp_parser_identifier (parser
);
17815 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17816 && parser
->scope
!= error_mark_node
)
17818 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17820 /*is_template=*/false,
17821 /*is_namespace=*/false,
17822 /*check_dependency=*/true,
17823 /*ambiguous_decls=*/NULL
,
17825 if (tmpl
&& tmpl
!= error_mark_node
17826 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17827 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17828 type
= make_template_placeholder (tmpl
);
17831 type
= error_mark_node
;
17832 if (!cp_parser_simulate_error (parser
))
17833 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17834 NLE_TYPE
, token
->location
);
17838 type
= error_mark_node
;
17840 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17841 && !cp_parser_parse_definitely (parser
))
17844 if (type
&& decl_specs
)
17845 cp_parser_set_decl_spec_type (decl_specs
, type
,
17847 /*type_definition_p=*/false);
17850 /* If we didn't get a type-name, issue an error message. */
17851 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17853 cp_parser_error (parser
, "expected type-name");
17854 return error_mark_node
;
17857 if (type
&& type
!= error_mark_node
)
17859 /* See if TYPE is an Objective-C type, and if so, parse and
17860 accept any protocol references following it. Do this before
17861 the cp_parser_check_for_invalid_template_id() call, because
17862 Objective-C types can be followed by '<...>' which would
17863 enclose protocol names rather than template arguments, and so
17864 everything is fine. */
17865 if (c_dialect_objc () && !parser
->scope
17866 && (objc_is_id (type
) || objc_is_class_name (type
)))
17868 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17869 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17871 /* Clobber the "unqualified" type previously entered into
17872 DECL_SPECS with the new, improved protocol-qualified version. */
17874 decl_specs
->type
= qual_type
;
17879 /* There is no valid C++ program where a non-template type is
17880 followed by a "<". That usually indicates that the user
17881 thought that the type was a template. */
17882 cp_parser_check_for_invalid_template_id (parser
, type
,
17890 /* Parse a type-name.
17896 simple-template-id [in c++0x]
17913 Returns a TYPE_DECL for the type. */
17916 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17920 /* We can't know yet whether it is a class-name or not. */
17921 cp_parser_parse_tentatively (parser
);
17922 /* Try a class-name. */
17923 type_decl
= cp_parser_class_name (parser
,
17924 typename_keyword_p
,
17925 /*template_keyword_p=*/false,
17927 /*check_dependency_p=*/true,
17928 /*class_head_p=*/false,
17929 /*is_declaration=*/false);
17930 /* If it's not a class-name, keep looking. */
17931 if (!cp_parser_parse_definitely (parser
))
17933 if (cxx_dialect
< cxx11
)
17934 /* It must be a typedef-name or an enum-name. */
17935 return cp_parser_nonclass_name (parser
);
17937 cp_parser_parse_tentatively (parser
);
17938 /* It is either a simple-template-id representing an
17939 instantiation of an alias template... */
17940 type_decl
= cp_parser_template_id (parser
,
17941 /*template_keyword_p=*/false,
17942 /*check_dependency_p=*/true,
17944 /*is_declaration=*/false);
17945 /* Note that this must be an instantiation of an alias template
17946 because [temp.names]/6 says:
17948 A template-id that names an alias template specialization
17951 Whereas [temp.names]/7 says:
17953 A simple-template-id that names a class template
17954 specialization is a class-name.
17956 With concepts, this could also be a partial-concept-id that
17957 declares a non-type template parameter. */
17958 if (type_decl
!= NULL_TREE
17959 && TREE_CODE (type_decl
) == TYPE_DECL
17960 && TYPE_DECL_ALIAS_P (type_decl
))
17961 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17962 else if (is_constrained_parameter (type_decl
))
17963 /* Don't do anything. */ ;
17965 cp_parser_simulate_error (parser
);
17967 if (!cp_parser_parse_definitely (parser
))
17968 /* ... Or a typedef-name or an enum-name. */
17969 return cp_parser_nonclass_name (parser
);
17975 /* Check if DECL and ARGS can form a constrained-type-specifier.
17976 If ARGS is non-null, we try to form a concept check of the
17977 form DECL<?, ARGS> where ? is a wildcard that matches any
17978 kind of template argument. If ARGS is NULL, then we try to
17979 form a concept check of the form DECL<?>. */
17982 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17983 tree decl
, tree args
)
17985 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17987 /* If we a constrained-type-specifier cannot be deduced. */
17988 if (parser
->prevent_constrained_type_specifiers
)
17991 /* A constrained type specifier can only be found in an
17992 overload set or as a reference to a template declaration.
17994 FIXME: This might be masking a bug. It's possible that
17995 that the deduction below is causing template specializations
17996 to be formed with the wildcard as an argument. */
17997 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
18000 /* Try to build a call expression that evaluates the
18001 concept. This can fail if the overload set refers
18002 only to non-templates. */
18003 tree placeholder
= build_nt (WILDCARD_DECL
);
18004 tree check
= build_concept_check (decl
, placeholder
, args
);
18005 if (check
== error_mark_node
)
18008 /* Deduce the checked constraint and the prototype parameter.
18010 FIXME: In certain cases, failure to deduce should be a
18011 diagnosable error. */
18014 if (!deduce_constrained_parameter (check
, conc
, proto
))
18017 /* In template parameter scope, this results in a constrained
18018 parameter. Return a descriptor of that parm. */
18019 if (processing_template_parmlist
)
18020 return build_constrained_parameter (conc
, proto
, args
);
18022 /* In a parameter-declaration-clause, constrained-type
18023 specifiers result in invented template parameters. */
18024 if (parser
->auto_is_implicit_function_template_parm_p
)
18026 tree x
= build_constrained_parameter (conc
, proto
, args
);
18027 return synthesize_implicit_template_parm (parser
, x
);
18031 /* Otherwise, we're in a context where the constrained
18032 type name is deduced and the constraint applies
18033 after deduction. */
18034 return make_constrained_auto (conc
, args
);
18040 /* If DECL refers to a concept, return a TYPE_DECL representing
18041 the result of using the constrained type specifier in the
18042 current context. DECL refers to a concept if
18044 - it is an overload set containing a function concept taking a single
18047 - it is a variable concept taking a single type argument. */
18050 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
18053 && (TREE_CODE (decl
) == OVERLOAD
18054 || BASELINK_P (decl
)
18055 || variable_concept_p (decl
)))
18056 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
18061 /* Check if DECL and ARGS form a partial-concept-id. If so,
18062 assign ID to the resulting constrained placeholder.
18064 Returns true if the partial-concept-id designates a placeholder
18065 and false otherwise. Note that *id is set to NULL_TREE in
18069 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
18071 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
18074 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18086 Returns a TYPE_DECL for the type. */
18089 cp_parser_nonclass_name (cp_parser
* parser
)
18094 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18095 identifier
= cp_parser_identifier (parser
);
18096 if (identifier
== error_mark_node
)
18097 return error_mark_node
;
18099 /* Look up the type-name. */
18100 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
18102 type_decl
= strip_using_decl (type_decl
);
18104 /* If we found an overload set, then it may refer to a concept-name. */
18105 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
18108 if (TREE_CODE (type_decl
) != TYPE_DECL
18109 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
18111 /* See if this is an Objective-C type. */
18112 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18113 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
18115 type_decl
= TYPE_NAME (type
);
18118 /* Issue an error if we did not find a type-name. */
18119 if (TREE_CODE (type_decl
) != TYPE_DECL
18120 /* In Objective-C, we have the complication that class names are
18121 normally type names and start declarations (eg, the
18122 "NSObject" in "NSObject *object;"), but can be used in an
18123 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18124 is an expression. So, a classname followed by a dot is not a
18125 valid type-name. */
18126 || (objc_is_class_name (TREE_TYPE (type_decl
))
18127 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
18129 if (!cp_parser_simulate_error (parser
))
18130 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
18131 NLE_TYPE
, token
->location
);
18132 return error_mark_node
;
18134 /* Remember that the name was used in the definition of the
18135 current class so that we can check later to see if the
18136 meaning would have been different after the class was
18137 entirely defined. */
18138 else if (type_decl
!= error_mark_node
18140 maybe_note_name_used_in_class (identifier
, type_decl
);
18145 /* Parse an elaborated-type-specifier. Note that the grammar given
18146 here incorporates the resolution to DR68.
18148 elaborated-type-specifier:
18149 class-key :: [opt] nested-name-specifier [opt] identifier
18150 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18151 enum-key :: [opt] nested-name-specifier [opt] identifier
18152 typename :: [opt] nested-name-specifier identifier
18153 typename :: [opt] nested-name-specifier template [opt]
18158 elaborated-type-specifier:
18159 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18160 class-key attributes :: [opt] nested-name-specifier [opt]
18161 template [opt] template-id
18162 enum attributes :: [opt] nested-name-specifier [opt] identifier
18164 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18165 declared `friend'. If IS_DECLARATION is TRUE, then this
18166 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18167 something is being declared.
18169 Returns the TYPE specified. */
18172 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
18174 bool is_declaration
)
18176 enum tag_types tag_type
;
18178 tree type
= NULL_TREE
;
18179 tree attributes
= NULL_TREE
;
18181 cp_token
*token
= NULL
;
18183 /* See if we're looking at the `enum' keyword. */
18184 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
18186 /* Consume the `enum' token. */
18187 cp_lexer_consume_token (parser
->lexer
);
18188 /* Remember that it's an enumeration type. */
18189 tag_type
= enum_type
;
18190 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18191 enums) is used here. */
18192 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18193 if (cp_parser_is_keyword (token
, RID_CLASS
)
18194 || cp_parser_is_keyword (token
, RID_STRUCT
))
18196 gcc_rich_location
richloc (token
->location
);
18197 richloc
.add_range (input_location
);
18198 richloc
.add_fixit_remove ();
18199 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
18200 "a scoped enum must not use the %qD keyword",
18202 /* Consume the `struct' or `class' and parse it anyway. */
18203 cp_lexer_consume_token (parser
->lexer
);
18205 /* Parse the attributes. */
18206 attributes
= cp_parser_attributes_opt (parser
);
18208 /* Or, it might be `typename'. */
18209 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
18212 /* Consume the `typename' token. */
18213 cp_lexer_consume_token (parser
->lexer
);
18214 /* Remember that it's a `typename' type. */
18215 tag_type
= typename_type
;
18217 /* Otherwise it must be a class-key. */
18220 tag_type
= cp_parser_class_key (parser
);
18221 if (tag_type
== none_type
)
18222 return error_mark_node
;
18223 /* Parse the attributes. */
18224 attributes
= cp_parser_attributes_opt (parser
);
18227 /* Look for the `::' operator. */
18228 globalscope
= cp_parser_global_scope_opt (parser
,
18229 /*current_scope_valid_p=*/false);
18230 /* Look for the nested-name-specifier. */
18231 tree nested_name_specifier
;
18232 if (tag_type
== typename_type
&& !globalscope
)
18234 nested_name_specifier
18235 = cp_parser_nested_name_specifier (parser
,
18236 /*typename_keyword_p=*/true,
18237 /*check_dependency_p=*/true,
18240 if (!nested_name_specifier
)
18241 return error_mark_node
;
18244 /* Even though `typename' is not present, the proposed resolution
18245 to Core Issue 180 says that in `class A<T>::B', `B' should be
18246 considered a type-name, even if `A<T>' is dependent. */
18247 nested_name_specifier
18248 = cp_parser_nested_name_specifier_opt (parser
,
18249 /*typename_keyword_p=*/true,
18250 /*check_dependency_p=*/true,
18253 /* For everything but enumeration types, consider a template-id.
18254 For an enumeration type, consider only a plain identifier. */
18255 if (tag_type
!= enum_type
)
18257 bool template_p
= false;
18260 /* Allow the `template' keyword. */
18261 template_p
= cp_parser_optional_template_keyword (parser
);
18262 /* If we didn't see `template', we don't know if there's a
18263 template-id or not. */
18265 cp_parser_parse_tentatively (parser
);
18266 /* The `template' keyword must follow a nested-name-specifier. */
18267 else if (!nested_name_specifier
)
18269 cp_parser_error (parser
, "%<template%> must follow a nested-"
18271 return error_mark_node
;
18274 /* Parse the template-id. */
18275 token
= cp_lexer_peek_token (parser
->lexer
);
18276 decl
= cp_parser_template_id (parser
, template_p
,
18277 /*check_dependency_p=*/true,
18280 /* If we didn't find a template-id, look for an ordinary
18282 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18284 /* We can get here when cp_parser_template_id, called by
18285 cp_parser_class_name with tag_type == none_type, succeeds
18286 and caches a BASELINK. Then, when called again here,
18287 instead of failing and returning an error_mark_node
18288 returns it (see template/typename17.C in C++11).
18289 ??? Could we diagnose this earlier? */
18290 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18292 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18293 type
= error_mark_node
;
18295 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18296 in effect, then we must assume that, upon instantiation, the
18297 template will correspond to a class. */
18298 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18299 && tag_type
== typename_type
)
18300 type
= make_typename_type (parser
->scope
, decl
,
18302 /*complain=*/tf_error
);
18303 /* If the `typename' keyword is in effect and DECL is not a type
18304 decl, then type is non existent. */
18305 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18307 else if (TREE_CODE (decl
) == TYPE_DECL
)
18309 type
= check_elaborated_type_specifier (tag_type
, decl
,
18310 /*allow_template_p=*/true);
18312 /* If the next token is a semicolon, this must be a specialization,
18313 instantiation, or friend declaration. Check the scope while we
18314 still know whether or not we had a nested-name-specifier. */
18315 if (type
!= error_mark_node
18316 && !nested_name_specifier
&& !is_friend
18317 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18318 check_unqualified_spec_or_inst (type
, token
->location
);
18320 else if (decl
== error_mark_node
)
18321 type
= error_mark_node
;
18326 token
= cp_lexer_peek_token (parser
->lexer
);
18327 identifier
= cp_parser_identifier (parser
);
18329 if (identifier
== error_mark_node
)
18331 parser
->scope
= NULL_TREE
;
18332 return error_mark_node
;
18335 /* For a `typename', we needn't call xref_tag. */
18336 if (tag_type
== typename_type
18337 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18338 return cp_parser_make_typename_type (parser
, identifier
,
18341 /* Template parameter lists apply only if we are not within a
18342 function parameter list. */
18343 bool template_parm_lists_apply
18344 = parser
->num_template_parameter_lists
;
18345 if (template_parm_lists_apply
)
18346 for (cp_binding_level
*s
= current_binding_level
;
18347 s
&& s
->kind
!= sk_template_parms
;
18348 s
= s
->level_chain
)
18349 if (s
->kind
== sk_function_parms
)
18350 template_parm_lists_apply
= false;
18352 /* Look up a qualified name in the usual way. */
18356 tree ambiguous_decls
;
18358 decl
= cp_parser_lookup_name (parser
, identifier
,
18360 /*is_template=*/false,
18361 /*is_namespace=*/false,
18362 /*check_dependency=*/true,
18366 /* If the lookup was ambiguous, an error will already have been
18368 if (ambiguous_decls
)
18369 return error_mark_node
;
18371 /* If we are parsing friend declaration, DECL may be a
18372 TEMPLATE_DECL tree node here. However, we need to check
18373 whether this TEMPLATE_DECL results in valid code. Consider
18374 the following example:
18377 template <class T> class C {};
18380 template <class T> friend class N::C; // #1, valid code
18382 template <class T> class Y {
18383 friend class N::C; // #2, invalid code
18386 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18387 name lookup of `N::C'. We see that friend declaration must
18388 be template for the code to be valid. Note that
18389 processing_template_decl does not work here since it is
18390 always 1 for the above two cases. */
18392 decl
= (cp_parser_maybe_treat_template_as_class
18393 (decl
, /*tag_name_p=*/is_friend
18394 && template_parm_lists_apply
));
18396 if (TREE_CODE (decl
) != TYPE_DECL
)
18398 cp_parser_diagnose_invalid_type_name (parser
,
18401 return error_mark_node
;
18404 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18406 bool allow_template
= (template_parm_lists_apply
18407 || DECL_SELF_REFERENCE_P (decl
));
18408 type
= check_elaborated_type_specifier (tag_type
, decl
,
18411 if (type
== error_mark_node
)
18412 return error_mark_node
;
18415 /* Forward declarations of nested types, such as
18420 are invalid unless all components preceding the final '::'
18421 are complete. If all enclosing types are complete, these
18422 declarations become merely pointless.
18424 Invalid forward declarations of nested types are errors
18425 caught elsewhere in parsing. Those that are pointless arrive
18428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18429 && !is_friend
&& !processing_explicit_instantiation
)
18430 warning (0, "declaration %qD does not declare anything", decl
);
18432 type
= TREE_TYPE (decl
);
18436 /* An elaborated-type-specifier sometimes introduces a new type and
18437 sometimes names an existing type. Normally, the rule is that it
18438 introduces a new type only if there is not an existing type of
18439 the same name already in scope. For example, given:
18442 void f() { struct S s; }
18444 the `struct S' in the body of `f' is the same `struct S' as in
18445 the global scope; the existing definition is used. However, if
18446 there were no global declaration, this would introduce a new
18447 local class named `S'.
18449 An exception to this rule applies to the following code:
18451 namespace N { struct S; }
18453 Here, the elaborated-type-specifier names a new type
18454 unconditionally; even if there is already an `S' in the
18455 containing scope this declaration names a new type.
18456 This exception only applies if the elaborated-type-specifier
18457 forms the complete declaration:
18461 A declaration consisting solely of `class-key identifier ;' is
18462 either a redeclaration of the name in the current scope or a
18463 forward declaration of the identifier as a class name. It
18464 introduces the name into the current scope.
18466 We are in this situation precisely when the next token is a `;'.
18468 An exception to the exception is that a `friend' declaration does
18469 *not* name a new type; i.e., given:
18471 struct S { friend struct T; };
18473 `T' is not a new type in the scope of `S'.
18475 Also, `new struct S' or `sizeof (struct S)' never results in the
18476 definition of a new type; a new type can only be declared in a
18477 declaration context. */
18483 /* Friends have special name lookup rules. */
18484 ts
= ts_within_enclosing_non_class
;
18485 else if (is_declaration
18486 && cp_lexer_next_token_is (parser
->lexer
,
18488 /* This is a `class-key identifier ;' */
18494 (template_parm_lists_apply
18495 && (cp_parser_next_token_starts_class_definition_p (parser
)
18496 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18497 /* An unqualified name was used to reference this type, so
18498 there were no qualifying templates. */
18499 if (template_parm_lists_apply
18500 && !cp_parser_check_template_parameters (parser
,
18501 /*num_templates=*/0,
18502 /*template_id*/false,
18504 /*declarator=*/NULL
))
18505 return error_mark_node
;
18506 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18510 if (type
== error_mark_node
)
18511 return error_mark_node
;
18513 /* Allow attributes on forward declarations of classes. */
18516 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18517 warning (OPT_Wattributes
,
18518 "attributes ignored on uninstantiated type");
18519 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18520 && ! processing_explicit_instantiation
)
18521 warning (OPT_Wattributes
,
18522 "attributes ignored on template instantiation");
18523 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18524 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18526 warning (OPT_Wattributes
,
18527 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18530 if (tag_type
!= enum_type
)
18532 /* Indicate whether this class was declared as a `class' or as a
18534 if (CLASS_TYPE_P (type
))
18535 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18536 cp_parser_check_class_key (tag_type
, type
);
18539 /* A "<" cannot follow an elaborated type specifier. If that
18540 happens, the user was probably trying to form a template-id. */
18541 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18547 /* Parse an enum-specifier.
18550 enum-head { enumerator-list [opt] }
18551 enum-head { enumerator-list , } [C++0x]
18554 enum-key identifier [opt] enum-base [opt]
18555 enum-key nested-name-specifier identifier enum-base [opt]
18560 enum struct [C++0x]
18563 : type-specifier-seq
18565 opaque-enum-specifier:
18566 enum-key identifier enum-base [opt] ;
18569 enum-key attributes[opt] identifier [opt] enum-base [opt]
18570 { enumerator-list [opt] }attributes[opt]
18571 enum-key attributes[opt] identifier [opt] enum-base [opt]
18572 { enumerator-list, }attributes[opt] [C++0x]
18574 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18575 if the token stream isn't an enum-specifier after all. */
18578 cp_parser_enum_specifier (cp_parser
* parser
)
18581 tree type
= NULL_TREE
;
18583 tree nested_name_specifier
= NULL_TREE
;
18585 bool scoped_enum_p
= false;
18586 bool has_underlying_type
= false;
18587 bool nested_being_defined
= false;
18588 bool new_value_list
= false;
18589 bool is_new_type
= false;
18590 bool is_unnamed
= false;
18591 tree underlying_type
= NULL_TREE
;
18592 cp_token
*type_start_token
= NULL
;
18593 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18595 parser
->colon_corrects_to_scope_p
= false;
18597 /* Parse tentatively so that we can back up if we don't find a
18599 cp_parser_parse_tentatively (parser
);
18601 /* Caller guarantees that the current token is 'enum', an identifier
18602 possibly follows, and the token after that is an opening brace.
18603 If we don't have an identifier, fabricate an anonymous name for
18604 the enumeration being defined. */
18605 cp_lexer_consume_token (parser
->lexer
);
18607 /* Parse the "class" or "struct", which indicates a scoped
18608 enumeration type in C++0x. */
18609 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18610 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18612 if (cxx_dialect
< cxx11
)
18613 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18615 /* Consume the `struct' or `class' token. */
18616 cp_lexer_consume_token (parser
->lexer
);
18618 scoped_enum_p
= true;
18621 attributes
= cp_parser_attributes_opt (parser
);
18623 /* Clear the qualification. */
18624 parser
->scope
= NULL_TREE
;
18625 parser
->qualifying_scope
= NULL_TREE
;
18626 parser
->object_scope
= NULL_TREE
;
18628 /* Figure out in what scope the declaration is being placed. */
18629 prev_scope
= current_scope ();
18631 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18633 push_deferring_access_checks (dk_no_check
);
18634 nested_name_specifier
18635 = cp_parser_nested_name_specifier_opt (parser
,
18636 /*typename_keyword_p=*/true,
18637 /*check_dependency_p=*/false,
18639 /*is_declaration=*/false);
18641 if (nested_name_specifier
)
18645 identifier
= cp_parser_identifier (parser
);
18646 name
= cp_parser_lookup_name (parser
, identifier
,
18648 /*is_template=*/false,
18649 /*is_namespace=*/false,
18650 /*check_dependency=*/true,
18651 /*ambiguous_decls=*/NULL
,
18653 if (name
&& name
!= error_mark_node
)
18655 type
= TREE_TYPE (name
);
18656 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18658 /* Are template enums allowed in ISO? */
18659 if (template_parm_scope_p ())
18660 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18661 "%qD is an enumeration template", name
);
18662 /* ignore a typename reference, for it will be solved by name
18667 else if (nested_name_specifier
== error_mark_node
)
18668 /* We already issued an error. */;
18671 error_at (type_start_token
->location
,
18672 "%qD does not name an enumeration in %qT",
18673 identifier
, nested_name_specifier
);
18674 nested_name_specifier
= error_mark_node
;
18679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18680 identifier
= cp_parser_identifier (parser
);
18683 identifier
= make_anon_name ();
18686 error_at (type_start_token
->location
,
18687 "unnamed scoped enum is not allowed");
18690 pop_deferring_access_checks ();
18692 /* Check for the `:' that denotes a specified underlying type in C++0x.
18693 Note that a ':' could also indicate a bitfield width, however. */
18694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18696 cp_decl_specifier_seq type_specifiers
;
18698 /* Consume the `:'. */
18699 cp_lexer_consume_token (parser
->lexer
);
18701 /* Parse the type-specifier-seq. */
18702 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
18703 /*is_declaration=*/false,
18704 /*is_trailing_return=*/false,
18707 /* At this point this is surely not elaborated type specifier. */
18708 if (!cp_parser_parse_definitely (parser
))
18711 if (cxx_dialect
< cxx11
)
18712 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18714 has_underlying_type
= true;
18716 /* If that didn't work, stop. */
18717 if (type_specifiers
.type
!= error_mark_node
)
18719 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18720 /*initialized=*/0, NULL
);
18721 if (underlying_type
== error_mark_node
18722 || check_for_bare_parameter_packs (underlying_type
))
18723 underlying_type
= NULL_TREE
;
18727 /* Look for the `{' but don't consume it yet. */
18728 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18730 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18732 cp_parser_error (parser
, "expected %<{%>");
18733 if (has_underlying_type
)
18739 /* An opaque-enum-specifier must have a ';' here. */
18740 if ((scoped_enum_p
|| underlying_type
)
18741 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18743 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18744 if (has_underlying_type
)
18752 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18755 if (nested_name_specifier
)
18757 if (CLASS_TYPE_P (nested_name_specifier
))
18759 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18760 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18761 push_scope (nested_name_specifier
);
18763 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18765 push_nested_namespace (nested_name_specifier
);
18769 /* Issue an error message if type-definitions are forbidden here. */
18770 if (!cp_parser_check_type_definition (parser
))
18771 type
= error_mark_node
;
18773 /* Create the new type. We do this before consuming the opening
18774 brace so the enum will be recorded as being on the line of its
18775 tag (or the 'enum' keyword, if there is no tag). */
18776 type
= start_enum (identifier
, type
, underlying_type
,
18777 attributes
, scoped_enum_p
, &is_new_type
);
18779 /* If the next token is not '{' it is an opaque-enum-specifier or an
18780 elaborated-type-specifier. */
18781 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18783 timevar_push (TV_PARSE_ENUM
);
18784 if (nested_name_specifier
18785 && nested_name_specifier
!= error_mark_node
)
18787 /* The following catches invalid code such as:
18788 enum class S<int>::E { A, B, C }; */
18789 if (!processing_specialization
18790 && CLASS_TYPE_P (nested_name_specifier
)
18791 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18792 error_at (type_start_token
->location
, "cannot add an enumerator "
18793 "list to a template instantiation");
18795 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18797 error_at (type_start_token
->location
,
18798 "%<%T::%E%> has not been declared",
18799 TYPE_CONTEXT (nested_name_specifier
),
18800 nested_name_specifier
);
18801 type
= error_mark_node
;
18803 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18804 && !CLASS_TYPE_P (nested_name_specifier
))
18806 error_at (type_start_token
->location
, "nested name specifier "
18807 "%qT for enum declaration does not name a class "
18808 "or namespace", nested_name_specifier
);
18809 type
= error_mark_node
;
18811 /* If that scope does not contain the scope in which the
18812 class was originally declared, the program is invalid. */
18813 else if (prev_scope
&& !is_ancestor (prev_scope
,
18814 nested_name_specifier
))
18816 if (at_namespace_scope_p ())
18817 error_at (type_start_token
->location
,
18818 "declaration of %qD in namespace %qD which does not "
18820 type
, prev_scope
, nested_name_specifier
);
18822 error_at (type_start_token
->location
,
18823 "declaration of %qD in %qD which does not "
18825 type
, prev_scope
, nested_name_specifier
);
18826 type
= error_mark_node
;
18828 /* If that scope is the scope where the declaration is being placed
18829 the program is invalid. */
18830 else if (CLASS_TYPE_P (nested_name_specifier
)
18831 && CLASS_TYPE_P (prev_scope
)
18832 && same_type_p (nested_name_specifier
, prev_scope
))
18834 permerror (type_start_token
->location
,
18835 "extra qualification not allowed");
18836 nested_name_specifier
= NULL_TREE
;
18841 begin_scope (sk_scoped_enum
, type
);
18843 /* Consume the opening brace. */
18844 matching_braces braces
;
18845 braces
.consume_open (parser
);
18847 if (type
== error_mark_node
)
18848 ; /* Nothing to add */
18849 else if (OPAQUE_ENUM_P (type
)
18850 || (cxx_dialect
> cxx98
&& processing_specialization
))
18852 new_value_list
= true;
18853 SET_OPAQUE_ENUM_P (type
, false);
18854 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18858 error_at (type_start_token
->location
,
18859 "multiple definition of %q#T", type
);
18860 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18861 "previous definition here");
18862 type
= error_mark_node
;
18865 if (type
== error_mark_node
)
18866 cp_parser_skip_to_end_of_block_or_statement (parser
);
18867 /* If the next token is not '}', then there are some enumerators. */
18868 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18870 if (is_unnamed
&& !scoped_enum_p
)
18871 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18872 "ISO C++ forbids empty unnamed enum");
18875 cp_parser_enumerator_list (parser
, type
);
18877 /* Consume the final '}'. */
18878 braces
.require_close (parser
);
18882 timevar_pop (TV_PARSE_ENUM
);
18886 /* If a ';' follows, then it is an opaque-enum-specifier
18887 and additional restrictions apply. */
18888 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18891 error_at (type_start_token
->location
,
18892 "opaque-enum-specifier without name");
18893 else if (nested_name_specifier
)
18894 error_at (type_start_token
->location
,
18895 "opaque-enum-specifier must use a simple identifier");
18899 /* Look for trailing attributes to apply to this enumeration, and
18900 apply them if appropriate. */
18901 if (cp_parser_allow_gnu_extensions_p (parser
))
18903 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18904 cplus_decl_attributes (&type
,
18906 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18909 /* Finish up the enumeration. */
18910 if (type
!= error_mark_node
)
18912 if (new_value_list
)
18913 finish_enum_value_list (type
);
18915 finish_enum (type
);
18918 if (nested_name_specifier
)
18920 if (CLASS_TYPE_P (nested_name_specifier
))
18922 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18923 pop_scope (nested_name_specifier
);
18925 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18927 pop_nested_namespace (nested_name_specifier
);
18931 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18935 /* Parse an enumerator-list. The enumerators all have the indicated
18939 enumerator-definition
18940 enumerator-list , enumerator-definition */
18943 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18947 /* Parse an enumerator-definition. */
18948 cp_parser_enumerator_definition (parser
, type
);
18950 /* If the next token is not a ',', we've reached the end of
18952 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18954 /* Otherwise, consume the `,' and keep going. */
18955 cp_lexer_consume_token (parser
->lexer
);
18956 /* If the next token is a `}', there is a trailing comma. */
18957 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18959 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18960 pedwarn (input_location
, OPT_Wpedantic
,
18961 "comma at end of enumerator list");
18967 /* Parse an enumerator-definition. The enumerator has the indicated
18970 enumerator-definition:
18972 enumerator = constant-expression
18979 enumerator-definition:
18980 enumerator attributes [opt]
18981 enumerator attributes [opt] = constant-expression */
18984 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18990 /* Save the input location because we are interested in the location
18991 of the identifier and not the location of the explicit value. */
18992 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18994 /* Look for the identifier. */
18995 identifier
= cp_parser_identifier (parser
);
18996 if (identifier
== error_mark_node
)
18999 /* Parse any specified attributes. */
19000 tree attrs
= cp_parser_attributes_opt (parser
);
19002 /* If the next token is an '=', then there is an explicit value. */
19003 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19005 /* Consume the `=' token. */
19006 cp_lexer_consume_token (parser
->lexer
);
19007 /* Parse the value. */
19008 value
= cp_parser_constant_expression (parser
);
19013 /* If we are processing a template, make sure the initializer of the
19014 enumerator doesn't contain any bare template parameter pack. */
19015 if (check_for_bare_parameter_packs (value
))
19016 value
= error_mark_node
;
19018 /* Create the enumerator. */
19019 build_enumerator (identifier
, value
, type
, attrs
, loc
);
19022 /* Parse a namespace-name.
19025 original-namespace-name
19028 Returns the NAMESPACE_DECL for the namespace. */
19031 cp_parser_namespace_name (cp_parser
* parser
)
19034 tree namespace_decl
;
19036 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19038 /* Get the name of the namespace. */
19039 identifier
= cp_parser_identifier (parser
);
19040 if (identifier
== error_mark_node
)
19041 return error_mark_node
;
19043 /* Look up the identifier in the currently active scope. Look only
19044 for namespaces, due to:
19046 [basic.lookup.udir]
19048 When looking up a namespace-name in a using-directive or alias
19049 definition, only namespace names are considered.
19053 [basic.lookup.qual]
19055 During the lookup of a name preceding the :: scope resolution
19056 operator, object, function, and enumerator names are ignored.
19058 (Note that cp_parser_qualifying_entity only calls this
19059 function if the token after the name is the scope resolution
19061 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
19063 /*is_template=*/false,
19064 /*is_namespace=*/true,
19065 /*check_dependency=*/true,
19066 /*ambiguous_decls=*/NULL
,
19068 /* If it's not a namespace, issue an error. */
19069 if (namespace_decl
== error_mark_node
19070 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
19072 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
19074 auto_diagnostic_group d
;
19076 if (namespace_decl
== error_mark_node
19077 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
19078 hint
= suggest_alternative_in_explicit_scope (token
->location
,
19081 if (const char *suggestion
= hint
.suggestion ())
19083 gcc_rich_location
richloc (token
->location
);
19084 richloc
.add_fixit_replace (suggestion
);
19085 error_at (&richloc
,
19086 "%qD is not a namespace-name; did you mean %qs?",
19087 identifier
, suggestion
);
19090 error_at (token
->location
, "%qD is not a namespace-name",
19094 cp_parser_error (parser
, "expected namespace-name");
19095 namespace_decl
= error_mark_node
;
19098 return namespace_decl
;
19101 /* Parse a namespace-definition.
19103 namespace-definition:
19104 named-namespace-definition
19105 unnamed-namespace-definition
19107 named-namespace-definition:
19108 original-namespace-definition
19109 extension-namespace-definition
19111 original-namespace-definition:
19112 namespace identifier { namespace-body }
19114 extension-namespace-definition:
19115 namespace original-namespace-name { namespace-body }
19117 unnamed-namespace-definition:
19118 namespace { namespace-body } */
19121 cp_parser_namespace_definition (cp_parser
* parser
)
19124 int nested_definition_count
= 0;
19126 cp_ensure_no_omp_declare_simd (parser
);
19127 cp_ensure_no_oacc_routine (parser
);
19129 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
19130 const bool topmost_inline_p
= is_inline
;
19134 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
19135 cp_lexer_consume_token (parser
->lexer
);
19138 /* Look for the `namespace' keyword. */
19140 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19142 /* Parse any specified attributes before the identifier. */
19143 tree attribs
= cp_parser_attributes_opt (parser
);
19147 identifier
= NULL_TREE
;
19149 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
19151 if (nested_inline_p
&& nested_definition_count
!= 0)
19153 if (cxx_dialect
< cxx2a
)
19154 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
19155 OPT_Wpedantic
, "nested inline namespace definitions only "
19156 "available with -std=c++2a or -std=gnu++2a");
19157 cp_lexer_consume_token (parser
->lexer
);
19160 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19162 identifier
= cp_parser_identifier (parser
);
19164 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19165 pedwarn (input_location
, OPT_Wpedantic
,
19166 "standard attributes on namespaces must precede "
19167 "the namespace name");
19169 /* Parse any attributes specified after the identifier. */
19170 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
19173 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
19175 /* Don't forget that the innermost namespace might have been
19176 marked as inline. Use |= because we cannot overwrite
19177 IS_INLINE in case the outermost namespace is inline, but
19178 there are no nested inlines. */
19179 is_inline
|= nested_inline_p
;
19183 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
19184 pedwarn (input_location
, OPT_Wpedantic
,
19185 "nested namespace definitions only available with "
19186 "-std=c++17 or -std=gnu++17");
19188 /* Nested namespace names can create new namespaces (unlike
19189 other qualified-ids). */
19190 if (int count
= (identifier
19191 ? push_namespace (identifier
, nested_inline_p
)
19193 nested_definition_count
+= count
;
19195 cp_parser_error (parser
, "nested namespace name required");
19196 cp_lexer_consume_token (parser
->lexer
);
19199 if (nested_definition_count
&& !identifier
)
19200 cp_parser_error (parser
, "namespace name required");
19202 if (nested_definition_count
&& attribs
)
19203 error_at (token
->location
,
19204 "a nested namespace definition cannot have attributes");
19205 if (nested_definition_count
&& topmost_inline_p
)
19206 error_at (token
->location
,
19207 "a nested namespace definition cannot be inline");
19209 /* Start the namespace. */
19210 nested_definition_count
+= push_namespace (identifier
, is_inline
);
19212 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
19214 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
19216 /* Look for the `{' to validate starting the namespace. */
19217 matching_braces braces
;
19218 if (braces
.require_open (parser
))
19220 /* Parse the body of the namespace. */
19221 cp_parser_namespace_body (parser
);
19223 /* Look for the final `}'. */
19224 braces
.require_close (parser
);
19227 if (has_visibility
)
19228 pop_visibility (1);
19230 /* Pop the nested namespace definitions. */
19231 while (nested_definition_count
--)
19235 /* Parse a namespace-body.
19238 declaration-seq [opt] */
19241 cp_parser_namespace_body (cp_parser
* parser
)
19243 cp_parser_declaration_seq_opt (parser
);
19246 /* Parse a namespace-alias-definition.
19248 namespace-alias-definition:
19249 namespace identifier = qualified-namespace-specifier ; */
19252 cp_parser_namespace_alias_definition (cp_parser
* parser
)
19255 tree namespace_specifier
;
19257 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19259 /* Look for the `namespace' keyword. */
19260 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19261 /* Look for the identifier. */
19262 identifier
= cp_parser_identifier (parser
);
19263 if (identifier
== error_mark_node
)
19265 /* Look for the `=' token. */
19266 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
19267 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19269 error_at (token
->location
, "%<namespace%> definition is not allowed here");
19270 /* Skip the definition. */
19271 cp_lexer_consume_token (parser
->lexer
);
19272 if (cp_parser_skip_to_closing_brace (parser
))
19273 cp_lexer_consume_token (parser
->lexer
);
19276 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19277 /* Look for the qualified-namespace-specifier. */
19278 namespace_specifier
19279 = cp_parser_qualified_namespace_specifier (parser
);
19280 /* Look for the `;' token. */
19281 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19283 /* Register the alias in the symbol table. */
19284 do_namespace_alias (identifier
, namespace_specifier
);
19287 /* Parse a qualified-namespace-specifier.
19289 qualified-namespace-specifier:
19290 :: [opt] nested-name-specifier [opt] namespace-name
19292 Returns a NAMESPACE_DECL corresponding to the specified
19296 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
19298 /* Look for the optional `::'. */
19299 cp_parser_global_scope_opt (parser
,
19300 /*current_scope_valid_p=*/false);
19302 /* Look for the optional nested-name-specifier. */
19303 cp_parser_nested_name_specifier_opt (parser
,
19304 /*typename_keyword_p=*/false,
19305 /*check_dependency_p=*/true,
19307 /*is_declaration=*/true);
19309 return cp_parser_namespace_name (parser
);
19312 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19313 access declaration.
19316 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19317 using :: unqualified-id ;
19319 access-declaration:
19325 cp_parser_using_declaration (cp_parser
* parser
,
19326 bool access_declaration_p
)
19329 bool typename_p
= false;
19330 bool global_scope_p
;
19334 int oldcount
= errorcount
;
19335 cp_token
*diag_token
= NULL
;
19337 if (access_declaration_p
)
19339 diag_token
= cp_lexer_peek_token (parser
->lexer
);
19340 cp_parser_parse_tentatively (parser
);
19344 /* Look for the `using' keyword. */
19345 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19348 /* Peek at the next token. */
19349 token
= cp_lexer_peek_token (parser
->lexer
);
19350 /* See if it's `typename'. */
19351 if (token
->keyword
== RID_TYPENAME
)
19353 /* Remember that we've seen it. */
19355 /* Consume the `typename' token. */
19356 cp_lexer_consume_token (parser
->lexer
);
19360 /* Look for the optional global scope qualification. */
19362 = (cp_parser_global_scope_opt (parser
,
19363 /*current_scope_valid_p=*/false)
19366 /* If we saw `typename', or didn't see `::', then there must be a
19367 nested-name-specifier present. */
19368 if (typename_p
|| !global_scope_p
)
19370 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19371 /*check_dependency_p=*/true,
19373 /*is_declaration=*/true);
19374 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19376 cp_parser_skip_to_end_of_block_or_statement (parser
);
19380 /* Otherwise, we could be in either of the two productions. In that
19381 case, treat the nested-name-specifier as optional. */
19383 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19384 /*typename_keyword_p=*/false,
19385 /*check_dependency_p=*/true,
19387 /*is_declaration=*/true);
19389 qscope
= global_namespace
;
19390 else if (UNSCOPED_ENUM_P (qscope
))
19391 qscope
= CP_TYPE_CONTEXT (qscope
);
19393 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19394 /* Something has already gone wrong; there's no need to parse
19395 further. Since an error has occurred, the return value of
19396 cp_parser_parse_definitely will be false, as required. */
19397 return cp_parser_parse_definitely (parser
);
19399 token
= cp_lexer_peek_token (parser
->lexer
);
19400 /* Parse the unqualified-id. */
19401 identifier
= cp_parser_unqualified_id (parser
,
19402 /*template_keyword_p=*/false,
19403 /*check_dependency_p=*/true,
19404 /*declarator_p=*/true,
19405 /*optional_p=*/false);
19407 if (access_declaration_p
)
19409 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19410 cp_parser_simulate_error (parser
);
19411 if (!cp_parser_parse_definitely (parser
))
19414 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19416 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19417 if (cxx_dialect
< cxx17
19418 && !in_system_header_at (ell
->location
))
19419 pedwarn (ell
->location
, 0,
19420 "pack expansion in using-declaration only available "
19421 "with -std=c++17 or -std=gnu++17");
19422 qscope
= make_pack_expansion (qscope
);
19425 /* The function we call to handle a using-declaration is different
19426 depending on what scope we are in. */
19427 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19429 else if (!identifier_p (identifier
)
19430 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19431 /* [namespace.udecl]
19433 A using declaration shall not name a template-id. */
19434 error_at (token
->location
,
19435 "a template-id may not appear in a using-declaration");
19438 if (at_class_scope_p ())
19440 /* Create the USING_DECL. */
19441 decl
= do_class_using_decl (qscope
, identifier
);
19443 if (decl
&& typename_p
)
19444 USING_DECL_TYPENAME_P (decl
) = 1;
19446 if (check_for_bare_parameter_packs (decl
))
19448 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19452 /* Add it to the list of members in this class. */
19453 finish_member_declaration (decl
);
19457 decl
= cp_parser_lookup_name_simple (parser
,
19460 if (decl
== error_mark_node
)
19461 cp_parser_name_lookup_error (parser
, identifier
,
19464 else if (check_for_bare_parameter_packs (decl
))
19466 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19469 else if (!at_namespace_scope_p ())
19470 finish_local_using_decl (decl
, qscope
, identifier
);
19472 finish_namespace_using_decl (decl
, qscope
, identifier
);
19476 if (!access_declaration_p
19477 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19479 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19480 if (cxx_dialect
< cxx17
)
19481 pedwarn (comma
->location
, 0,
19482 "comma-separated list in using-declaration only available "
19483 "with -std=c++17 or -std=gnu++17");
19487 /* Look for the final `;'. */
19488 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19490 if (access_declaration_p
&& errorcount
== oldcount
)
19491 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19492 "access declarations are deprecated "
19493 "in favour of using-declarations; "
19494 "suggestion: add the %<using%> keyword");
19499 /* Parse an alias-declaration.
19502 using identifier attribute-specifier-seq [opt] = type-id */
19505 cp_parser_alias_declaration (cp_parser
* parser
)
19507 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19508 location_t id_location
, type_location
;
19509 cp_declarator
*declarator
;
19510 cp_decl_specifier_seq decl_specs
;
19512 const char *saved_message
= NULL
;
19514 /* Look for the `using' keyword. */
19515 cp_token
*using_token
19516 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19517 if (using_token
== NULL
)
19518 return error_mark_node
;
19520 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19521 id
= cp_parser_identifier (parser
);
19522 if (id
== error_mark_node
)
19523 return error_mark_node
;
19525 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19526 attributes
= cp_parser_attributes_opt (parser
);
19527 if (attributes
== error_mark_node
)
19528 return error_mark_node
;
19530 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19532 if (cp_parser_error_occurred (parser
))
19533 return error_mark_node
;
19535 cp_parser_commit_to_tentative_parse (parser
);
19537 /* Now we are going to parse the type-id of the declaration. */
19542 "A type-specifier-seq shall not define a class or enumeration
19543 unless it appears in the type-id of an alias-declaration (7.1.3) that
19544 is not the declaration of a template-declaration."
19546 In other words, if we currently are in an alias template, the
19547 type-id should not define a type.
19549 So let's set parser->type_definition_forbidden_message in that
19550 case; cp_parser_check_type_definition (called by
19551 cp_parser_class_specifier) will then emit an error if a type is
19552 defined in the type-id. */
19553 if (parser
->num_template_parameter_lists
)
19555 saved_message
= parser
->type_definition_forbidden_message
;
19556 parser
->type_definition_forbidden_message
=
19557 G_("types may not be defined in alias template declarations");
19560 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
19563 /* Restore the error message if need be. */
19564 if (parser
->num_template_parameter_lists
)
19565 parser
->type_definition_forbidden_message
= saved_message
;
19567 if (type
== error_mark_node
19568 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19570 cp_parser_skip_to_end_of_block_or_statement (parser
);
19571 return error_mark_node
;
19574 /* A typedef-name can also be introduced by an alias-declaration. The
19575 identifier following the using keyword becomes a typedef-name. It has
19576 the same semantics as if it were introduced by the typedef
19577 specifier. In particular, it does not define a new type and it shall
19578 not appear in the type-id. */
19580 clear_decl_specs (&decl_specs
);
19581 decl_specs
.type
= type
;
19582 if (attributes
!= NULL_TREE
)
19584 decl_specs
.attributes
= attributes
;
19585 set_and_check_decl_spec_loc (&decl_specs
,
19589 set_and_check_decl_spec_loc (&decl_specs
,
19592 set_and_check_decl_spec_loc (&decl_specs
,
19595 decl_specs
.locations
[ds_type_spec
] = type_location
;
19597 if (parser
->num_template_parameter_lists
19598 && !cp_parser_check_template_parameters (parser
,
19599 /*num_templates=*/0,
19600 /*template_id*/false,
19602 /*declarator=*/NULL
))
19603 return error_mark_node
;
19605 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
19607 member_p
= at_class_scope_p ();
19609 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19610 NULL_TREE
, attributes
);
19612 decl
= start_decl (declarator
, &decl_specs
, 0,
19613 attributes
, NULL_TREE
, &pushed_scope
);
19614 if (decl
== error_mark_node
)
19617 // Attach constraints to the alias declaration.
19618 if (flag_concepts
&& current_template_parms
)
19620 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19621 tree constr
= build_constraints (reqs
, NULL_TREE
);
19622 set_constraints (decl
, constr
);
19625 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19628 pop_scope (pushed_scope
);
19630 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19631 added into the symbol table; otherwise, return the TYPE_DECL. */
19632 if (DECL_LANG_SPECIFIC (decl
)
19633 && DECL_TEMPLATE_INFO (decl
)
19634 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19636 decl
= DECL_TI_TEMPLATE (decl
);
19638 check_member_template (decl
);
19644 /* Parse a using-directive.
19647 using namespace :: [opt] nested-name-specifier [opt]
19648 namespace-name ; */
19651 cp_parser_using_directive (cp_parser
* parser
)
19653 tree namespace_decl
;
19656 /* Look for the `using' keyword. */
19657 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19658 /* And the `namespace' keyword. */
19659 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19660 /* Look for the optional `::' operator. */
19661 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19662 /* And the optional nested-name-specifier. */
19663 cp_parser_nested_name_specifier_opt (parser
,
19664 /*typename_keyword_p=*/false,
19665 /*check_dependency_p=*/true,
19667 /*is_declaration=*/true);
19668 /* Get the namespace being used. */
19669 namespace_decl
= cp_parser_namespace_name (parser
);
19670 /* And any specified attributes. */
19671 attribs
= cp_parser_attributes_opt (parser
);
19673 /* Update the symbol table. */
19674 if (namespace_bindings_p ())
19675 finish_namespace_using_directive (namespace_decl
, attribs
);
19677 finish_local_using_directive (namespace_decl
, attribs
);
19679 /* Look for the final `;'. */
19680 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19683 /* Parse an asm-definition.
19690 asm-qualifier-list:
19692 asm-qualifier-list asm-qualifier
19695 asm ( string-literal ) ;
19700 asm asm-qualifier-list [opt] ( string-literal ) ;
19701 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19702 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19703 : asm-operand-list [opt] ) ;
19704 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19705 : asm-operand-list [opt]
19706 : asm-clobber-list [opt] ) ;
19707 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19708 : asm-clobber-list [opt]
19709 : asm-goto-list ) ;
19711 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19712 contains goto, and is the only allowed form in that case. No duplicates are
19713 allowed in an asm-qualifier-list. */
19716 cp_parser_asm_definition (cp_parser
* parser
)
19719 tree outputs
= NULL_TREE
;
19720 tree inputs
= NULL_TREE
;
19721 tree clobbers
= NULL_TREE
;
19722 tree labels
= NULL_TREE
;
19724 bool extended_p
= false;
19725 bool invalid_inputs_p
= false;
19726 bool invalid_outputs_p
= false;
19727 required_token missing
= RT_NONE
;
19729 /* Look for the `asm' keyword. */
19730 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19732 if (parser
->in_function_body
19733 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19735 error ("%<asm%> in %<constexpr%> function");
19736 cp_function_chain
->invalid_constexpr
= true;
19739 /* Handle the asm-qualifier-list. */
19740 location_t volatile_loc
= UNKNOWN_LOCATION
;
19741 location_t inline_loc
= UNKNOWN_LOCATION
;
19742 location_t goto_loc
= UNKNOWN_LOCATION
;
19744 if (cp_parser_allow_gnu_extensions_p (parser
) && parser
->in_function_body
)
19747 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19748 location_t loc
= token
->location
;
19749 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
19754 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19755 inform (volatile_loc
, "first seen here");
19758 volatile_loc
= loc
;
19759 cp_lexer_consume_token (parser
->lexer
);
19765 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19766 inform (inline_loc
, "first seen here");
19770 cp_lexer_consume_token (parser
->lexer
);
19776 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19777 inform (goto_loc
, "first seen here");
19781 cp_lexer_consume_token (parser
->lexer
);
19786 error_at (loc
, "%qT is not an asm qualifier", token
->u
.value
);
19787 cp_lexer_consume_token (parser
->lexer
);
19796 bool volatile_p
= (volatile_loc
!= UNKNOWN_LOCATION
);
19797 bool inline_p
= (inline_loc
!= UNKNOWN_LOCATION
);
19798 bool goto_p
= (goto_loc
!= UNKNOWN_LOCATION
);
19800 /* Look for the opening `('. */
19801 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19803 /* Look for the string. */
19804 string
= cp_parser_string_literal (parser
, false, false);
19805 if (string
== error_mark_node
)
19807 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19808 /*consume_paren=*/true);
19812 /* If we're allowing GNU extensions, check for the extended assembly
19813 syntax. Unfortunately, the `:' tokens need not be separated by
19814 a space in C, and so, for compatibility, we tolerate that here
19815 too. Doing that means that we have to treat the `::' operator as
19817 if (cp_parser_allow_gnu_extensions_p (parser
)
19818 && parser
->in_function_body
19819 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19820 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19822 bool inputs_p
= false;
19823 bool clobbers_p
= false;
19824 bool labels_p
= false;
19826 /* The extended syntax was used. */
19829 /* Look for outputs. */
19830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19832 /* Consume the `:'. */
19833 cp_lexer_consume_token (parser
->lexer
);
19834 /* Parse the output-operands. */
19835 if (cp_lexer_next_token_is_not (parser
->lexer
,
19837 && cp_lexer_next_token_is_not (parser
->lexer
,
19839 && cp_lexer_next_token_is_not (parser
->lexer
,
19843 outputs
= cp_parser_asm_operand_list (parser
);
19844 if (outputs
== error_mark_node
)
19845 invalid_outputs_p
= true;
19848 /* If the next token is `::', there are no outputs, and the
19849 next token is the beginning of the inputs. */
19850 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19851 /* The inputs are coming next. */
19854 /* Look for inputs. */
19856 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19858 /* Consume the `:' or `::'. */
19859 cp_lexer_consume_token (parser
->lexer
);
19860 /* Parse the output-operands. */
19861 if (cp_lexer_next_token_is_not (parser
->lexer
,
19863 && cp_lexer_next_token_is_not (parser
->lexer
,
19865 && cp_lexer_next_token_is_not (parser
->lexer
,
19868 inputs
= cp_parser_asm_operand_list (parser
);
19869 if (inputs
== error_mark_node
)
19870 invalid_inputs_p
= true;
19873 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19874 /* The clobbers are coming next. */
19877 /* Look for clobbers. */
19879 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19882 /* Consume the `:' or `::'. */
19883 cp_lexer_consume_token (parser
->lexer
);
19884 /* Parse the clobbers. */
19885 if (cp_lexer_next_token_is_not (parser
->lexer
,
19887 && cp_lexer_next_token_is_not (parser
->lexer
,
19889 clobbers
= cp_parser_asm_clobber_list (parser
);
19891 else if (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19892 /* The labels are coming next. */
19895 /* Look for labels. */
19897 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19900 /* Consume the `:' or `::'. */
19901 cp_lexer_consume_token (parser
->lexer
);
19902 /* Parse the labels. */
19903 labels
= cp_parser_asm_label_list (parser
);
19906 if (goto_p
&& !labels_p
)
19907 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19910 missing
= RT_COLON_SCOPE
;
19912 /* Look for the closing `)'. */
19913 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19914 missing
? missing
: RT_CLOSE_PAREN
))
19915 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19916 /*consume_paren=*/true);
19917 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19919 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19921 /* Create the ASM_EXPR. */
19922 if (parser
->in_function_body
)
19924 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19925 inputs
, clobbers
, labels
, inline_p
);
19926 /* If the extended syntax was not used, mark the ASM_EXPR. */
19929 tree temp
= asm_stmt
;
19930 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19931 temp
= TREE_OPERAND (temp
, 0);
19933 ASM_INPUT_P (temp
) = 1;
19937 symtab
->finalize_toplevel_asm (string
);
19941 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19942 type that comes from the decl-specifier-seq. */
19945 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19947 for (cp_declarator
*d
= declarator
; d
;)
19957 if (TYPE_PTRMEMFUNC_P (type
))
19958 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19959 type
= TREE_TYPE (type
);
19967 /* Declarators [gram.dcl.decl] */
19969 /* Parse an init-declarator.
19972 declarator initializer [opt]
19977 declarator asm-specification [opt] attributes [opt] initializer [opt]
19979 function-definition:
19980 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19982 decl-specifier-seq [opt] declarator function-try-block
19986 function-definition:
19987 __extension__ function-definition
19991 function-definition:
19992 decl-specifier-seq [opt] declarator function-transaction-block
19994 The parser flags FLAGS is used to control type-specifier parsing.
19996 The DECL_SPECIFIERS apply to this declarator. Returns a
19997 representation of the entity declared. If MEMBER_P is TRUE, then
19998 this declarator appears in a class scope. The new DECL created by
19999 this declarator is returned.
20001 The CHECKS are access checks that should be performed once we know
20002 what entity is being declared (and, therefore, what classes have
20005 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20006 for a function-definition here as well. If the declarator is a
20007 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20008 be TRUE upon return. By that point, the function-definition will
20009 have been completely parsed.
20011 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20014 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20015 parsed declaration if it is an uninitialized single declarator not followed
20016 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20017 if present, will not be consumed. If returned, this declarator will be
20018 created with SD_INITIALIZED but will not call cp_finish_decl.
20020 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20021 and there is an initializer, the pointed location_t is set to the
20022 location of the '=' or `(', or '{' in C++11 token introducing the
20026 cp_parser_init_declarator (cp_parser
* parser
,
20027 cp_parser_flags flags
,
20028 cp_decl_specifier_seq
*decl_specifiers
,
20029 vec
<deferred_access_check
, va_gc
> *checks
,
20030 bool function_definition_allowed_p
,
20032 int declares_class_or_enum
,
20033 bool* function_definition_p
,
20034 tree
* maybe_range_for_decl
,
20035 location_t
* init_loc
,
20038 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
20039 *attributes_start_token
= NULL
;
20040 cp_declarator
*declarator
;
20041 tree prefix_attributes
;
20042 tree attributes
= NULL
;
20043 tree asm_specification
;
20045 tree decl
= NULL_TREE
;
20047 int is_initialized
;
20048 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20049 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20051 enum cpp_ttype initialization_kind
;
20052 bool is_direct_init
= false;
20053 bool is_non_constant_init
;
20054 int ctor_dtor_or_conv_p
;
20055 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
20056 tree pushed_scope
= NULL_TREE
;
20057 bool range_for_decl_p
= false;
20058 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20059 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
20061 /* Gather the attributes that were provided with the
20062 decl-specifiers. */
20063 prefix_attributes
= decl_specifiers
->attributes
;
20065 /* Assume that this is not the declarator for a function
20067 if (function_definition_p
)
20068 *function_definition_p
= false;
20070 /* Default arguments are only permitted for function parameters. */
20071 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
20072 parser
->default_arg_ok_p
= false;
20074 /* Defer access checks while parsing the declarator; we cannot know
20075 what names are accessible until we know what is being
20077 resume_deferring_access_checks ();
20079 token
= cp_lexer_peek_token (parser
->lexer
);
20081 /* Parse the declarator. */
20083 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20084 flags
, &ctor_dtor_or_conv_p
,
20085 /*parenthesized_p=*/NULL
,
20086 member_p
, friend_p
, /*static_p=*/false);
20087 /* Gather up the deferred checks. */
20088 stop_deferring_access_checks ();
20090 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20092 /* If the DECLARATOR was erroneous, there's no need to go
20094 if (declarator
== cp_error_declarator
)
20095 return error_mark_node
;
20097 /* Check that the number of template-parameter-lists is OK. */
20098 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
20100 return error_mark_node
;
20102 if (declares_class_or_enum
& 2)
20103 cp_parser_check_for_definition_in_return_type (declarator
,
20104 decl_specifiers
->type
,
20105 decl_specifiers
->locations
[ds_type_spec
]);
20107 /* Figure out what scope the entity declared by the DECLARATOR is
20108 located in. `grokdeclarator' sometimes changes the scope, so
20109 we compute it now. */
20110 scope
= get_scope_of_declarator (declarator
);
20112 /* Perform any lookups in the declared type which were thought to be
20113 dependent, but are not in the scope of the declarator. */
20114 decl_specifiers
->type
20115 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
20117 /* If we're allowing GNU extensions, look for an
20118 asm-specification. */
20119 if (cp_parser_allow_gnu_extensions_p (parser
))
20121 /* Look for an asm-specification. */
20122 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
20123 asm_specification
= cp_parser_asm_specification_opt (parser
);
20126 asm_specification
= NULL_TREE
;
20128 /* Look for attributes. */
20129 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
20130 attributes
= cp_parser_attributes_opt (parser
);
20132 /* Peek at the next token. */
20133 token
= cp_lexer_peek_token (parser
->lexer
);
20135 bool bogus_implicit_tmpl
= false;
20137 if (function_declarator_p (declarator
))
20139 /* Handle C++17 deduction guides. */
20140 if (!decl_specifiers
->type
20141 && ctor_dtor_or_conv_p
<= 0
20142 && cxx_dialect
>= cxx17
)
20144 cp_declarator
*id
= get_id_declarator (declarator
);
20145 tree name
= id
->u
.id
.unqualified_name
;
20146 parser
->scope
= id
->u
.id
.qualifying_scope
;
20147 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
20149 && (DECL_CLASS_TEMPLATE_P (tmpl
)
20150 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
20152 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
20153 id
->u
.id
.sfk
= sfk_deduction_guide
;
20154 ctor_dtor_or_conv_p
= 1;
20158 /* Check to see if the token indicates the start of a
20159 function-definition. */
20160 if (cp_parser_token_starts_function_definition_p (token
))
20162 if (!function_definition_allowed_p
)
20164 /* If a function-definition should not appear here, issue an
20166 cp_parser_error (parser
,
20167 "a function-definition is not allowed here");
20168 return error_mark_node
;
20171 location_t func_brace_location
20172 = cp_lexer_peek_token (parser
->lexer
)->location
;
20174 /* Neither attributes nor an asm-specification are allowed
20175 on a function-definition. */
20176 if (asm_specification
)
20177 error_at (asm_spec_start_token
->location
,
20178 "an asm-specification is not allowed "
20179 "on a function-definition");
20181 error_at (attributes_start_token
->location
,
20182 "attributes are not allowed "
20183 "on a function-definition");
20184 /* This is a function-definition. */
20185 *function_definition_p
= true;
20187 /* Parse the function definition. */
20189 decl
= cp_parser_save_member_function_body (parser
,
20192 prefix_attributes
);
20195 (cp_parser_function_definition_from_specifiers_and_declarator
20196 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
20198 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
20200 /* This is where the prologue starts... */
20201 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
20202 = func_brace_location
;
20208 else if (parser
->fully_implicit_function_template_p
)
20210 /* A non-template declaration involving a function parameter list
20211 containing an implicit template parameter will be made into a
20212 template. If the resulting declaration is not going to be an
20213 actual function then finish the template scope here to prevent it.
20214 An error message will be issued once we have a decl to talk about.
20216 FIXME probably we should do type deduction rather than create an
20217 implicit template, but the standard currently doesn't allow it. */
20218 bogus_implicit_tmpl
= true;
20219 finish_fully_implicit_template (parser
, NULL_TREE
);
20224 Only in function declarations for constructors, destructors, type
20225 conversions, and deduction guides can the decl-specifier-seq be omitted.
20227 We explicitly postpone this check past the point where we handle
20228 function-definitions because we tolerate function-definitions
20229 that are missing their return types in some modes. */
20230 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
20232 cp_parser_error (parser
,
20233 "expected constructor, destructor, or type conversion");
20234 return error_mark_node
;
20237 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20238 if (token
->type
== CPP_EQ
20239 || token
->type
== CPP_OPEN_PAREN
20240 || token
->type
== CPP_OPEN_BRACE
)
20242 is_initialized
= SD_INITIALIZED
;
20243 initialization_kind
= token
->type
;
20244 if (maybe_range_for_decl
)
20245 *maybe_range_for_decl
= error_mark_node
;
20246 tmp_init_loc
= token
->location
;
20247 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
20248 *init_loc
= tmp_init_loc
;
20250 if (token
->type
== CPP_EQ
20251 && function_declarator_p (declarator
))
20253 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20254 if (t2
->keyword
== RID_DEFAULT
)
20255 is_initialized
= SD_DEFAULTED
;
20256 else if (t2
->keyword
== RID_DELETE
)
20257 is_initialized
= SD_DELETED
;
20262 /* If the init-declarator isn't initialized and isn't followed by a
20263 `,' or `;', it's not a valid init-declarator. */
20264 if (token
->type
!= CPP_COMMA
20265 && token
->type
!= CPP_SEMICOLON
)
20267 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
20268 range_for_decl_p
= true;
20271 if (!maybe_range_for_decl
)
20272 cp_parser_error (parser
, "expected initializer");
20273 return error_mark_node
;
20276 is_initialized
= SD_UNINITIALIZED
;
20277 initialization_kind
= CPP_EOF
;
20280 /* Because start_decl has side-effects, we should only call it if we
20281 know we're going ahead. By this point, we know that we cannot
20282 possibly be looking at any other construct. */
20283 cp_parser_commit_to_tentative_parse (parser
);
20285 /* Enter the newly declared entry in the symbol table. If we're
20286 processing a declaration in a class-specifier, we wait until
20287 after processing the initializer. */
20290 if (parser
->in_unbraced_linkage_specification_p
)
20291 decl_specifiers
->storage_class
= sc_extern
;
20292 decl
= start_decl (declarator
, decl_specifiers
,
20293 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
20294 attributes
, prefix_attributes
, &pushed_scope
);
20295 cp_finalize_omp_declare_simd (parser
, decl
);
20296 cp_finalize_oacc_routine (parser
, decl
, false);
20297 /* Adjust location of decl if declarator->id_loc is more appropriate:
20298 set, and decl wasn't merged with another decl, in which case its
20299 location would be different from input_location, and more accurate. */
20301 && declarator
->id_loc
!= UNKNOWN_LOCATION
20302 && DECL_SOURCE_LOCATION (decl
) == input_location
)
20303 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
20306 /* Enter the SCOPE. That way unqualified names appearing in the
20307 initializer will be looked up in SCOPE. */
20308 pushed_scope
= push_scope (scope
);
20310 /* Perform deferred access control checks, now that we know in which
20311 SCOPE the declared entity resides. */
20312 if (!member_p
&& decl
)
20314 tree saved_current_function_decl
= NULL_TREE
;
20316 /* If the entity being declared is a function, pretend that we
20317 are in its scope. If it is a `friend', it may have access to
20318 things that would not otherwise be accessible. */
20319 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20321 saved_current_function_decl
= current_function_decl
;
20322 current_function_decl
= decl
;
20325 /* Perform access checks for template parameters. */
20326 cp_parser_perform_template_parameter_access_checks (checks
);
20328 /* Perform the access control checks for the declarator and the
20329 decl-specifiers. */
20330 perform_deferred_access_checks (tf_warning_or_error
);
20332 /* Restore the saved value. */
20333 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20334 current_function_decl
= saved_current_function_decl
;
20337 /* Parse the initializer. */
20338 initializer
= NULL_TREE
;
20339 is_direct_init
= false;
20340 is_non_constant_init
= true;
20341 if (is_initialized
)
20343 if (function_declarator_p (declarator
))
20345 if (initialization_kind
== CPP_EQ
)
20346 initializer
= cp_parser_pure_specifier (parser
);
20349 /* If the declaration was erroneous, we don't really
20350 know what the user intended, so just silently
20351 consume the initializer. */
20352 if (decl
!= error_mark_node
)
20353 error_at (tmp_init_loc
, "initializer provided for function");
20354 cp_parser_skip_to_closing_parenthesis (parser
,
20355 /*recovering=*/true,
20356 /*or_comma=*/false,
20357 /*consume_paren=*/true);
20362 /* We want to record the extra mangling scope for in-class
20363 initializers of class members and initializers of static data
20364 member templates. The former involves deferring
20365 parsing of the initializer until end of class as with default
20366 arguments. So right here we only handle the latter. */
20367 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20368 start_lambda_scope (decl
);
20369 initializer
= cp_parser_initializer (parser
,
20371 &is_non_constant_init
);
20372 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20373 finish_lambda_scope ();
20374 if (initializer
== error_mark_node
)
20375 cp_parser_skip_to_end_of_statement (parser
);
20379 /* The old parser allows attributes to appear after a parenthesized
20380 initializer. Mark Mitchell proposed removing this functionality
20381 on the GCC mailing lists on 2002-08-13. This parser accepts the
20382 attributes -- but ignores them. Made a permerror in GCC 8. */
20383 if (cp_parser_allow_gnu_extensions_p (parser
)
20384 && initialization_kind
== CPP_OPEN_PAREN
20385 && cp_parser_attributes_opt (parser
)
20386 && permerror (input_location
,
20387 "attributes after parenthesized initializer ignored"))
20390 if (flag_permissive
&& !hint
)
20393 inform (input_location
,
20394 "this flexibility is deprecated and will be removed");
20398 /* And now complain about a non-function implicit template. */
20399 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20400 error_at (DECL_SOURCE_LOCATION (decl
),
20401 "non-function %qD declared as implicit template", decl
);
20403 /* For an in-class declaration, use `grokfield' to create the
20409 pop_scope (pushed_scope
);
20410 pushed_scope
= NULL_TREE
;
20412 decl
= grokfield (declarator
, decl_specifiers
,
20413 initializer
, !is_non_constant_init
,
20414 /*asmspec=*/NULL_TREE
,
20415 attr_chainon (attributes
, prefix_attributes
));
20416 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20417 cp_parser_save_default_args (parser
, decl
);
20418 cp_finalize_omp_declare_simd (parser
, decl
);
20419 cp_finalize_oacc_routine (parser
, decl
, false);
20422 /* Finish processing the declaration. But, skip member
20424 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20426 cp_finish_decl (decl
,
20427 initializer
, !is_non_constant_init
,
20429 /* If the initializer is in parentheses, then this is
20430 a direct-initialization, which means that an
20431 `explicit' constructor is OK. Otherwise, an
20432 `explicit' constructor cannot be used. */
20433 ((is_direct_init
|| !is_initialized
)
20434 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
20436 else if ((cxx_dialect
!= cxx98
) && friend_p
20437 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20438 /* Core issue #226 (C++0x only): A default template-argument
20439 shall not be specified in a friend class template
20441 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20442 /*is_partial=*/false, /*is_friend_decl=*/1);
20444 if (!friend_p
&& pushed_scope
)
20445 pop_scope (pushed_scope
);
20447 if (function_declarator_p (declarator
)
20448 && parser
->fully_implicit_function_template_p
)
20451 decl
= finish_fully_implicit_template (parser
, decl
);
20453 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
20456 if (auto_result
&& is_initialized
&& decl_specifiers
->type
20457 && type_uses_auto (decl_specifiers
->type
))
20458 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
20463 /* Parse a declarator.
20467 ptr-operator declarator
20469 abstract-declarator:
20470 ptr-operator abstract-declarator [opt]
20471 direct-abstract-declarator
20476 attributes [opt] direct-declarator
20477 attributes [opt] ptr-operator declarator
20479 abstract-declarator:
20480 attributes [opt] ptr-operator abstract-declarator [opt]
20481 attributes [opt] direct-abstract-declarator
20483 The parser flags FLAGS is used to control type-specifier parsing.
20485 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20486 detect constructors, destructors, deduction guides, or conversion operators.
20487 It is set to -1 if the declarator is a name, and +1 if it is a
20488 function. Otherwise it is set to zero. Usually you just want to
20489 test for >0, but internally the negative value is used.
20491 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20492 a decl-specifier-seq unless it declares a constructor, destructor,
20493 or conversion. It might seem that we could check this condition in
20494 semantic analysis, rather than parsing, but that makes it difficult
20495 to handle something like `f()'. We want to notice that there are
20496 no decl-specifiers, and therefore realize that this is an
20497 expression, not a declaration.)
20499 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20500 the declarator is a direct-declarator of the form "(...)".
20502 MEMBER_P is true iff this declarator is a member-declarator.
20504 FRIEND_P is true iff this declarator is a friend.
20506 STATIC_P is true iff the keyword static was seen. */
20508 static cp_declarator
*
20509 cp_parser_declarator (cp_parser
* parser
,
20510 cp_parser_declarator_kind dcl_kind
,
20511 cp_parser_flags flags
,
20512 int* ctor_dtor_or_conv_p
,
20513 bool* parenthesized_p
,
20514 bool member_p
, bool friend_p
, bool static_p
)
20516 cp_declarator
*declarator
;
20517 enum tree_code code
;
20518 cp_cv_quals cv_quals
;
20520 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
20522 /* Assume this is not a constructor, destructor, or type-conversion
20524 if (ctor_dtor_or_conv_p
)
20525 *ctor_dtor_or_conv_p
= 0;
20527 if (cp_parser_allow_gnu_extensions_p (parser
))
20528 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
20530 /* Check for the ptr-operator production. */
20531 cp_parser_parse_tentatively (parser
);
20532 /* Parse the ptr-operator. */
20533 code
= cp_parser_ptr_operator (parser
,
20538 /* If that worked, then we have a ptr-operator. */
20539 if (cp_parser_parse_definitely (parser
))
20541 /* If a ptr-operator was found, then this declarator was not
20543 if (parenthesized_p
)
20544 *parenthesized_p
= true;
20545 /* The dependent declarator is optional if we are parsing an
20546 abstract-declarator. */
20547 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20548 cp_parser_parse_tentatively (parser
);
20550 /* Parse the dependent declarator. */
20551 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20552 CP_PARSER_FLAGS_NONE
,
20553 /*ctor_dtor_or_conv_p=*/NULL
,
20554 /*parenthesized_p=*/NULL
,
20555 /*member_p=*/false,
20556 friend_p
, /*static_p=*/false);
20558 /* If we are parsing an abstract-declarator, we must handle the
20559 case where the dependent declarator is absent. */
20560 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20561 && !cp_parser_parse_definitely (parser
))
20564 declarator
= cp_parser_make_indirect_declarator
20565 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20567 /* Everything else is a direct-declarator. */
20570 if (parenthesized_p
)
20571 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20573 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20574 flags
, ctor_dtor_or_conv_p
,
20575 member_p
, friend_p
, static_p
);
20578 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20579 declarator
->attributes
= gnu_attributes
;
20583 /* Parse a direct-declarator or direct-abstract-declarator.
20587 direct-declarator ( parameter-declaration-clause )
20588 cv-qualifier-seq [opt]
20589 ref-qualifier [opt]
20590 exception-specification [opt]
20591 direct-declarator [ constant-expression [opt] ]
20594 direct-abstract-declarator:
20595 direct-abstract-declarator [opt]
20596 ( parameter-declaration-clause )
20597 cv-qualifier-seq [opt]
20598 ref-qualifier [opt]
20599 exception-specification [opt]
20600 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20601 ( abstract-declarator )
20603 Returns a representation of the declarator. DCL_KIND is
20604 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20605 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20606 we are parsing a direct-declarator. It is
20607 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20608 of ambiguity we prefer an abstract declarator, as per
20610 The parser flags FLAGS is used to control type-specifier parsing.
20611 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20612 as for cp_parser_declarator. */
20614 static cp_declarator
*
20615 cp_parser_direct_declarator (cp_parser
* parser
,
20616 cp_parser_declarator_kind dcl_kind
,
20617 cp_parser_flags flags
,
20618 int* ctor_dtor_or_conv_p
,
20619 bool member_p
, bool friend_p
, bool static_p
)
20622 cp_declarator
*declarator
= NULL
;
20623 tree scope
= NULL_TREE
;
20624 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20625 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20627 tree pushed_scope
= NULL_TREE
;
20628 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20632 /* Peek at the next token. */
20633 token
= cp_lexer_peek_token (parser
->lexer
);
20634 if (token
->type
== CPP_OPEN_PAREN
)
20636 /* This is either a parameter-declaration-clause, or a
20637 parenthesized declarator. When we know we are parsing a
20638 named declarator, it must be a parenthesized declarator
20639 if FIRST is true. For instance, `(int)' is a
20640 parameter-declaration-clause, with an omitted
20641 direct-abstract-declarator. But `((*))', is a
20642 parenthesized abstract declarator. Finally, when T is a
20643 template parameter `(T)' is a
20644 parameter-declaration-clause, and not a parenthesized
20647 We first try and parse a parameter-declaration-clause,
20648 and then try a nested declarator (if FIRST is true).
20650 It is not an error for it not to be a
20651 parameter-declaration-clause, even when FIRST is
20657 The first is the declaration of a function while the
20658 second is the definition of a variable, including its
20661 Having seen only the parenthesis, we cannot know which of
20662 these two alternatives should be selected. Even more
20663 complex are examples like:
20668 The former is a function-declaration; the latter is a
20669 variable initialization.
20671 Thus again, we try a parameter-declaration-clause, and if
20672 that fails, we back out and return. */
20674 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20677 bool is_declarator
= false;
20681 /* In a member-declarator, the only valid interpretation
20682 of a parenthesis is the start of a
20683 parameter-declaration-clause. (It is invalid to
20684 initialize a static data member with a parenthesized
20685 initializer; only the "=" form of initialization is
20688 cp_parser_parse_tentatively (parser
);
20690 /* Consume the `('. */
20691 matching_parens parens
;
20692 parens
.consume_open (parser
);
20695 /* If this is going to be an abstract declarator, we're
20696 in a declarator and we can't have default args. */
20697 parser
->default_arg_ok_p
= false;
20698 parser
->in_declarator_p
= true;
20701 begin_scope (sk_function_parms
, NULL_TREE
);
20703 /* Parse the parameter-declaration-clause. */
20705 = cp_parser_parameter_declaration_clause (parser
, flags
);
20707 /* Consume the `)'. */
20708 parens
.require_close (parser
);
20710 /* If all went well, parse the cv-qualifier-seq,
20711 ref-qualifier and the exception-specification. */
20712 if (member_p
|| cp_parser_parse_definitely (parser
))
20714 cp_cv_quals cv_quals
;
20715 cp_virt_specifiers virt_specifiers
;
20716 cp_ref_qualifier ref_qual
;
20717 tree exception_specification
;
20720 bool memfn
= (member_p
|| (pushed_scope
20721 && CLASS_TYPE_P (pushed_scope
)));
20722 unsigned char local_variables_forbidden_p
20723 = parser
->local_variables_forbidden_p
;
20724 /* 'this' is not allowed in static member functions. */
20725 if (static_p
|| friend_p
)
20726 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
20728 is_declarator
= true;
20730 if (ctor_dtor_or_conv_p
)
20731 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20734 /* Parse the cv-qualifier-seq. */
20735 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20736 /* Parse the ref-qualifier. */
20737 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20738 /* Parse the tx-qualifier. */
20739 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20740 /* And the exception-specification. */
20741 exception_specification
20742 = cp_parser_exception_specification_opt (parser
);
20744 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20746 /* In here, we handle cases where attribute is used after
20747 the function declaration. For example:
20748 void func (int x) __attribute__((vector(..))); */
20749 tree gnu_attrs
= NULL_TREE
;
20750 tree requires_clause
= NULL_TREE
;
20751 late_return
= (cp_parser_late_return_type_opt
20752 (parser
, declarator
, requires_clause
,
20753 memfn
? cv_quals
: -1));
20755 /* Parse the virt-specifier-seq. */
20756 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20758 /* Create the function-declarator. */
20759 declarator
= make_call_declarator (declarator
,
20765 exception_specification
,
20768 declarator
->std_attributes
= attrs
;
20769 declarator
->attributes
= gnu_attrs
;
20770 /* Any subsequent parameter lists are to do with
20771 return type, so are not those of the declared
20773 parser
->default_arg_ok_p
= false;
20775 /* Restore the state of local_variables_forbidden_p. */
20776 parser
->local_variables_forbidden_p
20777 = local_variables_forbidden_p
;
20780 /* Remove the function parms from scope. */
20781 pop_bindings_and_leave_scope ();
20784 /* Repeat the main loop. */
20788 /* If this is the first, we can try a parenthesized
20792 bool saved_in_type_id_in_expr_p
;
20794 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20795 parser
->in_declarator_p
= saved_in_declarator_p
;
20797 open_paren
= token
;
20798 /* Consume the `('. */
20799 matching_parens parens
;
20800 parens
.consume_open (parser
);
20801 /* Parse the nested declarator. */
20802 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20803 parser
->in_type_id_in_expr_p
= true;
20805 = cp_parser_declarator (parser
, dcl_kind
, flags
,
20806 ctor_dtor_or_conv_p
,
20807 /*parenthesized_p=*/NULL
,
20808 member_p
, friend_p
,
20809 /*static_p=*/false);
20810 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20812 /* Expect a `)'. */
20813 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20814 if (!parens
.require_close (parser
))
20815 declarator
= cp_error_declarator
;
20816 if (declarator
== cp_error_declarator
)
20819 goto handle_declarator
;
20821 /* Otherwise, we must be done. */
20825 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20826 && token
->type
== CPP_OPEN_SQUARE
20827 && !cp_next_tokens_can_be_attribute_p (parser
))
20829 /* Parse an array-declarator. */
20830 tree bounds
, attrs
;
20832 if (ctor_dtor_or_conv_p
)
20833 *ctor_dtor_or_conv_p
= 0;
20837 parser
->default_arg_ok_p
= false;
20838 parser
->in_declarator_p
= true;
20839 /* Consume the `['. */
20840 cp_lexer_consume_token (parser
->lexer
);
20841 /* Peek at the next token. */
20842 token
= cp_lexer_peek_token (parser
->lexer
);
20843 /* If the next token is `]', then there is no
20844 constant-expression. */
20845 if (token
->type
!= CPP_CLOSE_SQUARE
)
20847 bool non_constant_p
;
20849 = cp_parser_constant_expression (parser
,
20850 /*allow_non_constant=*/true,
20852 if (!non_constant_p
)
20854 else if (error_operand_p (bounds
))
20855 /* Already gave an error. */;
20856 else if (!parser
->in_function_body
20857 || current_binding_level
->kind
== sk_function_parms
)
20859 /* Normally, the array bound must be an integral constant
20860 expression. However, as an extension, we allow VLAs
20861 in function scopes as long as they aren't part of a
20862 parameter declaration. */
20863 cp_parser_error (parser
,
20864 "array bound is not an integer constant");
20865 bounds
= error_mark_node
;
20867 else if (processing_template_decl
20868 && !type_dependent_expression_p (bounds
))
20870 /* Remember this wasn't a constant-expression. */
20871 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20872 TREE_SIDE_EFFECTS (bounds
) = 1;
20876 bounds
= NULL_TREE
;
20877 /* Look for the closing `]'. */
20878 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20880 declarator
= cp_error_declarator
;
20884 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20885 declarator
= make_array_declarator (declarator
, bounds
);
20886 declarator
->std_attributes
= attrs
;
20888 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20891 tree qualifying_scope
;
20892 tree unqualified_name
;
20894 special_function_kind sfk
;
20896 bool pack_expansion_p
= false;
20897 cp_token
*declarator_id_start_token
;
20899 /* Parse a declarator-id */
20900 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20903 cp_parser_parse_tentatively (parser
);
20905 /* If we see an ellipsis, we should be looking at a
20907 if (token
->type
== CPP_ELLIPSIS
)
20909 /* Consume the `...' */
20910 cp_lexer_consume_token (parser
->lexer
);
20912 pack_expansion_p
= true;
20916 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20918 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20919 qualifying_scope
= parser
->scope
;
20924 if (!unqualified_name
&& pack_expansion_p
)
20926 /* Check whether an error occurred. */
20927 okay
= !cp_parser_error_occurred (parser
);
20929 /* We already consumed the ellipsis to mark a
20930 parameter pack, but we have no way to report it,
20931 so abort the tentative parse. We will be exiting
20932 immediately anyway. */
20933 cp_parser_abort_tentative_parse (parser
);
20936 okay
= cp_parser_parse_definitely (parser
);
20939 unqualified_name
= error_mark_node
;
20940 else if (unqualified_name
20941 && (qualifying_scope
20942 || (!identifier_p (unqualified_name
))))
20944 cp_parser_error (parser
, "expected unqualified-id");
20945 unqualified_name
= error_mark_node
;
20949 if (!unqualified_name
)
20951 if (unqualified_name
== error_mark_node
)
20953 declarator
= cp_error_declarator
;
20954 pack_expansion_p
= false;
20955 declarator
->parameter_pack_p
= false;
20959 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20961 if (qualifying_scope
&& at_namespace_scope_p ()
20962 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20964 /* In the declaration of a member of a template class
20965 outside of the class itself, the SCOPE will sometimes
20966 be a TYPENAME_TYPE. For example, given:
20968 template <typename T>
20969 int S<T>::R::i = 3;
20971 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20972 this context, we must resolve S<T>::R to an ordinary
20973 type, rather than a typename type.
20975 The reason we normally avoid resolving TYPENAME_TYPEs
20976 is that a specialization of `S' might render
20977 `S<T>::R' not a type. However, if `S' is
20978 specialized, then this `i' will not be used, so there
20979 is no harm in resolving the types here. */
20982 /* Resolve the TYPENAME_TYPE. */
20983 type
= resolve_typename_type (qualifying_scope
,
20984 /*only_current_p=*/false);
20985 /* If that failed, the declarator is invalid. */
20986 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20988 if (typedef_variant_p (type
))
20989 error_at (declarator_id_start_token
->location
,
20990 "cannot define member of dependent typedef "
20993 error_at (declarator_id_start_token
->location
,
20994 "%<%T::%E%> is not a type",
20995 TYPE_CONTEXT (qualifying_scope
),
20996 TYPE_IDENTIFIER (qualifying_scope
));
20998 qualifying_scope
= type
;
21003 if (unqualified_name
)
21007 if (qualifying_scope
21008 && CLASS_TYPE_P (qualifying_scope
))
21009 class_type
= qualifying_scope
;
21011 class_type
= current_class_type
;
21013 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
21015 tree name_type
= TREE_TYPE (unqualified_name
);
21017 if (!class_type
|| !same_type_p (name_type
, class_type
))
21019 /* We do not attempt to print the declarator
21020 here because we do not have enough
21021 information about its original syntactic
21023 cp_parser_error (parser
, "invalid declarator");
21024 declarator
= cp_error_declarator
;
21027 else if (qualifying_scope
21028 && CLASSTYPE_USE_TEMPLATE (name_type
))
21030 error_at (declarator_id_start_token
->location
,
21031 "invalid use of constructor as a template");
21032 inform (declarator_id_start_token
->location
,
21033 "use %<%T::%D%> instead of %<%T::%D%> to "
21034 "name the constructor in a qualified name",
21036 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
21037 class_type
, name_type
);
21038 declarator
= cp_error_declarator
;
21041 unqualified_name
= constructor_name (class_type
);
21046 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
21047 sfk
= sfk_destructor
;
21048 else if (identifier_p (unqualified_name
)
21049 && IDENTIFIER_CONV_OP_P (unqualified_name
))
21050 sfk
= sfk_conversion
;
21051 else if (/* There's no way to declare a constructor
21052 for an unnamed type, even if the type
21053 got a name for linkage purposes. */
21054 !TYPE_WAS_UNNAMED (class_type
)
21055 /* Handle correctly (c++/19200):
21069 friend void N::S();
21071 && (!friend_p
|| class_type
== qualifying_scope
)
21072 && constructor_name_p (unqualified_name
,
21074 sfk
= sfk_constructor
;
21075 else if (is_overloaded_fn (unqualified_name
)
21076 && DECL_CONSTRUCTOR_P (get_first_fn
21077 (unqualified_name
)))
21078 sfk
= sfk_constructor
;
21080 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
21081 *ctor_dtor_or_conv_p
= -1;
21084 declarator
= make_id_declarator (qualifying_scope
,
21086 sfk
, token
->location
);
21087 declarator
->std_attributes
= attrs
;
21088 declarator
->parameter_pack_p
= pack_expansion_p
;
21090 if (pack_expansion_p
)
21091 maybe_warn_variadic_templates ();
21094 handle_declarator
:;
21095 scope
= get_scope_of_declarator (declarator
);
21098 /* Any names that appear after the declarator-id for a
21099 member are looked up in the containing scope. */
21100 if (at_function_scope_p ())
21102 /* But declarations with qualified-ids can't appear in a
21104 cp_parser_error (parser
, "qualified-id in declaration");
21105 declarator
= cp_error_declarator
;
21108 pushed_scope
= push_scope (scope
);
21110 parser
->in_declarator_p
= true;
21111 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
21112 || (declarator
&& declarator
->kind
== cdk_id
))
21113 /* Default args are only allowed on function
21115 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21117 parser
->default_arg_ok_p
= false;
21126 /* For an abstract declarator, we might wind up with nothing at this
21127 point. That's an error; the declarator is not optional. */
21129 cp_parser_error (parser
, "expected declarator");
21130 else if (open_paren
)
21132 /* Record overly parenthesized declarator so we can give a
21133 diagnostic about confusing decl/expr disambiguation. */
21134 if (declarator
->kind
== cdk_array
)
21136 /* If the open and close parens are on different lines, this
21137 is probably a formatting thing, so ignore. */
21138 expanded_location open
= expand_location (open_paren
->location
);
21139 expanded_location close
= expand_location (close_paren
->location
);
21140 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
21144 declarator
->parenthesized
= open_paren
->location
;
21147 /* If we entered a scope, we must exit it now. */
21149 pop_scope (pushed_scope
);
21151 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21152 parser
->in_declarator_p
= saved_in_declarator_p
;
21157 /* Parse a ptr-operator.
21160 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21161 * cv-qualifier-seq [opt]
21163 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21164 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21169 & cv-qualifier-seq [opt]
21171 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21172 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21173 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21174 filled in with the TYPE containing the member. *CV_QUALS is
21175 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21176 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21177 Note that the tree codes returned by this function have nothing
21178 to do with the types of trees that will be eventually be created
21179 to represent the pointer or reference type being parsed. They are
21180 just constants with suggestive names. */
21181 static enum tree_code
21182 cp_parser_ptr_operator (cp_parser
* parser
,
21184 cp_cv_quals
*cv_quals
,
21187 enum tree_code code
= ERROR_MARK
;
21189 tree attrs
= NULL_TREE
;
21191 /* Assume that it's not a pointer-to-member. */
21193 /* And that there are no cv-qualifiers. */
21194 *cv_quals
= TYPE_UNQUALIFIED
;
21196 /* Peek at the next token. */
21197 token
= cp_lexer_peek_token (parser
->lexer
);
21199 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21200 if (token
->type
== CPP_MULT
)
21201 code
= INDIRECT_REF
;
21202 else if (token
->type
== CPP_AND
)
21204 else if ((cxx_dialect
!= cxx98
) &&
21205 token
->type
== CPP_AND_AND
) /* C++0x only */
21206 code
= NON_LVALUE_EXPR
;
21208 if (code
!= ERROR_MARK
)
21210 /* Consume the `*', `&' or `&&'. */
21211 cp_lexer_consume_token (parser
->lexer
);
21213 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21214 `&', if we are allowing GNU extensions. (The only qualifier
21215 that can legally appear after `&' is `restrict', but that is
21216 enforced during semantic analysis. */
21217 if (code
== INDIRECT_REF
21218 || cp_parser_allow_gnu_extensions_p (parser
))
21219 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21221 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21222 if (attributes
!= NULL
)
21223 *attributes
= attrs
;
21227 /* Try the pointer-to-member case. */
21228 cp_parser_parse_tentatively (parser
);
21229 /* Look for the optional `::' operator. */
21230 cp_parser_global_scope_opt (parser
,
21231 /*current_scope_valid_p=*/false);
21232 /* Look for the nested-name specifier. */
21233 token
= cp_lexer_peek_token (parser
->lexer
);
21234 cp_parser_nested_name_specifier (parser
,
21235 /*typename_keyword_p=*/false,
21236 /*check_dependency_p=*/true,
21238 /*is_declaration=*/false);
21239 /* If we found it, and the next token is a `*', then we are
21240 indeed looking at a pointer-to-member operator. */
21241 if (!cp_parser_error_occurred (parser
)
21242 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
21244 /* Indicate that the `*' operator was used. */
21245 code
= INDIRECT_REF
;
21247 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
21248 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
21249 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
21250 error_at (token
->location
, "cannot form pointer to member of "
21251 "non-class %q#T", parser
->scope
);
21254 /* The type of which the member is a member is given by the
21256 *type
= parser
->scope
;
21257 /* The next name will not be qualified. */
21258 parser
->scope
= NULL_TREE
;
21259 parser
->qualifying_scope
= NULL_TREE
;
21260 parser
->object_scope
= NULL_TREE
;
21261 /* Look for optional c++11 attributes. */
21262 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21263 if (attributes
!= NULL
)
21264 *attributes
= attrs
;
21265 /* Look for the optional cv-qualifier-seq. */
21266 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21269 /* If that didn't work we don't have a ptr-operator. */
21270 if (!cp_parser_parse_definitely (parser
))
21271 cp_parser_error (parser
, "expected ptr-operator");
21277 /* Parse an (optional) cv-qualifier-seq.
21280 cv-qualifier cv-qualifier-seq [opt]
21291 Returns a bitmask representing the cv-qualifiers. */
21294 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
21296 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
21301 cp_cv_quals cv_qualifier
;
21303 /* Peek at the next token. */
21304 token
= cp_lexer_peek_token (parser
->lexer
);
21305 /* See if it's a cv-qualifier. */
21306 switch (token
->keyword
)
21309 cv_qualifier
= TYPE_QUAL_CONST
;
21313 cv_qualifier
= TYPE_QUAL_VOLATILE
;
21317 cv_qualifier
= TYPE_QUAL_RESTRICT
;
21321 cv_qualifier
= TYPE_UNQUALIFIED
;
21328 if (cv_quals
& cv_qualifier
)
21330 gcc_rich_location
richloc (token
->location
);
21331 richloc
.add_fixit_remove ();
21332 error_at (&richloc
, "duplicate cv-qualifier");
21333 cp_lexer_purge_token (parser
->lexer
);
21337 cp_lexer_consume_token (parser
->lexer
);
21338 cv_quals
|= cv_qualifier
;
21345 /* Parse an (optional) ref-qualifier
21351 Returns cp_ref_qualifier representing ref-qualifier. */
21353 static cp_ref_qualifier
21354 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
21356 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
21358 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21359 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
21364 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
21365 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21367 switch (token
->type
)
21370 curr_ref_qual
= REF_QUAL_LVALUE
;
21374 curr_ref_qual
= REF_QUAL_RVALUE
;
21378 curr_ref_qual
= REF_QUAL_NONE
;
21382 if (!curr_ref_qual
)
21386 error_at (token
->location
, "multiple ref-qualifiers");
21387 cp_lexer_purge_token (parser
->lexer
);
21391 ref_qual
= curr_ref_qual
;
21392 cp_lexer_consume_token (parser
->lexer
);
21399 /* Parse an optional tx-qualifier.
21403 transaction_safe_dynamic */
21406 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
21408 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21409 if (token
->type
== CPP_NAME
)
21411 tree name
= token
->u
.value
;
21412 const char *p
= IDENTIFIER_POINTER (name
);
21413 const int len
= strlen ("transaction_safe");
21414 if (!strncmp (p
, "transaction_safe", len
))
21418 || !strcmp (p
, "_dynamic"))
21420 cp_lexer_consume_token (parser
->lexer
);
21423 error ("%qE requires %<-fgnu-tm%>", name
);
21434 /* Parse an (optional) virt-specifier-seq.
21436 virt-specifier-seq:
21437 virt-specifier virt-specifier-seq [opt]
21443 Returns a bitmask representing the virt-specifiers. */
21445 static cp_virt_specifiers
21446 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
21448 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21453 cp_virt_specifiers virt_specifier
;
21455 /* Peek at the next token. */
21456 token
= cp_lexer_peek_token (parser
->lexer
);
21457 /* See if it's a virt-specifier-qualifier. */
21458 if (token
->type
!= CPP_NAME
)
21460 if (id_equal (token
->u
.value
, "override"))
21462 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21463 virt_specifier
= VIRT_SPEC_OVERRIDE
;
21465 else if (id_equal (token
->u
.value
, "final"))
21467 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21468 virt_specifier
= VIRT_SPEC_FINAL
;
21470 else if (id_equal (token
->u
.value
, "__final"))
21472 virt_specifier
= VIRT_SPEC_FINAL
;
21477 if (virt_specifiers
& virt_specifier
)
21479 gcc_rich_location
richloc (token
->location
);
21480 richloc
.add_fixit_remove ();
21481 error_at (&richloc
, "duplicate virt-specifier");
21482 cp_lexer_purge_token (parser
->lexer
);
21486 cp_lexer_consume_token (parser
->lexer
);
21487 virt_specifiers
|= virt_specifier
;
21490 return virt_specifiers
;
21493 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21494 is in scope even though it isn't real. */
21497 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
21501 if (current_class_ptr
)
21503 /* We don't clear this between NSDMIs. Is it already what we want? */
21504 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
21505 if (DECL_P (current_class_ptr
)
21506 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
21507 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
21508 && cp_type_quals (type
) == quals
)
21512 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
21513 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21514 current_class_ptr
= NULL_TREE
;
21516 = cp_build_fold_indirect_ref (this_parm
);
21517 current_class_ptr
= this_parm
;
21520 /* Return true iff our current scope is a non-static data member
21524 parsing_nsdmi (void)
21526 /* We recognize NSDMI context by the context-less 'this' pointer set up
21527 by the function above. */
21528 if (current_class_ptr
21529 && TREE_CODE (current_class_ptr
) == PARM_DECL
21530 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
21535 /* Parse a late-specified return type, if any. This is not a separate
21536 non-terminal, but part of a function declarator, which looks like
21538 -> trailing-type-specifier-seq abstract-declarator(opt)
21540 Returns the type indicated by the type-id.
21542 In addition to this, parse any queued up #pragma omp declare simd
21543 clauses, and #pragma acc routine clauses.
21545 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21549 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
21550 tree
& requires_clause
, cp_cv_quals quals
)
21553 tree type
= NULL_TREE
;
21554 bool declare_simd_p
= (parser
->omp_declare_simd
21556 && declarator
->kind
== cdk_id
);
21558 bool oacc_routine_p
= (parser
->oacc_routine
21560 && declarator
->kind
== cdk_id
);
21562 /* Peek at the next token. */
21563 token
= cp_lexer_peek_token (parser
->lexer
);
21564 /* A late-specified return type is indicated by an initial '->'. */
21565 if (token
->type
!= CPP_DEREF
21566 && token
->keyword
!= RID_REQUIRES
21567 && !(token
->type
== CPP_NAME
21568 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21569 && !(declare_simd_p
|| oacc_routine_p
))
21572 tree save_ccp
= current_class_ptr
;
21573 tree save_ccr
= current_class_ref
;
21576 /* DR 1207: 'this' is in scope in the trailing return type. */
21577 inject_this_parameter (current_class_type
, quals
);
21580 if (token
->type
== CPP_DEREF
)
21582 /* Consume the ->. */
21583 cp_lexer_consume_token (parser
->lexer
);
21585 type
= cp_parser_trailing_type_id (parser
);
21588 /* Function declarations may be followed by a trailing
21589 requires-clause. */
21590 requires_clause
= cp_parser_requires_clause_opt (parser
);
21592 if (declare_simd_p
)
21593 declarator
->attributes
21594 = cp_parser_late_parsing_omp_declare_simd (parser
,
21595 declarator
->attributes
);
21596 if (oacc_routine_p
)
21597 declarator
->attributes
21598 = cp_parser_late_parsing_oacc_routine (parser
,
21599 declarator
->attributes
);
21603 current_class_ptr
= save_ccp
;
21604 current_class_ref
= save_ccr
;
21610 /* Parse a declarator-id.
21614 :: [opt] nested-name-specifier [opt] type-name
21616 In the `id-expression' case, the value returned is as for
21617 cp_parser_id_expression if the id-expression was an unqualified-id.
21618 If the id-expression was a qualified-id, then a SCOPE_REF is
21619 returned. The first operand is the scope (either a NAMESPACE_DECL
21620 or TREE_TYPE), but the second is still just a representation of an
21624 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21627 /* The expression must be an id-expression. Assume that qualified
21628 names are the names of types so that:
21631 int S<T>::R::i = 3;
21633 will work; we must treat `S<T>::R' as the name of a type.
21634 Similarly, assume that qualified names are templates, where
21638 int S<T>::R<T>::i = 3;
21641 id
= cp_parser_id_expression (parser
,
21642 /*template_keyword_p=*/false,
21643 /*check_dependency_p=*/false,
21644 /*template_p=*/NULL
,
21645 /*declarator_p=*/true,
21647 if (id
&& BASELINK_P (id
))
21648 id
= BASELINK_FUNCTIONS (id
);
21652 /* Parse a type-id.
21655 type-specifier-seq abstract-declarator [opt]
21657 The parser flags FLAGS is used to control type-specifier parsing.
21659 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21661 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21662 i.e. we've just seen "->".
21664 Returns the TYPE specified. */
21667 cp_parser_type_id_1 (cp_parser
*parser
, cp_parser_flags flags
,
21668 bool is_template_arg
, bool is_trailing_return
,
21669 location_t
*type_location
)
21671 cp_decl_specifier_seq type_specifier_seq
;
21672 cp_declarator
*abstract_declarator
;
21674 /* Parse the type-specifier-seq. */
21675 cp_parser_type_specifier_seq (parser
, flags
,
21676 /*is_declaration=*/false,
21677 is_trailing_return
,
21678 &type_specifier_seq
);
21680 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
21682 if (is_template_arg
&& type_specifier_seq
.type
21683 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21684 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21685 /* A bare template name as a template argument is a template template
21686 argument, not a placeholder, so fail parsing it as a type argument. */
21688 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21689 cp_parser_simulate_error (parser
);
21690 return error_mark_node
;
21692 if (type_specifier_seq
.type
== error_mark_node
)
21693 return error_mark_node
;
21695 /* There might or might not be an abstract declarator. */
21696 cp_parser_parse_tentatively (parser
);
21697 /* Look for the declarator. */
21698 abstract_declarator
21699 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
,
21700 CP_PARSER_FLAGS_NONE
, NULL
,
21701 /*parenthesized_p=*/NULL
,
21702 /*member_p=*/false,
21703 /*friend_p=*/false,
21704 /*static_p=*/false);
21705 /* Check to see if there really was a declarator. */
21706 if (!cp_parser_parse_definitely (parser
))
21707 abstract_declarator
= NULL
;
21709 if (type_specifier_seq
.type
21710 /* The concepts TS allows 'auto' as a type-id. */
21711 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21712 /* None of the valid uses of 'auto' in C++14 involve the type-id
21713 nonterminal, but it is valid in a trailing-return-type. */
21714 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21715 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21717 /* A type-id with type 'auto' is only ok if the abstract declarator
21718 is a function declarator with a late-specified return type.
21720 A type-id with 'auto' is also valid in a trailing-return-type
21721 in a compound-requirement. */
21722 if (abstract_declarator
21723 && abstract_declarator
->kind
== cdk_function
21724 && abstract_declarator
->u
.function
.late_return_type
)
21726 else if (parser
->in_result_type_constraint_p
)
21730 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21731 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21733 error_at (loc
, "missing template arguments after %qT",
21735 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21739 error_at (loc
, "invalid use of %qT", auto_node
);
21740 return error_mark_node
;
21744 return groktypename (&type_specifier_seq
, abstract_declarator
,
21748 /* Wrapper for cp_parser_type_id_1. */
21751 cp_parser_type_id (cp_parser
*parser
, cp_parser_flags flags
,
21752 location_t
*type_location
)
21754 return cp_parser_type_id_1 (parser
, flags
, false, false, type_location
);
21757 /* Wrapper for cp_parser_type_id_1. */
21760 cp_parser_template_type_arg (cp_parser
*parser
)
21763 const char *saved_message
= parser
->type_definition_forbidden_message
;
21764 parser
->type_definition_forbidden_message
21765 = G_("types may not be defined in template arguments");
21766 r
= cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_NONE
, true, false, NULL
);
21767 parser
->type_definition_forbidden_message
= saved_message
;
21768 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21770 error ("invalid use of %<auto%> in template argument");
21771 r
= error_mark_node
;
21776 /* Wrapper for cp_parser_type_id_1. */
21779 cp_parser_trailing_type_id (cp_parser
*parser
)
21781 return cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
21782 false, true, NULL
);
21785 /* Parse a type-specifier-seq.
21787 type-specifier-seq:
21788 type-specifier type-specifier-seq [opt]
21792 type-specifier-seq:
21793 attributes type-specifier-seq [opt]
21795 The parser flags FLAGS is used to control type-specifier parsing.
21797 If IS_DECLARATION is true, we are at the start of a "condition" or
21798 exception-declaration, so we might be followed by a declarator-id.
21800 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21801 i.e. we've just seen "->".
21803 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21806 cp_parser_type_specifier_seq (cp_parser
* parser
,
21807 cp_parser_flags flags
,
21808 bool is_declaration
,
21809 bool is_trailing_return
,
21810 cp_decl_specifier_seq
*type_specifier_seq
)
21812 bool seen_type_specifier
= false;
21813 cp_token
*start_token
= NULL
;
21815 /* Clear the TYPE_SPECIFIER_SEQ. */
21816 clear_decl_specs (type_specifier_seq
);
21818 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
21819 /* In the context of a trailing return type, enum E { } is an
21820 elaborated-type-specifier followed by a function-body, not an
21822 if (is_trailing_return
)
21823 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21825 /* Parse the type-specifiers and attributes. */
21828 tree type_specifier
;
21829 bool is_cv_qualifier
;
21831 /* Check for attributes first. */
21832 if (cp_next_tokens_can_be_attribute_p (parser
))
21834 type_specifier_seq
->attributes
21835 = attr_chainon (type_specifier_seq
->attributes
,
21836 cp_parser_attributes_opt (parser
));
21840 /* record the token of the beginning of the type specifier seq,
21841 for error reporting purposes*/
21843 start_token
= cp_lexer_peek_token (parser
->lexer
);
21845 /* Look for the type-specifier. */
21846 type_specifier
= cp_parser_type_specifier (parser
,
21848 type_specifier_seq
,
21849 /*is_declaration=*/false,
21852 if (!type_specifier
)
21854 /* If the first type-specifier could not be found, this is not a
21855 type-specifier-seq at all. */
21856 if (!seen_type_specifier
)
21858 /* Set in_declarator_p to avoid skipping to the semicolon. */
21859 int in_decl
= parser
->in_declarator_p
;
21860 parser
->in_declarator_p
= true;
21862 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21863 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21864 cp_parser_error (parser
, "expected type-specifier");
21866 parser
->in_declarator_p
= in_decl
;
21868 type_specifier_seq
->type
= error_mark_node
;
21871 /* If subsequent type-specifiers could not be found, the
21872 type-specifier-seq is complete. */
21876 seen_type_specifier
= true;
21877 /* The standard says that a condition can be:
21879 type-specifier-seq declarator = assignment-expression
21886 we should treat the "S" as a declarator, not as a
21887 type-specifier. The standard doesn't say that explicitly for
21888 type-specifier-seq, but it does say that for
21889 decl-specifier-seq in an ordinary declaration. Perhaps it
21890 would be clearer just to allow a decl-specifier-seq here, and
21891 then add a semantic restriction that if any decl-specifiers
21892 that are not type-specifiers appear, the program is invalid. */
21893 if (is_declaration
&& !is_cv_qualifier
)
21894 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21898 /* Return whether the function currently being declared has an associated
21899 template parameter list. */
21902 function_being_declared_is_template_p (cp_parser
* parser
)
21904 if (!current_template_parms
|| processing_template_parmlist
)
21907 if (parser
->implicit_template_scope
)
21910 if (at_class_scope_p ()
21911 && TYPE_BEING_DEFINED (current_class_type
))
21912 return parser
->num_template_parameter_lists
!= 0;
21914 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21915 (current_class_type
));
21918 /* Parse a parameter-declaration-clause.
21920 parameter-declaration-clause:
21921 parameter-declaration-list [opt] ... [opt]
21922 parameter-declaration-list , ...
21924 The parser flags FLAGS is used to control type-specifier parsing.
21926 Returns a representation for the parameter declarations. A return
21927 value of NULL indicates a parameter-declaration-clause consisting
21928 only of an ellipsis. */
21931 cp_parser_parameter_declaration_clause (cp_parser
* parser
,
21932 cp_parser_flags flags
)
21938 temp_override
<bool> cleanup
21939 (parser
->auto_is_implicit_function_template_parm_p
);
21941 if (!processing_specialization
21942 && !processing_template_parmlist
21943 && !processing_explicit_instantiation
21944 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21945 actual function or a random abstract declarator. */
21946 && parser
->default_arg_ok_p
)
21947 if (!current_function_decl
21948 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21949 parser
->auto_is_implicit_function_template_parm_p
= true;
21951 /* Peek at the next token. */
21952 token
= cp_lexer_peek_token (parser
->lexer
);
21953 /* Check for trivial parameter-declaration-clauses. */
21954 if (token
->type
== CPP_ELLIPSIS
)
21956 /* Consume the `...' token. */
21957 cp_lexer_consume_token (parser
->lexer
);
21960 else if (token
->type
== CPP_CLOSE_PAREN
)
21961 /* There are no parameters. */
21962 return void_list_node
;
21963 /* Check for `(void)', too, which is a special case. */
21964 else if (token
->keyword
== RID_VOID
21965 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21966 == CPP_CLOSE_PAREN
))
21968 /* Consume the `void' token. */
21969 cp_lexer_consume_token (parser
->lexer
);
21970 /* There are no parameters. */
21971 return void_list_node
;
21974 /* Parse the parameter-declaration-list. */
21975 parameters
= cp_parser_parameter_declaration_list (parser
, flags
);
21976 /* If a parse error occurred while parsing the
21977 parameter-declaration-list, then the entire
21978 parameter-declaration-clause is erroneous. */
21979 if (parameters
== error_mark_node
)
21982 /* Peek at the next token. */
21983 token
= cp_lexer_peek_token (parser
->lexer
);
21984 /* If it's a `,', the clause should terminate with an ellipsis. */
21985 if (token
->type
== CPP_COMMA
)
21987 /* Consume the `,'. */
21988 cp_lexer_consume_token (parser
->lexer
);
21989 /* Expect an ellipsis. */
21991 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21993 /* It might also be `...' if the optional trailing `,' was
21995 else if (token
->type
== CPP_ELLIPSIS
)
21997 /* Consume the `...' token. */
21998 cp_lexer_consume_token (parser
->lexer
);
21999 /* And remember that we saw it. */
22003 ellipsis_p
= false;
22005 /* Finish the parameter list. */
22007 parameters
= chainon (parameters
, void_list_node
);
22012 /* Parse a parameter-declaration-list.
22014 parameter-declaration-list:
22015 parameter-declaration
22016 parameter-declaration-list , parameter-declaration
22018 The parser flags FLAGS is used to control type-specifier parsing.
22020 Returns a representation of the parameter-declaration-list, as for
22021 cp_parser_parameter_declaration_clause. However, the
22022 `void_list_node' is never appended to the list. */
22025 cp_parser_parameter_declaration_list (cp_parser
* parser
, cp_parser_flags flags
)
22027 tree parameters
= NULL_TREE
;
22028 tree
*tail
= ¶meters
;
22029 bool saved_in_unbraced_linkage_specification_p
;
22032 /* The special considerations that apply to a function within an
22033 unbraced linkage specifications do not apply to the parameters
22034 to the function. */
22035 saved_in_unbraced_linkage_specification_p
22036 = parser
->in_unbraced_linkage_specification_p
;
22037 parser
->in_unbraced_linkage_specification_p
= false;
22039 /* Look for more parameters. */
22042 cp_parameter_declarator
*parameter
;
22043 tree decl
= error_mark_node
;
22044 bool parenthesized_p
= false;
22046 /* Parse the parameter. */
22048 = cp_parser_parameter_declaration (parser
, flags
,
22049 /*template_parm_p=*/false,
22052 /* We don't know yet if the enclosing context is deprecated, so wait
22053 and warn in grokparms if appropriate. */
22054 deprecated_state
= DEPRECATED_SUPPRESS
;
22058 decl
= grokdeclarator (parameter
->declarator
,
22059 ¶meter
->decl_specifiers
,
22061 parameter
->default_argument
!= NULL_TREE
,
22062 ¶meter
->decl_specifiers
.attributes
);
22063 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
22064 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
22067 deprecated_state
= DEPRECATED_NORMAL
;
22069 /* If a parse error occurred parsing the parameter declaration,
22070 then the entire parameter-declaration-list is erroneous. */
22071 if (decl
== error_mark_node
)
22073 parameters
= error_mark_node
;
22077 if (parameter
->decl_specifiers
.attributes
)
22078 cplus_decl_attributes (&decl
,
22079 parameter
->decl_specifiers
.attributes
,
22081 if (DECL_NAME (decl
))
22082 decl
= pushdecl (decl
);
22084 if (decl
!= error_mark_node
)
22086 retrofit_lang_decl (decl
);
22087 DECL_PARM_INDEX (decl
) = ++index
;
22088 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
22091 /* Add the new parameter to the list. */
22092 *tail
= build_tree_list (parameter
->default_argument
, decl
);
22093 tail
= &TREE_CHAIN (*tail
);
22095 /* Peek at the next token. */
22096 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
22097 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
22098 /* These are for Objective-C++ */
22099 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22100 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22101 /* The parameter-declaration-list is complete. */
22103 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22107 /* Peek at the next token. */
22108 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22109 /* If it's an ellipsis, then the list is complete. */
22110 if (token
->type
== CPP_ELLIPSIS
)
22112 /* Otherwise, there must be more parameters. Consume the
22114 cp_lexer_consume_token (parser
->lexer
);
22115 /* When parsing something like:
22117 int i(float f, double d)
22119 we can tell after seeing the declaration for "f" that we
22120 are not looking at an initialization of a variable "i",
22121 but rather at the declaration of a function "i".
22123 Due to the fact that the parsing of template arguments
22124 (as specified to a template-id) requires backtracking we
22125 cannot use this technique when inside a template argument
22127 if (!parser
->in_template_argument_list_p
22128 && !parser
->in_type_id_in_expr_p
22129 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22130 /* However, a parameter-declaration of the form
22131 "float(f)" (which is a valid declaration of a
22132 parameter "f") can also be interpreted as an
22133 expression (the conversion of "f" to "float"). */
22134 && !parenthesized_p
)
22135 cp_parser_commit_to_tentative_parse (parser
);
22139 cp_parser_error (parser
, "expected %<,%> or %<...%>");
22140 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
22141 cp_parser_skip_to_closing_parenthesis (parser
,
22142 /*recovering=*/true,
22143 /*or_comma=*/false,
22144 /*consume_paren=*/false);
22149 parser
->in_unbraced_linkage_specification_p
22150 = saved_in_unbraced_linkage_specification_p
;
22152 /* Reset implicit_template_scope if we are about to leave the function
22153 parameter list that introduced it. Note that for out-of-line member
22154 definitions, there will be one or more class scopes before we get to
22155 the template parameter scope. */
22157 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
22158 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
22160 while (maybe_its
->kind
== sk_class
)
22161 maybe_its
= maybe_its
->level_chain
;
22162 if (maybe_its
== its
)
22164 parser
->implicit_template_parms
= 0;
22165 parser
->implicit_template_scope
= 0;
22172 /* Parse a parameter declaration.
22174 parameter-declaration:
22175 decl-specifier-seq ... [opt] declarator
22176 decl-specifier-seq declarator = assignment-expression
22177 decl-specifier-seq ... [opt] abstract-declarator [opt]
22178 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22180 The parser flags FLAGS is used to control type-specifier parsing.
22182 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22183 declares a template parameter. (In that case, a non-nested `>'
22184 token encountered during the parsing of the assignment-expression
22185 is not interpreted as a greater-than operator.)
22187 Returns a representation of the parameter, or NULL if an error
22188 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22189 true iff the declarator is of the form "(p)". */
22191 static cp_parameter_declarator
*
22192 cp_parser_parameter_declaration (cp_parser
*parser
,
22193 cp_parser_flags flags
,
22194 bool template_parm_p
,
22195 bool *parenthesized_p
)
22197 int declares_class_or_enum
;
22198 cp_decl_specifier_seq decl_specifiers
;
22199 cp_declarator
*declarator
;
22200 tree default_argument
;
22201 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
22202 const char *saved_message
;
22203 bool template_parameter_pack_p
= false;
22205 /* In a template parameter, `>' is not an operator.
22209 When parsing a default template-argument for a non-type
22210 template-parameter, the first non-nested `>' is taken as the end
22211 of the template parameter-list rather than a greater-than
22214 /* Type definitions may not appear in parameter types. */
22215 saved_message
= parser
->type_definition_forbidden_message
;
22216 parser
->type_definition_forbidden_message
22217 = G_("types may not be defined in parameter types");
22219 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
22220 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22221 (current_template_parms
)) : 0);
22223 /* Parse the declaration-specifiers. */
22224 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
22225 cp_parser_decl_specifier_seq (parser
,
22228 &declares_class_or_enum
);
22230 /* Complain about missing 'typename' or other invalid type names. */
22231 if (!decl_specifiers
.any_type_specifiers_p
22232 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22233 decl_specifiers
.type
= error_mark_node
;
22235 /* If an error occurred, there's no reason to attempt to parse the
22236 rest of the declaration. */
22237 if (cp_parser_error_occurred (parser
))
22239 parser
->type_definition_forbidden_message
= saved_message
;
22243 /* Peek at the next token. */
22244 token
= cp_lexer_peek_token (parser
->lexer
);
22246 /* If the next token is a `)', `,', `=', `>', or `...', then there
22247 is no declarator. However, when variadic templates are enabled,
22248 there may be a declarator following `...'. */
22249 if (token
->type
== CPP_CLOSE_PAREN
22250 || token
->type
== CPP_COMMA
22251 || token
->type
== CPP_EQ
22252 || token
->type
== CPP_GREATER
)
22255 if (parenthesized_p
)
22256 *parenthesized_p
= false;
22258 /* Otherwise, there should be a declarator. */
22261 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22262 parser
->default_arg_ok_p
= false;
22264 /* After seeing a decl-specifier-seq, if the next token is not a
22265 "(", there is no possibility that the code is a valid
22266 expression. Therefore, if parsing tentatively, we commit at
22268 if (!parser
->in_template_argument_list_p
22269 /* In an expression context, having seen:
22273 we cannot be sure whether we are looking at a
22274 function-type (taking a "char" as a parameter) or a cast
22275 of some object of type "char" to "int". */
22276 && !parser
->in_type_id_in_expr_p
22277 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22278 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
22279 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
22280 cp_parser_commit_to_tentative_parse (parser
);
22281 /* Parse the declarator. */
22282 declarator_token_start
= token
;
22283 declarator
= cp_parser_declarator (parser
,
22284 CP_PARSER_DECLARATOR_EITHER
,
22285 CP_PARSER_FLAGS_NONE
,
22286 /*ctor_dtor_or_conv_p=*/NULL
,
22288 /*member_p=*/false,
22289 /*friend_p=*/false,
22290 /*static_p=*/false);
22291 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22292 /* After the declarator, allow more attributes. */
22293 decl_specifiers
.attributes
22294 = attr_chainon (decl_specifiers
.attributes
,
22295 cp_parser_attributes_opt (parser
));
22297 /* If the declarator is a template parameter pack, remember that and
22298 clear the flag in the declarator itself so we don't get errors
22299 from grokdeclarator. */
22300 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
22302 declarator
->parameter_pack_p
= false;
22303 template_parameter_pack_p
= true;
22307 /* If the next token is an ellipsis, and we have not seen a declarator
22308 name, and if either the type of the declarator contains parameter
22309 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22310 for, eg, abbreviated integral type names), then we actually have a
22311 parameter pack expansion expression. Otherwise, leave the ellipsis
22312 for a C-style variadic function. */
22313 token
= cp_lexer_peek_token (parser
->lexer
);
22315 /* If a function parameter pack was specified and an implicit template
22316 parameter was introduced during cp_parser_parameter_declaration,
22317 change any implicit parameters introduced into packs. */
22318 if (parser
->implicit_template_parms
22319 && ((token
->type
== CPP_ELLIPSIS
22320 && declarator_can_be_parameter_pack (declarator
))
22321 || (declarator
&& declarator
->parameter_pack_p
)))
22323 int latest_template_parm_idx
= TREE_VEC_LENGTH
22324 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
22326 if (latest_template_parm_idx
!= template_parm_idx
)
22327 decl_specifiers
.type
= convert_generic_types_to_packs
22328 (decl_specifiers
.type
,
22329 template_parm_idx
, latest_template_parm_idx
);
22332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22334 tree type
= decl_specifiers
.type
;
22336 if (type
&& DECL_P (type
))
22337 type
= TREE_TYPE (type
);
22340 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
22341 && (template_parm_p
|| uses_parameter_packs (type
)))
22342 || (!type
&& template_parm_p
))
22343 && declarator_can_be_parameter_pack (declarator
))
22345 /* Consume the `...'. */
22346 cp_lexer_consume_token (parser
->lexer
);
22347 maybe_warn_variadic_templates ();
22349 /* Build a pack expansion type */
22350 if (template_parm_p
)
22351 template_parameter_pack_p
= true;
22352 else if (declarator
)
22353 declarator
->parameter_pack_p
= true;
22355 decl_specifiers
.type
= make_pack_expansion (type
);
22359 /* The restriction on defining new types applies only to the type
22360 of the parameter, not to the default argument. */
22361 parser
->type_definition_forbidden_message
= saved_message
;
22363 /* If the next token is `=', then process a default argument. */
22364 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22366 tree type
= decl_specifiers
.type
;
22367 token
= cp_lexer_peek_token (parser
->lexer
);
22368 /* If we are defining a class, then the tokens that make up the
22369 default argument must be saved and processed later. */
22370 if (!template_parm_p
&& at_class_scope_p ()
22371 && TYPE_BEING_DEFINED (current_class_type
)
22372 && !LAMBDA_TYPE_P (current_class_type
))
22373 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
22375 // A constrained-type-specifier may declare a type template-parameter.
22376 else if (declares_constrained_type_template_parameter (type
))
22378 = cp_parser_default_type_template_argument (parser
);
22380 // A constrained-type-specifier may declare a template-template-parameter.
22381 else if (declares_constrained_template_template_parameter (type
))
22383 = cp_parser_default_template_template_argument (parser
);
22385 /* Outside of a class definition, we can just parse the
22386 assignment-expression. */
22389 = cp_parser_default_argument (parser
, template_parm_p
);
22391 if (!parser
->default_arg_ok_p
)
22393 permerror (token
->location
,
22394 "default arguments are only "
22395 "permitted for function parameters");
22397 else if ((declarator
&& declarator
->parameter_pack_p
)
22398 || template_parameter_pack_p
22399 || (decl_specifiers
.type
22400 && PACK_EXPANSION_P (decl_specifiers
.type
)))
22402 /* Find the name of the parameter pack. */
22403 cp_declarator
*id_declarator
= declarator
;
22404 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
22405 id_declarator
= id_declarator
->declarator
;
22407 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
22408 error_at (declarator_token_start
->location
,
22410 ? G_("template parameter pack %qD "
22411 "cannot have a default argument")
22412 : G_("parameter pack %qD cannot have "
22413 "a default argument"),
22414 id_declarator
->u
.id
.unqualified_name
);
22416 error_at (declarator_token_start
->location
,
22418 ? G_("template parameter pack cannot have "
22419 "a default argument")
22420 : G_("parameter pack cannot have a "
22421 "default argument"));
22423 default_argument
= NULL_TREE
;
22427 default_argument
= NULL_TREE
;
22429 if (default_argument
)
22430 STRIP_ANY_LOCATION_WRAPPER (default_argument
);
22432 /* Generate a location for the parameter, ranging from the start of the
22433 initial token to the end of the final token (using input_location for
22434 the latter, set up by cp_lexer_set_source_position_from_token when
22437 If we have a identifier, then use it for the caret location, e.g.
22439 extern int callee (int one, int (*two)(int, int), float three);
22440 ~~~~~~^~~~~~~~~~~~~~
22442 otherwise, reuse the start location for the caret location e.g.:
22444 extern int callee (int one, int (*)(int, int), float three);
22448 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
22449 ? declarator
->id_loc
22450 : decl_spec_token_start
->location
);
22451 location_t param_loc
= make_location (caret_loc
,
22452 decl_spec_token_start
->location
,
22455 return make_parameter_declarator (&decl_specifiers
,
22459 template_parameter_pack_p
);
22462 /* Parse a default argument and return it.
22464 TEMPLATE_PARM_P is true if this is a default argument for a
22465 non-type template parameter. */
22467 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
22469 tree default_argument
= NULL_TREE
;
22470 bool saved_greater_than_is_operator_p
;
22471 unsigned char saved_local_variables_forbidden_p
;
22472 bool non_constant_p
, is_direct_init
;
22474 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22476 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
22477 parser
->greater_than_is_operator_p
= !template_parm_p
;
22478 /* Local variable names (and the `this' keyword) may not
22479 appear in a default argument. */
22480 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22481 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
22482 /* Parse the assignment-expression. */
22483 if (template_parm_p
)
22484 push_deferring_access_checks (dk_no_deferred
);
22485 tree saved_class_ptr
= NULL_TREE
;
22486 tree saved_class_ref
= NULL_TREE
;
22487 /* The "this" pointer is not valid in a default argument. */
22490 saved_class_ptr
= current_class_ptr
;
22491 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
22492 saved_class_ref
= current_class_ref
;
22493 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
22496 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
22497 /* Restore the "this" pointer. */
22500 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
22501 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
22503 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
22504 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22505 if (template_parm_p
)
22506 pop_deferring_access_checks ();
22507 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
22508 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22510 return default_argument
;
22513 /* Parse a function-body.
22516 compound_statement */
22519 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
22521 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
22522 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
22526 /* Parse a ctor-initializer-opt followed by a function-body. Return
22527 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22528 is true we are parsing a function-try-block. */
22531 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
22532 bool in_function_try_block
)
22535 const bool check_body_p
=
22536 DECL_CONSTRUCTOR_P (current_function_decl
)
22537 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
22540 /* Begin the function body. */
22541 body
= begin_function_body ();
22542 /* Parse the optional ctor-initializer. */
22543 cp_parser_ctor_initializer_opt (parser
);
22545 /* If we're parsing a constexpr constructor definition, we need
22546 to check that the constructor body is indeed empty. However,
22547 before we get to cp_parser_function_body lot of junk has been
22548 generated, so we can't just check that we have an empty block.
22549 Rather we take a snapshot of the outermost block, and check whether
22550 cp_parser_function_body changed its state. */
22553 list
= cur_stmt_list
;
22554 if (STATEMENT_LIST_TAIL (list
))
22555 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
22557 /* Parse the function-body. */
22558 cp_parser_function_body (parser
, in_function_try_block
);
22560 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
22561 /* Finish the function body. */
22562 finish_function_body (body
);
22565 /* Parse an initializer.
22568 = initializer-clause
22569 ( expression-list )
22571 Returns an expression representing the initializer. If no
22572 initializer is present, NULL_TREE is returned.
22574 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22575 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22576 set to TRUE if there is no initializer present. If there is an
22577 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22578 is set to true; otherwise it is set to false. */
22581 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
22582 bool* non_constant_p
, bool subexpression_p
)
22587 /* Peek at the next token. */
22588 token
= cp_lexer_peek_token (parser
->lexer
);
22590 /* Let our caller know whether or not this initializer was
22592 *is_direct_init
= (token
->type
!= CPP_EQ
);
22593 /* Assume that the initializer is constant. */
22594 *non_constant_p
= false;
22596 if (token
->type
== CPP_EQ
)
22598 /* Consume the `='. */
22599 cp_lexer_consume_token (parser
->lexer
);
22600 /* Parse the initializer-clause. */
22601 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22603 else if (token
->type
== CPP_OPEN_PAREN
)
22605 vec
<tree
, va_gc
> *vec
;
22606 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22608 /*allow_expansion_p=*/true,
22611 return error_mark_node
;
22612 init
= build_tree_list_vec (vec
);
22613 release_tree_vector (vec
);
22615 else if (token
->type
== CPP_OPEN_BRACE
)
22617 cp_lexer_set_source_position (parser
->lexer
);
22618 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22619 init
= cp_parser_braced_list (parser
, non_constant_p
);
22620 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22624 /* Anything else is an error. */
22625 cp_parser_error (parser
, "expected initializer");
22626 init
= error_mark_node
;
22629 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22630 init
= error_mark_node
;
22635 /* Parse an initializer-clause.
22637 initializer-clause:
22638 assignment-expression
22641 Returns an expression representing the initializer.
22643 If the `assignment-expression' production is used the value
22644 returned is simply a representation for the expression.
22646 Otherwise, calls cp_parser_braced_list. */
22649 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22651 cp_expr initializer
;
22653 /* Assume the expression is constant. */
22654 *non_constant_p
= false;
22656 /* If it is not a `{', then we are looking at an
22657 assignment-expression. */
22658 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22661 = cp_parser_constant_expression (parser
,
22662 /*allow_non_constant_p=*/true,
22666 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22668 return initializer
;
22671 /* Parse a brace-enclosed initializer list.
22674 { initializer-list , [opt] }
22675 { designated-initializer-list , [opt] }
22678 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22679 the elements of the initializer-list (or NULL, if the last
22680 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22681 NULL_TREE. There is no way to detect whether or not the optional
22682 trailing `,' was provided. NON_CONSTANT_P is as for
22683 cp_parser_initializer. */
22686 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22689 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22691 /* Consume the `{' token. */
22692 matching_braces braces
;
22693 braces
.require_open (parser
);
22694 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22695 initializer
= make_node (CONSTRUCTOR
);
22696 /* If it's not a `}', then there is a non-trivial initializer. */
22697 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22699 /* Parse the initializer list. */
22700 CONSTRUCTOR_ELTS (initializer
)
22701 = cp_parser_initializer_list (parser
, non_constant_p
);
22702 /* A trailing `,' token is allowed. */
22703 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22704 cp_lexer_consume_token (parser
->lexer
);
22707 *non_constant_p
= false;
22708 /* Now, there should be a trailing `}'. */
22709 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22710 braces
.require_close (parser
);
22711 TREE_TYPE (initializer
) = init_list_type_node
;
22713 cp_expr
result (initializer
);
22714 /* Build a location of the form:
22717 with caret==start at the open brace, finish at the close brace. */
22718 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22719 result
.set_location (combined_loc
);
22723 /* Consume tokens up to, and including, the next non-nested closing `]'.
22724 Returns true iff we found a closing `]'. */
22727 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22729 unsigned square_depth
= 0;
22733 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22735 switch (token
->type
)
22737 case CPP_PRAGMA_EOL
:
22738 if (!parser
->lexer
->in_pragma
)
22742 /* If we've run out of tokens, then there is no closing `]'. */
22745 case CPP_OPEN_SQUARE
:
22749 case CPP_CLOSE_SQUARE
:
22750 if (!square_depth
--)
22752 cp_lexer_consume_token (parser
->lexer
);
22761 /* Consume the token. */
22762 cp_lexer_consume_token (parser
->lexer
);
22766 /* Return true if we are looking at an array-designator, false otherwise. */
22769 cp_parser_array_designator_p (cp_parser
*parser
)
22771 /* Consume the `['. */
22772 cp_lexer_consume_token (parser
->lexer
);
22774 cp_lexer_save_tokens (parser
->lexer
);
22776 /* Skip tokens until the next token is a closing square bracket.
22777 If we find the closing `]', and the next token is a `=', then
22778 we are looking at an array designator. */
22779 bool array_designator_p
22780 = (cp_parser_skip_to_closing_square_bracket (parser
)
22781 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22783 /* Roll back the tokens we skipped. */
22784 cp_lexer_rollback_tokens (parser
->lexer
);
22786 return array_designator_p
;
22789 /* Parse an initializer-list.
22792 initializer-clause ... [opt]
22793 initializer-list , initializer-clause ... [opt]
22797 designated-initializer-list:
22798 designated-initializer-clause
22799 designated-initializer-list , designated-initializer-clause
22801 designated-initializer-clause:
22802 designator brace-or-equal-initializer
22810 designation initializer-clause ...[opt]
22811 initializer-list , designation initializer-clause ...[opt]
22816 [ constant-expression ] =
22818 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22819 for the initializer. If the INDEX of the elt is non-NULL, it is the
22820 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22821 as for cp_parser_initializer. */
22823 static vec
<constructor_elt
, va_gc
> *
22824 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22826 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22827 bool first_p
= true;
22828 tree first_designator
= NULL_TREE
;
22830 /* Assume all of the expressions are constant. */
22831 *non_constant_p
= false;
22833 /* Parse the rest of the list. */
22839 bool clause_non_constant_p
;
22840 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22842 /* Handle the C++2A syntax, '. id ='. */
22843 if ((cxx_dialect
>= cxx2a
22844 || cp_parser_allow_gnu_extensions_p (parser
))
22845 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22846 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22847 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22848 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22849 == CPP_OPEN_BRACE
)))
22851 if (cxx_dialect
< cxx2a
)
22852 pedwarn (loc
, OPT_Wpedantic
,
22853 "C++ designated initializers only available with "
22854 "-std=c++2a or -std=gnu++2a");
22855 /* Consume the `.'. */
22856 cp_lexer_consume_token (parser
->lexer
);
22857 /* Consume the identifier. */
22858 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22859 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22860 /* Consume the `='. */
22861 cp_lexer_consume_token (parser
->lexer
);
22863 /* Also, if the next token is an identifier and the following one is a
22864 colon, we are looking at the GNU designated-initializer
22866 else if (cp_parser_allow_gnu_extensions_p (parser
)
22867 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22868 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22871 /* Warn the user that they are using an extension. */
22872 pedwarn (loc
, OPT_Wpedantic
,
22873 "ISO C++ does not allow GNU designated initializers");
22874 /* Consume the identifier. */
22875 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22876 /* Consume the `:'. */
22877 cp_lexer_consume_token (parser
->lexer
);
22879 /* Also handle C99 array designators, '[ const ] ='. */
22880 else if (cp_parser_allow_gnu_extensions_p (parser
)
22881 && !c_dialect_objc ()
22882 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22884 /* In C++11, [ could start a lambda-introducer. */
22885 bool non_const
= false;
22887 cp_parser_parse_tentatively (parser
);
22889 if (!cp_parser_array_designator_p (parser
))
22891 cp_parser_simulate_error (parser
);
22892 designator
= NULL_TREE
;
22896 designator
= cp_parser_constant_expression (parser
, true,
22898 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22899 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22902 if (!cp_parser_parse_definitely (parser
))
22903 designator
= NULL_TREE
;
22905 && (!require_potential_rvalue_constant_expression
22907 designator
= NULL_TREE
;
22909 /* Warn the user that they are using an extension. */
22910 pedwarn (loc
, OPT_Wpedantic
,
22911 "ISO C++ does not allow C99 designated initializers");
22914 designator
= NULL_TREE
;
22918 first_designator
= designator
;
22921 else if (cxx_dialect
>= cxx2a
22922 && first_designator
!= error_mark_node
22923 && (!first_designator
!= !designator
))
22925 error_at (loc
, "either all initializer clauses should be designated "
22926 "or none of them should be");
22927 first_designator
= error_mark_node
;
22929 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22930 first_designator
= designator
;
22932 /* Parse the initializer. */
22933 initializer
= cp_parser_initializer_clause (parser
,
22934 &clause_non_constant_p
);
22935 /* If any clause is non-constant, so is the entire initializer. */
22936 if (clause_non_constant_p
)
22937 *non_constant_p
= true;
22939 /* If we have an ellipsis, this is an initializer pack
22941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22943 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22945 /* Consume the `...'. */
22946 cp_lexer_consume_token (parser
->lexer
);
22948 if (designator
&& cxx_dialect
>= cxx2a
)
22950 "%<...%> not allowed in designated initializer list");
22952 /* Turn the initializer into an initializer expansion. */
22953 initializer
= make_pack_expansion (initializer
);
22956 /* Add it to the vector. */
22957 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22959 /* If the next token is not a comma, we have reached the end of
22961 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22964 /* Peek at the next token. */
22965 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22966 /* If the next token is a `}', then we're still done. An
22967 initializer-clause can have a trailing `,' after the
22968 initializer-list and before the closing `}'. */
22969 if (token
->type
== CPP_CLOSE_BRACE
)
22972 /* Consume the `,' token. */
22973 cp_lexer_consume_token (parser
->lexer
);
22976 /* The same identifier shall not appear in multiple designators
22977 of a designated-initializer-list. */
22978 if (first_designator
)
22981 tree designator
, val
;
22982 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22983 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22985 if (IDENTIFIER_MARKED (designator
))
22987 error_at (cp_expr_loc_or_loc (val
, input_location
),
22988 "%<.%s%> designator used multiple times in "
22989 "the same initializer list",
22990 IDENTIFIER_POINTER (designator
));
22991 (*v
)[i
].index
= error_mark_node
;
22994 IDENTIFIER_MARKED (designator
) = 1;
22996 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22997 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22998 IDENTIFIER_MARKED (designator
) = 0;
23004 /* Classes [gram.class] */
23006 /* Parse a class-name.
23012 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23013 to indicate that names looked up in dependent types should be
23014 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23015 keyword has been used to indicate that the name that appears next
23016 is a template. TAG_TYPE indicates the explicit tag given before
23017 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23018 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23019 is the class being defined in a class-head. If ENUM_OK is TRUE,
23020 enum-names are also accepted.
23022 Returns the TYPE_DECL representing the class. */
23025 cp_parser_class_name (cp_parser
*parser
,
23026 bool typename_keyword_p
,
23027 bool template_keyword_p
,
23028 enum tag_types tag_type
,
23029 bool check_dependency_p
,
23031 bool is_declaration
,
23038 tree identifier
= NULL_TREE
;
23040 /* All class-names start with an identifier. */
23041 token
= cp_lexer_peek_token (parser
->lexer
);
23042 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
23044 cp_parser_error (parser
, "expected class-name");
23045 return error_mark_node
;
23048 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23049 to a template-id, so we save it here. */
23050 scope
= parser
->scope
;
23051 if (scope
== error_mark_node
)
23052 return error_mark_node
;
23054 /* Any name names a type if we're following the `typename' keyword
23055 in a qualified name where the enclosing scope is type-dependent. */
23056 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
23057 && dependent_type_p (scope
));
23058 /* Handle the common case (an identifier, but not a template-id)
23060 if (token
->type
== CPP_NAME
23061 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
23063 cp_token
*identifier_token
;
23066 /* Look for the identifier. */
23067 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
23068 ambiguous_p
= identifier_token
->error_reported
;
23069 identifier
= cp_parser_identifier (parser
);
23070 /* If the next token isn't an identifier, we are certainly not
23071 looking at a class-name. */
23072 if (identifier
== error_mark_node
)
23073 decl
= error_mark_node
;
23074 /* If we know this is a type-name, there's no need to look it
23076 else if (typename_p
)
23080 tree ambiguous_decls
;
23081 /* If we already know that this lookup is ambiguous, then
23082 we've already issued an error message; there's no reason
23086 cp_parser_simulate_error (parser
);
23087 return error_mark_node
;
23089 /* If the next token is a `::', then the name must be a type
23092 [basic.lookup.qual]
23094 During the lookup for a name preceding the :: scope
23095 resolution operator, object, function, and enumerator
23096 names are ignored. */
23097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23098 tag_type
= scope_type
;
23099 /* Look up the name. */
23100 decl
= cp_parser_lookup_name (parser
, identifier
,
23102 /*is_template=*/false,
23103 /*is_namespace=*/false,
23104 check_dependency_p
,
23106 identifier_token
->location
);
23107 if (ambiguous_decls
)
23109 if (cp_parser_parsing_tentatively (parser
))
23110 cp_parser_simulate_error (parser
);
23111 return error_mark_node
;
23117 /* Try a template-id. */
23118 decl
= cp_parser_template_id (parser
, template_keyword_p
,
23119 check_dependency_p
,
23122 if (decl
== error_mark_node
)
23123 return error_mark_node
;
23126 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
23128 /* If this is a typename, create a TYPENAME_TYPE. */
23129 if (typename_p
&& decl
!= error_mark_node
)
23131 decl
= make_typename_type (scope
, decl
, typename_type
,
23132 /*complain=*/tf_error
);
23133 if (decl
!= error_mark_node
)
23134 decl
= TYPE_NAME (decl
);
23137 decl
= strip_using_decl (decl
);
23139 /* Check to see that it is really the name of a class. */
23140 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
23141 && identifier_p (TREE_OPERAND (decl
, 0))
23142 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23143 /* Situations like this:
23145 template <typename T> struct A {
23146 typename T::template X<int>::I i;
23149 are problematic. Is `T::template X<int>' a class-name? The
23150 standard does not seem to be definitive, but there is no other
23151 valid interpretation of the following `::'. Therefore, those
23152 names are considered class-names. */
23154 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
23155 if (decl
!= error_mark_node
)
23156 decl
= TYPE_NAME (decl
);
23158 else if (TREE_CODE (decl
) != TYPE_DECL
23159 || TREE_TYPE (decl
) == error_mark_node
23160 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
23161 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
23162 /* In Objective-C 2.0, a classname followed by '.' starts a
23163 dot-syntax expression, and it's not a type-name. */
23164 || (c_dialect_objc ()
23165 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
23166 && objc_is_class_name (decl
)))
23167 decl
= error_mark_node
;
23169 if (decl
== error_mark_node
)
23170 cp_parser_error (parser
, "expected class-name");
23171 else if (identifier
&& !parser
->scope
)
23172 maybe_note_name_used_in_class (identifier
, decl
);
23177 /* Parse a class-specifier.
23180 class-head { member-specification [opt] }
23182 Returns the TREE_TYPE representing the class. */
23185 cp_parser_class_specifier_1 (cp_parser
* parser
)
23188 tree attributes
= NULL_TREE
;
23189 bool nested_name_specifier_p
;
23190 unsigned saved_num_template_parameter_lists
;
23191 bool saved_in_function_body
;
23192 unsigned char in_statement
;
23193 bool in_switch_statement_p
;
23194 bool saved_in_unbraced_linkage_specification_p
;
23195 tree old_scope
= NULL_TREE
;
23196 tree scope
= NULL_TREE
;
23197 cp_token
*closing_brace
;
23199 push_deferring_access_checks (dk_no_deferred
);
23201 /* Parse the class-head. */
23202 type
= cp_parser_class_head (parser
,
23203 &nested_name_specifier_p
);
23204 /* If the class-head was a semantic disaster, skip the entire body
23208 cp_parser_skip_to_end_of_block_or_statement (parser
);
23209 pop_deferring_access_checks ();
23210 return error_mark_node
;
23213 /* Look for the `{'. */
23214 matching_braces braces
;
23215 if (!braces
.require_open (parser
))
23217 pop_deferring_access_checks ();
23218 return error_mark_node
;
23221 cp_ensure_no_omp_declare_simd (parser
);
23222 cp_ensure_no_oacc_routine (parser
);
23224 /* Issue an error message if type-definitions are forbidden here. */
23225 bool type_definition_ok_p
= cp_parser_check_type_definition (parser
);
23226 /* Remember that we are defining one more class. */
23227 ++parser
->num_classes_being_defined
;
23228 /* Inside the class, surrounding template-parameter-lists do not
23230 saved_num_template_parameter_lists
23231 = parser
->num_template_parameter_lists
;
23232 parser
->num_template_parameter_lists
= 0;
23233 /* We are not in a function body. */
23234 saved_in_function_body
= parser
->in_function_body
;
23235 parser
->in_function_body
= false;
23236 /* Or in a loop. */
23237 in_statement
= parser
->in_statement
;
23238 parser
->in_statement
= 0;
23239 /* Or in a switch. */
23240 in_switch_statement_p
= parser
->in_switch_statement_p
;
23241 parser
->in_switch_statement_p
= false;
23242 /* We are not immediately inside an extern "lang" block. */
23243 saved_in_unbraced_linkage_specification_p
23244 = parser
->in_unbraced_linkage_specification_p
;
23245 parser
->in_unbraced_linkage_specification_p
= false;
23247 // Associate constraints with the type.
23249 type
= associate_classtype_constraints (type
);
23251 /* Start the class. */
23252 if (nested_name_specifier_p
)
23254 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
23255 old_scope
= push_inner_scope (scope
);
23257 type
= begin_class_definition (type
);
23259 if (type
== error_mark_node
)
23260 /* If the type is erroneous, skip the entire body of the class. */
23261 cp_parser_skip_to_closing_brace (parser
);
23263 /* Parse the member-specification. */
23264 cp_parser_member_specification_opt (parser
);
23266 /* Look for the trailing `}'. */
23267 closing_brace
= braces
.require_close (parser
);
23268 /* Look for trailing attributes to apply to this class. */
23269 if (cp_parser_allow_gnu_extensions_p (parser
))
23270 attributes
= cp_parser_gnu_attributes_opt (parser
);
23271 if (type
!= error_mark_node
)
23272 type
= finish_struct (type
, attributes
);
23273 if (nested_name_specifier_p
)
23274 pop_inner_scope (old_scope
, scope
);
23276 /* We've finished a type definition. Check for the common syntax
23277 error of forgetting a semicolon after the definition. We need to
23278 be careful, as we can't just check for not-a-semicolon and be done
23279 with it; the user might have typed:
23281 class X { } c = ...;
23282 class X { } *p = ...;
23284 and so forth. Instead, enumerate all the possible tokens that
23285 might follow this production; if we don't see one of them, then
23286 complain and silently insert the semicolon. */
23288 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23289 bool want_semicolon
= true;
23291 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23292 /* Don't try to parse c++11 attributes here. As per the
23293 grammar, that should be a task for
23294 cp_parser_decl_specifier_seq. */
23295 want_semicolon
= false;
23297 switch (token
->type
)
23300 case CPP_SEMICOLON
:
23303 case CPP_OPEN_PAREN
:
23304 case CPP_CLOSE_PAREN
:
23306 want_semicolon
= false;
23309 /* While it's legal for type qualifiers and storage class
23310 specifiers to follow type definitions in the grammar, only
23311 compiler testsuites contain code like that. Assume that if
23312 we see such code, then what we're really seeing is a case
23316 const <type> var = ...;
23321 static <type> func (...) ...
23323 i.e. the qualifier or specifier applies to the next
23324 declaration. To do so, however, we need to look ahead one
23325 more token to see if *that* token is a type specifier.
23327 This code could be improved to handle:
23330 static const <type> var = ...; */
23332 if (keyword_is_decl_specifier (token
->keyword
))
23334 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23336 /* Handling user-defined types here would be nice, but very
23339 = (lookahead
->type
== CPP_KEYWORD
23340 && keyword_begins_type_specifier (lookahead
->keyword
));
23347 /* If we don't have a type, then something is very wrong and we
23348 shouldn't try to do anything clever. Likewise for not seeing the
23350 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
23352 /* Locate the closing brace. */
23353 cp_token_position prev
23354 = cp_lexer_previous_token_position (parser
->lexer
);
23355 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
23356 location_t loc
= prev_token
->location
;
23358 /* We want to suggest insertion of a ';' immediately *after* the
23359 closing brace, so, if we can, offset the location by 1 column. */
23360 location_t next_loc
= loc
;
23361 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
23362 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
23364 rich_location
richloc (line_table
, next_loc
);
23366 /* If we successfully offset the location, suggest the fix-it. */
23367 if (next_loc
!= loc
)
23368 richloc
.add_fixit_insert_before (next_loc
, ";");
23370 if (CLASSTYPE_DECLARED_CLASS (type
))
23371 error_at (&richloc
,
23372 "expected %<;%> after class definition");
23373 else if (TREE_CODE (type
) == RECORD_TYPE
)
23374 error_at (&richloc
,
23375 "expected %<;%> after struct definition");
23376 else if (TREE_CODE (type
) == UNION_TYPE
)
23377 error_at (&richloc
,
23378 "expected %<;%> after union definition");
23380 gcc_unreachable ();
23382 /* Unget one token and smash it to look as though we encountered
23383 a semicolon in the input stream. */
23384 cp_lexer_set_token_position (parser
->lexer
, prev
);
23385 token
= cp_lexer_peek_token (parser
->lexer
);
23386 token
->type
= CPP_SEMICOLON
;
23387 token
->keyword
= RID_MAX
;
23391 /* If this class is not itself within the scope of another class,
23392 then we need to parse the bodies of all of the queued function
23393 definitions. Note that the queued functions defined in a class
23394 are not always processed immediately following the
23395 class-specifier for that class. Consider:
23398 struct B { void f() { sizeof (A); } };
23401 If `f' were processed before the processing of `A' were
23402 completed, there would be no way to compute the size of `A'.
23403 Note that the nesting we are interested in here is lexical --
23404 not the semantic nesting given by TYPE_CONTEXT. In particular,
23407 struct A { struct B; };
23408 struct A::B { void f() { } };
23410 there is no need to delay the parsing of `A::B::f'. */
23411 if (--parser
->num_classes_being_defined
== 0)
23414 tree class_type
= NULL_TREE
;
23415 tree pushed_scope
= NULL_TREE
;
23417 cp_default_arg_entry
*e
;
23418 tree save_ccp
, save_ccr
;
23420 if (!type_definition_ok_p
|| any_erroneous_template_args_p (type
))
23422 /* Skip default arguments, NSDMIs, etc, in order to improve
23423 error recovery (c++/71169, c++/71832). */
23424 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23425 vec_safe_truncate (unparsed_nsdmis
, 0);
23426 vec_safe_truncate (unparsed_classes
, 0);
23427 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23430 /* In a first pass, parse default arguments to the functions.
23431 Then, in a second pass, parse the bodies of the functions.
23432 This two-phased approach handles cases like:
23440 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
23443 /* If there are default arguments that have not yet been processed,
23444 take care of them now. */
23445 if (class_type
!= e
->class_type
)
23448 pop_scope (pushed_scope
);
23449 class_type
= e
->class_type
;
23450 pushed_scope
= push_scope (class_type
);
23452 /* Make sure that any template parameters are in scope. */
23453 maybe_begin_member_template_processing (decl
);
23454 /* Parse the default argument expressions. */
23455 cp_parser_late_parsing_default_args (parser
, decl
);
23456 /* Remove any template parameters from the symbol table. */
23457 maybe_end_member_template_processing ();
23459 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23460 /* Now parse any NSDMIs. */
23461 save_ccp
= current_class_ptr
;
23462 save_ccr
= current_class_ref
;
23463 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
23465 if (class_type
!= DECL_CONTEXT (decl
))
23468 pop_scope (pushed_scope
);
23469 class_type
= DECL_CONTEXT (decl
);
23470 pushed_scope
= push_scope (class_type
);
23472 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
23473 cp_parser_late_parsing_nsdmi (parser
, decl
);
23475 vec_safe_truncate (unparsed_nsdmis
, 0);
23476 current_class_ptr
= save_ccp
;
23477 current_class_ref
= save_ccr
;
23479 pop_scope (pushed_scope
);
23481 /* Now do some post-NSDMI bookkeeping. */
23482 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
23483 after_nsdmi_defaulted_late_checks (class_type
);
23484 vec_safe_truncate (unparsed_classes
, 0);
23485 after_nsdmi_defaulted_late_checks (type
);
23487 /* Now parse the body of the functions. */
23490 /* OpenMP UDRs need to be parsed before all other functions. */
23491 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23492 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
23493 cp_parser_late_parsing_for_member (parser
, decl
);
23494 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23495 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
23496 cp_parser_late_parsing_for_member (parser
, decl
);
23499 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23500 cp_parser_late_parsing_for_member (parser
, decl
);
23501 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23504 vec_safe_push (unparsed_classes
, type
);
23506 /* Put back any saved access checks. */
23507 pop_deferring_access_checks ();
23509 /* Restore saved state. */
23510 parser
->in_switch_statement_p
= in_switch_statement_p
;
23511 parser
->in_statement
= in_statement
;
23512 parser
->in_function_body
= saved_in_function_body
;
23513 parser
->num_template_parameter_lists
23514 = saved_num_template_parameter_lists
;
23515 parser
->in_unbraced_linkage_specification_p
23516 = saved_in_unbraced_linkage_specification_p
;
23522 cp_parser_class_specifier (cp_parser
* parser
)
23525 timevar_push (TV_PARSE_STRUCT
);
23526 ret
= cp_parser_class_specifier_1 (parser
);
23527 timevar_pop (TV_PARSE_STRUCT
);
23531 /* Parse a class-head.
23534 class-key identifier [opt] base-clause [opt]
23535 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23536 class-key nested-name-specifier [opt] template-id
23539 class-virt-specifier:
23543 class-key attributes identifier [opt] base-clause [opt]
23544 class-key attributes nested-name-specifier identifier base-clause [opt]
23545 class-key attributes nested-name-specifier [opt] template-id
23548 Upon return BASES is initialized to the list of base classes (or
23549 NULL, if there are none) in the same form returned by
23550 cp_parser_base_clause.
23552 Returns the TYPE of the indicated class. Sets
23553 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23554 involving a nested-name-specifier was used, and FALSE otherwise.
23556 Returns error_mark_node if this is not a class-head.
23558 Returns NULL_TREE if the class-head is syntactically valid, but
23559 semantically invalid in a way that means we should skip the entire
23560 body of the class. */
23563 cp_parser_class_head (cp_parser
* parser
,
23564 bool* nested_name_specifier_p
)
23566 tree nested_name_specifier
;
23567 enum tag_types class_key
;
23568 tree id
= NULL_TREE
;
23569 tree type
= NULL_TREE
;
23572 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
23573 bool template_id_p
= false;
23574 bool qualified_p
= false;
23575 bool invalid_nested_name_p
= false;
23576 bool invalid_explicit_specialization_p
= false;
23577 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23578 tree pushed_scope
= NULL_TREE
;
23579 unsigned num_templates
;
23580 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
23581 /* Assume no nested-name-specifier will be present. */
23582 *nested_name_specifier_p
= false;
23583 /* Assume no template parameter lists will be used in defining the
23586 parser
->colon_corrects_to_scope_p
= false;
23588 /* Look for the class-key. */
23589 class_key
= cp_parser_class_key (parser
);
23590 if (class_key
== none_type
)
23591 return error_mark_node
;
23593 location_t class_head_start_location
= input_location
;
23595 /* Parse the attributes. */
23596 attributes
= cp_parser_attributes_opt (parser
);
23598 /* If the next token is `::', that is invalid -- but sometimes
23599 people do try to write:
23603 Handle this gracefully by accepting the extra qualifier, and then
23604 issuing an error about it later if this really is a
23605 class-head. If it turns out just to be an elaborated type
23606 specifier, remain silent. */
23607 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23608 qualified_p
= true;
23610 push_deferring_access_checks (dk_no_check
);
23612 /* Determine the name of the class. Begin by looking for an
23613 optional nested-name-specifier. */
23614 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23615 nested_name_specifier
23616 = cp_parser_nested_name_specifier_opt (parser
,
23617 /*typename_keyword_p=*/false,
23618 /*check_dependency_p=*/false,
23620 /*is_declaration=*/false);
23621 /* If there was a nested-name-specifier, then there *must* be an
23624 cp_token
*bad_template_keyword
= NULL
;
23626 if (nested_name_specifier
)
23628 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23629 /* Although the grammar says `identifier', it really means
23630 `class-name' or `template-name'. You are only allowed to
23631 define a class that has already been declared with this
23634 The proposed resolution for Core Issue 180 says that wherever
23635 you see `class T::X' you should treat `X' as a type-name.
23637 It is OK to define an inaccessible class; for example:
23639 class A { class B; };
23642 We do not know if we will see a class-name, or a
23643 template-name. We look for a class-name first, in case the
23644 class-name is a template-id; if we looked for the
23645 template-name first we would stop after the template-name. */
23646 cp_parser_parse_tentatively (parser
);
23647 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23648 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23649 type
= cp_parser_class_name (parser
,
23650 /*typename_keyword_p=*/false,
23651 /*template_keyword_p=*/false,
23653 /*check_dependency_p=*/false,
23654 /*class_head_p=*/true,
23655 /*is_declaration=*/false);
23656 /* If that didn't work, ignore the nested-name-specifier. */
23657 if (!cp_parser_parse_definitely (parser
))
23659 invalid_nested_name_p
= true;
23660 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23661 id
= cp_parser_identifier (parser
);
23662 if (id
== error_mark_node
)
23665 /* If we could not find a corresponding TYPE, treat this
23666 declaration like an unqualified declaration. */
23667 if (type
== error_mark_node
)
23668 nested_name_specifier
= NULL_TREE
;
23669 /* Otherwise, count the number of templates used in TYPE and its
23670 containing scopes. */
23672 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23674 /* Otherwise, the identifier is optional. */
23677 /* We don't know whether what comes next is a template-id,
23678 an identifier, or nothing at all. */
23679 cp_parser_parse_tentatively (parser
);
23680 /* Check for a template-id. */
23681 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23682 id
= cp_parser_template_id (parser
,
23683 /*template_keyword_p=*/false,
23684 /*check_dependency_p=*/true,
23686 /*is_declaration=*/true);
23687 /* If that didn't work, it could still be an identifier. */
23688 if (!cp_parser_parse_definitely (parser
))
23690 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23692 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23693 id
= cp_parser_identifier (parser
);
23700 template_id_p
= true;
23705 pop_deferring_access_checks ();
23709 cp_parser_check_for_invalid_template_id (parser
, id
,
23711 type_start_token
->location
);
23713 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23715 /* If it's not a `:' or a `{' then we can't really be looking at a
23716 class-head, since a class-head only appears as part of a
23717 class-specifier. We have to detect this situation before calling
23718 xref_tag, since that has irreversible side-effects. */
23719 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23721 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23722 type
= error_mark_node
;
23726 /* At this point, we're going ahead with the class-specifier, even
23727 if some other problem occurs. */
23728 cp_parser_commit_to_tentative_parse (parser
);
23729 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23731 cp_parser_error (parser
,
23732 "cannot specify %<override%> for a class");
23733 type
= error_mark_node
;
23736 /* Issue the error about the overly-qualified name now. */
23739 cp_parser_error (parser
,
23740 "global qualification of class name is invalid");
23741 type
= error_mark_node
;
23744 else if (invalid_nested_name_p
)
23746 cp_parser_error (parser
,
23747 "qualified name does not name a class");
23748 type
= error_mark_node
;
23751 else if (nested_name_specifier
)
23755 if (bad_template_keyword
)
23756 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23757 keyword template shall not appear at the top level. */
23758 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23759 "keyword %<template%> not allowed in class-head-name");
23761 /* Reject typedef-names in class heads. */
23762 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23764 error_at (type_start_token
->location
,
23765 "invalid class name in declaration of %qD",
23771 /* Figure out in what scope the declaration is being placed. */
23772 scope
= current_scope ();
23773 /* If that scope does not contain the scope in which the
23774 class was originally declared, the program is invalid. */
23775 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23777 if (at_namespace_scope_p ())
23778 error_at (type_start_token
->location
,
23779 "declaration of %qD in namespace %qD which does not "
23781 type
, scope
, nested_name_specifier
);
23783 error_at (type_start_token
->location
,
23784 "declaration of %qD in %qD which does not enclose %qD",
23785 type
, scope
, nested_name_specifier
);
23791 A declarator-id shall not be qualified except for the
23792 definition of a ... nested class outside of its class
23793 ... [or] the definition or explicit instantiation of a
23794 class member of a namespace outside of its namespace. */
23795 if (scope
== nested_name_specifier
)
23797 permerror (nested_name_specifier_token_start
->location
,
23798 "extra qualification not allowed");
23799 nested_name_specifier
= NULL_TREE
;
23803 /* An explicit-specialization must be preceded by "template <>". If
23804 it is not, try to recover gracefully. */
23805 if (at_namespace_scope_p ()
23806 && parser
->num_template_parameter_lists
== 0
23807 && !processing_template_parmlist
23810 /* Build a location of this form:
23811 struct typename <ARGS>
23812 ^~~~~~~~~~~~~~~~~~~~~~
23813 with caret==start at the start token, and
23814 finishing at the end of the type. */
23815 location_t reported_loc
23816 = make_location (class_head_start_location
,
23817 class_head_start_location
,
23818 get_finish (type_start_token
->location
));
23819 rich_location
richloc (line_table
, reported_loc
);
23820 richloc
.add_fixit_insert_before (class_head_start_location
,
23822 error_at (&richloc
,
23823 "an explicit specialization must be preceded by"
23824 " %<template <>%>");
23825 invalid_explicit_specialization_p
= true;
23826 /* Take the same action that would have been taken by
23827 cp_parser_explicit_specialization. */
23828 ++parser
->num_template_parameter_lists
;
23829 begin_specialization ();
23831 /* There must be no "return" statements between this point and the
23832 end of this function; set "type "to the correct return value and
23833 use "goto done;" to return. */
23834 /* Make sure that the right number of template parameters were
23836 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23838 type_start_token
->location
,
23839 /*declarator=*/NULL
))
23841 /* If something went wrong, there is no point in even trying to
23842 process the class-definition. */
23847 /* Look up the type. */
23850 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23851 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23852 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23854 error_at (type_start_token
->location
,
23855 "function template %qD redeclared as a class template", id
);
23856 type
= error_mark_node
;
23860 type
= TREE_TYPE (id
);
23861 type
= maybe_process_partial_specialization (type
);
23863 /* Check the scope while we still know whether or not we had a
23864 nested-name-specifier. */
23865 if (type
!= error_mark_node
)
23866 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23868 if (nested_name_specifier
)
23869 pushed_scope
= push_scope (nested_name_specifier
);
23871 else if (nested_name_specifier
)
23877 template <typename T> struct S { struct T };
23878 template <typename T> struct S<T>::T { };
23880 we will get a TYPENAME_TYPE when processing the definition of
23881 `S::T'. We need to resolve it to the actual type before we
23882 try to define it. */
23883 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23885 class_type
= resolve_typename_type (TREE_TYPE (type
),
23886 /*only_current_p=*/false);
23887 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23888 type
= TYPE_NAME (class_type
);
23891 cp_parser_error (parser
, "could not resolve typename type");
23892 type
= error_mark_node
;
23896 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23897 == error_mark_node
)
23903 class_type
= current_class_type
;
23904 /* Enter the scope indicated by the nested-name-specifier. */
23905 pushed_scope
= push_scope (nested_name_specifier
);
23906 /* Get the canonical version of this type. */
23907 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23908 /* Call push_template_decl if it seems like we should be defining a
23909 template either from the template headers or the type we're
23910 defining, so that we diagnose both extra and missing headers. */
23911 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23912 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23913 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23915 type
= push_template_decl (type
);
23916 if (type
== error_mark_node
)
23923 type
= TREE_TYPE (type
);
23924 *nested_name_specifier_p
= true;
23926 else /* The name is not a nested name. */
23928 /* If the class was unnamed, create a dummy name. */
23930 id
= make_anon_name ();
23931 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23932 ? ts_within_enclosing_non_class
23934 type
= xref_tag (class_key
, id
, tag_scope
,
23935 parser
->num_template_parameter_lists
);
23938 /* Indicate whether this class was declared as a `class' or as a
23940 if (TREE_CODE (type
) == RECORD_TYPE
)
23941 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23942 cp_parser_check_class_key (class_key
, type
);
23944 /* If this type was already complete, and we see another definition,
23945 that's an error. */
23946 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23948 error_at (type_start_token
->location
, "redefinition of %q#T",
23950 inform (location_of (type
), "previous definition of %q#T",
23955 else if (type
== error_mark_node
)
23960 /* Apply attributes now, before any use of the class as a template
23961 argument in its base list. */
23962 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23963 fixup_attribute_variants (type
);
23966 /* We will have entered the scope containing the class; the names of
23967 base classes should be looked up in that context. For example:
23969 struct A { struct B {}; struct C; };
23970 struct A::C : B {};
23974 /* Get the list of base-classes, if there is one. */
23975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23977 /* PR59482: enter the class scope so that base-specifiers are looked
23981 bases
= cp_parser_base_clause (parser
);
23982 /* PR59482: get out of the previously pushed class scope so that the
23983 subsequent pops pop the right thing. */
23990 /* If we're really defining a class, process the base classes.
23991 If they're invalid, fail. */
23992 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23993 xref_basetypes (type
, bases
);
23996 /* Leave the scope given by the nested-name-specifier. We will
23997 enter the class scope itself while processing the members. */
23999 pop_scope (pushed_scope
);
24001 if (invalid_explicit_specialization_p
)
24003 end_specialization ();
24004 --parser
->num_template_parameter_lists
;
24008 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
24009 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
24010 CLASSTYPE_FINAL (type
) = 1;
24012 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24016 /* Parse a class-key.
24023 Returns the kind of class-key specified, or none_type to indicate
24026 static enum tag_types
24027 cp_parser_class_key (cp_parser
* parser
)
24030 enum tag_types tag_type
;
24032 /* Look for the class-key. */
24033 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
24037 /* Check to see if the TOKEN is a class-key. */
24038 tag_type
= cp_parser_token_is_class_key (token
);
24040 cp_parser_error (parser
, "expected class-key");
24044 /* Parse a type-parameter-key.
24046 type-parameter-key:
24052 cp_parser_type_parameter_key (cp_parser
* parser
)
24054 /* Look for the type-parameter-key. */
24055 enum tag_types tag_type
= none_type
;
24056 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24057 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
24059 cp_lexer_consume_token (parser
->lexer
);
24060 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
24061 /* typename is not allowed in a template template parameter
24062 by the standard until C++17. */
24063 pedwarn (token
->location
, OPT_Wpedantic
,
24064 "ISO C++ forbids typename key in template template parameter;"
24065 " use -std=c++17 or -std=gnu++17");
24068 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
24073 /* Parse an (optional) member-specification.
24075 member-specification:
24076 member-declaration member-specification [opt]
24077 access-specifier : member-specification [opt] */
24080 cp_parser_member_specification_opt (cp_parser
* parser
)
24087 /* Peek at the next token. */
24088 token
= cp_lexer_peek_token (parser
->lexer
);
24089 /* If it's a `}', or EOF then we've seen all the members. */
24090 if (token
->type
== CPP_CLOSE_BRACE
24091 || token
->type
== CPP_EOF
24092 || token
->type
== CPP_PRAGMA_EOL
)
24095 /* See if this token is a keyword. */
24096 keyword
= token
->keyword
;
24100 case RID_PROTECTED
:
24102 /* Consume the access-specifier. */
24103 cp_lexer_consume_token (parser
->lexer
);
24104 /* Remember which access-specifier is active. */
24105 current_access_specifier
= token
->u
.value
;
24106 /* Look for the `:'. */
24107 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24111 /* Accept #pragmas at class scope. */
24112 if (token
->type
== CPP_PRAGMA
)
24114 cp_parser_pragma (parser
, pragma_member
, NULL
);
24118 /* Otherwise, the next construction must be a
24119 member-declaration. */
24120 cp_parser_member_declaration (parser
);
24125 /* Parse a member-declaration.
24127 member-declaration:
24128 decl-specifier-seq [opt] member-declarator-list [opt] ;
24129 function-definition ; [opt]
24130 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24132 template-declaration
24135 member-declarator-list:
24137 member-declarator-list , member-declarator
24140 declarator pure-specifier [opt]
24141 declarator constant-initializer [opt]
24142 identifier [opt] : constant-expression
24146 member-declaration:
24147 __extension__ member-declaration
24150 declarator attributes [opt] pure-specifier [opt]
24151 declarator attributes [opt] constant-initializer [opt]
24152 identifier [opt] attributes [opt] : constant-expression
24156 member-declaration:
24157 static_assert-declaration */
24160 cp_parser_member_declaration (cp_parser
* parser
)
24162 cp_decl_specifier_seq decl_specifiers
;
24163 tree prefix_attributes
;
24165 int declares_class_or_enum
;
24167 cp_token
*token
= NULL
;
24168 cp_token
*decl_spec_token_start
= NULL
;
24169 cp_token
*initializer_token_start
= NULL
;
24170 int saved_pedantic
;
24171 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
24173 /* Check for the `__extension__' keyword. */
24174 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
24177 cp_parser_member_declaration (parser
);
24178 /* Restore the old value of the PEDANTIC flag. */
24179 pedantic
= saved_pedantic
;
24184 /* Check for a template-declaration. */
24185 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24187 /* An explicit specialization here is an error condition, and we
24188 expect the specialization handler to detect and report this. */
24189 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
24190 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
24191 cp_parser_explicit_specialization (parser
);
24193 cp_parser_template_declaration (parser
, /*member_p=*/true);
24197 /* Check for a template introduction. */
24198 else if (cp_parser_template_declaration_after_export (parser
, true))
24201 /* Check for a using-declaration. */
24202 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24204 if (cxx_dialect
< cxx11
)
24206 /* Parse the using-declaration. */
24207 cp_parser_using_declaration (parser
,
24208 /*access_declaration_p=*/false);
24214 bool alias_decl_expected
;
24215 cp_parser_parse_tentatively (parser
);
24216 decl
= cp_parser_alias_declaration (parser
);
24217 /* Note that if we actually see the '=' token after the
24218 identifier, cp_parser_alias_declaration commits the
24219 tentative parse. In that case, we really expect an
24220 alias-declaration. Otherwise, we expect a using
24222 alias_decl_expected
=
24223 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
24224 cp_parser_parse_definitely (parser
);
24226 if (alias_decl_expected
)
24227 finish_member_declaration (decl
);
24229 cp_parser_using_declaration (parser
,
24230 /*access_declaration_p=*/false);
24235 /* Check for @defs. */
24236 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
24239 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
24240 ivar
= ivar_chains
;
24244 ivar
= TREE_CHAIN (member
);
24245 TREE_CHAIN (member
) = NULL_TREE
;
24246 finish_member_declaration (member
);
24251 /* If the next token is `static_assert' we have a static assertion. */
24252 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
24254 cp_parser_static_assert (parser
, /*member_p=*/true);
24258 parser
->colon_corrects_to_scope_p
= false;
24260 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
24263 /* Parse the decl-specifier-seq. */
24264 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
24265 cp_parser_decl_specifier_seq (parser
,
24266 (CP_PARSER_FLAGS_OPTIONAL
24267 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
24269 &declares_class_or_enum
);
24270 /* Check for an invalid type-name. */
24271 if (!decl_specifiers
.any_type_specifiers_p
24272 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24274 /* If there is no declarator, then the decl-specifier-seq should
24276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24278 /* If there was no decl-specifier-seq, and the next token is a
24279 `;', then we have something like:
24285 Each member-declaration shall declare at least one member
24286 name of the class. */
24287 if (!decl_specifiers
.any_specifiers_p
)
24289 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24290 if (!in_system_header_at (token
->location
))
24292 gcc_rich_location
richloc (token
->location
);
24293 richloc
.add_fixit_remove ();
24294 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
24301 /* See if this declaration is a friend. */
24302 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24303 /* If there were decl-specifiers, check to see if there was
24304 a class-declaration. */
24305 type
= check_tag_decl (&decl_specifiers
,
24306 /*explicit_type_instantiation_p=*/false);
24307 /* Nested classes have already been added to the class, but
24308 a `friend' needs to be explicitly registered. */
24311 /* If the `friend' keyword was present, the friend must
24312 be introduced with a class-key. */
24313 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
24314 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
24315 "in C++03 a class-key must be used "
24316 "when declaring a friend");
24319 template <typename T> struct A {
24320 friend struct A<T>::B;
24323 A<T>::B will be represented by a TYPENAME_TYPE, and
24324 therefore not recognized by check_tag_decl. */
24327 type
= decl_specifiers
.type
;
24328 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
24329 type
= TREE_TYPE (type
);
24331 if (!type
|| !TYPE_P (type
))
24332 error_at (decl_spec_token_start
->location
,
24333 "friend declaration does not name a class or "
24336 make_friend_class (current_class_type
, type
,
24337 /*complain=*/true);
24339 /* If there is no TYPE, an error message will already have
24341 else if (!type
|| type
== error_mark_node
)
24343 /* An anonymous aggregate has to be handled specially; such
24344 a declaration really declares a data member (with a
24345 particular type), as opposed to a nested class. */
24346 else if (ANON_AGGR_TYPE_P (type
))
24349 if (decl_specifiers
.storage_class
!= sc_none
)
24350 error_at (decl_spec_token_start
->location
,
24351 "a storage class on an anonymous aggregate "
24352 "in class scope is not allowed");
24354 /* Remove constructors and such from TYPE, now that we
24355 know it is an anonymous aggregate. */
24356 fixup_anonymous_aggr (type
);
24357 /* And make the corresponding data member. */
24358 decl
= build_decl (decl_spec_token_start
->location
,
24359 FIELD_DECL
, NULL_TREE
, type
);
24360 /* Add it to the class. */
24361 finish_member_declaration (decl
);
24364 cp_parser_check_access_in_redeclaration
24366 decl_spec_token_start
->location
);
24371 bool assume_semicolon
= false;
24373 /* Clear attributes from the decl_specifiers but keep them
24374 around as prefix attributes that apply them to the entity
24376 prefix_attributes
= decl_specifiers
.attributes
;
24377 decl_specifiers
.attributes
= NULL_TREE
;
24379 /* See if these declarations will be friends. */
24380 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24382 /* Keep going until we hit the `;' at the end of the
24384 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24386 tree attributes
= NULL_TREE
;
24387 tree first_attribute
;
24389 bool named_bitfld
= false;
24391 /* Peek at the next token. */
24392 token
= cp_lexer_peek_token (parser
->lexer
);
24394 /* The following code wants to know early if it is a bit-field
24395 or some other declaration. Attributes can appear before
24396 the `:' token. Skip over them without consuming any tokens
24397 to peek if they are followed by `:'. */
24398 if (cp_next_tokens_can_be_attribute_p (parser
)
24399 || (token
->type
== CPP_NAME
24400 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
24401 && (named_bitfld
= true)))
24404 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
24405 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24408 /* Check for a bitfield declaration. */
24409 if (token
->type
== CPP_COLON
24410 || (token
->type
== CPP_NAME
24411 && token
== cp_lexer_peek_token (parser
->lexer
)
24412 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
24413 && (named_bitfld
= true)))
24417 tree late_attributes
= NULL_TREE
;
24418 location_t id_location
24419 = cp_lexer_peek_token (parser
->lexer
)->location
;
24422 identifier
= cp_parser_identifier (parser
);
24424 identifier
= NULL_TREE
;
24426 /* Look for attributes that apply to the bitfield. */
24427 attributes
= cp_parser_attributes_opt (parser
);
24429 /* Consume the `:' token. */
24430 cp_lexer_consume_token (parser
->lexer
);
24432 /* Get the width of the bitfield. */
24433 width
= cp_parser_constant_expression (parser
, false, NULL
,
24434 cxx_dialect
>= cxx11
);
24436 /* In C++2A and as extension for C++11 and above we allow
24437 default member initializers for bit-fields. */
24438 initializer
= NULL_TREE
;
24439 if (cxx_dialect
>= cxx11
24440 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
24441 || cp_lexer_next_token_is (parser
->lexer
,
24445 = cp_lexer_peek_token (parser
->lexer
)->location
;
24446 if (cxx_dialect
< cxx2a
24447 && !in_system_header_at (loc
)
24448 && identifier
!= NULL_TREE
)
24450 "default member initializers for bit-fields "
24451 "only available with -std=c++2a or "
24454 initializer
= cp_parser_save_nsdmi (parser
);
24455 if (identifier
== NULL_TREE
)
24457 error_at (loc
, "default member initializer for "
24458 "unnamed bit-field");
24459 initializer
= NULL_TREE
;
24464 /* Look for attributes that apply to the bitfield after
24465 the `:' token and width. This is where GCC used to
24466 parse attributes in the past, pedwarn if there is
24467 a std attribute. */
24468 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24469 pedwarn (input_location
, OPT_Wpedantic
,
24470 "ISO C++ allows bit-field attributes only "
24471 "before the %<:%> token");
24473 late_attributes
= cp_parser_attributes_opt (parser
);
24476 attributes
= attr_chainon (attributes
, late_attributes
);
24478 /* Remember which attributes are prefix attributes and
24480 first_attribute
= attributes
;
24481 /* Combine the attributes. */
24482 attributes
= attr_chainon (prefix_attributes
, attributes
);
24484 /* Create the bitfield declaration. */
24485 decl
= grokbitfield (identifier
24486 ? make_id_declarator (NULL_TREE
,
24492 width
, initializer
,
24497 cp_declarator
*declarator
;
24498 tree asm_specification
;
24499 int ctor_dtor_or_conv_p
;
24500 bool static_p
= (decl_specifiers
.storage_class
== sc_static
);
24502 /* Parse the declarator. */
24504 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24505 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
24506 &ctor_dtor_or_conv_p
,
24507 /*parenthesized_p=*/NULL
,
24509 friend_p
, static_p
);
24511 /* If something went wrong parsing the declarator, make sure
24512 that we at least consume some tokens. */
24513 if (declarator
== cp_error_declarator
)
24515 /* Skip to the end of the statement. */
24516 cp_parser_skip_to_end_of_statement (parser
);
24517 /* If the next token is not a semicolon, that is
24518 probably because we just skipped over the body of
24519 a function. So, we consume a semicolon if
24520 present, but do not issue an error message if it
24522 if (cp_lexer_next_token_is (parser
->lexer
,
24524 cp_lexer_consume_token (parser
->lexer
);
24528 if (declares_class_or_enum
& 2)
24529 cp_parser_check_for_definition_in_return_type
24530 (declarator
, decl_specifiers
.type
,
24531 decl_specifiers
.locations
[ds_type_spec
]);
24533 /* Look for an asm-specification. */
24534 asm_specification
= cp_parser_asm_specification_opt (parser
);
24535 /* Look for attributes that apply to the declaration. */
24536 attributes
= cp_parser_attributes_opt (parser
);
24537 /* Remember which attributes are prefix attributes and
24539 first_attribute
= attributes
;
24540 /* Combine the attributes. */
24541 attributes
= attr_chainon (prefix_attributes
, attributes
);
24543 /* If it's an `=', then we have a constant-initializer or a
24544 pure-specifier. It is not correct to parse the
24545 initializer before registering the member declaration
24546 since the member declaration should be in scope while
24547 its initializer is processed. However, the rest of the
24548 front end does not yet provide an interface that allows
24549 us to handle this correctly. */
24550 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24554 A pure-specifier shall be used only in the declaration of
24555 a virtual function.
24557 A member-declarator can contain a constant-initializer
24558 only if it declares a static member of integral or
24561 Therefore, if the DECLARATOR is for a function, we look
24562 for a pure-specifier; otherwise, we look for a
24563 constant-initializer. When we call `grokfield', it will
24564 perform more stringent semantics checks. */
24565 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
24566 if (function_declarator_p (declarator
)
24567 || (decl_specifiers
.type
24568 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
24569 && declarator
->kind
== cdk_id
24570 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
24571 == FUNCTION_TYPE
)))
24572 initializer
= cp_parser_pure_specifier (parser
);
24573 else if (decl_specifiers
.storage_class
!= sc_static
)
24574 initializer
= cp_parser_save_nsdmi (parser
);
24575 else if (cxx_dialect
>= cxx11
)
24578 /* Don't require a constant rvalue in C++11, since we
24579 might want a reference constant. We'll enforce
24580 constancy later. */
24581 cp_lexer_consume_token (parser
->lexer
);
24582 /* Parse the initializer. */
24583 initializer
= cp_parser_initializer_clause (parser
,
24587 /* Parse the initializer. */
24588 initializer
= cp_parser_constant_initializer (parser
);
24590 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
24591 && !function_declarator_p (declarator
))
24594 if (decl_specifiers
.storage_class
!= sc_static
)
24595 initializer
= cp_parser_save_nsdmi (parser
);
24597 initializer
= cp_parser_initializer (parser
, &x
, &x
);
24599 /* Otherwise, there is no initializer. */
24601 initializer
= NULL_TREE
;
24603 /* See if we are probably looking at a function
24604 definition. We are certainly not looking at a
24605 member-declarator. Calling `grokfield' has
24606 side-effects, so we must not do it unless we are sure
24607 that we are looking at a member-declarator. */
24608 if (cp_parser_token_starts_function_definition_p
24609 (cp_lexer_peek_token (parser
->lexer
)))
24611 /* The grammar does not allow a pure-specifier to be
24612 used when a member function is defined. (It is
24613 possible that this fact is an oversight in the
24614 standard, since a pure function may be defined
24615 outside of the class-specifier. */
24616 if (initializer
&& initializer_token_start
)
24617 error_at (initializer_token_start
->location
,
24618 "pure-specifier on function-definition");
24619 decl
= cp_parser_save_member_function_body (parser
,
24623 if (parser
->fully_implicit_function_template_p
)
24624 decl
= finish_fully_implicit_template (parser
, decl
);
24625 /* If the member was not a friend, declare it here. */
24627 finish_member_declaration (decl
);
24628 /* Peek at the next token. */
24629 token
= cp_lexer_peek_token (parser
->lexer
);
24630 /* If the next token is a semicolon, consume it. */
24631 if (token
->type
== CPP_SEMICOLON
)
24633 location_t semicolon_loc
24634 = cp_lexer_consume_token (parser
->lexer
)->location
;
24635 gcc_rich_location
richloc (semicolon_loc
);
24636 richloc
.add_fixit_remove ();
24637 warning_at (&richloc
, OPT_Wextra_semi
,
24638 "extra %<;%> after in-class "
24639 "function definition");
24644 if (declarator
->kind
== cdk_function
)
24645 declarator
->id_loc
= token
->location
;
24646 /* Create the declaration. */
24647 decl
= grokfield (declarator
, &decl_specifiers
,
24648 initializer
, /*init_const_expr_p=*/true,
24649 asm_specification
, attributes
);
24650 if (parser
->fully_implicit_function_template_p
)
24653 finish_fully_implicit_template (parser
, 0);
24655 decl
= finish_fully_implicit_template (parser
, decl
);
24659 cp_finalize_omp_declare_simd (parser
, decl
);
24660 cp_finalize_oacc_routine (parser
, decl
, false);
24662 /* Reset PREFIX_ATTRIBUTES. */
24663 if (attributes
!= error_mark_node
)
24665 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24666 attributes
= TREE_CHAIN (attributes
);
24668 TREE_CHAIN (attributes
) = NULL_TREE
;
24671 /* If there is any qualification still in effect, clear it
24672 now; we will be starting fresh with the next declarator. */
24673 parser
->scope
= NULL_TREE
;
24674 parser
->qualifying_scope
= NULL_TREE
;
24675 parser
->object_scope
= NULL_TREE
;
24676 /* If it's a `,', then there are more declarators. */
24677 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24679 cp_lexer_consume_token (parser
->lexer
);
24680 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24682 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24683 gcc_rich_location
richloc (token
->location
);
24684 richloc
.add_fixit_remove ();
24685 error_at (&richloc
, "stray %<,%> at end of "
24686 "member declaration");
24689 /* If the next token isn't a `;', then we have a parse error. */
24690 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24693 /* The next token might be a ways away from where the
24694 actual semicolon is missing. Find the previous token
24695 and use that for our error position. */
24696 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24697 gcc_rich_location
richloc (token
->location
);
24698 richloc
.add_fixit_insert_after (";");
24699 error_at (&richloc
, "expected %<;%> at end of "
24700 "member declaration");
24702 /* Assume that the user meant to provide a semicolon. If
24703 we were to cp_parser_skip_to_end_of_statement, we might
24704 skip to a semicolon inside a member function definition
24705 and issue nonsensical error messages. */
24706 assume_semicolon
= true;
24711 /* Add DECL to the list of members. */
24713 /* Explicitly include, eg, NSDMIs, for better error
24714 recovery (c++/58650). */
24715 || !DECL_DECLARES_FUNCTION_P (decl
))
24716 finish_member_declaration (decl
);
24718 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24719 cp_parser_save_default_args (parser
, decl
);
24720 else if (TREE_CODE (decl
) == FIELD_DECL
24721 && DECL_INITIAL (decl
))
24722 /* Add DECL to the queue of NSDMI to be parsed later. */
24723 vec_safe_push (unparsed_nsdmis
, decl
);
24726 if (assume_semicolon
)
24731 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24733 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24736 /* Parse a pure-specifier.
24741 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24742 Otherwise, ERROR_MARK_NODE is returned. */
24745 cp_parser_pure_specifier (cp_parser
* parser
)
24749 /* Look for the `=' token. */
24750 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24751 return error_mark_node
;
24752 /* Look for the `0' token. */
24753 token
= cp_lexer_peek_token (parser
->lexer
);
24755 if (token
->type
== CPP_EOF
24756 || token
->type
== CPP_PRAGMA_EOL
)
24757 return error_mark_node
;
24759 cp_lexer_consume_token (parser
->lexer
);
24761 /* Accept = default or = delete in c++0x mode. */
24762 if (token
->keyword
== RID_DEFAULT
24763 || token
->keyword
== RID_DELETE
)
24765 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24766 return token
->u
.value
;
24769 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24770 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24772 cp_parser_error (parser
,
24773 "invalid pure specifier (only %<= 0%> is allowed)");
24774 cp_parser_skip_to_end_of_statement (parser
);
24775 return error_mark_node
;
24777 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24779 error_at (token
->location
, "templates may not be %<virtual%>");
24780 return error_mark_node
;
24783 return integer_zero_node
;
24786 /* Parse a constant-initializer.
24788 constant-initializer:
24789 = constant-expression
24791 Returns a representation of the constant-expression. */
24794 cp_parser_constant_initializer (cp_parser
* parser
)
24796 /* Look for the `=' token. */
24797 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24798 return error_mark_node
;
24800 /* It is invalid to write:
24802 struct S { static const int i = { 7 }; };
24805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24807 cp_parser_error (parser
,
24808 "a brace-enclosed initializer is not allowed here");
24809 /* Consume the opening brace. */
24810 matching_braces braces
;
24811 braces
.consume_open (parser
);
24812 /* Skip the initializer. */
24813 cp_parser_skip_to_closing_brace (parser
);
24814 /* Look for the trailing `}'. */
24815 braces
.require_close (parser
);
24817 return error_mark_node
;
24820 return cp_parser_constant_expression (parser
);
24823 /* Derived classes [gram.class.derived] */
24825 /* Parse a base-clause.
24828 : base-specifier-list
24830 base-specifier-list:
24831 base-specifier ... [opt]
24832 base-specifier-list , base-specifier ... [opt]
24834 Returns a TREE_LIST representing the base-classes, in the order in
24835 which they were declared. The representation of each node is as
24836 described by cp_parser_base_specifier.
24838 In the case that no bases are specified, this function will return
24839 NULL_TREE, not ERROR_MARK_NODE. */
24842 cp_parser_base_clause (cp_parser
* parser
)
24844 tree bases
= NULL_TREE
;
24846 /* Look for the `:' that begins the list. */
24847 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24849 /* Scan the base-specifier-list. */
24854 bool pack_expansion_p
= false;
24856 /* Look for the base-specifier. */
24857 base
= cp_parser_base_specifier (parser
);
24858 /* Look for the (optional) ellipsis. */
24859 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24861 /* Consume the `...'. */
24862 cp_lexer_consume_token (parser
->lexer
);
24864 pack_expansion_p
= true;
24867 /* Add BASE to the front of the list. */
24868 if (base
&& base
!= error_mark_node
)
24870 if (pack_expansion_p
)
24871 /* Make this a pack expansion type. */
24872 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24874 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24876 TREE_CHAIN (base
) = bases
;
24880 /* Peek at the next token. */
24881 token
= cp_lexer_peek_token (parser
->lexer
);
24882 /* If it's not a comma, then the list is complete. */
24883 if (token
->type
!= CPP_COMMA
)
24885 /* Consume the `,'. */
24886 cp_lexer_consume_token (parser
->lexer
);
24889 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24890 base class had a qualified name. However, the next name that
24891 appears is certainly not qualified. */
24892 parser
->scope
= NULL_TREE
;
24893 parser
->qualifying_scope
= NULL_TREE
;
24894 parser
->object_scope
= NULL_TREE
;
24896 return nreverse (bases
);
24899 /* Parse a base-specifier.
24902 :: [opt] nested-name-specifier [opt] class-name
24903 virtual access-specifier [opt] :: [opt] nested-name-specifier
24905 access-specifier virtual [opt] :: [opt] nested-name-specifier
24908 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24909 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24910 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24911 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24914 cp_parser_base_specifier (cp_parser
* parser
)
24918 bool virtual_p
= false;
24919 bool duplicate_virtual_error_issued_p
= false;
24920 bool duplicate_access_error_issued_p
= false;
24921 bool class_scope_p
, template_p
;
24922 tree access
= access_default_node
;
24925 /* Process the optional `virtual' and `access-specifier'. */
24928 /* Peek at the next token. */
24929 token
= cp_lexer_peek_token (parser
->lexer
);
24930 /* Process `virtual'. */
24931 switch (token
->keyword
)
24934 /* If `virtual' appears more than once, issue an error. */
24935 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24937 cp_parser_error (parser
,
24938 "%<virtual%> specified more than once in base-specifier");
24939 duplicate_virtual_error_issued_p
= true;
24944 /* Consume the `virtual' token. */
24945 cp_lexer_consume_token (parser
->lexer
);
24950 case RID_PROTECTED
:
24952 /* If more than one access specifier appears, issue an
24954 if (access
!= access_default_node
24955 && !duplicate_access_error_issued_p
)
24957 cp_parser_error (parser
,
24958 "more than one access specifier in base-specifier");
24959 duplicate_access_error_issued_p
= true;
24962 access
= ridpointers
[(int) token
->keyword
];
24964 /* Consume the access-specifier. */
24965 cp_lexer_consume_token (parser
->lexer
);
24974 /* It is not uncommon to see programs mechanically, erroneously, use
24975 the 'typename' keyword to denote (dependent) qualified types
24976 as base classes. */
24977 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24979 token
= cp_lexer_peek_token (parser
->lexer
);
24980 if (!processing_template_decl
)
24981 error_at (token
->location
,
24982 "keyword %<typename%> not allowed outside of templates");
24984 error_at (token
->location
,
24985 "keyword %<typename%> not allowed in this context "
24986 "(the base class is implicitly a type)");
24987 cp_lexer_consume_token (parser
->lexer
);
24990 /* Look for the optional `::' operator. */
24991 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24992 /* Look for the nested-name-specifier. The simplest way to
24997 The keyword `typename' is not permitted in a base-specifier or
24998 mem-initializer; in these contexts a qualified name that
24999 depends on a template-parameter is implicitly assumed to be a
25002 is to pretend that we have seen the `typename' keyword at this
25004 cp_parser_nested_name_specifier_opt (parser
,
25005 /*typename_keyword_p=*/true,
25006 /*check_dependency_p=*/true,
25008 /*is_declaration=*/true);
25009 /* If the base class is given by a qualified name, assume that names
25010 we see are type names or templates, as appropriate. */
25011 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
25012 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
25015 && cp_lexer_next_token_is_decltype (parser
->lexer
))
25016 /* DR 950 allows decltype as a base-specifier. */
25017 type
= cp_parser_decltype (parser
);
25020 /* Otherwise, look for the class-name. */
25021 type
= cp_parser_class_name (parser
,
25025 /*check_dependency_p=*/true,
25026 /*class_head_p=*/false,
25027 /*is_declaration=*/true);
25028 type
= TREE_TYPE (type
);
25031 if (type
== error_mark_node
)
25032 return error_mark_node
;
25034 return finish_base_specifier (type
, access
, virtual_p
);
25037 /* Exception handling [gram.exception] */
25039 /* Parse an (optional) noexcept-specification.
25041 noexcept-specification:
25042 noexcept ( constant-expression ) [opt]
25044 If no noexcept-specification is present, returns NULL_TREE.
25045 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25046 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25047 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25048 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25049 in which case a boolean condition is returned instead. */
25052 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
25053 bool require_constexpr
,
25054 bool* consumed_expr
,
25058 const char *saved_message
;
25060 /* Peek at the next token. */
25061 token
= cp_lexer_peek_token (parser
->lexer
);
25063 /* Is it a noexcept-specification? */
25064 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
25067 cp_lexer_consume_token (parser
->lexer
);
25069 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
25071 matching_parens parens
;
25072 parens
.consume_open (parser
);
25074 tree save_ccp
= current_class_ptr
;
25075 tree save_ccr
= current_class_ref
;
25077 if (current_class_type
)
25078 inject_this_parameter (current_class_type
, TYPE_UNQUALIFIED
);
25080 if (require_constexpr
)
25082 /* Types may not be defined in an exception-specification. */
25083 saved_message
= parser
->type_definition_forbidden_message
;
25084 parser
->type_definition_forbidden_message
25085 = G_("types may not be defined in an exception-specification");
25087 expr
= cp_parser_constant_expression (parser
);
25089 /* Restore the saved message. */
25090 parser
->type_definition_forbidden_message
= saved_message
;
25094 expr
= cp_parser_expression (parser
);
25095 *consumed_expr
= true;
25098 parens
.require_close (parser
);
25100 current_class_ptr
= save_ccp
;
25101 current_class_ref
= save_ccr
;
25105 expr
= boolean_true_node
;
25106 if (!require_constexpr
)
25107 *consumed_expr
= false;
25110 /* We cannot build a noexcept-spec right away because this will check
25111 that expr is a constexpr. */
25113 return build_noexcept_spec (expr
, tf_warning_or_error
);
25121 /* Parse an (optional) exception-specification.
25123 exception-specification:
25124 throw ( type-id-list [opt] )
25126 Returns a TREE_LIST representing the exception-specification. The
25127 TREE_VALUE of each node is a type. */
25130 cp_parser_exception_specification_opt (cp_parser
* parser
)
25134 const char *saved_message
;
25136 /* Peek at the next token. */
25137 token
= cp_lexer_peek_token (parser
->lexer
);
25139 /* Is it a noexcept-specification? */
25140 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
25142 if (type_id_list
!= NULL_TREE
)
25143 return type_id_list
;
25145 /* If it's not `throw', then there's no exception-specification. */
25146 if (!cp_parser_is_keyword (token
, RID_THROW
))
25149 location_t loc
= token
->location
;
25151 /* Consume the `throw'. */
25152 cp_lexer_consume_token (parser
->lexer
);
25154 /* Look for the `('. */
25155 matching_parens parens
;
25156 parens
.require_open (parser
);
25158 /* Peek at the next token. */
25159 token
= cp_lexer_peek_token (parser
->lexer
);
25160 /* If it's not a `)', then there is a type-id-list. */
25161 if (token
->type
!= CPP_CLOSE_PAREN
)
25163 /* Types may not be defined in an exception-specification. */
25164 saved_message
= parser
->type_definition_forbidden_message
;
25165 parser
->type_definition_forbidden_message
25166 = G_("types may not be defined in an exception-specification");
25167 /* Parse the type-id-list. */
25168 type_id_list
= cp_parser_type_id_list (parser
);
25169 /* Restore the saved message. */
25170 parser
->type_definition_forbidden_message
= saved_message
;
25172 if (cxx_dialect
>= cxx17
)
25174 error_at (loc
, "ISO C++17 does not allow dynamic exception "
25176 type_id_list
= NULL_TREE
;
25178 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
25179 warning_at (loc
, OPT_Wdeprecated
,
25180 "dynamic exception specifications are deprecated in "
25183 /* In C++17, throw() is equivalent to noexcept (true). throw()
25184 is deprecated in C++11 and above as well, but is still widely used,
25185 so don't warn about it yet. */
25186 else if (cxx_dialect
>= cxx17
)
25187 type_id_list
= noexcept_true_spec
;
25189 type_id_list
= empty_except_spec
;
25191 /* Look for the `)'. */
25192 parens
.require_close (parser
);
25194 return type_id_list
;
25197 /* Parse an (optional) type-id-list.
25201 type-id-list , type-id ... [opt]
25203 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25204 in the order that the types were presented. */
25207 cp_parser_type_id_list (cp_parser
* parser
)
25209 tree types
= NULL_TREE
;
25216 token
= cp_lexer_peek_token (parser
->lexer
);
25218 /* Get the next type-id. */
25219 type
= cp_parser_type_id (parser
);
25220 /* Check for invalid 'auto'. */
25221 if (flag_concepts
&& type_uses_auto (type
))
25223 error_at (token
->location
,
25224 "invalid use of %<auto%> in exception-specification");
25225 type
= error_mark_node
;
25227 /* Parse the optional ellipsis. */
25228 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25230 /* Consume the `...'. */
25231 cp_lexer_consume_token (parser
->lexer
);
25233 /* Turn the type into a pack expansion expression. */
25234 type
= make_pack_expansion (type
);
25236 /* Add it to the list. */
25237 types
= add_exception_specifier (types
, type
, /*complain=*/1);
25238 /* Peek at the next token. */
25239 token
= cp_lexer_peek_token (parser
->lexer
);
25240 /* If it is not a `,', we are done. */
25241 if (token
->type
!= CPP_COMMA
)
25243 /* Consume the `,'. */
25244 cp_lexer_consume_token (parser
->lexer
);
25247 return nreverse (types
);
25250 /* Parse a try-block.
25253 try compound-statement handler-seq */
25256 cp_parser_try_block (cp_parser
* parser
)
25260 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
25261 if (parser
->in_function_body
25262 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
25263 error ("%<try%> in %<constexpr%> function");
25265 try_block
= begin_try_block ();
25266 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
25267 finish_try_block (try_block
);
25268 cp_parser_handler_seq (parser
);
25269 finish_handler_sequence (try_block
);
25274 /* Parse a function-try-block.
25276 function-try-block:
25277 try ctor-initializer [opt] function-body handler-seq */
25280 cp_parser_function_try_block (cp_parser
* parser
)
25282 tree compound_stmt
;
25285 /* Look for the `try' keyword. */
25286 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
25288 /* Let the rest of the front end know where we are. */
25289 try_block
= begin_function_try_block (&compound_stmt
);
25290 /* Parse the function-body. */
25291 cp_parser_ctor_initializer_opt_and_function_body
25292 (parser
, /*in_function_try_block=*/true);
25293 /* We're done with the `try' part. */
25294 finish_function_try_block (try_block
);
25295 /* Parse the handlers. */
25296 cp_parser_handler_seq (parser
);
25297 /* We're done with the handlers. */
25298 finish_function_handler_sequence (try_block
, compound_stmt
);
25301 /* Parse a handler-seq.
25304 handler handler-seq [opt] */
25307 cp_parser_handler_seq (cp_parser
* parser
)
25313 /* Parse the handler. */
25314 cp_parser_handler (parser
);
25315 /* Peek at the next token. */
25316 token
= cp_lexer_peek_token (parser
->lexer
);
25317 /* If it's not `catch' then there are no more handlers. */
25318 if (!cp_parser_is_keyword (token
, RID_CATCH
))
25323 /* Parse a handler.
25326 catch ( exception-declaration ) compound-statement */
25329 cp_parser_handler (cp_parser
* parser
)
25334 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
25335 handler
= begin_handler ();
25336 matching_parens parens
;
25337 parens
.require_open (parser
);
25338 declaration
= cp_parser_exception_declaration (parser
);
25339 finish_handler_parms (declaration
, handler
);
25340 parens
.require_close (parser
);
25341 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
25342 finish_handler (handler
);
25345 /* Parse an exception-declaration.
25347 exception-declaration:
25348 type-specifier-seq declarator
25349 type-specifier-seq abstract-declarator
25353 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25354 ellipsis variant is used. */
25357 cp_parser_exception_declaration (cp_parser
* parser
)
25359 cp_decl_specifier_seq type_specifiers
;
25360 cp_declarator
*declarator
;
25361 const char *saved_message
;
25363 /* If it's an ellipsis, it's easy to handle. */
25364 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25366 /* Consume the `...' token. */
25367 cp_lexer_consume_token (parser
->lexer
);
25371 /* Types may not be defined in exception-declarations. */
25372 saved_message
= parser
->type_definition_forbidden_message
;
25373 parser
->type_definition_forbidden_message
25374 = G_("types may not be defined in exception-declarations");
25376 /* Parse the type-specifier-seq. */
25377 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
25378 /*is_declaration=*/true,
25379 /*is_trailing_return=*/false,
25381 /* If it's a `)', then there is no declarator. */
25382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25385 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
25386 CP_PARSER_FLAGS_NONE
,
25387 /*ctor_dtor_or_conv_p=*/NULL
,
25388 /*parenthesized_p=*/NULL
,
25389 /*member_p=*/false,
25390 /*friend_p=*/false,
25391 /*static_p=*/false);
25393 /* Restore the saved message. */
25394 parser
->type_definition_forbidden_message
= saved_message
;
25396 if (!type_specifiers
.any_specifiers_p
)
25397 return error_mark_node
;
25399 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
25402 /* Parse a throw-expression.
25405 throw assignment-expression [opt]
25407 Returns a THROW_EXPR representing the throw-expression. */
25410 cp_parser_throw_expression (cp_parser
* parser
)
25415 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
25416 token
= cp_lexer_peek_token (parser
->lexer
);
25417 /* Figure out whether or not there is an assignment-expression
25418 following the "throw" keyword. */
25419 if (token
->type
== CPP_COMMA
25420 || token
->type
== CPP_SEMICOLON
25421 || token
->type
== CPP_CLOSE_PAREN
25422 || token
->type
== CPP_CLOSE_SQUARE
25423 || token
->type
== CPP_CLOSE_BRACE
25424 || token
->type
== CPP_COLON
)
25425 expression
= NULL_TREE
;
25427 expression
= cp_parser_assignment_expression (parser
);
25429 return build_throw (expression
);
25432 /* GNU Extensions */
25434 /* Parse an (optional) asm-specification.
25437 asm ( string-literal )
25439 If the asm-specification is present, returns a STRING_CST
25440 corresponding to the string-literal. Otherwise, returns
25444 cp_parser_asm_specification_opt (cp_parser
* parser
)
25447 tree asm_specification
;
25449 /* Peek at the next token. */
25450 token
= cp_lexer_peek_token (parser
->lexer
);
25451 /* If the next token isn't the `asm' keyword, then there's no
25452 asm-specification. */
25453 if (!cp_parser_is_keyword (token
, RID_ASM
))
25456 /* Consume the `asm' token. */
25457 cp_lexer_consume_token (parser
->lexer
);
25458 /* Look for the `('. */
25459 matching_parens parens
;
25460 parens
.require_open (parser
);
25462 /* Look for the string-literal. */
25463 asm_specification
= cp_parser_string_literal (parser
, false, false);
25465 /* Look for the `)'. */
25466 parens
.require_close (parser
);
25468 return asm_specification
;
25471 /* Parse an asm-operand-list.
25475 asm-operand-list , asm-operand
25478 string-literal ( expression )
25479 [ string-literal ] string-literal ( expression )
25481 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25482 each node is the expression. The TREE_PURPOSE is itself a
25483 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25484 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25485 is a STRING_CST for the string literal before the parenthesis. Returns
25486 ERROR_MARK_NODE if any of the operands are invalid. */
25489 cp_parser_asm_operand_list (cp_parser
* parser
)
25491 tree asm_operands
= NULL_TREE
;
25492 bool invalid_operands
= false;
25496 tree string_literal
;
25500 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25502 /* Consume the `[' token. */
25503 cp_lexer_consume_token (parser
->lexer
);
25504 /* Read the operand name. */
25505 name
= cp_parser_identifier (parser
);
25506 if (name
!= error_mark_node
)
25507 name
= build_string (IDENTIFIER_LENGTH (name
),
25508 IDENTIFIER_POINTER (name
));
25509 /* Look for the closing `]'. */
25510 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25514 /* Look for the string-literal. */
25515 string_literal
= cp_parser_string_literal (parser
, false, false);
25517 /* Look for the `('. */
25518 matching_parens parens
;
25519 parens
.require_open (parser
);
25520 /* Parse the expression. */
25521 expression
= cp_parser_expression (parser
);
25522 /* Look for the `)'. */
25523 parens
.require_close (parser
);
25525 if (name
== error_mark_node
25526 || string_literal
== error_mark_node
25527 || expression
== error_mark_node
)
25528 invalid_operands
= true;
25530 /* Add this operand to the list. */
25531 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
25534 /* If the next token is not a `,', there are no more
25536 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25538 /* Consume the `,'. */
25539 cp_lexer_consume_token (parser
->lexer
);
25542 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
25545 /* Parse an asm-clobber-list.
25549 asm-clobber-list , string-literal
25551 Returns a TREE_LIST, indicating the clobbers in the order that they
25552 appeared. The TREE_VALUE of each node is a STRING_CST. */
25555 cp_parser_asm_clobber_list (cp_parser
* parser
)
25557 tree clobbers
= NULL_TREE
;
25561 tree string_literal
;
25563 /* Look for the string literal. */
25564 string_literal
= cp_parser_string_literal (parser
, false, false);
25565 /* Add it to the list. */
25566 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
25567 /* If the next token is not a `,', then the list is
25569 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25571 /* Consume the `,' token. */
25572 cp_lexer_consume_token (parser
->lexer
);
25578 /* Parse an asm-label-list.
25582 asm-label-list , identifier
25584 Returns a TREE_LIST, indicating the labels in the order that they
25585 appeared. The TREE_VALUE of each node is a label. */
25588 cp_parser_asm_label_list (cp_parser
* parser
)
25590 tree labels
= NULL_TREE
;
25594 tree identifier
, label
, name
;
25596 /* Look for the identifier. */
25597 identifier
= cp_parser_identifier (parser
);
25598 if (!error_operand_p (identifier
))
25600 label
= lookup_label (identifier
);
25601 if (TREE_CODE (label
) == LABEL_DECL
)
25603 TREE_USED (label
) = 1;
25604 check_goto (label
);
25605 name
= build_string (IDENTIFIER_LENGTH (identifier
),
25606 IDENTIFIER_POINTER (identifier
));
25607 labels
= tree_cons (name
, label
, labels
);
25610 /* If the next token is not a `,', then the list is
25612 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25614 /* Consume the `,' token. */
25615 cp_lexer_consume_token (parser
->lexer
);
25618 return nreverse (labels
);
25621 /* Return TRUE iff the next tokens in the stream are possibly the
25622 beginning of a GNU extension attribute. */
25625 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25627 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25630 /* Return TRUE iff the next tokens in the stream are possibly the
25631 beginning of a standard C++-11 attribute specifier. */
25634 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25636 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25639 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25640 beginning of a standard C++-11 attribute specifier. */
25643 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25645 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25647 return (cxx_dialect
>= cxx11
25648 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25649 || (token
->type
== CPP_OPEN_SQUARE
25650 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25651 && token
->type
== CPP_OPEN_SQUARE
)));
25654 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25655 beginning of a GNU extension attribute. */
25658 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25660 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25662 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25665 /* Return true iff the next tokens can be the beginning of either a
25666 GNU attribute list, or a standard C++11 attribute sequence. */
25669 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25671 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25672 || cp_next_tokens_can_be_std_attribute_p (parser
));
25675 /* Return true iff the next Nth tokens can be the beginning of either
25676 a GNU attribute list, or a standard C++11 attribute sequence. */
25679 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25681 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25682 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25685 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25686 of GNU attributes, or return NULL. */
25689 cp_parser_attributes_opt (cp_parser
*parser
)
25691 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25692 return cp_parser_gnu_attributes_opt (parser
);
25693 return cp_parser_std_attribute_spec_seq (parser
);
25696 /* Parse an (optional) series of attributes.
25699 attributes attribute
25702 __attribute__ (( attribute-list [opt] ))
25704 The return value is as for cp_parser_gnu_attribute_list. */
25707 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25709 tree attributes
= NULL_TREE
;
25711 temp_override
<bool> cleanup
25712 (parser
->auto_is_implicit_function_template_parm_p
, false);
25717 tree attribute_list
;
25720 /* Peek at the next token. */
25721 token
= cp_lexer_peek_token (parser
->lexer
);
25722 /* If it's not `__attribute__', then we're done. */
25723 if (token
->keyword
!= RID_ATTRIBUTE
)
25726 /* Consume the `__attribute__' keyword. */
25727 cp_lexer_consume_token (parser
->lexer
);
25728 /* Look for the two `(' tokens. */
25729 matching_parens outer_parens
;
25730 outer_parens
.require_open (parser
);
25731 matching_parens inner_parens
;
25732 inner_parens
.require_open (parser
);
25734 /* Peek at the next token. */
25735 token
= cp_lexer_peek_token (parser
->lexer
);
25736 if (token
->type
!= CPP_CLOSE_PAREN
)
25737 /* Parse the attribute-list. */
25738 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25740 /* If the next token is a `)', then there is no attribute
25742 attribute_list
= NULL
;
25744 /* Look for the two `)' tokens. */
25745 if (!inner_parens
.require_close (parser
))
25747 if (!outer_parens
.require_close (parser
))
25750 cp_parser_skip_to_end_of_statement (parser
);
25752 /* Add these new attributes to the list. */
25753 attributes
= attr_chainon (attributes
, attribute_list
);
25759 /* Parse a GNU attribute-list.
25763 attribute-list , attribute
25767 identifier ( identifier )
25768 identifier ( identifier , expression-list )
25769 identifier ( expression-list )
25771 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25772 to an attribute. The TREE_PURPOSE of each node is the identifier
25773 indicating which attribute is in use. The TREE_VALUE represents
25774 the arguments, if any. */
25777 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
25779 tree attribute_list
= NULL_TREE
;
25780 bool save_translate_strings_p
= parser
->translate_strings_p
;
25782 /* Don't create wrapper nodes within attributes: the
25783 handlers don't know how to handle them. */
25784 auto_suppress_location_wrappers sentinel
;
25786 parser
->translate_strings_p
= false;
25793 /* Look for the identifier. We also allow keywords here; for
25794 example `__attribute__ ((const))' is legal. */
25795 token
= cp_lexer_peek_token (parser
->lexer
);
25796 if (token
->type
== CPP_NAME
25797 || token
->type
== CPP_KEYWORD
)
25799 tree arguments
= NULL_TREE
;
25801 /* Consume the token, but save it since we need it for the
25802 SIMD enabled function parsing. */
25803 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25805 /* Save away the identifier that indicates which attribute
25807 identifier
= (token
->type
== CPP_KEYWORD
)
25808 /* For keywords, use the canonical spelling, not the
25809 parsed identifier. */
25810 ? ridpointers
[(int) token
->keyword
]
25811 : id_token
->u
.value
;
25813 identifier
= canonicalize_attr_name (identifier
);
25814 attribute
= build_tree_list (identifier
, NULL_TREE
);
25816 /* Peek at the next token. */
25817 token
= cp_lexer_peek_token (parser
->lexer
);
25818 /* If it's an `(', then parse the attribute arguments. */
25819 if (token
->type
== CPP_OPEN_PAREN
)
25821 vec
<tree
, va_gc
> *vec
;
25822 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25823 ? id_attr
: normal_attr
);
25824 vec
= cp_parser_parenthesized_expression_list
25825 (parser
, attr_flag
, /*cast_p=*/false,
25826 /*allow_expansion_p=*/false,
25827 /*non_constant_p=*/NULL
);
25829 arguments
= error_mark_node
;
25832 arguments
= build_tree_list_vec (vec
);
25833 release_tree_vector (vec
);
25835 /* Save the arguments away. */
25836 TREE_VALUE (attribute
) = arguments
;
25839 if (arguments
!= error_mark_node
)
25841 /* Add this attribute to the list. */
25842 TREE_CHAIN (attribute
) = attribute_list
;
25843 attribute_list
= attribute
;
25846 token
= cp_lexer_peek_token (parser
->lexer
);
25848 /* Unless EXACTLY_ONE is set look for more attributes.
25849 If the next token isn't a `,', we're done. */
25850 if (exactly_one
|| token
->type
!= CPP_COMMA
)
25853 /* Consume the comma and keep going. */
25854 cp_lexer_consume_token (parser
->lexer
);
25856 parser
->translate_strings_p
= save_translate_strings_p
;
25858 /* We built up the list in reverse order. */
25859 return nreverse (attribute_list
);
25862 /* Parse a standard C++11 attribute.
25864 The returned representation is a TREE_LIST which TREE_PURPOSE is
25865 the scoped name of the attribute, and the TREE_VALUE is its
25868 Note that the scoped name of the attribute is itself a TREE_LIST
25869 which TREE_PURPOSE is the namespace of the attribute, and
25870 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25871 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25872 and which TREE_PURPOSE is directly the attribute name.
25874 Clients of the attribute code should use get_attribute_namespace
25875 and get_attribute_name to get the actual namespace and name of
25876 attributes, regardless of their being GNU or C++11 attributes.
25879 attribute-token attribute-argument-clause [opt]
25883 attribute-scoped-token
25885 attribute-scoped-token:
25886 attribute-namespace :: identifier
25888 attribute-namespace:
25891 attribute-argument-clause:
25892 ( balanced-token-seq )
25894 balanced-token-seq:
25895 balanced-token [opt]
25896 balanced-token-seq balanced-token
25899 ( balanced-token-seq )
25900 [ balanced-token-seq ]
25901 { balanced-token-seq }. */
25904 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25906 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25909 temp_override
<bool> cleanup
25910 (parser
->auto_is_implicit_function_template_parm_p
, false);
25912 /* First, parse name of the attribute, a.k.a attribute-token. */
25914 token
= cp_lexer_peek_token (parser
->lexer
);
25915 if (token
->type
== CPP_NAME
)
25916 attr_id
= token
->u
.value
;
25917 else if (token
->type
== CPP_KEYWORD
)
25918 attr_id
= ridpointers
[(int) token
->keyword
];
25919 else if (token
->flags
& NAMED_OP
)
25920 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25922 if (attr_id
== NULL_TREE
)
25925 cp_lexer_consume_token (parser
->lexer
);
25927 token
= cp_lexer_peek_token (parser
->lexer
);
25928 if (token
->type
== CPP_SCOPE
)
25930 /* We are seeing a scoped attribute token. */
25932 cp_lexer_consume_token (parser
->lexer
);
25934 error_at (token
->location
, "attribute using prefix used together "
25935 "with scoped attribute token");
25938 token
= cp_lexer_consume_token (parser
->lexer
);
25939 if (token
->type
== CPP_NAME
)
25940 attr_id
= token
->u
.value
;
25941 else if (token
->type
== CPP_KEYWORD
)
25942 attr_id
= ridpointers
[(int) token
->keyword
];
25943 else if (token
->flags
& NAMED_OP
)
25944 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25947 error_at (token
->location
,
25948 "expected an identifier for the attribute name");
25949 return error_mark_node
;
25952 attr_ns
= canonicalize_attr_name (attr_ns
);
25953 attr_id
= canonicalize_attr_name (attr_id
);
25954 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25956 token
= cp_lexer_peek_token (parser
->lexer
);
25960 attr_ns
= canonicalize_attr_name (attr_ns
);
25961 attr_id
= canonicalize_attr_name (attr_id
);
25962 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25967 attr_id
= canonicalize_attr_name (attr_id
);
25968 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25970 /* C++11 noreturn attribute is equivalent to GNU's. */
25971 if (is_attribute_p ("noreturn", attr_id
))
25972 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25973 /* C++14 deprecated attribute is equivalent to GNU's. */
25974 else if (is_attribute_p ("deprecated", attr_id
))
25975 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25976 /* C++17 fallthrough attribute is equivalent to GNU's. */
25977 else if (is_attribute_p ("fallthrough", attr_id
))
25978 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25979 /* Transactional Memory TS optimize_for_synchronized attribute is
25980 equivalent to GNU transaction_callable. */
25981 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25982 TREE_PURPOSE (attribute
)
25983 = get_identifier ("transaction_callable");
25984 /* Transactional Memory attributes are GNU attributes. */
25985 else if (tm_attr_to_mask (attr_id
))
25986 TREE_PURPOSE (attribute
) = attr_id
;
25989 /* Now parse the optional argument clause of the attribute. */
25991 if (token
->type
!= CPP_OPEN_PAREN
)
25995 vec
<tree
, va_gc
> *vec
;
25996 int attr_flag
= normal_attr
;
25998 if (attr_ns
== gnu_identifier
25999 && attribute_takes_identifier_p (attr_id
))
26000 /* A GNU attribute that takes an identifier in parameter. */
26001 attr_flag
= id_attr
;
26003 vec
= cp_parser_parenthesized_expression_list
26004 (parser
, attr_flag
, /*cast_p=*/false,
26005 /*allow_expansion_p=*/true,
26006 /*non_constant_p=*/NULL
);
26008 arguments
= error_mark_node
;
26011 arguments
= build_tree_list_vec (vec
);
26012 release_tree_vector (vec
);
26015 if (arguments
== error_mark_node
)
26016 attribute
= error_mark_node
;
26018 TREE_VALUE (attribute
) = arguments
;
26024 /* Check that the attribute ATTRIBUTE appears at most once in the
26025 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26026 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26027 isn't implemented yet in GCC. */
26030 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
26034 tree name
= get_attribute_name (attribute
);
26035 if (is_attribute_p ("noreturn", name
)
26036 && lookup_attribute ("noreturn", attributes
))
26037 error ("attribute %<noreturn%> can appear at most once "
26038 "in an attribute-list");
26039 else if (is_attribute_p ("deprecated", name
)
26040 && lookup_attribute ("deprecated", attributes
))
26041 error ("attribute %<deprecated%> can appear at most once "
26042 "in an attribute-list");
26046 /* Parse a list of standard C++-11 attributes.
26050 attribute-list , attribute[opt]
26052 attribute-list , attribute ...
26056 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
26058 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
26059 cp_token
*token
= NULL
;
26063 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
26064 if (attribute
== error_mark_node
)
26066 if (attribute
!= NULL_TREE
)
26068 cp_parser_check_std_attribute (attributes
, attribute
);
26069 TREE_CHAIN (attribute
) = attributes
;
26070 attributes
= attribute
;
26072 token
= cp_lexer_peek_token (parser
->lexer
);
26073 if (token
->type
== CPP_ELLIPSIS
)
26075 cp_lexer_consume_token (parser
->lexer
);
26076 if (attribute
== NULL_TREE
)
26077 error_at (token
->location
,
26078 "expected attribute before %<...%>");
26081 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
26082 if (pack
== error_mark_node
)
26083 return error_mark_node
;
26084 TREE_VALUE (attribute
) = pack
;
26086 token
= cp_lexer_peek_token (parser
->lexer
);
26088 if (token
->type
!= CPP_COMMA
)
26090 cp_lexer_consume_token (parser
->lexer
);
26092 attributes
= nreverse (attributes
);
26096 /* Parse a standard C++-11 attribute specifier.
26098 attribute-specifier:
26099 [ [ attribute-using-prefix [opt] attribute-list ] ]
26100 alignment-specifier
26102 attribute-using-prefix:
26103 using attribute-namespace :
26105 alignment-specifier:
26106 alignas ( type-id ... [opt] )
26107 alignas ( alignment-expression ... [opt] ). */
26110 cp_parser_std_attribute_spec (cp_parser
*parser
)
26112 tree attributes
= NULL_TREE
;
26113 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26115 if (token
->type
== CPP_OPEN_SQUARE
26116 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
26118 tree attr_ns
= NULL_TREE
;
26120 cp_lexer_consume_token (parser
->lexer
);
26121 cp_lexer_consume_token (parser
->lexer
);
26123 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26125 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26126 if (token
->type
== CPP_NAME
)
26127 attr_ns
= token
->u
.value
;
26128 else if (token
->type
== CPP_KEYWORD
)
26129 attr_ns
= ridpointers
[(int) token
->keyword
];
26130 else if (token
->flags
& NAMED_OP
)
26131 attr_ns
= get_identifier (cpp_type2name (token
->type
,
26134 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
26136 if (cxx_dialect
< cxx17
26137 && !in_system_header_at (input_location
))
26138 pedwarn (input_location
, 0,
26139 "attribute using prefix only available "
26140 "with -std=c++17 or -std=gnu++17");
26142 cp_lexer_consume_token (parser
->lexer
);
26143 cp_lexer_consume_token (parser
->lexer
);
26144 cp_lexer_consume_token (parser
->lexer
);
26147 attr_ns
= NULL_TREE
;
26150 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
26152 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
26153 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
26154 cp_parser_skip_to_end_of_statement (parser
);
26156 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26157 when we are sure that we have actually parsed them. */
26158 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26164 /* Look for an alignment-specifier. */
26166 token
= cp_lexer_peek_token (parser
->lexer
);
26168 if (token
->type
!= CPP_KEYWORD
26169 || token
->keyword
!= RID_ALIGNAS
)
26172 cp_lexer_consume_token (parser
->lexer
);
26173 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26175 matching_parens parens
;
26176 if (!parens
.require_open (parser
))
26177 return error_mark_node
;
26179 cp_parser_parse_tentatively (parser
);
26180 alignas_expr
= cp_parser_type_id (parser
);
26182 if (!cp_parser_parse_definitely (parser
))
26184 alignas_expr
= cp_parser_assignment_expression (parser
);
26185 if (alignas_expr
== error_mark_node
)
26186 cp_parser_skip_to_end_of_statement (parser
);
26187 if (alignas_expr
== NULL_TREE
26188 || alignas_expr
== error_mark_node
)
26189 return alignas_expr
;
26192 alignas_expr
= cxx_alignas_expr (alignas_expr
);
26193 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
26195 /* Handle alignas (pack...). */
26196 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26198 cp_lexer_consume_token (parser
->lexer
);
26199 alignas_expr
= make_pack_expansion (alignas_expr
);
26202 /* Something went wrong, so don't build the attribute. */
26203 if (alignas_expr
== error_mark_node
)
26204 return error_mark_node
;
26206 if (!parens
.require_close (parser
))
26207 return error_mark_node
;
26209 /* Build the C++-11 representation of an 'aligned'
26212 = build_tree_list (build_tree_list (gnu_identifier
,
26213 aligned_identifier
), alignas_expr
);
26219 /* Parse a standard C++-11 attribute-specifier-seq.
26221 attribute-specifier-seq:
26222 attribute-specifier-seq [opt] attribute-specifier
26226 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
26228 tree attr_specs
= NULL_TREE
;
26229 tree attr_last
= NULL_TREE
;
26231 /* Don't create wrapper nodes within attributes: the
26232 handlers don't know how to handle them. */
26233 auto_suppress_location_wrappers sentinel
;
26237 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
26238 if (attr_spec
== NULL_TREE
)
26240 if (attr_spec
== error_mark_node
)
26241 return error_mark_node
;
26244 TREE_CHAIN (attr_last
) = attr_spec
;
26246 attr_specs
= attr_last
= attr_spec
;
26247 attr_last
= tree_last (attr_last
);
26253 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26254 return index of the first token after balanced-token, or N on failure. */
26257 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
26260 int nparens
= 0, nbraces
= 0, nsquares
= 0;
26262 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
26264 case CPP_PRAGMA_EOL
:
26265 if (!parser
->lexer
->in_pragma
)
26269 /* Ran out of tokens. */
26271 case CPP_OPEN_PAREN
:
26274 case CPP_OPEN_BRACE
:
26277 case CPP_OPEN_SQUARE
:
26280 case CPP_CLOSE_PAREN
:
26283 case CPP_CLOSE_BRACE
:
26286 case CPP_CLOSE_SQUARE
:
26292 while (nparens
|| nbraces
|| nsquares
);
26296 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26297 return index of the first token after the GNU attribute tokens, or N on
26301 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
26305 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
26306 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
26307 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
26310 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
26313 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
26320 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26321 next token), return index of the first token after the standard C++11
26322 attribute tokens, or N on failure. */
26325 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
26329 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
26330 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
26332 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26335 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
26339 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
26340 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
26342 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26353 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26354 as the next token), return index of the first token after the attribute
26355 tokens, or N on failure. */
26358 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
26360 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
26361 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
26362 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
26365 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26366 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26367 current value of the PEDANTIC flag, regardless of whether or not
26368 the `__extension__' keyword is present. The caller is responsible
26369 for restoring the value of the PEDANTIC flag. */
26372 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
26374 /* Save the old value of the PEDANTIC flag. */
26375 *saved_pedantic
= pedantic
;
26377 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
26379 /* Consume the `__extension__' token. */
26380 cp_lexer_consume_token (parser
->lexer
);
26381 /* We're not being pedantic while the `__extension__' keyword is
26391 /* Parse a label declaration.
26394 __label__ label-declarator-seq ;
26396 label-declarator-seq:
26397 identifier , label-declarator-seq
26401 cp_parser_label_declaration (cp_parser
* parser
)
26403 /* Look for the `__label__' keyword. */
26404 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
26410 /* Look for an identifier. */
26411 identifier
= cp_parser_identifier (parser
);
26412 /* If we failed, stop. */
26413 if (identifier
== error_mark_node
)
26415 /* Declare it as a label. */
26416 finish_label_decl (identifier
);
26417 /* If the next token is a `;', stop. */
26418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26420 /* Look for the `,' separating the label declarations. */
26421 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
26424 /* Look for the final `;'. */
26425 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
26428 // -------------------------------------------------------------------------- //
26431 // Parse a requires clause.
26433 // requires-clause:
26434 // 'requires' logical-or-expression
26436 // The required logical-or-expression must be a constant expression. Note
26437 // that we don't check that the expression is constepxr here. We defer until
26438 // we analyze constraints and then, we only check atomic constraints.
26440 cp_parser_requires_clause (cp_parser
*parser
)
26442 // Parse the requires clause so that it is not automatically folded.
26443 ++processing_template_decl
;
26444 tree expr
= cp_parser_binary_expression (parser
, false, false,
26445 PREC_NOT_OPERATOR
, NULL
);
26446 if (check_for_bare_parameter_packs (expr
))
26447 expr
= error_mark_node
;
26448 --processing_template_decl
;
26452 // Optionally parse a requires clause:
26454 cp_parser_requires_clause_opt (cp_parser
*parser
)
26456 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
26457 if (tok
->keyword
!= RID_REQUIRES
)
26459 if (!flag_concepts
&& tok
->type
== CPP_NAME
26460 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
26462 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26463 "%<requires%> only available with -fconcepts");
26464 /* Parse and discard the requires-clause. */
26465 cp_lexer_consume_token (parser
->lexer
);
26466 cp_parser_requires_clause (parser
);
26470 cp_lexer_consume_token (parser
->lexer
);
26471 return cp_parser_requires_clause (parser
);
26475 /*---------------------------------------------------------------------------
26476 Requires expressions
26477 ---------------------------------------------------------------------------*/
26479 /* Parse a requires expression
26481 requirement-expression:
26482 'requires' requirement-parameter-list [opt] requirement-body */
26484 cp_parser_requires_expression (cp_parser
*parser
)
26486 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
26487 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26489 /* A requires-expression shall appear only within a concept
26490 definition or a requires-clause.
26492 TODO: Implement this diagnostic correctly. */
26493 if (!processing_template_decl
)
26495 error_at (loc
, "a requires expression cannot appear outside a template");
26496 cp_parser_skip_to_end_of_statement (parser
);
26497 return error_mark_node
;
26502 /* Local parameters are delared as variables within the scope
26503 of the expression. They are not visible past the end of
26504 the expression. Expressions within the requires-expression
26505 are unevaluated. */
26506 struct scope_sentinel
26510 ++cp_unevaluated_operand
;
26511 begin_scope (sk_block
, NULL_TREE
);
26516 pop_bindings_and_leave_scope ();
26517 --cp_unevaluated_operand
;
26521 /* Parse the optional parameter list. */
26522 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26524 parms
= cp_parser_requirement_parameter_list (parser
);
26525 if (parms
== error_mark_node
)
26526 return error_mark_node
;
26531 /* Parse the requirement body. */
26532 reqs
= cp_parser_requirement_body (parser
);
26533 if (reqs
== error_mark_node
)
26534 return error_mark_node
;
26537 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26539 grokparms (parms
, &parms
);
26540 return finish_requires_expr (parms
, reqs
);
26543 /* Parse a parameterized requirement.
26545 requirement-parameter-list:
26546 '(' parameter-declaration-clause ')' */
26548 cp_parser_requirement_parameter_list (cp_parser
*parser
)
26550 matching_parens parens
;
26551 if (!parens
.require_open (parser
))
26552 return error_mark_node
;
26555 = cp_parser_parameter_declaration_clause (parser
, CP_PARSER_FLAGS_NONE
);
26557 if (!parens
.require_close (parser
))
26558 return error_mark_node
;
26563 /* Parse the body of a requirement.
26566 '{' requirement-list '}' */
26568 cp_parser_requirement_body (cp_parser
*parser
)
26570 matching_braces braces
;
26571 if (!braces
.require_open (parser
))
26572 return error_mark_node
;
26574 tree reqs
= cp_parser_requirement_list (parser
);
26576 if (!braces
.require_close (parser
))
26577 return error_mark_node
;
26582 /* Parse a list of requirements.
26586 requirement-list ';' requirement[opt] */
26588 cp_parser_requirement_list (cp_parser
*parser
)
26590 tree result
= NULL_TREE
;
26593 tree req
= cp_parser_requirement (parser
);
26594 if (req
== error_mark_node
)
26595 return error_mark_node
;
26597 result
= tree_cons (NULL_TREE
, req
, result
);
26599 /* If we see a semi-colon, consume it. */
26600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26601 cp_lexer_consume_token (parser
->lexer
);
26603 /* Stop processing at the end of the list. */
26604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26608 /* Reverse the order of requirements so they are analyzed in
26609 declaration order. */
26610 return nreverse (result
);
26613 /* Parse a syntactic requirement or type requirement.
26617 compound-requirement
26619 nested-requirement */
26621 cp_parser_requirement (cp_parser
*parser
)
26623 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26624 return cp_parser_compound_requirement (parser
);
26625 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26626 return cp_parser_type_requirement (parser
);
26627 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
26628 return cp_parser_nested_requirement (parser
);
26630 return cp_parser_simple_requirement (parser
);
26633 /* Parse a simple requirement.
26635 simple-requirement:
26638 cp_parser_simple_requirement (cp_parser
*parser
)
26640 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26641 if (!expr
|| expr
== error_mark_node
)
26642 return error_mark_node
;
26644 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26645 return error_mark_node
;
26647 return finish_simple_requirement (expr
);
26650 /* Parse a type requirement
26653 nested-name-specifier [opt] required-type-name ';'
26655 required-type-name:
26657 'template' [opt] simple-template-id */
26659 cp_parser_type_requirement (cp_parser
*parser
)
26661 cp_lexer_consume_token (parser
->lexer
);
26663 // Save the scope before parsing name specifiers.
26664 tree saved_scope
= parser
->scope
;
26665 tree saved_object_scope
= parser
->object_scope
;
26666 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26667 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26668 cp_parser_nested_name_specifier_opt (parser
,
26669 /*typename_keyword_p=*/true,
26670 /*check_dependency_p=*/false,
26672 /*is_declaration=*/false);
26675 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26677 cp_lexer_consume_token (parser
->lexer
);
26678 type
= cp_parser_template_id (parser
,
26679 /*template_keyword_p=*/true,
26680 /*check_dependency=*/false,
26681 /*tag_type=*/none_type
,
26682 /*is_declaration=*/false);
26683 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26684 /*complain=*/tf_error
);
26687 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26689 if (TREE_CODE (type
) == TYPE_DECL
)
26690 type
= TREE_TYPE (type
);
26692 parser
->scope
= saved_scope
;
26693 parser
->object_scope
= saved_object_scope
;
26694 parser
->qualifying_scope
= saved_qualifying_scope
;
26696 if (type
== error_mark_node
)
26697 cp_parser_skip_to_end_of_statement (parser
);
26699 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26700 return error_mark_node
;
26701 if (type
== error_mark_node
)
26702 return error_mark_node
;
26704 return finish_type_requirement (type
);
26707 /* Parse a compound requirement
26709 compound-requirement:
26710 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26712 cp_parser_compound_requirement (cp_parser
*parser
)
26714 /* Parse an expression enclosed in '{ }'s. */
26715 matching_braces braces
;
26716 if (!braces
.require_open (parser
))
26717 return error_mark_node
;
26719 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26720 if (!expr
|| expr
== error_mark_node
)
26721 return error_mark_node
;
26723 if (!braces
.require_close (parser
))
26724 return error_mark_node
;
26726 /* Parse the optional noexcept. */
26727 bool noexcept_p
= false;
26728 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26730 cp_lexer_consume_token (parser
->lexer
);
26734 /* Parse the optional trailing return type. */
26735 tree type
= NULL_TREE
;
26736 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26738 cp_lexer_consume_token (parser
->lexer
);
26739 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26740 parser
->in_result_type_constraint_p
= true;
26741 type
= cp_parser_trailing_type_id (parser
);
26742 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26743 if (type
== error_mark_node
)
26744 return error_mark_node
;
26747 return finish_compound_requirement (expr
, type
, noexcept_p
);
26750 /* Parse a nested requirement. This is the same as a requires clause.
26752 nested-requirement:
26755 cp_parser_nested_requirement (cp_parser
*parser
)
26757 cp_lexer_consume_token (parser
->lexer
);
26758 tree req
= cp_parser_requires_clause (parser
);
26759 if (req
== error_mark_node
)
26760 return error_mark_node
;
26761 return finish_nested_requirement (req
);
26764 /* Support Functions */
26766 /* Return the appropriate prefer_type argument for lookup_name_real based on
26767 tag_type and template_mem_access. */
26770 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26772 /* DR 141: When looking in the current enclosing context for a template-name
26773 after -> or ., only consider class templates. */
26774 if (template_mem_access
)
26778 case none_type
: return 0; // No preference.
26779 case scope_type
: return 1; // Type or namespace.
26780 default: return 2; // Type only.
26784 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26785 NAME should have one of the representations used for an
26786 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26787 is returned. If PARSER->SCOPE is a dependent type, then a
26788 SCOPE_REF is returned.
26790 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26791 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26792 was formed. Abstractly, such entities should not be passed to this
26793 function, because they do not need to be looked up, but it is
26794 simpler to check for this special case here, rather than at the
26797 In cases not explicitly covered above, this function returns a
26798 DECL, OVERLOAD, or baselink representing the result of the lookup.
26799 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26802 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26803 (e.g., "struct") that was used. In that case bindings that do not
26804 refer to types are ignored.
26806 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26809 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26812 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26815 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26816 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26817 NULL_TREE otherwise. */
26820 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26821 enum tag_types tag_type
,
26824 bool check_dependency
,
26825 tree
*ambiguous_decls
,
26826 location_t name_location
)
26829 tree object_type
= parser
->context
->object_type
;
26831 /* Assume that the lookup will be unambiguous. */
26832 if (ambiguous_decls
)
26833 *ambiguous_decls
= NULL_TREE
;
26835 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26836 no longer valid. Note that if we are parsing tentatively, and
26837 the parse fails, OBJECT_TYPE will be automatically restored. */
26838 parser
->context
->object_type
= NULL_TREE
;
26840 if (name
== error_mark_node
)
26841 return error_mark_node
;
26843 /* A template-id has already been resolved; there is no lookup to
26845 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26847 if (BASELINK_P (name
))
26849 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26850 == TEMPLATE_ID_EXPR
);
26854 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26855 it should already have been checked to make sure that the name
26856 used matches the type being destroyed. */
26857 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26861 /* Figure out to which type this destructor applies. */
26863 type
= parser
->scope
;
26864 else if (object_type
)
26865 type
= object_type
;
26867 type
= current_class_type
;
26868 /* If that's not a class type, there is no destructor. */
26869 if (!type
|| !CLASS_TYPE_P (type
))
26870 return error_mark_node
;
26872 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26873 lazily_declare_fn (sfk_destructor
, type
);
26875 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26878 return error_mark_node
;
26881 /* By this point, the NAME should be an ordinary identifier. If
26882 the id-expression was a qualified name, the qualifying scope is
26883 stored in PARSER->SCOPE at this point. */
26884 gcc_assert (identifier_p (name
));
26886 /* Perform the lookup. */
26891 if (parser
->scope
== error_mark_node
)
26892 return error_mark_node
;
26894 /* If the SCOPE is dependent, the lookup must be deferred until
26895 the template is instantiated -- unless we are explicitly
26896 looking up names in uninstantiated templates. Even then, we
26897 cannot look up the name if the scope is not a class type; it
26898 might, for example, be a template type parameter. */
26899 dependent_p
= (TYPE_P (parser
->scope
)
26900 && dependent_scope_p (parser
->scope
));
26901 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26903 /* Defer lookup. */
26904 decl
= error_mark_node
;
26907 tree pushed_scope
= NULL_TREE
;
26909 /* If PARSER->SCOPE is a dependent type, then it must be a
26910 class type, and we must not be checking dependencies;
26911 otherwise, we would have processed this lookup above. So
26912 that PARSER->SCOPE is not considered a dependent base by
26913 lookup_member, we must enter the scope here. */
26915 pushed_scope
= push_scope (parser
->scope
);
26917 /* If the PARSER->SCOPE is a template specialization, it
26918 may be instantiated during name lookup. In that case,
26919 errors may be issued. Even if we rollback the current
26920 tentative parse, those errors are valid. */
26921 decl
= lookup_qualified_name (parser
->scope
, name
,
26922 prefer_type_arg (tag_type
),
26923 /*complain=*/true);
26925 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26926 lookup result and the nested-name-specifier nominates a class C:
26927 * if the name specified after the nested-name-specifier, when
26928 looked up in C, is the injected-class-name of C (Clause 9), or
26929 * if the name specified after the nested-name-specifier is the
26930 same as the identifier or the simple-template-id's template-
26931 name in the last component of the nested-name-specifier,
26932 the name is instead considered to name the constructor of
26933 class C. [ Note: for example, the constructor is not an
26934 acceptable lookup result in an elaborated-type-specifier so
26935 the constructor would not be used in place of the
26936 injected-class-name. --end note ] Such a constructor name
26937 shall be used only in the declarator-id of a declaration that
26938 names a constructor or in a using-declaration. */
26939 if (tag_type
== none_type
26940 && DECL_SELF_REFERENCE_P (decl
)
26941 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26942 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26943 prefer_type_arg (tag_type
),
26944 /*complain=*/true);
26946 /* If we have a single function from a using decl, pull it out. */
26947 if (TREE_CODE (decl
) == OVERLOAD
26948 && !really_overloaded_fn (decl
))
26949 decl
= OVL_FUNCTION (decl
);
26952 pop_scope (pushed_scope
);
26955 /* If the scope is a dependent type and either we deferred lookup or
26956 we did lookup but didn't find the name, rememeber the name. */
26957 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26958 && dependent_type_p (parser
->scope
))
26964 /* The resolution to Core Issue 180 says that `struct
26965 A::B' should be considered a type-name, even if `A'
26967 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26968 /*complain=*/tf_error
);
26969 if (type
!= error_mark_node
)
26970 decl
= TYPE_NAME (type
);
26972 else if (is_template
26973 && (cp_parser_next_token_ends_template_argument_p (parser
)
26974 || cp_lexer_next_token_is (parser
->lexer
,
26976 decl
= make_unbound_class_template (parser
->scope
,
26978 /*complain=*/tf_error
);
26980 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26981 parser
->scope
, name
,
26984 parser
->qualifying_scope
= parser
->scope
;
26985 parser
->object_scope
= NULL_TREE
;
26987 else if (object_type
)
26989 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26990 OBJECT_TYPE is not a class. */
26991 if (CLASS_TYPE_P (object_type
))
26992 /* If the OBJECT_TYPE is a template specialization, it may
26993 be instantiated during name lookup. In that case, errors
26994 may be issued. Even if we rollback the current tentative
26995 parse, those errors are valid. */
26996 decl
= lookup_member (object_type
,
26999 prefer_type_arg (tag_type
),
27000 tf_warning_or_error
);
27005 /* Look it up in the enclosing context. DR 141: When looking for a
27006 template-name after -> or ., only consider class templates. */
27007 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
27009 /*block_p=*/true, is_namespace
, 0);
27010 if (object_type
== unknown_type_node
)
27011 /* The object is type-dependent, so we can't look anything up; we used
27012 this to get the DR 141 behavior. */
27013 object_type
= NULL_TREE
;
27014 parser
->object_scope
= object_type
;
27015 parser
->qualifying_scope
= NULL_TREE
;
27019 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
27021 /*block_p=*/true, is_namespace
, 0);
27022 parser
->qualifying_scope
= NULL_TREE
;
27023 parser
->object_scope
= NULL_TREE
;
27026 /* If the lookup failed, let our caller know. */
27027 if (!decl
|| decl
== error_mark_node
)
27028 return error_mark_node
;
27030 /* Pull out the template from an injected-class-name (or multiple). */
27032 decl
= maybe_get_template_decl_from_type_decl (decl
);
27034 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27035 if (TREE_CODE (decl
) == TREE_LIST
)
27037 if (ambiguous_decls
)
27038 *ambiguous_decls
= decl
;
27039 /* The error message we have to print is too complicated for
27040 cp_parser_error, so we incorporate its actions directly. */
27041 if (!cp_parser_simulate_error (parser
))
27043 error_at (name_location
, "reference to %qD is ambiguous",
27045 print_candidates (decl
);
27047 return error_mark_node
;
27050 gcc_assert (DECL_P (decl
)
27051 || TREE_CODE (decl
) == OVERLOAD
27052 || TREE_CODE (decl
) == SCOPE_REF
27053 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
27054 || BASELINK_P (decl
));
27056 /* If we have resolved the name of a member declaration, check to
27057 see if the declaration is accessible. When the name resolves to
27058 set of overloaded functions, accessibility is checked when
27059 overload resolution is done.
27061 During an explicit instantiation, access is not checked at all,
27062 as per [temp.explicit]. */
27064 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
27066 maybe_record_typedef_use (decl
);
27068 return cp_expr (decl
, name_location
);
27071 /* Like cp_parser_lookup_name, but for use in the typical case where
27072 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27073 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27076 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
27078 return cp_parser_lookup_name (parser
, name
,
27080 /*is_template=*/false,
27081 /*is_namespace=*/false,
27082 /*check_dependency=*/true,
27083 /*ambiguous_decls=*/NULL
,
27087 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27088 the current context, return the TYPE_DECL. If TAG_NAME_P is
27089 true, the DECL indicates the class being defined in a class-head,
27090 or declared in an elaborated-type-specifier.
27092 Otherwise, return DECL. */
27095 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
27097 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27098 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27101 template <typename T> struct B;
27104 template <typename T> struct A::B {};
27106 Similarly, in an elaborated-type-specifier:
27108 namespace N { struct X{}; }
27111 template <typename T> friend struct N::X;
27114 However, if the DECL refers to a class type, and we are in
27115 the scope of the class, then the name lookup automatically
27116 finds the TYPE_DECL created by build_self_reference rather
27117 than a TEMPLATE_DECL. For example, in:
27119 template <class T> struct S {
27123 there is no need to handle such case. */
27125 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
27126 return DECL_TEMPLATE_RESULT (decl
);
27131 /* If too many, or too few, template-parameter lists apply to the
27132 declarator, issue an error message. Returns TRUE if all went well,
27133 and FALSE otherwise. */
27136 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
27137 cp_declarator
*declarator
,
27138 location_t declarator_location
)
27140 switch (declarator
->kind
)
27144 unsigned num_templates
= 0;
27145 tree scope
= declarator
->u
.id
.qualifying_scope
;
27146 bool template_id_p
= false;
27149 num_templates
= num_template_headers_for_class (scope
);
27150 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
27151 == TEMPLATE_ID_EXPR
)
27153 /* If the DECLARATOR has the form `X<y>' then it uses one
27154 additional level of template parameters. */
27156 template_id_p
= true;
27159 return cp_parser_check_template_parameters
27160 (parser
, num_templates
, template_id_p
, declarator_location
,
27167 case cdk_reference
:
27169 return (cp_parser_check_declarator_template_parameters
27170 (parser
, declarator
->declarator
, declarator_location
));
27177 gcc_unreachable ();
27182 /* NUM_TEMPLATES were used in the current declaration. If that is
27183 invalid, return FALSE and issue an error messages. Otherwise,
27184 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27185 declarator and we can print more accurate diagnostics. */
27188 cp_parser_check_template_parameters (cp_parser
* parser
,
27189 unsigned num_templates
,
27190 bool template_id_p
,
27191 location_t location
,
27192 cp_declarator
*declarator
)
27194 /* If there are the same number of template classes and parameter
27195 lists, that's OK. */
27196 if (parser
->num_template_parameter_lists
== num_templates
)
27198 /* If there are more, but only one more, and the name ends in an identifier,
27199 then we are declaring a primary template. That's OK too. */
27201 && parser
->num_template_parameter_lists
== num_templates
+ 1)
27203 /* If there are more template classes than parameter lists, we have
27206 template <class T> void S<T>::R<T>::f (); */
27207 if (parser
->num_template_parameter_lists
< num_templates
)
27209 if (declarator
&& !current_function_decl
)
27210 error_at (location
, "specializing member %<%T::%E%> "
27211 "requires %<template<>%> syntax",
27212 declarator
->u
.id
.qualifying_scope
,
27213 declarator
->u
.id
.unqualified_name
);
27214 else if (declarator
)
27215 error_at (location
, "invalid declaration of %<%T::%E%>",
27216 declarator
->u
.id
.qualifying_scope
,
27217 declarator
->u
.id
.unqualified_name
);
27219 error_at (location
, "too few template-parameter-lists");
27222 /* Otherwise, there are too many template parameter lists. We have
27225 template <class T> template <class U> void S::f(); */
27226 error_at (location
, "too many template-parameter-lists");
27230 /* Parse an optional `::' token indicating that the following name is
27231 from the global namespace. If so, PARSER->SCOPE is set to the
27232 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27233 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27234 Returns the new value of PARSER->SCOPE, if the `::' token is
27235 present, and NULL_TREE otherwise. */
27238 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
27242 /* Peek at the next token. */
27243 token
= cp_lexer_peek_token (parser
->lexer
);
27244 /* If we're looking at a `::' token then we're starting from the
27245 global namespace, not our current location. */
27246 if (token
->type
== CPP_SCOPE
)
27248 /* Consume the `::' token. */
27249 cp_lexer_consume_token (parser
->lexer
);
27250 /* Set the SCOPE so that we know where to start the lookup. */
27251 parser
->scope
= global_namespace
;
27252 parser
->qualifying_scope
= global_namespace
;
27253 parser
->object_scope
= NULL_TREE
;
27255 return parser
->scope
;
27257 else if (!current_scope_valid_p
)
27259 parser
->scope
= NULL_TREE
;
27260 parser
->qualifying_scope
= NULL_TREE
;
27261 parser
->object_scope
= NULL_TREE
;
27267 /* Returns TRUE if the upcoming token sequence is the start of a
27268 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27269 declarator is preceded by the `friend' specifier. */
27272 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
27274 bool constructor_p
;
27275 bool outside_class_specifier_p
;
27276 tree nested_name_specifier
;
27277 cp_token
*next_token
;
27279 /* The common case is that this is not a constructor declarator, so
27280 try to avoid doing lots of work if at all possible. It's not
27281 valid declare a constructor at function scope. */
27282 if (parser
->in_function_body
)
27284 /* And only certain tokens can begin a constructor declarator. */
27285 next_token
= cp_lexer_peek_token (parser
->lexer
);
27286 if (next_token
->type
!= CPP_NAME
27287 && next_token
->type
!= CPP_SCOPE
27288 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
27289 && next_token
->type
!= CPP_TEMPLATE_ID
)
27292 /* Parse tentatively; we are going to roll back all of the tokens
27294 cp_parser_parse_tentatively (parser
);
27295 /* Assume that we are looking at a constructor declarator. */
27296 constructor_p
= true;
27298 /* Look for the optional `::' operator. */
27299 cp_parser_global_scope_opt (parser
,
27300 /*current_scope_valid_p=*/false);
27301 /* Look for the nested-name-specifier. */
27302 nested_name_specifier
27303 = (cp_parser_nested_name_specifier_opt (parser
,
27304 /*typename_keyword_p=*/false,
27305 /*check_dependency_p=*/false,
27307 /*is_declaration=*/false));
27309 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27310 if (nested_name_specifier
27311 && TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
27313 tree s
= resolve_typename_type (nested_name_specifier
,
27314 /*only_current_p=*/false);
27315 if (TREE_CODE (s
) != TYPENAME_TYPE
)
27316 nested_name_specifier
= s
;
27319 outside_class_specifier_p
= (!at_class_scope_p ()
27320 || !TYPE_BEING_DEFINED (current_class_type
)
27323 /* Outside of a class-specifier, there must be a
27324 nested-name-specifier. Except in C++17 mode, where we
27325 might be declaring a guiding declaration. */
27326 if (!nested_name_specifier
&& outside_class_specifier_p
27327 && cxx_dialect
< cxx17
)
27328 constructor_p
= false;
27329 else if (nested_name_specifier
== error_mark_node
)
27330 constructor_p
= false;
27332 /* If we have a class scope, this is easy; DR 147 says that S::S always
27333 names the constructor, and no other qualified name could. */
27334 if (constructor_p
&& nested_name_specifier
27335 && CLASS_TYPE_P (nested_name_specifier
))
27337 tree id
= cp_parser_unqualified_id (parser
,
27338 /*template_keyword_p=*/false,
27339 /*check_dependency_p=*/false,
27340 /*declarator_p=*/true,
27341 /*optional_p=*/false);
27342 if (is_overloaded_fn (id
))
27343 id
= DECL_NAME (get_first_fn (id
));
27344 if (!constructor_name_p (id
, nested_name_specifier
))
27345 constructor_p
= false;
27347 /* If we still think that this might be a constructor-declarator,
27348 look for a class-name. */
27349 else if (constructor_p
)
27353 template <typename T> struct S {
27357 we must recognize that the nested `S' names a class. */
27358 if (cxx_dialect
>= cxx17
)
27359 cp_parser_parse_tentatively (parser
);
27362 type_decl
= cp_parser_class_name (parser
,
27363 /*typename_keyword_p=*/false,
27364 /*template_keyword_p=*/false,
27366 /*check_dependency_p=*/false,
27367 /*class_head_p=*/false,
27368 /*is_declaration=*/false);
27370 if (cxx_dialect
>= cxx17
27371 && !cp_parser_parse_definitely (parser
))
27373 type_decl
= NULL_TREE
;
27374 tree tmpl
= cp_parser_template_name (parser
,
27375 /*template_keyword*/false,
27376 /*check_dependency_p*/false,
27377 /*is_declaration*/false,
27379 /*is_identifier*/NULL
);
27380 if (DECL_CLASS_TEMPLATE_P (tmpl
)
27381 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
27382 /* It's a deduction guide, return true. */;
27384 cp_parser_simulate_error (parser
);
27387 /* If there was no class-name, then this is not a constructor.
27388 Otherwise, if we are in a class-specifier and we aren't
27389 handling a friend declaration, check that its type matches
27390 current_class_type (c++/38313). Note: error_mark_node
27391 is left alone for error recovery purposes. */
27392 constructor_p
= (!cp_parser_error_occurred (parser
)
27393 && (outside_class_specifier_p
27394 || type_decl
== NULL_TREE
27395 || type_decl
== error_mark_node
27396 || same_type_p (current_class_type
,
27397 TREE_TYPE (type_decl
))));
27399 /* If we're still considering a constructor, we have to see a `(',
27400 to begin the parameter-declaration-clause, followed by either a
27401 `)', an `...', or a decl-specifier. We need to check for a
27402 type-specifier to avoid being fooled into thinking that:
27406 is a constructor. (It is actually a function named `f' that
27407 takes one parameter (of type `int') and returns a value of type
27410 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27411 constructor_p
= false;
27414 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
27415 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
27416 /* A parameter declaration begins with a decl-specifier,
27417 which is either the "attribute" keyword, a storage class
27418 specifier, or (usually) a type-specifier. */
27419 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
27422 tree pushed_scope
= NULL_TREE
;
27423 unsigned saved_num_template_parameter_lists
;
27425 /* Names appearing in the type-specifier should be looked up
27426 in the scope of the class. */
27427 if (current_class_type
)
27429 else if (type_decl
)
27431 type
= TREE_TYPE (type_decl
);
27432 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27434 type
= resolve_typename_type (type
,
27435 /*only_current_p=*/false);
27436 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27438 cp_parser_abort_tentative_parse (parser
);
27442 pushed_scope
= push_scope (type
);
27445 /* Inside the constructor parameter list, surrounding
27446 template-parameter-lists do not apply. */
27447 saved_num_template_parameter_lists
27448 = parser
->num_template_parameter_lists
;
27449 parser
->num_template_parameter_lists
= 0;
27451 /* Look for the type-specifier. */
27452 cp_parser_type_specifier (parser
,
27453 CP_PARSER_FLAGS_NONE
,
27454 /*decl_specs=*/NULL
,
27455 /*is_declarator=*/true,
27456 /*declares_class_or_enum=*/NULL
,
27457 /*is_cv_qualifier=*/NULL
);
27459 parser
->num_template_parameter_lists
27460 = saved_num_template_parameter_lists
;
27462 /* Leave the scope of the class. */
27464 pop_scope (pushed_scope
);
27466 constructor_p
= !cp_parser_error_occurred (parser
);
27470 /* We did not really want to consume any tokens. */
27471 cp_parser_abort_tentative_parse (parser
);
27473 return constructor_p
;
27476 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27477 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27478 they must be performed once we are in the scope of the function.
27480 Returns the function defined. */
27483 cp_parser_function_definition_from_specifiers_and_declarator
27484 (cp_parser
* parser
,
27485 cp_decl_specifier_seq
*decl_specifiers
,
27487 const cp_declarator
*declarator
)
27492 /* Begin the function-definition. */
27493 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
27495 /* The things we're about to see are not directly qualified by any
27496 template headers we've seen thus far. */
27497 reset_specialization ();
27499 /* If there were names looked up in the decl-specifier-seq that we
27500 did not check, check them now. We must wait until we are in the
27501 scope of the function to perform the checks, since the function
27502 might be a friend. */
27503 perform_deferred_access_checks (tf_warning_or_error
);
27507 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
27508 parser
->omp_declare_simd
= NULL
;
27509 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
27510 parser
->oacc_routine
= NULL
;
27515 /* Skip the entire function. */
27516 cp_parser_skip_to_end_of_block_or_statement (parser
);
27517 fn
= error_mark_node
;
27519 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
27521 /* Seen already, skip it. An error message has already been output. */
27522 cp_parser_skip_to_end_of_block_or_statement (parser
);
27523 fn
= current_function_decl
;
27524 current_function_decl
= NULL_TREE
;
27525 /* If this is a function from a class, pop the nested class. */
27526 if (current_class_name
)
27527 pop_nested_class ();
27532 if (DECL_DECLARED_INLINE_P (current_function_decl
))
27533 tv
= TV_PARSE_INLINE
;
27535 tv
= TV_PARSE_FUNC
;
27537 fn
= cp_parser_function_definition_after_declarator (parser
,
27538 /*inline_p=*/false);
27545 /* Parse the part of a function-definition that follows the
27546 declarator. INLINE_P is TRUE iff this function is an inline
27547 function defined within a class-specifier.
27549 Returns the function defined. */
27552 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
27556 bool saved_in_unbraced_linkage_specification_p
;
27557 bool saved_in_function_body
;
27558 unsigned saved_num_template_parameter_lists
;
27560 bool fully_implicit_function_template_p
27561 = parser
->fully_implicit_function_template_p
;
27562 parser
->fully_implicit_function_template_p
= false;
27563 tree implicit_template_parms
27564 = parser
->implicit_template_parms
;
27565 parser
->implicit_template_parms
= 0;
27566 cp_binding_level
* implicit_template_scope
27567 = parser
->implicit_template_scope
;
27568 parser
->implicit_template_scope
= 0;
27570 saved_in_function_body
= parser
->in_function_body
;
27571 parser
->in_function_body
= true;
27572 /* If the next token is `return', then the code may be trying to
27573 make use of the "named return value" extension that G++ used to
27575 token
= cp_lexer_peek_token (parser
->lexer
);
27576 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
27578 /* Consume the `return' keyword. */
27579 cp_lexer_consume_token (parser
->lexer
);
27580 /* Look for the identifier that indicates what value is to be
27582 cp_parser_identifier (parser
);
27583 /* Issue an error message. */
27584 error_at (token
->location
,
27585 "named return values are no longer supported");
27586 /* Skip tokens until we reach the start of the function body. */
27589 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27590 if (token
->type
== CPP_OPEN_BRACE
27591 || token
->type
== CPP_EOF
27592 || token
->type
== CPP_PRAGMA_EOL
)
27594 cp_lexer_consume_token (parser
->lexer
);
27597 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27598 anything declared inside `f'. */
27599 saved_in_unbraced_linkage_specification_p
27600 = parser
->in_unbraced_linkage_specification_p
;
27601 parser
->in_unbraced_linkage_specification_p
= false;
27602 /* Inside the function, surrounding template-parameter-lists do not
27604 saved_num_template_parameter_lists
27605 = parser
->num_template_parameter_lists
;
27606 parser
->num_template_parameter_lists
= 0;
27608 /* If the next token is `try', `__transaction_atomic', or
27609 `__transaction_relaxed`, then we are looking at either function-try-block
27610 or function-transaction-block. Note that all of these include the
27612 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
27613 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
27614 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27615 RID_TRANSACTION_RELAXED
))
27616 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
27617 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27618 cp_parser_function_try_block (parser
);
27620 cp_parser_ctor_initializer_opt_and_function_body
27621 (parser
, /*in_function_try_block=*/false);
27623 /* Finish the function. */
27624 fn
= finish_function (inline_p
);
27625 /* Generate code for it, if necessary. */
27626 expand_or_defer_fn (fn
);
27627 /* Restore the saved values. */
27628 parser
->in_unbraced_linkage_specification_p
27629 = saved_in_unbraced_linkage_specification_p
;
27630 parser
->num_template_parameter_lists
27631 = saved_num_template_parameter_lists
;
27632 parser
->in_function_body
= saved_in_function_body
;
27634 parser
->fully_implicit_function_template_p
27635 = fully_implicit_function_template_p
;
27636 parser
->implicit_template_parms
27637 = implicit_template_parms
;
27638 parser
->implicit_template_scope
27639 = implicit_template_scope
;
27641 if (parser
->fully_implicit_function_template_p
)
27642 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27647 /* Parse a template-declaration body (following argument list). */
27650 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27651 tree parameter_list
,
27654 tree decl
= NULL_TREE
;
27655 bool friend_p
= false;
27657 /* We just processed one more parameter list. */
27658 ++parser
->num_template_parameter_lists
;
27660 /* Get the deferred access checks from the parameter list. These
27661 will be checked once we know what is being declared, as for a
27662 member template the checks must be performed in the scope of the
27663 class containing the member. */
27664 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27666 /* Tentatively parse for a new template parameter list, which can either be
27667 the template keyword or a template introduction. */
27668 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27670 else if (cxx_dialect
>= cxx11
27671 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27672 decl
= cp_parser_alias_declaration (parser
);
27675 /* There are no access checks when parsing a template, as we do not
27676 know if a specialization will be a friend. */
27677 push_deferring_access_checks (dk_no_check
);
27678 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27679 decl
= cp_parser_single_declaration (parser
,
27682 /*explicit_specialization_p=*/false,
27684 pop_deferring_access_checks ();
27686 /* If this is a member template declaration, let the front
27688 if (member_p
&& !friend_p
&& decl
)
27690 if (TREE_CODE (decl
) == TYPE_DECL
)
27691 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27693 decl
= finish_member_template_decl (decl
);
27695 else if (friend_p
&& decl
27696 && DECL_DECLARES_TYPE_P (decl
))
27697 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27698 /*complain=*/true);
27700 /* We are done with the current parameter list. */
27701 --parser
->num_template_parameter_lists
;
27703 pop_deferring_access_checks ();
27706 finish_template_decl (parameter_list
);
27708 /* Check the template arguments for a literal operator template. */
27710 && DECL_DECLARES_FUNCTION_P (decl
)
27711 && UDLIT_OPER_P (DECL_NAME (decl
)))
27714 if (parameter_list
== NULL_TREE
)
27718 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27719 if (num_parms
== 1)
27721 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27722 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27723 if (CLASS_TYPE_P (TREE_TYPE (parm
)))
27724 /* OK, C++20 string literal operator template. We don't need
27725 to warn in lower dialects here because we will have already
27726 warned about the template parameter. */;
27727 else if (TREE_TYPE (parm
) != char_type_node
27728 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27731 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27733 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27734 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27735 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27736 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27737 if (parm
== error_mark_node
27738 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27739 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27742 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27743 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
27744 "ISO C++ did not adopt string literal operator templa"
27745 "tes taking an argument pack of characters");
27752 if (cxx_dialect
> cxx17
)
27753 error ("literal operator template %qD has invalid parameter list;"
27754 " Expected non-type template parameter pack <char...> "
27755 " or single non-type parameter of class type",
27758 error ("literal operator template %qD has invalid parameter list."
27759 " Expected non-type template parameter pack <char...>",
27764 /* Register member declarations. */
27765 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27766 finish_member_declaration (decl
);
27767 /* If DECL is a function template, we must return to parse it later.
27768 (Even though there is no definition, there might be default
27769 arguments that need handling.) */
27770 if (member_p
&& decl
27771 && DECL_DECLARES_FUNCTION_P (decl
))
27772 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27775 /* Parse a template introduction header for a template-declaration. Returns
27776 false if tentative parse fails. */
27779 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27781 cp_parser_parse_tentatively (parser
);
27783 tree saved_scope
= parser
->scope
;
27784 tree saved_object_scope
= parser
->object_scope
;
27785 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27787 /* Look for the optional `::' operator. */
27788 cp_parser_global_scope_opt (parser
,
27789 /*current_scope_valid_p=*/false);
27790 /* Look for the nested-name-specifier. */
27791 cp_parser_nested_name_specifier_opt (parser
,
27792 /*typename_keyword_p=*/false,
27793 /*check_dependency_p=*/true,
27795 /*is_declaration=*/false);
27797 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27798 tree concept_name
= cp_parser_identifier (parser
);
27800 /* Look up the concept for which we will be matching
27801 template parameters. */
27802 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27804 parser
->scope
= saved_scope
;
27805 parser
->object_scope
= saved_object_scope
;
27806 parser
->qualifying_scope
= saved_qualifying_scope
;
27808 if (concept_name
== error_mark_node
)
27809 cp_parser_simulate_error (parser
);
27811 /* Look for opening brace for introduction. */
27812 matching_braces braces
;
27813 braces
.require_open (parser
);
27815 if (!cp_parser_parse_definitely (parser
))
27818 push_deferring_access_checks (dk_deferred
);
27820 /* Build vector of placeholder parameters and grab
27821 matching identifiers. */
27822 tree introduction_list
= cp_parser_introduction_list (parser
);
27824 /* Look for closing brace for introduction. */
27825 if (!braces
.require_close (parser
))
27828 /* The introduction-list shall not be empty. */
27829 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27832 /* In cp_parser_introduction_list we have already issued an error. */
27836 if (tmpl_decl
== error_mark_node
)
27838 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27843 /* Build and associate the constraint. */
27844 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27845 if (parms
&& parms
!= error_mark_node
)
27847 cp_parser_template_declaration_after_parameters (parser
, parms
,
27852 error_at (token
->location
, "no matching concept for template-introduction");
27856 /* Parse a normal template-declaration following the template keyword. */
27859 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27861 tree parameter_list
;
27862 bool need_lang_pop
;
27863 location_t location
= input_location
;
27865 /* Look for the `<' token. */
27866 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27868 if (at_class_scope_p () && current_function_decl
)
27870 /* 14.5.2.2 [temp.mem]
27872 A local class shall not have member templates. */
27873 error_at (location
,
27874 "invalid declaration of member template in local class");
27875 cp_parser_skip_to_end_of_block_or_statement (parser
);
27880 A template ... shall not have C linkage. */
27881 if (current_lang_name
== lang_name_c
)
27883 error_at (location
, "template with C linkage");
27884 maybe_show_extern_c_location ();
27885 /* Give it C++ linkage to avoid confusing other parts of the
27887 push_lang_context (lang_name_cplusplus
);
27888 need_lang_pop
= true;
27891 need_lang_pop
= false;
27893 /* We cannot perform access checks on the template parameter
27894 declarations until we know what is being declared, just as we
27895 cannot check the decl-specifier list. */
27896 push_deferring_access_checks (dk_deferred
);
27898 /* If the next token is `>', then we have an invalid
27899 specialization. Rather than complain about an invalid template
27900 parameter, issue an error message here. */
27901 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27903 cp_parser_error (parser
, "invalid explicit specialization");
27904 begin_specialization ();
27905 parameter_list
= NULL_TREE
;
27909 /* Parse the template parameters. */
27910 parameter_list
= cp_parser_template_parameter_list (parser
);
27913 /* Look for the `>'. */
27914 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27916 /* Manage template requirements */
27919 tree reqs
= get_shorthand_constraints (current_template_parms
);
27920 if (tree r
= cp_parser_requires_clause_opt (parser
))
27921 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27922 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27925 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27928 /* For the erroneous case of a template with C linkage, we pushed an
27929 implicit C++ linkage scope; exit that scope now. */
27931 pop_lang_context ();
27934 /* Parse a template-declaration, assuming that the `export' (and
27935 `extern') keywords, if present, has already been scanned. MEMBER_P
27936 is as for cp_parser_template_declaration. */
27939 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27941 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27943 cp_lexer_consume_token (parser
->lexer
);
27944 cp_parser_explicit_template_declaration (parser
, member_p
);
27947 else if (flag_concepts
)
27948 return cp_parser_template_introduction (parser
, member_p
);
27953 /* Perform the deferred access checks from a template-parameter-list.
27954 CHECKS is a TREE_LIST of access checks, as returned by
27955 get_deferred_access_checks. */
27958 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27960 ++processing_template_parmlist
;
27961 perform_access_checks (checks
, tf_warning_or_error
);
27962 --processing_template_parmlist
;
27965 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27966 `function-definition' sequence that follows a template header.
27967 If MEMBER_P is true, this declaration appears in a class scope.
27969 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27970 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27973 cp_parser_single_declaration (cp_parser
* parser
,
27974 vec
<deferred_access_check
, va_gc
> *checks
,
27976 bool explicit_specialization_p
,
27979 int declares_class_or_enum
;
27980 tree decl
= NULL_TREE
;
27981 cp_decl_specifier_seq decl_specifiers
;
27982 bool function_definition_p
= false;
27983 cp_token
*decl_spec_token_start
;
27985 /* This function is only used when processing a template
27987 gcc_assert (innermost_scope_kind () == sk_template_parms
27988 || innermost_scope_kind () == sk_template_spec
);
27990 /* Defer access checks until we know what is being declared. */
27991 push_deferring_access_checks (dk_deferred
);
27993 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27995 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27996 cp_parser_decl_specifier_seq (parser
,
27997 (CP_PARSER_FLAGS_OPTIONAL
27998 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
28000 &declares_class_or_enum
);
28002 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
28004 /* There are no template typedefs. */
28005 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
28007 error_at (decl_spec_token_start
->location
,
28008 "template declaration of %<typedef%>");
28009 decl
= error_mark_node
;
28012 /* Gather up the access checks that occurred the
28013 decl-specifier-seq. */
28014 stop_deferring_access_checks ();
28016 /* Check for the declaration of a template class. */
28017 if (declares_class_or_enum
)
28019 if (cp_parser_declares_only_class_p (parser
)
28020 || (declares_class_or_enum
& 2))
28022 // If this is a declaration, but not a definition, associate
28023 // any constraints with the type declaration. Constraints
28024 // are associated with definitions in cp_parser_class_specifier.
28025 if (declares_class_or_enum
== 1)
28026 associate_classtype_constraints (decl_specifiers
.type
);
28028 decl
= shadow_tag (&decl_specifiers
);
28033 friend template <typename T> struct A<T>::B;
28036 A<T>::B will be represented by a TYPENAME_TYPE, and
28037 therefore not recognized by shadow_tag. */
28038 if (friend_p
&& *friend_p
28040 && decl_specifiers
.type
28041 && TYPE_P (decl_specifiers
.type
))
28042 decl
= decl_specifiers
.type
;
28044 if (decl
&& decl
!= error_mark_node
)
28045 decl
= TYPE_NAME (decl
);
28047 decl
= error_mark_node
;
28049 /* Perform access checks for template parameters. */
28050 cp_parser_perform_template_parameter_access_checks (checks
);
28052 /* Give a helpful diagnostic for
28053 template <class T> struct A { } a;
28054 if we aren't already recovering from an error. */
28055 if (!cp_parser_declares_only_class_p (parser
)
28058 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
28059 "a class template declaration must not declare "
28061 cp_parser_skip_to_end_of_block_or_statement (parser
);
28067 /* Complain about missing 'typename' or other invalid type names. */
28068 if (!decl_specifiers
.any_type_specifiers_p
28069 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
28071 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28072 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28073 the rest of this declaration. */
28074 decl
= error_mark_node
;
28078 /* If it's not a template class, try for a template function. If
28079 the next token is a `;', then this declaration does not declare
28080 anything. But, if there were errors in the decl-specifiers, then
28081 the error might well have come from an attempted class-specifier.
28082 In that case, there's no need to warn about a missing declarator. */
28084 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
28085 || decl_specifiers
.type
!= error_mark_node
))
28087 decl
= cp_parser_init_declarator (parser
,
28088 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
28091 /*function_definition_allowed_p=*/true,
28093 declares_class_or_enum
,
28094 &function_definition_p
,
28097 /* 7.1.1-1 [dcl.stc]
28099 A storage-class-specifier shall not be specified in an explicit
28100 specialization... */
28102 && explicit_specialization_p
28103 && decl_specifiers
.storage_class
!= sc_none
)
28105 error_at (decl_spec_token_start
->location
,
28106 "explicit template specialization cannot have a storage class");
28107 decl
= error_mark_node
;
28110 if (decl
&& VAR_P (decl
))
28111 check_template_variable (decl
);
28114 /* Look for a trailing `;' after the declaration. */
28115 if (!function_definition_p
28116 && (decl
== error_mark_node
28117 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
28118 cp_parser_skip_to_end_of_block_or_statement (parser
);
28121 pop_deferring_access_checks ();
28123 /* Clear any current qualification; whatever comes next is the start
28124 of something new. */
28125 parser
->scope
= NULL_TREE
;
28126 parser
->qualifying_scope
= NULL_TREE
;
28127 parser
->object_scope
= NULL_TREE
;
28132 /* Parse a cast-expression that is not the operand of a unary "&". */
28135 cp_parser_simple_cast_expression (cp_parser
*parser
)
28137 return cp_parser_cast_expression (parser
, /*address_p=*/false,
28138 /*cast_p=*/false, /*decltype*/false, NULL
);
28141 /* Parse a functional cast to TYPE. Returns an expression
28142 representing the cast. */
28145 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
28147 vec
<tree
, va_gc
> *vec
;
28148 tree expression_list
;
28152 location_t start_loc
= input_location
;
28155 type
= error_mark_node
;
28157 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28159 cp_lexer_set_source_position (parser
->lexer
);
28160 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28161 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
28162 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
28163 if (TREE_CODE (type
) == TYPE_DECL
)
28164 type
= TREE_TYPE (type
);
28166 cast
= finish_compound_literal (type
, expression_list
,
28167 tf_warning_or_error
, fcl_functional
);
28168 /* Create a location of the form:
28171 with caret == start at the start of the type name,
28172 finishing at the closing brace. */
28173 location_t finish_loc
28174 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28175 location_t combined_loc
= make_location (start_loc
, start_loc
,
28177 cast
.set_location (combined_loc
);
28182 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
28184 /*allow_expansion_p=*/true,
28185 /*non_constant_p=*/NULL
);
28187 expression_list
= error_mark_node
;
28190 expression_list
= build_tree_list_vec (vec
);
28191 release_tree_vector (vec
);
28194 cast
= build_functional_cast (type
, expression_list
,
28195 tf_warning_or_error
);
28196 /* [expr.const]/1: In an integral constant expression "only type
28197 conversions to integral or enumeration type can be used". */
28198 if (TREE_CODE (type
) == TYPE_DECL
)
28199 type
= TREE_TYPE (type
);
28200 if (cast
!= error_mark_node
28201 && !cast_valid_in_integral_constant_expression_p (type
)
28202 && cp_parser_non_integral_constant_expression (parser
,
28204 return error_mark_node
;
28206 /* Create a location of the form:
28209 with caret == start at the start of the type name,
28210 finishing at the closing paren. */
28211 location_t finish_loc
28212 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28213 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
28214 cast
.set_location (combined_loc
);
28218 /* Save the tokens that make up the body of a member function defined
28219 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28220 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28221 specifiers applied to the declaration. Returns the FUNCTION_DECL
28222 for the member function. */
28225 cp_parser_save_member_function_body (cp_parser
* parser
,
28226 cp_decl_specifier_seq
*decl_specifiers
,
28227 cp_declarator
*declarator
,
28233 bool function_try_block
= false;
28235 /* Create the FUNCTION_DECL. */
28236 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
28237 cp_finalize_omp_declare_simd (parser
, fn
);
28238 cp_finalize_oacc_routine (parser
, fn
, true);
28239 /* If something went badly wrong, bail out now. */
28240 if (fn
== error_mark_node
)
28242 /* If there's a function-body, skip it. */
28243 if (cp_parser_token_starts_function_definition_p
28244 (cp_lexer_peek_token (parser
->lexer
)))
28245 cp_parser_skip_to_end_of_block_or_statement (parser
);
28246 return error_mark_node
;
28249 /* Remember it, if there default args to post process. */
28250 cp_parser_save_default_args (parser
, fn
);
28252 /* Save away the tokens that make up the body of the
28254 first
= parser
->lexer
->next_token
;
28256 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
28257 cp_lexer_consume_token (parser
->lexer
);
28258 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
28259 RID_TRANSACTION_ATOMIC
))
28261 cp_lexer_consume_token (parser
->lexer
);
28262 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28263 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
28264 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
28265 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
28266 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
28267 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
28268 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
28270 cp_lexer_consume_token (parser
->lexer
);
28271 cp_lexer_consume_token (parser
->lexer
);
28272 cp_lexer_consume_token (parser
->lexer
);
28273 cp_lexer_consume_token (parser
->lexer
);
28274 cp_lexer_consume_token (parser
->lexer
);
28277 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
28278 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
28280 cp_lexer_consume_token (parser
->lexer
);
28281 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28286 /* Handle function try blocks. */
28287 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
28289 cp_lexer_consume_token (parser
->lexer
);
28290 function_try_block
= true;
28292 /* We can have braced-init-list mem-initializers before the fn body. */
28293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
28295 cp_lexer_consume_token (parser
->lexer
);
28296 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
28298 /* cache_group will stop after an un-nested { } pair, too. */
28299 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28302 /* variadic mem-inits have ... after the ')'. */
28303 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28304 cp_lexer_consume_token (parser
->lexer
);
28307 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28308 /* Handle function try blocks. */
28309 if (function_try_block
)
28310 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
28311 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28312 last
= parser
->lexer
->next_token
;
28314 /* Save away the inline definition; we will process it when the
28315 class is complete. */
28316 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
28317 DECL_PENDING_INLINE_P (fn
) = 1;
28319 /* We need to know that this was defined in the class, so that
28320 friend templates are handled correctly. */
28321 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
28323 /* Add FN to the queue of functions to be parsed later. */
28324 vec_safe_push (unparsed_funs_with_definitions
, fn
);
28329 /* Save the tokens that make up the in-class initializer for a non-static
28330 data member. Returns a DEFAULT_ARG. */
28333 cp_parser_save_nsdmi (cp_parser
* parser
)
28335 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
28338 /* Parse a template-argument-list, as well as the trailing ">" (but
28339 not the opening "<"). See cp_parser_template_argument_list for the
28343 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
28347 tree saved_qualifying_scope
;
28348 tree saved_object_scope
;
28349 bool saved_greater_than_is_operator_p
;
28353 When parsing a template-id, the first non-nested `>' is taken as
28354 the end of the template-argument-list rather than a greater-than
28356 saved_greater_than_is_operator_p
28357 = parser
->greater_than_is_operator_p
;
28358 parser
->greater_than_is_operator_p
= false;
28359 /* Parsing the argument list may modify SCOPE, so we save it
28361 saved_scope
= parser
->scope
;
28362 saved_qualifying_scope
= parser
->qualifying_scope
;
28363 saved_object_scope
= parser
->object_scope
;
28364 /* We need to evaluate the template arguments, even though this
28365 template-id may be nested within a "sizeof". */
28367 /* Parse the template-argument-list itself. */
28368 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
28369 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28370 arguments
= NULL_TREE
;
28372 arguments
= cp_parser_template_argument_list (parser
);
28373 /* Look for the `>' that ends the template-argument-list. If we find
28374 a '>>' instead, it's probably just a typo. */
28375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28377 if (cxx_dialect
!= cxx98
)
28379 /* In C++0x, a `>>' in a template argument list or cast
28380 expression is considered to be two separate `>'
28381 tokens. So, change the current token to a `>', but don't
28382 consume it: it will be consumed later when the outer
28383 template argument list (or cast expression) is parsed.
28384 Note that this replacement of `>' for `>>' is necessary
28385 even if we are parsing tentatively: in the tentative
28386 case, after calling
28387 cp_parser_enclosed_template_argument_list we will always
28388 throw away all of the template arguments and the first
28389 closing `>', either because the template argument list
28390 was erroneous or because we are replacing those tokens
28391 with a CPP_TEMPLATE_ID token. The second `>' (which will
28392 not have been thrown away) is needed either to close an
28393 outer template argument list or to complete a new-style
28395 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28396 token
->type
= CPP_GREATER
;
28398 else if (!saved_greater_than_is_operator_p
)
28400 /* If we're in a nested template argument list, the '>>' has
28401 to be a typo for '> >'. We emit the error message, but we
28402 continue parsing and we push a '>' as next token, so that
28403 the argument list will be parsed correctly. Note that the
28404 global source location is still on the token before the
28405 '>>', so we need to say explicitly where we want it. */
28406 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28407 gcc_rich_location
richloc (token
->location
);
28408 richloc
.add_fixit_replace ("> >");
28409 error_at (&richloc
, "%<>>%> should be %<> >%> "
28410 "within a nested template argument list");
28412 token
->type
= CPP_GREATER
;
28416 /* If this is not a nested template argument list, the '>>'
28417 is a typo for '>'. Emit an error message and continue.
28418 Same deal about the token location, but here we can get it
28419 right by consuming the '>>' before issuing the diagnostic. */
28420 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
28421 error_at (token
->location
,
28422 "spurious %<>>%>, use %<>%> to terminate "
28423 "a template argument list");
28427 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28428 /* The `>' token might be a greater-than operator again now. */
28429 parser
->greater_than_is_operator_p
28430 = saved_greater_than_is_operator_p
;
28431 /* Restore the SAVED_SCOPE. */
28432 parser
->scope
= saved_scope
;
28433 parser
->qualifying_scope
= saved_qualifying_scope
;
28434 parser
->object_scope
= saved_object_scope
;
28439 /* MEMBER_FUNCTION is a member function, or a friend. If default
28440 arguments, or the body of the function have not yet been parsed,
28444 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
28446 timevar_push (TV_PARSE_INMETH
);
28447 /* If this member is a template, get the underlying
28449 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
28450 member_function
= DECL_TEMPLATE_RESULT (member_function
);
28452 /* There should not be any class definitions in progress at this
28453 point; the bodies of members are only parsed outside of all class
28455 gcc_assert (parser
->num_classes_being_defined
== 0);
28456 /* While we're parsing the member functions we might encounter more
28457 classes. We want to handle them right away, but we don't want
28458 them getting mixed up with functions that are currently in the
28460 push_unparsed_function_queues (parser
);
28462 /* Make sure that any template parameters are in scope. */
28463 maybe_begin_member_template_processing (member_function
);
28465 /* If the body of the function has not yet been parsed, parse it
28467 if (DECL_PENDING_INLINE_P (member_function
))
28469 tree function_scope
;
28470 cp_token_cache
*tokens
;
28472 /* The function is no longer pending; we are processing it. */
28473 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
28474 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
28475 DECL_PENDING_INLINE_P (member_function
) = 0;
28477 /* If this is a local class, enter the scope of the containing
28479 function_scope
= current_function_decl
;
28480 if (function_scope
)
28481 push_function_context ();
28483 /* Push the body of the function onto the lexer stack. */
28484 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28486 /* Let the front end know that we going to be defining this
28488 start_preparsed_function (member_function
, NULL_TREE
,
28489 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
28491 /* Don't do access checking if it is a templated function. */
28492 if (processing_template_decl
)
28493 push_deferring_access_checks (dk_no_check
);
28495 /* #pragma omp declare reduction needs special parsing. */
28496 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
28498 parser
->lexer
->in_pragma
= true;
28499 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
28500 finish_function (/*inline_p=*/true);
28501 cp_check_omp_declare_reduction (member_function
);
28504 /* Now, parse the body of the function. */
28505 cp_parser_function_definition_after_declarator (parser
,
28506 /*inline_p=*/true);
28508 if (processing_template_decl
)
28509 pop_deferring_access_checks ();
28511 /* Leave the scope of the containing function. */
28512 if (function_scope
)
28513 pop_function_context ();
28514 cp_parser_pop_lexer (parser
);
28517 /* Remove any template parameters from the symbol table. */
28518 maybe_end_member_template_processing ();
28520 /* Restore the queue. */
28521 pop_unparsed_function_queues (parser
);
28522 timevar_pop (TV_PARSE_INMETH
);
28525 /* If DECL contains any default args, remember it on the unparsed
28526 functions queue. */
28529 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
28533 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
28535 probe
= TREE_CHAIN (probe
))
28536 if (TREE_PURPOSE (probe
))
28538 cp_default_arg_entry entry
= {current_class_type
, decl
};
28539 vec_safe_push (unparsed_funs_with_default_args
, entry
);
28544 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28545 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28546 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28547 from the parameter-type-list. */
28550 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
28551 tree default_arg
, tree parmtype
)
28553 cp_token_cache
*tokens
;
28557 if (default_arg
== error_mark_node
)
28558 return error_mark_node
;
28560 /* Push the saved tokens for the default argument onto the parser's
28562 tokens
= DEFARG_TOKENS (default_arg
);
28563 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28565 start_lambda_scope (decl
);
28567 /* Parse the default argument. */
28568 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
28569 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
28570 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28572 finish_lambda_scope ();
28574 if (parsed_arg
== error_mark_node
)
28575 cp_parser_skip_to_end_of_statement (parser
);
28577 if (!processing_template_decl
)
28579 /* In a non-template class, check conversions now. In a template,
28580 we'll wait and instantiate these as needed. */
28581 if (TREE_CODE (decl
) == PARM_DECL
)
28582 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
28583 tf_warning_or_error
);
28584 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
28585 parsed_arg
= error_mark_node
;
28587 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
28590 /* If the token stream has not been completely used up, then
28591 there was extra junk after the end of the default
28593 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
28595 if (TREE_CODE (decl
) == PARM_DECL
)
28596 cp_parser_error (parser
, "expected %<,%>");
28598 cp_parser_error (parser
, "expected %<;%>");
28601 /* Revert to the main lexer. */
28602 cp_parser_pop_lexer (parser
);
28607 /* FIELD is a non-static data member with an initializer which we saved for
28608 later; parse it now. */
28611 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
28615 maybe_begin_member_template_processing (field
);
28617 push_unparsed_function_queues (parser
);
28618 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
28619 DECL_INITIAL (field
),
28621 pop_unparsed_function_queues (parser
);
28623 maybe_end_member_template_processing ();
28625 DECL_INITIAL (field
) = def
;
28628 /* FN is a FUNCTION_DECL which may contains a parameter with an
28629 unparsed DEFAULT_ARG. Parse the default args now. This function
28630 assumes that the current scope is the scope in which the default
28631 argument should be processed. */
28634 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
28636 unsigned char saved_local_variables_forbidden_p
;
28637 tree parm
, parmdecl
;
28639 /* While we're parsing the default args, we might (due to the
28640 statement expression extension) encounter more classes. We want
28641 to handle them right away, but we don't want them getting mixed
28642 up with default args that are currently in the queue. */
28643 push_unparsed_function_queues (parser
);
28645 /* Local variable names (and the `this' keyword) may not appear
28646 in a default argument. */
28647 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28648 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
28650 push_defarg_context (fn
);
28652 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28653 parmdecl
= DECL_ARGUMENTS (fn
);
28654 parm
&& parm
!= void_list_node
;
28655 parm
= TREE_CHAIN (parm
),
28656 parmdecl
= DECL_CHAIN (parmdecl
))
28658 tree default_arg
= TREE_PURPOSE (parm
);
28660 vec
<tree
, va_gc
> *insts
;
28667 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28668 /* This can happen for a friend declaration for a function
28669 already declared with default arguments. */
28673 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28675 TREE_VALUE (parm
));
28676 TREE_PURPOSE (parm
) = parsed_arg
;
28678 /* Update any instantiations we've already created. */
28679 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28680 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28681 TREE_PURPOSE (copy
) = parsed_arg
;
28684 pop_defarg_context ();
28686 /* Make sure no default arg is missing. */
28687 check_default_args (fn
);
28689 /* Restore the state of local_variables_forbidden_p. */
28690 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28692 /* Restore the queue. */
28693 pop_unparsed_function_queues (parser
);
28696 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28698 sizeof ... ( identifier )
28700 where the 'sizeof' token has already been consumed. */
28703 cp_parser_sizeof_pack (cp_parser
*parser
)
28705 /* Consume the `...'. */
28706 cp_lexer_consume_token (parser
->lexer
);
28707 maybe_warn_variadic_templates ();
28709 matching_parens parens
;
28710 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28712 parens
.consume_open (parser
);
28714 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28715 "%<sizeof...%> argument must be surrounded by parentheses");
28717 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28718 tree name
= cp_parser_identifier (parser
);
28719 if (name
== error_mark_node
)
28720 return error_mark_node
;
28721 /* The name is not qualified. */
28722 parser
->scope
= NULL_TREE
;
28723 parser
->qualifying_scope
= NULL_TREE
;
28724 parser
->object_scope
= NULL_TREE
;
28725 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28726 if (expr
== error_mark_node
)
28727 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28729 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28730 expr
= TREE_TYPE (expr
);
28731 else if (TREE_CODE (expr
) == CONST_DECL
)
28732 expr
= DECL_INITIAL (expr
);
28733 expr
= make_pack_expansion (expr
);
28734 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28737 parens
.require_close (parser
);
28742 /* Parse the operand of `sizeof' (or a similar operator). Returns
28743 either a TYPE or an expression, depending on the form of the
28744 input. The KEYWORD indicates which kind of expression we have
28748 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28750 tree expr
= NULL_TREE
;
28751 const char *saved_message
;
28753 bool saved_integral_constant_expression_p
;
28754 bool saved_non_integral_constant_expression_p
;
28756 /* If it's a `...', then we are computing the length of a parameter
28758 if (keyword
== RID_SIZEOF
28759 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28760 return cp_parser_sizeof_pack (parser
);
28762 /* Types cannot be defined in a `sizeof' expression. Save away the
28764 saved_message
= parser
->type_definition_forbidden_message
;
28765 /* And create the new one. */
28766 tmp
= concat ("types may not be defined in %<",
28767 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28768 "%> expressions", NULL
);
28769 parser
->type_definition_forbidden_message
= tmp
;
28771 /* The restrictions on constant-expressions do not apply inside
28772 sizeof expressions. */
28773 saved_integral_constant_expression_p
28774 = parser
->integral_constant_expression_p
;
28775 saved_non_integral_constant_expression_p
28776 = parser
->non_integral_constant_expression_p
;
28777 parser
->integral_constant_expression_p
= false;
28779 /* Do not actually evaluate the expression. */
28780 ++cp_unevaluated_operand
;
28781 ++c_inhibit_evaluation_warnings
;
28782 /* If it's a `(', then we might be looking at the type-id
28784 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28786 tree type
= NULL_TREE
;
28788 /* We can't be sure yet whether we're looking at a type-id or an
28790 cp_parser_parse_tentatively (parser
);
28792 matching_parens parens
;
28793 parens
.consume_open (parser
);
28795 /* Note: as a GNU Extension, compound literals are considered
28796 postfix-expressions as they are in C99, so they are valid
28797 arguments to sizeof. See comment in cp_parser_cast_expression
28799 if (cp_parser_compound_literal_p (parser
))
28800 cp_parser_simulate_error (parser
);
28803 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28804 parser
->in_type_id_in_expr_p
= true;
28805 /* Look for the type-id. */
28806 type
= cp_parser_type_id (parser
);
28807 /* Look for the closing `)'. */
28808 parens
.require_close (parser
);
28809 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28812 /* If all went well, then we're done. */
28813 if (cp_parser_parse_definitely (parser
))
28817 /* If the type-id production did not work out, then we must be
28818 looking at the unary-expression production. */
28820 expr
= cp_parser_unary_expression (parser
);
28822 /* Go back to evaluating expressions. */
28823 --cp_unevaluated_operand
;
28824 --c_inhibit_evaluation_warnings
;
28826 /* Free the message we created. */
28828 /* And restore the old one. */
28829 parser
->type_definition_forbidden_message
= saved_message
;
28830 parser
->integral_constant_expression_p
28831 = saved_integral_constant_expression_p
;
28832 parser
->non_integral_constant_expression_p
28833 = saved_non_integral_constant_expression_p
;
28838 /* If the current declaration has no declarator, return true. */
28841 cp_parser_declares_only_class_p (cp_parser
*parser
)
28843 /* If the next token is a `;' or a `,' then there is no
28845 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28846 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28849 /* Update the DECL_SPECS to reflect the storage class indicated by
28853 cp_parser_set_storage_class (cp_parser
*parser
,
28854 cp_decl_specifier_seq
*decl_specs
,
28858 cp_storage_class storage_class
;
28860 if (parser
->in_unbraced_linkage_specification_p
)
28862 error_at (token
->location
, "invalid use of %qD in linkage specification",
28863 ridpointers
[keyword
]);
28866 else if (decl_specs
->storage_class
!= sc_none
)
28868 decl_specs
->conflicting_specifiers_p
= true;
28872 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28873 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28874 && decl_specs
->gnu_thread_keyword_p
)
28876 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28877 "%<__thread%> before %qD", ridpointers
[keyword
]);
28883 storage_class
= sc_auto
;
28886 storage_class
= sc_register
;
28889 storage_class
= sc_static
;
28892 storage_class
= sc_extern
;
28895 storage_class
= sc_mutable
;
28898 gcc_unreachable ();
28900 decl_specs
->storage_class
= storage_class
;
28901 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28903 /* A storage class specifier cannot be applied alongside a typedef
28904 specifier. If there is a typedef specifier present then set
28905 conflicting_specifiers_p which will trigger an error later
28906 on in grokdeclarator. */
28907 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28908 decl_specs
->conflicting_specifiers_p
= true;
28911 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28912 is true, the type is a class or enum definition. */
28915 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28918 bool type_definition_p
)
28920 decl_specs
->any_specifiers_p
= true;
28922 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28923 (with, for example, in "typedef int wchar_t;") we remember that
28924 this is what happened. In system headers, we ignore these
28925 declarations so that G++ can work with system headers that are not
28927 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28928 && !type_definition_p
28929 && (type_spec
== boolean_type_node
28930 || type_spec
== char16_type_node
28931 || type_spec
== char32_type_node
28932 || type_spec
== wchar_type_node
)
28933 && (decl_specs
->type
28934 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28935 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28936 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28937 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28939 decl_specs
->redefined_builtin_type
= type_spec
;
28940 set_and_check_decl_spec_loc (decl_specs
,
28941 ds_redefined_builtin_type_spec
,
28943 if (!decl_specs
->type
)
28945 decl_specs
->type
= type_spec
;
28946 decl_specs
->type_definition_p
= false;
28947 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28950 else if (decl_specs
->type
)
28951 decl_specs
->multiple_types_p
= true;
28954 decl_specs
->type
= type_spec
;
28955 decl_specs
->type_definition_p
= type_definition_p
;
28956 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28957 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28961 /* True iff TOKEN is the GNU keyword __thread. */
28964 token_is__thread (cp_token
*token
)
28966 gcc_assert (token
->keyword
== RID_THREAD
);
28967 return id_equal (token
->u
.value
, "__thread");
28970 /* Set the location for a declarator specifier and check if it is
28973 DECL_SPECS is the sequence of declarator specifiers onto which to
28976 DS is the single declarator specifier to set which location is to
28977 be set onto the existing sequence of declarators.
28979 LOCATION is the location for the declarator specifier to
28983 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28984 cp_decl_spec ds
, cp_token
*token
)
28986 gcc_assert (ds
< ds_last
);
28988 if (decl_specs
== NULL
)
28991 location_t location
= token
->location
;
28993 if (decl_specs
->locations
[ds
] == 0)
28995 decl_specs
->locations
[ds
] = location
;
28996 if (ds
== ds_thread
)
28997 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
29003 if (decl_specs
->locations
[ds_long_long
] != 0)
29004 error_at (location
,
29005 "%<long long long%> is too long for GCC");
29008 decl_specs
->locations
[ds_long_long
] = location
;
29009 pedwarn_cxx98 (location
,
29011 "ISO C++ 1998 does not support %<long long%>");
29014 else if (ds
== ds_thread
)
29016 bool gnu
= token_is__thread (token
);
29017 gcc_rich_location
richloc (location
);
29018 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
29020 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
29021 error_at (&richloc
,
29022 "both %<__thread%> and %<thread_local%> specified");
29026 richloc
.add_fixit_remove ();
29027 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
29032 static const char *const decl_spec_names
[] = {
29049 gcc_rich_location
richloc (location
);
29050 richloc
.add_fixit_remove ();
29051 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
29056 /* Return true iff the declarator specifier DS is present in the
29057 sequence of declarator specifiers DECL_SPECS. */
29060 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
29063 gcc_assert (ds
< ds_last
);
29065 if (decl_specs
== NULL
)
29068 return decl_specs
->locations
[ds
] != 0;
29071 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29072 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29075 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
29077 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
29080 /* Issue an error message indicating that TOKEN_DESC was expected.
29081 If KEYWORD is true, it indicated this function is called by
29082 cp_parser_require_keword and the required token can only be
29083 a indicated keyword.
29085 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29086 within any error as the location of an "opening" token matching
29087 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29088 RT_CLOSE_PAREN). */
29091 cp_parser_required_error (cp_parser
*parser
,
29092 required_token token_desc
,
29094 location_t matching_location
)
29096 if (cp_parser_simulate_error (parser
))
29099 const char *gmsgid
= NULL
;
29100 switch (token_desc
)
29103 gmsgid
= G_("expected %<new%>");
29106 gmsgid
= G_("expected %<delete%>");
29109 gmsgid
= G_("expected %<return%>");
29112 gmsgid
= G_("expected %<while%>");
29115 gmsgid
= G_("expected %<extern%>");
29117 case RT_STATIC_ASSERT
:
29118 gmsgid
= G_("expected %<static_assert%>");
29121 gmsgid
= G_("expected %<decltype%>");
29124 gmsgid
= G_("expected %<operator%>");
29127 gmsgid
= G_("expected %<class%>");
29130 gmsgid
= G_("expected %<template%>");
29133 gmsgid
= G_("expected %<namespace%>");
29136 gmsgid
= G_("expected %<using%>");
29139 gmsgid
= G_("expected %<asm%>");
29142 gmsgid
= G_("expected %<try%>");
29145 gmsgid
= G_("expected %<catch%>");
29148 gmsgid
= G_("expected %<throw%>");
29151 gmsgid
= G_("expected %<__label__%>");
29154 gmsgid
= G_("expected %<@try%>");
29156 case RT_AT_SYNCHRONIZED
:
29157 gmsgid
= G_("expected %<@synchronized%>");
29160 gmsgid
= G_("expected %<@throw%>");
29162 case RT_TRANSACTION_ATOMIC
:
29163 gmsgid
= G_("expected %<__transaction_atomic%>");
29165 case RT_TRANSACTION_RELAXED
:
29166 gmsgid
= G_("expected %<__transaction_relaxed%>");
29172 if (!gmsgid
&& !keyword
)
29174 switch (token_desc
)
29177 gmsgid
= G_("expected %<;%>");
29179 case RT_OPEN_PAREN
:
29180 gmsgid
= G_("expected %<(%>");
29182 case RT_CLOSE_BRACE
:
29183 gmsgid
= G_("expected %<}%>");
29185 case RT_OPEN_BRACE
:
29186 gmsgid
= G_("expected %<{%>");
29188 case RT_CLOSE_SQUARE
:
29189 gmsgid
= G_("expected %<]%>");
29191 case RT_OPEN_SQUARE
:
29192 gmsgid
= G_("expected %<[%>");
29195 gmsgid
= G_("expected %<,%>");
29198 gmsgid
= G_("expected %<::%>");
29201 gmsgid
= G_("expected %<<%>");
29204 gmsgid
= G_("expected %<>%>");
29207 gmsgid
= G_("expected %<=%>");
29210 gmsgid
= G_("expected %<...%>");
29213 gmsgid
= G_("expected %<*%>");
29216 gmsgid
= G_("expected %<~%>");
29219 gmsgid
= G_("expected %<:%>");
29221 case RT_COLON_SCOPE
:
29222 gmsgid
= G_("expected %<:%> or %<::%>");
29224 case RT_CLOSE_PAREN
:
29225 gmsgid
= G_("expected %<)%>");
29227 case RT_COMMA_CLOSE_PAREN
:
29228 gmsgid
= G_("expected %<,%> or %<)%>");
29230 case RT_PRAGMA_EOL
:
29231 gmsgid
= G_("expected end of line");
29234 gmsgid
= G_("expected identifier");
29237 gmsgid
= G_("expected selection-statement");
29240 gmsgid
= G_("expected iteration-statement");
29243 gmsgid
= G_("expected jump-statement");
29246 gmsgid
= G_("expected class-key");
29248 case RT_CLASS_TYPENAME_TEMPLATE
:
29249 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
29252 gcc_unreachable ();
29257 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
29261 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29262 issue an error message indicating that TOKEN_DESC was expected.
29264 Returns the token consumed, if the token had the appropriate type.
29265 Otherwise, returns NULL.
29267 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29268 within any error as the location of an "opening" token matching
29269 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29270 RT_CLOSE_PAREN). */
29273 cp_parser_require (cp_parser
* parser
,
29274 enum cpp_ttype type
,
29275 required_token token_desc
,
29276 location_t matching_location
)
29278 if (cp_lexer_next_token_is (parser
->lexer
, type
))
29279 return cp_lexer_consume_token (parser
->lexer
);
29282 /* Output the MESSAGE -- unless we're parsing tentatively. */
29283 if (!cp_parser_simulate_error (parser
))
29284 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
29285 matching_location
);
29290 /* An error message is produced if the next token is not '>'.
29291 All further tokens are skipped until the desired token is
29292 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29295 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
29297 /* Current level of '< ... >'. */
29298 unsigned level
= 0;
29299 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29300 unsigned nesting_depth
= 0;
29302 /* Are we ready, yet? If not, issue error message. */
29303 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
29306 /* Skip tokens until the desired token is found. */
29309 /* Peek at the next token. */
29310 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29313 if (!nesting_depth
)
29318 if (cxx_dialect
== cxx98
)
29319 /* C++0x views the `>>' operator as two `>' tokens, but
29322 else if (!nesting_depth
&& level
-- == 0)
29324 /* We've hit a `>>' where the first `>' closes the
29325 template argument list, and the second `>' is
29326 spurious. Just consume the `>>' and stop; we've
29327 already produced at least one error. */
29328 cp_lexer_consume_token (parser
->lexer
);
29331 /* Fall through for C++0x, so we handle the second `>' in
29333 gcc_fallthrough ();
29336 if (!nesting_depth
&& level
-- == 0)
29338 /* We've reached the token we want, consume it and stop. */
29339 cp_lexer_consume_token (parser
->lexer
);
29344 case CPP_OPEN_PAREN
:
29345 case CPP_OPEN_SQUARE
:
29349 case CPP_CLOSE_PAREN
:
29350 case CPP_CLOSE_SQUARE
:
29351 if (nesting_depth
-- == 0)
29356 case CPP_PRAGMA_EOL
:
29357 case CPP_SEMICOLON
:
29358 case CPP_OPEN_BRACE
:
29359 case CPP_CLOSE_BRACE
:
29360 /* The '>' was probably forgotten, don't look further. */
29367 /* Consume this token. */
29368 cp_lexer_consume_token (parser
->lexer
);
29372 /* If the next token is the indicated keyword, consume it. Otherwise,
29373 issue an error message indicating that TOKEN_DESC was expected.
29375 Returns the token consumed, if the token had the appropriate type.
29376 Otherwise, returns NULL. */
29379 cp_parser_require_keyword (cp_parser
* parser
,
29381 required_token token_desc
)
29383 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
29385 if (token
&& token
->keyword
!= keyword
)
29387 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
29395 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29396 function-definition. */
29399 cp_parser_token_starts_function_definition_p (cp_token
* token
)
29401 return (/* An ordinary function-body begins with an `{'. */
29402 token
->type
== CPP_OPEN_BRACE
29403 /* A ctor-initializer begins with a `:'. */
29404 || token
->type
== CPP_COLON
29405 /* A function-try-block begins with `try'. */
29406 || token
->keyword
== RID_TRY
29407 /* A function-transaction-block begins with `__transaction_atomic'
29408 or `__transaction_relaxed'. */
29409 || token
->keyword
== RID_TRANSACTION_ATOMIC
29410 || token
->keyword
== RID_TRANSACTION_RELAXED
29411 /* The named return value extension begins with `return'. */
29412 || token
->keyword
== RID_RETURN
);
29415 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29419 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
29423 token
= cp_lexer_peek_token (parser
->lexer
);
29424 return (token
->type
== CPP_OPEN_BRACE
29425 || (token
->type
== CPP_COLON
29426 && !parser
->colon_doesnt_start_class_def_p
));
29429 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29430 C++0x) ending a template-argument. */
29433 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
29437 token
= cp_lexer_peek_token (parser
->lexer
);
29438 return (token
->type
== CPP_COMMA
29439 || token
->type
== CPP_GREATER
29440 || token
->type
== CPP_ELLIPSIS
29441 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
29444 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29445 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29448 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
29453 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
29454 if (token
->type
== CPP_LESS
)
29456 /* Check for the sequence `<::' in the original code. It would be lexed as
29457 `[:', where `[' is a digraph, and there is no whitespace before
29459 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
29462 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
29463 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
29469 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29470 or none_type otherwise. */
29472 static enum tag_types
29473 cp_parser_token_is_class_key (cp_token
* token
)
29475 switch (token
->keyword
)
29480 return record_type
;
29489 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29490 or none_type otherwise or if the token is null. */
29492 static enum tag_types
29493 cp_parser_token_is_type_parameter_key (cp_token
* token
)
29498 switch (token
->keyword
)
29503 return typename_type
;
29510 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29513 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
29515 if (type
== error_mark_node
)
29517 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
29519 if (permerror (input_location
, "%qs tag used in naming %q#T",
29520 class_key
== union_type
? "union"
29521 : class_key
== record_type
? "struct" : "class",
29523 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
29524 "%q#T was previously declared here", type
);
29528 /* Issue an error message if DECL is redeclared with different
29529 access than its original declaration [class.access.spec/3].
29530 This applies to nested classes, nested class templates and
29531 enumerations [class.mem/1]. */
29534 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
29537 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
29538 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
29541 if ((TREE_PRIVATE (decl
)
29542 != (current_access_specifier
== access_private_node
))
29543 || (TREE_PROTECTED (decl
)
29544 != (current_access_specifier
== access_protected_node
)))
29545 error_at (location
, "%qD redeclared with different access", decl
);
29548 /* Look for the `template' keyword, as a syntactic disambiguator.
29549 Return TRUE iff it is present, in which case it will be
29553 cp_parser_optional_template_keyword (cp_parser
*parser
)
29555 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29557 /* In C++98 the `template' keyword can only be used within templates;
29558 outside templates the parser can always figure out what is a
29559 template and what is not. In C++11, per the resolution of DR 468,
29560 `template' is allowed in cases where it is not strictly necessary. */
29561 if (!processing_template_decl
29562 && pedantic
&& cxx_dialect
== cxx98
)
29564 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29565 pedwarn (token
->location
, OPT_Wpedantic
,
29566 "in C++98 %<template%> (as a disambiguator) is only "
29567 "allowed within templates");
29568 /* If this part of the token stream is rescanned, the same
29569 error message would be generated. So, we purge the token
29570 from the stream. */
29571 cp_lexer_purge_token (parser
->lexer
);
29576 /* Consume the `template' keyword. */
29577 cp_lexer_consume_token (parser
->lexer
);
29584 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29585 set PARSER->SCOPE, and perform other related actions. */
29588 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
29590 struct tree_check
*check_value
;
29592 /* Get the stored value. */
29593 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
29594 /* Set the scope from the stored value. */
29595 parser
->scope
= saved_checks_value (check_value
);
29596 parser
->qualifying_scope
= check_value
->qualifying_scope
;
29597 parser
->object_scope
= NULL_TREE
;
29600 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29601 encounter the end of a block before what we were looking for. */
29604 cp_parser_cache_group (cp_parser
*parser
,
29605 enum cpp_ttype end
,
29610 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29612 /* Abort a parenthesized expression if we encounter a semicolon. */
29613 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
29614 && token
->type
== CPP_SEMICOLON
)
29616 /* If we've reached the end of the file, stop. */
29617 if (token
->type
== CPP_EOF
29618 || (end
!= CPP_PRAGMA_EOL
29619 && token
->type
== CPP_PRAGMA_EOL
))
29621 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
29622 /* We've hit the end of an enclosing block, so there's been some
29623 kind of syntax error. */
29626 /* Consume the token. */
29627 cp_lexer_consume_token (parser
->lexer
);
29628 /* See if it starts a new group. */
29629 if (token
->type
== CPP_OPEN_BRACE
)
29631 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29632 /* In theory this should probably check end == '}', but
29633 cp_parser_save_member_function_body needs it to exit
29634 after either '}' or ')' when called with ')'. */
29638 else if (token
->type
== CPP_OPEN_PAREN
)
29640 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29641 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29644 else if (token
->type
== CPP_PRAGMA
)
29645 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29646 else if (token
->type
== end
)
29651 /* Like above, for caching a default argument or NSDMI. Both of these are
29652 terminated by a non-nested comma, but it can be unclear whether or not a
29653 comma is nested in a template argument list unless we do more parsing.
29654 In order to handle this ambiguity, when we encounter a ',' after a '<'
29655 we try to parse what follows as a parameter-declaration-list (in the
29656 case of a default argument) or a member-declarator (in the case of an
29657 NSDMI). If that succeeds, then we stop caching. */
29660 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29662 unsigned depth
= 0;
29663 int maybe_template_id
= 0;
29664 cp_token
*first_token
;
29666 tree default_argument
;
29668 /* Add tokens until we have processed the entire default
29669 argument. We add the range [first_token, token). */
29670 first_token
= cp_lexer_peek_token (parser
->lexer
);
29671 if (first_token
->type
== CPP_OPEN_BRACE
)
29673 /* For list-initialization, this is straightforward. */
29674 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29675 token
= cp_lexer_peek_token (parser
->lexer
);
29681 /* Peek at the next token. */
29682 token
= cp_lexer_peek_token (parser
->lexer
);
29683 /* What we do depends on what token we have. */
29684 switch (token
->type
)
29686 /* In valid code, a default argument must be
29687 immediately followed by a `,' `)', or `...'. */
29689 if (depth
== 0 && maybe_template_id
)
29691 /* If we've seen a '<', we might be in a
29692 template-argument-list. Until Core issue 325 is
29693 resolved, we don't know how this situation ought
29694 to be handled, so try to DTRT. We check whether
29695 what comes after the comma is a valid parameter
29696 declaration list. If it is, then the comma ends
29697 the default argument; otherwise the default
29698 argument continues. */
29699 bool error
= false;
29702 /* Set ITALP so cp_parser_parameter_declaration_list
29703 doesn't decide to commit to this parse. */
29704 bool saved_italp
= parser
->in_template_argument_list_p
;
29705 parser
->in_template_argument_list_p
= true;
29707 cp_parser_parse_tentatively (parser
);
29711 /* Parse declarators until we reach a non-comma or
29712 somthing that cannot be an initializer.
29713 Just checking whether we're looking at a single
29714 declarator is insufficient. Consider:
29715 int var = tuple<T,U>::x;
29716 The template parameter 'U' looks exactly like a
29720 int ctor_dtor_or_conv_p
;
29721 cp_lexer_consume_token (parser
->lexer
);
29722 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29723 CP_PARSER_FLAGS_NONE
,
29724 &ctor_dtor_or_conv_p
,
29725 /*parenthesized_p=*/NULL
,
29727 /*friend_p=*/false,
29728 /*static_p=*/false);
29729 peek
= cp_lexer_peek_token (parser
->lexer
);
29730 if (cp_parser_error_occurred (parser
))
29733 while (peek
->type
== CPP_COMMA
);
29734 /* If we met an '=' or ';' then the original comma
29735 was the end of the NSDMI. Otherwise assume
29736 we're still in the NSDMI. */
29737 error
= (peek
->type
!= CPP_EQ
29738 && peek
->type
!= CPP_SEMICOLON
);
29742 cp_lexer_consume_token (parser
->lexer
);
29743 begin_scope (sk_function_parms
, NULL_TREE
);
29744 tree t
= cp_parser_parameter_declaration_list
29745 (parser
, CP_PARSER_FLAGS_NONE
);
29746 if (t
== error_mark_node
)
29748 pop_bindings_and_leave_scope ();
29750 if (!cp_parser_error_occurred (parser
) && !error
)
29752 cp_parser_abort_tentative_parse (parser
);
29754 parser
->in_template_argument_list_p
= saved_italp
;
29758 case CPP_CLOSE_PAREN
:
29760 /* If we run into a non-nested `;', `}', or `]',
29761 then the code is invalid -- but the default
29762 argument is certainly over. */
29763 case CPP_SEMICOLON
:
29764 case CPP_CLOSE_BRACE
:
29765 case CPP_CLOSE_SQUARE
:
29767 /* Handle correctly int n = sizeof ... ( p ); */
29768 && token
->type
!= CPP_ELLIPSIS
)
29770 /* Update DEPTH, if necessary. */
29771 else if (token
->type
== CPP_CLOSE_PAREN
29772 || token
->type
== CPP_CLOSE_BRACE
29773 || token
->type
== CPP_CLOSE_SQUARE
)
29777 case CPP_OPEN_PAREN
:
29778 case CPP_OPEN_SQUARE
:
29779 case CPP_OPEN_BRACE
:
29785 /* This might be the comparison operator, or it might
29786 start a template argument list. */
29787 ++maybe_template_id
;
29791 if (cxx_dialect
== cxx98
)
29793 /* Fall through for C++0x, which treats the `>>'
29794 operator like two `>' tokens in certain
29796 gcc_fallthrough ();
29801 /* This might be an operator, or it might close a
29802 template argument list. But if a previous '<'
29803 started a template argument list, this will have
29804 closed it, so we can't be in one anymore. */
29805 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29806 if (maybe_template_id
< 0)
29807 maybe_template_id
= 0;
29811 /* If we run out of tokens, issue an error message. */
29813 case CPP_PRAGMA_EOL
:
29814 error_at (token
->location
, "file ends in default argument");
29815 return error_mark_node
;
29819 /* In these cases, we should look for template-ids.
29820 For example, if the default argument is
29821 `X<int, double>()', we need to do name lookup to
29822 figure out whether or not `X' is a template; if
29823 so, the `,' does not end the default argument.
29825 That is not yet done. */
29832 /* If we've reached the end, stop. */
29836 /* Add the token to the token block. */
29837 token
= cp_lexer_consume_token (parser
->lexer
);
29840 /* Create a DEFAULT_ARG to represent the unparsed default
29842 default_argument
= make_node (DEFAULT_ARG
);
29843 DEFARG_TOKENS (default_argument
)
29844 = cp_token_cache_new (first_token
, token
);
29845 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29847 return default_argument
;
29850 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29853 defarg_location (tree default_argument
)
29855 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29856 location_t start
= tokens
->first
->location
;
29857 location_t end
= tokens
->last
->location
;
29858 return make_location (start
, start
, end
);
29861 /* Begin parsing tentatively. We always save tokens while parsing
29862 tentatively so that if the tentative parsing fails we can restore the
29866 cp_parser_parse_tentatively (cp_parser
* parser
)
29868 /* Enter a new parsing context. */
29869 parser
->context
= cp_parser_context_new (parser
->context
);
29870 /* Begin saving tokens. */
29871 cp_lexer_save_tokens (parser
->lexer
);
29872 /* In order to avoid repetitive access control error messages,
29873 access checks are queued up until we are no longer parsing
29875 push_deferring_access_checks (dk_deferred
);
29878 /* Commit to the currently active tentative parse. */
29881 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29883 cp_parser_context
*context
;
29886 /* Mark all of the levels as committed. */
29887 lexer
= parser
->lexer
;
29888 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29890 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29892 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29893 while (!cp_lexer_saving_tokens (lexer
))
29894 lexer
= lexer
->next
;
29895 cp_lexer_commit_tokens (lexer
);
29899 /* Commit to the topmost currently active tentative parse.
29901 Note that this function shouldn't be called when there are
29902 irreversible side-effects while in a tentative state. For
29903 example, we shouldn't create a permanent entry in the symbol
29904 table, or issue an error message that might not apply if the
29905 tentative parse is aborted. */
29908 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29910 cp_parser_context
*context
= parser
->context
;
29911 cp_lexer
*lexer
= parser
->lexer
;
29915 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29917 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29919 while (!cp_lexer_saving_tokens (lexer
))
29920 lexer
= lexer
->next
;
29921 cp_lexer_commit_tokens (lexer
);
29925 /* Abort the currently active tentative parse. All consumed tokens
29926 will be rolled back, and no diagnostics will be issued. */
29929 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29931 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29932 || errorcount
> 0);
29933 cp_parser_simulate_error (parser
);
29934 /* Now, pretend that we want to see if the construct was
29935 successfully parsed. */
29936 cp_parser_parse_definitely (parser
);
29939 /* Stop parsing tentatively. If a parse error has occurred, restore the
29940 token stream. Otherwise, commit to the tokens we have consumed.
29941 Returns true if no error occurred; false otherwise. */
29944 cp_parser_parse_definitely (cp_parser
* parser
)
29946 bool error_occurred
;
29947 cp_parser_context
*context
;
29949 /* Remember whether or not an error occurred, since we are about to
29950 destroy that information. */
29951 error_occurred
= cp_parser_error_occurred (parser
);
29952 /* Remove the topmost context from the stack. */
29953 context
= parser
->context
;
29954 parser
->context
= context
->next
;
29955 /* If no parse errors occurred, commit to the tentative parse. */
29956 if (!error_occurred
)
29958 /* Commit to the tokens read tentatively, unless that was
29960 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29961 cp_lexer_commit_tokens (parser
->lexer
);
29963 pop_to_parent_deferring_access_checks ();
29965 /* Otherwise, if errors occurred, roll back our state so that things
29966 are just as they were before we began the tentative parse. */
29969 cp_lexer_rollback_tokens (parser
->lexer
);
29970 pop_deferring_access_checks ();
29972 /* Add the context to the front of the free list. */
29973 context
->next
= cp_parser_context_free_list
;
29974 cp_parser_context_free_list
= context
;
29976 return !error_occurred
;
29979 /* Returns true if we are parsing tentatively and are not committed to
29980 this tentative parse. */
29983 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29985 return (cp_parser_parsing_tentatively (parser
)
29986 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29989 /* Returns nonzero iff an error has occurred during the most recent
29990 tentative parse. */
29993 cp_parser_error_occurred (cp_parser
* parser
)
29995 return (cp_parser_parsing_tentatively (parser
)
29996 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29999 /* Returns nonzero if GNU extensions are allowed. */
30002 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
30004 return parser
->allow_gnu_extensions_p
;
30007 /* Objective-C++ Productions */
30010 /* Parse an Objective-C expression, which feeds into a primary-expression
30014 objc-message-expression
30015 objc-string-literal
30016 objc-encode-expression
30017 objc-protocol-expression
30018 objc-selector-expression
30020 Returns a tree representation of the expression. */
30023 cp_parser_objc_expression (cp_parser
* parser
)
30025 /* Try to figure out what kind of declaration is present. */
30026 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30030 case CPP_OPEN_SQUARE
:
30031 return cp_parser_objc_message_expression (parser
);
30033 case CPP_OBJC_STRING
:
30034 kwd
= cp_lexer_consume_token (parser
->lexer
);
30035 return objc_build_string_object (kwd
->u
.value
);
30038 switch (kwd
->keyword
)
30040 case RID_AT_ENCODE
:
30041 return cp_parser_objc_encode_expression (parser
);
30043 case RID_AT_PROTOCOL
:
30044 return cp_parser_objc_protocol_expression (parser
);
30046 case RID_AT_SELECTOR
:
30047 return cp_parser_objc_selector_expression (parser
);
30054 error_at (kwd
->location
,
30055 "misplaced %<@%D%> Objective-C++ construct",
30057 cp_parser_skip_to_end_of_block_or_statement (parser
);
30060 return error_mark_node
;
30063 /* Parse an Objective-C message expression.
30065 objc-message-expression:
30066 [ objc-message-receiver objc-message-args ]
30068 Returns a representation of an Objective-C message. */
30071 cp_parser_objc_message_expression (cp_parser
* parser
)
30073 tree receiver
, messageargs
;
30075 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30076 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
30077 receiver
= cp_parser_objc_message_receiver (parser
);
30078 messageargs
= cp_parser_objc_message_args (parser
);
30079 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30080 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
30082 tree result
= objc_build_message_expr (receiver
, messageargs
);
30084 /* Construct a location e.g.
30087 ranging from the '[' to the ']', with the caret at the start. */
30088 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
30089 protected_set_expr_location (result
, combined_loc
);
30094 /* Parse an objc-message-receiver.
30096 objc-message-receiver:
30098 simple-type-specifier
30100 Returns a representation of the type or expression. */
30103 cp_parser_objc_message_receiver (cp_parser
* parser
)
30107 /* An Objective-C message receiver may be either (1) a type
30108 or (2) an expression. */
30109 cp_parser_parse_tentatively (parser
);
30110 rcv
= cp_parser_expression (parser
);
30112 /* If that worked out, fine. */
30113 if (cp_parser_parse_definitely (parser
))
30116 cp_parser_parse_tentatively (parser
);
30117 rcv
= cp_parser_simple_type_specifier (parser
,
30118 /*decl_specs=*/NULL
,
30119 CP_PARSER_FLAGS_NONE
);
30121 if (cp_parser_parse_definitely (parser
))
30122 return objc_get_class_reference (rcv
);
30124 cp_parser_error (parser
, "objective-c++ message receiver expected");
30125 return error_mark_node
;
30128 /* Parse the arguments and selectors comprising an Objective-C message.
30133 objc-selector-args , objc-comma-args
30135 objc-selector-args:
30136 objc-selector [opt] : assignment-expression
30137 objc-selector-args objc-selector [opt] : assignment-expression
30140 assignment-expression
30141 objc-comma-args , assignment-expression
30143 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30144 selector arguments and TREE_VALUE containing a list of comma
30148 cp_parser_objc_message_args (cp_parser
* parser
)
30150 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
30151 bool maybe_unary_selector_p
= true;
30152 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30154 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30156 tree selector
= NULL_TREE
, arg
;
30158 if (token
->type
!= CPP_COLON
)
30159 selector
= cp_parser_objc_selector (parser
);
30161 /* Detect if we have a unary selector. */
30162 if (maybe_unary_selector_p
30163 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30164 return build_tree_list (selector
, NULL_TREE
);
30166 maybe_unary_selector_p
= false;
30167 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30168 arg
= cp_parser_assignment_expression (parser
);
30171 = chainon (sel_args
,
30172 build_tree_list (selector
, arg
));
30174 token
= cp_lexer_peek_token (parser
->lexer
);
30177 /* Handle non-selector arguments, if any. */
30178 while (token
->type
== CPP_COMMA
)
30182 cp_lexer_consume_token (parser
->lexer
);
30183 arg
= cp_parser_assignment_expression (parser
);
30186 = chainon (addl_args
,
30187 build_tree_list (NULL_TREE
, arg
));
30189 token
= cp_lexer_peek_token (parser
->lexer
);
30192 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
30194 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
30195 return build_tree_list (error_mark_node
, error_mark_node
);
30198 return build_tree_list (sel_args
, addl_args
);
30201 /* Parse an Objective-C encode expression.
30203 objc-encode-expression:
30204 @encode objc-typename
30206 Returns an encoded representation of the type argument. */
30209 cp_parser_objc_encode_expression (cp_parser
* parser
)
30213 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30215 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
30216 matching_parens parens
;
30217 parens
.require_open (parser
);
30218 token
= cp_lexer_peek_token (parser
->lexer
);
30219 type
= complete_type (cp_parser_type_id (parser
));
30220 parens
.require_close (parser
);
30224 error_at (token
->location
,
30225 "%<@encode%> must specify a type as an argument");
30226 return error_mark_node
;
30229 /* This happens if we find @encode(T) (where T is a template
30230 typename or something dependent on a template typename) when
30231 parsing a template. In that case, we can't compile it
30232 immediately, but we rather create an AT_ENCODE_EXPR which will
30233 need to be instantiated when the template is used.
30235 if (dependent_type_p (type
))
30237 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
30238 TREE_READONLY (value
) = 1;
30243 /* Build a location of the form:
30246 with caret==start at the @ token, finishing at the close paren. */
30247 location_t combined_loc
30248 = make_location (start_loc
, start_loc
,
30249 cp_lexer_previous_token (parser
->lexer
)->location
);
30251 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
30254 /* Parse an Objective-C @defs expression. */
30257 cp_parser_objc_defs_expression (cp_parser
*parser
)
30261 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
30262 matching_parens parens
;
30263 parens
.require_open (parser
);
30264 name
= cp_parser_identifier (parser
);
30265 parens
.require_close (parser
);
30267 return objc_get_class_ivars (name
);
30270 /* Parse an Objective-C protocol expression.
30272 objc-protocol-expression:
30273 @protocol ( identifier )
30275 Returns a representation of the protocol expression. */
30278 cp_parser_objc_protocol_expression (cp_parser
* parser
)
30281 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30283 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30284 matching_parens parens
;
30285 parens
.require_open (parser
);
30286 proto
= cp_parser_identifier (parser
);
30287 parens
.require_close (parser
);
30289 /* Build a location of the form:
30292 with caret==start at the @ token, finishing at the close paren. */
30293 location_t combined_loc
30294 = make_location (start_loc
, start_loc
,
30295 cp_lexer_previous_token (parser
->lexer
)->location
);
30296 tree result
= objc_build_protocol_expr (proto
);
30297 protected_set_expr_location (result
, combined_loc
);
30301 /* Parse an Objective-C selector expression.
30303 objc-selector-expression:
30304 @selector ( objc-method-signature )
30306 objc-method-signature:
30312 objc-selector-seq objc-selector :
30314 Returns a representation of the method selector. */
30317 cp_parser_objc_selector_expression (cp_parser
* parser
)
30319 tree sel_seq
= NULL_TREE
;
30320 bool maybe_unary_selector_p
= true;
30322 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30324 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
30325 matching_parens parens
;
30326 parens
.require_open (parser
);
30327 token
= cp_lexer_peek_token (parser
->lexer
);
30329 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
30330 || token
->type
== CPP_SCOPE
)
30332 tree selector
= NULL_TREE
;
30334 if (token
->type
!= CPP_COLON
30335 || token
->type
== CPP_SCOPE
)
30336 selector
= cp_parser_objc_selector (parser
);
30338 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
30339 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
30341 /* Detect if we have a unary selector. */
30342 if (maybe_unary_selector_p
)
30344 sel_seq
= selector
;
30345 goto finish_selector
;
30349 cp_parser_error (parser
, "expected %<:%>");
30352 maybe_unary_selector_p
= false;
30353 token
= cp_lexer_consume_token (parser
->lexer
);
30355 if (token
->type
== CPP_SCOPE
)
30358 = chainon (sel_seq
,
30359 build_tree_list (selector
, NULL_TREE
));
30361 = chainon (sel_seq
,
30362 build_tree_list (NULL_TREE
, NULL_TREE
));
30366 = chainon (sel_seq
,
30367 build_tree_list (selector
, NULL_TREE
));
30369 token
= cp_lexer_peek_token (parser
->lexer
);
30373 parens
.require_close (parser
);
30376 /* Build a location of the form:
30379 with caret==start at the @ token, finishing at the close paren. */
30380 location_t combined_loc
30381 = make_location (loc
, loc
,
30382 cp_lexer_previous_token (parser
->lexer
)->location
);
30383 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
30384 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30385 protected_set_expr_location (result
, combined_loc
);
30389 /* Parse a list of identifiers.
30391 objc-identifier-list:
30393 objc-identifier-list , identifier
30395 Returns a TREE_LIST of identifier nodes. */
30398 cp_parser_objc_identifier_list (cp_parser
* parser
)
30404 identifier
= cp_parser_identifier (parser
);
30405 if (identifier
== error_mark_node
)
30406 return error_mark_node
;
30408 list
= build_tree_list (NULL_TREE
, identifier
);
30409 sep
= cp_lexer_peek_token (parser
->lexer
);
30411 while (sep
->type
== CPP_COMMA
)
30413 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30414 identifier
= cp_parser_identifier (parser
);
30415 if (identifier
== error_mark_node
)
30418 list
= chainon (list
, build_tree_list (NULL_TREE
,
30420 sep
= cp_lexer_peek_token (parser
->lexer
);
30426 /* Parse an Objective-C alias declaration.
30428 objc-alias-declaration:
30429 @compatibility_alias identifier identifier ;
30431 This function registers the alias mapping with the Objective-C front end.
30432 It returns nothing. */
30435 cp_parser_objc_alias_declaration (cp_parser
* parser
)
30439 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
30440 alias
= cp_parser_identifier (parser
);
30441 orig
= cp_parser_identifier (parser
);
30442 objc_declare_alias (alias
, orig
);
30443 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30446 /* Parse an Objective-C class forward-declaration.
30448 objc-class-declaration:
30449 @class objc-identifier-list ;
30451 The function registers the forward declarations with the Objective-C
30452 front end. It returns nothing. */
30455 cp_parser_objc_class_declaration (cp_parser
* parser
)
30457 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
30462 id
= cp_parser_identifier (parser
);
30463 if (id
== error_mark_node
)
30466 objc_declare_class (id
);
30468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30469 cp_lexer_consume_token (parser
->lexer
);
30473 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30476 /* Parse a list of Objective-C protocol references.
30478 objc-protocol-refs-opt:
30479 objc-protocol-refs [opt]
30481 objc-protocol-refs:
30482 < objc-identifier-list >
30484 Returns a TREE_LIST of identifiers, if any. */
30487 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
30489 tree protorefs
= NULL_TREE
;
30491 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
30493 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
30494 protorefs
= cp_parser_objc_identifier_list (parser
);
30495 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
30501 /* Parse a Objective-C visibility specification. */
30504 cp_parser_objc_visibility_spec (cp_parser
* parser
)
30506 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
30508 switch (vis
->keyword
)
30510 case RID_AT_PRIVATE
:
30511 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
30513 case RID_AT_PROTECTED
:
30514 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
30516 case RID_AT_PUBLIC
:
30517 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
30519 case RID_AT_PACKAGE
:
30520 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
30526 /* Eat '@private'/'@protected'/'@public'. */
30527 cp_lexer_consume_token (parser
->lexer
);
30530 /* Parse an Objective-C method type. Return 'true' if it is a class
30531 (+) method, and 'false' if it is an instance (-) method. */
30534 cp_parser_objc_method_type (cp_parser
* parser
)
30536 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
30542 /* Parse an Objective-C protocol qualifier. */
30545 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
30547 tree quals
= NULL_TREE
, node
;
30548 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30550 node
= token
->u
.value
;
30552 while (node
&& identifier_p (node
)
30553 && (node
== ridpointers
[(int) RID_IN
]
30554 || node
== ridpointers
[(int) RID_OUT
]
30555 || node
== ridpointers
[(int) RID_INOUT
]
30556 || node
== ridpointers
[(int) RID_BYCOPY
]
30557 || node
== ridpointers
[(int) RID_BYREF
]
30558 || node
== ridpointers
[(int) RID_ONEWAY
]))
30560 quals
= tree_cons (NULL_TREE
, node
, quals
);
30561 cp_lexer_consume_token (parser
->lexer
);
30562 token
= cp_lexer_peek_token (parser
->lexer
);
30563 node
= token
->u
.value
;
30569 /* Parse an Objective-C typename. */
30572 cp_parser_objc_typename (cp_parser
* parser
)
30574 tree type_name
= NULL_TREE
;
30576 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30578 tree proto_quals
, cp_type
= NULL_TREE
;
30580 matching_parens parens
;
30581 parens
.consume_open (parser
); /* Eat '('. */
30582 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
30584 /* An ObjC type name may consist of just protocol qualifiers, in which
30585 case the type shall default to 'id'. */
30586 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30588 cp_type
= cp_parser_type_id (parser
);
30590 /* If the type could not be parsed, an error has already
30591 been produced. For error recovery, behave as if it had
30592 not been specified, which will use the default type
30594 if (cp_type
== error_mark_node
)
30596 cp_type
= NULL_TREE
;
30597 /* We need to skip to the closing parenthesis as
30598 cp_parser_type_id() does not seem to do it for
30600 cp_parser_skip_to_closing_parenthesis (parser
,
30601 /*recovering=*/true,
30602 /*or_comma=*/false,
30603 /*consume_paren=*/false);
30607 parens
.require_close (parser
);
30608 type_name
= build_tree_list (proto_quals
, cp_type
);
30614 /* Check to see if TYPE refers to an Objective-C selector name. */
30617 cp_parser_objc_selector_p (enum cpp_ttype type
)
30619 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
30620 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
30621 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
30622 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
30623 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
30626 /* Parse an Objective-C selector. */
30629 cp_parser_objc_selector (cp_parser
* parser
)
30631 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30633 if (!cp_parser_objc_selector_p (token
->type
))
30635 error_at (token
->location
, "invalid Objective-C++ selector name");
30636 return error_mark_node
;
30639 /* C++ operator names are allowed to appear in ObjC selectors. */
30640 switch (token
->type
)
30642 case CPP_AND_AND
: return get_identifier ("and");
30643 case CPP_AND_EQ
: return get_identifier ("and_eq");
30644 case CPP_AND
: return get_identifier ("bitand");
30645 case CPP_OR
: return get_identifier ("bitor");
30646 case CPP_COMPL
: return get_identifier ("compl");
30647 case CPP_NOT
: return get_identifier ("not");
30648 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30649 case CPP_OR_OR
: return get_identifier ("or");
30650 case CPP_OR_EQ
: return get_identifier ("or_eq");
30651 case CPP_XOR
: return get_identifier ("xor");
30652 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30653 default: return token
->u
.value
;
30657 /* Parse an Objective-C params list. */
30660 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30662 tree params
= NULL_TREE
;
30663 bool maybe_unary_selector_p
= true;
30664 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30666 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30668 tree selector
= NULL_TREE
, type_name
, identifier
;
30669 tree parm_attr
= NULL_TREE
;
30671 if (token
->keyword
== RID_ATTRIBUTE
)
30674 if (token
->type
!= CPP_COLON
)
30675 selector
= cp_parser_objc_selector (parser
);
30677 /* Detect if we have a unary selector. */
30678 if (maybe_unary_selector_p
30679 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30681 params
= selector
; /* Might be followed by attributes. */
30685 maybe_unary_selector_p
= false;
30686 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30688 /* Something went quite wrong. There should be a colon
30689 here, but there is not. Stop parsing parameters. */
30692 type_name
= cp_parser_objc_typename (parser
);
30693 /* New ObjC allows attributes on parameters too. */
30694 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30695 parm_attr
= cp_parser_attributes_opt (parser
);
30696 identifier
= cp_parser_identifier (parser
);
30700 objc_build_keyword_decl (selector
,
30705 token
= cp_lexer_peek_token (parser
->lexer
);
30708 if (params
== NULL_TREE
)
30710 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30711 return error_mark_node
;
30714 /* We allow tail attributes for the method. */
30715 if (token
->keyword
== RID_ATTRIBUTE
)
30717 *attributes
= cp_parser_attributes_opt (parser
);
30718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30719 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30721 cp_parser_error (parser
,
30722 "method attributes must be specified at the end");
30723 return error_mark_node
;
30726 if (params
== NULL_TREE
)
30728 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30729 return error_mark_node
;
30734 /* Parse the non-keyword Objective-C params. */
30737 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30740 tree params
= make_node (TREE_LIST
);
30741 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30742 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30744 while (token
->type
== CPP_COMMA
)
30746 cp_parameter_declarator
*parmdecl
;
30749 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30750 token
= cp_lexer_peek_token (parser
->lexer
);
30752 if (token
->type
== CPP_ELLIPSIS
)
30754 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30756 token
= cp_lexer_peek_token (parser
->lexer
);
30760 /* TODO: parse attributes for tail parameters. */
30761 parmdecl
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
30763 parm
= grokdeclarator (parmdecl
->declarator
,
30764 &parmdecl
->decl_specifiers
,
30765 PARM
, /*initialized=*/0,
30766 /*attrlist=*/NULL
);
30768 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30769 token
= cp_lexer_peek_token (parser
->lexer
);
30772 /* We allow tail attributes for the method. */
30773 if (token
->keyword
== RID_ATTRIBUTE
)
30775 if (*attributes
== NULL_TREE
)
30777 *attributes
= cp_parser_attributes_opt (parser
);
30778 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30779 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30783 /* We have an error, but parse the attributes, so that we can
30785 *attributes
= cp_parser_attributes_opt (parser
);
30787 cp_parser_error (parser
,
30788 "method attributes must be specified at the end");
30789 return error_mark_node
;
30795 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30798 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30800 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30802 /* If the next token is `extern' and the following token is a string
30803 literal, then we have a linkage specification. */
30804 if (token
->keyword
== RID_EXTERN
30805 && cp_parser_is_pure_string_literal
30806 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30807 cp_parser_linkage_specification (parser
);
30808 /* Handle #pragma, if any. */
30809 else if (token
->type
== CPP_PRAGMA
)
30810 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30811 /* Allow stray semicolons. */
30812 else if (token
->type
== CPP_SEMICOLON
)
30813 cp_lexer_consume_token (parser
->lexer
);
30814 /* Mark methods as optional or required, when building protocols. */
30815 else if (token
->keyword
== RID_AT_OPTIONAL
)
30817 cp_lexer_consume_token (parser
->lexer
);
30818 objc_set_method_opt (true);
30820 else if (token
->keyword
== RID_AT_REQUIRED
)
30822 cp_lexer_consume_token (parser
->lexer
);
30823 objc_set_method_opt (false);
30825 else if (token
->keyword
== RID_NAMESPACE
)
30826 cp_parser_namespace_definition (parser
);
30827 /* Other stray characters must generate errors. */
30828 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30830 cp_lexer_consume_token (parser
->lexer
);
30831 error ("stray %qs between Objective-C++ methods",
30832 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30834 /* Finally, try to parse a block-declaration, or a function-definition. */
30836 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30839 /* Parse a method signature. */
30842 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30844 tree rettype
, kwdparms
, optparms
;
30845 bool ellipsis
= false;
30846 bool is_class_method
;
30848 is_class_method
= cp_parser_objc_method_type (parser
);
30849 rettype
= cp_parser_objc_typename (parser
);
30850 *attributes
= NULL_TREE
;
30851 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30852 if (kwdparms
== error_mark_node
)
30853 return error_mark_node
;
30854 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30855 if (optparms
== error_mark_node
)
30856 return error_mark_node
;
30858 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30862 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30865 cp_lexer_save_tokens (parser
->lexer
);
30866 tattr
= cp_parser_attributes_opt (parser
);
30867 gcc_assert (tattr
) ;
30869 /* If the attributes are followed by a method introducer, this is not allowed.
30870 Dump the attributes and flag the situation. */
30871 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30872 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30875 /* Otherwise, the attributes introduce some interstitial code, possibly so
30876 rewind to allow that check. */
30877 cp_lexer_rollback_tokens (parser
->lexer
);
30881 /* Parse an Objective-C method prototype list. */
30884 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30886 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30888 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30890 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30892 tree attributes
, sig
;
30893 bool is_class_method
;
30894 if (token
->type
== CPP_PLUS
)
30895 is_class_method
= true;
30897 is_class_method
= false;
30898 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30899 if (sig
== error_mark_node
)
30901 cp_parser_skip_to_end_of_block_or_statement (parser
);
30902 token
= cp_lexer_peek_token (parser
->lexer
);
30905 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30906 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30908 else if (token
->keyword
== RID_AT_PROPERTY
)
30909 cp_parser_objc_at_property_declaration (parser
);
30910 else if (token
->keyword
== RID_ATTRIBUTE
30911 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30912 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30914 "prefix attributes are ignored for methods");
30916 /* Allow for interspersed non-ObjC++ code. */
30917 cp_parser_objc_interstitial_code (parser
);
30919 token
= cp_lexer_peek_token (parser
->lexer
);
30922 if (token
->type
!= CPP_EOF
)
30923 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30925 cp_parser_error (parser
, "expected %<@end%>");
30927 objc_finish_interface ();
30930 /* Parse an Objective-C method definition list. */
30933 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30935 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30937 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30941 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30944 tree sig
, attribute
;
30945 bool is_class_method
;
30946 if (token
->type
== CPP_PLUS
)
30947 is_class_method
= true;
30949 is_class_method
= false;
30950 push_deferring_access_checks (dk_deferred
);
30951 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30952 if (sig
== error_mark_node
)
30954 cp_parser_skip_to_end_of_block_or_statement (parser
);
30955 token
= cp_lexer_peek_token (parser
->lexer
);
30958 objc_start_method_definition (is_class_method
, sig
, attribute
,
30961 /* For historical reasons, we accept an optional semicolon. */
30962 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30963 cp_lexer_consume_token (parser
->lexer
);
30965 ptk
= cp_lexer_peek_token (parser
->lexer
);
30966 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30967 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30969 perform_deferred_access_checks (tf_warning_or_error
);
30970 stop_deferring_access_checks ();
30971 meth
= cp_parser_function_definition_after_declarator (parser
,
30973 pop_deferring_access_checks ();
30974 objc_finish_method_definition (meth
);
30977 /* The following case will be removed once @synthesize is
30978 completely implemented. */
30979 else if (token
->keyword
== RID_AT_PROPERTY
)
30980 cp_parser_objc_at_property_declaration (parser
);
30981 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30982 cp_parser_objc_at_synthesize_declaration (parser
);
30983 else if (token
->keyword
== RID_AT_DYNAMIC
)
30984 cp_parser_objc_at_dynamic_declaration (parser
);
30985 else if (token
->keyword
== RID_ATTRIBUTE
30986 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30987 warning_at (token
->location
, OPT_Wattributes
,
30988 "prefix attributes are ignored for methods");
30990 /* Allow for interspersed non-ObjC++ code. */
30991 cp_parser_objc_interstitial_code (parser
);
30993 token
= cp_lexer_peek_token (parser
->lexer
);
30996 if (token
->type
!= CPP_EOF
)
30997 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30999 cp_parser_error (parser
, "expected %<@end%>");
31001 objc_finish_implementation ();
31004 /* Parse Objective-C ivars. */
31007 cp_parser_objc_class_ivars (cp_parser
* parser
)
31009 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31011 if (token
->type
!= CPP_OPEN_BRACE
)
31012 return; /* No ivars specified. */
31014 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
31015 token
= cp_lexer_peek_token (parser
->lexer
);
31017 while (token
->type
!= CPP_CLOSE_BRACE
31018 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31020 cp_decl_specifier_seq declspecs
;
31021 int decl_class_or_enum_p
;
31022 tree prefix_attributes
;
31024 cp_parser_objc_visibility_spec (parser
);
31026 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
31029 cp_parser_decl_specifier_seq (parser
,
31030 CP_PARSER_FLAGS_OPTIONAL
,
31032 &decl_class_or_enum_p
);
31034 /* auto, register, static, extern, mutable. */
31035 if (declspecs
.storage_class
!= sc_none
)
31037 cp_parser_error (parser
, "invalid type for instance variable");
31038 declspecs
.storage_class
= sc_none
;
31041 /* thread_local. */
31042 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31044 cp_parser_error (parser
, "invalid type for instance variable");
31045 declspecs
.locations
[ds_thread
] = 0;
31049 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31051 cp_parser_error (parser
, "invalid type for instance variable");
31052 declspecs
.locations
[ds_typedef
] = 0;
31055 prefix_attributes
= declspecs
.attributes
;
31056 declspecs
.attributes
= NULL_TREE
;
31058 /* Keep going until we hit the `;' at the end of the
31060 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31062 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
31063 cp_declarator
*declarator
= NULL
;
31064 int ctor_dtor_or_conv_p
;
31066 /* Check for a (possibly unnamed) bitfield declaration. */
31067 token
= cp_lexer_peek_token (parser
->lexer
);
31068 if (token
->type
== CPP_COLON
)
31071 if (token
->type
== CPP_NAME
31072 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
31075 /* Get the name of the bitfield. */
31076 declarator
= make_id_declarator (NULL_TREE
,
31077 cp_parser_identifier (parser
),
31078 sfk_none
, token
->location
);
31081 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
31082 /* Get the width of the bitfield. */
31084 = cp_parser_constant_expression (parser
);
31088 /* Parse the declarator. */
31090 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31091 CP_PARSER_FLAGS_NONE
,
31092 &ctor_dtor_or_conv_p
,
31093 /*parenthesized_p=*/NULL
,
31094 /*member_p=*/false,
31095 /*friend_p=*/false,
31096 /*static_p=*/false);
31099 /* Look for attributes that apply to the ivar. */
31100 attributes
= cp_parser_attributes_opt (parser
);
31101 /* Remember which attributes are prefix attributes and
31103 first_attribute
= attributes
;
31104 /* Combine the attributes. */
31105 attributes
= attr_chainon (prefix_attributes
, attributes
);
31108 /* Create the bitfield declaration. */
31109 decl
= grokbitfield (declarator
, &declspecs
,
31110 width
, NULL_TREE
, attributes
);
31112 decl
= grokfield (declarator
, &declspecs
,
31113 NULL_TREE
, /*init_const_expr_p=*/false,
31114 NULL_TREE
, attributes
);
31116 /* Add the instance variable. */
31117 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
31118 objc_add_instance_variable (decl
);
31120 /* Reset PREFIX_ATTRIBUTES. */
31121 if (attributes
!= error_mark_node
)
31123 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31124 attributes
= TREE_CHAIN (attributes
);
31126 TREE_CHAIN (attributes
) = NULL_TREE
;
31129 token
= cp_lexer_peek_token (parser
->lexer
);
31131 if (token
->type
== CPP_COMMA
)
31133 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31139 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31140 token
= cp_lexer_peek_token (parser
->lexer
);
31143 if (token
->keyword
== RID_AT_END
)
31144 cp_parser_error (parser
, "expected %<}%>");
31146 /* Do not consume the RID_AT_END, so it will be read again as terminating
31147 the @interface of @implementation. */
31148 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31149 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
31151 /* For historical reasons, we accept an optional semicolon. */
31152 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31153 cp_lexer_consume_token (parser
->lexer
);
31156 /* Parse an Objective-C protocol declaration. */
31159 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
31161 tree proto
, protorefs
;
31164 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
31165 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31167 tok
= cp_lexer_peek_token (parser
->lexer
);
31168 error_at (tok
->location
, "identifier expected after %<@protocol%>");
31169 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31173 /* See if we have a forward declaration or a definition. */
31174 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
31176 /* Try a forward declaration first. */
31177 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
31183 id
= cp_parser_identifier (parser
);
31184 if (id
== error_mark_node
)
31187 objc_declare_protocol (id
, attributes
);
31189 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31190 cp_lexer_consume_token (parser
->lexer
);
31194 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31197 /* Ok, we got a full-fledged definition (or at least should). */
31200 proto
= cp_parser_identifier (parser
);
31201 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
31202 objc_start_protocol (proto
, protorefs
, attributes
);
31203 cp_parser_objc_method_prototype_list (parser
);
31207 /* Parse an Objective-C superclass or category. */
31210 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
31213 tree
*categ
, bool *is_class_extension
)
31215 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
31217 *super
= *categ
= NULL_TREE
;
31218 *is_class_extension
= false;
31219 if (next
->type
== CPP_COLON
)
31221 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
31222 *super
= cp_parser_identifier (parser
);
31224 else if (next
->type
== CPP_OPEN_PAREN
)
31226 matching_parens parens
;
31227 parens
.consume_open (parser
); /* Eat '('. */
31229 /* If there is no category name, and this is an @interface, we
31230 have a class extension. */
31231 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31233 *categ
= NULL_TREE
;
31234 *is_class_extension
= true;
31237 *categ
= cp_parser_identifier (parser
);
31239 parens
.require_close (parser
);
31243 /* Parse an Objective-C class interface. */
31246 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
31248 tree name
, super
, categ
, protos
;
31249 bool is_class_extension
;
31251 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
31252 name
= cp_parser_identifier (parser
);
31253 if (name
== error_mark_node
)
31255 /* It's hard to recover because even if valid @interface stuff
31256 is to follow, we can't compile it (or validate it) if we
31257 don't even know which class it refers to. Let's assume this
31258 was a stray '@interface' token in the stream and skip it.
31262 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
31263 &is_class_extension
);
31264 protos
= cp_parser_objc_protocol_refs_opt (parser
);
31266 /* We have either a class or a category on our hands. */
31267 if (categ
|| is_class_extension
)
31268 objc_start_category_interface (name
, categ
, protos
, attributes
);
31271 objc_start_class_interface (name
, super
, protos
, attributes
);
31272 /* Handle instance variable declarations, if any. */
31273 cp_parser_objc_class_ivars (parser
);
31274 objc_continue_interface ();
31277 cp_parser_objc_method_prototype_list (parser
);
31280 /* Parse an Objective-C class implementation. */
31283 cp_parser_objc_class_implementation (cp_parser
* parser
)
31285 tree name
, super
, categ
;
31286 bool is_class_extension
;
31288 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
31289 name
= cp_parser_identifier (parser
);
31290 if (name
== error_mark_node
)
31292 /* It's hard to recover because even if valid @implementation
31293 stuff is to follow, we can't compile it (or validate it) if
31294 we don't even know which class it refers to. Let's assume
31295 this was a stray '@implementation' token in the stream and
31300 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
31301 &is_class_extension
);
31303 /* We have either a class or a category on our hands. */
31305 objc_start_category_implementation (name
, categ
);
31308 objc_start_class_implementation (name
, super
);
31309 /* Handle instance variable declarations, if any. */
31310 cp_parser_objc_class_ivars (parser
);
31311 objc_continue_implementation ();
31314 cp_parser_objc_method_definition_list (parser
);
31317 /* Consume the @end token and finish off the implementation. */
31320 cp_parser_objc_end_implementation (cp_parser
* parser
)
31322 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31323 objc_finish_implementation ();
31326 /* Parse an Objective-C declaration. */
31329 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
31331 /* Try to figure out what kind of declaration is present. */
31332 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31335 switch (kwd
->keyword
)
31340 error_at (kwd
->location
, "attributes may not be specified before"
31341 " the %<@%D%> Objective-C++ keyword",
31345 case RID_AT_IMPLEMENTATION
:
31346 warning_at (kwd
->location
, OPT_Wattributes
,
31347 "prefix attributes are ignored before %<@%D%>",
31354 switch (kwd
->keyword
)
31357 cp_parser_objc_alias_declaration (parser
);
31360 cp_parser_objc_class_declaration (parser
);
31362 case RID_AT_PROTOCOL
:
31363 cp_parser_objc_protocol_declaration (parser
, attributes
);
31365 case RID_AT_INTERFACE
:
31366 cp_parser_objc_class_interface (parser
, attributes
);
31368 case RID_AT_IMPLEMENTATION
:
31369 cp_parser_objc_class_implementation (parser
);
31372 cp_parser_objc_end_implementation (parser
);
31375 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31377 cp_parser_skip_to_end_of_block_or_statement (parser
);
31381 /* Parse an Objective-C try-catch-finally statement.
31383 objc-try-catch-finally-stmt:
31384 @try compound-statement objc-catch-clause-seq [opt]
31385 objc-finally-clause [opt]
31387 objc-catch-clause-seq:
31388 objc-catch-clause objc-catch-clause-seq [opt]
31391 @catch ( objc-exception-declaration ) compound-statement
31393 objc-finally-clause:
31394 @finally compound-statement
31396 objc-exception-declaration:
31397 parameter-declaration
31400 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31404 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31405 for C. Keep them in sync. */
31408 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
31410 location_t location
;
31413 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
31414 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31415 objc_maybe_warn_exceptions (location
);
31416 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31417 node, lest it get absorbed into the surrounding block. */
31418 stmt
= push_stmt_list ();
31419 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31420 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
31422 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
31424 cp_parameter_declarator
*parm
;
31425 tree parameter_declaration
= error_mark_node
;
31426 bool seen_open_paren
= false;
31427 matching_parens parens
;
31429 cp_lexer_consume_token (parser
->lexer
);
31430 if (parens
.require_open (parser
))
31431 seen_open_paren
= true;
31432 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
31434 /* We have "@catch (...)" (where the '...' are literally
31435 what is in the code). Skip the '...'.
31436 parameter_declaration is set to NULL_TREE, and
31437 objc_being_catch_clauses() knows that that means
31439 cp_lexer_consume_token (parser
->lexer
);
31440 parameter_declaration
= NULL_TREE
;
31444 /* We have "@catch (NSException *exception)" or something
31445 like that. Parse the parameter declaration. */
31446 parm
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
31449 parameter_declaration
= error_mark_node
;
31451 parameter_declaration
= grokdeclarator (parm
->declarator
,
31452 &parm
->decl_specifiers
,
31453 PARM
, /*initialized=*/0,
31454 /*attrlist=*/NULL
);
31456 if (seen_open_paren
)
31457 parens
.require_close (parser
);
31460 /* If there was no open parenthesis, we are recovering from
31461 an error, and we are trying to figure out what mistake
31462 the user has made. */
31464 /* If there is an immediate closing parenthesis, the user
31465 probably forgot the opening one (ie, they typed "@catch
31466 NSException *e)". Parse the closing parenthesis and keep
31468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31469 cp_lexer_consume_token (parser
->lexer
);
31471 /* If these is no immediate closing parenthesis, the user
31472 probably doesn't know that parenthesis are required at
31473 all (ie, they typed "@catch NSException *e"). So, just
31474 forget about the closing parenthesis and keep going. */
31476 objc_begin_catch_clause (parameter_declaration
);
31477 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31478 objc_finish_catch_clause ();
31480 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
31482 cp_lexer_consume_token (parser
->lexer
);
31483 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31484 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31485 node, lest it get absorbed into the surrounding block. */
31486 stmt
= push_stmt_list ();
31487 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31488 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
31491 return objc_finish_try_stmt ();
31494 /* Parse an Objective-C synchronized statement.
31496 objc-synchronized-stmt:
31497 @synchronized ( expression ) compound-statement
31499 Returns NULL_TREE. */
31502 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
31504 location_t location
;
31507 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
31509 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31510 objc_maybe_warn_exceptions (location
);
31511 matching_parens parens
;
31512 parens
.require_open (parser
);
31513 lock
= cp_parser_expression (parser
);
31514 parens
.require_close (parser
);
31516 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31517 node, lest it get absorbed into the surrounding block. */
31518 stmt
= push_stmt_list ();
31519 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31521 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
31524 /* Parse an Objective-C throw statement.
31527 @throw assignment-expression [opt] ;
31529 Returns a constructed '@throw' statement. */
31532 cp_parser_objc_throw_statement (cp_parser
*parser
)
31534 tree expr
= NULL_TREE
;
31535 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31537 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
31539 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31540 expr
= cp_parser_expression (parser
);
31542 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31544 return objc_build_throw_stmt (loc
, expr
);
31547 /* Parse an Objective-C statement. */
31550 cp_parser_objc_statement (cp_parser
* parser
)
31552 /* Try to figure out what kind of declaration is present. */
31553 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31555 switch (kwd
->keyword
)
31558 return cp_parser_objc_try_catch_finally_statement (parser
);
31559 case RID_AT_SYNCHRONIZED
:
31560 return cp_parser_objc_synchronized_statement (parser
);
31562 return cp_parser_objc_throw_statement (parser
);
31564 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31566 cp_parser_skip_to_end_of_block_or_statement (parser
);
31569 return error_mark_node
;
31572 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31573 look ahead to see if an objc keyword follows the attributes. This
31574 is to detect the use of prefix attributes on ObjC @interface and
31578 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
31580 cp_lexer_save_tokens (parser
->lexer
);
31581 *attrib
= cp_parser_attributes_opt (parser
);
31582 gcc_assert (*attrib
);
31583 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
31585 cp_lexer_commit_tokens (parser
->lexer
);
31588 cp_lexer_rollback_tokens (parser
->lexer
);
31592 /* This routine is a minimal replacement for
31593 c_parser_struct_declaration () used when parsing the list of
31594 types/names or ObjC++ properties. For example, when parsing the
31597 @property (readonly) int a, b, c;
31599 this function is responsible for parsing "int a, int b, int c" and
31600 returning the declarations as CHAIN of DECLs.
31602 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31603 similar parsing. */
31605 cp_parser_objc_struct_declaration (cp_parser
*parser
)
31607 tree decls
= NULL_TREE
;
31608 cp_decl_specifier_seq declspecs
;
31609 int decl_class_or_enum_p
;
31610 tree prefix_attributes
;
31612 cp_parser_decl_specifier_seq (parser
,
31613 CP_PARSER_FLAGS_NONE
,
31615 &decl_class_or_enum_p
);
31617 if (declspecs
.type
== error_mark_node
)
31618 return error_mark_node
;
31620 /* auto, register, static, extern, mutable. */
31621 if (declspecs
.storage_class
!= sc_none
)
31623 cp_parser_error (parser
, "invalid type for property");
31624 declspecs
.storage_class
= sc_none
;
31627 /* thread_local. */
31628 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31630 cp_parser_error (parser
, "invalid type for property");
31631 declspecs
.locations
[ds_thread
] = 0;
31635 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31637 cp_parser_error (parser
, "invalid type for property");
31638 declspecs
.locations
[ds_typedef
] = 0;
31641 prefix_attributes
= declspecs
.attributes
;
31642 declspecs
.attributes
= NULL_TREE
;
31644 /* Keep going until we hit the `;' at the end of the declaration. */
31645 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31647 tree attributes
, first_attribute
, decl
;
31648 cp_declarator
*declarator
;
31651 /* Parse the declarator. */
31652 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31653 CP_PARSER_FLAGS_NONE
,
31654 NULL
, NULL
, false, false, false);
31656 /* Look for attributes that apply to the ivar. */
31657 attributes
= cp_parser_attributes_opt (parser
);
31658 /* Remember which attributes are prefix attributes and
31660 first_attribute
= attributes
;
31661 /* Combine the attributes. */
31662 attributes
= attr_chainon (prefix_attributes
, attributes
);
31664 decl
= grokfield (declarator
, &declspecs
,
31665 NULL_TREE
, /*init_const_expr_p=*/false,
31666 NULL_TREE
, attributes
);
31668 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31669 return error_mark_node
;
31671 /* Reset PREFIX_ATTRIBUTES. */
31672 if (attributes
!= error_mark_node
)
31674 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31675 attributes
= TREE_CHAIN (attributes
);
31677 TREE_CHAIN (attributes
) = NULL_TREE
;
31680 DECL_CHAIN (decl
) = decls
;
31683 token
= cp_lexer_peek_token (parser
->lexer
);
31684 if (token
->type
== CPP_COMMA
)
31686 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31695 /* Parse an Objective-C @property declaration. The syntax is:
31697 objc-property-declaration:
31698 '@property' objc-property-attributes[opt] struct-declaration ;
31700 objc-property-attributes:
31701 '(' objc-property-attribute-list ')'
31703 objc-property-attribute-list:
31704 objc-property-attribute
31705 objc-property-attribute-list, objc-property-attribute
31707 objc-property-attribute
31708 'getter' = identifier
31709 'setter' = identifier
31718 @property NSString *name;
31719 @property (readonly) id object;
31720 @property (retain, nonatomic, getter=getTheName) id name;
31721 @property int a, b, c;
31723 PS: This function is identical to
31724 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31726 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31728 /* The following variables hold the attributes of the properties as
31729 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31730 seen. When we see an attribute, we set them to 'true' (if they
31731 are boolean properties) or to the identifier (if they have an
31732 argument, ie, for getter and setter). Note that here we only
31733 parse the list of attributes, check the syntax and accumulate the
31734 attributes that we find. objc_add_property_declaration() will
31735 then process the information. */
31736 bool property_assign
= false;
31737 bool property_copy
= false;
31738 tree property_getter_ident
= NULL_TREE
;
31739 bool property_nonatomic
= false;
31740 bool property_readonly
= false;
31741 bool property_readwrite
= false;
31742 bool property_retain
= false;
31743 tree property_setter_ident
= NULL_TREE
;
31745 /* 'properties' is the list of properties that we read. Usually a
31746 single one, but maybe more (eg, in "@property int a, b, c;" there
31751 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31753 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31755 /* Parse the optional attribute list... */
31756 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31759 matching_parens parens
;
31760 parens
.consume_open (parser
);
31764 bool syntax_error
= false;
31765 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31768 if (token
->type
!= CPP_NAME
)
31770 cp_parser_error (parser
, "expected identifier");
31773 keyword
= C_RID_CODE (token
->u
.value
);
31774 cp_lexer_consume_token (parser
->lexer
);
31777 case RID_ASSIGN
: property_assign
= true; break;
31778 case RID_COPY
: property_copy
= true; break;
31779 case RID_NONATOMIC
: property_nonatomic
= true; break;
31780 case RID_READONLY
: property_readonly
= true; break;
31781 case RID_READWRITE
: property_readwrite
= true; break;
31782 case RID_RETAIN
: property_retain
= true; break;
31786 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31788 if (keyword
== RID_GETTER
)
31789 cp_parser_error (parser
,
31790 "missing %<=%> (after %<getter%> attribute)");
31792 cp_parser_error (parser
,
31793 "missing %<=%> (after %<setter%> attribute)");
31794 syntax_error
= true;
31797 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31798 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31800 cp_parser_error (parser
, "expected identifier");
31801 syntax_error
= true;
31804 if (keyword
== RID_SETTER
)
31806 if (property_setter_ident
!= NULL_TREE
)
31808 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31809 cp_lexer_consume_token (parser
->lexer
);
31812 property_setter_ident
= cp_parser_objc_selector (parser
);
31813 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31814 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31816 cp_lexer_consume_token (parser
->lexer
);
31820 if (property_getter_ident
!= NULL_TREE
)
31822 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31823 cp_lexer_consume_token (parser
->lexer
);
31826 property_getter_ident
= cp_parser_objc_selector (parser
);
31830 cp_parser_error (parser
, "unknown property attribute");
31831 syntax_error
= true;
31838 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31839 cp_lexer_consume_token (parser
->lexer
);
31844 /* FIXME: "@property (setter, assign);" will generate a spurious
31845 "error: expected ‘)’ before ‘,’ token". This is because
31846 cp_parser_require, unlike the C counterpart, will produce an
31847 error even if we are in error recovery. */
31848 if (!parens
.require_close (parser
))
31850 cp_parser_skip_to_closing_parenthesis (parser
,
31851 /*recovering=*/true,
31852 /*or_comma=*/false,
31853 /*consume_paren=*/true);
31857 /* ... and the property declaration(s). */
31858 properties
= cp_parser_objc_struct_declaration (parser
);
31860 if (properties
== error_mark_node
)
31862 cp_parser_skip_to_end_of_statement (parser
);
31863 /* If the next token is now a `;', consume it. */
31864 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31865 cp_lexer_consume_token (parser
->lexer
);
31869 if (properties
== NULL_TREE
)
31870 cp_parser_error (parser
, "expected identifier");
31873 /* Comma-separated properties are chained together in
31874 reverse order; add them one by one. */
31875 properties
= nreverse (properties
);
31877 for (; properties
; properties
= TREE_CHAIN (properties
))
31878 objc_add_property_declaration (loc
, copy_node (properties
),
31879 property_readonly
, property_readwrite
,
31880 property_assign
, property_retain
,
31881 property_copy
, property_nonatomic
,
31882 property_getter_ident
, property_setter_ident
);
31885 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31888 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31890 objc-synthesize-declaration:
31891 @synthesize objc-synthesize-identifier-list ;
31893 objc-synthesize-identifier-list:
31894 objc-synthesize-identifier
31895 objc-synthesize-identifier-list, objc-synthesize-identifier
31897 objc-synthesize-identifier
31899 identifier = identifier
31902 @synthesize MyProperty;
31903 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31905 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31906 for C. Keep them in sync.
31909 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31911 tree list
= NULL_TREE
;
31913 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31915 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31918 tree property
, ivar
;
31919 property
= cp_parser_identifier (parser
);
31920 if (property
== error_mark_node
)
31922 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31925 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31927 cp_lexer_consume_token (parser
->lexer
);
31928 ivar
= cp_parser_identifier (parser
);
31929 if (ivar
== error_mark_node
)
31931 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31937 list
= chainon (list
, build_tree_list (ivar
, property
));
31938 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31939 cp_lexer_consume_token (parser
->lexer
);
31943 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31944 objc_add_synthesize_declaration (loc
, list
);
31947 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31949 objc-dynamic-declaration:
31950 @dynamic identifier-list ;
31953 @dynamic MyProperty;
31954 @dynamic MyProperty, AnotherProperty;
31956 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31957 for C. Keep them in sync.
31960 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31962 tree list
= NULL_TREE
;
31964 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31966 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31970 property
= cp_parser_identifier (parser
);
31971 if (property
== error_mark_node
)
31973 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31976 list
= chainon (list
, build_tree_list (NULL
, property
));
31977 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31978 cp_lexer_consume_token (parser
->lexer
);
31982 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31983 objc_add_dynamic_declaration (loc
, list
);
31987 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31989 /* Returns name of the next clause.
31990 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31991 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31992 returned and the token is consumed. */
31994 static pragma_omp_clause
31995 cp_parser_omp_clause_name (cp_parser
*parser
)
31997 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31999 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32000 result
= PRAGMA_OACC_CLAUSE_AUTO
;
32001 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
32002 result
= PRAGMA_OMP_CLAUSE_IF
;
32003 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
32004 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
32005 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
32006 result
= PRAGMA_OACC_CLAUSE_DELETE
;
32007 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
32008 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
32009 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
32010 result
= PRAGMA_OMP_CLAUSE_FOR
;
32011 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32013 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32014 const char *p
= IDENTIFIER_POINTER (id
);
32019 if (!strcmp ("aligned", p
))
32020 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
32021 else if (!strcmp ("async", p
))
32022 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
32025 if (!strcmp ("collapse", p
))
32026 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
32027 else if (!strcmp ("copy", p
))
32028 result
= PRAGMA_OACC_CLAUSE_COPY
;
32029 else if (!strcmp ("copyin", p
))
32030 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
32031 else if (!strcmp ("copyout", p
))
32032 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
32033 else if (!strcmp ("copyprivate", p
))
32034 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
32035 else if (!strcmp ("create", p
))
32036 result
= PRAGMA_OACC_CLAUSE_CREATE
;
32039 if (!strcmp ("defaultmap", p
))
32040 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
32041 else if (!strcmp ("depend", p
))
32042 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
32043 else if (!strcmp ("device", p
))
32044 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
32045 else if (!strcmp ("deviceptr", p
))
32046 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
32047 else if (!strcmp ("device_resident", p
))
32048 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
32049 else if (!strcmp ("dist_schedule", p
))
32050 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
32053 if (!strcmp ("final", p
))
32054 result
= PRAGMA_OMP_CLAUSE_FINAL
;
32055 else if (!strcmp ("finalize", p
))
32056 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
32057 else if (!strcmp ("firstprivate", p
))
32058 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
32059 else if (!strcmp ("from", p
))
32060 result
= PRAGMA_OMP_CLAUSE_FROM
;
32063 if (!strcmp ("gang", p
))
32064 result
= PRAGMA_OACC_CLAUSE_GANG
;
32065 else if (!strcmp ("grainsize", p
))
32066 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
32069 if (!strcmp ("hint", p
))
32070 result
= PRAGMA_OMP_CLAUSE_HINT
;
32071 else if (!strcmp ("host", p
))
32072 result
= PRAGMA_OACC_CLAUSE_HOST
;
32075 if (!strcmp ("if_present", p
))
32076 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
32077 else if (!strcmp ("in_reduction", p
))
32078 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
32079 else if (!strcmp ("inbranch", p
))
32080 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
32081 else if (!strcmp ("independent", p
))
32082 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
32083 else if (!strcmp ("is_device_ptr", p
))
32084 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
32087 if (!strcmp ("lastprivate", p
))
32088 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
32089 else if (!strcmp ("linear", p
))
32090 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
32091 else if (!strcmp ("link", p
))
32092 result
= PRAGMA_OMP_CLAUSE_LINK
;
32095 if (!strcmp ("map", p
))
32096 result
= PRAGMA_OMP_CLAUSE_MAP
;
32097 else if (!strcmp ("mergeable", p
))
32098 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
32101 if (!strcmp ("nogroup", p
))
32102 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
32103 else if (!strcmp ("nontemporal", p
))
32104 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
32105 else if (!strcmp ("notinbranch", p
))
32106 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
32107 else if (!strcmp ("nowait", p
))
32108 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
32109 else if (!strcmp ("num_gangs", p
))
32110 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
32111 else if (!strcmp ("num_tasks", p
))
32112 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
32113 else if (!strcmp ("num_teams", p
))
32114 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
32115 else if (!strcmp ("num_threads", p
))
32116 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
32117 else if (!strcmp ("num_workers", p
))
32118 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
32121 if (!strcmp ("ordered", p
))
32122 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
32125 if (!strcmp ("parallel", p
))
32126 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
32127 else if (!strcmp ("present", p
))
32128 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
32129 else if (!strcmp ("present_or_copy", p
)
32130 || !strcmp ("pcopy", p
))
32131 result
= PRAGMA_OACC_CLAUSE_COPY
;
32132 else if (!strcmp ("present_or_copyin", p
)
32133 || !strcmp ("pcopyin", p
))
32134 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
32135 else if (!strcmp ("present_or_copyout", p
)
32136 || !strcmp ("pcopyout", p
))
32137 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
32138 else if (!strcmp ("present_or_create", p
)
32139 || !strcmp ("pcreate", p
))
32140 result
= PRAGMA_OACC_CLAUSE_CREATE
;
32141 else if (!strcmp ("priority", p
))
32142 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
32143 else if (!strcmp ("proc_bind", p
))
32144 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
32147 if (!strcmp ("reduction", p
))
32148 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
32151 if (!strcmp ("safelen", p
))
32152 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
32153 else if (!strcmp ("schedule", p
))
32154 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
32155 else if (!strcmp ("sections", p
))
32156 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
32157 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
32158 result
= PRAGMA_OACC_CLAUSE_HOST
;
32159 else if (!strcmp ("seq", p
))
32160 result
= PRAGMA_OACC_CLAUSE_SEQ
;
32161 else if (!strcmp ("shared", p
))
32162 result
= PRAGMA_OMP_CLAUSE_SHARED
;
32163 else if (!strcmp ("simd", p
))
32164 result
= PRAGMA_OMP_CLAUSE_SIMD
;
32165 else if (!strcmp ("simdlen", p
))
32166 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
32169 if (!strcmp ("task_reduction", p
))
32170 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
32171 else if (!strcmp ("taskgroup", p
))
32172 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
32173 else if (!strcmp ("thread_limit", p
))
32174 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
32175 else if (!strcmp ("threads", p
))
32176 result
= PRAGMA_OMP_CLAUSE_THREADS
;
32177 else if (!strcmp ("tile", p
))
32178 result
= PRAGMA_OACC_CLAUSE_TILE
;
32179 else if (!strcmp ("to", p
))
32180 result
= PRAGMA_OMP_CLAUSE_TO
;
32183 if (!strcmp ("uniform", p
))
32184 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
32185 else if (!strcmp ("untied", p
))
32186 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
32187 else if (!strcmp ("use_device", p
))
32188 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
32189 else if (!strcmp ("use_device_ptr", p
))
32190 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
32193 if (!strcmp ("vector", p
))
32194 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
32195 else if (!strcmp ("vector_length", p
))
32196 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
32199 if (!strcmp ("wait", p
))
32200 result
= PRAGMA_OACC_CLAUSE_WAIT
;
32201 else if (!strcmp ("worker", p
))
32202 result
= PRAGMA_OACC_CLAUSE_WORKER
;
32207 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
32208 cp_lexer_consume_token (parser
->lexer
);
32213 /* Validate that a clause of the given type does not already exist. */
32216 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
32217 const char *name
, location_t location
)
32221 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32222 if (OMP_CLAUSE_CODE (c
) == code
)
32224 error_at (location
, "too many %qs clauses", name
);
32232 variable-list , identifier
32234 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32235 colon). An opening parenthesis will have been consumed by the caller.
32237 If KIND is nonzero, create the appropriate node and install the decl
32238 in OMP_CLAUSE_DECL and add the node to the head of the list.
32240 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32241 return the list created.
32243 COLON can be NULL if only closing parenthesis should end the list,
32244 or pointer to bool which will receive false if the list is terminated
32245 by closing parenthesis or true if the list is terminated by colon. */
32248 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
32249 tree list
, bool *colon
)
32252 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32255 parser
->colon_corrects_to_scope_p
= false;
32262 if (kind
== OMP_CLAUSE_DEPEND
)
32263 cp_parser_parse_tentatively (parser
);
32264 token
= cp_lexer_peek_token (parser
->lexer
);
32266 && current_class_ptr
32267 && cp_parser_is_keyword (token
, RID_THIS
))
32269 decl
= finish_this_expr ();
32270 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
32271 || CONVERT_EXPR_P (decl
))
32272 decl
= TREE_OPERAND (decl
, 0);
32273 cp_lexer_consume_token (parser
->lexer
);
32277 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
32278 /*check_dependency_p=*/true,
32279 /*template_p=*/NULL
,
32280 /*declarator_p=*/false,
32281 /*optional_p=*/false);
32282 if (name
== error_mark_node
)
32284 if (kind
== OMP_CLAUSE_DEPEND
32285 && cp_parser_simulate_error (parser
))
32286 goto depend_lvalue
;
32290 if (identifier_p (name
))
32291 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
32294 if (decl
== error_mark_node
)
32296 if (kind
== OMP_CLAUSE_DEPEND
32297 && cp_parser_simulate_error (parser
))
32298 goto depend_lvalue
;
32299 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
32303 if (decl
== error_mark_node
)
32305 else if (kind
!= 0)
32309 case OMP_CLAUSE__CACHE_
:
32310 /* The OpenACC cache directive explicitly only allows "array
32311 elements or subarrays". */
32312 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
32314 error_at (token
->location
, "expected %<[%>");
32315 decl
= error_mark_node
;
32319 case OMP_CLAUSE_MAP
:
32320 case OMP_CLAUSE_FROM
:
32321 case OMP_CLAUSE_TO
:
32322 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
32325 = cp_lexer_peek_token (parser
->lexer
)->location
;
32326 cp_id_kind idk
= CP_ID_KIND_NONE
;
32327 cp_lexer_consume_token (parser
->lexer
);
32328 decl
= convert_from_reference (decl
);
32330 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
32335 case OMP_CLAUSE_DEPEND
:
32336 case OMP_CLAUSE_REDUCTION
:
32337 case OMP_CLAUSE_IN_REDUCTION
:
32338 case OMP_CLAUSE_TASK_REDUCTION
:
32339 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
32341 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
32343 parser
->colon_corrects_to_scope_p
= false;
32344 cp_lexer_consume_token (parser
->lexer
);
32345 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32346 low_bound
= cp_parser_expression (parser
);
32348 parser
->colon_corrects_to_scope_p
32349 = saved_colon_corrects_to_scope_p
;
32350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
32351 length
= integer_one_node
;
32354 /* Look for `:'. */
32355 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32357 if (kind
== OMP_CLAUSE_DEPEND
32358 && cp_parser_simulate_error (parser
))
32359 goto depend_lvalue
;
32362 if (kind
== OMP_CLAUSE_DEPEND
)
32363 cp_parser_commit_to_tentative_parse (parser
);
32364 if (!cp_lexer_next_token_is (parser
->lexer
,
32366 length
= cp_parser_expression (parser
);
32368 /* Look for the closing `]'. */
32369 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
32372 if (kind
== OMP_CLAUSE_DEPEND
32373 && cp_parser_simulate_error (parser
))
32374 goto depend_lvalue
;
32378 decl
= tree_cons (low_bound
, length
, decl
);
32385 if (kind
== OMP_CLAUSE_DEPEND
)
32387 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
32388 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
32389 && cp_parser_simulate_error (parser
))
32392 cp_parser_abort_tentative_parse (parser
);
32393 decl
= cp_parser_assignment_expression (parser
, NULL
,
32397 cp_parser_parse_definitely (parser
);
32400 tree u
= build_omp_clause (token
->location
, kind
);
32401 OMP_CLAUSE_DECL (u
) = decl
;
32402 OMP_CLAUSE_CHAIN (u
) = list
;
32406 list
= tree_cons (decl
, NULL_TREE
, list
);
32409 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
32411 cp_lexer_consume_token (parser
->lexer
);
32415 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32417 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32420 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32424 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32428 /* Try to resync to an unnested comma. Copied from
32429 cp_parser_parenthesized_expression_list. */
32432 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32433 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
32434 /*recovering=*/true,
32436 /*consume_paren=*/true);
32444 /* Similarly, but expect leading and trailing parenthesis. This is a very
32445 common case for omp clauses. */
32448 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
32450 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32451 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
32456 copy ( variable-list )
32457 copyin ( variable-list )
32458 copyout ( variable-list )
32459 create ( variable-list )
32460 delete ( variable-list )
32461 present ( variable-list ) */
32464 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
32467 enum gomp_map_kind kind
;
32470 case PRAGMA_OACC_CLAUSE_COPY
:
32471 kind
= GOMP_MAP_TOFROM
;
32473 case PRAGMA_OACC_CLAUSE_COPYIN
:
32474 kind
= GOMP_MAP_TO
;
32476 case PRAGMA_OACC_CLAUSE_COPYOUT
:
32477 kind
= GOMP_MAP_FROM
;
32479 case PRAGMA_OACC_CLAUSE_CREATE
:
32480 kind
= GOMP_MAP_ALLOC
;
32482 case PRAGMA_OACC_CLAUSE_DELETE
:
32483 kind
= GOMP_MAP_RELEASE
;
32485 case PRAGMA_OACC_CLAUSE_DEVICE
:
32486 kind
= GOMP_MAP_FORCE_TO
;
32488 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
32489 kind
= GOMP_MAP_DEVICE_RESIDENT
;
32491 case PRAGMA_OACC_CLAUSE_HOST
:
32492 kind
= GOMP_MAP_FORCE_FROM
;
32494 case PRAGMA_OACC_CLAUSE_LINK
:
32495 kind
= GOMP_MAP_LINK
;
32497 case PRAGMA_OACC_CLAUSE_PRESENT
:
32498 kind
= GOMP_MAP_FORCE_PRESENT
;
32501 gcc_unreachable ();
32504 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
32506 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32507 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32513 deviceptr ( variable-list ) */
32516 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
32518 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32521 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32522 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32523 variable-list must only allow for pointer variables. */
32524 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
32525 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
32527 tree v
= TREE_PURPOSE (t
);
32528 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
32529 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
32530 OMP_CLAUSE_DECL (u
) = v
;
32531 OMP_CLAUSE_CHAIN (u
) = list
;
32546 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
32547 enum omp_clause_code code
,
32548 tree list
, location_t location
)
32550 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32551 tree c
= build_omp_clause (location
, code
);
32552 OMP_CLAUSE_CHAIN (c
) = list
;
32557 num_gangs ( expression )
32558 num_workers ( expression )
32559 vector_length ( expression ) */
32562 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
32563 const char *str
, tree list
)
32565 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32567 matching_parens parens
;
32568 if (!parens
.require_open (parser
))
32571 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
32573 if (t
== error_mark_node
32574 || !parens
.require_close (parser
))
32576 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32577 /*or_comma=*/false,
32578 /*consume_paren=*/true);
32582 check_no_duplicate_clause (list
, code
, str
, loc
);
32584 tree c
= build_omp_clause (loc
, code
);
32585 OMP_CLAUSE_OPERAND (c
, 0) = t
;
32586 OMP_CLAUSE_CHAIN (c
) = list
;
32592 gang [( gang-arg-list )]
32593 worker [( [num:] int-expr )]
32594 vector [( [length:] int-expr )]
32596 where gang-arg is one of:
32601 and size-expr may be:
32608 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
32609 const char *str
, tree list
)
32611 const char *id
= "num";
32612 cp_lexer
*lexer
= parser
->lexer
;
32613 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
32614 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
32616 if (kind
== OMP_CLAUSE_VECTOR
)
32619 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
32621 matching_parens parens
;
32622 parens
.consume_open (parser
);
32626 cp_token
*next
= cp_lexer_peek_token (lexer
);
32629 /* Gang static argument. */
32630 if (kind
== OMP_CLAUSE_GANG
32631 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
32633 cp_lexer_consume_token (lexer
);
32635 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32636 goto cleanup_error
;
32639 if (ops
[idx
] != NULL
)
32641 cp_parser_error (parser
, "too many %<static%> arguments");
32642 goto cleanup_error
;
32645 /* Check for the '*' argument. */
32646 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
32647 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32648 || cp_lexer_nth_token_is (parser
->lexer
, 2,
32651 cp_lexer_consume_token (lexer
);
32652 ops
[idx
] = integer_minus_one_node
;
32654 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32656 cp_lexer_consume_token (lexer
);
32662 /* Worker num: argument and vector length: arguments. */
32663 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
32664 && id_equal (next
->u
.value
, id
)
32665 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
32667 cp_lexer_consume_token (lexer
); /* id */
32668 cp_lexer_consume_token (lexer
); /* ':' */
32671 /* Now collect the actual argument. */
32672 if (ops
[idx
] != NULL_TREE
)
32674 cp_parser_error (parser
, "unexpected argument");
32675 goto cleanup_error
;
32678 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
32680 if (expr
== error_mark_node
)
32681 goto cleanup_error
;
32683 mark_exp_read (expr
);
32686 if (kind
== OMP_CLAUSE_GANG
32687 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32689 cp_lexer_consume_token (lexer
);
32696 if (!parens
.require_close (parser
))
32697 goto cleanup_error
;
32700 check_no_duplicate_clause (list
, kind
, str
, loc
);
32702 c
= build_omp_clause (loc
, kind
);
32705 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32707 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32708 OMP_CLAUSE_CHAIN (c
) = list
;
32713 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32718 tile ( size-expr-list ) */
32721 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32723 tree c
, expr
= error_mark_node
;
32724 tree tile
= NULL_TREE
;
32726 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32727 so, but the spec authors never considered such a case and have
32728 differing opinions on what it might mean, including 'not
32730 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32731 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32734 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32739 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32742 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32743 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32744 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32746 cp_lexer_consume_token (parser
->lexer
);
32747 expr
= integer_zero_node
;
32750 expr
= cp_parser_constant_expression (parser
);
32752 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32754 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32756 /* Consume the trailing ')'. */
32757 cp_lexer_consume_token (parser
->lexer
);
32759 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32760 tile
= nreverse (tile
);
32761 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32762 OMP_CLAUSE_CHAIN (c
) = list
;
32767 Parse wait clause or directive parameters. */
32770 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32772 vec
<tree
, va_gc
> *args
;
32775 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32777 /*allow_expansion_p=*/true,
32778 /*non_constant_p=*/NULL
);
32780 if (args
== NULL
|| args
->length () == 0)
32784 cp_parser_error (parser
, "expected integer expression list");
32785 release_tree_vector (args
);
32790 args_tree
= build_tree_list_vec (args
);
32792 release_tree_vector (args
);
32794 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32796 tree targ
= TREE_VALUE (t
);
32798 if (targ
!= error_mark_node
)
32800 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32801 error ("%<wait%> expression must be integral");
32804 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32806 targ
= mark_rvalue_use (targ
);
32807 OMP_CLAUSE_DECL (c
) = targ
;
32808 OMP_CLAUSE_CHAIN (c
) = list
;
32818 wait ( int-expr-list ) */
32821 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32823 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32825 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32828 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32834 collapse ( constant-expression ) */
32837 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32843 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32844 matching_parens parens
;
32845 if (!parens
.require_open (parser
))
32848 num
= cp_parser_constant_expression (parser
);
32850 if (!parens
.require_close (parser
))
32851 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32852 /*or_comma=*/false,
32853 /*consume_paren=*/true);
32855 if (num
== error_mark_node
)
32857 num
= fold_non_dependent_expr (num
);
32858 if (!tree_fits_shwi_p (num
)
32859 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32860 || (n
= tree_to_shwi (num
)) <= 0
32863 error_at (loc
, "collapse argument needs positive constant integer expression");
32867 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32868 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32869 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32870 OMP_CLAUSE_CHAIN (c
) = list
;
32871 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32877 default ( none | shared )
32880 default ( none | present ) */
32883 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32884 location_t location
, bool is_oacc
)
32886 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32889 matching_parens parens
;
32890 if (!parens
.require_open (parser
))
32892 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32894 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32895 const char *p
= IDENTIFIER_POINTER (id
);
32900 if (strcmp ("none", p
) != 0)
32902 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32906 if (strcmp ("present", p
) != 0 || !is_oacc
)
32908 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32912 if (strcmp ("shared", p
) != 0 || is_oacc
)
32914 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32921 cp_lexer_consume_token (parser
->lexer
);
32927 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32929 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32932 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32933 || !parens
.require_close (parser
))
32934 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32935 /*or_comma=*/false,
32936 /*consume_paren=*/true);
32938 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32941 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32942 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32943 OMP_CLAUSE_CHAIN (c
) = list
;
32944 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32950 final ( expression ) */
32953 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32957 matching_parens parens
;
32958 if (!parens
.require_open (parser
))
32961 t
= cp_parser_assignment_expression (parser
);
32963 if (t
== error_mark_node
32964 || !parens
.require_close (parser
))
32965 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32966 /*or_comma=*/false,
32967 /*consume_paren=*/true);
32969 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32971 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32972 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32973 OMP_CLAUSE_CHAIN (c
) = list
;
32982 if ( directive-name-modifier : expression )
32984 directive-name-modifier:
32985 parallel | task | taskloop | target data | target | target update
32986 | target enter data | target exit data
32989 directive-name-modifier:
32990 ... | simd | cancel */
32993 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32997 enum tree_code if_modifier
= ERROR_MARK
;
32999 matching_parens parens
;
33000 if (!parens
.require_open (parser
))
33003 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33005 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33006 const char *p
= IDENTIFIER_POINTER (id
);
33009 if (strcmp ("cancel", p
) == 0)
33010 if_modifier
= VOID_CST
;
33011 else if (strcmp ("parallel", p
) == 0)
33012 if_modifier
= OMP_PARALLEL
;
33013 else if (strcmp ("simd", p
) == 0)
33014 if_modifier
= OMP_SIMD
;
33015 else if (strcmp ("task", p
) == 0)
33016 if_modifier
= OMP_TASK
;
33017 else if (strcmp ("taskloop", p
) == 0)
33018 if_modifier
= OMP_TASKLOOP
;
33019 else if (strcmp ("target", p
) == 0)
33021 if_modifier
= OMP_TARGET
;
33022 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
33024 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
33025 p
= IDENTIFIER_POINTER (id
);
33026 if (strcmp ("data", p
) == 0)
33027 if_modifier
= OMP_TARGET_DATA
;
33028 else if (strcmp ("update", p
) == 0)
33029 if_modifier
= OMP_TARGET_UPDATE
;
33030 else if (strcmp ("enter", p
) == 0)
33031 if_modifier
= OMP_TARGET_ENTER_DATA
;
33032 else if (strcmp ("exit", p
) == 0)
33033 if_modifier
= OMP_TARGET_EXIT_DATA
;
33034 if (if_modifier
!= OMP_TARGET
)
33039 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
33040 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
33042 if_modifier
= ERROR_MARK
;
33044 if (if_modifier
== OMP_TARGET_ENTER_DATA
33045 || if_modifier
== OMP_TARGET_EXIT_DATA
)
33047 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
33049 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
33050 p
= IDENTIFIER_POINTER (id
);
33051 if (strcmp ("data", p
) == 0)
33057 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
33058 error_at (loc
, "expected %<data%>");
33059 if_modifier
= ERROR_MARK
;
33064 if (if_modifier
!= ERROR_MARK
)
33066 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
33069 cp_lexer_consume_token (parser
->lexer
);
33076 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
33077 error_at (loc
, "expected %<:%>");
33079 if_modifier
= ERROR_MARK
;
33084 t
= cp_parser_assignment_expression (parser
);
33086 if (t
== error_mark_node
33087 || !parens
.require_close (parser
))
33088 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33089 /*or_comma=*/false,
33090 /*consume_paren=*/true);
33092 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33093 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
33095 if (if_modifier
!= ERROR_MARK
33096 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33098 const char *p
= NULL
;
33099 switch (if_modifier
)
33101 case VOID_CST
: p
= "cancel"; break;
33102 case OMP_PARALLEL
: p
= "parallel"; break;
33103 case OMP_SIMD
: p
= "simd"; break;
33104 case OMP_TASK
: p
= "task"; break;
33105 case OMP_TASKLOOP
: p
= "taskloop"; break;
33106 case OMP_TARGET_DATA
: p
= "target data"; break;
33107 case OMP_TARGET
: p
= "target"; break;
33108 case OMP_TARGET_UPDATE
: p
= "target update"; break;
33109 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
33110 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
33111 default: gcc_unreachable ();
33113 error_at (location
, "too many %<if%> clauses with %qs modifier",
33117 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33120 error_at (location
, "too many %<if%> clauses");
33122 error_at (location
, "too many %<if%> clauses without modifier");
33125 else if (if_modifier
== ERROR_MARK
33126 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
33128 error_at (location
, "if any %<if%> clause has modifier, then all "
33129 "%<if%> clauses have to use modifier");
33134 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
33135 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
33136 OMP_CLAUSE_IF_EXPR (c
) = t
;
33137 OMP_CLAUSE_CHAIN (c
) = list
;
33146 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
33147 tree list
, location_t location
)
33151 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
33154 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
33155 OMP_CLAUSE_CHAIN (c
) = list
;
33163 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
33164 tree list
, location_t location
)
33168 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
33170 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
33171 OMP_CLAUSE_CHAIN (c
) = list
;
33176 num_threads ( expression ) */
33179 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
33180 location_t location
)
33184 matching_parens parens
;
33185 if (!parens
.require_open (parser
))
33188 t
= cp_parser_assignment_expression (parser
);
33190 if (t
== error_mark_node
33191 || !parens
.require_close (parser
))
33192 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33193 /*or_comma=*/false,
33194 /*consume_paren=*/true);
33196 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
33197 "num_threads", location
);
33199 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
33200 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
33201 OMP_CLAUSE_CHAIN (c
) = list
;
33207 num_tasks ( expression ) */
33210 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
33211 location_t location
)
33215 matching_parens parens
;
33216 if (!parens
.require_open (parser
))
33219 t
= cp_parser_assignment_expression (parser
);
33221 if (t
== error_mark_node
33222 || !parens
.require_close (parser
))
33223 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33224 /*or_comma=*/false,
33225 /*consume_paren=*/true);
33227 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
33228 "num_tasks", location
);
33230 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
33231 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
33232 OMP_CLAUSE_CHAIN (c
) = list
;
33238 grainsize ( expression ) */
33241 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
33242 location_t location
)
33246 matching_parens parens
;
33247 if (!parens
.require_open (parser
))
33250 t
= cp_parser_assignment_expression (parser
);
33252 if (t
== error_mark_node
33253 || !parens
.require_close (parser
))
33254 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33255 /*or_comma=*/false,
33256 /*consume_paren=*/true);
33258 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
33259 "grainsize", location
);
33261 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
33262 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
33263 OMP_CLAUSE_CHAIN (c
) = list
;
33269 priority ( expression ) */
33272 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
33273 location_t location
)
33277 matching_parens parens
;
33278 if (!parens
.require_open (parser
))
33281 t
= cp_parser_assignment_expression (parser
);
33283 if (t
== error_mark_node
33284 || !parens
.require_close (parser
))
33285 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33286 /*or_comma=*/false,
33287 /*consume_paren=*/true);
33289 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
33290 "priority", location
);
33292 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
33293 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
33294 OMP_CLAUSE_CHAIN (c
) = list
;
33300 hint ( expression ) */
33303 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
33307 matching_parens parens
;
33308 if (!parens
.require_open (parser
))
33311 t
= cp_parser_assignment_expression (parser
);
33313 if (t
== error_mark_node
33314 || !parens
.require_close (parser
))
33315 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33316 /*or_comma=*/false,
33317 /*consume_paren=*/true);
33319 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
33321 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
33322 OMP_CLAUSE_HINT_EXPR (c
) = t
;
33323 OMP_CLAUSE_CHAIN (c
) = list
;
33329 defaultmap ( tofrom : scalar )
33332 defaultmap ( implicit-behavior [ : variable-category ] ) */
33335 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
33336 location_t location
)
33340 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33341 enum omp_clause_defaultmap_kind category
33342 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
33344 matching_parens parens
;
33345 if (!parens
.require_open (parser
))
33348 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
33350 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33353 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
33354 "%<tofrom%>, %<firstprivate%>, %<none%> "
33360 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33361 p
= IDENTIFIER_POINTER (id
);
33367 if (strcmp ("alloc", p
) == 0)
33368 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
33370 goto invalid_behavior
;
33374 if (strcmp ("default", p
) == 0)
33375 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33377 goto invalid_behavior
;
33381 if (strcmp ("firstprivate", p
) == 0)
33382 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
33383 else if (strcmp ("from", p
) == 0)
33384 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
33386 goto invalid_behavior
;
33390 if (strcmp ("none", p
) == 0)
33391 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
33393 goto invalid_behavior
;
33397 if (strcmp ("tofrom", p
) == 0)
33398 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
33399 else if (strcmp ("to", p
) == 0)
33400 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
33402 goto invalid_behavior
;
33406 goto invalid_behavior
;
33408 cp_lexer_consume_token (parser
->lexer
);
33410 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33412 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33415 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33418 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
33422 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33423 p
= IDENTIFIER_POINTER (id
);
33428 if (strcmp ("aggregate", p
) == 0)
33429 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
33431 goto invalid_category
;
33435 if (strcmp ("pointer", p
) == 0)
33436 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
33438 goto invalid_category
;
33442 if (strcmp ("scalar", p
) == 0)
33443 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
33445 goto invalid_category
;
33449 goto invalid_category
;
33452 cp_lexer_consume_token (parser
->lexer
);
33454 if (!parens
.require_close (parser
))
33457 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33458 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
33459 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33460 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
33461 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
33462 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
33464 enum omp_clause_defaultmap_kind cat
= category
;
33465 location_t loc
= OMP_CLAUSE_LOCATION (c
);
33466 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
33467 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
33471 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
33474 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
33477 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
33480 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
33484 gcc_unreachable ();
33487 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
33490 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
33495 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
33496 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
33497 OMP_CLAUSE_CHAIN (c
) = list
;
33501 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33502 /*or_comma=*/false,
33503 /*consume_paren=*/true);
33511 ordered ( constant-expression ) */
33514 cp_parser_omp_clause_ordered (cp_parser
*parser
,
33515 tree list
, location_t location
)
33517 tree c
, num
= NULL_TREE
;
33520 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
33521 "ordered", location
);
33523 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33525 matching_parens parens
;
33526 parens
.consume_open (parser
);
33528 num
= cp_parser_constant_expression (parser
);
33530 if (!parens
.require_close (parser
))
33531 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33532 /*or_comma=*/false,
33533 /*consume_paren=*/true);
33535 if (num
== error_mark_node
)
33537 num
= fold_non_dependent_expr (num
);
33538 if (!tree_fits_shwi_p (num
)
33539 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33540 || (n
= tree_to_shwi (num
)) <= 0
33543 error_at (location
,
33544 "ordered argument needs positive constant integer "
33550 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
33551 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
33552 OMP_CLAUSE_CHAIN (c
) = list
;
33557 reduction ( reduction-operator : variable-list )
33559 reduction-operator:
33560 One of: + * - & ^ | && ||
33564 reduction-operator:
33565 One of: + * - & ^ | && || min max
33569 reduction-operator:
33570 One of: + * - & ^ | && ||
33574 reduction ( reduction-modifier, reduction-operator : variable-list )
33575 in_reduction ( reduction-operator : variable-list )
33576 task_reduction ( reduction-operator : variable-list ) */
33579 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
33580 bool is_omp
, tree list
)
33582 enum tree_code code
= ERROR_MARK
;
33583 tree nlist
, c
, id
= NULL_TREE
;
33585 bool inscan
= false;
33587 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33590 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
33592 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
33593 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33595 cp_lexer_consume_token (parser
->lexer
);
33596 cp_lexer_consume_token (parser
->lexer
);
33598 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33599 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33601 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33602 const char *p
= IDENTIFIER_POINTER (id
);
33603 if (strcmp (p
, "task") == 0)
33605 else if (strcmp (p
, "inscan") == 0)
33608 sorry ("%<inscan%> modifier on %<reduction%> clause "
33609 "not supported yet");
33611 if (task
|| inscan
)
33613 cp_lexer_consume_token (parser
->lexer
);
33614 cp_lexer_consume_token (parser
->lexer
);
33619 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33621 case CPP_PLUS
: code
= PLUS_EXPR
; break;
33622 case CPP_MULT
: code
= MULT_EXPR
; break;
33623 case CPP_MINUS
: code
= MINUS_EXPR
; break;
33624 case CPP_AND
: code
= BIT_AND_EXPR
; break;
33625 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
33626 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
33627 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
33628 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
33632 if (code
!= ERROR_MARK
)
33633 cp_lexer_consume_token (parser
->lexer
);
33636 bool saved_colon_corrects_to_scope_p
;
33637 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33638 parser
->colon_corrects_to_scope_p
= false;
33639 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
33640 /*check_dependency_p=*/true,
33641 /*template_p=*/NULL
,
33642 /*declarator_p=*/false,
33643 /*optional_p=*/false);
33644 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33645 if (identifier_p (id
))
33647 const char *p
= IDENTIFIER_POINTER (id
);
33649 if (strcmp (p
, "min") == 0)
33651 else if (strcmp (p
, "max") == 0)
33653 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
33655 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
33657 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
33659 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
33660 code
= BIT_AND_EXPR
;
33661 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
33662 code
= BIT_IOR_EXPR
;
33663 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
33664 code
= BIT_XOR_EXPR
;
33665 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
33666 code
= TRUTH_ANDIF_EXPR
;
33667 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
33668 code
= TRUTH_ORIF_EXPR
;
33669 id
= omp_reduction_id (code
, id
, NULL_TREE
);
33670 tree scope
= parser
->scope
;
33672 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
33673 parser
->scope
= NULL_TREE
;
33674 parser
->qualifying_scope
= NULL_TREE
;
33675 parser
->object_scope
= NULL_TREE
;
33679 error ("invalid reduction-identifier");
33681 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33682 /*or_comma=*/false,
33683 /*consume_paren=*/true);
33688 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33691 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
33693 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33695 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
33697 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
33699 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
33700 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
33707 schedule ( schedule-kind )
33708 schedule ( schedule-kind , expression )
33711 static | dynamic | guided | runtime | auto
33714 schedule ( schedule-modifier : schedule-kind )
33715 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33723 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
33726 int modifiers
= 0, nmodifiers
= 0;
33728 matching_parens parens
;
33729 if (!parens
.require_open (parser
))
33732 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
33734 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33736 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33737 const char *p
= IDENTIFIER_POINTER (id
);
33738 if (strcmp ("simd", p
) == 0)
33739 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
33740 else if (strcmp ("monotonic", p
) == 0)
33741 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
33742 else if (strcmp ("nonmonotonic", p
) == 0)
33743 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
33746 cp_lexer_consume_token (parser
->lexer
);
33747 if (nmodifiers
++ == 0
33748 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33749 cp_lexer_consume_token (parser
->lexer
);
33752 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33757 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33759 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33760 const char *p
= IDENTIFIER_POINTER (id
);
33765 if (strcmp ("dynamic", p
) != 0)
33767 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
33771 if (strcmp ("guided", p
) != 0)
33773 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
33777 if (strcmp ("runtime", p
) != 0)
33779 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
33786 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33787 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
33788 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
33789 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
33792 cp_lexer_consume_token (parser
->lexer
);
33794 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
33795 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33796 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33797 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33799 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33804 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33807 cp_lexer_consume_token (parser
->lexer
);
33809 token
= cp_lexer_peek_token (parser
->lexer
);
33810 t
= cp_parser_assignment_expression (parser
);
33812 if (t
== error_mark_node
)
33814 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
33815 error_at (token
->location
, "schedule %<runtime%> does not take "
33816 "a %<chunk_size%> parameter");
33817 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
33818 error_at (token
->location
, "schedule %<auto%> does not take "
33819 "a %<chunk_size%> parameter");
33821 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
33823 if (!parens
.require_close (parser
))
33826 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33829 OMP_CLAUSE_SCHEDULE_KIND (c
)
33830 = (enum omp_clause_schedule_kind
)
33831 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
33833 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
33834 OMP_CLAUSE_CHAIN (c
) = list
;
33838 cp_parser_error (parser
, "invalid schedule kind");
33840 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33841 /*or_comma=*/false,
33842 /*consume_paren=*/true);
33850 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
33851 tree list
, location_t location
)
33855 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
33857 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
33858 OMP_CLAUSE_CHAIN (c
) = list
;
33867 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
33868 tree list
, location_t location
)
33870 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33871 tree c
= build_omp_clause (location
, code
);
33872 OMP_CLAUSE_CHAIN (c
) = list
;
33883 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33884 enum omp_clause_code code
,
33885 tree list
, location_t location
)
33887 tree c
= build_omp_clause (location
, code
);
33888 OMP_CLAUSE_CHAIN (c
) = list
;
33896 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33897 tree list
, location_t location
)
33899 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33900 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33901 OMP_CLAUSE_CHAIN (c
) = list
;
33910 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33911 enum omp_clause_code code
,
33912 tree list
, location_t location
)
33914 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33915 tree c
= build_omp_clause (location
, code
);
33916 OMP_CLAUSE_CHAIN (c
) = list
;
33921 num_teams ( expression ) */
33924 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33925 location_t location
)
33929 matching_parens parens
;
33930 if (!parens
.require_open (parser
))
33933 t
= cp_parser_assignment_expression (parser
);
33935 if (t
== error_mark_node
33936 || !parens
.require_close (parser
))
33937 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33938 /*or_comma=*/false,
33939 /*consume_paren=*/true);
33941 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33942 "num_teams", location
);
33944 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33945 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33946 OMP_CLAUSE_CHAIN (c
) = list
;
33952 thread_limit ( expression ) */
33955 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33956 location_t location
)
33960 matching_parens parens
;
33961 if (!parens
.require_open (parser
))
33964 t
= cp_parser_assignment_expression (parser
);
33966 if (t
== error_mark_node
33967 || !parens
.require_close (parser
))
33968 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33969 /*or_comma=*/false,
33970 /*consume_paren=*/true);
33972 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33973 "thread_limit", location
);
33975 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33976 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33977 OMP_CLAUSE_CHAIN (c
) = list
;
33983 aligned ( variable-list )
33984 aligned ( variable-list : constant-expression ) */
33987 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33989 tree nlist
, c
, alignment
= NULL_TREE
;
33992 matching_parens parens
;
33993 if (!parens
.require_open (parser
))
33996 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
34001 alignment
= cp_parser_constant_expression (parser
);
34003 if (!parens
.require_close (parser
))
34004 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34005 /*or_comma=*/false,
34006 /*consume_paren=*/true);
34008 if (alignment
== error_mark_node
)
34009 alignment
= NULL_TREE
;
34012 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34013 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
34019 lastprivate ( variable-list )
34022 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34025 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
34027 bool conditional
= false;
34029 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34032 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34033 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
34035 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34036 const char *p
= IDENTIFIER_POINTER (id
);
34038 if (strcmp ("conditional", p
) == 0)
34040 conditional
= true;
34041 cp_lexer_consume_token (parser
->lexer
);
34042 cp_lexer_consume_token (parser
->lexer
);
34046 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
34050 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34051 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
34056 linear ( variable-list )
34057 linear ( variable-list : expression )
34060 linear ( modifier ( variable-list ) )
34061 linear ( modifier ( variable-list ) : expression ) */
34064 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
34067 tree nlist
, c
, step
= integer_one_node
;
34069 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
34071 matching_parens parens
;
34072 if (!parens
.require_open (parser
))
34075 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34077 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34078 const char *p
= IDENTIFIER_POINTER (id
);
34080 if (strcmp ("ref", p
) == 0)
34081 kind
= OMP_CLAUSE_LINEAR_REF
;
34082 else if (strcmp ("val", p
) == 0)
34083 kind
= OMP_CLAUSE_LINEAR_VAL
;
34084 else if (strcmp ("uval", p
) == 0)
34085 kind
= OMP_CLAUSE_LINEAR_UVAL
;
34086 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
34087 cp_lexer_consume_token (parser
->lexer
);
34089 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
34092 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
34093 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
34097 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
34098 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
34100 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
34101 else if (!parens
.require_close (parser
))
34102 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34103 /*or_comma=*/false,
34104 /*consume_paren=*/true);
34111 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34112 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
34114 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34115 cp_parser_parse_tentatively (parser
);
34116 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
34117 /*check_dependency_p=*/true,
34118 /*template_p=*/NULL
,
34119 /*declarator_p=*/false,
34120 /*optional_p=*/false);
34121 if (step
!= error_mark_node
)
34122 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
34123 if (step
== error_mark_node
)
34126 cp_parser_abort_tentative_parse (parser
);
34128 else if (!cp_parser_parse_definitely (parser
))
34132 step
= cp_parser_assignment_expression (parser
);
34134 if (!parens
.require_close (parser
))
34135 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34136 /*or_comma=*/false,
34137 /*consume_paren=*/true);
34139 if (step
== error_mark_node
)
34143 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34145 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
34146 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
34153 safelen ( constant-expression ) */
34156 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
34157 location_t location
)
34161 matching_parens parens
;
34162 if (!parens
.require_open (parser
))
34165 t
= cp_parser_constant_expression (parser
);
34167 if (t
== error_mark_node
34168 || !parens
.require_close (parser
))
34169 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34170 /*or_comma=*/false,
34171 /*consume_paren=*/true);
34173 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
34175 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
34176 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
34177 OMP_CLAUSE_CHAIN (c
) = list
;
34183 simdlen ( constant-expression ) */
34186 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
34187 location_t location
)
34191 matching_parens parens
;
34192 if (!parens
.require_open (parser
))
34195 t
= cp_parser_constant_expression (parser
);
34197 if (t
== error_mark_node
34198 || !parens
.require_close (parser
))
34199 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34200 /*or_comma=*/false,
34201 /*consume_paren=*/true);
34203 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
34205 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
34206 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
34207 OMP_CLAUSE_CHAIN (c
) = list
;
34214 identifier [+/- integer]
34215 vec , identifier [+/- integer]
34219 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
34224 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34226 cp_parser_error (parser
, "expected identifier");
34230 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34232 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34233 tree t
, identifier
= cp_parser_identifier (parser
);
34234 tree addend
= NULL
;
34236 if (identifier
== error_mark_node
)
34237 t
= error_mark_node
;
34240 t
= cp_parser_lookup_name_simple
34241 (parser
, identifier
,
34242 cp_lexer_peek_token (parser
->lexer
)->location
);
34243 if (t
== error_mark_node
)
34244 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
34249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
34251 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
34253 addend
= integer_zero_node
;
34254 goto add_to_vector
;
34256 cp_lexer_consume_token (parser
->lexer
);
34258 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
34260 cp_parser_error (parser
, "expected integer");
34264 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34265 if (TREE_CODE (addend
) != INTEGER_CST
)
34267 cp_parser_error (parser
, "expected integer");
34270 cp_lexer_consume_token (parser
->lexer
);
34273 if (t
!= error_mark_node
)
34275 vec
= tree_cons (addend
, t
, vec
);
34277 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
34280 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
34283 cp_lexer_consume_token (parser
->lexer
);
34286 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
34288 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
34289 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
34290 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
34291 OMP_CLAUSE_CHAIN (u
) = list
;
34298 iterators ( iterators-definition )
34300 iterators-definition:
34302 iterator-specifier , iterators-definition
34304 iterator-specifier:
34305 identifier = range-specification
34306 iterator-type identifier = range-specification
34308 range-specification:
34310 begin : end : step */
34313 cp_parser_omp_iterators (cp_parser
*parser
)
34315 tree ret
= NULL_TREE
, *last
= &ret
;
34316 cp_lexer_consume_token (parser
->lexer
);
34318 matching_parens parens
;
34319 if (!parens
.require_open (parser
))
34320 return error_mark_node
;
34322 bool saved_colon_corrects_to_scope_p
34323 = parser
->colon_corrects_to_scope_p
;
34324 bool saved_colon_doesnt_start_class_def_p
34325 = parser
->colon_doesnt_start_class_def_p
;
34330 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34331 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
34332 iter_type
= integer_type_node
;
34335 const char *saved_message
34336 = parser
->type_definition_forbidden_message
;
34337 parser
->type_definition_forbidden_message
34338 = G_("types may not be defined in iterator type");
34340 iter_type
= cp_parser_type_id (parser
);
34342 parser
->type_definition_forbidden_message
= saved_message
;
34345 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34346 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34348 cp_parser_error (parser
, "expected identifier");
34352 tree id
= cp_parser_identifier (parser
);
34353 if (id
== error_mark_node
)
34356 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34359 parser
->colon_corrects_to_scope_p
= false;
34360 parser
->colon_doesnt_start_class_def_p
= true;
34361 tree begin
= cp_parser_assignment_expression (parser
);
34363 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34366 tree end
= cp_parser_assignment_expression (parser
);
34368 tree step
= integer_one_node
;
34369 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
34371 cp_lexer_consume_token (parser
->lexer
);
34372 step
= cp_parser_assignment_expression (parser
);
34375 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
34376 DECL_ARTIFICIAL (iter_var
) = 1;
34377 DECL_CONTEXT (iter_var
) = current_function_decl
;
34378 pushdecl (iter_var
);
34380 *last
= make_tree_vec (6);
34381 TREE_VEC_ELT (*last
, 0) = iter_var
;
34382 TREE_VEC_ELT (*last
, 1) = begin
;
34383 TREE_VEC_ELT (*last
, 2) = end
;
34384 TREE_VEC_ELT (*last
, 3) = step
;
34385 last
= &TREE_CHAIN (*last
);
34387 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34389 cp_lexer_consume_token (parser
->lexer
);
34396 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34397 parser
->colon_doesnt_start_class_def_p
34398 = saved_colon_doesnt_start_class_def_p
;
34400 if (!parens
.require_close (parser
))
34401 cp_parser_skip_to_closing_parenthesis (parser
,
34402 /*recovering=*/true,
34403 /*or_comma=*/false,
34404 /*consume_paren=*/true);
34406 return ret
? ret
: error_mark_node
;
34410 depend ( depend-kind : variable-list )
34418 depend ( sink : vec )
34421 depend ( depend-modifier , depend-kind: variable-list )
34424 in | out | inout | mutexinoutset | depobj
34427 iterator ( iterators-definition ) */
34430 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
34432 tree nlist
, c
, iterators
= NULL_TREE
;
34433 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
34435 matching_parens parens
;
34436 if (!parens
.require_open (parser
))
34441 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34444 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34445 const char *p
= IDENTIFIER_POINTER (id
);
34447 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
34449 begin_scope (sk_omp
, NULL
);
34450 iterators
= cp_parser_omp_iterators (parser
);
34451 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
34454 if (strcmp ("in", p
) == 0)
34455 kind
= OMP_CLAUSE_DEPEND_IN
;
34456 else if (strcmp ("inout", p
) == 0)
34457 kind
= OMP_CLAUSE_DEPEND_INOUT
;
34458 else if (strcmp ("mutexinoutset", p
) == 0)
34459 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
34460 else if (strcmp ("out", p
) == 0)
34461 kind
= OMP_CLAUSE_DEPEND_OUT
;
34462 else if (strcmp ("depobj", p
) == 0)
34463 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
34464 else if (strcmp ("sink", p
) == 0)
34465 kind
= OMP_CLAUSE_DEPEND_SINK
;
34466 else if (strcmp ("source", p
) == 0)
34467 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
34474 cp_lexer_consume_token (parser
->lexer
);
34477 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
34479 poplevel (0, 1, 0);
34480 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
34481 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
34482 iterators
= NULL_TREE
;
34485 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
34487 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
34488 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34489 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
34490 OMP_CLAUSE_CHAIN (c
) = list
;
34491 if (!parens
.require_close (parser
))
34492 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34493 /*or_comma=*/false,
34494 /*consume_paren=*/true);
34498 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34501 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
34502 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
34505 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
34510 tree block
= poplevel (1, 1, 0);
34511 if (iterators
== error_mark_node
)
34512 iterators
= NULL_TREE
;
34514 TREE_VEC_ELT (iterators
, 5) = block
;
34517 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34519 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34521 OMP_CLAUSE_DECL (c
)
34522 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
34528 cp_parser_error (parser
, "invalid depend kind");
34531 poplevel (0, 1, 0);
34532 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34533 /*or_comma=*/false,
34534 /*consume_paren=*/true);
34539 map ( map-kind : variable-list )
34540 map ( variable-list )
34543 alloc | to | from | tofrom
34547 alloc | to | from | tofrom | release | delete
34549 map ( always [,] map-kind: variable-list ) */
34552 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
34555 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
34556 bool always
= false;
34558 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34561 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34563 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34564 const char *p
= IDENTIFIER_POINTER (id
);
34566 if (strcmp ("always", p
) == 0)
34569 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
34571 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
34572 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
34574 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
34578 cp_lexer_consume_token (parser
->lexer
);
34580 cp_lexer_consume_token (parser
->lexer
);
34585 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34586 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34588 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34589 const char *p
= IDENTIFIER_POINTER (id
);
34591 if (strcmp ("alloc", p
) == 0)
34592 kind
= GOMP_MAP_ALLOC
;
34593 else if (strcmp ("to", p
) == 0)
34594 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
34595 else if (strcmp ("from", p
) == 0)
34596 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
34597 else if (strcmp ("tofrom", p
) == 0)
34598 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
34599 else if (strcmp ("release", p
) == 0)
34600 kind
= GOMP_MAP_RELEASE
;
34603 cp_parser_error (parser
, "invalid map kind");
34604 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34605 /*or_comma=*/false,
34606 /*consume_paren=*/true);
34609 cp_lexer_consume_token (parser
->lexer
);
34610 cp_lexer_consume_token (parser
->lexer
);
34612 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
34613 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34615 kind
= GOMP_MAP_DELETE
;
34616 cp_lexer_consume_token (parser
->lexer
);
34617 cp_lexer_consume_token (parser
->lexer
);
34620 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
34623 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34624 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
34630 device ( expression ) */
34633 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
34634 location_t location
)
34638 matching_parens parens
;
34639 if (!parens
.require_open (parser
))
34642 t
= cp_parser_assignment_expression (parser
);
34644 if (t
== error_mark_node
34645 || !parens
.require_close (parser
))
34646 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34647 /*or_comma=*/false,
34648 /*consume_paren=*/true);
34650 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
34651 "device", location
);
34653 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
34654 OMP_CLAUSE_DEVICE_ID (c
) = t
;
34655 OMP_CLAUSE_CHAIN (c
) = list
;
34661 dist_schedule ( static )
34662 dist_schedule ( static , expression ) */
34665 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
34666 location_t location
)
34670 matching_parens parens
;
34671 if (!parens
.require_open (parser
))
34674 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
34676 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
34678 cp_lexer_consume_token (parser
->lexer
);
34680 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34682 cp_lexer_consume_token (parser
->lexer
);
34684 t
= cp_parser_assignment_expression (parser
);
34686 if (t
== error_mark_node
)
34688 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
34690 if (!parens
.require_close (parser
))
34693 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34696 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
34698 OMP_CLAUSE_CHAIN (c
) = list
;
34702 cp_parser_error (parser
, "invalid dist_schedule kind");
34704 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34705 /*or_comma=*/false,
34706 /*consume_paren=*/true);
34711 proc_bind ( proc-bind-kind )
34714 master | close | spread */
34717 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
34718 location_t location
)
34721 enum omp_clause_proc_bind_kind kind
;
34723 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34726 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34728 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34729 const char *p
= IDENTIFIER_POINTER (id
);
34731 if (strcmp ("master", p
) == 0)
34732 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
34733 else if (strcmp ("close", p
) == 0)
34734 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
34735 else if (strcmp ("spread", p
) == 0)
34736 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
34743 cp_lexer_consume_token (parser
->lexer
);
34744 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34747 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
34748 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
34750 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
34751 OMP_CLAUSE_CHAIN (c
) = list
;
34755 cp_parser_error (parser
, "invalid depend kind");
34757 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34758 /*or_comma=*/false,
34759 /*consume_paren=*/true);
34764 async [( int-expr )] */
34767 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
34770 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34772 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
34774 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34776 matching_parens parens
;
34777 parens
.consume_open (parser
);
34779 t
= cp_parser_expression (parser
);
34780 if (t
== error_mark_node
34781 || !parens
.require_close (parser
))
34782 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34783 /*or_comma=*/false,
34784 /*consume_paren=*/true);
34787 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
34789 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
34790 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
34791 OMP_CLAUSE_CHAIN (c
) = list
;
34797 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34798 is a bitmask in MASK. Return the list of clauses found. */
34801 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34802 const char *where
, cp_token
*pragma_tok
,
34803 bool finish_p
= true)
34805 tree clauses
= NULL
;
34808 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34811 pragma_omp_clause c_kind
;
34812 omp_clause_code code
;
34813 const char *c_name
;
34814 tree prev
= clauses
;
34816 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34817 cp_lexer_consume_token (parser
->lexer
);
34819 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
34820 c_kind
= cp_parser_omp_clause_name (parser
);
34824 case PRAGMA_OACC_CLAUSE_ASYNC
:
34825 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
34828 case PRAGMA_OACC_CLAUSE_AUTO
:
34829 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
34833 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
34834 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
34835 c_name
= "collapse";
34837 case PRAGMA_OACC_CLAUSE_COPY
:
34838 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34841 case PRAGMA_OACC_CLAUSE_COPYIN
:
34842 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34845 case PRAGMA_OACC_CLAUSE_COPYOUT
:
34846 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34847 c_name
= "copyout";
34849 case PRAGMA_OACC_CLAUSE_CREATE
:
34850 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34853 case PRAGMA_OACC_CLAUSE_DELETE
:
34854 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34857 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34858 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
34859 c_name
= "default";
34861 case PRAGMA_OACC_CLAUSE_DEVICE
:
34862 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34865 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
34866 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
34867 c_name
= "deviceptr";
34869 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
34870 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34871 c_name
= "device_resident";
34873 case PRAGMA_OACC_CLAUSE_FINALIZE
:
34874 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
34876 c_name
= "finalize";
34878 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
34879 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34881 c_name
= "firstprivate";
34883 case PRAGMA_OACC_CLAUSE_GANG
:
34885 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
34888 case PRAGMA_OACC_CLAUSE_HOST
:
34889 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34892 case PRAGMA_OACC_CLAUSE_IF
:
34893 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
34896 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
34897 clauses
= cp_parser_oacc_simple_clause (parser
,
34898 OMP_CLAUSE_IF_PRESENT
,
34900 c_name
= "if_present";
34902 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
34903 clauses
= cp_parser_oacc_simple_clause (parser
,
34904 OMP_CLAUSE_INDEPENDENT
,
34906 c_name
= "independent";
34908 case PRAGMA_OACC_CLAUSE_LINK
:
34909 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34912 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
34913 code
= OMP_CLAUSE_NUM_GANGS
;
34914 c_name
= "num_gangs";
34915 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34918 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
34919 c_name
= "num_workers";
34920 code
= OMP_CLAUSE_NUM_WORKERS
;
34921 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34924 case PRAGMA_OACC_CLAUSE_PRESENT
:
34925 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34926 c_name
= "present";
34928 case PRAGMA_OACC_CLAUSE_PRIVATE
:
34929 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34931 c_name
= "private";
34933 case PRAGMA_OACC_CLAUSE_REDUCTION
:
34935 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
34937 c_name
= "reduction";
34939 case PRAGMA_OACC_CLAUSE_SEQ
:
34940 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
34944 case PRAGMA_OACC_CLAUSE_TILE
:
34945 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
34948 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
34949 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34951 c_name
= "use_device";
34953 case PRAGMA_OACC_CLAUSE_VECTOR
:
34955 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
34958 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
34959 c_name
= "vector_length";
34960 code
= OMP_CLAUSE_VECTOR_LENGTH
;
34961 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34964 case PRAGMA_OACC_CLAUSE_WAIT
:
34965 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
34968 case PRAGMA_OACC_CLAUSE_WORKER
:
34970 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
34974 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
34980 if (((mask
>> c_kind
) & 1) == 0)
34982 /* Remove the invalid clause(s) from the list to avoid
34983 confusing the rest of the compiler. */
34985 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
34990 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34993 return finish_omp_clauses (clauses
, C_ORT_ACC
);
34998 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34999 is a bitmask in MASK. Return the list of clauses found; the result
35000 of clause default goes in *pdefault. */
35003 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
35004 const char *where
, cp_token
*pragma_tok
,
35005 bool finish_p
= true)
35007 tree clauses
= NULL
;
35009 cp_token
*token
= NULL
;
35011 /* Don't create location wrapper nodes within OpenMP clauses. */
35012 auto_suppress_location_wrappers sentinel
;
35014 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35016 pragma_omp_clause c_kind
;
35017 const char *c_name
;
35018 tree prev
= clauses
;
35020 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35021 cp_lexer_consume_token (parser
->lexer
);
35023 token
= cp_lexer_peek_token (parser
->lexer
);
35024 c_kind
= cp_parser_omp_clause_name (parser
);
35028 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
35029 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
35031 c_name
= "collapse";
35033 case PRAGMA_OMP_CLAUSE_COPYIN
:
35034 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
35037 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
35038 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
35040 c_name
= "copyprivate";
35042 case PRAGMA_OMP_CLAUSE_DEFAULT
:
35043 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
35044 token
->location
, false);
35045 c_name
= "default";
35047 case PRAGMA_OMP_CLAUSE_FINAL
:
35048 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
35051 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
35052 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
35054 c_name
= "firstprivate";
35056 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
35057 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
35059 c_name
= "grainsize";
35061 case PRAGMA_OMP_CLAUSE_HINT
:
35062 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
35066 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
35067 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
35069 c_name
= "defaultmap";
35071 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
35072 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
35074 c_name
= "use_device_ptr";
35076 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
35077 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
35079 c_name
= "is_device_ptr";
35081 case PRAGMA_OMP_CLAUSE_IF
:
35082 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
35086 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
35088 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
35090 c_name
= "in_reduction";
35092 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
35093 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
35094 c_name
= "lastprivate";
35096 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
35097 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
35099 c_name
= "mergeable";
35101 case PRAGMA_OMP_CLAUSE_NOWAIT
:
35102 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
35105 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
35106 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
35108 c_name
= "num_tasks";
35110 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
35111 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
35113 c_name
= "num_threads";
35115 case PRAGMA_OMP_CLAUSE_ORDERED
:
35116 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
35118 c_name
= "ordered";
35120 case PRAGMA_OMP_CLAUSE_PRIORITY
:
35121 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
35123 c_name
= "priority";
35125 case PRAGMA_OMP_CLAUSE_PRIVATE
:
35126 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
35128 c_name
= "private";
35130 case PRAGMA_OMP_CLAUSE_REDUCTION
:
35132 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
35134 c_name
= "reduction";
35136 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
35137 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
35139 c_name
= "schedule";
35141 case PRAGMA_OMP_CLAUSE_SHARED
:
35142 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
35146 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
35148 = cp_parser_omp_clause_reduction (parser
,
35149 OMP_CLAUSE_TASK_REDUCTION
,
35151 c_name
= "task_reduction";
35153 case PRAGMA_OMP_CLAUSE_UNTIED
:
35154 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
35158 case PRAGMA_OMP_CLAUSE_INBRANCH
:
35159 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
35160 clauses
, token
->location
);
35161 c_name
= "inbranch";
35163 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
35164 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
35166 c_name
= "nontemporal";
35168 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
35169 clauses
= cp_parser_omp_clause_branch (parser
,
35170 OMP_CLAUSE_NOTINBRANCH
,
35171 clauses
, token
->location
);
35172 c_name
= "notinbranch";
35174 case PRAGMA_OMP_CLAUSE_PARALLEL
:
35175 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
35176 clauses
, token
->location
);
35177 c_name
= "parallel";
35181 error_at (token
->location
, "%qs must be the first clause of %qs",
35186 case PRAGMA_OMP_CLAUSE_FOR
:
35187 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
35188 clauses
, token
->location
);
35191 goto clause_not_first
;
35193 case PRAGMA_OMP_CLAUSE_SECTIONS
:
35194 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
35195 clauses
, token
->location
);
35196 c_name
= "sections";
35198 goto clause_not_first
;
35200 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
35201 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
35202 clauses
, token
->location
);
35203 c_name
= "taskgroup";
35205 goto clause_not_first
;
35207 case PRAGMA_OMP_CLAUSE_LINK
:
35208 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
35211 case PRAGMA_OMP_CLAUSE_TO
:
35212 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
35213 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
35216 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
35219 case PRAGMA_OMP_CLAUSE_FROM
:
35220 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
35223 case PRAGMA_OMP_CLAUSE_UNIFORM
:
35224 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
35226 c_name
= "uniform";
35228 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
35229 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
35231 c_name
= "num_teams";
35233 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
35234 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
35236 c_name
= "thread_limit";
35238 case PRAGMA_OMP_CLAUSE_ALIGNED
:
35239 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
35240 c_name
= "aligned";
35242 case PRAGMA_OMP_CLAUSE_LINEAR
:
35244 bool declare_simd
= false;
35245 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
35246 declare_simd
= true;
35247 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
35251 case PRAGMA_OMP_CLAUSE_DEPEND
:
35252 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
35256 case PRAGMA_OMP_CLAUSE_MAP
:
35257 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
35260 case PRAGMA_OMP_CLAUSE_DEVICE
:
35261 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
35265 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
35266 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
35268 c_name
= "dist_schedule";
35270 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
35271 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
35273 c_name
= "proc_bind";
35275 case PRAGMA_OMP_CLAUSE_SAFELEN
:
35276 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
35278 c_name
= "safelen";
35280 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
35281 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
35283 c_name
= "simdlen";
35285 case PRAGMA_OMP_CLAUSE_NOGROUP
:
35286 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
35288 c_name
= "nogroup";
35290 case PRAGMA_OMP_CLAUSE_THREADS
:
35292 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
35293 clauses
, token
->location
);
35294 c_name
= "threads";
35296 case PRAGMA_OMP_CLAUSE_SIMD
:
35298 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
35299 clauses
, token
->location
);
35303 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
35309 if (((mask
>> c_kind
) & 1) == 0)
35311 /* Remove the invalid clause(s) from the list to avoid
35312 confusing the rest of the compiler. */
35314 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
35318 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35321 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
35322 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
35324 return finish_omp_clauses (clauses
, C_ORT_OMP
);
35333 In practice, we're also interested in adding the statement to an
35334 outer node. So it is convenient if we work around the fact that
35335 cp_parser_statement calls add_stmt. */
35338 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
35340 unsigned save
= parser
->in_statement
;
35342 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35343 This preserves the "not within loop or switch" style error messages
35344 for nonsense cases like
35350 if (parser
->in_statement
)
35351 parser
->in_statement
= IN_OMP_BLOCK
;
35357 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
35359 parser
->in_statement
= save
;
35363 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
35365 tree stmt
= begin_omp_structured_block ();
35366 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
35368 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35370 cp_parser_end_omp_structured_block (parser
, save
);
35371 return finish_omp_structured_block (stmt
);
35375 # pragma omp atomic new-line
35379 x binop= expr | x++ | ++x | x-- | --x
35381 +, *, -, /, &, ^, |, <<, >>
35383 where x is an lvalue expression with scalar type.
35386 # pragma omp atomic new-line
35389 # pragma omp atomic read new-line
35392 # pragma omp atomic write new-line
35395 # pragma omp atomic update new-line
35398 # pragma omp atomic capture new-line
35401 # pragma omp atomic capture new-line
35409 expression-stmt | x = x binop expr
35411 v = expression-stmt
35413 { v = x; update-stmt; } | { update-stmt; v = x; }
35417 expression-stmt | x = x binop expr | x = expr binop x
35421 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35423 where x and v are lvalue expressions with scalar type. */
35426 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
35428 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
35429 tree rhs1
= NULL_TREE
, orig_lhs
;
35430 location_t loc
= pragma_tok
->location
;
35431 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
35432 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
35433 bool structured_block
= false;
35435 tree clauses
= NULL_TREE
;
35437 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35439 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35440 cp_lexer_consume_token (parser
->lexer
);
35444 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35446 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35447 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35448 const char *p
= IDENTIFIER_POINTER (id
);
35449 enum tree_code new_code
= ERROR_MARK
;
35450 enum omp_memory_order new_memory_order
35451 = OMP_MEMORY_ORDER_UNSPECIFIED
;
35453 if (!strcmp (p
, "read"))
35454 new_code
= OMP_ATOMIC_READ
;
35455 else if (!strcmp (p
, "write"))
35456 new_code
= NOP_EXPR
;
35457 else if (!strcmp (p
, "update"))
35458 new_code
= OMP_ATOMIC
;
35459 else if (!strcmp (p
, "capture"))
35460 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
35461 else if (!strcmp (p
, "seq_cst"))
35462 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35463 else if (!strcmp (p
, "acq_rel"))
35464 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35465 else if (!strcmp (p
, "release"))
35466 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35467 else if (!strcmp (p
, "acquire"))
35468 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35469 else if (!strcmp (p
, "relaxed"))
35470 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35471 else if (!strcmp (p
, "hint"))
35473 cp_lexer_consume_token (parser
->lexer
);
35474 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
35480 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
35481 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35482 "%<release%>, %<relaxed%> or %<hint%> clause");
35486 if (new_code
!= ERROR_MARK
)
35488 if (code
!= ERROR_MARK
)
35489 error_at (cloc
, "too many atomic clauses");
35493 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35495 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35496 error_at (cloc
, "too many memory order clauses");
35498 memory_order
= new_memory_order
;
35500 cp_lexer_consume_token (parser
->lexer
);
35506 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35508 if (code
== ERROR_MARK
)
35510 if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
35513 = (enum omp_requires
) (omp_requires_mask
35514 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
35515 switch ((enum omp_memory_order
)
35516 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
35518 case OMP_MEMORY_ORDER_UNSPECIFIED
:
35519 case OMP_MEMORY_ORDER_RELAXED
:
35520 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35522 case OMP_MEMORY_ORDER_SEQ_CST
:
35523 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35525 case OMP_MEMORY_ORDER_ACQ_REL
:
35528 case OMP_ATOMIC_READ
:
35529 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35531 case NOP_EXPR
: /* atomic write */
35533 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35536 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35541 gcc_unreachable ();
35547 case OMP_ATOMIC_READ
:
35548 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35549 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
35551 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
35552 "%<acq_rel%> or %<release%> clauses");
35553 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35556 case NOP_EXPR
: /* atomic write */
35557 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35558 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35560 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
35561 "%<acq_rel%> or %<acquire%> clauses");
35562 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35566 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35567 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35569 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
35570 "%<acq_rel%> or %<acquire%> clauses");
35571 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35580 case OMP_ATOMIC_READ
:
35581 case NOP_EXPR
: /* atomic write */
35582 v
= cp_parser_unary_expression (parser
);
35583 if (v
== error_mark_node
)
35585 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35587 if (code
== NOP_EXPR
)
35588 lhs
= cp_parser_expression (parser
);
35590 lhs
= cp_parser_unary_expression (parser
);
35591 if (lhs
== error_mark_node
)
35593 if (code
== NOP_EXPR
)
35595 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35603 case OMP_ATOMIC_CAPTURE_NEW
:
35604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35606 cp_lexer_consume_token (parser
->lexer
);
35607 structured_block
= true;
35611 v
= cp_parser_unary_expression (parser
);
35612 if (v
== error_mark_node
)
35614 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35622 lhs
= cp_parser_unary_expression (parser
);
35624 switch (TREE_CODE (lhs
))
35629 case POSTINCREMENT_EXPR
:
35630 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35631 code
= OMP_ATOMIC_CAPTURE_OLD
;
35633 case PREINCREMENT_EXPR
:
35634 lhs
= TREE_OPERAND (lhs
, 0);
35635 opcode
= PLUS_EXPR
;
35636 rhs
= integer_one_node
;
35639 case POSTDECREMENT_EXPR
:
35640 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35641 code
= OMP_ATOMIC_CAPTURE_OLD
;
35643 case PREDECREMENT_EXPR
:
35644 lhs
= TREE_OPERAND (lhs
, 0);
35645 opcode
= MINUS_EXPR
;
35646 rhs
= integer_one_node
;
35649 case COMPOUND_EXPR
:
35650 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
35651 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
35652 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
35653 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
35654 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35655 (TREE_OPERAND (lhs
, 1), 0), 0)))
35657 /* Undo effects of boolean_increment for post {in,de}crement. */
35658 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
35661 if (TREE_CODE (lhs
) == MODIFY_EXPR
35662 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
35664 /* Undo effects of boolean_increment. */
35665 if (integer_onep (TREE_OPERAND (lhs
, 1)))
35667 /* This is pre or post increment. */
35668 rhs
= TREE_OPERAND (lhs
, 1);
35669 lhs
= TREE_OPERAND (lhs
, 0);
35671 if (code
== OMP_ATOMIC_CAPTURE_NEW
35672 && !structured_block
35673 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
35674 code
= OMP_ATOMIC_CAPTURE_OLD
;
35680 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35683 opcode
= MULT_EXPR
;
35686 opcode
= TRUNC_DIV_EXPR
;
35689 opcode
= PLUS_EXPR
;
35692 opcode
= MINUS_EXPR
;
35694 case CPP_LSHIFT_EQ
:
35695 opcode
= LSHIFT_EXPR
;
35697 case CPP_RSHIFT_EQ
:
35698 opcode
= RSHIFT_EXPR
;
35701 opcode
= BIT_AND_EXPR
;
35704 opcode
= BIT_IOR_EXPR
;
35707 opcode
= BIT_XOR_EXPR
;
35710 enum cp_parser_prec oprec
;
35712 cp_lexer_consume_token (parser
->lexer
);
35713 cp_parser_parse_tentatively (parser
);
35714 rhs1
= cp_parser_simple_cast_expression (parser
);
35715 if (rhs1
== error_mark_node
)
35717 cp_parser_abort_tentative_parse (parser
);
35718 cp_parser_simple_cast_expression (parser
);
35721 token
= cp_lexer_peek_token (parser
->lexer
);
35722 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
35724 cp_parser_abort_tentative_parse (parser
);
35725 cp_parser_parse_tentatively (parser
);
35726 rhs
= cp_parser_binary_expression (parser
, false, true,
35727 PREC_NOT_OPERATOR
, NULL
);
35728 if (rhs
== error_mark_node
)
35730 cp_parser_abort_tentative_parse (parser
);
35731 cp_parser_binary_expression (parser
, false, true,
35732 PREC_NOT_OPERATOR
, NULL
);
35735 switch (TREE_CODE (rhs
))
35738 case TRUNC_DIV_EXPR
:
35747 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
35749 if (cp_parser_parse_definitely (parser
))
35751 opcode
= TREE_CODE (rhs
);
35752 rhs1
= TREE_OPERAND (rhs
, 0);
35753 rhs
= TREE_OPERAND (rhs
, 1);
35763 cp_parser_abort_tentative_parse (parser
);
35764 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
35766 rhs
= cp_parser_expression (parser
);
35767 if (rhs
== error_mark_node
)
35773 cp_parser_error (parser
,
35774 "invalid form of %<#pragma omp atomic%>");
35777 if (!cp_parser_parse_definitely (parser
))
35779 switch (token
->type
)
35781 case CPP_SEMICOLON
:
35782 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35784 code
= OMP_ATOMIC_CAPTURE_OLD
;
35789 cp_lexer_consume_token (parser
->lexer
);
35792 else if (structured_block
)
35799 cp_parser_error (parser
,
35800 "invalid form of %<#pragma omp atomic%>");
35803 opcode
= MULT_EXPR
;
35806 opcode
= TRUNC_DIV_EXPR
;
35809 opcode
= PLUS_EXPR
;
35812 opcode
= MINUS_EXPR
;
35815 opcode
= LSHIFT_EXPR
;
35818 opcode
= RSHIFT_EXPR
;
35821 opcode
= BIT_AND_EXPR
;
35824 opcode
= BIT_IOR_EXPR
;
35827 opcode
= BIT_XOR_EXPR
;
35830 cp_parser_error (parser
,
35831 "invalid operator for %<#pragma omp atomic%>");
35834 oprec
= TOKEN_PRECEDENCE (token
);
35835 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
35836 if (commutative_tree_code (opcode
))
35837 oprec
= (enum cp_parser_prec
) (oprec
- 1);
35838 cp_lexer_consume_token (parser
->lexer
);
35839 rhs
= cp_parser_binary_expression (parser
, false, false,
35841 if (rhs
== error_mark_node
)
35846 cp_parser_error (parser
,
35847 "invalid operator for %<#pragma omp atomic%>");
35850 cp_lexer_consume_token (parser
->lexer
);
35852 rhs
= cp_parser_expression (parser
);
35853 if (rhs
== error_mark_node
)
35858 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35860 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
35862 v
= cp_parser_unary_expression (parser
);
35863 if (v
== error_mark_node
)
35865 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35867 lhs1
= cp_parser_unary_expression (parser
);
35868 if (lhs1
== error_mark_node
)
35871 if (structured_block
)
35873 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35874 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
35877 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
35878 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
35879 rhs1
, clauses
, memory_order
);
35880 if (!structured_block
)
35881 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35885 cp_parser_skip_to_end_of_block_or_statement (parser
);
35886 if (structured_block
)
35888 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35889 cp_lexer_consume_token (parser
->lexer
);
35890 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
35892 cp_parser_skip_to_end_of_block_or_statement (parser
);
35893 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35894 cp_lexer_consume_token (parser
->lexer
);
35901 # pragma omp barrier new-line */
35904 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
35906 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35907 finish_omp_barrier ();
35911 # pragma omp critical [(name)] new-line
35915 # pragma omp critical [(name) [hint(expression)]] new-line
35916 structured-block */
35918 #define OMP_CRITICAL_CLAUSE_MASK \
35919 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35922 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35924 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
35926 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35928 matching_parens parens
;
35929 parens
.consume_open (parser
);
35931 name
= cp_parser_identifier (parser
);
35933 if (name
== error_mark_node
35934 || !parens
.require_close (parser
))
35935 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35936 /*or_comma=*/false,
35937 /*consume_paren=*/true);
35938 if (name
== error_mark_node
)
35941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
35942 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
35943 cp_lexer_consume_token (parser
->lexer
);
35945 clauses
= cp_parser_omp_all_clauses (parser
,
35946 OMP_CRITICAL_CLAUSE_MASK
,
35947 "#pragma omp critical", pragma_tok
);
35950 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35952 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
35953 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
35957 # pragma omp depobj ( depobj ) depobj-clause new-line
35960 depend (dependence-type : locator)
35962 update (dependence-type)
35971 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
35973 location_t loc
= pragma_tok
->location
;
35974 matching_parens parens
;
35975 if (!parens
.require_open (parser
))
35977 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35981 tree depobj
= cp_parser_assignment_expression (parser
);
35983 if (!parens
.require_close (parser
))
35984 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35985 /*or_comma=*/false,
35986 /*consume_paren=*/true);
35988 tree clause
= NULL_TREE
;
35989 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
35990 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35991 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35993 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35994 const char *p
= IDENTIFIER_POINTER (id
);
35996 cp_lexer_consume_token (parser
->lexer
);
35997 if (!strcmp ("depend", p
))
35999 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
36001 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
36003 clause
= error_mark_node
;
36005 else if (!strcmp ("destroy", p
))
36006 kind
= OMP_CLAUSE_DEPEND_LAST
;
36007 else if (!strcmp ("update", p
))
36009 matching_parens c_parens
;
36010 if (c_parens
.require_open (parser
))
36013 = cp_lexer_peek_token (parser
->lexer
)->location
;
36014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36016 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36017 const char *p2
= IDENTIFIER_POINTER (id2
);
36019 cp_lexer_consume_token (parser
->lexer
);
36020 if (!strcmp ("in", p2
))
36021 kind
= OMP_CLAUSE_DEPEND_IN
;
36022 else if (!strcmp ("out", p2
))
36023 kind
= OMP_CLAUSE_DEPEND_OUT
;
36024 else if (!strcmp ("inout", p2
))
36025 kind
= OMP_CLAUSE_DEPEND_INOUT
;
36026 else if (!strcmp ("mutexinoutset", p2
))
36027 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
36029 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36031 clause
= error_mark_node
;
36032 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
36033 "%<mutexinoutset%>");
36035 if (!c_parens
.require_close (parser
))
36036 cp_parser_skip_to_closing_parenthesis (parser
,
36037 /*recovering=*/true,
36038 /*or_comma=*/false,
36039 /*consume_paren=*/true);
36042 clause
= error_mark_node
;
36045 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36047 clause
= error_mark_node
;
36048 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
36050 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36052 finish_omp_depobj (loc
, depobj
, kind
, clause
);
36057 # pragma omp flush flush-vars[opt] new-line
36063 # pragma omp flush memory-order-clause new-line */
36066 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
36068 enum memmodel mo
= MEMMODEL_LAST
;
36069 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36071 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36072 const char *p
= IDENTIFIER_POINTER (id
);
36073 if (!strcmp (p
, "acq_rel"))
36074 mo
= MEMMODEL_ACQ_REL
;
36075 else if (!strcmp (p
, "release"))
36076 mo
= MEMMODEL_RELEASE
;
36077 else if (!strcmp (p
, "acquire"))
36078 mo
= MEMMODEL_ACQUIRE
;
36080 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36081 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36082 cp_lexer_consume_token (parser
->lexer
);
36084 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36086 if (mo
!= MEMMODEL_LAST
)
36087 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36088 "%<flush%> list specified together with memory order "
36090 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36092 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36094 finish_omp_flush (mo
);
36097 /* Helper function, to parse omp for increment expression. */
36100 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
36102 tree cond
= cp_parser_binary_expression (parser
, false, true,
36103 PREC_NOT_OPERATOR
, NULL
);
36104 if (cond
== error_mark_node
36105 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36107 cp_parser_skip_to_end_of_statement (parser
);
36108 return error_mark_node
;
36111 switch (TREE_CODE (cond
))
36119 if (code
!= OACC_LOOP
)
36121 gcc_fallthrough ();
36123 return error_mark_node
;
36126 /* If decl is an iterator, preserve LHS and RHS of the relational
36127 expr until finish_omp_for. */
36129 && (type_dependent_expression_p (decl
)
36130 || CLASS_TYPE_P (TREE_TYPE (decl
))))
36133 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
36135 TREE_OPERAND (cond
, 0), ERROR_MARK
,
36136 TREE_OPERAND (cond
, 1), ERROR_MARK
,
36137 /*overload=*/NULL
, tf_warning_or_error
);
36140 /* Helper function, to parse omp for increment expression. */
36143 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
36145 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36151 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36153 op
= (token
->type
== CPP_PLUS_PLUS
36154 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
36155 cp_lexer_consume_token (parser
->lexer
);
36156 lhs
= cp_parser_simple_cast_expression (parser
);
36158 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36159 return error_mark_node
;
36160 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36163 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
36165 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36166 return error_mark_node
;
36168 token
= cp_lexer_peek_token (parser
->lexer
);
36169 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36171 op
= (token
->type
== CPP_PLUS_PLUS
36172 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
36173 cp_lexer_consume_token (parser
->lexer
);
36174 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36177 op
= cp_parser_assignment_operator_opt (parser
);
36178 if (op
== ERROR_MARK
)
36179 return error_mark_node
;
36181 if (op
!= NOP_EXPR
)
36183 rhs
= cp_parser_assignment_expression (parser
);
36184 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
36185 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36188 lhs
= cp_parser_binary_expression (parser
, false, false,
36189 PREC_ADDITIVE_EXPRESSION
, NULL
);
36190 token
= cp_lexer_peek_token (parser
->lexer
);
36191 decl_first
= (lhs
== decl
36192 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
36195 if (token
->type
!= CPP_PLUS
36196 && token
->type
!= CPP_MINUS
)
36197 return error_mark_node
;
36201 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
36202 cp_lexer_consume_token (parser
->lexer
);
36203 rhs
= cp_parser_binary_expression (parser
, false, false,
36204 PREC_ADDITIVE_EXPRESSION
, NULL
);
36205 token
= cp_lexer_peek_token (parser
->lexer
);
36206 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
36208 if (lhs
== NULL_TREE
)
36210 if (op
== PLUS_EXPR
)
36213 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
36214 tf_warning_or_error
);
36217 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
36218 ERROR_MARK
, NULL
, tf_warning_or_error
);
36221 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
36226 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
36227 || op
== MINUS_EXPR
)
36228 return error_mark_node
;
36229 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
36232 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
36234 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36237 /* Parse the initialization statement of an OpenMP for loop.
36239 Return true if the resulting construct should have an
36240 OMP_CLAUSE_PRIVATE added to it. */
36243 cp_parser_omp_for_loop_init (cp_parser
*parser
,
36244 tree
&this_pre_body
,
36245 vec
<tree
, va_gc
> *&for_block
,
36251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36254 tree add_private_clause
= NULL_TREE
;
36256 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36260 integer-type var = lb
36261 random-access-iterator-type var = lb
36262 pointer-type var = lb
36264 cp_decl_specifier_seq type_specifiers
;
36266 /* First, try to parse as an initialized declaration. See
36267 cp_parser_condition, from whence the bulk of this is copied. */
36269 cp_parser_parse_tentatively (parser
);
36270 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
36271 /*is_declaration=*/true,
36272 /*is_trailing_return=*/false,
36274 if (cp_parser_parse_definitely (parser
))
36276 /* If parsing a type specifier seq succeeded, then this
36277 MUST be a initialized declaration. */
36278 tree asm_specification
, attributes
;
36279 cp_declarator
*declarator
;
36281 declarator
= cp_parser_declarator (parser
,
36282 CP_PARSER_DECLARATOR_NAMED
,
36283 CP_PARSER_FLAGS_NONE
,
36284 /*ctor_dtor_or_conv_p=*/NULL
,
36285 /*parenthesized_p=*/NULL
,
36286 /*member_p=*/false,
36287 /*friend_p=*/false,
36288 /*static_p=*/false);
36289 attributes
= cp_parser_attributes_opt (parser
);
36290 asm_specification
= cp_parser_asm_specification_opt (parser
);
36292 if (declarator
== cp_error_declarator
)
36293 cp_parser_skip_to_end_of_statement (parser
);
36297 tree pushed_scope
, auto_node
;
36299 decl
= start_decl (declarator
, &type_specifiers
,
36300 SD_INITIALIZED
, attributes
,
36301 /*prefix_attributes=*/NULL_TREE
,
36304 auto_node
= type_uses_auto (TREE_TYPE (decl
));
36305 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
36307 if (cp_lexer_next_token_is (parser
->lexer
,
36309 error ("parenthesized initialization is not allowed in "
36310 "OpenMP %<for%> loop");
36312 /* Trigger an error. */
36313 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36315 init
= error_mark_node
;
36316 cp_parser_skip_to_end_of_statement (parser
);
36318 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
36319 || type_dependent_expression_p (decl
)
36322 bool is_direct_init
, is_non_constant_init
;
36324 init
= cp_parser_initializer (parser
,
36326 &is_non_constant_init
);
36331 = do_auto_deduction (TREE_TYPE (decl
), init
,
36334 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
36335 && !type_dependent_expression_p (decl
))
36339 cp_finish_decl (decl
, init
, !is_non_constant_init
,
36341 LOOKUP_ONLYCONVERTING
);
36343 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
36345 vec_safe_push (for_block
, this_pre_body
);
36350 init
= pop_stmt_list (this_pre_body
);
36351 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
36353 tree_stmt_iterator i
= tsi_start (init
);
36354 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36355 while (!tsi_end_p (i
))
36357 tree t
= tsi_stmt (i
);
36358 if (TREE_CODE (t
) == DECL_EXPR
36359 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
36362 vec_safe_push (for_block
, t
);
36367 if (tsi_one_before_end_p (i
))
36369 tree t
= tsi_stmt (i
);
36371 free_stmt_list (init
);
36376 this_pre_body
= NULL_TREE
;
36381 cp_lexer_consume_token (parser
->lexer
);
36382 init
= cp_parser_assignment_expression (parser
);
36385 if (TYPE_REF_P (TREE_TYPE (decl
)))
36386 init
= error_mark_node
;
36388 cp_finish_decl (decl
, NULL_TREE
,
36389 /*init_const_expr_p=*/false,
36391 LOOKUP_ONLYCONVERTING
);
36395 pop_scope (pushed_scope
);
36401 /* If parsing a type specifier sequence failed, then
36402 this MUST be a simple expression. */
36403 cp_parser_parse_tentatively (parser
);
36404 decl
= cp_parser_primary_expression (parser
, false, false,
36406 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
36407 if (!cp_parser_error_occurred (parser
)
36409 && (TREE_CODE (decl
) == COMPONENT_REF
36410 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
36412 cp_parser_abort_tentative_parse (parser
);
36413 cp_parser_parse_tentatively (parser
);
36414 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36415 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36416 /*check_dependency_p=*/true,
36417 /*template_p=*/NULL
,
36418 /*declarator_p=*/false,
36419 /*optional_p=*/false);
36420 if (name
!= error_mark_node
36421 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
36423 decl
= cp_parser_lookup_name_simple (parser
, name
,
36425 if (TREE_CODE (decl
) == FIELD_DECL
)
36426 add_private_clause
= omp_privatize_field (decl
, false);
36428 cp_parser_abort_tentative_parse (parser
);
36429 cp_parser_parse_tentatively (parser
);
36430 decl
= cp_parser_primary_expression (parser
, false, false,
36433 if (!cp_parser_error_occurred (parser
)
36436 && CLASS_TYPE_P (TREE_TYPE (decl
)))
36440 cp_parser_parse_definitely (parser
);
36441 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36442 rhs
= cp_parser_assignment_expression (parser
);
36444 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
36447 tf_warning_or_error
));
36448 if (!add_private_clause
)
36449 add_private_clause
= decl
;
36454 cp_parser_abort_tentative_parse (parser
);
36455 init
= cp_parser_expression (parser
);
36458 if (TREE_CODE (init
) == MODIFY_EXPR
36459 || TREE_CODE (init
) == MODOP_EXPR
)
36460 real_decl
= TREE_OPERAND (init
, 0);
36464 return add_private_clause
;
36467 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36470 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
36471 tree
&decl
, tree
&orig_decl
, tree
&init
,
36472 tree
&orig_init
, tree
&cond
, tree
&incr
)
36474 tree begin
, end
, range_temp_decl
= NULL_TREE
;
36475 tree iter_type
, begin_expr
, end_expr
;
36477 if (processing_template_decl
)
36479 if (check_for_bare_parameter_packs (init
))
36480 init
= error_mark_node
;
36481 if (!type_dependent_expression_p (init
)
36482 /* do_auto_deduction doesn't mess with template init-lists. */
36483 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
36486 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
36488 tree v
= DECL_VALUE_EXPR (decl
);
36489 if (TREE_CODE (v
) == ARRAY_REF
36490 && VAR_P (TREE_OPERAND (v
, 0))
36491 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36492 d
= TREE_OPERAND (v
, 0);
36494 do_range_for_auto_deduction (d
, init
);
36496 cond
= global_namespace
;
36500 this_pre_body
= pop_stmt_list (this_pre_body
);
36504 init
= mark_lvalue_use (init
);
36506 if (decl
== error_mark_node
|| init
== error_mark_node
)
36507 /* If an error happened previously do nothing or else a lot of
36508 unhelpful errors would be issued. */
36509 begin_expr
= end_expr
= iter_type
= error_mark_node
;
36515 && array_of_runtime_bound_p (TREE_TYPE (init
)))
36516 /* Can't bind a reference to an array of runtime bound. */
36520 range_temp
= build_range_temp (init
);
36521 DECL_NAME (range_temp
) = NULL_TREE
;
36522 pushdecl (range_temp
);
36523 cp_finish_decl (range_temp
, init
,
36524 /*is_constant_init*/false, NULL_TREE
,
36525 LOOKUP_ONLYCONVERTING
);
36526 range_temp_decl
= range_temp
;
36527 range_temp
= convert_from_reference (range_temp
);
36529 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
36530 &begin_expr
, &end_expr
);
36533 tree end_iter_type
= iter_type
;
36534 if (cxx_dialect
>= cxx17
)
36535 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
36536 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
36537 TREE_USED (end
) = 1;
36538 DECL_ARTIFICIAL (end
) = 1;
36540 cp_finish_decl (end
, end_expr
,
36541 /*is_constant_init*/false, NULL_TREE
,
36542 LOOKUP_ONLYCONVERTING
);
36544 /* The new for initialization statement. */
36545 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
36546 TREE_USED (begin
) = 1;
36547 DECL_ARTIFICIAL (begin
) = 1;
36550 if (CLASS_TYPE_P (iter_type
))
36555 begin_expr
= NULL_TREE
;
36557 cp_finish_decl (begin
, begin_expr
,
36558 /*is_constant_init*/false, NULL_TREE
,
36559 LOOKUP_ONLYCONVERTING
);
36561 /* The new for condition. */
36562 if (CLASS_TYPE_P (iter_type
))
36563 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
36565 cond
= build_x_binary_op (input_location
, NE_EXPR
,
36568 NULL
, tf_warning_or_error
);
36570 /* The new increment expression. */
36571 if (CLASS_TYPE_P (iter_type
))
36572 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
36574 incr
= finish_unary_op_expr (input_location
,
36575 PREINCREMENT_EXPR
, begin
,
36576 tf_warning_or_error
);
36582 vec_safe_push (for_block
, this_pre_body
);
36583 this_pre_body
= NULL_TREE
;
36586 tree decomp_first_name
= NULL_TREE
;
36587 unsigned decomp_cnt
= 0;
36588 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
36590 tree v
= DECL_VALUE_EXPR (orig_decl
);
36591 if (TREE_CODE (v
) == ARRAY_REF
36592 && VAR_P (TREE_OPERAND (v
, 0))
36593 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36595 tree d
= orig_decl
;
36596 orig_decl
= TREE_OPERAND (v
, 0);
36597 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
36598 decomp_first_name
= d
;
36602 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
36605 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36607 if (!error_operand_p (t
))
36608 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
36612 tree v
= make_tree_vec (decomp_cnt
+ 3);
36613 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
36614 TREE_VEC_ELT (v
, 1) = end
;
36615 TREE_VEC_ELT (v
, 2) = orig_decl
;
36616 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
36618 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
36619 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
36621 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
36624 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36625 inside of the collapsed body. */
36628 cp_finish_omp_range_for (tree orig
, tree begin
)
36630 gcc_assert (TREE_CODE (orig
) == TREE_LIST
36631 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
36632 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
36633 tree decomp_first_name
= NULL_TREE
;
36634 unsigned int decomp_cnt
= 0;
36636 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36638 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
36639 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
36640 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
36643 /* The declaration is initialized with *__begin inside the loop body. */
36644 cp_finish_decl (decl
,
36645 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36646 tf_warning_or_error
),
36647 /*is_constant_init*/false, NULL_TREE
,
36648 LOOKUP_ONLYCONVERTING
);
36649 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36650 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
36653 /* Parse the restricted form of the for statement allowed by OpenMP. */
36656 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
36657 tree
*cclauses
, bool *if_p
)
36659 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
36661 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
36662 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
36663 location_t loc_first
;
36664 bool collapse_err
= false;
36665 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
36666 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
36667 auto_vec
<tree
, 4> orig_inits
;
36668 bool tiling
= false;
36670 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
36671 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
36672 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
36673 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
36676 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
36678 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
36679 && OMP_CLAUSE_ORDERED_EXPR (cl
))
36682 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
36685 if (ordered
&& ordered
< collapse
)
36687 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36688 "%<ordered%> clause parameter is less than %<collapse%>");
36689 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
36690 = build_int_cst (NULL_TREE
, collapse
);
36691 ordered
= collapse
;
36695 for (tree
*pc
= &clauses
; *pc
; )
36696 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
36698 error_at (OMP_CLAUSE_LOCATION (*pc
),
36699 "%<linear%> clause may not be specified together "
36700 "with %<ordered%> clause with a parameter");
36701 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36704 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36707 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
36708 count
= ordered
? ordered
: collapse
;
36710 declv
= make_tree_vec (count
);
36711 initv
= make_tree_vec (count
);
36712 condv
= make_tree_vec (count
);
36713 incrv
= make_tree_vec (count
);
36714 orig_declv
= NULL_TREE
;
36716 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
36718 for (i
= 0; i
< count
; i
++)
36720 int bracecount
= 0;
36721 tree add_private_clause
= NULL_TREE
;
36724 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36727 cp_parser_error (parser
, "for statement expected");
36730 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
36732 /* Don't create location wrapper nodes within an OpenMP "for"
36734 auto_suppress_location_wrappers sentinel
;
36736 matching_parens parens
;
36737 if (!parens
.require_open (parser
))
36740 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
36741 this_pre_body
= push_stmt_list ();
36743 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
36745 /* Save tokens so that we can put them back. */
36746 cp_lexer_save_tokens (parser
->lexer
);
36748 /* Look for ':' that is not nested in () or {}. */
36750 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
36751 /*recovering=*/false,
36756 /* Roll back the tokens we skipped. */
36757 cp_lexer_rollback_tokens (parser
->lexer
);
36761 bool saved_colon_corrects_to_scope_p
36762 = parser
->colon_corrects_to_scope_p
;
36764 /* A colon is used in range-based for. */
36765 parser
->colon_corrects_to_scope_p
= false;
36767 /* Parse the declaration. */
36768 cp_parser_simple_declaration (parser
,
36769 /*function_definition_allowed_p=*/
36771 parser
->colon_corrects_to_scope_p
36772 = saved_colon_corrects_to_scope_p
;
36774 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36776 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
36779 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
36780 orig_decl
, init
, orig_init
,
36787 pre_body
= push_stmt_list ();
36789 add_stmt (this_pre_body
);
36790 pre_body
= pop_stmt_list (pre_body
);
36793 pre_body
= this_pre_body
;
36797 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36798 "%<ordered%> clause with parameter on "
36799 "range-based %<for%> loop");
36801 goto parse_close_paren
;
36806 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
36807 init
, orig_init
, decl
, real_decl
);
36809 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36812 this_pre_body
= pop_stmt_list (this_pre_body
);
36816 pre_body
= push_stmt_list ();
36818 add_stmt (this_pre_body
);
36819 pre_body
= pop_stmt_list (pre_body
);
36822 pre_body
= this_pre_body
;
36827 if (cclauses
!= NULL
36828 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
36829 && real_decl
!= NULL_TREE
)
36832 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
36833 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
36834 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36836 error_at (loc
, "iteration variable %qD"
36837 " should not be firstprivate", real_decl
);
36838 *c
= OMP_CLAUSE_CHAIN (*c
);
36840 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
36841 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36843 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36845 *c
= OMP_CLAUSE_CHAIN (*c
);
36846 if (code
== OMP_SIMD
)
36848 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36849 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
36853 OMP_CLAUSE_CHAIN (l
) = clauses
;
36856 add_private_clause
= NULL_TREE
;
36860 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
36861 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36862 add_private_clause
= NULL_TREE
;
36863 c
= &OMP_CLAUSE_CHAIN (*c
);
36867 if (add_private_clause
)
36870 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
36872 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
36873 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
36874 && OMP_CLAUSE_DECL (c
) == decl
)
36876 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
36877 && OMP_CLAUSE_DECL (c
) == decl
)
36878 error_at (loc
, "iteration variable %qD "
36879 "should not be firstprivate",
36881 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
36882 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
36883 && OMP_CLAUSE_DECL (c
) == decl
)
36884 error_at (loc
, "iteration variable %qD should not be reduction",
36889 if (code
!= OMP_SIMD
)
36890 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
36891 else if (collapse
== 1)
36892 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
36894 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
36895 OMP_CLAUSE_DECL (c
) = add_private_clause
;
36896 c
= finish_omp_clauses (c
, C_ORT_OMP
);
36899 OMP_CLAUSE_CHAIN (c
) = clauses
;
36901 /* For linear, signal that we need to fill up
36902 the so far unknown linear step. */
36903 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
36904 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
36910 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36911 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
36912 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36915 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
36917 /* If decl is an iterator, preserve the operator on decl
36918 until finish_omp_for. */
36920 && ((processing_template_decl
36921 && (TREE_TYPE (real_decl
) == NULL_TREE
36922 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
36923 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
36924 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
36926 incr
= cp_parser_expression (parser
);
36927 if (!EXPR_HAS_LOCATION (incr
))
36928 protected_set_expr_location (incr
, input_location
);
36932 if (!parens
.require_close (parser
))
36933 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36934 /*or_comma=*/false,
36935 /*consume_paren=*/true);
36937 TREE_VEC_ELT (declv
, i
) = decl
;
36938 TREE_VEC_ELT (initv
, i
) = init
;
36939 TREE_VEC_ELT (condv
, i
) = cond
;
36940 TREE_VEC_ELT (incrv
, i
) = incr
;
36943 orig_inits
.safe_grow_cleared (i
+ 1);
36944 orig_inits
[i
] = orig_init
;
36949 orig_declv
= copy_node (declv
);
36950 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
36952 else if (orig_declv
)
36953 TREE_VEC_ELT (orig_declv
, i
) = decl
;
36955 if (i
== count
- 1)
36958 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36959 in between the collapsed for loops to be still considered perfectly
36960 nested. Hopefully the final version clarifies this.
36961 For now handle (multiple) {'s and empty statements. */
36962 cp_parser_parse_tentatively (parser
);
36965 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36967 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
36969 cp_lexer_consume_token (parser
->lexer
);
36972 else if (bracecount
36973 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36974 cp_lexer_consume_token (parser
->lexer
);
36977 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36978 error_at (loc
, "not enough for loops to collapse");
36979 collapse_err
= true;
36980 cp_parser_abort_tentative_parse (parser
);
36988 cp_parser_parse_definitely (parser
);
36989 nbraces
+= bracecount
;
36996 /* Note that we saved the original contents of this flag when we entered
36997 the structured block, and so we don't need to re-save it here. */
36998 parser
->in_statement
= IN_OMP_FOR
;
37000 /* Note that the grammar doesn't call for a structured block here,
37001 though the loop as a whole is a structured block. */
37004 body
= begin_omp_structured_block ();
37005 for (i
= 0; i
< count
; i
++)
37006 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
37007 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
37008 TREE_VEC_ELT (declv
, i
));
37011 body
= push_stmt_list ();
37012 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37014 body
= finish_omp_structured_block (body
);
37016 body
= pop_stmt_list (body
);
37018 if (declv
== NULL_TREE
)
37021 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
37022 incrv
, body
, pre_body
, &orig_inits
, clauses
);
37026 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
37028 cp_lexer_consume_token (parser
->lexer
);
37031 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
37032 cp_lexer_consume_token (parser
->lexer
);
37037 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37038 "collapsed loops not perfectly nested");
37040 collapse_err
= true;
37041 cp_parser_statement_seq_opt (parser
, NULL
);
37042 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
37047 while (!for_block
->is_empty ())
37049 tree t
= for_block
->pop ();
37050 if (TREE_CODE (t
) == STATEMENT_LIST
)
37051 add_stmt (pop_stmt_list (t
));
37055 release_tree_vector (for_block
);
37060 /* Helper function for OpenMP parsing, split clauses and call
37061 finish_omp_clauses on each of the set of clauses afterwards. */
37064 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
37065 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
37068 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
37069 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
37071 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
37075 #pragma omp simd simd-clause[optseq] new-line
37078 #define OMP_SIMD_CLAUSE_MASK \
37079 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37091 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37092 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37095 tree clauses
, sb
, ret
;
37097 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37099 strcat (p_name
, " simd");
37100 mask
|= OMP_SIMD_CLAUSE_MASK
;
37102 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37106 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
37107 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
37108 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
37109 OMP_CLAUSE_ORDERED
);
37110 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
37112 error_at (OMP_CLAUSE_LOCATION (c
),
37113 "%<ordered%> clause with parameter may not be specified "
37114 "on %qs construct", p_name
);
37115 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
37119 keep_next_level (true);
37120 sb
= begin_omp_structured_block ();
37121 save
= cp_parser_begin_omp_structured_block (parser
);
37123 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
37125 cp_parser_end_omp_structured_block (parser
, save
);
37126 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37132 #pragma omp for for-clause[optseq] new-line
37136 #pragma omp for simd for-simd-clause[optseq] new-line
37139 #define OMP_FOR_CLAUSE_MASK \
37140 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37151 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
37152 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37155 tree clauses
, sb
, ret
;
37157 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37159 strcat (p_name
, " for");
37160 mask
|= OMP_FOR_CLAUSE_MASK
;
37161 /* parallel for{, simd} disallows nowait clause, but for
37162 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37163 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
37164 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37165 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37166 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37167 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
37169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37171 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37172 const char *p
= IDENTIFIER_POINTER (id
);
37174 if (strcmp (p
, "simd") == 0)
37176 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37177 if (cclauses
== NULL
)
37178 cclauses
= cclauses_buf
;
37180 cp_lexer_consume_token (parser
->lexer
);
37181 if (!flag_openmp
) /* flag_openmp_simd */
37182 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37184 sb
= begin_omp_structured_block ();
37185 save
= cp_parser_begin_omp_structured_block (parser
);
37186 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37188 cp_parser_end_omp_structured_block (parser
, save
);
37189 tree body
= finish_omp_structured_block (sb
);
37192 ret
= make_node (OMP_FOR
);
37193 TREE_TYPE (ret
) = void_type_node
;
37194 OMP_FOR_BODY (ret
) = body
;
37195 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37196 SET_EXPR_LOCATION (ret
, loc
);
37201 if (!flag_openmp
) /* flag_openmp_simd */
37203 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37207 /* Composite distribute parallel for disallows linear clause. */
37208 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37209 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
37211 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37215 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
37216 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37219 keep_next_level (true);
37220 sb
= begin_omp_structured_block ();
37221 save
= cp_parser_begin_omp_structured_block (parser
);
37223 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
37225 cp_parser_end_omp_structured_block (parser
, save
);
37226 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37231 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
37232 omp_clause_mask
, tree
*, bool *);
37235 # pragma omp master new-line
37236 structured-block */
37239 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
37240 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37243 tree clauses
, sb
, ret
;
37245 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37247 strcat (p_name
, " master");
37249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37251 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37252 const char *p
= IDENTIFIER_POINTER (id
);
37254 if (strcmp (p
, "taskloop") == 0)
37256 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37257 if (cclauses
== NULL
)
37258 cclauses
= cclauses_buf
;
37260 cp_lexer_consume_token (parser
->lexer
);
37261 if (!flag_openmp
) /* flag_openmp_simd */
37262 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37264 sb
= begin_omp_structured_block ();
37265 save
= cp_parser_begin_omp_structured_block (parser
);
37266 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37268 cp_parser_end_omp_structured_block (parser
, save
);
37269 tree body
= finish_omp_structured_block (sb
);
37272 return c_finish_omp_master (loc
, body
);
37275 if (!flag_openmp
) /* flag_openmp_simd */
37277 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37283 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37285 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
37288 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37290 return c_finish_omp_master (loc
,
37291 cp_parser_omp_structured_block (parser
, if_p
));
37295 # pragma omp ordered new-line
37299 # pragma omp ordered ordered-clauses new-line
37300 structured-block */
37302 #define OMP_ORDERED_CLAUSE_MASK \
37303 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37306 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37307 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37310 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
37311 enum pragma_context context
, bool *if_p
)
37313 location_t loc
= pragma_tok
->location
;
37315 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37317 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37318 const char *p
= IDENTIFIER_POINTER (id
);
37320 if (strcmp (p
, "depend") == 0)
37322 if (!flag_openmp
) /* flag_openmp_simd */
37324 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37327 if (context
== pragma_stmt
)
37329 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
37330 "%<depend%> clause may only be used in compound "
37332 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37336 = cp_parser_omp_all_clauses (parser
,
37337 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
37338 "#pragma omp ordered", pragma_tok
);
37339 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
37345 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
37346 "#pragma omp ordered", pragma_tok
);
37348 if (!flag_openmp
/* flag_openmp_simd */
37349 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
37352 c_finish_omp_ordered (loc
, clauses
,
37353 cp_parser_omp_structured_block (parser
, if_p
));
37360 { section-sequence }
37363 section-directive[opt] structured-block
37364 section-sequence section-directive structured-block */
37367 cp_parser_omp_sections_scope (cp_parser
*parser
)
37369 tree stmt
, substmt
;
37370 bool error_suppress
= false;
37373 matching_braces braces
;
37374 if (!braces
.require_open (parser
))
37377 stmt
= push_stmt_list ();
37379 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
37380 != PRAGMA_OMP_SECTION
)
37382 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37383 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37384 add_stmt (substmt
);
37389 tok
= cp_lexer_peek_token (parser
->lexer
);
37390 if (tok
->type
== CPP_CLOSE_BRACE
)
37392 if (tok
->type
== CPP_EOF
)
37395 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
37397 cp_lexer_consume_token (parser
->lexer
);
37398 cp_parser_require_pragma_eol (parser
, tok
);
37399 error_suppress
= false;
37401 else if (!error_suppress
)
37403 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
37404 error_suppress
= true;
37407 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37408 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37409 add_stmt (substmt
);
37411 braces
.require_close (parser
);
37413 substmt
= pop_stmt_list (stmt
);
37415 stmt
= make_node (OMP_SECTIONS
);
37416 TREE_TYPE (stmt
) = void_type_node
;
37417 OMP_SECTIONS_BODY (stmt
) = substmt
;
37424 # pragma omp sections sections-clause[optseq] newline
37427 #define OMP_SECTIONS_CLAUSE_MASK \
37428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37435 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
37436 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
37439 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37441 strcat (p_name
, " sections");
37442 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
37444 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37446 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37450 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
37451 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
37454 ret
= cp_parser_omp_sections_scope (parser
);
37456 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
37462 # pragma omp parallel parallel-clause[optseq] new-line
37464 # pragma omp parallel for parallel-for-clause[optseq] new-line
37466 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37470 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37471 structured-block */
37473 #define OMP_PARALLEL_CLAUSE_MASK \
37474 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37485 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37486 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37489 tree stmt
, clauses
, block
;
37491 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37493 strcat (p_name
, " parallel");
37494 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
37495 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37496 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
37497 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
37498 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
37500 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37502 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37503 if (cclauses
== NULL
)
37504 cclauses
= cclauses_buf
;
37506 cp_lexer_consume_token (parser
->lexer
);
37507 if (!flag_openmp
) /* flag_openmp_simd */
37508 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37510 block
= begin_omp_parallel ();
37511 save
= cp_parser_begin_omp_structured_block (parser
);
37512 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37514 cp_parser_end_omp_structured_block (parser
, save
);
37515 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37517 if (ret
== NULL_TREE
)
37519 OMP_PARALLEL_COMBINED (stmt
) = 1;
37522 /* When combined with distribute, parallel has to be followed by for.
37523 #pragma omp target parallel is allowed though. */
37525 && (mask
& (OMP_CLAUSE_MASK_1
37526 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37528 error_at (loc
, "expected %<for%> after %qs", p_name
);
37529 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37532 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37534 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37535 const char *p
= IDENTIFIER_POINTER (id
);
37536 if (strcmp (p
, "master") == 0)
37538 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37539 cclauses
= cclauses_buf
;
37541 cp_lexer_consume_token (parser
->lexer
);
37542 block
= begin_omp_parallel ();
37543 save
= cp_parser_begin_omp_structured_block (parser
);
37544 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
37546 cp_parser_end_omp_structured_block (parser
, save
);
37547 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37549 OMP_PARALLEL_COMBINED (stmt
) = 1;
37550 if (ret
== NULL_TREE
)
37554 else if (!flag_openmp
) /* flag_openmp_simd */
37556 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37559 else if (strcmp (p
, "sections") == 0)
37561 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37562 cclauses
= cclauses_buf
;
37564 cp_lexer_consume_token (parser
->lexer
);
37565 block
= begin_omp_parallel ();
37566 save
= cp_parser_begin_omp_structured_block (parser
);
37567 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
37568 cp_parser_end_omp_structured_block (parser
, save
);
37569 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37571 OMP_PARALLEL_COMBINED (stmt
) = 1;
37575 else if (!flag_openmp
) /* flag_openmp_simd */
37577 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37581 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37585 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
37586 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
37589 block
= begin_omp_parallel ();
37590 save
= cp_parser_begin_omp_structured_block (parser
);
37591 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37592 cp_parser_end_omp_structured_block (parser
, save
);
37593 stmt
= finish_omp_parallel (clauses
, block
);
37598 # pragma omp single single-clause[optseq] new-line
37599 structured-block */
37601 #define OMP_SINGLE_CLAUSE_MASK \
37602 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37608 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37610 tree stmt
= make_node (OMP_SINGLE
);
37611 TREE_TYPE (stmt
) = void_type_node
;
37612 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37614 OMP_SINGLE_CLAUSES (stmt
)
37615 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
37616 "#pragma omp single", pragma_tok
);
37617 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37619 return add_stmt (stmt
);
37623 # pragma omp task task-clause[optseq] new-line
37624 structured-block */
37626 #define OMP_TASK_CLAUSE_MASK \
37627 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37640 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37642 tree clauses
, block
;
37645 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
37646 "#pragma omp task", pragma_tok
);
37647 block
= begin_omp_task ();
37648 save
= cp_parser_begin_omp_structured_block (parser
);
37649 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37650 cp_parser_end_omp_structured_block (parser
, save
);
37651 return finish_omp_task (clauses
, block
);
37655 # pragma omp taskwait new-line
37658 # pragma omp taskwait taskwait-clause[opt] new-line */
37660 #define OMP_TASKWAIT_CLAUSE_MASK \
37661 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37664 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
37667 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
37668 "#pragma omp taskwait", pragma_tok
);
37672 tree stmt
= make_node (OMP_TASK
);
37673 TREE_TYPE (stmt
) = void_node
;
37674 OMP_TASK_CLAUSES (stmt
) = clauses
;
37675 OMP_TASK_BODY (stmt
) = NULL_TREE
;
37676 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37680 finish_omp_taskwait ();
37684 # pragma omp taskyield new-line */
37687 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
37689 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37690 finish_omp_taskyield ();
37694 # pragma omp taskgroup new-line
37698 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37700 #define OMP_TASKGROUP_CLAUSE_MASK \
37701 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37704 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37707 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
37708 "#pragma omp taskgroup", pragma_tok
);
37709 return c_finish_omp_taskgroup (input_location
,
37710 cp_parser_omp_structured_block (parser
,
37717 # pragma omp threadprivate (variable-list) */
37720 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
37724 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
37725 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37727 finish_omp_threadprivate (vars
);
37731 # pragma omp cancel cancel-clause[optseq] new-line */
37733 #define OMP_CANCEL_CLAUSE_MASK \
37734 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37741 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
37743 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
37744 "#pragma omp cancel", pragma_tok
);
37745 finish_omp_cancel (clauses
);
37749 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37751 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37752 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37758 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
37759 enum pragma_context context
)
37762 bool point_seen
= false;
37764 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37766 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37767 const char *p
= IDENTIFIER_POINTER (id
);
37769 if (strcmp (p
, "point") == 0)
37771 cp_lexer_consume_token (parser
->lexer
);
37777 cp_parser_error (parser
, "expected %<point%>");
37778 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37782 if (context
!= pragma_compound
)
37784 if (context
== pragma_stmt
)
37785 error_at (pragma_tok
->location
,
37786 "%<#pragma %s%> may only be used in compound statements",
37787 "omp cancellation point");
37789 cp_parser_error (parser
, "expected declaration specifiers");
37790 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37794 clauses
= cp_parser_omp_all_clauses (parser
,
37795 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
37796 "#pragma omp cancellation point",
37798 finish_omp_cancellation_point (clauses
);
37802 #pragma omp distribute distribute-clause[optseq] new-line
37805 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37806 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37813 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
37814 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37817 tree clauses
, sb
, ret
;
37819 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37821 strcat (p_name
, " distribute");
37822 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
37824 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37826 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37827 const char *p
= IDENTIFIER_POINTER (id
);
37829 bool parallel
= false;
37831 if (strcmp (p
, "simd") == 0)
37834 parallel
= strcmp (p
, "parallel") == 0;
37835 if (parallel
|| simd
)
37837 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37838 if (cclauses
== NULL
)
37839 cclauses
= cclauses_buf
;
37840 cp_lexer_consume_token (parser
->lexer
);
37841 if (!flag_openmp
) /* flag_openmp_simd */
37844 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37847 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37850 sb
= begin_omp_structured_block ();
37851 save
= cp_parser_begin_omp_structured_block (parser
);
37853 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37856 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37858 cp_parser_end_omp_structured_block (parser
, save
);
37859 tree body
= finish_omp_structured_block (sb
);
37862 ret
= make_node (OMP_DISTRIBUTE
);
37863 TREE_TYPE (ret
) = void_type_node
;
37864 OMP_FOR_BODY (ret
) = body
;
37865 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37866 SET_EXPR_LOCATION (ret
, loc
);
37871 if (!flag_openmp
) /* flag_openmp_simd */
37873 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37877 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37881 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
37882 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37885 keep_next_level (true);
37886 sb
= begin_omp_structured_block ();
37887 save
= cp_parser_begin_omp_structured_block (parser
);
37889 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
37891 cp_parser_end_omp_structured_block (parser
, save
);
37892 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37898 # pragma omp teams teams-clause[optseq] new-line
37899 structured-block */
37901 #define OMP_TEAMS_CLAUSE_MASK \
37902 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37911 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
37912 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37915 tree clauses
, sb
, ret
;
37917 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37919 strcat (p_name
, " teams");
37920 mask
|= OMP_TEAMS_CLAUSE_MASK
;
37922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37924 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37925 const char *p
= IDENTIFIER_POINTER (id
);
37926 if (strcmp (p
, "distribute") == 0)
37928 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37929 if (cclauses
== NULL
)
37930 cclauses
= cclauses_buf
;
37932 cp_lexer_consume_token (parser
->lexer
);
37933 if (!flag_openmp
) /* flag_openmp_simd */
37934 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37936 keep_next_level (true);
37937 sb
= begin_omp_structured_block ();
37938 save
= cp_parser_begin_omp_structured_block (parser
);
37939 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37941 cp_parser_end_omp_structured_block (parser
, save
);
37942 tree body
= finish_omp_structured_block (sb
);
37945 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37946 ret
= make_node (OMP_TEAMS
);
37947 TREE_TYPE (ret
) = void_type_node
;
37948 OMP_TEAMS_CLAUSES (ret
) = clauses
;
37949 OMP_TEAMS_BODY (ret
) = body
;
37950 OMP_TEAMS_COMBINED (ret
) = 1;
37951 SET_EXPR_LOCATION (ret
, loc
);
37952 return add_stmt (ret
);
37955 if (!flag_openmp
) /* flag_openmp_simd */
37957 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37961 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37965 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
37966 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37969 tree stmt
= make_node (OMP_TEAMS
);
37970 TREE_TYPE (stmt
) = void_type_node
;
37971 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
37972 keep_next_level (true);
37973 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37974 SET_EXPR_LOCATION (stmt
, loc
);
37976 return add_stmt (stmt
);
37980 # pragma omp target data target-data-clause[optseq] new-line
37981 structured-block */
37983 #define OMP_TARGET_DATA_CLAUSE_MASK \
37984 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37990 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37993 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
37994 "#pragma omp target data", pragma_tok
);
37996 for (tree
*pc
= &clauses
; *pc
;)
37998 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
37999 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38002 case GOMP_MAP_ALWAYS_TO
:
38003 case GOMP_MAP_FROM
:
38004 case GOMP_MAP_ALWAYS_FROM
:
38005 case GOMP_MAP_TOFROM
:
38006 case GOMP_MAP_ALWAYS_TOFROM
:
38007 case GOMP_MAP_ALLOC
:
38010 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38011 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38012 case GOMP_MAP_ALWAYS_POINTER
:
38016 error_at (OMP_CLAUSE_LOCATION (*pc
),
38017 "%<#pragma omp target data%> with map-type other "
38018 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38019 "on %<map%> clause");
38020 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38023 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
)
38025 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38031 error_at (pragma_tok
->location
,
38032 "%<#pragma omp target data%> must contain at least "
38033 "one %<map%> or %<use_device_ptr%> clause");
38037 tree stmt
= make_node (OMP_TARGET_DATA
);
38038 TREE_TYPE (stmt
) = void_type_node
;
38039 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
38041 keep_next_level (true);
38042 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38044 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38045 return add_stmt (stmt
);
38049 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38050 structured-block */
38052 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38053 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38060 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38061 enum pragma_context context
)
38063 bool data_seen
= false;
38064 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38066 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38067 const char *p
= IDENTIFIER_POINTER (id
);
38069 if (strcmp (p
, "data") == 0)
38071 cp_lexer_consume_token (parser
->lexer
);
38077 cp_parser_error (parser
, "expected %<data%>");
38078 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38082 if (context
== pragma_stmt
)
38084 error_at (pragma_tok
->location
,
38085 "%<#pragma %s%> may only be used in compound statements",
38086 "omp target enter data");
38087 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38092 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
38093 "#pragma omp target enter data", pragma_tok
);
38095 for (tree
*pc
= &clauses
; *pc
;)
38097 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38098 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38101 case GOMP_MAP_ALWAYS_TO
:
38102 case GOMP_MAP_ALLOC
:
38105 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38106 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38107 case GOMP_MAP_ALWAYS_POINTER
:
38111 error_at (OMP_CLAUSE_LOCATION (*pc
),
38112 "%<#pragma omp target enter data%> with map-type other "
38113 "than %<to%> or %<alloc%> on %<map%> clause");
38114 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38117 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38123 error_at (pragma_tok
->location
,
38124 "%<#pragma omp target enter data%> must contain at least "
38125 "one %<map%> clause");
38129 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
38130 TREE_TYPE (stmt
) = void_type_node
;
38131 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
38132 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38133 return add_stmt (stmt
);
38137 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38138 structured-block */
38140 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38141 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38148 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38149 enum pragma_context context
)
38151 bool data_seen
= false;
38152 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38154 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38155 const char *p
= IDENTIFIER_POINTER (id
);
38157 if (strcmp (p
, "data") == 0)
38159 cp_lexer_consume_token (parser
->lexer
);
38165 cp_parser_error (parser
, "expected %<data%>");
38166 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38170 if (context
== pragma_stmt
)
38172 error_at (pragma_tok
->location
,
38173 "%<#pragma %s%> may only be used in compound statements",
38174 "omp target exit data");
38175 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38180 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
38181 "#pragma omp target exit data", pragma_tok
);
38183 for (tree
*pc
= &clauses
; *pc
;)
38185 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38186 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38188 case GOMP_MAP_FROM
:
38189 case GOMP_MAP_ALWAYS_FROM
:
38190 case GOMP_MAP_RELEASE
:
38191 case GOMP_MAP_DELETE
:
38194 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38195 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38196 case GOMP_MAP_ALWAYS_POINTER
:
38200 error_at (OMP_CLAUSE_LOCATION (*pc
),
38201 "%<#pragma omp target exit data%> with map-type other "
38202 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38204 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38207 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38213 error_at (pragma_tok
->location
,
38214 "%<#pragma omp target exit data%> must contain at least "
38215 "one %<map%> clause");
38219 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
38220 TREE_TYPE (stmt
) = void_type_node
;
38221 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
38222 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38223 return add_stmt (stmt
);
38227 # pragma omp target update target-update-clause[optseq] new-line */
38229 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38230 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38238 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
38239 enum pragma_context context
)
38241 if (context
== pragma_stmt
)
38243 error_at (pragma_tok
->location
,
38244 "%<#pragma %s%> may only be used in compound statements",
38245 "omp target update");
38246 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38251 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
38252 "#pragma omp target update", pragma_tok
);
38253 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
38254 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
38256 error_at (pragma_tok
->location
,
38257 "%<#pragma omp target update%> must contain at least one "
38258 "%<from%> or %<to%> clauses");
38262 tree stmt
= make_node (OMP_TARGET_UPDATE
);
38263 TREE_TYPE (stmt
) = void_type_node
;
38264 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
38265 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38271 # pragma omp target target-clause[optseq] new-line
38272 structured-block */
38274 #define OMP_TARGET_CLAUSE_MASK \
38275 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38286 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
38287 enum pragma_context context
, bool *if_p
)
38289 tree
*pc
= NULL
, stmt
;
38293 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
38295 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38297 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38298 const char *p
= IDENTIFIER_POINTER (id
);
38299 enum tree_code ccode
= ERROR_MARK
;
38301 if (strcmp (p
, "teams") == 0)
38303 else if (strcmp (p
, "parallel") == 0)
38304 ccode
= OMP_PARALLEL
;
38305 else if (strcmp (p
, "simd") == 0)
38307 if (ccode
!= ERROR_MARK
)
38309 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
38310 char p_name
[sizeof ("#pragma omp target teams distribute "
38311 "parallel for simd")];
38313 cp_lexer_consume_token (parser
->lexer
);
38314 strcpy (p_name
, "#pragma omp target");
38315 if (!flag_openmp
) /* flag_openmp_simd */
38321 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38322 OMP_TARGET_CLAUSE_MASK
,
38326 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38327 OMP_TARGET_CLAUSE_MASK
,
38331 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38332 OMP_TARGET_CLAUSE_MASK
,
38336 gcc_unreachable ();
38338 return stmt
!= NULL_TREE
;
38340 keep_next_level (true);
38341 tree sb
= begin_omp_structured_block (), ret
;
38342 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
38346 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38347 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38351 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38352 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38356 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38357 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38361 gcc_unreachable ();
38363 cp_parser_end_omp_structured_block (parser
, save
);
38364 tree body
= finish_omp_structured_block (sb
);
38365 if (ret
== NULL_TREE
)
38367 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
38369 /* For combined target teams, ensure the num_teams and
38370 thread_limit clause expressions are evaluated on the host,
38371 before entering the target construct. */
38373 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38374 c
; c
= OMP_CLAUSE_CHAIN (c
))
38375 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
38376 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
38377 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
38379 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
38380 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
38381 if (expr
== error_mark_node
)
38383 tree tmp
= TARGET_EXPR_SLOT (expr
);
38385 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
38386 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
38387 OMP_CLAUSE_FIRSTPRIVATE
);
38388 OMP_CLAUSE_DECL (tc
) = tmp
;
38389 OMP_CLAUSE_CHAIN (tc
)
38390 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38391 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
38394 tree stmt
= make_node (OMP_TARGET
);
38395 TREE_TYPE (stmt
) = void_type_node
;
38396 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38397 OMP_TARGET_BODY (stmt
) = body
;
38398 OMP_TARGET_COMBINED (stmt
) = 1;
38399 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38401 pc
= &OMP_TARGET_CLAUSES (stmt
);
38402 goto check_clauses
;
38404 else if (!flag_openmp
) /* flag_openmp_simd */
38406 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38409 else if (strcmp (p
, "data") == 0)
38411 cp_lexer_consume_token (parser
->lexer
);
38412 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
38415 else if (strcmp (p
, "enter") == 0)
38417 cp_lexer_consume_token (parser
->lexer
);
38418 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
38421 else if (strcmp (p
, "exit") == 0)
38423 cp_lexer_consume_token (parser
->lexer
);
38424 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
38427 else if (strcmp (p
, "update") == 0)
38429 cp_lexer_consume_token (parser
->lexer
);
38430 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
38433 if (!flag_openmp
) /* flag_openmp_simd */
38435 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38439 stmt
= make_node (OMP_TARGET
);
38440 TREE_TYPE (stmt
) = void_type_node
;
38442 OMP_TARGET_CLAUSES (stmt
)
38443 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
38444 "#pragma omp target", pragma_tok
);
38445 pc
= &OMP_TARGET_CLAUSES (stmt
);
38446 keep_next_level (true);
38447 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38449 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38455 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38456 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38459 case GOMP_MAP_ALWAYS_TO
:
38460 case GOMP_MAP_FROM
:
38461 case GOMP_MAP_ALWAYS_FROM
:
38462 case GOMP_MAP_TOFROM
:
38463 case GOMP_MAP_ALWAYS_TOFROM
:
38464 case GOMP_MAP_ALLOC
:
38465 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38466 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38467 case GOMP_MAP_ALWAYS_POINTER
:
38470 error_at (OMP_CLAUSE_LOCATION (*pc
),
38471 "%<#pragma omp target%> with map-type other "
38472 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38473 "on %<map%> clause");
38474 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38477 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38483 # pragma acc cache (variable-list) new-line
38487 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
38489 tree stmt
, clauses
;
38491 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
38492 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38494 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
38496 stmt
= make_node (OACC_CACHE
);
38497 TREE_TYPE (stmt
) = void_type_node
;
38498 OACC_CACHE_CLAUSES (stmt
) = clauses
;
38499 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38506 # pragma acc data oacc-data-clause[optseq] new-line
38507 structured-block */
38509 #define OACC_DATA_CLAUSE_MASK \
38510 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38519 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38521 tree stmt
, clauses
, block
;
38524 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
38525 "#pragma acc data", pragma_tok
);
38527 block
= begin_omp_parallel ();
38528 save
= cp_parser_begin_omp_structured_block (parser
);
38529 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38530 cp_parser_end_omp_structured_block (parser
, save
);
38531 stmt
= finish_oacc_data (clauses
, block
);
38536 # pragma acc host_data <clauses> new-line
38537 structured-block */
38539 #define OACC_HOST_DATA_CLAUSE_MASK \
38540 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38543 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38545 tree stmt
, clauses
, block
;
38548 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
38549 "#pragma acc host_data", pragma_tok
);
38551 block
= begin_omp_parallel ();
38552 save
= cp_parser_begin_omp_structured_block (parser
);
38553 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38554 cp_parser_end_omp_structured_block (parser
, save
);
38555 stmt
= finish_oacc_host_data (clauses
, block
);
38560 # pragma acc declare oacc-data-clause[optseq] new-line
38563 #define OACC_DECLARE_CLAUSE_MASK \
38564 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38574 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
38576 tree clauses
, stmt
;
38577 bool error
= false;
38579 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
38580 "#pragma acc declare", pragma_tok
, true);
38583 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38585 error_at (pragma_tok
->location
,
38586 "no valid clauses specified in %<#pragma acc declare%>");
38590 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
38592 location_t loc
= OMP_CLAUSE_LOCATION (t
);
38593 tree decl
= OMP_CLAUSE_DECL (t
);
38594 if (!DECL_P (decl
))
38596 error_at (loc
, "array section in %<#pragma acc declare%>");
38600 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
38601 switch (OMP_CLAUSE_MAP_KIND (t
))
38603 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38604 case GOMP_MAP_ALLOC
:
38606 case GOMP_MAP_FORCE_DEVICEPTR
:
38607 case GOMP_MAP_DEVICE_RESIDENT
:
38610 case GOMP_MAP_LINK
:
38611 if (!global_bindings_p ()
38612 && (TREE_STATIC (decl
)
38613 || !DECL_EXTERNAL (decl
)))
38616 "%qD must be a global variable in "
38617 "%<#pragma acc declare link%>",
38625 if (global_bindings_p ())
38627 error_at (loc
, "invalid OpenACC clause at file scope");
38631 if (DECL_EXTERNAL (decl
))
38634 "invalid use of %<extern%> variable %qD "
38635 "in %<#pragma acc declare%>", decl
);
38639 else if (TREE_PUBLIC (decl
))
38642 "invalid use of %<global%> variable %qD "
38643 "in %<#pragma acc declare%>", decl
);
38650 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
38651 || lookup_attribute ("omp declare target link",
38652 DECL_ATTRIBUTES (decl
)))
38654 error_at (loc
, "variable %qD used more than once with "
38655 "%<#pragma acc declare%>", decl
);
38664 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
38665 id
= get_identifier ("omp declare target link");
38667 id
= get_identifier ("omp declare target");
38669 DECL_ATTRIBUTES (decl
)
38670 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
38671 if (global_bindings_p ())
38673 symtab_node
*node
= symtab_node::get (decl
);
38676 node
->offloadable
= 1;
38677 if (ENABLE_OFFLOADING
)
38679 g
->have_offload
= true;
38680 if (is_a
<varpool_node
*> (node
))
38681 vec_safe_push (offload_vars
, decl
);
38688 if (error
|| global_bindings_p ())
38691 stmt
= make_node (OACC_DECLARE
);
38692 TREE_TYPE (stmt
) = void_type_node
;
38693 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
38694 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38702 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38706 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38708 LOC is the location of the #pragma token.
38711 #define OACC_ENTER_DATA_CLAUSE_MASK \
38712 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38718 #define OACC_EXIT_DATA_CLAUSE_MASK \
38719 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38727 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38730 location_t loc
= pragma_tok
->location
;
38731 tree stmt
, clauses
;
38732 const char *p
= "";
38734 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38735 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38737 if (strcmp (p
, "data") != 0)
38739 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
38740 enter
? "enter" : "exit");
38741 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38745 cp_lexer_consume_token (parser
->lexer
);
38748 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
38749 "#pragma acc enter data", pragma_tok
);
38751 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
38752 "#pragma acc exit data", pragma_tok
);
38754 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38756 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
38757 enter
? "enter" : "exit");
38761 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
38762 TREE_TYPE (stmt
) = void_type_node
;
38763 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
38764 SET_EXPR_LOCATION (stmt
, loc
);
38770 # pragma acc loop oacc-loop-clause[optseq] new-line
38771 structured-block */
38773 #define OACC_LOOP_CLAUSE_MASK \
38774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38786 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
38787 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
38789 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
38791 strcat (p_name
, " loop");
38792 mask
|= OACC_LOOP_CLAUSE_MASK
;
38794 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38798 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
38800 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
38802 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38805 tree block
= begin_omp_structured_block ();
38806 int save
= cp_parser_begin_omp_structured_block (parser
);
38807 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
38808 cp_parser_end_omp_structured_block (parser
, save
);
38809 add_stmt (finish_omp_structured_block (block
));
38815 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38820 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38824 #define OACC_KERNELS_CLAUSE_MASK \
38825 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38839 #define OACC_PARALLEL_CLAUSE_MASK \
38840 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38858 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
38859 char *p_name
, bool *if_p
)
38861 omp_clause_mask mask
;
38862 enum tree_code code
;
38863 switch (cp_parser_pragma_kind (pragma_tok
))
38865 case PRAGMA_OACC_KERNELS
:
38866 strcat (p_name
, " kernels");
38867 mask
= OACC_KERNELS_CLAUSE_MASK
;
38868 code
= OACC_KERNELS
;
38870 case PRAGMA_OACC_PARALLEL
:
38871 strcat (p_name
, " parallel");
38872 mask
= OACC_PARALLEL_CLAUSE_MASK
;
38873 code
= OACC_PARALLEL
;
38876 gcc_unreachable ();
38879 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38882 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38883 if (strcmp (p
, "loop") == 0)
38885 cp_lexer_consume_token (parser
->lexer
);
38886 tree block
= begin_omp_parallel ();
38888 tree stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
,
38890 protected_set_expr_location (stmt
, pragma_tok
->location
);
38891 return finish_omp_construct (code
, block
, clauses
);
38895 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
38897 tree block
= begin_omp_parallel ();
38898 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
38899 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38900 cp_parser_end_omp_structured_block (parser
, save
);
38901 return finish_omp_construct (code
, block
, clauses
);
38905 # pragma acc update oacc-update-clause[optseq] new-line
38908 #define OACC_UPDATE_CLAUSE_MASK \
38909 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38917 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
38919 tree stmt
, clauses
;
38921 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
38922 "#pragma acc update", pragma_tok
);
38924 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38926 error_at (pragma_tok
->location
,
38927 "%<#pragma acc update%> must contain at least one "
38928 "%<device%> or %<host%> or %<self%> clause");
38932 stmt
= make_node (OACC_UPDATE
);
38933 TREE_TYPE (stmt
) = void_type_node
;
38934 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
38935 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38941 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38943 LOC is the location of the #pragma token.
38946 #define OACC_WAIT_CLAUSE_MASK \
38947 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38950 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
38952 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
38953 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38955 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38956 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
38958 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
38959 "#pragma acc wait", pragma_tok
);
38961 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
38962 stmt
= finish_expr_stmt (stmt
);
38968 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38970 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38971 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38979 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
38980 enum pragma_context context
)
38982 bool first_p
= parser
->omp_declare_simd
== NULL
;
38983 cp_omp_declare_simd_data data
;
38986 data
.error_seen
= false;
38987 data
.fndecl_seen
= false;
38988 data
.tokens
= vNULL
;
38989 data
.clauses
= NULL_TREE
;
38990 /* It is safe to take the address of a local variable; it will only be
38991 used while this scope is live. */
38992 parser
->omp_declare_simd
= &data
;
38995 /* Store away all pragma tokens. */
38996 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38997 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38998 cp_lexer_consume_token (parser
->lexer
);
38999 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39000 parser
->omp_declare_simd
->error_seen
= true;
39001 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39002 struct cp_token_cache
*cp
39003 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
39004 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
39008 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
39009 cp_parser_pragma (parser
, context
, NULL
);
39012 case pragma_external
:
39013 cp_parser_declaration (parser
);
39015 case pragma_member
:
39016 cp_parser_member_declaration (parser
);
39018 case pragma_objc_icode
:
39019 cp_parser_block_declaration (parser
, /*statement_p=*/false);
39022 cp_parser_declaration_statement (parser
);
39025 if (parser
->omp_declare_simd
39026 && !parser
->omp_declare_simd
->error_seen
39027 && !parser
->omp_declare_simd
->fndecl_seen
)
39028 error_at (pragma_tok
->location
,
39029 "%<#pragma omp declare simd%> not immediately followed by "
39030 "function declaration or definition");
39031 data
.tokens
.release ();
39032 parser
->omp_declare_simd
= NULL
;
39036 /* Finalize #pragma omp declare simd clauses after direct declarator has
39037 been parsed, and put that into "omp declare simd" attribute. */
39040 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
39042 struct cp_token_cache
*ce
;
39043 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
39046 if (!data
->error_seen
&& data
->fndecl_seen
)
39048 error ("%<#pragma omp declare simd%> not immediately followed by "
39049 "a single function declaration or definition");
39050 data
->error_seen
= true;
39052 if (data
->error_seen
)
39055 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
39059 cp_parser_push_lexer_for_tokens (parser
, ce
);
39060 parser
->lexer
->in_pragma
= true;
39061 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
39062 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
39063 cp_lexer_consume_token (parser
->lexer
);
39064 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
39065 "#pragma omp declare simd", pragma_tok
);
39066 cp_parser_pop_lexer (parser
);
39068 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
39069 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
39070 TREE_CHAIN (c
) = attrs
;
39071 if (processing_template_decl
)
39072 ATTR_IS_DEPENDENT (c
) = 1;
39076 data
->fndecl_seen
= true;
39082 # pragma omp declare target new-line
39083 declarations and definitions
39084 # pragma omp end declare target new-line
39087 # pragma omp declare target ( extended-list ) new-line
39089 # pragma omp declare target declare-target-clauses[seq] new-line */
39091 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39092 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39096 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39098 tree clauses
= NULL_TREE
;
39099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39101 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
39102 "#pragma omp declare target", pragma_tok
);
39103 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39105 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
39107 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
39108 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39112 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39113 scope_chain
->omp_declare_target_attribute
++;
39116 if (scope_chain
->omp_declare_target_attribute
)
39117 error_at (pragma_tok
->location
,
39118 "%<#pragma omp declare target%> with clauses in between "
39119 "%<#pragma omp declare target%> without clauses and "
39120 "%<#pragma omp end declare target%>");
39121 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
39123 tree t
= OMP_CLAUSE_DECL (c
), id
;
39124 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
39125 tree at2
= lookup_attribute ("omp declare target link",
39126 DECL_ATTRIBUTES (t
));
39127 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
39129 id
= get_identifier ("omp declare target link");
39130 std::swap (at1
, at2
);
39133 id
= get_identifier ("omp declare target");
39136 error_at (OMP_CLAUSE_LOCATION (c
),
39137 "%qD specified both in declare target %<link%> and %<to%>"
39143 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
39144 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
39147 symtab_node
*node
= symtab_node::get (t
);
39150 node
->offloadable
= 1;
39151 if (ENABLE_OFFLOADING
)
39153 g
->have_offload
= true;
39154 if (is_a
<varpool_node
*> (node
))
39155 vec_safe_push (offload_vars
, t
);
39163 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39165 const char *p
= "";
39166 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39168 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39169 p
= IDENTIFIER_POINTER (id
);
39171 if (strcmp (p
, "declare") == 0)
39173 cp_lexer_consume_token (parser
->lexer
);
39175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39177 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39178 p
= IDENTIFIER_POINTER (id
);
39180 if (strcmp (p
, "target") == 0)
39181 cp_lexer_consume_token (parser
->lexer
);
39184 cp_parser_error (parser
, "expected %<target%>");
39185 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39191 cp_parser_error (parser
, "expected %<declare%>");
39192 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39195 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39196 if (!scope_chain
->omp_declare_target_attribute
)
39197 error_at (pragma_tok
->location
,
39198 "%<#pragma omp end declare target%> without corresponding "
39199 "%<#pragma omp declare target%>");
39201 scope_chain
->omp_declare_target_attribute
--;
39204 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39205 expression and optional initializer clause of
39206 #pragma omp declare reduction. We store the expression(s) as
39207 either 3, 6 or 7 special statements inside of the artificial function's
39208 body. The first two statements are DECL_EXPRs for the artificial
39209 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39210 expression that uses those variables.
39211 If there was any INITIALIZER clause, this is followed by further statements,
39212 the fourth and fifth statements are DECL_EXPRs for the artificial
39213 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39214 constructor variant (first token after open paren is not omp_priv),
39215 then the sixth statement is a statement with the function call expression
39216 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39217 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39218 to initialize the OMP_PRIV artificial variable and there is seventh
39219 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39222 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
39224 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
39225 gcc_assert (TYPE_REF_P (type
));
39226 type
= TREE_TYPE (type
);
39227 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
39228 DECL_ARTIFICIAL (omp_out
) = 1;
39229 pushdecl (omp_out
);
39230 add_decl_expr (omp_out
);
39231 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
39232 DECL_ARTIFICIAL (omp_in
) = 1;
39234 add_decl_expr (omp_in
);
39236 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
39238 keep_next_level (true);
39239 tree block
= begin_omp_structured_block ();
39240 combiner
= cp_parser_expression (parser
);
39241 finish_expr_stmt (combiner
);
39242 block
= finish_omp_structured_block (block
);
39245 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
39248 const char *p
= "";
39249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39251 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39252 p
= IDENTIFIER_POINTER (id
);
39255 if (strcmp (p
, "initializer") == 0)
39257 cp_lexer_consume_token (parser
->lexer
);
39258 matching_parens parens
;
39259 if (!parens
.require_open (parser
))
39263 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39265 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39266 p
= IDENTIFIER_POINTER (id
);
39269 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
39270 DECL_ARTIFICIAL (omp_priv
) = 1;
39271 pushdecl (omp_priv
);
39272 add_decl_expr (omp_priv
);
39273 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
39274 DECL_ARTIFICIAL (omp_orig
) = 1;
39275 pushdecl (omp_orig
);
39276 add_decl_expr (omp_orig
);
39278 keep_next_level (true);
39279 block
= begin_omp_structured_block ();
39282 if (strcmp (p
, "omp_priv") == 0)
39284 bool is_direct_init
, is_non_constant_init
;
39286 cp_lexer_consume_token (parser
->lexer
);
39287 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39288 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
39289 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39290 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
39292 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
39293 == CPP_CLOSE_PAREN
))
39295 finish_omp_structured_block (block
);
39296 error ("invalid initializer clause");
39299 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
39300 &is_non_constant_init
);
39301 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
39302 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
39306 cp_parser_parse_tentatively (parser
);
39307 /* Don't create location wrapper nodes here. */
39308 auto_suppress_location_wrappers sentinel
;
39309 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
39310 /*check_dependency_p=*/true,
39311 /*template_p=*/NULL
,
39312 /*declarator_p=*/false,
39313 /*optional_p=*/false);
39314 vec
<tree
, va_gc
> *args
;
39315 if (fn_name
== error_mark_node
39316 || cp_parser_error_occurred (parser
)
39317 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39318 || ((args
= cp_parser_parenthesized_expression_list
39319 (parser
, non_attr
, /*cast_p=*/false,
39320 /*allow_expansion_p=*/true,
39321 /*non_constant_p=*/NULL
)),
39322 cp_parser_error_occurred (parser
)))
39324 finish_omp_structured_block (block
);
39325 cp_parser_abort_tentative_parse (parser
);
39326 cp_parser_error (parser
, "expected id-expression (arguments)");
39331 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
39332 if (arg
== omp_priv
39333 || (TREE_CODE (arg
) == ADDR_EXPR
39334 && TREE_OPERAND (arg
, 0) == omp_priv
))
39336 cp_parser_abort_tentative_parse (parser
);
39337 if (arg
== NULL_TREE
)
39338 error ("one of the initializer call arguments should be %<omp_priv%>"
39339 " or %<&omp_priv%>");
39340 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
39342 finish_expr_stmt (initializer
);
39345 block
= finish_omp_structured_block (block
);
39346 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
39350 add_decl_expr (omp_orig
);
39352 if (!parens
.require_close (parser
))
39356 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
39357 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
39364 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39365 initializer-clause[opt] new-line
39367 initializer-clause:
39368 initializer (omp_priv initializer)
39369 initializer (function-name (argument-list)) */
39372 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
39373 enum pragma_context
)
39375 auto_vec
<tree
> types
;
39376 enum tree_code reduc_code
= ERROR_MARK
;
39377 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
39379 cp_token
*first_token
;
39380 cp_token_cache
*cp
;
39384 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39385 p
= obstack_alloc (&declarator_obstack
, 0);
39387 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39390 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
39393 reduc_code
= PLUS_EXPR
;
39396 reduc_code
= MULT_EXPR
;
39399 reduc_code
= MINUS_EXPR
;
39402 reduc_code
= BIT_AND_EXPR
;
39405 reduc_code
= BIT_XOR_EXPR
;
39408 reduc_code
= BIT_IOR_EXPR
;
39411 reduc_code
= TRUTH_ANDIF_EXPR
;
39414 reduc_code
= TRUTH_ORIF_EXPR
;
39417 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
39420 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39421 "%<|%>, %<&&%>, %<||%> or identifier");
39425 if (reduc_code
!= ERROR_MARK
)
39426 cp_lexer_consume_token (parser
->lexer
);
39428 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
39429 if (reduc_id
== error_mark_node
)
39432 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39435 /* Types may not be defined in declare reduction type list. */
39436 const char *saved_message
;
39437 saved_message
= parser
->type_definition_forbidden_message
;
39438 parser
->type_definition_forbidden_message
39439 = G_("types may not be defined in declare reduction type list");
39440 bool saved_colon_corrects_to_scope_p
;
39441 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
39442 parser
->colon_corrects_to_scope_p
= false;
39443 bool saved_colon_doesnt_start_class_def_p
;
39444 saved_colon_doesnt_start_class_def_p
39445 = parser
->colon_doesnt_start_class_def_p
;
39446 parser
->colon_doesnt_start_class_def_p
= true;
39450 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39451 type
= cp_parser_type_id (parser
);
39452 if (type
== error_mark_node
)
39454 else if (ARITHMETIC_TYPE_P (type
)
39455 && (orig_reduc_id
== NULL_TREE
39456 || (TREE_CODE (type
) != COMPLEX_TYPE
39457 && (id_equal (orig_reduc_id
, "min")
39458 || id_equal (orig_reduc_id
, "max")))))
39459 error_at (loc
, "predeclared arithmetic type %qT in "
39460 "%<#pragma omp declare reduction%>", type
);
39461 else if (TREE_CODE (type
) == FUNCTION_TYPE
39462 || TREE_CODE (type
) == METHOD_TYPE
39463 || TREE_CODE (type
) == ARRAY_TYPE
)
39464 error_at (loc
, "function or array type %qT in "
39465 "%<#pragma omp declare reduction%>", type
);
39466 else if (TYPE_REF_P (type
))
39467 error_at (loc
, "reference type %qT in "
39468 "%<#pragma omp declare reduction%>", type
);
39469 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
39470 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
39471 "%<#pragma omp declare reduction%>", type
);
39473 types
.safe_push (type
);
39475 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39476 cp_lexer_consume_token (parser
->lexer
);
39481 /* Restore the saved message. */
39482 parser
->type_definition_forbidden_message
= saved_message
;
39483 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39484 parser
->colon_doesnt_start_class_def_p
39485 = saved_colon_doesnt_start_class_def_p
;
39487 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
39488 || types
.is_empty ())
39491 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39495 first_token
= cp_lexer_peek_token (parser
->lexer
);
39498 FOR_EACH_VEC_ELT (types
, i
, type
)
39501 = build_function_type_list (void_type_node
,
39502 cp_build_reference_type (type
, false),
39504 tree this_reduc_id
= reduc_id
;
39505 if (!dependent_type_p (type
))
39506 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
39507 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
39508 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
39509 DECL_ARTIFICIAL (fndecl
) = 1;
39510 DECL_EXTERNAL (fndecl
) = 1;
39511 DECL_DECLARED_INLINE_P (fndecl
) = 1;
39512 DECL_IGNORED_P (fndecl
) = 1;
39513 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
39514 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
39515 DECL_ATTRIBUTES (fndecl
)
39516 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
39517 DECL_ATTRIBUTES (fndecl
));
39518 if (processing_template_decl
)
39519 fndecl
= push_template_decl (fndecl
);
39520 bool block_scope
= false;
39521 tree block
= NULL_TREE
;
39522 if (current_function_decl
)
39524 block_scope
= true;
39525 DECL_CONTEXT (fndecl
) = global_namespace
;
39526 if (!processing_template_decl
)
39529 else if (current_class_type
)
39533 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39534 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39535 cp_lexer_consume_token (parser
->lexer
);
39536 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39538 cp
= cp_token_cache_new (first_token
,
39539 cp_lexer_peek_nth_token (parser
->lexer
,
39542 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
39543 finish_member_declaration (fndecl
);
39544 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
39545 DECL_PENDING_INLINE_P (fndecl
) = 1;
39546 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
39551 DECL_CONTEXT (fndecl
) = current_namespace
;
39555 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
39557 block
= begin_omp_structured_block ();
39560 cp_parser_push_lexer_for_tokens (parser
, cp
);
39561 parser
->lexer
->in_pragma
= true;
39563 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
39566 finish_function (/*inline_p=*/false);
39568 DECL_CONTEXT (fndecl
) = current_function_decl
;
39570 cp_parser_pop_lexer (parser
);
39574 cp_parser_pop_lexer (parser
);
39576 finish_function (/*inline_p=*/false);
39579 DECL_CONTEXT (fndecl
) = current_function_decl
;
39580 block
= finish_omp_structured_block (block
);
39581 if (TREE_CODE (block
) == BIND_EXPR
)
39582 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
39583 else if (TREE_CODE (block
) == STATEMENT_LIST
)
39584 DECL_SAVED_TREE (fndecl
) = block
;
39585 if (processing_template_decl
)
39586 add_decl_expr (fndecl
);
39588 cp_check_omp_declare_reduction (fndecl
);
39589 if (cp
== NULL
&& types
.length () > 1)
39590 cp
= cp_token_cache_new (first_token
,
39591 cp_lexer_peek_nth_token (parser
->lexer
, 2));
39592 if (errs
!= errorcount
)
39596 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39599 /* Free any declarators allocated. */
39600 obstack_free (&declarator_obstack
, p
);
39604 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39605 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39606 initializer-clause[opt] new-line
39607 #pragma omp declare target new-line */
39610 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
39611 enum pragma_context context
)
39613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39615 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39616 const char *p
= IDENTIFIER_POINTER (id
);
39618 if (strcmp (p
, "simd") == 0)
39620 cp_lexer_consume_token (parser
->lexer
);
39621 cp_parser_omp_declare_simd (parser
, pragma_tok
,
39625 cp_ensure_no_omp_declare_simd (parser
);
39626 if (strcmp (p
, "reduction") == 0)
39628 cp_lexer_consume_token (parser
->lexer
);
39629 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
39633 if (!flag_openmp
) /* flag_openmp_simd */
39635 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39638 if (strcmp (p
, "target") == 0)
39640 cp_lexer_consume_token (parser
->lexer
);
39641 cp_parser_omp_declare_target (parser
, pragma_tok
);
39645 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
39647 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39652 #pragma omp requires clauses[optseq] new-line */
39655 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
39658 enum omp_requires new_req
= (enum omp_requires
) 0;
39660 location_t loc
= pragma_tok
->location
;
39661 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39663 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39664 cp_lexer_consume_token (parser
->lexer
);
39668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39670 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39671 const char *p
= IDENTIFIER_POINTER (id
);
39672 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39673 enum omp_requires this_req
= (enum omp_requires
) 0;
39675 if (!strcmp (p
, "unified_address"))
39676 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
39677 else if (!strcmp (p
, "unified_shared_memory"))
39678 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
39679 else if (!strcmp (p
, "dynamic_allocators"))
39680 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
39681 else if (!strcmp (p
, "reverse_offload"))
39682 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
39683 else if (!strcmp (p
, "atomic_default_mem_order"))
39685 cp_lexer_consume_token (parser
->lexer
);
39687 matching_parens parens
;
39688 if (parens
.require_open (parser
))
39690 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39692 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39693 p
= IDENTIFIER_POINTER (id
);
39695 if (!strcmp (p
, "seq_cst"))
39697 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
39698 else if (!strcmp (p
, "relaxed"))
39700 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
39701 else if (!strcmp (p
, "acq_rel"))
39703 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
39707 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39708 "expected %<seq_cst%>, %<relaxed%> or "
39710 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
39712 cp_lexer_consume_token (parser
->lexer
);
39715 cp_lexer_consume_token (parser
->lexer
);
39717 if (!parens
.require_close (parser
))
39718 cp_parser_skip_to_closing_parenthesis (parser
,
39719 /*recovering=*/true,
39720 /*or_comma=*/false,
39726 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39734 error_at (cloc
, "expected %<unified_address%>, "
39735 "%<unified_shared_memory%>, "
39736 "%<dynamic_allocators%>, "
39737 "%<reverse_offload%> "
39738 "or %<atomic_default_mem_order%> clause");
39739 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39743 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
39744 "supported yet", p
);
39746 cp_lexer_consume_token (parser
->lexer
);
39749 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39751 if ((this_req
& new_req
) != 0)
39752 error_at (cloc
, "too many %qs clauses", p
);
39753 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
39754 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
39755 error_at (cloc
, "%qs clause used lexically after first "
39756 "target construct or offloading API", p
);
39758 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39760 error_at (cloc
, "too many %qs clauses",
39761 "atomic_default_mem_order");
39762 this_req
= (enum omp_requires
) 0;
39764 else if ((omp_requires_mask
39765 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39767 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
39768 " clause in a single compilation unit");
39770 = (enum omp_requires
)
39772 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
39774 else if ((omp_requires_mask
39775 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
39776 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
39777 "lexically after first %<atomic%> construct "
39778 "without memory order clause");
39779 new_req
= (enum omp_requires
) (new_req
| this_req
);
39781 = (enum omp_requires
) (omp_requires_mask
| this_req
);
39787 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39790 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
39796 #pragma omp taskloop taskloop-clause[optseq] new-line
39799 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39802 #define OMP_TASKLOOP_CLAUSE_MASK \
39803 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39821 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
39822 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39825 tree clauses
, sb
, ret
;
39827 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39829 strcat (p_name
, " taskloop");
39830 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
39831 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39833 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
39834 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
39836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39838 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39839 const char *p
= IDENTIFIER_POINTER (id
);
39841 if (strcmp (p
, "simd") == 0)
39843 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39844 if (cclauses
== NULL
)
39845 cclauses
= cclauses_buf
;
39847 cp_lexer_consume_token (parser
->lexer
);
39848 if (!flag_openmp
) /* flag_openmp_simd */
39849 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39851 sb
= begin_omp_structured_block ();
39852 save
= cp_parser_begin_omp_structured_block (parser
);
39853 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39855 cp_parser_end_omp_structured_block (parser
, save
);
39856 tree body
= finish_omp_structured_block (sb
);
39859 ret
= make_node (OMP_TASKLOOP
);
39860 TREE_TYPE (ret
) = void_type_node
;
39861 OMP_FOR_BODY (ret
) = body
;
39862 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39863 SET_EXPR_LOCATION (ret
, loc
);
39868 if (!flag_openmp
) /* flag_openmp_simd */
39870 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39874 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39878 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
39879 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39882 keep_next_level (true);
39883 sb
= begin_omp_structured_block ();
39884 save
= cp_parser_begin_omp_structured_block (parser
);
39886 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
39889 cp_parser_end_omp_structured_block (parser
, save
);
39890 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39897 # pragma acc routine oacc-routine-clause[optseq] new-line
39898 function-definition
39900 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39903 #define OACC_ROUTINE_CLAUSE_MASK \
39904 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39910 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39911 component, which must resolve to a declared namespace-scope
39912 function. The clauses are either processed directly (for a named
39913 function), or defered until the immediatley following declaration
39917 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
39918 enum pragma_context context
)
39920 gcc_checking_assert (context
== pragma_external
);
39921 /* The checking for "another pragma following this one" in the "no optional
39922 '( name )'" case makes sure that we dont re-enter. */
39923 gcc_checking_assert (parser
->oacc_routine
== NULL
);
39925 cp_oacc_routine_data data
;
39926 data
.error_seen
= false;
39927 data
.fndecl_seen
= false;
39928 data
.tokens
= vNULL
;
39929 data
.clauses
= NULL_TREE
;
39930 data
.loc
= pragma_tok
->location
;
39931 /* It is safe to take the address of a local variable; it will only be
39932 used while this scope is live. */
39933 parser
->oacc_routine
= &data
;
39935 /* Look for optional '( name )'. */
39936 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39938 matching_parens parens
;
39939 parens
.consume_open (parser
); /* '(' */
39941 /* We parse the name as an id-expression. If it resolves to
39942 anything other than a non-overloaded function at namespace
39943 scope, it's an error. */
39944 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39945 tree name
= cp_parser_id_expression (parser
,
39946 /*template_keyword_p=*/false,
39947 /*check_dependency_p=*/false,
39948 /*template_p=*/NULL
,
39949 /*declarator_p=*/false,
39950 /*optional_p=*/false);
39951 tree decl
= (identifier_p (name
)
39952 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
39954 if (name
!= error_mark_node
&& decl
== error_mark_node
)
39955 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
39957 if (decl
== error_mark_node
39958 || !parens
.require_close (parser
))
39960 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39961 parser
->oacc_routine
= NULL
;
39966 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
39967 "#pragma acc routine",
39968 cp_lexer_peek_token (parser
->lexer
));
39970 if (decl
&& is_overloaded_fn (decl
)
39971 && (TREE_CODE (decl
) != FUNCTION_DECL
39972 || DECL_FUNCTION_TEMPLATE_P (decl
)))
39974 error_at (name_loc
,
39975 "%<#pragma acc routine%> names a set of overloads");
39976 parser
->oacc_routine
= NULL
;
39980 /* Perhaps we should use the same rule as declarations in different
39982 if (!DECL_NAMESPACE_SCOPE_P (decl
))
39984 error_at (name_loc
,
39985 "%qD does not refer to a namespace scope function", decl
);
39986 parser
->oacc_routine
= NULL
;
39990 if (TREE_CODE (decl
) != FUNCTION_DECL
)
39992 error_at (name_loc
, "%qD does not refer to a function", decl
);
39993 parser
->oacc_routine
= NULL
;
39997 cp_finalize_oacc_routine (parser
, decl
, false);
39998 parser
->oacc_routine
= NULL
;
40000 else /* No optional '( name )'. */
40002 /* Store away all pragma tokens. */
40003 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
40004 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
40005 cp_lexer_consume_token (parser
->lexer
);
40006 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
40007 parser
->oacc_routine
->error_seen
= true;
40008 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40009 struct cp_token_cache
*cp
40010 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
40011 parser
->oacc_routine
->tokens
.safe_push (cp
);
40013 /* Emit a helpful diagnostic if there's another pragma following this
40015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
40017 cp_ensure_no_oacc_routine (parser
);
40018 data
.tokens
.release ();
40019 /* ..., and then just keep going. */
40023 /* We only have to consider the pragma_external case here. */
40024 cp_parser_declaration (parser
);
40025 if (parser
->oacc_routine
40026 && !parser
->oacc_routine
->fndecl_seen
)
40027 cp_ensure_no_oacc_routine (parser
);
40029 parser
->oacc_routine
= NULL
;
40030 data
.tokens
.release ();
40034 /* Finalize #pragma acc routine clauses after direct declarator has
40038 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
40040 struct cp_token_cache
*ce
;
40041 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
40043 if (!data
->error_seen
&& data
->fndecl_seen
)
40045 error_at (data
->loc
,
40046 "%<#pragma acc routine%> not immediately followed by "
40047 "a single function declaration or definition");
40048 data
->error_seen
= true;
40050 if (data
->error_seen
)
40053 gcc_checking_assert (data
->tokens
.length () == 1);
40054 ce
= data
->tokens
[0];
40056 cp_parser_push_lexer_for_tokens (parser
, ce
);
40057 parser
->lexer
->in_pragma
= true;
40058 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
40060 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40061 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
40062 parser
->oacc_routine
->clauses
40063 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
40064 "#pragma acc routine", pragma_tok
);
40065 cp_parser_pop_lexer (parser
);
40066 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40072 /* Apply any saved OpenACC routine clauses to a just-parsed
40076 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
40078 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
40080 /* Keep going if we're in error reporting mode. */
40081 if (parser
->oacc_routine
->error_seen
40082 || fndecl
== error_mark_node
)
40085 if (parser
->oacc_routine
->fndecl_seen
)
40087 error_at (parser
->oacc_routine
->loc
,
40088 "%<#pragma acc routine%> not immediately followed by"
40089 " a single function declaration or definition");
40090 parser
->oacc_routine
= NULL
;
40093 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
40095 cp_ensure_no_oacc_routine (parser
);
40099 if (oacc_get_fn_attrib (fndecl
))
40101 error_at (parser
->oacc_routine
->loc
,
40102 "%<#pragma acc routine%> already applied to %qD", fndecl
);
40103 parser
->oacc_routine
= NULL
;
40107 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
40109 error_at (parser
->oacc_routine
->loc
,
40111 ? G_("%<#pragma acc routine%> must be applied before use")
40112 : G_("%<#pragma acc routine%> must be applied before "
40114 parser
->oacc_routine
= NULL
;
40118 /* Process the routine's dimension clauses. */
40119 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
40120 oacc_replace_fn_attrib (fndecl
, dims
);
40122 /* Add an "omp declare target" attribute. */
40123 DECL_ATTRIBUTES (fndecl
)
40124 = tree_cons (get_identifier ("omp declare target"),
40125 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
40127 /* Don't unset parser->oacc_routine here: we may still need it to
40128 diagnose wrong usage. But, remember that we've used this "#pragma acc
40130 parser
->oacc_routine
->fndecl_seen
= true;
40134 /* Main entry point to OpenMP statement pragmas. */
40137 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40140 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
40141 omp_clause_mask
mask (0);
40143 switch (cp_parser_pragma_kind (pragma_tok
))
40145 case PRAGMA_OACC_ATOMIC
:
40146 cp_parser_omp_atomic (parser
, pragma_tok
);
40148 case PRAGMA_OACC_CACHE
:
40149 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
40151 case PRAGMA_OACC_DATA
:
40152 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
40154 case PRAGMA_OACC_ENTER_DATA
:
40155 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
40157 case PRAGMA_OACC_EXIT_DATA
:
40158 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
40160 case PRAGMA_OACC_HOST_DATA
:
40161 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
40163 case PRAGMA_OACC_KERNELS
:
40164 case PRAGMA_OACC_PARALLEL
:
40165 strcpy (p_name
, "#pragma acc");
40166 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
40169 case PRAGMA_OACC_LOOP
:
40170 strcpy (p_name
, "#pragma acc");
40171 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40174 case PRAGMA_OACC_UPDATE
:
40175 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
40177 case PRAGMA_OACC_WAIT
:
40178 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
40180 case PRAGMA_OMP_ATOMIC
:
40181 cp_parser_omp_atomic (parser
, pragma_tok
);
40183 case PRAGMA_OMP_CRITICAL
:
40184 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
40186 case PRAGMA_OMP_DISTRIBUTE
:
40187 strcpy (p_name
, "#pragma omp");
40188 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
40191 case PRAGMA_OMP_FOR
:
40192 strcpy (p_name
, "#pragma omp");
40193 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
40196 case PRAGMA_OMP_MASTER
:
40197 strcpy (p_name
, "#pragma omp");
40198 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
40201 case PRAGMA_OMP_PARALLEL
:
40202 strcpy (p_name
, "#pragma omp");
40203 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
40206 case PRAGMA_OMP_SECTIONS
:
40207 strcpy (p_name
, "#pragma omp");
40208 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
40210 case PRAGMA_OMP_SIMD
:
40211 strcpy (p_name
, "#pragma omp");
40212 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
40215 case PRAGMA_OMP_SINGLE
:
40216 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
40218 case PRAGMA_OMP_TASK
:
40219 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
40221 case PRAGMA_OMP_TASKGROUP
:
40222 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
40224 case PRAGMA_OMP_TASKLOOP
:
40225 strcpy (p_name
, "#pragma omp");
40226 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40229 case PRAGMA_OMP_TEAMS
:
40230 strcpy (p_name
, "#pragma omp");
40231 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
40235 gcc_unreachable ();
40238 protected_set_expr_location (stmt
, pragma_tok
->location
);
40241 /* Transactional Memory parsing routines. */
40243 /* Parse a transaction attribute.
40249 We use this instead of cp_parser_attributes_opt for transactions to avoid
40250 the pedwarn in C++98 mode. */
40253 cp_parser_txn_attribute_opt (cp_parser
*parser
)
40256 tree attr_name
, attr
= NULL
;
40258 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
40259 return cp_parser_attributes_opt (parser
);
40261 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
40263 cp_lexer_consume_token (parser
->lexer
);
40264 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
40267 token
= cp_lexer_peek_token (parser
->lexer
);
40268 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
40270 token
= cp_lexer_consume_token (parser
->lexer
);
40272 attr_name
= (token
->type
== CPP_KEYWORD
40273 /* For keywords, use the canonical spelling,
40274 not the parsed identifier. */
40275 ? ridpointers
[(int) token
->keyword
]
40277 attr
= build_tree_list (attr_name
, NULL_TREE
);
40280 cp_parser_error (parser
, "expected identifier");
40282 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40284 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40288 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40290 transaction-statement:
40291 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40293 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40297 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
40299 unsigned char old_in
= parser
->in_transaction
;
40300 unsigned char this_in
= 1, new_in
;
40301 enum rid keyword
= token
->keyword
;
40302 tree stmt
, attrs
, noex
;
40304 cp_lexer_consume_token (parser
->lexer
);
40306 if (keyword
== RID_TRANSACTION_RELAXED
40307 || keyword
== RID_SYNCHRONIZED
)
40308 this_in
|= TM_STMT_ATTR_RELAXED
;
40311 attrs
= cp_parser_txn_attribute_opt (parser
);
40313 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40316 /* Parse a noexcept specification. */
40317 if (keyword
== RID_ATOMIC_NOEXCEPT
)
40318 noex
= boolean_true_node
;
40319 else if (keyword
== RID_ATOMIC_CANCEL
)
40321 /* cancel-and-throw is unimplemented. */
40322 sorry ("atomic_cancel");
40326 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
40328 /* Keep track if we're in the lexical scope of an outer transaction. */
40329 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
40331 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
40333 parser
->in_transaction
= new_in
;
40334 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
40335 parser
->in_transaction
= old_in
;
40337 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
40342 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40344 transaction-expression:
40345 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40346 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40350 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
40352 unsigned char old_in
= parser
->in_transaction
;
40353 unsigned char this_in
= 1;
40357 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40359 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40360 || keyword
== RID_TRANSACTION_RELAXED
);
40364 keyword
== RID_TRANSACTION_RELAXED
40365 ? G_("%<__transaction_relaxed%> without transactional memory "
40367 : G_("%<__transaction_atomic%> without transactional memory "
40368 "support enabled"));
40370 token
= cp_parser_require_keyword (parser
, keyword
,
40371 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40372 : RT_TRANSACTION_RELAXED
));
40373 gcc_assert (token
!= NULL
);
40375 if (keyword
== RID_TRANSACTION_RELAXED
)
40376 this_in
|= TM_STMT_ATTR_RELAXED
;
40378 /* Set this early. This might mean that we allow transaction_cancel in
40379 an expression that we find out later actually has to be a constexpr.
40380 However, we expect that cxx_constant_value will be able to deal with
40381 this; also, if the noexcept has no constexpr, then what we parse next
40382 really is a transaction's body. */
40383 parser
->in_transaction
= this_in
;
40385 /* Parse a noexcept specification. */
40386 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
40389 if (!noex
|| !noex_expr
40390 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
40392 matching_parens parens
;
40393 parens
.require_open (parser
);
40395 expr
= cp_parser_expression (parser
);
40396 expr
= finish_parenthesized_expr (expr
);
40398 parens
.require_close (parser
);
40402 /* The only expression that is available got parsed for the noexcept
40403 already. noexcept is true then. */
40405 noex
= boolean_true_node
;
40408 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
40409 parser
->in_transaction
= old_in
;
40411 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
40412 return error_mark_node
;
40414 return (flag_tm
? expr
: error_mark_node
);
40417 /* Parse a function-transaction-block.
40419 function-transaction-block:
40420 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40422 __transaction_atomic txn-attribute[opt] function-try-block
40423 __transaction_relaxed ctor-initializer[opt] function-body
40424 __transaction_relaxed function-try-block
40428 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
40430 unsigned char old_in
= parser
->in_transaction
;
40431 unsigned char new_in
= 1;
40432 tree compound_stmt
, stmt
, attrs
;
40435 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40436 || keyword
== RID_TRANSACTION_RELAXED
);
40437 token
= cp_parser_require_keyword (parser
, keyword
,
40438 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40439 : RT_TRANSACTION_RELAXED
));
40440 gcc_assert (token
!= NULL
);
40442 if (keyword
== RID_TRANSACTION_RELAXED
)
40443 new_in
|= TM_STMT_ATTR_RELAXED
;
40446 attrs
= cp_parser_txn_attribute_opt (parser
);
40448 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40451 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
40453 parser
->in_transaction
= new_in
;
40455 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
40456 cp_parser_function_try_block (parser
);
40458 cp_parser_ctor_initializer_opt_and_function_body
40459 (parser
, /*in_function_try_block=*/false);
40461 parser
->in_transaction
= old_in
;
40463 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
40466 /* Parse a __transaction_cancel statement.
40469 __transaction_cancel txn-attribute[opt] ;
40470 __transaction_cancel txn-attribute[opt] throw-expression ;
40472 ??? Cancel and throw is not yet implemented. */
40475 cp_parser_transaction_cancel (cp_parser
*parser
)
40478 bool is_outer
= false;
40481 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
40482 RT_TRANSACTION_CANCEL
);
40483 gcc_assert (token
!= NULL
);
40485 attrs
= cp_parser_txn_attribute_opt (parser
);
40487 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
40489 /* ??? Parse cancel-and-throw here. */
40491 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
40495 error_at (token
->location
, "%<__transaction_cancel%> without "
40496 "transactional memory support enabled");
40497 return error_mark_node
;
40499 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
40501 error_at (token
->location
, "%<__transaction_cancel%> within a "
40502 "%<__transaction_relaxed%>");
40503 return error_mark_node
;
40507 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
40508 && !is_tm_may_cancel_outer (current_function_decl
))
40510 error_at (token
->location
, "outer %<__transaction_cancel%> not "
40511 "within outer %<__transaction_atomic%>");
40512 error_at (token
->location
,
40513 " or a %<transaction_may_cancel_outer%> function");
40514 return error_mark_node
;
40517 else if (parser
->in_transaction
== 0)
40519 error_at (token
->location
, "%<__transaction_cancel%> not within "
40520 "%<__transaction_atomic%>");
40521 return error_mark_node
;
40524 stmt
= build_tm_abort_call (token
->location
, is_outer
);
40532 static GTY (()) cp_parser
*the_parser
;
40535 /* Special handling for the first token or line in the file. The first
40536 thing in the file might be #pragma GCC pch_preprocess, which loads a
40537 PCH file, which is a GC collection point. So we need to handle this
40538 first pragma without benefit of an existing lexer structure.
40540 Always returns one token to the caller in *FIRST_TOKEN. This is
40541 either the true first token of the file, or the first token after
40542 the initial pragma. */
40545 cp_parser_initial_pragma (cp_token
*first_token
)
40549 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40550 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
40553 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40554 if (first_token
->type
== CPP_STRING
)
40556 name
= first_token
->u
.value
;
40558 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40559 if (first_token
->type
!= CPP_PRAGMA_EOL
)
40560 error_at (first_token
->location
,
40561 "junk at end of %<#pragma GCC pch_preprocess%>");
40564 error_at (first_token
->location
, "expected string literal");
40566 /* Skip to the end of the pragma. */
40567 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
40568 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40570 /* Now actually load the PCH file. */
40572 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
40574 /* Read one more token to return to our caller. We have to do this
40575 after reading the PCH file in, since its pointers have to be
40577 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40580 /* Parse a pragma GCC ivdep. */
40583 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
40585 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40589 /* Parse a pragma GCC unroll. */
40591 static unsigned short
40592 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
40594 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
40595 tree expr
= cp_parser_constant_expression (parser
);
40596 unsigned short unroll
;
40597 expr
= maybe_constant_value (expr
);
40598 HOST_WIDE_INT lunroll
= 0;
40599 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
40600 || TREE_CODE (expr
) != INTEGER_CST
40601 || (lunroll
= tree_to_shwi (expr
)) < 0
40602 || lunroll
>= USHRT_MAX
)
40604 error_at (location
, "%<#pragma GCC unroll%> requires an"
40605 " assignment-expression that evaluates to a non-negative"
40606 " integral constant less than %u", USHRT_MAX
);
40611 unroll
= (unsigned short)lunroll
;
40615 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40619 /* Normal parsing of a pragma token. Here we can (and must) use the
40623 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
40625 cp_token
*pragma_tok
;
40630 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40631 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
40632 parser
->lexer
->in_pragma
= true;
40634 id
= cp_parser_pragma_kind (pragma_tok
);
40635 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
40636 cp_ensure_no_omp_declare_simd (parser
);
40639 case PRAGMA_GCC_PCH_PREPROCESS
:
40640 error_at (pragma_tok
->location
,
40641 "%<#pragma GCC pch_preprocess%> must be first");
40644 case PRAGMA_OMP_BARRIER
:
40647 case pragma_compound
:
40648 cp_parser_omp_barrier (parser
, pragma_tok
);
40651 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40652 "used in compound statements", "omp barrier");
40659 case PRAGMA_OMP_DEPOBJ
:
40662 case pragma_compound
:
40663 cp_parser_omp_depobj (parser
, pragma_tok
);
40666 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40667 "used in compound statements", "omp depobj");
40674 case PRAGMA_OMP_FLUSH
:
40677 case pragma_compound
:
40678 cp_parser_omp_flush (parser
, pragma_tok
);
40681 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40682 "used in compound statements", "omp flush");
40689 case PRAGMA_OMP_TASKWAIT
:
40692 case pragma_compound
:
40693 cp_parser_omp_taskwait (parser
, pragma_tok
);
40696 error_at (pragma_tok
->location
,
40697 "%<#pragma %s%> may only be used in compound statements",
40705 case PRAGMA_OMP_TASKYIELD
:
40708 case pragma_compound
:
40709 cp_parser_omp_taskyield (parser
, pragma_tok
);
40712 error_at (pragma_tok
->location
,
40713 "%<#pragma %s%> may only be used in compound statements",
40721 case PRAGMA_OMP_CANCEL
:
40724 case pragma_compound
:
40725 cp_parser_omp_cancel (parser
, pragma_tok
);
40728 error_at (pragma_tok
->location
,
40729 "%<#pragma %s%> may only be used in compound statements",
40737 case PRAGMA_OMP_CANCELLATION_POINT
:
40738 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
40741 case PRAGMA_OMP_THREADPRIVATE
:
40742 cp_parser_omp_threadprivate (parser
, pragma_tok
);
40745 case PRAGMA_OMP_DECLARE
:
40746 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
40748 case PRAGMA_OACC_DECLARE
:
40749 cp_parser_oacc_declare (parser
, pragma_tok
);
40752 case PRAGMA_OACC_ENTER_DATA
:
40753 if (context
== pragma_stmt
)
40755 error_at (pragma_tok
->location
,
40756 "%<#pragma %s%> may only be used in compound statements",
40760 else if (context
!= pragma_compound
)
40762 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40765 case PRAGMA_OACC_EXIT_DATA
:
40766 if (context
== pragma_stmt
)
40768 error_at (pragma_tok
->location
,
40769 "%<#pragma %s%> may only be used in compound statements",
40773 else if (context
!= pragma_compound
)
40775 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40778 case PRAGMA_OACC_ROUTINE
:
40779 if (context
!= pragma_external
)
40781 error_at (pragma_tok
->location
,
40782 "%<#pragma acc routine%> must be at file scope");
40785 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
40788 case PRAGMA_OACC_UPDATE
:
40789 if (context
== pragma_stmt
)
40791 error_at (pragma_tok
->location
,
40792 "%<#pragma %s%> may only be used in compound statements",
40796 else if (context
!= pragma_compound
)
40798 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40801 case PRAGMA_OACC_WAIT
:
40802 if (context
== pragma_stmt
)
40804 error_at (pragma_tok
->location
,
40805 "%<#pragma %s%> may only be used in compound statements",
40809 else if (context
!= pragma_compound
)
40811 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40814 case PRAGMA_OACC_ATOMIC
:
40815 case PRAGMA_OACC_CACHE
:
40816 case PRAGMA_OACC_DATA
:
40817 case PRAGMA_OACC_HOST_DATA
:
40818 case PRAGMA_OACC_KERNELS
:
40819 case PRAGMA_OACC_PARALLEL
:
40820 case PRAGMA_OACC_LOOP
:
40821 case PRAGMA_OMP_ATOMIC
:
40822 case PRAGMA_OMP_CRITICAL
:
40823 case PRAGMA_OMP_DISTRIBUTE
:
40824 case PRAGMA_OMP_FOR
:
40825 case PRAGMA_OMP_MASTER
:
40826 case PRAGMA_OMP_PARALLEL
:
40827 case PRAGMA_OMP_SECTIONS
:
40828 case PRAGMA_OMP_SIMD
:
40829 case PRAGMA_OMP_SINGLE
:
40830 case PRAGMA_OMP_TASK
:
40831 case PRAGMA_OMP_TASKGROUP
:
40832 case PRAGMA_OMP_TASKLOOP
:
40833 case PRAGMA_OMP_TEAMS
:
40834 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40836 stmt
= push_omp_privatization_clauses (false);
40837 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40838 pop_omp_privatization_clauses (stmt
);
40841 case PRAGMA_OMP_REQUIRES
:
40842 return cp_parser_omp_requires (parser
, pragma_tok
);
40844 case PRAGMA_OMP_ORDERED
:
40845 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40847 stmt
= push_omp_privatization_clauses (false);
40848 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
40849 pop_omp_privatization_clauses (stmt
);
40852 case PRAGMA_OMP_TARGET
:
40853 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40855 stmt
= push_omp_privatization_clauses (false);
40856 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
40857 pop_omp_privatization_clauses (stmt
);
40860 case PRAGMA_OMP_END_DECLARE_TARGET
:
40861 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
40864 case PRAGMA_OMP_SECTION
:
40865 error_at (pragma_tok
->location
,
40866 "%<#pragma omp section%> may only be used in "
40867 "%<#pragma omp sections%> construct");
40872 if (context
== pragma_external
)
40874 error_at (pragma_tok
->location
,
40875 "%<#pragma GCC ivdep%> must be inside a function");
40878 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
40879 unsigned short unroll
;
40880 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40881 if (tok
->type
== CPP_PRAGMA
40882 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
40884 tok
= cp_lexer_consume_token (parser
->lexer
);
40885 unroll
= cp_parser_pragma_unroll (parser
, tok
);
40886 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40890 if (tok
->type
!= CPP_KEYWORD
40891 || (tok
->keyword
!= RID_FOR
40892 && tok
->keyword
!= RID_WHILE
40893 && tok
->keyword
!= RID_DO
))
40895 cp_parser_error (parser
, "for, while or do statement expected");
40898 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40902 case PRAGMA_UNROLL
:
40904 if (context
== pragma_external
)
40906 error_at (pragma_tok
->location
,
40907 "%<#pragma GCC unroll%> must be inside a function");
40910 const unsigned short unroll
40911 = cp_parser_pragma_unroll (parser
, pragma_tok
);
40913 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40914 if (tok
->type
== CPP_PRAGMA
40915 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
40917 tok
= cp_lexer_consume_token (parser
->lexer
);
40918 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
40919 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40923 if (tok
->type
!= CPP_KEYWORD
40924 || (tok
->keyword
!= RID_FOR
40925 && tok
->keyword
!= RID_WHILE
40926 && tok
->keyword
!= RID_DO
))
40928 cp_parser_error (parser
, "for, while or do statement expected");
40931 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40936 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
40937 c_invoke_pragma_handler (id
);
40941 cp_parser_error (parser
, "expected declaration specifiers");
40945 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40949 /* The interface the pragma parsers have to the lexer. */
40952 pragma_lex (tree
*value
, location_t
*loc
)
40954 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40955 enum cpp_ttype ret
= tok
->type
;
40957 *value
= tok
->u
.value
;
40959 *loc
= tok
->location
;
40961 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
40963 else if (ret
== CPP_STRING
)
40964 *value
= cp_parser_string_literal (the_parser
, false, false);
40967 if (ret
== CPP_KEYWORD
)
40969 cp_lexer_consume_token (the_parser
->lexer
);
40976 /* External interface. */
40978 /* Parse one entire translation unit. */
40981 c_parse_file (void)
40983 static bool already_called
= false;
40985 if (already_called
)
40986 fatal_error (input_location
,
40987 "inter-module optimizations not implemented for C++");
40988 already_called
= true;
40990 the_parser
= cp_parser_new ();
40991 push_deferring_access_checks (flag_access_control
40992 ? dk_no_deferred
: dk_no_check
);
40993 cp_parser_translation_unit (the_parser
);
40996 finish_translation_unit ();
40999 /* Create an identifier for a generic parameter type (a synthesized
41000 template parameter implied by `auto' or a concept identifier). */
41002 static GTY(()) int generic_parm_count
;
41004 make_generic_type_name ()
41007 sprintf (buf
, "auto:%d", ++generic_parm_count
);
41008 return get_identifier (buf
);
41011 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41012 (creating a new template parameter list if necessary). Returns the newly
41013 created template type parm. */
41016 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
41018 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
41020 /* Before committing to modifying any scope, if we're in an
41021 implicit template scope, and we're trying to synthesize a
41022 constrained parameter, try to find a previous parameter with
41023 the same name. This is the same-type rule for abbreviated
41024 function templates.
41026 NOTE: We can generate implicit parameters when tentatively
41027 parsing a nested name specifier, only to reject that parse
41028 later. However, matching the same template-id as part of a
41029 direct-declarator should generate an identical template
41030 parameter, so this rule will merge them. */
41031 if (parser
->implicit_template_scope
&& constr
)
41033 tree t
= parser
->implicit_template_parms
;
41036 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
41038 tree d
= TREE_VALUE (t
);
41039 if (TREE_CODE (d
) == PARM_DECL
)
41040 /* Return the TEMPLATE_PARM_INDEX. */
41041 d
= DECL_INITIAL (d
);
41044 t
= TREE_CHAIN (t
);
41048 /* We are either continuing a function template that already contains implicit
41049 template parameters, creating a new fully-implicit function template, or
41050 extending an existing explicit function template with implicit template
41053 cp_binding_level
*const entry_scope
= current_binding_level
;
41055 bool become_template
= false;
41056 cp_binding_level
*parent_scope
= 0;
41058 if (parser
->implicit_template_scope
)
41060 gcc_assert (parser
->implicit_template_parms
);
41062 current_binding_level
= parser
->implicit_template_scope
;
41066 /* Roll back to the existing template parameter scope (in the case of
41067 extending an explicit function template) or introduce a new template
41068 parameter scope ahead of the function parameter scope (or class scope
41069 in the case of out-of-line member definitions). The function scope is
41070 added back after template parameter synthesis below. */
41072 cp_binding_level
*scope
= entry_scope
;
41074 while (scope
->kind
== sk_function_parms
)
41076 parent_scope
= scope
;
41077 scope
= scope
->level_chain
;
41079 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
41081 /* If not defining a class, then any class scope is a scope level in
41082 an out-of-line member definition. In this case simply wind back
41083 beyond the first such scope to inject the template parameter list.
41084 Otherwise wind back to the class being defined. The latter can
41085 occur in class member friend declarations such as:
41091 friend void A::foo (auto);
41094 The template parameter list synthesized for the friend declaration
41095 must be injected in the scope of 'B'. This can also occur in
41096 erroneous cases such as:
41102 void B::foo (auto) {}
41105 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41106 but, nevertheless, the template parameter list synthesized for the
41107 declarator should be injected into the scope of 'A' as if the
41108 ill-formed template was specified explicitly. */
41110 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
41112 parent_scope
= scope
;
41113 scope
= scope
->level_chain
;
41117 current_binding_level
= scope
;
41119 if (scope
->kind
!= sk_template_parms
41120 || !function_being_declared_is_template_p (parser
))
41122 /* Introduce a new template parameter list for implicit template
41125 become_template
= true;
41127 parser
->implicit_template_scope
41128 = begin_scope (sk_template_parms
, NULL
);
41130 ++processing_template_decl
;
41132 parser
->fully_implicit_function_template_p
= true;
41133 ++parser
->num_template_parameter_lists
;
41137 /* Synthesize implicit template parameters at the end of the explicit
41138 template parameter list. */
41140 gcc_assert (current_template_parms
);
41142 parser
->implicit_template_scope
= scope
;
41144 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41145 parser
->implicit_template_parms
41146 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
41150 /* Synthesize a new template parameter and track the current template
41151 parameter chain with implicit_template_parms. */
41153 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
41154 tree synth_id
= make_generic_type_name ();
41155 tree synth_tmpl_parm
;
41156 bool non_type
= false;
41158 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
41160 = finish_template_type_parm (class_type_node
, synth_id
);
41161 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
41163 = finish_constrained_template_template_parm (proto
, synth_id
);
41166 synth_tmpl_parm
= copy_decl (proto
);
41167 DECL_NAME (synth_tmpl_parm
) = synth_id
;
41171 // Attach the constraint to the parm before processing.
41172 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
41173 TREE_TYPE (node
) = constr
;
41175 = process_template_parm (parser
->implicit_template_parms
,
41178 /*non_type=*/non_type
,
41179 /*param_pack=*/false);
41181 // Chain the new parameter to the list of implicit parameters.
41182 if (parser
->implicit_template_parms
)
41183 parser
->implicit_template_parms
41184 = TREE_CHAIN (parser
->implicit_template_parms
);
41186 parser
->implicit_template_parms
= new_parm
;
41188 tree new_decl
= get_local_decls ();
41190 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41191 new_decl
= DECL_INITIAL (new_decl
);
41193 /* If creating a fully implicit function template, start the new implicit
41194 template parameter list with this synthesized type, otherwise grow the
41195 current template parameter list. */
41197 if (become_template
)
41199 parent_scope
->level_chain
= current_binding_level
;
41201 tree new_parms
= make_tree_vec (1);
41202 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
41203 current_template_parms
= tree_cons (size_int (processing_template_decl
),
41204 new_parms
, current_template_parms
);
41208 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41209 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
41210 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
41211 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
41214 // If the new parameter was constrained, we need to add that to the
41215 // constraints in the template parameter list.
41216 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
41218 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
41219 reqs
= conjoin_constraints (reqs
, req
);
41220 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
41223 current_binding_level
= entry_scope
;
41228 /* Finish the declaration of a fully implicit function template. Such a
41229 template has no explicit template parameter list so has not been through the
41230 normal template head and tail processing. synthesize_implicit_template_parm
41231 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41232 provided if the declaration is a class member such that its template
41233 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41234 form is returned. Otherwise NULL_TREE is returned. */
41237 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
41239 gcc_assert (parser
->fully_implicit_function_template_p
);
41241 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
41242 && DECL_VIRTUAL_P (member_decl_opt
))
41244 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
41245 "implicit templates may not be %<virtual%>");
41246 DECL_VIRTUAL_P (member_decl_opt
) = false;
41249 if (member_decl_opt
)
41250 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
41251 end_template_decl ();
41253 parser
->fully_implicit_function_template_p
= false;
41254 parser
->implicit_template_parms
= 0;
41255 parser
->implicit_template_scope
= 0;
41256 --parser
->num_template_parameter_lists
;
41258 return member_decl_opt
;
41261 /* Like finish_fully_implicit_template, but to be used in error
41262 recovery, rearranging scopes so that we restore the state we had
41263 before synthesize_implicit_template_parm inserted the implement
41264 template parms scope. */
41267 abort_fully_implicit_template (cp_parser
*parser
)
41269 cp_binding_level
*return_to_scope
= current_binding_level
;
41271 if (parser
->implicit_template_scope
41272 && return_to_scope
!= parser
->implicit_template_scope
)
41274 cp_binding_level
*child
= return_to_scope
;
41275 for (cp_binding_level
*scope
= child
->level_chain
;
41276 scope
!= parser
->implicit_template_scope
;
41277 scope
= child
->level_chain
)
41279 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
41280 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
41281 current_binding_level
= parser
->implicit_template_scope
;
41284 return_to_scope
= return_to_scope
->level_chain
;
41286 finish_fully_implicit_template (parser
, NULL
);
41288 gcc_assert (current_binding_level
== return_to_scope
);
41291 /* Helper function for diagnostics that have complained about things
41292 being used with 'extern "C"' linkage.
41294 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41297 maybe_show_extern_c_location (void)
41299 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
41300 inform (the_parser
->innermost_linkage_specification_location
,
41301 "%<extern \"C\"%> linkage started here");
41304 #include "gt-cp-parser.h"