2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
55 static cp_token eof_token
=
57 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant
{
63 /* floating-point literal */
67 /* %<__FUNCTION__%> */
69 /* %<__PRETTY_FUNCTION__%> */
77 /* %<typeid%> operator */
79 /* non-constant compound literals */
87 /* an array reference */
93 /* the address of a label */
107 /* calls to overloaded operators */
111 /* a comma operator */
113 /* a call to a constructor */
115 /* a transaction expression */
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error
{
125 /* is not a class or namespace */
127 /* is not a class, namespace, or enumeration */
131 /* The various kinds of required token */
132 enum required_token
{
134 RT_SEMICOLON
, /* ';' */
135 RT_OPEN_PAREN
, /* '(' */
136 RT_CLOSE_BRACE
, /* '}' */
137 RT_OPEN_BRACE
, /* '{' */
138 RT_CLOSE_SQUARE
, /* ']' */
139 RT_OPEN_SQUARE
, /* '[' */
143 RT_GREATER
, /* '>' */
145 RT_ELLIPSIS
, /* '...' */
149 RT_COLON_SCOPE
, /* ':' or '::' */
150 RT_CLOSE_PAREN
, /* ')' */
151 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
152 RT_PRAGMA_EOL
, /* end of line */
153 RT_NAME
, /* identifier */
155 /* The type is CPP_KEYWORD */
157 RT_DELETE
, /* delete */
158 RT_RETURN
, /* return */
159 RT_WHILE
, /* while */
160 RT_EXTERN
, /* extern */
161 RT_STATIC_ASSERT
, /* static_assert */
162 RT_DECLTYPE
, /* decltype */
163 RT_OPERATOR
, /* operator */
164 RT_CLASS
, /* class */
165 RT_TEMPLATE
, /* template */
166 RT_NAMESPACE
, /* namespace */
167 RT_USING
, /* using */
170 RT_CATCH
, /* catch */
171 RT_THROW
, /* throw */
172 RT_LABEL
, /* __label__ */
173 RT_AT_TRY
, /* @try */
174 RT_AT_SYNCHRONIZED
, /* @synchronized */
175 RT_AT_THROW
, /* @throw */
177 RT_SELECT
, /* selection-statement */
178 RT_ITERATION
, /* iteration-statement */
179 RT_JUMP
, /* jump-statement */
180 RT_CLASS_KEY
, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
190 class type_id_in_expr_sentinel
195 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
197 saved (parser
->in_type_id_in_expr_p
)
198 { parser
->in_type_id_in_expr_p
= set
; }
199 ~type_id_in_expr_sentinel ()
200 { parser
->in_type_id_in_expr_p
= saved
; }
205 static cp_lexer
*cp_lexer_new_main
207 static cp_lexer
*cp_lexer_new_from_tokens
208 (cp_token_cache
*tokens
);
209 static void cp_lexer_destroy
211 static int cp_lexer_saving_tokens
213 static cp_token
*cp_lexer_token_at
214 (cp_lexer
*, cp_token_position
);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer
*, cp_token
*);
217 static inline cp_token
*cp_lexer_peek_token
219 static cp_token
*cp_lexer_peek_nth_token
220 (cp_lexer
*, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer
*, enum cpp_ttype
);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer
*, enum cpp_ttype
);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer
*, enum rid
);
227 static cp_token
*cp_lexer_consume_token
229 static void cp_lexer_purge_token
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer
*, cp_token_position
);
233 static void cp_lexer_save_tokens
235 static void cp_lexer_commit_tokens
237 static void cp_lexer_rollback_tokens
239 static void cp_lexer_print_token
240 (FILE *, cp_token
*);
241 static inline bool cp_lexer_debugging_p
243 static void cp_lexer_start_debugging
244 (cp_lexer
*) ATTRIBUTE_UNUSED
;
245 static void cp_lexer_stop_debugging
246 (cp_lexer
*) ATTRIBUTE_UNUSED
;
248 static cp_token_cache
*cp_token_cache_new
249 (cp_token
*, cp_token
*);
251 static void cp_parser_initial_pragma
254 static bool cp_parser_omp_declare_reduction_exprs
256 static void cp_finalize_oacc_routine
257 (cp_parser
*, tree
, bool);
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream
;
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand
;
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
279 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
280 cp_token
*start_token
, unsigned num
,
281 cp_token
*curr_token
)
283 unsigned i
, nprinted
;
287 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
293 num
= buffer
->length ();
295 if (start_token
== NULL
)
296 start_token
= buffer
->address ();
298 if (start_token
> buffer
->address ())
300 cp_lexer_print_token (file
, &(*buffer
)[0]);
301 fprintf (file
, " ... ");
306 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
308 if (token
== start_token
)
315 if (token
== curr_token
)
316 fprintf (file
, "[[");
318 cp_lexer_print_token (file
, token
);
320 if (token
== curr_token
)
321 fprintf (file
, "]]");
327 case CPP_CLOSE_BRACE
:
337 if (i
== num
&& i
< buffer
->length ())
339 fprintf (file
, " ... ");
340 cp_lexer_print_token (file
, &buffer
->last ());
343 fprintf (file
, "\n");
347 /* Dump all tokens in BUFFER to stderr. */
350 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
352 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
356 debug (vec
<cp_token
, va_gc
> &ref
)
358 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
362 debug (vec
<cp_token
, va_gc
> *ptr
)
367 fprintf (stderr
, "<nil>\n");
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
375 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
379 fprintf (file
, "%s: ", desc
);
380 print_node_brief (file
, "", t
, 0);
385 /* Dump parser context C to FILE. */
388 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
390 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
392 print_node_brief (file
, "", c
->object_type
, 0);
393 fprintf (file
, "}\n");
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
400 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
403 cp_parser_context
*c
;
405 fprintf (file
, "Parsing context stack:\n");
406 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
408 fprintf (file
, "\t#%u: ", i
);
409 cp_debug_print_context (file
, c
);
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
417 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
420 fprintf (file
, "%s: true\n", desc
);
424 /* Print an unparsed function entry UF to FILE. */
427 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
430 cp_default_arg_entry
*default_arg_fn
;
433 fprintf (file
, "\tFunctions with default args:\n");
435 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
438 fprintf (file
, "\t\tClass type: ");
439 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
440 fprintf (file
, "\t\tDeclaration: ");
441 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
442 fprintf (file
, "\n");
445 fprintf (file
, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
449 print_node_brief (file
, "", fn
, 0);
452 fprintf (file
, "\n");
454 fprintf (file
, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
458 print_node_brief (file
, "", fn
, 0);
461 fprintf (file
, "\n");
465 /* Print the stack of unparsed member functions S to FILE. */
468 cp_debug_print_unparsed_queues (FILE *file
,
469 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
472 cp_unparsed_functions_entry
*uf
;
474 fprintf (file
, "Unparsed functions\n");
475 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
477 fprintf (file
, "#%u:\n", i
);
478 cp_debug_print_unparsed_function (file
, uf
);
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
487 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
489 cp_token
*next_token
, *first_token
, *start_token
;
494 next_token
= parser
->lexer
->next_token
;
495 first_token
= parser
->lexer
->buffer
->address ();
496 start_token
= (next_token
> first_token
+ window_size
/ 2)
497 ? next_token
- window_size
/ 2
499 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
508 cp_debug_parser (FILE *file
, cp_parser
*parser
)
510 const size_t window_size
= 20;
512 expanded_location eloc
;
517 fprintf (file
, "Parser state\n\n");
518 fprintf (file
, "Number of tokens: %u\n",
519 vec_safe_length (parser
->lexer
->buffer
));
520 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
521 cp_debug_print_tree_if_set (file
, "Object scope",
522 parser
->object_scope
);
523 cp_debug_print_tree_if_set (file
, "Qualifying scope",
524 parser
->qualifying_scope
);
525 cp_debug_print_context_stack (file
, parser
->context
);
526 cp_debug_print_flag (file
, "Allow GNU extensions",
527 parser
->allow_gnu_extensions_p
);
528 cp_debug_print_flag (file
, "'>' token is greater-than",
529 parser
->greater_than_is_operator_p
);
530 cp_debug_print_flag (file
, "Default args allowed in current "
531 "parameter list", parser
->default_arg_ok_p
);
532 cp_debug_print_flag (file
, "Parsing integral constant-expression",
533 parser
->integral_constant_expression_p
);
534 cp_debug_print_flag (file
, "Allow non-constant expression in current "
535 "constant-expression",
536 parser
->allow_non_integral_constant_expression_p
);
537 cp_debug_print_flag (file
, "Seen non-constant expression",
538 parser
->non_integral_constant_expression_p
);
539 cp_debug_print_flag (file
, "Local names forbidden in current context",
540 (parser
->local_variables_forbidden_p
541 & LOCAL_VARS_FORBIDDEN
));
542 cp_debug_print_flag (file
, "'this' forbidden in current context",
543 (parser
->local_variables_forbidden_p
545 cp_debug_print_flag (file
, "In unbraced linkage specification",
546 parser
->in_unbraced_linkage_specification_p
);
547 cp_debug_print_flag (file
, "Parsing a declarator",
548 parser
->in_declarator_p
);
549 cp_debug_print_flag (file
, "In template argument list",
550 parser
->in_template_argument_list_p
);
551 cp_debug_print_flag (file
, "Parsing an iteration statement",
552 parser
->in_statement
& IN_ITERATION_STMT
);
553 cp_debug_print_flag (file
, "Parsing a switch statement",
554 parser
->in_statement
& IN_SWITCH_STMT
);
555 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
556 parser
->in_statement
& IN_OMP_BLOCK
);
557 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
558 parser
->in_statement
& IN_OMP_FOR
);
559 cp_debug_print_flag (file
, "Parsing an if statement",
560 parser
->in_statement
& IN_IF_STMT
);
561 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
562 "context", parser
->in_type_id_in_expr_p
);
563 cp_debug_print_flag (file
, "String expressions should be translated "
564 "to execution character set",
565 parser
->translate_strings_p
);
566 cp_debug_print_flag (file
, "Parsing function body outside of a "
567 "local class", parser
->in_function_body
);
568 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
569 parser
->colon_corrects_to_scope_p
);
570 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
571 parser
->colon_doesnt_start_class_def_p
);
572 if (parser
->type_definition_forbidden_message
)
573 fprintf (file
, "Error message for forbidden type definitions: %s\n",
574 parser
->type_definition_forbidden_message
);
575 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
576 fprintf (file
, "Number of class definitions in progress: %u\n",
577 parser
->num_classes_being_defined
);
578 fprintf (file
, "Number of template parameter lists for the current "
579 "declaration: %u\n", parser
->num_template_parameter_lists
);
580 cp_debug_parser_tokens (file
, parser
, window_size
);
581 token
= parser
->lexer
->next_token
;
582 fprintf (file
, "Next token to parse:\n");
583 fprintf (file
, "\tToken: ");
584 cp_lexer_print_token (file
, token
);
585 eloc
= expand_location (token
->location
);
586 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
587 fprintf (file
, "\tLine: %d\n", eloc
.line
);
588 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
592 debug (cp_parser
&ref
)
594 cp_debug_parser (stderr
, &ref
);
598 debug (cp_parser
*ptr
)
603 fprintf (stderr
, "<nil>\n");
606 /* Allocate memory for a new lexer object and return it. */
609 cp_lexer_alloc (void)
613 c_common_no_more_pch ();
615 /* Allocate the memory. */
616 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
618 /* Initially we are not debugging. */
619 lexer
->debugging_p
= false;
621 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
623 /* Create the buffer. */
624 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
630 /* Create a new main C++ lexer, the lexer that gets tokens from the
634 cp_lexer_new_main (void)
639 /* It's possible that parsing the first pragma will load a PCH file,
640 which is a GC collection point. So we have to do that before
641 allocating any memory. */
642 cp_parser_initial_pragma (&token
);
644 lexer
= cp_lexer_alloc ();
646 /* Put the first token in the buffer. */
647 lexer
->buffer
->quick_push (token
);
649 /* Get the remaining tokens from the preprocessor. */
650 while (token
.type
!= CPP_EOF
)
652 cp_lexer_get_preprocessor_token (lexer
, &token
);
653 vec_safe_push (lexer
->buffer
, token
);
656 lexer
->last_token
= lexer
->buffer
->address ()
657 + lexer
->buffer
->length ()
659 lexer
->next_token
= lexer
->buffer
->length ()
660 ? lexer
->buffer
->address ()
663 /* Subsequent preprocessor diagnostics should use compiler
664 diagnostic functions to get the compiler source location. */
667 gcc_assert (!lexer
->next_token
->purged_p
);
671 /* Create a new lexer whose token stream is primed with the tokens in
672 CACHE. When these tokens are exhausted, no new tokens will be read. */
675 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
677 cp_token
*first
= cache
->first
;
678 cp_token
*last
= cache
->last
;
679 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
681 /* We do not own the buffer. */
682 lexer
->buffer
= NULL
;
683 lexer
->next_token
= first
== last
? &eof_token
: first
;
684 lexer
->last_token
= last
;
686 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
688 /* Initially we are not debugging. */
689 lexer
->debugging_p
= false;
691 gcc_assert (!lexer
->next_token
->purged_p
);
695 /* Frees all resources associated with LEXER. */
698 cp_lexer_destroy (cp_lexer
*lexer
)
700 vec_free (lexer
->buffer
);
701 lexer
->saved_tokens
.release ();
705 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
706 be used. The point of this flag is to help the compiler to fold away calls
707 to cp_lexer_debugging_p within this source file at compile time, when the
708 lexer is not being debugged. */
710 #define LEXER_DEBUGGING_ENABLED_P false
712 /* Returns nonzero if debugging information should be output. */
715 cp_lexer_debugging_p (cp_lexer
*lexer
)
717 if (!LEXER_DEBUGGING_ENABLED_P
)
720 return lexer
->debugging_p
;
724 static inline cp_token_position
725 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
727 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
729 return lexer
->next_token
- previous_p
;
732 static inline cp_token
*
733 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
739 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
741 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
744 static inline cp_token_position
745 cp_lexer_previous_token_position (cp_lexer
*lexer
)
747 if (lexer
->next_token
== &eof_token
)
748 return lexer
->last_token
- 1;
750 return cp_lexer_token_position (lexer
, true);
753 static inline cp_token
*
754 cp_lexer_previous_token (cp_lexer
*lexer
)
756 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
758 /* Skip past purged tokens. */
761 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
765 return cp_lexer_token_at (lexer
, tp
);
768 /* nonzero if we are presently saving tokens. */
771 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
773 return lexer
->saved_tokens
.length () != 0;
776 /* Store the next token from the preprocessor in *TOKEN. Return true
777 if we reach EOF. If LEXER is NULL, assume we are handling an
778 initial #pragma pch_preprocess, and thus want the lexer to return
779 processed strings. */
782 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
784 static int is_extern_c
= 0;
786 /* Get a new token from the preprocessor. */
788 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
789 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
790 token
->keyword
= RID_MAX
;
791 token
->purged_p
= false;
792 token
->error_reported
= false;
794 /* On some systems, some header files are surrounded by an
795 implicit extern "C" block. Set a flag in the token if it
796 comes from such a header. */
797 is_extern_c
+= pending_lang_change
;
798 pending_lang_change
= 0;
799 token
->implicit_extern_c
= is_extern_c
> 0;
801 /* Check to see if this token is a keyword. */
802 if (token
->type
== CPP_NAME
)
804 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
806 /* Mark this token as a keyword. */
807 token
->type
= CPP_KEYWORD
;
808 /* Record which keyword. */
809 token
->keyword
= C_RID_CODE (token
->u
.value
);
813 if (warn_cxx11_compat
814 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
815 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
817 /* Warn about the C++0x keyword (but still treat it as
819 warning (OPT_Wc__11_compat
,
820 "identifier %qE is a keyword in C++11",
823 /* Clear out the C_RID_CODE so we don't warn about this
824 particular identifier-turned-keyword again. */
825 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
828 token
->keyword
= RID_MAX
;
831 else if (token
->type
== CPP_AT_NAME
)
833 /* This only happens in Objective-C++; it must be a keyword. */
834 token
->type
= CPP_KEYWORD
;
835 switch (C_RID_CODE (token
->u
.value
))
837 /* Replace 'class' with '@class', 'private' with '@private',
838 etc. This prevents confusion with the C++ keyword
839 'class', and makes the tokens consistent with other
840 Objective-C 'AT' keywords. For example '@class' is
841 reported as RID_AT_CLASS which is consistent with
842 '@synchronized', which is reported as
845 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
846 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
847 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
848 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
849 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
850 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
851 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
852 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
853 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
858 /* Update the globals input_location and the input file stack from TOKEN. */
860 cp_lexer_set_source_position_from_token (cp_token
*token
)
862 if (token
->type
!= CPP_EOF
)
864 input_location
= token
->location
;
868 /* Update the globals input_location and the input file stack from LEXER. */
870 cp_lexer_set_source_position (cp_lexer
*lexer
)
872 cp_token
*token
= cp_lexer_peek_token (lexer
);
873 cp_lexer_set_source_position_from_token (token
);
876 /* Return a pointer to the next token in the token stream, but do not
879 static inline cp_token
*
880 cp_lexer_peek_token (cp_lexer
*lexer
)
882 if (cp_lexer_debugging_p (lexer
))
884 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
885 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
886 putc ('\n', cp_lexer_debug_stream
);
888 return lexer
->next_token
;
891 /* Return true if the next token has the indicated TYPE. */
894 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
896 return cp_lexer_peek_token (lexer
)->type
== type
;
899 /* Return true if the next token does not have the indicated TYPE. */
902 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
904 return !cp_lexer_next_token_is (lexer
, type
);
907 /* Return true if the next token is the indicated KEYWORD. */
910 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
912 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
916 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
918 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
922 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
924 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
927 /* Return true if KEYWORD can start a decl-specifier. */
930 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
934 /* auto specifier: storage-class-specifier in C++,
935 simple-type-specifier in C++0x. */
937 /* Storage classes. */
943 /* Elaborated type specifiers. */
949 /* Simple type specifiers. */
964 /* GNU extensions. */
967 /* C++0x extensions. */
969 case RID_UNDERLYING_TYPE
:
974 if (keyword
>= RID_FIRST_INT_N
975 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
976 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
982 /* Return true if the next token is a keyword for a decl-specifier. */
985 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
989 token
= cp_lexer_peek_token (lexer
);
990 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
993 /* Returns TRUE iff the token T begins a decltype type. */
996 token_is_decltype (cp_token
*t
)
998 return (t
->keyword
== RID_DECLTYPE
999 || t
->type
== CPP_DECLTYPE
);
1002 /* Returns TRUE iff the next token begins a decltype type. */
1005 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1007 cp_token
*t
= cp_lexer_peek_token (lexer
);
1008 return token_is_decltype (t
);
1011 /* Called when processing a token with tree_check_value; perform or defer the
1012 associated checks and return the value. */
1015 saved_checks_value (struct tree_check
*check_value
)
1017 /* Perform any access checks that were deferred. */
1018 vec
<deferred_access_check
, va_gc
> *checks
;
1019 deferred_access_check
*chk
;
1020 checks
= check_value
->checks
;
1024 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1025 perform_or_defer_access_check (chk
->binfo
,
1027 chk
->diag_decl
, tf_warning_or_error
);
1029 /* Return the stored value. */
1030 return check_value
->value
;
1033 /* Return a pointer to the Nth token in the token stream. If N is 1,
1034 then this is precisely equivalent to cp_lexer_peek_token (except
1035 that it is not inline). One would like to disallow that case, but
1036 there is one case (cp_parser_nth_token_starts_template_id) where
1037 the caller passes a variable for N and it might be 1. */
1040 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1044 /* N is 1-based, not zero-based. */
1047 if (cp_lexer_debugging_p (lexer
))
1048 fprintf (cp_lexer_debug_stream
,
1049 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1052 token
= lexer
->next_token
;
1053 gcc_assert (!n
|| token
!= &eof_token
);
1057 if (token
== lexer
->last_token
)
1063 if (!token
->purged_p
)
1067 if (cp_lexer_debugging_p (lexer
))
1069 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1070 putc ('\n', cp_lexer_debug_stream
);
1076 /* Return the next token, and advance the lexer's next_token pointer
1077 to point to the next non-purged token. */
1080 cp_lexer_consume_token (cp_lexer
* lexer
)
1082 cp_token
*token
= lexer
->next_token
;
1084 gcc_assert (token
!= &eof_token
);
1085 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1089 lexer
->next_token
++;
1090 if (lexer
->next_token
== lexer
->last_token
)
1092 lexer
->next_token
= &eof_token
;
1097 while (lexer
->next_token
->purged_p
);
1099 cp_lexer_set_source_position_from_token (token
);
1101 /* Provide debugging output. */
1102 if (cp_lexer_debugging_p (lexer
))
1104 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1105 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1106 putc ('\n', cp_lexer_debug_stream
);
1112 /* Permanently remove the next token from the token stream, and
1113 advance the next_token pointer to refer to the next non-purged
1117 cp_lexer_purge_token (cp_lexer
*lexer
)
1119 cp_token
*tok
= lexer
->next_token
;
1121 gcc_assert (tok
!= &eof_token
);
1122 tok
->purged_p
= true;
1123 tok
->location
= UNKNOWN_LOCATION
;
1124 tok
->u
.value
= NULL_TREE
;
1125 tok
->keyword
= RID_MAX
;
1130 if (tok
== lexer
->last_token
)
1136 while (tok
->purged_p
);
1137 lexer
->next_token
= tok
;
1140 /* Permanently remove all tokens after TOK, up to, but not
1141 including, the token that will be returned next by
1142 cp_lexer_peek_token. */
1145 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1147 cp_token
*peek
= lexer
->next_token
;
1149 if (peek
== &eof_token
)
1150 peek
= lexer
->last_token
;
1152 gcc_assert (tok
< peek
);
1154 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1156 tok
->purged_p
= true;
1157 tok
->location
= UNKNOWN_LOCATION
;
1158 tok
->u
.value
= NULL_TREE
;
1159 tok
->keyword
= RID_MAX
;
1163 /* Begin saving tokens. All tokens consumed after this point will be
1167 cp_lexer_save_tokens (cp_lexer
* lexer
)
1169 /* Provide debugging output. */
1170 if (cp_lexer_debugging_p (lexer
))
1171 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1173 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1176 /* Commit to the portion of the token stream most recently saved. */
1179 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer
))
1183 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1185 lexer
->saved_tokens
.pop ();
1188 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1189 to the token stream. Stop saving tokens. */
1192 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1194 /* Provide debugging output. */
1195 if (cp_lexer_debugging_p (lexer
))
1196 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1198 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1201 /* RAII wrapper around the above functions, with sanity checking. Creating
1202 a variable saves tokens, which are committed when the variable is
1203 destroyed unless they are explicitly rolled back by calling the rollback
1206 struct saved_token_sentinel
1211 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1213 len
= lexer
->saved_tokens
.length ();
1214 cp_lexer_save_tokens (lexer
);
1218 cp_lexer_rollback_tokens (lexer
);
1221 ~saved_token_sentinel()
1224 cp_lexer_commit_tokens (lexer
);
1225 gcc_assert (lexer
->saved_tokens
.length () == len
);
1229 /* Print a representation of the TOKEN on the STREAM. */
1232 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1234 /* We don't use cpp_type2name here because the parser defines
1235 a few tokens of its own. */
1236 static const char *const token_names
[] = {
1237 /* cpplib-defined token types */
1238 #define OP(e, s) #e,
1239 #define TK(e, s) #e,
1243 /* C++ parser token types - see "Manifest constants", above. */
1246 "NESTED_NAME_SPECIFIER",
1249 /* For some tokens, print the associated data. */
1250 switch (token
->type
)
1253 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1254 For example, `struct' is mapped to an INTEGER_CST. */
1255 if (!identifier_p (token
->u
.value
))
1259 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1266 case CPP_UTF8STRING
:
1267 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1271 print_generic_expr (stream
, token
->u
.value
);
1275 /* If we have a name for the token, print it out. Otherwise, we
1276 simply give the numeric code. */
1277 if (token
->type
< ARRAY_SIZE(token_names
))
1278 fputs (token_names
[token
->type
], stream
);
1280 fprintf (stream
, "[%d]", token
->type
);
1286 debug (cp_token
&ref
)
1288 cp_lexer_print_token (stderr
, &ref
);
1289 fprintf (stderr
, "\n");
1293 debug (cp_token
*ptr
)
1298 fprintf (stderr
, "<nil>\n");
1302 /* Start emitting debugging information. */
1305 cp_lexer_start_debugging (cp_lexer
* lexer
)
1307 if (!LEXER_DEBUGGING_ENABLED_P
)
1308 fatal_error (input_location
,
1309 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1311 lexer
->debugging_p
= true;
1312 cp_lexer_debug_stream
= stderr
;
1315 /* Stop emitting debugging information. */
1318 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1320 if (!LEXER_DEBUGGING_ENABLED_P
)
1321 fatal_error (input_location
,
1322 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1324 lexer
->debugging_p
= false;
1325 cp_lexer_debug_stream
= NULL
;
1328 /* Create a new cp_token_cache, representing a range of tokens. */
1330 static cp_token_cache
*
1331 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1333 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1334 cache
->first
= first
;
1339 /* Diagnose if #pragma omp declare simd isn't followed immediately
1340 by function declaration or definition. */
1343 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1345 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1347 error ("%<#pragma omp declare simd%> not immediately followed by "
1348 "function declaration or definition");
1349 parser
->omp_declare_simd
= NULL
;
1353 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1354 and put that into "omp declare simd" attribute. */
1357 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1359 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1361 if (fndecl
== error_mark_node
)
1363 parser
->omp_declare_simd
= NULL
;
1366 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1368 cp_ensure_no_omp_declare_simd (parser
);
1374 /* Diagnose if #pragma acc routine isn't followed immediately by function
1375 declaration or definition. */
1378 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1380 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1382 error_at (parser
->oacc_routine
->loc
,
1383 "%<#pragma acc routine%> not immediately followed by "
1384 "function declaration or definition");
1385 parser
->oacc_routine
= NULL
;
1389 /* Decl-specifiers. */
1391 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1394 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1396 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1401 /* Nothing other than the parser should be creating declarators;
1402 declarators are a semi-syntactic representation of C++ entities.
1403 Other parts of the front end that need to create entities (like
1404 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1406 static cp_declarator
*make_call_declarator
1407 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1408 static cp_declarator
*make_array_declarator
1409 (cp_declarator
*, tree
);
1410 static cp_declarator
*make_pointer_declarator
1411 (cp_cv_quals
, cp_declarator
*, tree
);
1412 static cp_declarator
*make_reference_declarator
1413 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1414 static cp_declarator
*make_ptrmem_declarator
1415 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1417 /* An erroneous declarator. */
1418 static cp_declarator
*cp_error_declarator
;
1420 /* The obstack on which declarators and related data structures are
1422 static struct obstack declarator_obstack
;
1424 /* Alloc BYTES from the declarator memory pool. */
1426 static inline void *
1427 alloc_declarator (size_t bytes
)
1429 return obstack_alloc (&declarator_obstack
, bytes
);
1432 /* Allocate a declarator of the indicated KIND. Clear fields that are
1433 common to all declarators. */
1435 static cp_declarator
*
1436 make_declarator (cp_declarator_kind kind
)
1438 cp_declarator
*declarator
;
1440 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1441 declarator
->kind
= kind
;
1442 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1443 declarator
->attributes
= NULL_TREE
;
1444 declarator
->std_attributes
= NULL_TREE
;
1445 declarator
->declarator
= NULL
;
1446 declarator
->parameter_pack_p
= false;
1447 declarator
->id_loc
= UNKNOWN_LOCATION
;
1452 /* Make a declarator for a generalized identifier. If
1453 QUALIFYING_SCOPE is non-NULL, the identifier is
1454 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1455 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1458 static cp_declarator
*
1459 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1460 special_function_kind sfk
, location_t id_location
)
1462 cp_declarator
*declarator
;
1464 /* It is valid to write:
1466 class C { void f(); };
1470 The standard is not clear about whether `typedef const C D' is
1471 legal; as of 2002-09-15 the committee is considering that
1472 question. EDG 3.0 allows that syntax. Therefore, we do as
1474 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1475 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1477 gcc_assert (identifier_p (unqualified_name
)
1478 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1479 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1481 declarator
= make_declarator (cdk_id
);
1482 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1483 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1484 declarator
->u
.id
.sfk
= sfk
;
1485 declarator
->id_loc
= id_location
;
1490 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1491 of modifiers such as const or volatile to apply to the pointer
1492 type, represented as identifiers. ATTRIBUTES represent the attributes that
1493 appertain to the pointer or reference. */
1496 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1499 cp_declarator
*declarator
;
1501 declarator
= make_declarator (cdk_pointer
);
1502 declarator
->declarator
= target
;
1503 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1504 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1507 declarator
->id_loc
= target
->id_loc
;
1508 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1509 target
->parameter_pack_p
= false;
1512 declarator
->parameter_pack_p
= false;
1514 declarator
->std_attributes
= attributes
;
1519 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1520 represent the attributes that appertain to the pointer or
1524 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1525 bool rvalue_ref
, tree attributes
)
1527 cp_declarator
*declarator
;
1529 declarator
= make_declarator (cdk_reference
);
1530 declarator
->declarator
= target
;
1531 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1532 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1535 declarator
->id_loc
= target
->id_loc
;
1536 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1537 target
->parameter_pack_p
= false;
1540 declarator
->parameter_pack_p
= false;
1542 declarator
->std_attributes
= attributes
;
1547 /* Like make_pointer_declarator -- but for a pointer to a non-static
1548 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1549 appertain to the pointer or reference. */
1552 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1553 cp_declarator
*pointee
,
1556 cp_declarator
*declarator
;
1558 declarator
= make_declarator (cdk_ptrmem
);
1559 declarator
->declarator
= pointee
;
1560 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1561 declarator
->u
.pointer
.class_type
= class_type
;
1565 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1566 pointee
->parameter_pack_p
= false;
1569 declarator
->parameter_pack_p
= false;
1571 declarator
->std_attributes
= attributes
;
1576 /* Make a declarator for the function given by TARGET, with the
1577 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1578 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1579 indicates what exceptions can be thrown. */
1582 make_call_declarator (cp_declarator
*target
,
1584 cp_cv_quals cv_qualifiers
,
1585 cp_virt_specifiers virt_specifiers
,
1586 cp_ref_qualifier ref_qualifier
,
1588 tree exception_specification
,
1589 tree late_return_type
,
1590 tree requires_clause
)
1592 cp_declarator
*declarator
;
1594 declarator
= make_declarator (cdk_function
);
1595 declarator
->declarator
= target
;
1596 declarator
->u
.function
.parameters
= parms
;
1597 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1598 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1599 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1600 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1601 declarator
->u
.function
.exception_specification
= exception_specification
;
1602 declarator
->u
.function
.late_return_type
= late_return_type
;
1603 declarator
->u
.function
.requires_clause
= requires_clause
;
1606 declarator
->id_loc
= target
->id_loc
;
1607 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1608 target
->parameter_pack_p
= false;
1611 declarator
->parameter_pack_p
= false;
1616 /* Make a declarator for an array of BOUNDS elements, each of which is
1617 defined by ELEMENT. */
1620 make_array_declarator (cp_declarator
*element
, tree bounds
)
1622 cp_declarator
*declarator
;
1624 declarator
= make_declarator (cdk_array
);
1625 declarator
->declarator
= element
;
1626 declarator
->u
.array
.bounds
= bounds
;
1629 declarator
->id_loc
= element
->id_loc
;
1630 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1631 element
->parameter_pack_p
= false;
1634 declarator
->parameter_pack_p
= false;
1639 /* Determine whether the declarator we've seen so far can be a
1640 parameter pack, when followed by an ellipsis. */
1642 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1644 if (declarator
&& declarator
->parameter_pack_p
)
1645 /* We already saw an ellipsis. */
1648 /* Search for a declarator name, or any other declarator that goes
1649 after the point where the ellipsis could appear in a parameter
1650 pack. If we find any of these, then this declarator cannot be
1651 made into a parameter pack. */
1653 while (declarator
&& !found
)
1655 switch ((int)declarator
->kind
)
1667 declarator
= declarator
->declarator
;
1675 cp_parameter_declarator
*no_parameters
;
1677 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1678 DECLARATOR and DEFAULT_ARGUMENT. */
1680 cp_parameter_declarator
*
1681 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1682 cp_declarator
*declarator
,
1683 tree default_argument
,
1685 bool template_parameter_pack_p
= false)
1687 cp_parameter_declarator
*parameter
;
1689 parameter
= ((cp_parameter_declarator
*)
1690 alloc_declarator (sizeof (cp_parameter_declarator
)));
1691 parameter
->next
= NULL
;
1692 if (decl_specifiers
)
1693 parameter
->decl_specifiers
= *decl_specifiers
;
1695 clear_decl_specs (¶meter
->decl_specifiers
);
1696 parameter
->declarator
= declarator
;
1697 parameter
->default_argument
= default_argument
;
1698 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1699 parameter
->loc
= loc
;
1704 /* Returns true iff DECLARATOR is a declaration for a function. */
1707 function_declarator_p (const cp_declarator
*declarator
)
1711 if (declarator
->kind
== cdk_function
1712 && declarator
->declarator
->kind
== cdk_id
)
1714 if (declarator
->kind
== cdk_id
1715 || declarator
->kind
== cdk_decomp
1716 || declarator
->kind
== cdk_error
)
1718 declarator
= declarator
->declarator
;
1728 A cp_parser parses the token stream as specified by the C++
1729 grammar. Its job is purely parsing, not semantic analysis. For
1730 example, the parser breaks the token stream into declarators,
1731 expressions, statements, and other similar syntactic constructs.
1732 It does not check that the types of the expressions on either side
1733 of an assignment-statement are compatible, or that a function is
1734 not declared with a parameter of type `void'.
1736 The parser invokes routines elsewhere in the compiler to perform
1737 semantic analysis and to build up the abstract syntax tree for the
1740 The parser (and the template instantiation code, which is, in a
1741 way, a close relative of parsing) are the only parts of the
1742 compiler that should be calling push_scope and pop_scope, or
1743 related functions. The parser (and template instantiation code)
1744 keeps track of what scope is presently active; everything else
1745 should simply honor that. (The code that generates static
1746 initializers may also need to set the scope, in order to check
1747 access control correctly when emitting the initializers.)
1752 The parser is of the standard recursive-descent variety. Upcoming
1753 tokens in the token stream are examined in order to determine which
1754 production to use when parsing a non-terminal. Some C++ constructs
1755 require arbitrary look ahead to disambiguate. For example, it is
1756 impossible, in the general case, to tell whether a statement is an
1757 expression or declaration without scanning the entire statement.
1758 Therefore, the parser is capable of "parsing tentatively." When the
1759 parser is not sure what construct comes next, it enters this mode.
1760 Then, while we attempt to parse the construct, the parser queues up
1761 error messages, rather than issuing them immediately, and saves the
1762 tokens it consumes. If the construct is parsed successfully, the
1763 parser "commits", i.e., it issues any queued error messages and
1764 the tokens that were being preserved are permanently discarded.
1765 If, however, the construct is not parsed successfully, the parser
1766 rolls back its state completely so that it can resume parsing using
1767 a different alternative.
1772 The performance of the parser could probably be improved substantially.
1773 We could often eliminate the need to parse tentatively by looking ahead
1774 a little bit. In some places, this approach might not entirely eliminate
1775 the need to parse tentatively, but it might still speed up the average
1778 /* Flags that are passed to some parsing functions. These values can
1779 be bitwise-ored together. */
1784 CP_PARSER_FLAGS_NONE
= 0x0,
1785 /* The construct is optional. If it is not present, then no error
1786 should be issued. */
1787 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1788 /* When parsing a type-specifier, treat user-defined type-names
1789 as non-type identifiers. */
1790 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1791 /* When parsing a type-specifier, do not try to parse a class-specifier
1792 or enum-specifier. */
1793 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1794 /* When parsing a decl-specifier-seq, only allow type-specifier or
1796 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1797 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1798 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10,
1799 /* When parsing a decl-specifier-seq, allow missing typename. */
1800 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
= 0x20
1803 /* This type is used for parameters and variables which hold
1804 combinations of the above flags. */
1805 typedef int cp_parser_flags
;
1807 /* The different kinds of declarators we want to parse. */
1809 enum cp_parser_declarator_kind
1811 /* We want an abstract declarator. */
1812 CP_PARSER_DECLARATOR_ABSTRACT
,
1813 /* We want a named declarator. */
1814 CP_PARSER_DECLARATOR_NAMED
,
1815 /* We don't mind, but the name must be an unqualified-id. */
1816 CP_PARSER_DECLARATOR_EITHER
1819 /* The precedence values used to parse binary expressions. The minimum value
1820 of PREC must be 1, because zero is reserved to quickly discriminate
1821 binary operators from other tokens. */
1826 PREC_LOGICAL_OR_EXPRESSION
,
1827 PREC_LOGICAL_AND_EXPRESSION
,
1828 PREC_INCLUSIVE_OR_EXPRESSION
,
1829 PREC_EXCLUSIVE_OR_EXPRESSION
,
1830 PREC_AND_EXPRESSION
,
1831 PREC_EQUALITY_EXPRESSION
,
1832 PREC_RELATIONAL_EXPRESSION
,
1833 PREC_SHIFT_EXPRESSION
,
1834 PREC_ADDITIVE_EXPRESSION
,
1835 PREC_MULTIPLICATIVE_EXPRESSION
,
1837 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1840 /* A mapping from a token type to a corresponding tree node type, with a
1841 precedence value. */
1843 struct cp_parser_binary_operations_map_node
1845 /* The token type. */
1846 enum cpp_ttype token_type
;
1847 /* The corresponding tree code. */
1848 enum tree_code tree_type
;
1849 /* The precedence of this operator. */
1850 enum cp_parser_prec prec
;
1853 struct cp_parser_expression_stack_entry
1855 /* Left hand side of the binary operation we are currently
1858 /* Original tree code for left hand side, if it was a binary
1859 expression itself (used for -Wparentheses). */
1860 enum tree_code lhs_type
;
1861 /* Tree code for the binary operation we are parsing. */
1862 enum tree_code tree_type
;
1863 /* Precedence of the binary operation we are parsing. */
1864 enum cp_parser_prec prec
;
1865 /* Location of the binary operation we are parsing. */
1869 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1870 entries because precedence levels on the stack are monotonically
1872 typedef struct cp_parser_expression_stack_entry
1873 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1877 /* Constructors and destructors. */
1879 static cp_parser_context
*cp_parser_context_new
1880 (cp_parser_context
*);
1882 /* Class variables. */
1884 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1886 /* The operator-precedence table used by cp_parser_binary_expression.
1887 Transformed into an associative array (binops_by_token) by
1890 static const cp_parser_binary_operations_map_node binops
[] = {
1891 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1892 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1894 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1895 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1896 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1898 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1899 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1901 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1902 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1904 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1905 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1906 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1907 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1909 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1910 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1912 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1914 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1916 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1918 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1920 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1923 /* The same as binops, but initialized by cp_parser_new so that
1924 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1926 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1928 /* Constructors and destructors. */
1930 /* Construct a new context. The context below this one on the stack
1931 is given by NEXT. */
1933 static cp_parser_context
*
1934 cp_parser_context_new (cp_parser_context
* next
)
1936 cp_parser_context
*context
;
1938 /* Allocate the storage. */
1939 if (cp_parser_context_free_list
!= NULL
)
1941 /* Pull the first entry from the free list. */
1942 context
= cp_parser_context_free_list
;
1943 cp_parser_context_free_list
= context
->next
;
1944 memset (context
, 0, sizeof (*context
));
1947 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1949 /* No errors have occurred yet in this context. */
1950 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1951 /* If this is not the bottommost context, copy information that we
1952 need from the previous context. */
1955 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1956 expression, then we are parsing one in this context, too. */
1957 context
->object_type
= next
->object_type
;
1958 /* Thread the stack. */
1959 context
->next
= next
;
1965 /* Managing the unparsed function queues. */
1967 #define unparsed_funs_with_default_args \
1968 parser->unparsed_queues->last ().funs_with_default_args
1969 #define unparsed_funs_with_definitions \
1970 parser->unparsed_queues->last ().funs_with_definitions
1971 #define unparsed_nsdmis \
1972 parser->unparsed_queues->last ().nsdmis
1973 #define unparsed_classes \
1974 parser->unparsed_queues->last ().classes
1977 push_unparsed_function_queues (cp_parser
*parser
)
1979 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1980 vec_safe_push (parser
->unparsed_queues
, e
);
1984 pop_unparsed_function_queues (cp_parser
*parser
)
1986 release_tree_vector (unparsed_funs_with_definitions
);
1987 parser
->unparsed_queues
->pop ();
1992 /* Constructors and destructors. */
1994 static cp_parser
*cp_parser_new
1997 /* Routines to parse various constructs.
1999 Those that return `tree' will return the error_mark_node (rather
2000 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2001 Sometimes, they will return an ordinary node if error-recovery was
2002 attempted, even though a parse error occurred. So, to check
2003 whether or not a parse error occurred, you should always use
2004 cp_parser_error_occurred. If the construct is optional (indicated
2005 either by an `_opt' in the name of the function that does the
2006 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2007 the construct is not present. */
2009 /* Lexical conventions [gram.lex] */
2011 static cp_expr cp_parser_identifier
2013 static cp_expr cp_parser_string_literal
2014 (cp_parser
*, bool, bool, bool);
2015 static cp_expr cp_parser_userdef_char_literal
2017 static tree cp_parser_userdef_string_literal
2019 static cp_expr cp_parser_userdef_numeric_literal
2022 /* Basic concepts [gram.basic] */
2024 static void cp_parser_translation_unit (cp_parser
*);
2026 /* Expressions [gram.expr] */
2028 static cp_expr cp_parser_primary_expression
2029 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2030 static cp_expr cp_parser_id_expression
2031 (cp_parser
*, bool, bool, bool *, bool, bool);
2032 static cp_expr cp_parser_unqualified_id
2033 (cp_parser
*, bool, bool, bool, bool);
2034 static tree cp_parser_nested_name_specifier_opt
2035 (cp_parser
*, bool, bool, bool, bool, bool = false);
2036 static tree cp_parser_nested_name_specifier
2037 (cp_parser
*, bool, bool, bool, bool);
2038 static tree cp_parser_qualifying_entity
2039 (cp_parser
*, bool, bool, bool, bool, bool);
2040 static cp_expr cp_parser_postfix_expression
2041 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2042 static tree cp_parser_postfix_open_square_expression
2043 (cp_parser
*, tree
, bool, bool);
2044 static tree cp_parser_postfix_dot_deref_expression
2045 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2046 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2047 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2049 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2050 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2051 static void cp_parser_pseudo_destructor_name
2052 (cp_parser
*, tree
, tree
*, tree
*);
2053 static cp_expr cp_parser_unary_expression
2054 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2055 static enum tree_code cp_parser_unary_operator
2057 static tree cp_parser_has_attribute_expression
2059 static tree cp_parser_new_expression
2061 static vec
<tree
, va_gc
> *cp_parser_new_placement
2063 static tree cp_parser_new_type_id
2064 (cp_parser
*, tree
*);
2065 static cp_declarator
*cp_parser_new_declarator_opt
2067 static cp_declarator
*cp_parser_direct_new_declarator
2069 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2071 static tree cp_parser_delete_expression
2073 static cp_expr cp_parser_cast_expression
2074 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2075 static cp_expr cp_parser_binary_expression
2076 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2077 static tree cp_parser_question_colon_clause
2078 (cp_parser
*, cp_expr
);
2079 static cp_expr cp_parser_assignment_expression
2080 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2081 static enum tree_code cp_parser_assignment_operator_opt
2083 static cp_expr cp_parser_expression
2084 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2085 static cp_expr cp_parser_constant_expression
2086 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2087 static cp_expr cp_parser_builtin_offsetof
2089 static cp_expr cp_parser_lambda_expression
2091 static void cp_parser_lambda_introducer
2092 (cp_parser
*, tree
);
2093 static bool cp_parser_lambda_declarator_opt
2094 (cp_parser
*, tree
);
2095 static void cp_parser_lambda_body
2096 (cp_parser
*, tree
);
2098 /* Statements [gram.stmt.stmt] */
2100 static void cp_parser_statement
2101 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2102 static void cp_parser_label_for_labeled_statement
2103 (cp_parser
*, tree
);
2104 static tree cp_parser_expression_statement
2105 (cp_parser
*, tree
);
2106 static tree cp_parser_compound_statement
2107 (cp_parser
*, tree
, int, bool);
2108 static void cp_parser_statement_seq_opt
2109 (cp_parser
*, tree
);
2110 static tree cp_parser_selection_statement
2111 (cp_parser
*, bool *, vec
<tree
> *);
2112 static tree cp_parser_condition
2114 static tree cp_parser_iteration_statement
2115 (cp_parser
*, bool *, bool, unsigned short);
2116 static bool cp_parser_init_statement
2117 (cp_parser
*, tree
*decl
);
2118 static tree cp_parser_for
2119 (cp_parser
*, bool, unsigned short);
2120 static tree cp_parser_c_for
2121 (cp_parser
*, tree
, tree
, bool, unsigned short);
2122 static tree cp_parser_range_for
2123 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2124 static void do_range_for_auto_deduction
2126 static tree cp_parser_perform_range_for_lookup
2127 (tree
, tree
*, tree
*);
2128 static tree cp_parser_range_for_member_function
2130 static tree cp_parser_jump_statement
2132 static void cp_parser_declaration_statement
2135 static tree cp_parser_implicitly_scoped_statement
2136 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2137 static void cp_parser_already_scoped_statement
2138 (cp_parser
*, bool *, const token_indent_info
&);
2140 /* Declarations [gram.dcl.dcl] */
2142 static void cp_parser_declaration_seq_opt
2144 static void cp_parser_declaration
2146 static void cp_parser_toplevel_declaration
2148 static void cp_parser_block_declaration
2149 (cp_parser
*, bool);
2150 static void cp_parser_simple_declaration
2151 (cp_parser
*, bool, tree
*);
2152 static void cp_parser_decl_specifier_seq
2153 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2154 static tree cp_parser_storage_class_specifier_opt
2156 static tree cp_parser_function_specifier_opt
2157 (cp_parser
*, cp_decl_specifier_seq
*);
2158 static tree cp_parser_type_specifier
2159 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2161 static tree cp_parser_simple_type_specifier
2162 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2163 static tree cp_parser_type_name
2164 (cp_parser
*, bool);
2165 static tree cp_parser_nonclass_name
2166 (cp_parser
* parser
);
2167 static tree cp_parser_elaborated_type_specifier
2168 (cp_parser
*, bool, bool);
2169 static tree cp_parser_enum_specifier
2171 static void cp_parser_enumerator_list
2172 (cp_parser
*, tree
);
2173 static void cp_parser_enumerator_definition
2174 (cp_parser
*, tree
);
2175 static tree cp_parser_namespace_name
2177 static void cp_parser_namespace_definition
2179 static void cp_parser_namespace_body
2181 static tree cp_parser_qualified_namespace_specifier
2183 static void cp_parser_namespace_alias_definition
2185 static bool cp_parser_using_declaration
2186 (cp_parser
*, bool);
2187 static void cp_parser_using_directive
2189 static tree cp_parser_alias_declaration
2191 static void cp_parser_asm_definition
2193 static void cp_parser_linkage_specification
2195 static void cp_parser_static_assert
2196 (cp_parser
*, bool);
2197 static tree cp_parser_decltype
2199 static tree cp_parser_decomposition_declaration
2200 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2202 /* Declarators [gram.dcl.decl] */
2204 static tree cp_parser_init_declarator
2205 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*,
2206 vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*,
2207 location_t
*, tree
*);
2208 static cp_declarator
*cp_parser_declarator
2209 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool *,
2211 static cp_declarator
*cp_parser_direct_declarator
2212 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool, bool,
2214 static enum tree_code cp_parser_ptr_operator
2215 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2216 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2218 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2220 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2222 static tree cp_parser_tx_qualifier_opt
2224 static tree cp_parser_late_return_type_opt
2225 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2226 static tree cp_parser_declarator_id
2227 (cp_parser
*, bool);
2228 static tree cp_parser_type_id
2229 (cp_parser
*, cp_parser_flags
= CP_PARSER_FLAGS_NONE
, location_t
* = NULL
);
2230 static tree cp_parser_template_type_arg
2232 static tree
cp_parser_trailing_type_id (cp_parser
*);
2233 static tree cp_parser_type_id_1
2234 (cp_parser
*, cp_parser_flags
, bool, bool, location_t
*);
2235 static void cp_parser_type_specifier_seq
2236 (cp_parser
*, cp_parser_flags
, bool, bool, cp_decl_specifier_seq
*);
2237 static tree cp_parser_parameter_declaration_clause
2238 (cp_parser
*, cp_parser_flags
);
2239 static tree cp_parser_parameter_declaration_list
2240 (cp_parser
*, cp_parser_flags
);
2241 static cp_parameter_declarator
*cp_parser_parameter_declaration
2242 (cp_parser
*, cp_parser_flags
, bool, bool *);
2243 static tree cp_parser_default_argument
2244 (cp_parser
*, bool);
2245 static void cp_parser_function_body
2246 (cp_parser
*, bool);
2247 static tree cp_parser_initializer
2248 (cp_parser
*, bool *, bool *, bool = false);
2249 static cp_expr cp_parser_initializer_clause
2250 (cp_parser
*, bool *);
2251 static cp_expr cp_parser_braced_list
2252 (cp_parser
*, bool*);
2253 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2254 (cp_parser
*, bool *);
2256 static void cp_parser_ctor_initializer_opt_and_function_body
2257 (cp_parser
*, bool);
2259 static tree cp_parser_late_parsing_omp_declare_simd
2260 (cp_parser
*, tree
);
2262 static tree cp_parser_late_parsing_oacc_routine
2263 (cp_parser
*, tree
);
2265 static tree synthesize_implicit_template_parm
2266 (cp_parser
*, tree
);
2267 static tree finish_fully_implicit_template
2268 (cp_parser
*, tree
);
2269 static void abort_fully_implicit_template
2272 /* Classes [gram.class] */
2274 static tree cp_parser_class_name
2275 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2276 static tree cp_parser_class_specifier
2278 static tree cp_parser_class_head
2279 (cp_parser
*, bool *);
2280 static enum tag_types cp_parser_class_key
2282 static void cp_parser_type_parameter_key
2283 (cp_parser
* parser
);
2284 static void cp_parser_member_specification_opt
2286 static void cp_parser_member_declaration
2288 static tree cp_parser_pure_specifier
2290 static tree cp_parser_constant_initializer
2293 /* Derived classes [gram.class.derived] */
2295 static tree cp_parser_base_clause
2297 static tree cp_parser_base_specifier
2300 /* Special member functions [gram.special] */
2302 static tree cp_parser_conversion_function_id
2304 static tree cp_parser_conversion_type_id
2306 static cp_declarator
*cp_parser_conversion_declarator_opt
2308 static void cp_parser_ctor_initializer_opt
2310 static void cp_parser_mem_initializer_list
2312 static tree cp_parser_mem_initializer
2314 static tree cp_parser_mem_initializer_id
2317 /* Overloading [gram.over] */
2319 static cp_expr cp_parser_operator_function_id
2321 static cp_expr cp_parser_operator
2322 (cp_parser
*, location_t
);
2324 /* Templates [gram.temp] */
2326 static void cp_parser_template_declaration
2327 (cp_parser
*, bool);
2328 static tree cp_parser_template_parameter_list
2330 static tree cp_parser_template_parameter
2331 (cp_parser
*, bool *, bool *);
2332 static tree cp_parser_type_parameter
2333 (cp_parser
*, bool *);
2334 static tree cp_parser_template_id
2335 (cp_parser
*, bool, bool, enum tag_types
, bool);
2336 static tree cp_parser_template_name
2337 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2338 static tree cp_parser_template_argument_list
2340 static tree cp_parser_template_argument
2342 static void cp_parser_explicit_instantiation
2344 static void cp_parser_explicit_specialization
2347 /* Exception handling [gram.exception] */
2349 static tree cp_parser_try_block
2351 static void cp_parser_function_try_block
2353 static void cp_parser_handler_seq
2355 static void cp_parser_handler
2357 static tree cp_parser_exception_declaration
2359 static tree cp_parser_throw_expression
2361 static tree cp_parser_exception_specification_opt
2363 static tree cp_parser_type_id_list
2366 /* GNU Extensions */
2368 static tree cp_parser_asm_specification_opt
2370 static tree cp_parser_asm_operand_list
2372 static tree cp_parser_asm_clobber_list
2374 static tree cp_parser_asm_label_list
2376 static bool cp_next_tokens_can_be_attribute_p
2378 static bool cp_next_tokens_can_be_gnu_attribute_p
2380 static bool cp_next_tokens_can_be_std_attribute_p
2382 static bool cp_nth_tokens_can_be_std_attribute_p
2383 (cp_parser
*, size_t);
2384 static bool cp_nth_tokens_can_be_gnu_attribute_p
2385 (cp_parser
*, size_t);
2386 static bool cp_nth_tokens_can_be_attribute_p
2387 (cp_parser
*, size_t);
2388 static tree cp_parser_attributes_opt
2390 static tree cp_parser_gnu_attributes_opt
2392 static tree cp_parser_gnu_attribute_list
2393 (cp_parser
*, bool = false);
2394 static tree cp_parser_std_attribute
2395 (cp_parser
*, tree
);
2396 static tree cp_parser_std_attribute_spec
2398 static tree cp_parser_std_attribute_spec_seq
2400 static size_t cp_parser_skip_attributes_opt
2401 (cp_parser
*, size_t);
2402 static bool cp_parser_extension_opt
2403 (cp_parser
*, int *);
2404 static void cp_parser_label_declaration
2407 /* Concept Extensions */
2409 static tree cp_parser_requires_clause
2411 static tree cp_parser_requires_clause_opt
2413 static tree cp_parser_requires_expression
2415 static tree cp_parser_requirement_parameter_list
2417 static tree cp_parser_requirement_body
2419 static tree cp_parser_requirement_list
2421 static tree cp_parser_requirement
2423 static tree cp_parser_simple_requirement
2425 static tree cp_parser_compound_requirement
2427 static tree cp_parser_type_requirement
2429 static tree cp_parser_nested_requirement
2432 /* Transactional Memory Extensions */
2434 static tree cp_parser_transaction
2435 (cp_parser
*, cp_token
*);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser
*, enum rid
);
2438 static void cp_parser_function_transaction
2439 (cp_parser
*, enum rid
);
2440 static tree cp_parser_transaction_cancel
2443 enum pragma_context
{
2450 static bool cp_parser_pragma
2451 (cp_parser
*, enum pragma_context
, bool *);
2453 /* Objective-C++ Productions */
2455 static tree cp_parser_objc_message_receiver
2457 static tree cp_parser_objc_message_args
2459 static tree cp_parser_objc_message_expression
2461 static cp_expr cp_parser_objc_encode_expression
2463 static tree cp_parser_objc_defs_expression
2465 static tree cp_parser_objc_protocol_expression
2467 static tree cp_parser_objc_selector_expression
2469 static cp_expr cp_parser_objc_expression
2471 static bool cp_parser_objc_selector_p
2473 static tree cp_parser_objc_selector
2475 static tree cp_parser_objc_protocol_refs_opt
2477 static void cp_parser_objc_declaration
2478 (cp_parser
*, tree
);
2479 static tree cp_parser_objc_statement
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser
*, tree
*);
2483 static void cp_parser_objc_at_property_declaration
2485 static void cp_parser_objc_at_synthesize_declaration
2487 static void cp_parser_objc_at_dynamic_declaration
2489 static tree cp_parser_objc_struct_declaration
2492 /* Utility Routines */
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser
*, tree
, location_t
);
2498 static tree cp_parser_maybe_treat_template_as_class
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser
*, cp_declarator
*, location_t
);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2504 static cp_expr cp_parser_simple_cast_expression
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser
*, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser
*, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser
*, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser
*, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec
<deferred_access_check
, va_gc
> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser
*, tree
);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2524 static tree cp_parser_save_nsdmi
2526 static tree cp_parser_enclosed_template_argument_list
2528 static void cp_parser_save_default_args
2529 (cp_parser
*, tree
);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser
*, tree
);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser
*, tree
, tree
, tree
);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser
*, tree
);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser
*, tree
);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser
*, enum rid
);
2540 static cp_expr cp_parser_trait_expr
2541 (cp_parser
*, enum rid
);
2542 static bool cp_parser_declares_only_class_p
2544 static void cp_parser_set_storage_class
2545 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq
*decl_specs
,
2550 cp_decl_spec ds
, cp_token
*);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq
*);
2553 static void cp_parser_required_error
2554 (cp_parser
*, required_token
, bool, location_t
);
2555 static cp_token
*cp_parser_require
2556 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2557 static cp_token
*cp_parser_require_keyword
2558 (cp_parser
*, enum rid
, required_token
);
2559 static bool cp_parser_token_starts_function_definition_p
2561 static bool cp_parser_next_token_starts_class_definition_p
2563 static bool cp_parser_next_token_ends_template_argument_p
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser
*, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2571 static void cp_parser_check_class_key
2572 (enum tag_types
, tree type
);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type
, location_t location
);
2575 static bool cp_parser_optional_template_keyword
2577 static void cp_parser_pre_parsed_nested_name_specifier
2579 static bool cp_parser_cache_group
2580 (cp_parser
*, enum cpp_ttype
, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser
*parser
, bool nsdmi
);
2583 static void cp_parser_parse_tentatively
2585 static void cp_parser_commit_to_tentative_parse
2587 static void cp_parser_commit_to_topmost_tentative_parse
2589 static void cp_parser_abort_tentative_parse
2591 static bool cp_parser_parse_definitely
2593 static inline bool cp_parser_parsing_tentatively
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2597 static void cp_parser_error
2598 (cp_parser
*, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2601 static bool cp_parser_simulate_error
2603 static bool cp_parser_check_type_definition
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator
*, tree
, location_t type_location
);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser
*, non_integral_constant
);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser
*, tree
, location_t
);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser
*, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2621 static void cp_parser_skip_to_end_of_block_or_statement
2623 static bool cp_parser_skip_to_closing_brace
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser
*, cp_token
*);
2629 static bool cp_parser_error_occurred
2631 static bool cp_parser_allow_gnu_extensions_p
2633 static bool cp_parser_is_pure_string_literal
2635 static bool cp_parser_is_string_literal
2637 static bool cp_parser_is_keyword
2638 (cp_token
*, enum rid
);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser
*, tree
, location_t location
);
2641 static cp_declarator
* cp_parser_make_indirect_declarator
2642 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2643 static bool cp_parser_compound_literal_p
2645 static bool cp_parser_array_designator_p
2647 static bool cp_parser_init_statement_p
2649 static bool cp_parser_skip_to_closing_square_bracket
2652 /* Concept-related syntactic transformations */
2654 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2655 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2663 ++cp_unevaluated_operand
;
2664 ++c_inhibit_evaluation_warnings
;
2667 cp_unevaluated::~cp_unevaluated ()
2669 --c_inhibit_evaluation_warnings
;
2670 --cp_unevaluated_operand
;
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2676 /* Returns nonzero if we are parsing tentatively. */
2679 cp_parser_parsing_tentatively (cp_parser
* parser
)
2681 return parser
->context
->next
!= NULL
;
2684 /* Returns nonzero if TOKEN is a string literal. */
2687 cp_parser_is_pure_string_literal (cp_token
* token
)
2689 return (token
->type
== CPP_STRING
||
2690 token
->type
== CPP_STRING16
||
2691 token
->type
== CPP_STRING32
||
2692 token
->type
== CPP_WSTRING
||
2693 token
->type
== CPP_UTF8STRING
);
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2700 cp_parser_is_string_literal (cp_token
* token
)
2702 return (cp_parser_is_pure_string_literal (token
) ||
2703 token
->type
== CPP_STRING_USERDEF
||
2704 token
->type
== CPP_STRING16_USERDEF
||
2705 token
->type
== CPP_STRING32_USERDEF
||
2706 token
->type
== CPP_WSTRING_USERDEF
||
2707 token
->type
== CPP_UTF8STRING_USERDEF
);
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2713 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2715 return token
->keyword
== keyword
;
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token
*token
)
2724 if (token
->type
!= CPP_PRAGMA
)
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2742 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2743 location_t
*out_loc
)
2745 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2746 if (token2
->type
!= tok1_kind
)
2748 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2749 if (token3
->type
!= tok1_kind
)
2751 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2752 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2755 /* It must be at the start of the line. */
2756 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2757 if (LOCATION_COLUMN (start_loc
) != 1)
2760 /* We have a conflict marker. Construct a location of the form:
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc
= get_finish (token4
->location
);
2765 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2770 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2774 get_matching_symbol (required_token token_desc
)
2781 case RT_CLOSE_BRACE
:
2783 case RT_CLOSE_PAREN
:
2788 /* Attempt to convert TOKEN_DESC from a required_token to an
2789 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2791 static enum cpp_ttype
2792 get_required_cpp_ttype (required_token token_desc
)
2797 return CPP_SEMICOLON
;
2799 return CPP_OPEN_PAREN
;
2800 case RT_CLOSE_BRACE
:
2801 return CPP_CLOSE_BRACE
;
2803 return CPP_OPEN_BRACE
;
2804 case RT_CLOSE_SQUARE
:
2805 return CPP_CLOSE_SQUARE
;
2806 case RT_OPEN_SQUARE
:
2807 return CPP_OPEN_SQUARE
;
2812 case RT_CLOSE_PAREN
:
2813 return CPP_CLOSE_PAREN
;
2816 /* Use CPP_EOF as a "no completions possible" code. */
2822 /* Subroutine of cp_parser_error and cp_parser_required_error.
2824 Issue a diagnostic of the form
2825 FILE:LINE: MESSAGE before TOKEN
2826 where TOKEN is the next token in the input stream. MESSAGE
2827 (specified by the caller) is usually of the form "expected
2830 This bypasses the check for tentative passing, and potentially
2831 adds material needed by cp_parser_required_error.
2833 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2834 suggesting insertion of the missing token.
2836 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2837 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2841 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2842 required_token missing_token_desc
,
2843 location_t matching_location
)
2845 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2846 /* This diagnostic makes more sense if it is tagged to the line
2847 of the token we just peeked at. */
2848 cp_lexer_set_source_position_from_token (token
);
2850 if (token
->type
== CPP_PRAGMA
)
2852 error_at (token
->location
,
2853 "%<#pragma%> is not allowed here");
2854 cp_parser_skip_to_pragma_eol (parser
, token
);
2858 /* If this is actually a conflict marker, report it as such. */
2859 if (token
->type
== CPP_LSHIFT
2860 || token
->type
== CPP_RSHIFT
2861 || token
->type
== CPP_EQ_EQ
)
2864 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2866 error_at (loc
, "version control conflict marker in file");
2867 expanded_location token_exploc
= expand_location (token
->location
);
2868 /* Consume tokens until the end of the source line. */
2871 cp_lexer_consume_token (parser
->lexer
);
2872 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2875 expanded_location next_exploc
= expand_location (next
->location
);
2876 if (next_exploc
.file
!= token_exploc
.file
)
2878 if (next_exploc
.line
!= token_exploc
.line
)
2885 gcc_rich_location
richloc (input_location
);
2887 bool added_matching_location
= false;
2889 if (missing_token_desc
!= RT_NONE
)
2891 /* Potentially supply a fix-it hint, suggesting to add the
2892 missing token immediately after the *previous* token.
2893 This may move the primary location within richloc. */
2894 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2895 location_t prev_token_loc
2896 = cp_lexer_previous_token (parser
->lexer
)->location
;
2897 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2899 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2900 Attempt to consolidate diagnostics by printing it as a
2901 secondary range within the main diagnostic. */
2902 if (matching_location
!= UNKNOWN_LOCATION
)
2903 added_matching_location
2904 = richloc
.add_location_if_nearby (matching_location
);
2907 /* Actually emit the error. */
2908 c_parse_error (gmsgid
,
2909 /* Because c_parser_error does not understand
2910 CPP_KEYWORD, keywords are treated like
2912 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2913 token
->u
.value
, token
->flags
, &richloc
);
2915 if (missing_token_desc
!= RT_NONE
)
2917 /* If we weren't able to consolidate matching_location, then
2918 print it as a secondary diagnostic. */
2919 if (matching_location
!= UNKNOWN_LOCATION
2920 && !added_matching_location
)
2921 inform (matching_location
, "to match this %qs",
2922 get_matching_symbol (missing_token_desc
));
2926 /* If not parsing tentatively, issue a diagnostic of the form
2927 FILE:LINE: MESSAGE before TOKEN
2928 where TOKEN is the next token in the input stream. MESSAGE
2929 (specified by the caller) is usually of the form "expected
2933 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2935 if (!cp_parser_simulate_error (parser
))
2936 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2939 /* Issue an error about name-lookup failing. NAME is the
2940 IDENTIFIER_NODE DECL is the result of
2941 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2942 the thing that we hoped to find. */
2945 cp_parser_name_lookup_error (cp_parser
* parser
,
2948 name_lookup_error desired
,
2949 location_t location
)
2951 /* If name lookup completely failed, tell the user that NAME was not
2953 if (decl
== error_mark_node
)
2955 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2956 error_at (location
, "%<%E::%E%> has not been declared",
2957 parser
->scope
, name
);
2958 else if (parser
->scope
== global_namespace
)
2959 error_at (location
, "%<::%E%> has not been declared", name
);
2960 else if (parser
->object_scope
2961 && !CLASS_TYPE_P (parser
->object_scope
))
2962 error_at (location
, "request for member %qE in non-class type %qT",
2963 name
, parser
->object_scope
);
2964 else if (parser
->object_scope
)
2965 error_at (location
, "%<%T::%E%> has not been declared",
2966 parser
->object_scope
, name
);
2968 error_at (location
, "%qE has not been declared", name
);
2970 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2975 error_at (location
, "%<%E::%E%> is not a type",
2976 parser
->scope
, name
);
2979 error_at (location
, "%<%E::%E%> is not a class or namespace",
2980 parser
->scope
, name
);
2984 "%<%E::%E%> is not a class, namespace, or enumeration",
2985 parser
->scope
, name
);
2992 else if (parser
->scope
== global_namespace
)
2997 error_at (location
, "%<::%E%> is not a type", name
);
3000 error_at (location
, "%<::%E%> is not a class or namespace", name
);
3004 "%<::%E%> is not a class, namespace, or enumeration",
3016 error_at (location
, "%qE is not a type", name
);
3019 error_at (location
, "%qE is not a class or namespace", name
);
3023 "%qE is not a class, namespace, or enumeration", name
);
3031 /* If we are parsing tentatively, remember that an error has occurred
3032 during this tentative parse. Returns true if the error was
3033 simulated; false if a message should be issued by the caller. */
3036 cp_parser_simulate_error (cp_parser
* parser
)
3038 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3040 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3046 /* This function is called when a type is defined. If type
3047 definitions are forbidden at this point, an error message is
3051 cp_parser_check_type_definition (cp_parser
* parser
)
3053 /* If types are forbidden here, issue a message. */
3054 if (parser
->type_definition_forbidden_message
)
3056 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3057 in the message need to be interpreted. */
3058 error (parser
->type_definition_forbidden_message
);
3064 /* This function is called when the DECLARATOR is processed. The TYPE
3065 was a type defined in the decl-specifiers. If it is invalid to
3066 define a type in the decl-specifiers for DECLARATOR, an error is
3067 issued. TYPE_LOCATION is the location of TYPE and is used
3068 for error reporting. */
3071 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3072 tree type
, location_t type_location
)
3074 /* [dcl.fct] forbids type definitions in return types.
3075 Unfortunately, it's not easy to know whether or not we are
3076 processing a return type until after the fact. */
3078 && (declarator
->kind
== cdk_pointer
3079 || declarator
->kind
== cdk_reference
3080 || declarator
->kind
== cdk_ptrmem
))
3081 declarator
= declarator
->declarator
;
3083 && declarator
->kind
== cdk_function
)
3085 error_at (type_location
,
3086 "new types may not be defined in a return type");
3087 inform (type_location
,
3088 "(perhaps a semicolon is missing after the definition of %qT)",
3093 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3094 "<" in any valid C++ program. If the next token is indeed "<",
3095 issue a message warning the user about what appears to be an
3096 invalid attempt to form a template-id. LOCATION is the location
3097 of the type-specifier (TYPE) */
3100 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3102 enum tag_types tag_type
,
3103 location_t location
)
3105 cp_token_position start
= 0;
3107 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3109 if (TREE_CODE (type
) == TYPE_DECL
)
3110 type
= TREE_TYPE (type
);
3111 if (TYPE_P (type
) && !template_placeholder_p (type
))
3112 error_at (location
, "%qT is not a template", type
);
3113 else if (identifier_p (type
))
3115 if (tag_type
!= none_type
)
3116 error_at (location
, "%qE is not a class template", type
);
3118 error_at (location
, "%qE is not a template", type
);
3121 error_at (location
, "invalid template-id");
3122 /* Remember the location of the invalid "<". */
3123 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3124 start
= cp_lexer_token_position (parser
->lexer
, true);
3125 /* Consume the "<". */
3126 cp_lexer_consume_token (parser
->lexer
);
3127 /* Parse the template arguments. */
3128 cp_parser_enclosed_template_argument_list (parser
);
3129 /* Permanently remove the invalid template arguments so that
3130 this error message is not issued again. */
3132 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3136 /* If parsing an integral constant-expression, issue an error message
3137 about the fact that THING appeared and return true. Otherwise,
3138 return false. In either case, set
3139 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3142 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3143 non_integral_constant thing
)
3145 parser
->non_integral_constant_expression_p
= true;
3146 if (parser
->integral_constant_expression_p
)
3148 if (!parser
->allow_non_integral_constant_expression_p
)
3150 const char *msg
= NULL
;
3154 pedwarn (input_location
, OPT_Wpedantic
,
3155 "ISO C++ forbids using a floating-point literal "
3156 "in a constant-expression");
3159 error ("a cast to a type other than an integral or "
3160 "enumeration type cannot appear in a "
3161 "constant-expression");
3164 error ("%<typeid%> operator "
3165 "cannot appear in a constant-expression");
3168 error ("non-constant compound literals "
3169 "cannot appear in a constant-expression");
3172 error ("a function call "
3173 "cannot appear in a constant-expression");
3176 error ("an increment "
3177 "cannot appear in a constant-expression");
3180 error ("an decrement "
3181 "cannot appear in a constant-expression");
3184 error ("an array reference "
3185 "cannot appear in a constant-expression");
3187 case NIC_ADDR_LABEL
:
3188 error ("the address of a label "
3189 "cannot appear in a constant-expression");
3191 case NIC_OVERLOADED
:
3192 error ("calls to overloaded operators "
3193 "cannot appear in a constant-expression");
3195 case NIC_ASSIGNMENT
:
3196 error ("an assignment cannot appear in a constant-expression");
3199 error ("a comma operator "
3200 "cannot appear in a constant-expression");
3202 case NIC_CONSTRUCTOR
:
3203 error ("a call to a constructor "
3204 "cannot appear in a constant-expression");
3206 case NIC_TRANSACTION
:
3207 error ("a transaction expression "
3208 "cannot appear in a constant-expression");
3214 msg
= "__FUNCTION__";
3216 case NIC_PRETTY_FUNC
:
3217 msg
= "__PRETTY_FUNCTION__";
3237 case NIC_PREINCREMENT
:
3240 case NIC_PREDECREMENT
:
3253 error ("%qs cannot appear in a constant-expression", msg
);
3260 /* Emit a diagnostic for an invalid type name. This function commits
3261 to the current active tentative parse, if any. (Otherwise, the
3262 problematic construct might be encountered again later, resulting
3263 in duplicate error messages.) LOCATION is the location of ID. */
3266 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3267 location_t location
)
3269 tree decl
, ambiguous_decls
;
3270 cp_parser_commit_to_tentative_parse (parser
);
3271 /* Try to lookup the identifier. */
3272 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3273 /*is_template=*/false,
3274 /*is_namespace=*/false,
3275 /*check_dependency=*/true,
3276 &ambiguous_decls
, location
);
3277 if (ambiguous_decls
)
3278 /* If the lookup was ambiguous, an error will already have
3281 /* If the lookup found a template-name, it means that the user forgot
3282 to specify an argument list. Emit a useful error message. */
3283 if (DECL_TYPE_TEMPLATE_P (decl
))
3285 auto_diagnostic_group d
;
3287 "invalid use of template-name %qE without an argument list",
3289 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3290 inform (location
, "class template argument deduction is only available "
3291 "with -std=c++17 or -std=gnu++17");
3292 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3294 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3295 error_at (location
, "invalid use of destructor %qD as a type", id
);
3296 else if (TREE_CODE (decl
) == TYPE_DECL
)
3297 /* Something like 'unsigned A a;' */
3298 error_at (location
, "invalid combination of multiple type-specifiers");
3299 else if (!parser
->scope
)
3301 /* Issue an error message. */
3302 auto_diagnostic_group d
;
3304 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3305 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3306 if (const char *suggestion
= hint
.suggestion ())
3308 gcc_rich_location
richloc (location
);
3309 richloc
.add_fixit_replace (suggestion
);
3311 "%qE does not name a type; did you mean %qs?",
3315 error_at (location
, "%qE does not name a type", id
);
3316 /* If we're in a template class, it's possible that the user was
3317 referring to a type from a base class. For example:
3319 template <typename T> struct A { typedef T X; };
3320 template <typename T> struct B : public A<T> { X x; };
3322 The user should have said "typename A<T>::X". */
3323 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3324 inform (location
, "C++11 %<constexpr%> only available with "
3325 "-std=c++11 or -std=gnu++11");
3326 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3327 inform (location
, "C++11 %<noexcept%> only available with "
3328 "-std=c++11 or -std=gnu++11");
3329 else if (cxx_dialect
< cxx11
3330 && TREE_CODE (id
) == IDENTIFIER_NODE
3331 && id_equal (id
, "thread_local"))
3332 inform (location
, "C++11 %<thread_local%> only available with "
3333 "-std=c++11 or -std=gnu++11");
3334 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3335 inform (location
, "%<concept%> only available with -fconcepts");
3336 else if (processing_template_decl
&& current_class_type
3337 && TYPE_BINFO (current_class_type
))
3341 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3345 tree base_type
= BINFO_TYPE (b
);
3346 if (CLASS_TYPE_P (base_type
)
3347 && dependent_type_p (base_type
))
3350 /* Go from a particular instantiation of the
3351 template (which will have an empty TYPE_FIELDs),
3352 to the main version. */
3353 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3354 for (field
= TYPE_FIELDS (base_type
);
3356 field
= DECL_CHAIN (field
))
3357 if (TREE_CODE (field
) == TYPE_DECL
3358 && DECL_NAME (field
) == id
)
3361 "(perhaps %<typename %T::%E%> was intended)",
3362 BINFO_TYPE (b
), id
);
3371 /* Here we diagnose qualified-ids where the scope is actually correct,
3372 but the identifier does not resolve to a valid type name. */
3373 else if (parser
->scope
!= error_mark_node
)
3375 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3377 auto_diagnostic_group d
;
3379 if (decl
== error_mark_node
)
3380 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3382 const char *suggestion
= hint
.suggestion ();
3383 gcc_rich_location
richloc (location_of (id
));
3385 richloc
.add_fixit_replace (suggestion
);
3386 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3390 "%qE in namespace %qE does not name a template"
3391 " type; did you mean %qs?",
3392 id
, parser
->scope
, suggestion
);
3395 "%qE in namespace %qE does not name a template type",
3398 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3402 "%qE in namespace %qE does not name a template"
3403 " type; did you mean %qs?",
3404 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3407 "%qE in namespace %qE does not name a template"
3409 TREE_OPERAND (id
, 0), parser
->scope
);
3415 "%qE in namespace %qE does not name a type"
3416 "; did you mean %qs?",
3417 id
, parser
->scope
, suggestion
);
3420 "%qE in namespace %qE does not name a type",
3424 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3426 else if (CLASS_TYPE_P (parser
->scope
)
3427 && constructor_name_p (id
, parser
->scope
))
3430 auto_diagnostic_group d
;
3431 error_at (location
, "%<%T::%E%> names the constructor, not"
3432 " the type", parser
->scope
, id
);
3433 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3434 error_at (location
, "and %qT has no template constructors",
3437 else if (TYPE_P (parser
->scope
)
3438 && dependent_scope_p (parser
->scope
))
3440 gcc_rich_location
richloc (location
);
3441 richloc
.add_fixit_insert_before ("typename ");
3442 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3444 "need %<typename%> before %<%T::%D::%E%> because "
3445 "%<%T::%D%> is a dependent scope",
3446 TYPE_CONTEXT (parser
->scope
),
3447 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3449 TYPE_CONTEXT (parser
->scope
),
3450 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3452 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3453 "%qT is a dependent scope",
3454 parser
->scope
, id
, parser
->scope
);
3456 else if (TYPE_P (parser
->scope
))
3458 auto_diagnostic_group d
;
3459 if (!COMPLETE_TYPE_P (parser
->scope
))
3460 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3462 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3463 error_at (location_of (id
),
3464 "%qE in %q#T does not name a template type",
3466 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3467 error_at (location_of (id
),
3468 "%qE in %q#T does not name a template type",
3469 TREE_OPERAND (id
, 0), parser
->scope
);
3471 error_at (location_of (id
),
3472 "%qE in %q#T does not name a type",
3475 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3482 /* Check for a common situation where a type-name should be present,
3483 but is not, and issue a sensible error message. Returns true if an
3484 invalid type-name was detected.
3486 The situation handled by this function are variable declarations of the
3487 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3488 Usually, `ID' should name a type, but if we got here it means that it
3489 does not. We try to emit the best possible error message depending on
3490 how exactly the id-expression looks like. */
3493 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3496 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3498 /* Avoid duplicate error about ambiguous lookup. */
3499 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3501 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3502 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3506 cp_parser_parse_tentatively (parser
);
3507 id
= cp_parser_id_expression (parser
,
3508 /*template_keyword_p=*/false,
3509 /*check_dependency_p=*/true,
3510 /*template_p=*/NULL
,
3511 /*declarator_p=*/false,
3512 /*optional_p=*/false);
3513 /* If the next token is a (, this is a function with no explicit return
3514 type, i.e. constructor, destructor or conversion op. */
3515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3516 || TREE_CODE (id
) == TYPE_DECL
)
3518 cp_parser_abort_tentative_parse (parser
);
3521 if (!cp_parser_parse_definitely (parser
))
3524 /* Emit a diagnostic for the invalid type. */
3525 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3527 /* If we aren't in the middle of a declarator (i.e. in a
3528 parameter-declaration-clause), skip to the end of the declaration;
3529 there's no point in trying to process it. */
3530 if (!parser
->in_declarator_p
)
3531 cp_parser_skip_to_end_of_block_or_statement (parser
);
3535 /* Consume tokens up to, and including, the next non-nested closing `)'.
3536 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3537 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3538 found an unnested token of that type. */
3541 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3546 unsigned paren_depth
= 0;
3547 unsigned brace_depth
= 0;
3548 unsigned square_depth
= 0;
3549 unsigned condop_depth
= 0;
3551 if (recovering
&& or_ttype
== CPP_EOF
3552 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3557 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3559 /* Have we found what we're looking for before the closing paren? */
3560 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3561 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3564 switch (token
->type
)
3566 case CPP_PRAGMA_EOL
:
3567 if (!parser
->lexer
->in_pragma
)
3571 /* If we've run out of tokens, then there is no closing `)'. */
3574 /* This is good for lambda expression capture-lists. */
3575 case CPP_OPEN_SQUARE
:
3578 case CPP_CLOSE_SQUARE
:
3579 if (!square_depth
--)
3584 /* This matches the processing in skip_to_end_of_statement. */
3589 case CPP_OPEN_BRACE
:
3592 case CPP_CLOSE_BRACE
:
3597 case CPP_OPEN_PAREN
:
3602 case CPP_CLOSE_PAREN
:
3603 if (!brace_depth
&& !paren_depth
--)
3606 cp_lexer_consume_token (parser
->lexer
);
3612 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3617 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3625 /* Consume the token. */
3626 cp_lexer_consume_token (parser
->lexer
);
3630 /* Consume tokens up to, and including, the next non-nested closing `)'.
3631 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3632 are doing error recovery. Returns -1 if OR_COMMA is true and we
3633 found an unnested token of that type. */
3636 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3641 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3642 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3643 ttype
, consume_paren
);
3646 /* Consume tokens until we reach the end of the current statement.
3647 Normally, that will be just before consuming a `;'. However, if a
3648 non-nested `}' comes first, then we stop before consuming that. */
3651 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3653 unsigned nesting_depth
= 0;
3655 /* Unwind generic function template scope if necessary. */
3656 if (parser
->fully_implicit_function_template_p
)
3657 abort_fully_implicit_template (parser
);
3661 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3663 switch (token
->type
)
3665 case CPP_PRAGMA_EOL
:
3666 if (!parser
->lexer
->in_pragma
)
3670 /* If we've run out of tokens, stop. */
3674 /* If the next token is a `;', we have reached the end of the
3680 case CPP_CLOSE_BRACE
:
3681 /* If this is a non-nested '}', stop before consuming it.
3682 That way, when confronted with something like:
3686 we stop before consuming the closing '}', even though we
3687 have not yet reached a `;'. */
3688 if (nesting_depth
== 0)
3691 /* If it is the closing '}' for a block that we have
3692 scanned, stop -- but only after consuming the token.
3698 we will stop after the body of the erroneously declared
3699 function, but before consuming the following `typedef'
3701 if (--nesting_depth
== 0)
3703 cp_lexer_consume_token (parser
->lexer
);
3708 case CPP_OPEN_BRACE
:
3716 /* Consume the token. */
3717 cp_lexer_consume_token (parser
->lexer
);
3721 /* This function is called at the end of a statement or declaration.
3722 If the next token is a semicolon, it is consumed; otherwise, error
3723 recovery is attempted. */
3726 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3728 /* Look for the trailing `;'. */
3729 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3731 /* If there is additional (erroneous) input, skip to the end of
3733 cp_parser_skip_to_end_of_statement (parser
);
3734 /* If the next token is now a `;', consume it. */
3735 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3736 cp_lexer_consume_token (parser
->lexer
);
3740 /* Skip tokens until we have consumed an entire block, or until we
3741 have consumed a non-nested `;'. */
3744 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3746 int nesting_depth
= 0;
3748 /* Unwind generic function template scope if necessary. */
3749 if (parser
->fully_implicit_function_template_p
)
3750 abort_fully_implicit_template (parser
);
3752 while (nesting_depth
>= 0)
3754 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3756 switch (token
->type
)
3758 case CPP_PRAGMA_EOL
:
3759 if (!parser
->lexer
->in_pragma
)
3763 /* If we've run out of tokens, stop. */
3767 /* Stop if this is an unnested ';'. */
3772 case CPP_CLOSE_BRACE
:
3773 /* Stop if this is an unnested '}', or closes the outermost
3776 if (nesting_depth
< 0)
3782 case CPP_OPEN_BRACE
:
3791 /* Consume the token. */
3792 cp_lexer_consume_token (parser
->lexer
);
3796 /* Skip tokens until a non-nested closing curly brace is the next
3797 token, or there are no more tokens. Return true in the first case,
3801 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3803 unsigned nesting_depth
= 0;
3807 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3809 switch (token
->type
)
3811 case CPP_PRAGMA_EOL
:
3812 if (!parser
->lexer
->in_pragma
)
3816 /* If we've run out of tokens, stop. */
3819 case CPP_CLOSE_BRACE
:
3820 /* If the next token is a non-nested `}', then we have reached
3821 the end of the current block. */
3822 if (nesting_depth
-- == 0)
3826 case CPP_OPEN_BRACE
:
3827 /* If it the next token is a `{', then we are entering a new
3828 block. Consume the entire block. */
3836 /* Consume the token. */
3837 cp_lexer_consume_token (parser
->lexer
);
3841 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3842 parameter is the PRAGMA token, allowing us to purge the entire pragma
3846 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3850 parser
->lexer
->in_pragma
= false;
3853 token
= cp_lexer_consume_token (parser
->lexer
);
3854 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3856 /* Ensure that the pragma is not parsed again. */
3857 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3860 /* Require pragma end of line, resyncing with it as necessary. The
3861 arguments are as for cp_parser_skip_to_pragma_eol. */
3864 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3866 parser
->lexer
->in_pragma
= false;
3867 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3868 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3871 /* This is a simple wrapper around make_typename_type. When the id is
3872 an unresolved identifier node, we can provide a superior diagnostic
3873 using cp_parser_diagnose_invalid_type_name. */
3876 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3877 location_t id_location
)
3880 if (identifier_p (id
))
3882 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3883 /*complain=*/tf_none
);
3884 if (result
== error_mark_node
)
3885 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3888 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3891 /* This is a wrapper around the
3892 make_{pointer,ptrmem,reference}_declarator functions that decides
3893 which one to call based on the CODE and CLASS_TYPE arguments. The
3894 CODE argument should be one of the values returned by
3895 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3896 appertain to the pointer or reference. */
3898 static cp_declarator
*
3899 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3900 cp_cv_quals cv_qualifiers
,
3901 cp_declarator
*target
,
3904 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3905 return cp_error_declarator
;
3907 if (code
== INDIRECT_REF
)
3908 if (class_type
== NULL_TREE
)
3909 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3911 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3912 target
, attributes
);
3913 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3914 return make_reference_declarator (cv_qualifiers
, target
,
3916 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3917 return make_reference_declarator (cv_qualifiers
, target
,
3922 /* Create a new C++ parser. */
3925 cp_parser_new (void)
3931 /* cp_lexer_new_main is called before doing GC allocation because
3932 cp_lexer_new_main might load a PCH file. */
3933 lexer
= cp_lexer_new_main ();
3935 /* Initialize the binops_by_token so that we can get the tree
3936 directly from the token. */
3937 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3938 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3940 parser
= ggc_cleared_alloc
<cp_parser
> ();
3941 parser
->lexer
= lexer
;
3942 parser
->context
= cp_parser_context_new (NULL
);
3944 /* For now, we always accept GNU extensions. */
3945 parser
->allow_gnu_extensions_p
= 1;
3947 /* The `>' token is a greater-than operator, not the end of a
3949 parser
->greater_than_is_operator_p
= true;
3951 parser
->default_arg_ok_p
= true;
3953 /* We are not parsing a constant-expression. */
3954 parser
->integral_constant_expression_p
= false;
3955 parser
->allow_non_integral_constant_expression_p
= false;
3956 parser
->non_integral_constant_expression_p
= false;
3958 /* Local variable names are not forbidden. */
3959 parser
->local_variables_forbidden_p
= 0;
3961 /* We are not processing an `extern "C"' declaration. */
3962 parser
->in_unbraced_linkage_specification_p
= false;
3964 /* We are not processing a declarator. */
3965 parser
->in_declarator_p
= false;
3967 /* We are not processing a template-argument-list. */
3968 parser
->in_template_argument_list_p
= false;
3970 /* We are not in an iteration statement. */
3971 parser
->in_statement
= 0;
3973 /* We are not in a switch statement. */
3974 parser
->in_switch_statement_p
= false;
3976 /* We are not parsing a type-id inside an expression. */
3977 parser
->in_type_id_in_expr_p
= false;
3979 /* String literals should be translated to the execution character set. */
3980 parser
->translate_strings_p
= true;
3982 /* We are not parsing a function body. */
3983 parser
->in_function_body
= false;
3985 /* We can correct until told otherwise. */
3986 parser
->colon_corrects_to_scope_p
= true;
3988 /* The unparsed function queue is empty. */
3989 push_unparsed_function_queues (parser
);
3991 /* There are no classes being defined. */
3992 parser
->num_classes_being_defined
= 0;
3994 /* No template parameters apply. */
3995 parser
->num_template_parameter_lists
= 0;
3997 /* Special parsing data structures. */
3998 parser
->omp_declare_simd
= NULL
;
3999 parser
->oacc_routine
= NULL
;
4001 /* Not declaring an implicit function template. */
4002 parser
->auto_is_implicit_function_template_parm_p
= false;
4003 parser
->fully_implicit_function_template_p
= false;
4004 parser
->implicit_template_parms
= 0;
4005 parser
->implicit_template_scope
= 0;
4007 /* Allow constrained-type-specifiers. */
4008 parser
->prevent_constrained_type_specifiers
= 0;
4010 /* We haven't yet seen an 'extern "C"'. */
4011 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
4016 /* Create a cp_lexer structure which will emit the tokens in CACHE
4017 and push it onto the parser's lexer stack. This is used for delayed
4018 parsing of in-class method bodies and default arguments, and should
4019 not be confused with tentative parsing. */
4021 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4023 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4024 lexer
->next
= parser
->lexer
;
4025 parser
->lexer
= lexer
;
4027 /* Move the current source position to that of the first token in the
4029 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4032 /* Pop the top lexer off the parser stack. This is never used for the
4033 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4035 cp_parser_pop_lexer (cp_parser
*parser
)
4037 cp_lexer
*lexer
= parser
->lexer
;
4038 parser
->lexer
= lexer
->next
;
4039 cp_lexer_destroy (lexer
);
4041 /* Put the current source position back where it was before this
4042 lexer was pushed. */
4043 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4046 /* Lexical conventions [gram.lex] */
4048 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4052 cp_parser_identifier (cp_parser
* parser
)
4056 /* Look for the identifier. */
4057 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4058 /* Return the value. */
4060 return cp_expr (token
->u
.value
, token
->location
);
4062 return error_mark_node
;
4065 /* Parse a sequence of adjacent string constants. Returns a
4066 TREE_STRING representing the combined, nul-terminated string
4067 constant. If TRANSLATE is true, translate the string to the
4068 execution character set. If WIDE_OK is true, a wide string is
4071 C++98 [lex.string] says that if a narrow string literal token is
4072 adjacent to a wide string literal token, the behavior is undefined.
4073 However, C99 6.4.5p4 says that this results in a wide string literal.
4074 We follow C99 here, for consistency with the C front end.
4076 This code is largely lifted from lex_string() in c-lex.c.
4078 FUTURE: ObjC++ will need to handle @-strings here. */
4080 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4081 bool lookup_udlit
= true)
4085 struct obstack str_ob
;
4086 struct obstack loc_ob
;
4087 cpp_string str
, istr
, *strs
;
4089 enum cpp_ttype type
, curr_type
;
4090 int have_suffix_p
= 0;
4092 tree suffix_id
= NULL_TREE
;
4093 bool curr_tok_is_userdef_p
= false;
4095 tok
= cp_lexer_peek_token (parser
->lexer
);
4096 if (!cp_parser_is_string_literal (tok
))
4098 cp_parser_error (parser
, "expected string-literal");
4099 return error_mark_node
;
4102 location_t loc
= tok
->location
;
4104 if (cpp_userdef_string_p (tok
->type
))
4106 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4107 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4108 curr_tok_is_userdef_p
= true;
4112 string_tree
= tok
->u
.value
;
4113 curr_type
= tok
->type
;
4117 /* Try to avoid the overhead of creating and destroying an obstack
4118 for the common case of just one string. */
4119 if (!cp_parser_is_string_literal
4120 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4122 cp_lexer_consume_token (parser
->lexer
);
4124 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4125 str
.len
= TREE_STRING_LENGTH (string_tree
);
4128 if (curr_tok_is_userdef_p
)
4130 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4132 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4135 curr_type
= tok
->type
;
4141 location_t last_tok_loc
= tok
->location
;
4142 gcc_obstack_init (&str_ob
);
4143 gcc_obstack_init (&loc_ob
);
4148 cp_lexer_consume_token (parser
->lexer
);
4150 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4151 str
.len
= TREE_STRING_LENGTH (string_tree
);
4153 if (curr_tok_is_userdef_p
)
4155 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4156 if (have_suffix_p
== 0)
4158 suffix_id
= curr_suffix_id
;
4161 else if (have_suffix_p
== 1
4162 && curr_suffix_id
!= suffix_id
)
4164 error ("inconsistent user-defined literal suffixes"
4165 " %qD and %qD in string literal",
4166 suffix_id
, curr_suffix_id
);
4169 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4172 curr_type
= tok
->type
;
4174 if (type
!= curr_type
)
4176 if (type
== CPP_STRING
)
4178 else if (curr_type
!= CPP_STRING
)
4180 rich_location
rich_loc (line_table
, tok
->location
);
4181 rich_loc
.add_range (last_tok_loc
);
4182 error_at (&rich_loc
,
4183 "unsupported non-standard concatenation "
4184 "of string literals");
4188 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4189 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4191 last_tok_loc
= tok
->location
;
4193 tok
= cp_lexer_peek_token (parser
->lexer
);
4194 if (cpp_userdef_string_p (tok
->type
))
4196 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4197 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4198 curr_tok_is_userdef_p
= true;
4202 string_tree
= tok
->u
.value
;
4203 curr_type
= tok
->type
;
4204 curr_tok_is_userdef_p
= false;
4207 while (cp_parser_is_string_literal (tok
));
4209 /* A string literal built by concatenation has its caret=start at
4210 the start of the initial string, and its finish at the finish of
4211 the final string literal. */
4212 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4214 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4217 if (type
!= CPP_STRING
&& !wide_ok
)
4219 cp_parser_error (parser
, "a wide string is invalid in this context");
4223 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4224 (parse_in
, strs
, count
, &istr
, type
))
4226 value
= build_string (istr
.len
, (const char *)istr
.text
);
4227 free (CONST_CAST (unsigned char *, istr
.text
));
4230 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4231 gcc_assert (g_string_concat_db
);
4232 g_string_concat_db
->record_string_concatenation (count
, locs
);
4239 TREE_TYPE (value
) = char_array_type_node
;
4241 case CPP_UTF8STRING
:
4243 TREE_TYPE (value
) = char8_array_type_node
;
4245 TREE_TYPE (value
) = char_array_type_node
;
4248 TREE_TYPE (value
) = char16_array_type_node
;
4251 TREE_TYPE (value
) = char32_array_type_node
;
4254 TREE_TYPE (value
) = wchar_array_type_node
;
4258 value
= fix_string_type (value
);
4262 tree literal
= build_userdef_literal (suffix_id
, value
,
4263 OT_NONE
, NULL_TREE
);
4265 value
= cp_parser_userdef_string_literal (literal
);
4271 /* cpp_interpret_string has issued an error. */
4272 value
= error_mark_node
;
4276 obstack_free (&str_ob
, 0);
4277 obstack_free (&loc_ob
, 0);
4280 return cp_expr (value
, loc
);
4283 /* Look up a literal operator with the name and the exact arguments. */
4286 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4288 tree decl
= lookup_name (name
);
4289 if (!decl
|| !is_overloaded_fn (decl
))
4290 return error_mark_node
;
4292 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4296 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4302 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4303 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4305 tree tparm
= TREE_VALUE (parmtypes
);
4306 tree targ
= TREE_TYPE ((*args
)[ix
]);
4307 bool ptr
= TYPE_PTR_P (tparm
);
4308 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4309 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4311 || !same_type_p (TREE_TYPE (tparm
),
4317 && ix
== vec_safe_length (args
)
4318 /* May be this should be sufficient_parms_p instead,
4319 depending on how exactly should user-defined literals
4320 work in presence of default arguments on the literal
4321 operator parameters. */
4322 && parmtypes
== void_list_node
)
4327 return error_mark_node
;
4330 /* Parse a user-defined char constant. Returns a call to a user-defined
4331 literal operator taking the character as an argument. */
4334 cp_parser_userdef_char_literal (cp_parser
*parser
)
4336 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4337 tree literal
= token
->u
.value
;
4338 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4339 tree value
= USERDEF_LITERAL_VALUE (literal
);
4340 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4343 /* Build up a call to the user-defined operator */
4344 /* Lookup the name we got back from the id-expression. */
4345 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4346 vec_safe_push (args
, value
);
4347 decl
= lookup_literal_operator (name
, args
);
4348 if (!decl
|| decl
== error_mark_node
)
4350 error ("unable to find character literal operator %qD with %qT argument",
4351 name
, TREE_TYPE (value
));
4352 release_tree_vector (args
);
4353 return error_mark_node
;
4355 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4356 release_tree_vector (args
);
4360 /* A subroutine of cp_parser_userdef_numeric_literal to
4361 create a char... template parameter pack from a string node. */
4364 make_char_string_pack (tree value
)
4367 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4368 const char *str
= TREE_STRING_POINTER (value
);
4369 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4370 tree argvec
= make_tree_vec (1);
4372 /* Fill in CHARVEC with all of the parameters. */
4373 charvec
= make_tree_vec (len
);
4374 for (i
= 0; i
< len
; ++i
)
4376 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4377 cpp_string in
= { 3, s
};
4378 cpp_string out
= { 0, 0 };
4379 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4381 gcc_assert (out
.len
== 2);
4382 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4386 /* Build the argument packs. */
4387 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4389 TREE_VEC_ELT (argvec
, 0) = argpack
;
4394 /* A subroutine of cp_parser_userdef_numeric_literal to
4395 create a char... template parameter pack from a string node. */
4398 make_string_pack (tree value
)
4401 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4402 const unsigned char *str
4403 = (const unsigned char *) TREE_STRING_POINTER (value
);
4404 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4405 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4406 tree argvec
= make_tree_vec (2);
4408 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4409 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4411 /* First template parm is character type. */
4412 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4414 /* Fill in CHARVEC with all of the parameters. */
4415 charvec
= make_tree_vec (len
);
4416 for (int i
= 0; i
< len
; ++i
)
4417 TREE_VEC_ELT (charvec
, i
)
4418 = double_int_to_tree (str_char_type_node
,
4419 double_int::from_buffer (str
+ i
* sz
, sz
));
4421 /* Build the argument packs. */
4422 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4424 TREE_VEC_ELT (argvec
, 1) = argpack
;
4429 /* Parse a user-defined numeric constant. returns a call to a user-defined
4430 literal operator. */
4433 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4435 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4436 tree literal
= token
->u
.value
;
4437 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4438 tree value
= USERDEF_LITERAL_VALUE (literal
);
4439 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4440 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4441 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4443 vec
<tree
, va_gc
> *args
;
4445 /* Look for a literal operator taking the exact type of numeric argument
4446 as the literal value. */
4447 args
= make_tree_vector ();
4448 vec_safe_push (args
, value
);
4449 decl
= lookup_literal_operator (name
, args
);
4450 if (decl
&& decl
!= error_mark_node
)
4452 result
= finish_call_expr (decl
, &args
, false, true,
4453 tf_warning_or_error
);
4455 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4457 warning_at (token
->location
, OPT_Woverflow
,
4458 "integer literal exceeds range of %qT type",
4459 long_long_unsigned_type_node
);
4464 warning_at (token
->location
, OPT_Woverflow
,
4465 "floating literal exceeds range of %qT type",
4466 long_double_type_node
);
4467 else if (overflow
< 0)
4468 warning_at (token
->location
, OPT_Woverflow
,
4469 "floating literal truncated to zero");
4472 release_tree_vector (args
);
4475 release_tree_vector (args
);
4477 /* If the numeric argument didn't work, look for a raw literal
4478 operator taking a const char* argument consisting of the number
4479 in string format. */
4480 args
= make_tree_vector ();
4481 vec_safe_push (args
, num_string
);
4482 decl
= lookup_literal_operator (name
, args
);
4483 if (decl
&& decl
!= error_mark_node
)
4485 result
= finish_call_expr (decl
, &args
, false, true,
4486 tf_warning_or_error
);
4487 release_tree_vector (args
);
4490 release_tree_vector (args
);
4492 /* If the raw literal didn't work, look for a non-type template
4493 function with parameter pack char.... Call the function with
4494 template parameter characters representing the number. */
4495 args
= make_tree_vector ();
4496 decl
= lookup_literal_operator (name
, args
);
4497 if (decl
&& decl
!= error_mark_node
)
4499 tree tmpl_args
= make_char_string_pack (num_string
);
4500 if (tmpl_args
== NULL_TREE
)
4502 error ("failed to translate literal to execution character set %qT",
4504 return error_mark_node
;
4506 decl
= lookup_template_function (decl
, tmpl_args
);
4507 result
= finish_call_expr (decl
, &args
, false, true,
4508 tf_warning_or_error
);
4509 release_tree_vector (args
);
4513 release_tree_vector (args
);
4515 /* In C++14 the standard library defines complex number suffixes that
4516 conflict with GNU extensions. Prefer them if <complex> is #included. */
4517 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4518 bool i14
= (cxx_dialect
> cxx11
4519 && (id_equal (suffix_id
, "i")
4520 || id_equal (suffix_id
, "if")
4521 || id_equal (suffix_id
, "il")));
4522 diagnostic_t kind
= DK_ERROR
;
4527 tree cxlit
= lookup_qualified_name (std_node
,
4528 get_identifier ("complex_literals"),
4530 if (cxlit
== error_mark_node
)
4532 /* No <complex>, so pedwarn and use GNU semantics. */
4534 opt
= OPT_Wpedantic
;
4539 = emit_diagnostic (kind
, input_location
, opt
,
4540 "unable to find numeric literal operator %qD", name
);
4543 /* Don't inform either. */;
4546 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4547 "(from <complex>) to enable the C++14 user-defined literal "
4550 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4551 "GNU built-in suffix");
4554 inform (token
->location
, "use -fext-numeric-literals "
4555 "to enable more built-in suffixes");
4557 if (kind
== DK_ERROR
)
4558 value
= error_mark_node
;
4561 /* Use the built-in semantics. */
4563 if (id_equal (suffix_id
, "i"))
4565 if (TREE_CODE (value
) == INTEGER_CST
)
4566 type
= integer_type_node
;
4568 type
= double_type_node
;
4570 else if (id_equal (suffix_id
, "if"))
4571 type
= float_type_node
;
4572 else /* if (id_equal (suffix_id, "il")) */
4573 type
= long_double_type_node
;
4575 value
= build_complex (build_complex_type (type
),
4576 fold_convert (type
, integer_zero_node
),
4577 fold_convert (type
, value
));
4580 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4581 /* Avoid repeated diagnostics. */
4582 token
->u
.value
= value
;
4586 /* Parse a user-defined string constant. Returns a call to a user-defined
4587 literal operator taking a character pointer and the length of the string
4591 cp_parser_userdef_string_literal (tree literal
)
4593 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4594 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4595 tree value
= USERDEF_LITERAL_VALUE (literal
);
4596 int len
= TREE_STRING_LENGTH (value
)
4597 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4600 /* Build up a call to the user-defined operator. */
4601 /* Lookup the name we got back from the id-expression. */
4602 releasing_vec rargs
;
4603 vec
<tree
, va_gc
> *&args
= rargs
.get_ref();
4604 vec_safe_push (args
, value
);
4605 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4606 decl
= lookup_literal_operator (name
, args
);
4608 if (decl
&& decl
!= error_mark_node
)
4609 return finish_call_expr (decl
, &args
, false, true,
4610 tf_warning_or_error
);
4612 /* Look for a suitable template function, either (C++20) with a single
4613 parameter of class type, or (N3599) with typename parameter CharT and
4614 parameter pack CharT... */
4616 decl
= lookup_literal_operator (name
, args
);
4617 if (decl
&& decl
!= error_mark_node
)
4619 /* Use resolve_nondeduced_context to try to choose one form of template
4621 tree tmpl_args
= make_tree_vec (1);
4622 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4623 decl
= lookup_template_function (decl
, tmpl_args
);
4624 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4629 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4630 res
= resolve_nondeduced_context (decl
, tf_none
);
4634 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4635 TREE_OPERAND (decl
, 1) = tmpl_args
;
4636 return finish_call_expr (decl
, &args
, false, true,
4637 tf_warning_or_error
);
4640 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4641 name
, TREE_TYPE (value
), size_type_node
);
4642 return error_mark_node
;
4646 /* Basic concepts [gram.basic] */
4648 /* Parse a translation-unit.
4651 declaration-seq [opt] */
4654 cp_parser_translation_unit (cp_parser
* parser
)
4656 gcc_checking_assert (!cp_error_declarator
);
4658 /* Create the declarator obstack. */
4659 gcc_obstack_init (&declarator_obstack
);
4660 /* Create the error declarator. */
4661 cp_error_declarator
= make_declarator (cdk_error
);
4662 /* Create the empty parameter list. */
4663 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4665 /* Remember where the base of the declarator obstack lies. */
4666 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4668 bool implicit_extern_c
= false;
4672 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4674 /* If we're entering or exiting a region that's implicitly
4675 extern "C", modify the lang context appropriately. */
4676 if (implicit_extern_c
4677 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4679 implicit_extern_c
= !implicit_extern_c
;
4680 if (implicit_extern_c
)
4681 push_lang_context (lang_name_c
);
4683 pop_lang_context ();
4686 if (token
->type
== CPP_EOF
)
4689 if (token
->type
== CPP_CLOSE_BRACE
)
4691 cp_parser_error (parser
, "expected declaration");
4692 cp_lexer_consume_token (parser
->lexer
);
4693 /* If the next token is now a `;', consume it. */
4694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4695 cp_lexer_consume_token (parser
->lexer
);
4698 cp_parser_toplevel_declaration (parser
);
4701 /* Get rid of the token array; we don't need it any more. */
4702 cp_lexer_destroy (parser
->lexer
);
4703 parser
->lexer
= NULL
;
4705 /* The EOF should have reset this. */
4706 gcc_checking_assert (!implicit_extern_c
);
4708 /* Make sure the declarator obstack was fully cleaned up. */
4709 gcc_assert (obstack_next_free (&declarator_obstack
)
4710 == declarator_obstack_base
);
4713 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4714 decltype context. */
4716 static inline tsubst_flags_t
4717 complain_flags (bool decltype_p
)
4719 tsubst_flags_t complain
= tf_warning_or_error
;
4721 complain
|= tf_decltype
;
4725 /* We're about to parse a collection of statements. If we're currently
4726 parsing tentatively, set up a firewall so that any nested
4727 cp_parser_commit_to_tentative_parse won't affect the current context. */
4729 static cp_token_position
4730 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4732 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4735 cp_parser_parse_tentatively (parser
);
4736 cp_parser_commit_to_topmost_tentative_parse (parser
);
4737 return cp_lexer_token_position (parser
->lexer
, false);
4740 /* We've finished parsing the collection of statements. Wrap up the
4741 firewall and replace the relevant tokens with the parsed form. */
4744 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4750 /* Finish the firewall level. */
4751 cp_parser_parse_definitely (parser
);
4752 /* And remember the result of the parse for when we try again. */
4753 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4754 token
->type
= CPP_PREPARSED_EXPR
;
4755 token
->u
.value
= expr
;
4756 token
->keyword
= RID_MAX
;
4757 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4760 /* Like the above functions, but let the user modify the tokens. Used by
4761 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4762 later parses, so it makes sense to localize the effects of
4763 cp_parser_commit_to_tentative_parse. */
4765 struct tentative_firewall
4770 tentative_firewall (cp_parser
*p
): parser(p
)
4772 /* If we're currently parsing tentatively, start a committed level as a
4773 firewall and then an inner tentative parse. */
4774 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4776 cp_parser_parse_tentatively (parser
);
4777 cp_parser_commit_to_topmost_tentative_parse (parser
);
4778 cp_parser_parse_tentatively (parser
);
4782 ~tentative_firewall()
4786 /* Finish the inner tentative parse and the firewall, propagating any
4787 uncommitted error state to the outer tentative parse. */
4788 bool err
= cp_parser_error_occurred (parser
);
4789 cp_parser_parse_definitely (parser
);
4790 cp_parser_parse_definitely (parser
);
4792 cp_parser_simulate_error (parser
);
4797 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4798 This class is for tracking such a matching pair of symbols.
4799 In particular, it tracks the location of the first token,
4800 so that if the second token is missing, we can highlight the
4801 location of the first token when notifying the user about the
4804 template <typename traits_t
>
4808 /* token_pair's ctor. */
4809 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4811 /* If the next token is the opening symbol for this pair, consume it and
4813 Otherwise, issue an error and return false.
4814 In either case, record the location of the opening token. */
4816 bool require_open (cp_parser
*parser
)
4818 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4819 return cp_parser_require (parser
, traits_t::open_token_type
,
4820 traits_t::required_token_open
);
4823 /* Consume the next token from PARSER, recording its location as
4824 that of the opening token within the pair. */
4826 cp_token
* consume_open (cp_parser
*parser
)
4828 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4829 gcc_assert (tok
->type
== traits_t::open_token_type
);
4830 m_open_loc
= tok
->location
;
4834 /* If the next token is the closing symbol for this pair, consume it
4836 Otherwise, issue an error, highlighting the location of the
4837 corresponding opening token, and return NULL. */
4839 cp_token
*require_close (cp_parser
*parser
) const
4841 return cp_parser_require (parser
, traits_t::close_token_type
,
4842 traits_t::required_token_close
,
4847 location_t m_open_loc
;
4850 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4852 struct matching_paren_traits
4854 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4855 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4856 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4857 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4860 /* "matching_parens" is a token_pair<T> class for tracking matching
4861 pairs of parentheses. */
4863 typedef token_pair
<matching_paren_traits
> matching_parens
;
4865 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4867 struct matching_brace_traits
4869 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4870 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4871 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4872 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4875 /* "matching_braces" is a token_pair<T> class for tracking matching
4878 typedef token_pair
<matching_brace_traits
> matching_braces
;
4881 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4882 enclosing parentheses. */
4885 cp_parser_statement_expr (cp_parser
*parser
)
4887 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4889 /* Consume the '('. */
4890 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4891 matching_parens parens
;
4892 parens
.consume_open (parser
);
4893 /* Start the statement-expression. */
4894 tree expr
= begin_stmt_expr ();
4895 /* Parse the compound-statement. */
4896 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4898 expr
= finish_stmt_expr (expr
, false);
4899 /* Consume the ')'. */
4900 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4901 if (!parens
.require_close (parser
))
4902 cp_parser_skip_to_end_of_statement (parser
);
4904 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4905 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4906 return cp_expr (expr
, combined_loc
);
4909 /* Expressions [gram.expr] */
4911 /* Parse a fold-operator.
4914 - * / % ^ & | = < > << >>
4915 = -= *= /= %= ^= &= |= <<= >>=
4916 == != <= >= && || , .* ->*
4918 This returns the tree code corresponding to the matched operator
4919 as an int. When the current token matches a compound assignment
4920 opertor, the resulting tree code is the negative value of the
4921 non-assignment operator. */
4924 cp_parser_fold_operator (cp_token
*token
)
4926 switch (token
->type
)
4928 case CPP_PLUS
: return PLUS_EXPR
;
4929 case CPP_MINUS
: return MINUS_EXPR
;
4930 case CPP_MULT
: return MULT_EXPR
;
4931 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4932 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4933 case CPP_XOR
: return BIT_XOR_EXPR
;
4934 case CPP_AND
: return BIT_AND_EXPR
;
4935 case CPP_OR
: return BIT_IOR_EXPR
;
4936 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4937 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4939 case CPP_EQ
: return -NOP_EXPR
;
4940 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4941 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4942 case CPP_MULT_EQ
: return -MULT_EXPR
;
4943 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4944 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4945 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4946 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4947 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4948 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4949 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4951 case CPP_EQ_EQ
: return EQ_EXPR
;
4952 case CPP_NOT_EQ
: return NE_EXPR
;
4953 case CPP_LESS
: return LT_EXPR
;
4954 case CPP_GREATER
: return GT_EXPR
;
4955 case CPP_LESS_EQ
: return LE_EXPR
;
4956 case CPP_GREATER_EQ
: return GE_EXPR
;
4958 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4959 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4961 case CPP_COMMA
: return COMPOUND_EXPR
;
4963 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4964 case CPP_DEREF_STAR
: return MEMBER_REF
;
4966 default: return ERROR_MARK
;
4970 /* Returns true if CODE indicates a binary expression, which is not allowed in
4971 the LHS of a fold-expression. More codes will need to be added to use this
4972 function in other contexts. */
4975 is_binary_op (tree_code code
)
4980 case POINTER_PLUS_EXPR
:
4983 case TRUNC_DIV_EXPR
:
4984 case TRUNC_MOD_EXPR
:
5000 case TRUTH_ANDIF_EXPR
:
5001 case TRUTH_ORIF_EXPR
:
5014 /* If the next token is a suitable fold operator, consume it and return as
5015 the function above. */
5018 cp_parser_fold_operator (cp_parser
*parser
)
5020 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5021 int code
= cp_parser_fold_operator (token
);
5022 if (code
!= ERROR_MARK
)
5023 cp_lexer_consume_token (parser
->lexer
);
5027 /* Parse a fold-expression.
5030 ( ... folding-operator cast-expression)
5031 ( cast-expression folding-operator ... )
5032 ( cast-expression folding operator ... folding-operator cast-expression)
5034 Note that the '(' and ')' are matched in primary expression. */
5037 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5042 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5044 cp_lexer_consume_token (parser
->lexer
);
5045 int op
= cp_parser_fold_operator (parser
);
5046 if (op
== ERROR_MARK
)
5048 cp_parser_error (parser
, "expected binary operator");
5049 return error_mark_node
;
5052 tree expr
= cp_parser_cast_expression (parser
, false, false,
5054 if (expr
== error_mark_node
)
5055 return error_mark_node
;
5056 return finish_left_unary_fold_expr (expr
, op
);
5059 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5060 int op
= cp_parser_fold_operator (parser
);
5061 if (op
== ERROR_MARK
)
5063 cp_parser_error (parser
, "expected binary operator");
5064 return error_mark_node
;
5067 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5069 cp_parser_error (parser
, "expected ...");
5070 return error_mark_node
;
5072 cp_lexer_consume_token (parser
->lexer
);
5074 /* The operands of a fold-expression are cast-expressions, so binary or
5075 conditional expressions are not allowed. We check this here to avoid
5076 tentative parsing. */
5077 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5078 /* OK, the expression was parenthesized. */;
5079 else if (is_binary_op (TREE_CODE (expr1
)))
5080 error_at (location_of (expr1
),
5081 "binary expression in operand of fold-expression");
5082 else if (TREE_CODE (expr1
) == COND_EXPR
5083 || (REFERENCE_REF_P (expr1
)
5084 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5085 error_at (location_of (expr1
),
5086 "conditional expression in operand of fold-expression");
5089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5090 return finish_right_unary_fold_expr (expr1
, op
);
5092 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5094 cp_parser_error (parser
, "mismatched operator in fold-expression");
5095 return error_mark_node
;
5097 cp_lexer_consume_token (parser
->lexer
);
5099 // Binary left or right fold.
5100 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5101 if (expr2
== error_mark_node
)
5102 return error_mark_node
;
5103 return finish_binary_fold_expr (expr1
, expr2
, op
);
5106 /* Parse a primary-expression.
5113 lambda-expression (C++11)
5118 ( compound-statement )
5119 __builtin_va_arg ( assignment-expression , type-id )
5120 __builtin_offsetof ( type-id , offsetof-expression )
5123 __has_nothrow_assign ( type-id )
5124 __has_nothrow_constructor ( type-id )
5125 __has_nothrow_copy ( type-id )
5126 __has_trivial_assign ( type-id )
5127 __has_trivial_constructor ( type-id )
5128 __has_trivial_copy ( type-id )
5129 __has_trivial_destructor ( type-id )
5130 __has_virtual_destructor ( type-id )
5131 __is_abstract ( type-id )
5132 __is_base_of ( type-id , type-id )
5133 __is_class ( type-id )
5134 __is_empty ( type-id )
5135 __is_enum ( type-id )
5136 __is_final ( type-id )
5137 __is_literal_type ( type-id )
5138 __is_pod ( type-id )
5139 __is_polymorphic ( type-id )
5140 __is_std_layout ( type-id )
5141 __is_trivial ( type-id )
5142 __is_union ( type-id )
5144 Objective-C++ Extension:
5152 ADDRESS_P is true iff this expression was immediately preceded by
5153 "&" and therefore might denote a pointer-to-member. CAST_P is true
5154 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5155 true iff this expression is a template argument.
5157 Returns a representation of the expression. Upon return, *IDK
5158 indicates what kind of id-expression (if any) was present. */
5161 cp_parser_primary_expression (cp_parser
*parser
,
5164 bool template_arg_p
,
5168 cp_token
*token
= NULL
;
5170 /* Assume the primary expression is not an id-expression. */
5171 *idk
= CP_ID_KIND_NONE
;
5173 /* Peek at the next token. */
5174 token
= cp_lexer_peek_token (parser
->lexer
);
5175 switch ((int) token
->type
)
5184 user-defined-literal */
5191 case CPP_PREPARSED_EXPR
:
5192 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5193 return cp_parser_userdef_numeric_literal (parser
);
5194 token
= cp_lexer_consume_token (parser
->lexer
);
5195 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5197 error_at (token
->location
,
5198 "fixed-point types not supported in C++");
5199 return error_mark_node
;
5201 /* Floating-point literals are only allowed in an integral
5202 constant expression if they are cast to an integral or
5203 enumeration type. */
5204 if (TREE_CODE (token
->u
.value
) == REAL_CST
5205 && parser
->integral_constant_expression_p
5208 /* CAST_P will be set even in invalid code like "int(2.7 +
5209 ...)". Therefore, we have to check that the next token
5210 is sure to end the cast. */
5213 cp_token
*next_token
;
5215 next_token
= cp_lexer_peek_token (parser
->lexer
);
5216 if (/* The comma at the end of an
5217 enumerator-definition. */
5218 next_token
->type
!= CPP_COMMA
5219 /* The curly brace at the end of an enum-specifier. */
5220 && next_token
->type
!= CPP_CLOSE_BRACE
5221 /* The end of a statement. */
5222 && next_token
->type
!= CPP_SEMICOLON
5223 /* The end of the cast-expression. */
5224 && next_token
->type
!= CPP_CLOSE_PAREN
5225 /* The end of an array bound. */
5226 && next_token
->type
!= CPP_CLOSE_SQUARE
5227 /* The closing ">" in a template-argument-list. */
5228 && (next_token
->type
!= CPP_GREATER
5229 || parser
->greater_than_is_operator_p
)
5230 /* C++0x only: A ">>" treated like two ">" tokens,
5231 in a template-argument-list. */
5232 && (next_token
->type
!= CPP_RSHIFT
5233 || (cxx_dialect
== cxx98
)
5234 || parser
->greater_than_is_operator_p
))
5238 /* If we are within a cast, then the constraint that the
5239 cast is to an integral or enumeration type will be
5240 checked at that point. If we are not within a cast, then
5241 this code is invalid. */
5243 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5245 return (cp_expr (token
->u
.value
, token
->location
)
5246 .maybe_add_location_wrapper ());
5248 case CPP_CHAR_USERDEF
:
5249 case CPP_CHAR16_USERDEF
:
5250 case CPP_CHAR32_USERDEF
:
5251 case CPP_WCHAR_USERDEF
:
5252 case CPP_UTF8CHAR_USERDEF
:
5253 return cp_parser_userdef_char_literal (parser
);
5259 case CPP_UTF8STRING
:
5260 case CPP_STRING_USERDEF
:
5261 case CPP_STRING16_USERDEF
:
5262 case CPP_STRING32_USERDEF
:
5263 case CPP_WSTRING_USERDEF
:
5264 case CPP_UTF8STRING_USERDEF
:
5265 /* ??? Should wide strings be allowed when parser->translate_strings_p
5266 is false (i.e. in attributes)? If not, we can kill the third
5267 argument to cp_parser_string_literal. */
5268 return (cp_parser_string_literal (parser
,
5269 parser
->translate_strings_p
,
5271 .maybe_add_location_wrapper ());
5273 case CPP_OPEN_PAREN
:
5274 /* If we see `( { ' then we are looking at the beginning of
5275 a GNU statement-expression. */
5276 if (cp_parser_allow_gnu_extensions_p (parser
)
5277 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5279 /* Statement-expressions are not allowed by the standard. */
5280 pedwarn (token
->location
, OPT_Wpedantic
,
5281 "ISO C++ forbids braced-groups within expressions");
5283 /* And they're not allowed outside of a function-body; you
5284 cannot, for example, write:
5286 int i = ({ int j = 3; j + 1; });
5288 at class or namespace scope. */
5289 if (!parser
->in_function_body
5290 || parser
->in_template_argument_list_p
)
5292 error_at (token
->location
,
5293 "statement-expressions are not allowed outside "
5294 "functions nor in template-argument lists");
5295 cp_parser_skip_to_end_of_block_or_statement (parser
);
5296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5297 cp_lexer_consume_token (parser
->lexer
);
5298 return error_mark_node
;
5301 return cp_parser_statement_expr (parser
);
5303 /* Otherwise it's a normal parenthesized expression. */
5306 bool saved_greater_than_is_operator_p
;
5308 location_t open_paren_loc
= token
->location
;
5310 /* Consume the `('. */
5311 matching_parens parens
;
5312 parens
.consume_open (parser
);
5313 /* Within a parenthesized expression, a `>' token is always
5314 the greater-than operator. */
5315 saved_greater_than_is_operator_p
5316 = parser
->greater_than_is_operator_p
;
5317 parser
->greater_than_is_operator_p
= true;
5319 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5320 /* Left fold expression. */
5323 /* Parse the parenthesized expression. */
5324 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5326 token
= cp_lexer_peek_token (parser
->lexer
);
5327 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5329 expr
= cp_parser_fold_expression (parser
, expr
);
5330 if (expr
!= error_mark_node
5331 && cxx_dialect
< cxx17
5332 && !in_system_header_at (input_location
))
5333 pedwarn (input_location
, 0, "fold-expressions only available "
5334 "with -std=c++17 or -std=gnu++17");
5337 /* Let the front end know that this expression was
5338 enclosed in parentheses. This matters in case, for
5339 example, the expression is of the form `A::B', since
5340 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5342 expr
= finish_parenthesized_expr (expr
);
5344 /* DR 705: Wrapping an unqualified name in parentheses
5345 suppresses arg-dependent lookup. We want to pass back
5346 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5347 (c++/37862), but none of the others. */
5348 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5349 *idk
= CP_ID_KIND_NONE
;
5351 /* The `>' token might be the end of a template-id or
5352 template-parameter-list now. */
5353 parser
->greater_than_is_operator_p
5354 = saved_greater_than_is_operator_p
;
5356 /* Consume the `)'. */
5357 token
= cp_lexer_peek_token (parser
->lexer
);
5358 location_t close_paren_loc
= token
->location
;
5359 expr
.set_range (open_paren_loc
, close_paren_loc
);
5360 if (!parens
.require_close (parser
)
5361 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5362 cp_parser_skip_to_end_of_statement (parser
);
5367 case CPP_OPEN_SQUARE
:
5369 if (c_dialect_objc ())
5371 /* We might have an Objective-C++ message. */
5372 cp_parser_parse_tentatively (parser
);
5373 tree msg
= cp_parser_objc_message_expression (parser
);
5374 /* If that works out, we're done ... */
5375 if (cp_parser_parse_definitely (parser
))
5377 /* ... else, fall though to see if it's a lambda. */
5379 cp_expr lam
= cp_parser_lambda_expression (parser
);
5380 /* Don't warn about a failed tentative parse. */
5381 if (cp_parser_error_occurred (parser
))
5382 return error_mark_node
;
5383 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5387 case CPP_OBJC_STRING
:
5388 if (c_dialect_objc ())
5389 /* We have an Objective-C++ string literal. */
5390 return cp_parser_objc_expression (parser
);
5391 cp_parser_error (parser
, "expected primary-expression");
5392 return error_mark_node
;
5395 switch (token
->keyword
)
5397 /* These two are the boolean literals. */
5399 cp_lexer_consume_token (parser
->lexer
);
5400 return cp_expr (boolean_true_node
, token
->location
);
5402 cp_lexer_consume_token (parser
->lexer
);
5403 return cp_expr (boolean_false_node
, token
->location
);
5405 /* The `__null' literal. */
5407 cp_lexer_consume_token (parser
->lexer
);
5408 return cp_expr (null_node
, token
->location
);
5410 /* The `nullptr' literal. */
5412 cp_lexer_consume_token (parser
->lexer
);
5413 return cp_expr (nullptr_node
, token
->location
);
5415 /* Recognize the `this' keyword. */
5417 cp_lexer_consume_token (parser
->lexer
);
5418 if (parser
->local_variables_forbidden_p
& THIS_FORBIDDEN
)
5420 error_at (token
->location
,
5421 "%<this%> may not be used in this context");
5422 return error_mark_node
;
5424 /* Pointers cannot appear in constant-expressions. */
5425 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5426 return error_mark_node
;
5427 return cp_expr (finish_this_expr (), token
->location
);
5429 /* The `operator' keyword can be the beginning of an
5434 case RID_FUNCTION_NAME
:
5435 case RID_PRETTY_FUNCTION_NAME
:
5436 case RID_C99_FUNCTION_NAME
:
5438 non_integral_constant name
;
5440 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5441 __func__ are the names of variables -- but they are
5442 treated specially. Therefore, they are handled here,
5443 rather than relying on the generic id-expression logic
5444 below. Grammatically, these names are id-expressions.
5446 Consume the token. */
5447 token
= cp_lexer_consume_token (parser
->lexer
);
5449 switch (token
->keyword
)
5451 case RID_FUNCTION_NAME
:
5452 name
= NIC_FUNC_NAME
;
5454 case RID_PRETTY_FUNCTION_NAME
:
5455 name
= NIC_PRETTY_FUNC
;
5457 case RID_C99_FUNCTION_NAME
:
5458 name
= NIC_C99_FUNC
;
5464 if (cp_parser_non_integral_constant_expression (parser
, name
))
5465 return error_mark_node
;
5467 /* Look up the name. */
5468 return finish_fname (token
->u
.value
);
5475 location_t type_location
;
5476 location_t start_loc
5477 = cp_lexer_peek_token (parser
->lexer
)->location
;
5478 /* The `__builtin_va_arg' construct is used to handle
5479 `va_arg'. Consume the `__builtin_va_arg' token. */
5480 cp_lexer_consume_token (parser
->lexer
);
5481 /* Look for the opening `('. */
5482 matching_parens parens
;
5483 parens
.require_open (parser
);
5484 /* Now, parse the assignment-expression. */
5485 expression
= cp_parser_assignment_expression (parser
);
5486 /* Look for the `,'. */
5487 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5488 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5489 /* Parse the type-id. */
5491 type_id_in_expr_sentinel
s (parser
);
5492 type
= cp_parser_type_id (parser
);
5494 /* Look for the closing `)'. */
5495 location_t finish_loc
5496 = cp_lexer_peek_token (parser
->lexer
)->location
;
5497 parens
.require_close (parser
);
5498 /* Using `va_arg' in a constant-expression is not
5500 if (cp_parser_non_integral_constant_expression (parser
,
5502 return error_mark_node
;
5503 /* Construct a location of the form:
5504 __builtin_va_arg (v, int)
5505 ~~~~~~~~~~~~~~~~~~~~~^~~~
5506 with the caret at the type, ranging from the start of the
5507 "__builtin_va_arg" token to the close paren. */
5508 location_t combined_loc
5509 = make_location (type_location
, start_loc
, finish_loc
);
5510 return build_x_va_arg (combined_loc
, expression
, type
);
5514 return cp_parser_builtin_offsetof (parser
);
5516 case RID_HAS_NOTHROW_ASSIGN
:
5517 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5518 case RID_HAS_NOTHROW_COPY
:
5519 case RID_HAS_TRIVIAL_ASSIGN
:
5520 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5521 case RID_HAS_TRIVIAL_COPY
:
5522 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5523 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5524 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5525 case RID_IS_ABSTRACT
:
5526 case RID_IS_AGGREGATE
:
5527 case RID_IS_BASE_OF
:
5532 case RID_IS_LITERAL_TYPE
:
5534 case RID_IS_POLYMORPHIC
:
5535 case RID_IS_SAME_AS
:
5536 case RID_IS_STD_LAYOUT
:
5537 case RID_IS_TRIVIAL
:
5538 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5539 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5540 case RID_IS_TRIVIALLY_COPYABLE
:
5542 case RID_IS_ASSIGNABLE
:
5543 case RID_IS_CONSTRUCTIBLE
:
5544 return cp_parser_trait_expr (parser
, token
->keyword
);
5548 return cp_parser_requires_expression (parser
);
5550 /* Objective-C++ expressions. */
5552 case RID_AT_PROTOCOL
:
5553 case RID_AT_SELECTOR
:
5554 return cp_parser_objc_expression (parser
);
5557 if (parser
->in_function_body
5558 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5561 error_at (token
->location
,
5562 "a template declaration cannot appear at block scope");
5563 cp_parser_skip_to_end_of_block_or_statement (parser
);
5564 return error_mark_node
;
5568 cp_parser_error (parser
, "expected primary-expression");
5569 return error_mark_node
;
5572 /* An id-expression can start with either an identifier, a
5573 `::' as the beginning of a qualified-id, or the "operator"
5577 case CPP_TEMPLATE_ID
:
5578 case CPP_NESTED_NAME_SPECIFIER
:
5581 cp_expr id_expression
;
5583 const char *error_msg
;
5586 cp_token
*id_expr_token
;
5588 /* Parse the id-expression. */
5590 = cp_parser_id_expression (parser
,
5591 /*template_keyword_p=*/false,
5592 /*check_dependency_p=*/true,
5594 /*declarator_p=*/false,
5595 /*optional_p=*/false);
5596 if (id_expression
== error_mark_node
)
5597 return error_mark_node
;
5598 id_expr_token
= token
;
5599 token
= cp_lexer_peek_token (parser
->lexer
);
5600 done
= (token
->type
!= CPP_OPEN_SQUARE
5601 && token
->type
!= CPP_OPEN_PAREN
5602 && token
->type
!= CPP_DOT
5603 && token
->type
!= CPP_DEREF
5604 && token
->type
!= CPP_PLUS_PLUS
5605 && token
->type
!= CPP_MINUS_MINUS
);
5606 /* If we have a template-id, then no further lookup is
5607 required. If the template-id was for a template-class, we
5608 will sometimes have a TYPE_DECL at this point. */
5609 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5610 || TREE_CODE (id_expression
) == TYPE_DECL
)
5611 decl
= id_expression
;
5612 /* Look up the name. */
5615 tree ambiguous_decls
;
5617 /* If we already know that this lookup is ambiguous, then
5618 we've already issued an error message; there's no reason
5620 if (id_expr_token
->type
== CPP_NAME
5621 && id_expr_token
->error_reported
)
5623 cp_parser_simulate_error (parser
);
5624 return error_mark_node
;
5627 decl
= cp_parser_lookup_name (parser
, id_expression
,
5630 /*is_namespace=*/false,
5631 /*check_dependency=*/true,
5633 id_expression
.get_location ());
5634 /* If the lookup was ambiguous, an error will already have
5636 if (ambiguous_decls
)
5637 return error_mark_node
;
5639 /* In Objective-C++, we may have an Objective-C 2.0
5640 dot-syntax for classes here. */
5641 if (c_dialect_objc ()
5642 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5643 && TREE_CODE (decl
) == TYPE_DECL
5644 && objc_is_class_name (decl
))
5647 cp_lexer_consume_token (parser
->lexer
);
5648 component
= cp_parser_identifier (parser
);
5649 if (component
== error_mark_node
)
5650 return error_mark_node
;
5652 tree result
= objc_build_class_component_ref (id_expression
,
5654 /* Build a location of the form:
5657 with caret at the start of the component name (at
5658 input_location), ranging from the start of the id_expression
5659 to the end of the component name. */
5660 location_t combined_loc
5661 = make_location (input_location
, id_expression
.get_start (),
5662 get_finish (input_location
));
5663 protected_set_expr_location (result
, combined_loc
);
5667 /* In Objective-C++, an instance variable (ivar) may be preferred
5668 to whatever cp_parser_lookup_name() found.
5669 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5670 rest of c-family, we have to do a little extra work to preserve
5671 any location information in cp_expr "decl". Given that
5672 objc_lookup_ivar is implemented in "c-family" and "objc", we
5673 have a trip through the pure "tree" type, rather than cp_expr.
5674 Naively copying it back to "decl" would implicitly give the
5675 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5676 store an EXPR_LOCATION. Hence we only update "decl" (and
5677 hence its location_t) if we get back a different tree node. */
5678 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5680 if (decl_tree
!= decl
.get_value ())
5681 decl
= cp_expr (decl_tree
);
5683 /* If name lookup gives us a SCOPE_REF, then the
5684 qualifying scope was dependent. */
5685 if (TREE_CODE (decl
) == SCOPE_REF
)
5687 /* At this point, we do not know if DECL is a valid
5688 integral constant expression. We assume that it is
5689 in fact such an expression, so that code like:
5691 template <int N> struct A {
5695 is accepted. At template-instantiation time, we
5696 will check that B<N>::i is actually a constant. */
5699 /* Check to see if DECL is a local variable in a context
5700 where that is forbidden. */
5701 if ((parser
->local_variables_forbidden_p
& LOCAL_VARS_FORBIDDEN
)
5702 && local_variable_p (decl
))
5704 error_at (id_expression
.get_location (),
5705 "local variable %qD may not appear in this context",
5707 return error_mark_node
;
5711 decl
= (finish_id_expression
5712 (id_expression
, decl
, parser
->scope
,
5714 parser
->integral_constant_expression_p
,
5715 parser
->allow_non_integral_constant_expression_p
,
5716 &parser
->non_integral_constant_expression_p
,
5717 template_p
, done
, address_p
,
5720 id_expression
.get_location ()));
5722 cp_parser_error (parser
, error_msg
);
5723 /* Build a location for an id-expression of the form:
5729 i.e. from the start of the first token to the end of the final
5730 token, with the caret at the start of the unqualified-id. */
5731 location_t caret_loc
= get_pure_location (id_expression
.get_location ());
5732 location_t start_loc
= get_start (id_expr_token
->location
);
5733 location_t finish_loc
= get_finish (id_expression
.get_location ());
5734 location_t combined_loc
5735 = make_location (caret_loc
, start_loc
, finish_loc
);
5737 decl
.set_location (combined_loc
);
5741 /* Anything else is an error. */
5743 cp_parser_error (parser
, "expected primary-expression");
5744 return error_mark_node
;
5748 static inline cp_expr
5749 cp_parser_primary_expression (cp_parser
*parser
,
5752 bool template_arg_p
,
5755 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5756 /*decltype*/false, idk
);
5759 /* Parse an id-expression.
5766 :: [opt] nested-name-specifier template [opt] unqualified-id
5768 :: operator-function-id
5771 Return a representation of the unqualified portion of the
5772 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5773 a `::' or nested-name-specifier.
5775 Often, if the id-expression was a qualified-id, the caller will
5776 want to make a SCOPE_REF to represent the qualified-id. This
5777 function does not do this in order to avoid wastefully creating
5778 SCOPE_REFs when they are not required.
5780 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5783 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5784 uninstantiated templates.
5786 If *TEMPLATE_P is non-NULL, it is set to true iff the
5787 `template' keyword is used to explicitly indicate that the entity
5788 named is a template.
5790 If DECLARATOR_P is true, the id-expression is appearing as part of
5791 a declarator, rather than as part of an expression. */
5794 cp_parser_id_expression (cp_parser
*parser
,
5795 bool template_keyword_p
,
5796 bool check_dependency_p
,
5801 bool global_scope_p
;
5802 bool nested_name_specifier_p
;
5804 /* Assume the `template' keyword was not used. */
5806 *template_p
= template_keyword_p
;
5808 /* Look for the optional `::' operator. */
5810 = (!template_keyword_p
5811 && (cp_parser_global_scope_opt (parser
,
5812 /*current_scope_valid_p=*/false)
5815 /* Look for the optional nested-name-specifier. */
5816 nested_name_specifier_p
5817 = (cp_parser_nested_name_specifier_opt (parser
,
5818 /*typename_keyword_p=*/false,
5825 /* If there is a nested-name-specifier, then we are looking at
5826 the first qualified-id production. */
5827 if (nested_name_specifier_p
)
5830 tree saved_object_scope
;
5831 tree saved_qualifying_scope
;
5832 cp_expr unqualified_id
;
5835 /* See if the next token is the `template' keyword. */
5837 template_p
= &is_template
;
5838 *template_p
= cp_parser_optional_template_keyword (parser
);
5839 /* Name lookup we do during the processing of the
5840 unqualified-id might obliterate SCOPE. */
5841 saved_scope
= parser
->scope
;
5842 saved_object_scope
= parser
->object_scope
;
5843 saved_qualifying_scope
= parser
->qualifying_scope
;
5844 /* Process the final unqualified-id. */
5845 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5848 /*optional_p=*/false);
5849 /* Restore the SAVED_SCOPE for our caller. */
5850 parser
->scope
= saved_scope
;
5851 parser
->object_scope
= saved_object_scope
;
5852 parser
->qualifying_scope
= saved_qualifying_scope
;
5854 return unqualified_id
;
5856 /* Otherwise, if we are in global scope, then we are looking at one
5857 of the other qualified-id productions. */
5858 else if (global_scope_p
)
5863 /* Peek at the next token. */
5864 token
= cp_lexer_peek_token (parser
->lexer
);
5866 /* If it's an identifier, and the next token is not a "<", then
5867 we can avoid the template-id case. This is an optimization
5868 for this common case. */
5869 if (token
->type
== CPP_NAME
5870 && !cp_parser_nth_token_starts_template_argument_list_p
5872 return cp_parser_identifier (parser
);
5874 cp_parser_parse_tentatively (parser
);
5875 /* Try a template-id. */
5876 id
= cp_parser_template_id (parser
,
5877 /*template_keyword_p=*/false,
5878 /*check_dependency_p=*/true,
5881 /* If that worked, we're done. */
5882 if (cp_parser_parse_definitely (parser
))
5885 /* Peek at the next token. (Changes in the token buffer may
5886 have invalidated the pointer obtained above.) */
5887 token
= cp_lexer_peek_token (parser
->lexer
);
5889 switch (token
->type
)
5892 return cp_parser_identifier (parser
);
5895 if (token
->keyword
== RID_OPERATOR
)
5896 return cp_parser_operator_function_id (parser
);
5900 cp_parser_error (parser
, "expected id-expression");
5901 return error_mark_node
;
5905 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5906 /*check_dependency_p=*/true,
5911 /* Parse an unqualified-id.
5915 operator-function-id
5916 conversion-function-id
5920 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5921 keyword, in a construct like `A::template ...'.
5923 Returns a representation of unqualified-id. For the `identifier'
5924 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5925 production a BIT_NOT_EXPR is returned; the operand of the
5926 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5927 other productions, see the documentation accompanying the
5928 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5929 names are looked up in uninstantiated templates. If DECLARATOR_P
5930 is true, the unqualified-id is appearing as part of a declarator,
5931 rather than as part of an expression. */
5934 cp_parser_unqualified_id (cp_parser
* parser
,
5935 bool template_keyword_p
,
5936 bool check_dependency_p
,
5942 /* Peek at the next token. */
5943 token
= cp_lexer_peek_token (parser
->lexer
);
5945 switch ((int) token
->type
)
5951 /* We don't know yet whether or not this will be a
5953 cp_parser_parse_tentatively (parser
);
5954 /* Try a template-id. */
5955 id
= cp_parser_template_id (parser
, template_keyword_p
,
5959 /* If it worked, we're done. */
5960 if (cp_parser_parse_definitely (parser
))
5962 /* Otherwise, it's an ordinary identifier. */
5963 return cp_parser_identifier (parser
);
5966 case CPP_TEMPLATE_ID
:
5967 return cp_parser_template_id (parser
, template_keyword_p
,
5975 tree qualifying_scope
;
5980 /* Consume the `~' token. */
5981 cp_lexer_consume_token (parser
->lexer
);
5982 /* Parse the class-name. The standard, as written, seems to
5985 template <typename T> struct S { ~S (); };
5986 template <typename T> S<T>::~S() {}
5988 is invalid, since `~' must be followed by a class-name, but
5989 `S<T>' is dependent, and so not known to be a class.
5990 That's not right; we need to look in uninstantiated
5991 templates. A further complication arises from:
5993 template <typename T> void f(T t) {
5997 Here, it is not possible to look up `T' in the scope of `T'
5998 itself. We must look in both the current scope, and the
5999 scope of the containing complete expression.
6001 Yet another issue is:
6010 The standard does not seem to say that the `S' in `~S'
6011 should refer to the type `S' and not the data member
6014 /* DR 244 says that we look up the name after the "~" in the
6015 same scope as we looked up the qualifying name. That idea
6016 isn't fully worked out; it's more complicated than that. */
6017 scope
= parser
->scope
;
6018 object_scope
= parser
->object_scope
;
6019 qualifying_scope
= parser
->qualifying_scope
;
6021 /* Check for invalid scopes. */
6022 if (scope
== error_mark_node
)
6024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6025 cp_lexer_consume_token (parser
->lexer
);
6026 return error_mark_node
;
6028 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
6030 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6031 error_at (token
->location
,
6032 "scope %qT before %<~%> is not a class-name",
6034 cp_parser_simulate_error (parser
);
6035 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6036 cp_lexer_consume_token (parser
->lexer
);
6037 return error_mark_node
;
6039 gcc_assert (!scope
|| TYPE_P (scope
));
6041 /* If the name is of the form "X::~X" it's OK even if X is a
6043 token
= cp_lexer_peek_token (parser
->lexer
);
6045 && token
->type
== CPP_NAME
6046 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6048 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6049 || (CLASS_TYPE_P (scope
)
6050 && constructor_name_p (token
->u
.value
, scope
))))
6052 cp_lexer_consume_token (parser
->lexer
);
6053 return build_nt (BIT_NOT_EXPR
, scope
);
6056 /* ~auto means the destructor of whatever the object is. */
6057 if (cp_parser_is_keyword (token
, RID_AUTO
))
6059 if (cxx_dialect
< cxx14
)
6060 pedwarn (input_location
, 0,
6061 "%<~auto%> only available with "
6062 "-std=c++14 or -std=gnu++14");
6063 cp_lexer_consume_token (parser
->lexer
);
6064 return build_nt (BIT_NOT_EXPR
, make_auto ());
6067 /* If there was an explicit qualification (S::~T), first look
6068 in the scope given by the qualification (i.e., S).
6070 Note: in the calls to cp_parser_class_name below we pass
6071 typename_type so that lookup finds the injected-class-name
6072 rather than the constructor. */
6074 type_decl
= NULL_TREE
;
6077 cp_parser_parse_tentatively (parser
);
6078 type_decl
= cp_parser_class_name (parser
,
6079 /*typename_keyword_p=*/false,
6080 /*template_keyword_p=*/false,
6082 /*check_dependency=*/false,
6083 /*class_head_p=*/false,
6085 if (cp_parser_parse_definitely (parser
))
6088 /* In "N::S::~S", look in "N" as well. */
6089 if (!done
&& scope
&& qualifying_scope
)
6091 cp_parser_parse_tentatively (parser
);
6092 parser
->scope
= qualifying_scope
;
6093 parser
->object_scope
= NULL_TREE
;
6094 parser
->qualifying_scope
= NULL_TREE
;
6096 = cp_parser_class_name (parser
,
6097 /*typename_keyword_p=*/false,
6098 /*template_keyword_p=*/false,
6100 /*check_dependency=*/false,
6101 /*class_head_p=*/false,
6103 if (cp_parser_parse_definitely (parser
))
6106 /* In "p->S::~T", look in the scope given by "*p" as well. */
6107 else if (!done
&& object_scope
)
6109 cp_parser_parse_tentatively (parser
);
6110 parser
->scope
= object_scope
;
6111 parser
->object_scope
= NULL_TREE
;
6112 parser
->qualifying_scope
= NULL_TREE
;
6114 = cp_parser_class_name (parser
,
6115 /*typename_keyword_p=*/false,
6116 /*template_keyword_p=*/false,
6118 /*check_dependency=*/false,
6119 /*class_head_p=*/false,
6121 if (cp_parser_parse_definitely (parser
))
6124 /* Look in the surrounding context. */
6127 parser
->scope
= NULL_TREE
;
6128 parser
->object_scope
= NULL_TREE
;
6129 parser
->qualifying_scope
= NULL_TREE
;
6130 if (processing_template_decl
)
6131 cp_parser_parse_tentatively (parser
);
6133 = cp_parser_class_name (parser
,
6134 /*typename_keyword_p=*/false,
6135 /*template_keyword_p=*/false,
6137 /*check_dependency=*/false,
6138 /*class_head_p=*/false,
6140 if (processing_template_decl
6141 && ! cp_parser_parse_definitely (parser
))
6143 /* We couldn't find a type with this name. If we're parsing
6144 tentatively, fail and try something else. */
6145 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6147 cp_parser_simulate_error (parser
);
6148 return error_mark_node
;
6150 /* Otherwise, accept it and check for a match at instantiation
6152 type_decl
= cp_parser_identifier (parser
);
6153 if (type_decl
!= error_mark_node
)
6154 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6158 /* If an error occurred, assume that the name of the
6159 destructor is the same as the name of the qualifying
6160 class. That allows us to keep parsing after running
6161 into ill-formed destructor names. */
6162 if (type_decl
== error_mark_node
&& scope
)
6163 return build_nt (BIT_NOT_EXPR
, scope
);
6164 else if (type_decl
== error_mark_node
)
6165 return error_mark_node
;
6167 /* Check that destructor name and scope match. */
6168 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6170 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6171 error_at (token
->location
,
6172 "declaration of %<~%T%> as member of %qT",
6174 cp_parser_simulate_error (parser
);
6175 return error_mark_node
;
6180 A typedef-name that names a class shall not be used as the
6181 identifier in the declarator for a destructor declaration. */
6183 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6184 && !DECL_SELF_REFERENCE_P (type_decl
)
6185 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6186 error_at (token
->location
,
6187 "typedef-name %qD used as destructor declarator",
6190 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6194 if (token
->keyword
== RID_OPERATOR
)
6198 /* This could be a template-id, so we try that first. */
6199 cp_parser_parse_tentatively (parser
);
6200 /* Try a template-id. */
6201 id
= cp_parser_template_id (parser
, template_keyword_p
,
6202 /*check_dependency_p=*/true,
6205 /* If that worked, we're done. */
6206 if (cp_parser_parse_definitely (parser
))
6208 /* We still don't know whether we're looking at an
6209 operator-function-id or a conversion-function-id. */
6210 cp_parser_parse_tentatively (parser
);
6211 /* Try an operator-function-id. */
6212 id
= cp_parser_operator_function_id (parser
);
6213 /* If that didn't work, try a conversion-function-id. */
6214 if (!cp_parser_parse_definitely (parser
))
6215 id
= cp_parser_conversion_function_id (parser
);
6224 cp_parser_error (parser
, "expected unqualified-id");
6225 return error_mark_node
;
6229 /* Parse an (optional) nested-name-specifier.
6231 nested-name-specifier: [C++98]
6232 class-or-namespace-name :: nested-name-specifier [opt]
6233 class-or-namespace-name :: template nested-name-specifier [opt]
6235 nested-name-specifier: [C++0x]
6238 nested-name-specifier identifier ::
6239 nested-name-specifier template [opt] simple-template-id ::
6241 PARSER->SCOPE should be set appropriately before this function is
6242 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6243 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6246 Sets PARSER->SCOPE to the class (TYPE) or namespace
6247 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6248 it unchanged if there is no nested-name-specifier. Returns the new
6249 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6251 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6252 part of a declaration and/or decl-specifier. */
6255 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6256 bool typename_keyword_p
,
6257 bool check_dependency_p
,
6259 bool is_declaration
,
6260 bool template_keyword_p
/* = false */)
6262 bool success
= false;
6263 cp_token_position start
= 0;
6266 /* Remember where the nested-name-specifier starts. */
6267 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6269 start
= cp_lexer_token_position (parser
->lexer
, false);
6270 push_deferring_access_checks (dk_deferred
);
6277 tree saved_qualifying_scope
;
6279 /* Spot cases that cannot be the beginning of a
6280 nested-name-specifier. */
6281 token
= cp_lexer_peek_token (parser
->lexer
);
6283 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6284 the already parsed nested-name-specifier. */
6285 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6287 /* Grab the nested-name-specifier and continue the loop. */
6288 cp_parser_pre_parsed_nested_name_specifier (parser
);
6289 /* If we originally encountered this nested-name-specifier
6290 with IS_DECLARATION set to false, we will not have
6291 resolved TYPENAME_TYPEs, so we must do so here. */
6293 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6295 new_scope
= resolve_typename_type (parser
->scope
,
6296 /*only_current_p=*/false);
6297 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6298 parser
->scope
= new_scope
;
6304 /* Spot cases that cannot be the beginning of a
6305 nested-name-specifier. On the second and subsequent times
6306 through the loop, we look for the `template' keyword. */
6307 if (success
&& token
->keyword
== RID_TEMPLATE
)
6309 /* A template-id can start a nested-name-specifier. */
6310 else if (token
->type
== CPP_TEMPLATE_ID
)
6312 /* DR 743: decltype can be used in a nested-name-specifier. */
6313 else if (token_is_decltype (token
))
6317 /* If the next token is not an identifier, then it is
6318 definitely not a type-name or namespace-name. */
6319 if (token
->type
!= CPP_NAME
)
6321 /* If the following token is neither a `<' (to begin a
6322 template-id), nor a `::', then we are not looking at a
6323 nested-name-specifier. */
6324 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6326 if (token
->type
== CPP_COLON
6327 && parser
->colon_corrects_to_scope_p
6328 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6330 gcc_rich_location
richloc (token
->location
);
6331 richloc
.add_fixit_replace ("::");
6333 "found %<:%> in nested-name-specifier, "
6335 token
->type
= CPP_SCOPE
;
6338 if (token
->type
!= CPP_SCOPE
6339 && !cp_parser_nth_token_starts_template_argument_list_p
6344 /* The nested-name-specifier is optional, so we parse
6346 cp_parser_parse_tentatively (parser
);
6348 /* Look for the optional `template' keyword, if this isn't the
6349 first time through the loop. */
6351 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6353 /* Save the old scope since the name lookup we are about to do
6354 might destroy it. */
6355 old_scope
= parser
->scope
;
6356 saved_qualifying_scope
= parser
->qualifying_scope
;
6357 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6358 look up names in "X<T>::I" in order to determine that "Y" is
6359 a template. So, if we have a typename at this point, we make
6360 an effort to look through it. */
6362 && !typename_keyword_p
6364 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6365 parser
->scope
= resolve_typename_type (parser
->scope
,
6366 /*only_current_p=*/false);
6367 /* Parse the qualifying entity. */
6369 = cp_parser_qualifying_entity (parser
,
6375 /* Look for the `::' token. */
6376 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6378 /* If we found what we wanted, we keep going; otherwise, we're
6380 if (!cp_parser_parse_definitely (parser
))
6382 bool error_p
= false;
6384 /* Restore the OLD_SCOPE since it was valid before the
6385 failed attempt at finding the last
6386 class-or-namespace-name. */
6387 parser
->scope
= old_scope
;
6388 parser
->qualifying_scope
= saved_qualifying_scope
;
6390 /* If the next token is a decltype, and the one after that is a
6391 `::', then the decltype has failed to resolve to a class or
6392 enumeration type. Give this error even when parsing
6393 tentatively since it can't possibly be valid--and we're going
6394 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6395 won't get another chance.*/
6396 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6397 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6400 token
= cp_lexer_consume_token (parser
->lexer
);
6401 error_at (token
->location
, "decltype evaluates to %qT, "
6402 "which is not a class or enumeration type",
6403 token
->u
.tree_check_value
->value
);
6404 parser
->scope
= error_mark_node
;
6408 cp_lexer_consume_token (parser
->lexer
);
6411 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6412 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6414 /* If we have a non-type template-id followed by ::, it can't
6415 possibly be valid. */
6416 token
= cp_lexer_peek_token (parser
->lexer
);
6417 tree tid
= token
->u
.tree_check_value
->value
;
6418 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6419 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6421 tree tmpl
= NULL_TREE
;
6422 if (is_overloaded_fn (tid
))
6424 tree fns
= get_fns (tid
);
6425 if (OVL_SINGLE_P (fns
))
6426 tmpl
= OVL_FIRST (fns
);
6427 error_at (token
->location
, "function template-id %qD "
6428 "in nested-name-specifier", tid
);
6432 /* Variable template. */
6433 tmpl
= TREE_OPERAND (tid
, 0);
6434 gcc_assert (variable_template_p (tmpl
));
6435 error_at (token
->location
, "variable template-id %qD "
6436 "in nested-name-specifier", tid
);
6439 inform (DECL_SOURCE_LOCATION (tmpl
),
6440 "%qD declared here", tmpl
);
6442 parser
->scope
= error_mark_node
;
6446 cp_lexer_consume_token (parser
->lexer
);
6447 cp_lexer_consume_token (parser
->lexer
);
6451 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6453 /* If the next token is an identifier, and the one after
6454 that is a `::', then any valid interpretation would have
6455 found a class-or-namespace-name. */
6456 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6457 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6459 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6462 token
= cp_lexer_consume_token (parser
->lexer
);
6465 if (!token
->error_reported
)
6468 tree ambiguous_decls
;
6470 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6472 /*is_template=*/false,
6473 /*is_namespace=*/false,
6474 /*check_dependency=*/true,
6477 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6478 error_at (token
->location
,
6479 "%qD used without template arguments",
6481 else if (ambiguous_decls
)
6483 // cp_parser_lookup_name has the same diagnostic,
6484 // thus make sure to emit it at most once.
6485 if (cp_parser_uncommitted_to_tentative_parse_p
6488 error_at (token
->location
,
6489 "reference to %qD is ambiguous",
6491 print_candidates (ambiguous_decls
);
6493 decl
= error_mark_node
;
6497 if (cxx_dialect
!= cxx98
)
6498 cp_parser_name_lookup_error
6499 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6502 cp_parser_name_lookup_error
6503 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6507 parser
->scope
= error_mark_node
;
6509 /* Treat this as a successful nested-name-specifier
6514 If the name found is not a class-name (clause
6515 _class_) or namespace-name (_namespace.def_), the
6516 program is ill-formed. */
6519 cp_lexer_consume_token (parser
->lexer
);
6523 /* We've found one valid nested-name-specifier. */
6525 /* Name lookup always gives us a DECL. */
6526 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6527 new_scope
= TREE_TYPE (new_scope
);
6528 /* Uses of "template" must be followed by actual templates. */
6529 if (template_keyword_p
6530 && !(CLASS_TYPE_P (new_scope
)
6531 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6532 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6533 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6534 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6535 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6536 == TEMPLATE_ID_EXPR
)))
6537 permerror (input_location
, TYPE_P (new_scope
)
6538 ? G_("%qT is not a template")
6539 : G_("%qD is not a template"),
6541 /* If it is a class scope, try to complete it; we are about to
6542 be looking up names inside the class. */
6543 if (TYPE_P (new_scope
)
6544 /* Since checking types for dependency can be expensive,
6545 avoid doing it if the type is already complete. */
6546 && !COMPLETE_TYPE_P (new_scope
)
6547 /* Do not try to complete dependent types. */
6548 && !dependent_type_p (new_scope
))
6550 new_scope
= complete_type (new_scope
);
6551 /* If it is a typedef to current class, use the current
6552 class instead, as the typedef won't have any names inside
6554 if (!COMPLETE_TYPE_P (new_scope
)
6555 && currently_open_class (new_scope
))
6556 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6558 /* Make sure we look in the right scope the next time through
6560 parser
->scope
= new_scope
;
6563 /* If parsing tentatively, replace the sequence of tokens that makes
6564 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6565 token. That way, should we re-parse the token stream, we will
6566 not have to repeat the effort required to do the parse, nor will
6567 we issue duplicate error messages. */
6568 if (success
&& start
)
6572 token
= cp_lexer_token_at (parser
->lexer
, start
);
6573 /* Reset the contents of the START token. */
6574 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6575 /* Retrieve any deferred checks. Do not pop this access checks yet
6576 so the memory will not be reclaimed during token replacing below. */
6577 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6578 token
->u
.tree_check_value
->value
= parser
->scope
;
6579 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6580 token
->u
.tree_check_value
->qualifying_scope
=
6581 parser
->qualifying_scope
;
6582 token
->keyword
= RID_MAX
;
6584 /* Purge all subsequent tokens. */
6585 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6589 pop_to_parent_deferring_access_checks ();
6591 return success
? parser
->scope
: NULL_TREE
;
6594 /* Parse a nested-name-specifier. See
6595 cp_parser_nested_name_specifier_opt for details. This function
6596 behaves identically, except that it will an issue an error if no
6597 nested-name-specifier is present. */
6600 cp_parser_nested_name_specifier (cp_parser
*parser
,
6601 bool typename_keyword_p
,
6602 bool check_dependency_p
,
6604 bool is_declaration
)
6608 /* Look for the nested-name-specifier. */
6609 scope
= cp_parser_nested_name_specifier_opt (parser
,
6614 /* If it was not present, issue an error message. */
6617 cp_parser_error (parser
, "expected nested-name-specifier");
6618 parser
->scope
= NULL_TREE
;
6624 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6625 this is either a class-name or a namespace-name (which corresponds
6626 to the class-or-namespace-name production in the grammar). For
6627 C++0x, it can also be a type-name that refers to an enumeration
6628 type or a simple-template-id.
6630 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6631 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6632 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6633 TYPE_P is TRUE iff the next name should be taken as a class-name,
6634 even the same name is declared to be another entity in the same
6637 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6638 specified by the class-or-namespace-name. If neither is found the
6639 ERROR_MARK_NODE is returned. */
6642 cp_parser_qualifying_entity (cp_parser
*parser
,
6643 bool typename_keyword_p
,
6644 bool template_keyword_p
,
6645 bool check_dependency_p
,
6647 bool is_declaration
)
6650 tree saved_qualifying_scope
;
6651 tree saved_object_scope
;
6654 bool successful_parse_p
;
6656 /* DR 743: decltype can appear in a nested-name-specifier. */
6657 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6659 scope
= cp_parser_decltype (parser
);
6660 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6661 && !MAYBE_CLASS_TYPE_P (scope
))
6663 cp_parser_simulate_error (parser
);
6664 return error_mark_node
;
6666 if (TYPE_NAME (scope
))
6667 scope
= TYPE_NAME (scope
);
6671 /* Before we try to parse the class-name, we must save away the
6672 current PARSER->SCOPE since cp_parser_class_name will destroy
6674 saved_scope
= parser
->scope
;
6675 saved_qualifying_scope
= parser
->qualifying_scope
;
6676 saved_object_scope
= parser
->object_scope
;
6677 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6678 there is no need to look for a namespace-name. */
6679 only_class_p
= template_keyword_p
6680 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6682 cp_parser_parse_tentatively (parser
);
6683 scope
= cp_parser_class_name (parser
,
6686 type_p
? class_type
: none_type
,
6688 /*class_head_p=*/false,
6690 /*enum_ok=*/cxx_dialect
> cxx98
);
6691 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6692 /* If that didn't work, try for a namespace-name. */
6693 if (!only_class_p
&& !successful_parse_p
)
6695 /* Restore the saved scope. */
6696 parser
->scope
= saved_scope
;
6697 parser
->qualifying_scope
= saved_qualifying_scope
;
6698 parser
->object_scope
= saved_object_scope
;
6699 /* If we are not looking at an identifier followed by the scope
6700 resolution operator, then this is not part of a
6701 nested-name-specifier. (Note that this function is only used
6702 to parse the components of a nested-name-specifier.) */
6703 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6704 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6705 return error_mark_node
;
6706 scope
= cp_parser_namespace_name (parser
);
6712 /* Return true if we are looking at a compound-literal, false otherwise. */
6715 cp_parser_compound_literal_p (cp_parser
*parser
)
6717 cp_lexer_save_tokens (parser
->lexer
);
6719 /* Skip tokens until the next token is a closing parenthesis.
6720 If we find the closing `)', and the next token is a `{', then
6721 we are looking at a compound-literal. */
6722 bool compound_literal_p
6723 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6724 /*consume_paren=*/true)
6725 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6727 /* Roll back the tokens we skipped. */
6728 cp_lexer_rollback_tokens (parser
->lexer
);
6730 return compound_literal_p
;
6733 /* Return true if EXPR is the integer constant zero or a complex constant
6734 of zero, without any folding, but ignoring location wrappers. */
6737 literal_integer_zerop (const_tree expr
)
6739 return (location_wrapper_p (expr
)
6740 && integer_zerop (TREE_OPERAND (expr
, 0)));
6743 /* Parse a postfix-expression.
6747 postfix-expression [ expression ]
6748 postfix-expression ( expression-list [opt] )
6749 simple-type-specifier ( expression-list [opt] )
6750 typename :: [opt] nested-name-specifier identifier
6751 ( expression-list [opt] )
6752 typename :: [opt] nested-name-specifier template [opt] template-id
6753 ( expression-list [opt] )
6754 postfix-expression . template [opt] id-expression
6755 postfix-expression -> template [opt] id-expression
6756 postfix-expression . pseudo-destructor-name
6757 postfix-expression -> pseudo-destructor-name
6758 postfix-expression ++
6759 postfix-expression --
6760 dynamic_cast < type-id > ( expression )
6761 static_cast < type-id > ( expression )
6762 reinterpret_cast < type-id > ( expression )
6763 const_cast < type-id > ( expression )
6764 typeid ( expression )
6770 ( type-id ) { initializer-list , [opt] }
6772 This extension is a GNU version of the C99 compound-literal
6773 construct. (The C99 grammar uses `type-name' instead of `type-id',
6774 but they are essentially the same concept.)
6776 If ADDRESS_P is true, the postfix expression is the operand of the
6777 `&' operator. CAST_P is true if this expression is the target of a
6780 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6781 class member access expressions [expr.ref].
6783 Returns a representation of the expression. */
6786 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6787 bool member_access_only_p
, bool decltype_p
,
6788 cp_id_kind
* pidk_return
)
6793 cp_id_kind idk
= CP_ID_KIND_NONE
;
6794 cp_expr postfix_expression
= NULL_TREE
;
6795 bool is_member_access
= false;
6797 /* Peek at the next token. */
6798 token
= cp_lexer_peek_token (parser
->lexer
);
6799 loc
= token
->location
;
6800 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6802 /* Some of the productions are determined by keywords. */
6803 keyword
= token
->keyword
;
6813 const char *saved_message
;
6814 bool saved_in_type_id_in_expr_p
;
6816 /* All of these can be handled in the same way from the point
6817 of view of parsing. Begin by consuming the token
6818 identifying the cast. */
6819 cp_lexer_consume_token (parser
->lexer
);
6821 /* New types cannot be defined in the cast. */
6822 saved_message
= parser
->type_definition_forbidden_message
;
6823 parser
->type_definition_forbidden_message
6824 = G_("types may not be defined in casts");
6826 /* Look for the opening `<'. */
6827 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6828 /* Parse the type to which we are casting. */
6829 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6830 parser
->in_type_id_in_expr_p
= true;
6831 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
6833 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6834 /* Look for the closing `>'. */
6835 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6836 /* Restore the old message. */
6837 parser
->type_definition_forbidden_message
= saved_message
;
6839 bool saved_greater_than_is_operator_p
6840 = parser
->greater_than_is_operator_p
;
6841 parser
->greater_than_is_operator_p
= true;
6843 /* And the expression which is being cast. */
6844 matching_parens parens
;
6845 parens
.require_open (parser
);
6846 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6847 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6849 location_t end_loc
= close_paren
?
6850 close_paren
->location
: UNKNOWN_LOCATION
;
6852 parser
->greater_than_is_operator_p
6853 = saved_greater_than_is_operator_p
;
6855 /* Only type conversions to integral or enumeration types
6856 can be used in constant-expressions. */
6857 if (!cast_valid_in_integral_constant_expression_p (type
)
6858 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6860 postfix_expression
= error_mark_node
;
6868 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6872 = build_static_cast (type
, expression
, tf_warning_or_error
);
6876 = build_reinterpret_cast (type
, expression
,
6877 tf_warning_or_error
);
6881 = build_const_cast (type
, expression
, tf_warning_or_error
);
6887 /* Construct a location e.g. :
6888 reinterpret_cast <int *> (expr)
6889 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6890 ranging from the start of the "*_cast" token to the final closing
6891 paren, with the caret at the start. */
6892 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6893 postfix_expression
.set_location (cp_cast_loc
);
6900 const char *saved_message
;
6901 bool saved_in_type_id_in_expr_p
;
6903 /* Consume the `typeid' token. */
6904 cp_lexer_consume_token (parser
->lexer
);
6905 /* Look for the `(' token. */
6906 matching_parens parens
;
6907 parens
.require_open (parser
);
6908 /* Types cannot be defined in a `typeid' expression. */
6909 saved_message
= parser
->type_definition_forbidden_message
;
6910 parser
->type_definition_forbidden_message
6911 = G_("types may not be defined in a %<typeid%> expression");
6912 /* We can't be sure yet whether we're looking at a type-id or an
6914 cp_parser_parse_tentatively (parser
);
6915 /* Try a type-id first. */
6916 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6917 parser
->in_type_id_in_expr_p
= true;
6918 type
= cp_parser_type_id (parser
);
6919 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6920 /* Look for the `)' token. Otherwise, we can't be sure that
6921 we're not looking at an expression: consider `typeid (int
6922 (3))', for example. */
6923 cp_token
*close_paren
= parens
.require_close (parser
);
6924 /* If all went well, simply lookup the type-id. */
6925 if (cp_parser_parse_definitely (parser
))
6926 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6927 /* Otherwise, fall back to the expression variant. */
6932 /* Look for an expression. */
6933 expression
= cp_parser_expression (parser
, & idk
);
6934 /* Compute its typeid. */
6935 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6936 /* Look for the `)' token. */
6937 close_paren
= parens
.require_close (parser
);
6939 /* Restore the saved message. */
6940 parser
->type_definition_forbidden_message
= saved_message
;
6941 /* `typeid' may not appear in an integral constant expression. */
6942 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6943 postfix_expression
= error_mark_node
;
6945 /* Construct a location e.g. :
6948 ranging from the start of the "typeid" token to the final closing
6949 paren, with the caret at the start. */
6952 location_t typeid_loc
6953 = make_location (start_loc
, start_loc
, close_paren
->location
);
6954 postfix_expression
.set_location (typeid_loc
);
6955 postfix_expression
.maybe_add_location_wrapper ();
6963 /* The syntax permitted here is the same permitted for an
6964 elaborated-type-specifier. */
6965 ++parser
->prevent_constrained_type_specifiers
;
6966 type
= cp_parser_elaborated_type_specifier (parser
,
6967 /*is_friend=*/false,
6968 /*is_declaration=*/false);
6969 --parser
->prevent_constrained_type_specifiers
;
6970 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6975 case RID_BUILTIN_SHUFFLE
:
6976 case RID_BUILTIN_LAUNDER
:
6978 vec
<tree
, va_gc
> *vec
;
6982 cp_lexer_consume_token (parser
->lexer
);
6983 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6984 /*cast_p=*/false, /*allow_expansion_p=*/true,
6985 /*non_constant_p=*/NULL
);
6988 postfix_expression
= error_mark_node
;
6992 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6998 if (vec
->length () == 1)
7000 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
7003 error_at (loc
, "wrong number of arguments to "
7004 "%<__builtin_addressof%>");
7005 postfix_expression
= error_mark_node
;
7009 case RID_BUILTIN_LAUNDER
:
7010 if (vec
->length () == 1)
7011 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
7012 tf_warning_or_error
);
7015 error_at (loc
, "wrong number of arguments to "
7016 "%<__builtin_launder%>");
7017 postfix_expression
= error_mark_node
;
7021 case RID_BUILTIN_SHUFFLE
:
7022 if (vec
->length () == 2)
7024 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
7025 (*vec
)[1], tf_warning_or_error
);
7026 else if (vec
->length () == 3)
7028 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
7029 (*vec
)[2], tf_warning_or_error
);
7032 error_at (loc
, "wrong number of arguments to "
7033 "%<__builtin_shuffle%>");
7034 postfix_expression
= error_mark_node
;
7044 case RID_BUILTIN_CONVERTVECTOR
:
7048 /* Consume the `__builtin_convertvector' token. */
7049 cp_lexer_consume_token (parser
->lexer
);
7050 /* Look for the opening `('. */
7051 matching_parens parens
;
7052 parens
.require_open (parser
);
7053 /* Now, parse the assignment-expression. */
7054 expression
= cp_parser_assignment_expression (parser
);
7055 /* Look for the `,'. */
7056 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7057 location_t type_location
7058 = cp_lexer_peek_token (parser
->lexer
)->location
;
7059 /* Parse the type-id. */
7061 type_id_in_expr_sentinel
s (parser
);
7062 type
= cp_parser_type_id (parser
);
7064 /* Look for the closing `)'. */
7065 parens
.require_close (parser
);
7066 return cp_build_vec_convert (expression
, type_location
, type
,
7067 tf_warning_or_error
);
7074 /* If the next thing is a simple-type-specifier, we may be
7075 looking at a functional cast. We could also be looking at
7076 an id-expression. So, we try the functional cast, and if
7077 that doesn't work we fall back to the primary-expression. */
7078 cp_parser_parse_tentatively (parser
);
7079 /* Look for the simple-type-specifier. */
7080 ++parser
->prevent_constrained_type_specifiers
;
7081 type
= cp_parser_simple_type_specifier (parser
,
7082 /*decl_specs=*/NULL
,
7083 CP_PARSER_FLAGS_NONE
);
7084 --parser
->prevent_constrained_type_specifiers
;
7085 /* Parse the cast itself. */
7086 if (!cp_parser_error_occurred (parser
))
7088 = cp_parser_functional_cast (parser
, type
);
7089 /* If that worked, we're done. */
7090 if (cp_parser_parse_definitely (parser
))
7093 /* If the functional-cast didn't work out, try a
7094 compound-literal. */
7095 if (cp_parser_allow_gnu_extensions_p (parser
)
7096 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7098 cp_expr initializer
= NULL_TREE
;
7100 cp_parser_parse_tentatively (parser
);
7102 matching_parens parens
;
7103 parens
.consume_open (parser
);
7105 /* Avoid calling cp_parser_type_id pointlessly, see comment
7106 in cp_parser_cast_expression about c++/29234. */
7107 if (!cp_parser_compound_literal_p (parser
))
7108 cp_parser_simulate_error (parser
);
7111 /* Parse the type. */
7112 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7113 parser
->in_type_id_in_expr_p
= true;
7114 type
= cp_parser_type_id (parser
);
7115 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7116 parens
.require_close (parser
);
7119 /* If things aren't going well, there's no need to
7121 if (!cp_parser_error_occurred (parser
))
7123 bool non_constant_p
;
7124 /* Parse the brace-enclosed initializer list. */
7125 initializer
= cp_parser_braced_list (parser
,
7128 /* If that worked, we're definitely looking at a
7129 compound-literal expression. */
7130 if (cp_parser_parse_definitely (parser
))
7132 /* Warn the user that a compound literal is not
7133 allowed in standard C++. */
7134 pedwarn (input_location
, OPT_Wpedantic
,
7135 "ISO C++ forbids compound-literals");
7136 /* For simplicity, we disallow compound literals in
7137 constant-expressions. We could
7138 allow compound literals of integer type, whose
7139 initializer was a constant, in constant
7140 expressions. Permitting that usage, as a further
7141 extension, would not change the meaning of any
7142 currently accepted programs. (Of course, as
7143 compound literals are not part of ISO C++, the
7144 standard has nothing to say.) */
7145 if (cp_parser_non_integral_constant_expression (parser
,
7148 postfix_expression
= error_mark_node
;
7151 /* Form the representation of the compound-literal. */
7153 = finish_compound_literal (type
, initializer
,
7154 tf_warning_or_error
, fcl_c99
);
7155 postfix_expression
.set_location (initializer
.get_location ());
7160 /* It must be a primary-expression. */
7162 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7163 /*template_arg_p=*/false,
7170 /* Note that we don't need to worry about calling build_cplus_new on a
7171 class-valued CALL_EXPR in decltype when it isn't the end of the
7172 postfix-expression; unary_complex_lvalue will take care of that for
7175 /* Keep looping until the postfix-expression is complete. */
7178 if (idk
== CP_ID_KIND_UNQUALIFIED
7179 && identifier_p (postfix_expression
)
7180 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7181 /* It is not a Koenig lookup function call. */
7183 = unqualified_name_lookup_error (postfix_expression
);
7185 /* Peek at the next token. */
7186 token
= cp_lexer_peek_token (parser
->lexer
);
7188 switch (token
->type
)
7190 case CPP_OPEN_SQUARE
:
7191 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7193 cp_parser_error (parser
,
7194 "two consecutive %<[%> shall "
7195 "only introduce an attribute");
7196 return error_mark_node
;
7199 = cp_parser_postfix_open_square_expression (parser
,
7203 postfix_expression
.set_range (start_loc
,
7204 postfix_expression
.get_location ());
7206 idk
= CP_ID_KIND_NONE
;
7207 is_member_access
= false;
7210 case CPP_OPEN_PAREN
:
7211 /* postfix-expression ( expression-list [opt] ) */
7214 bool is_builtin_constant_p
;
7215 bool saved_integral_constant_expression_p
= false;
7216 bool saved_non_integral_constant_expression_p
= false;
7217 tsubst_flags_t complain
= complain_flags (decltype_p
);
7218 vec
<tree
, va_gc
> *args
;
7219 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7221 is_member_access
= false;
7223 tree stripped_expression
7224 = tree_strip_any_location_wrapper (postfix_expression
);
7225 is_builtin_constant_p
7226 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression
);
7227 if (is_builtin_constant_p
)
7229 /* The whole point of __builtin_constant_p is to allow
7230 non-constant expressions to appear as arguments. */
7231 saved_integral_constant_expression_p
7232 = parser
->integral_constant_expression_p
;
7233 saved_non_integral_constant_expression_p
7234 = parser
->non_integral_constant_expression_p
;
7235 parser
->integral_constant_expression_p
= false;
7237 args
= (cp_parser_parenthesized_expression_list
7239 /*cast_p=*/false, /*allow_expansion_p=*/true,
7240 /*non_constant_p=*/NULL
,
7241 /*close_paren_loc=*/&close_paren_loc
,
7242 /*wrap_locations_p=*/true));
7243 if (is_builtin_constant_p
)
7245 parser
->integral_constant_expression_p
7246 = saved_integral_constant_expression_p
;
7247 parser
->non_integral_constant_expression_p
7248 = saved_non_integral_constant_expression_p
;
7253 postfix_expression
= error_mark_node
;
7257 /* Function calls are not permitted in
7258 constant-expressions. */
7259 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7260 && cp_parser_non_integral_constant_expression (parser
,
7263 postfix_expression
= error_mark_node
;
7264 release_tree_vector (args
);
7269 if (idk
== CP_ID_KIND_UNQUALIFIED
7270 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7272 if (identifier_p (postfix_expression
)
7273 /* In C++2A, we may need to perform ADL for a template
7275 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7276 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7278 if (!args
->is_empty ())
7281 if (!any_type_dependent_arguments_p (args
))
7283 = perform_koenig_lookup (postfix_expression
, args
,
7288 = unqualified_fn_lookup_error (postfix_expression
);
7290 /* We do not perform argument-dependent lookup if
7291 normal lookup finds a non-function, in accordance
7292 with the expected resolution of DR 218. */
7293 else if (!args
->is_empty ()
7294 && is_overloaded_fn (postfix_expression
))
7296 /* We only need to look at the first function,
7297 because all the fns share the attribute we're
7298 concerned with (all member fns or all local
7300 tree fn
= get_first_fn (postfix_expression
);
7301 fn
= STRIP_TEMPLATE (fn
);
7303 /* Do not do argument dependent lookup if regular
7304 lookup finds a member function or a block-scope
7305 function declaration. [basic.lookup.argdep]/3 */
7306 if (!((TREE_CODE (fn
) == USING_DECL
&& DECL_DEPENDENT_P (fn
))
7307 || DECL_FUNCTION_MEMBER_P (fn
)
7308 || DECL_LOCAL_FUNCTION_P (fn
)))
7311 if (!any_type_dependent_arguments_p (args
))
7313 = perform_koenig_lookup (postfix_expression
, args
,
7319 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7321 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7322 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7324 if (processing_template_decl
7325 && (type_dependent_object_expression_p (instance
)
7326 || (!BASELINK_P (fn
)
7327 && TREE_CODE (fn
) != FIELD_DECL
)
7328 || type_dependent_expression_p (fn
)
7329 || any_type_dependent_arguments_p (args
)))
7331 maybe_generic_this_capture (instance
, fn
);
7333 = build_min_nt_call_vec (postfix_expression
, args
);
7334 release_tree_vector (args
);
7338 if (BASELINK_P (fn
))
7341 = (build_new_method_call
7342 (instance
, fn
, &args
, NULL_TREE
,
7343 (idk
== CP_ID_KIND_QUALIFIED
7344 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7351 = finish_call_expr (postfix_expression
, &args
,
7352 /*disallow_virtual=*/false,
7356 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7357 || TREE_CODE (postfix_expression
) == MEMBER_REF
7358 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7359 postfix_expression
= (build_offset_ref_call_from_tree
7360 (postfix_expression
, &args
,
7362 else if (idk
== CP_ID_KIND_QUALIFIED
)
7363 /* A call to a static class member, or a namespace-scope
7366 = finish_call_expr (postfix_expression
, &args
,
7367 /*disallow_virtual=*/true,
7371 /* All other function calls. */
7373 = finish_call_expr (postfix_expression
, &args
,
7374 /*disallow_virtual=*/false,
7378 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7380 location_t combined_loc
= make_location (token
->location
,
7383 postfix_expression
.set_location (combined_loc
);
7386 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7387 idk
= CP_ID_KIND_NONE
;
7389 release_tree_vector (args
);
7395 /* postfix-expression . template [opt] id-expression
7396 postfix-expression . pseudo-destructor-name
7397 postfix-expression -> template [opt] id-expression
7398 postfix-expression -> pseudo-destructor-name */
7400 /* Consume the `.' or `->' operator. */
7401 cp_lexer_consume_token (parser
->lexer
);
7404 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7408 is_member_access
= true;
7412 /* postfix-expression ++ */
7413 /* Consume the `++' token. */
7414 cp_lexer_consume_token (parser
->lexer
);
7415 /* Generate a representation for the complete expression. */
7417 = finish_increment_expr (postfix_expression
,
7418 POSTINCREMENT_EXPR
);
7419 /* Increments may not appear in constant-expressions. */
7420 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7421 postfix_expression
= error_mark_node
;
7422 idk
= CP_ID_KIND_NONE
;
7423 is_member_access
= false;
7426 case CPP_MINUS_MINUS
:
7427 /* postfix-expression -- */
7428 /* Consume the `--' token. */
7429 cp_lexer_consume_token (parser
->lexer
);
7430 /* Generate a representation for the complete expression. */
7432 = finish_increment_expr (postfix_expression
,
7433 POSTDECREMENT_EXPR
);
7434 /* Decrements may not appear in constant-expressions. */
7435 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7436 postfix_expression
= error_mark_node
;
7437 idk
= CP_ID_KIND_NONE
;
7438 is_member_access
= false;
7442 if (pidk_return
!= NULL
)
7443 * pidk_return
= idk
;
7444 if (member_access_only_p
)
7445 return is_member_access
7446 ? postfix_expression
7447 : cp_expr (error_mark_node
);
7449 return postfix_expression
;
7453 /* We should never get here. */
7455 return error_mark_node
;
7458 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7459 by cp_parser_builtin_offsetof. We're looking for
7461 postfix-expression [ expression ]
7462 postfix-expression [ braced-init-list ] (C++11)
7464 FOR_OFFSETOF is set if we're being called in that context, which
7465 changes how we deal with integer constant expressions. */
7468 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7469 tree postfix_expression
,
7473 tree index
= NULL_TREE
;
7474 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7475 bool saved_greater_than_is_operator_p
;
7477 /* Consume the `[' token. */
7478 cp_lexer_consume_token (parser
->lexer
);
7480 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7481 parser
->greater_than_is_operator_p
= true;
7483 /* Parse the index expression. */
7484 /* ??? For offsetof, there is a question of what to allow here. If
7485 offsetof is not being used in an integral constant expression context,
7486 then we *could* get the right answer by computing the value at runtime.
7487 If we are in an integral constant expression context, then we might
7488 could accept any constant expression; hard to say without analysis.
7489 Rather than open the barn door too wide right away, allow only integer
7490 constant expressions here. */
7492 index
= cp_parser_constant_expression (parser
);
7495 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7497 bool expr_nonconst_p
;
7498 cp_lexer_set_source_position (parser
->lexer
);
7499 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7500 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7503 index
= cp_parser_expression (parser
);
7506 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7508 /* Look for the closing `]'. */
7509 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7511 /* Build the ARRAY_REF. */
7512 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7515 /* When not doing offsetof, array references are not permitted in
7516 constant-expressions. */
7518 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7519 postfix_expression
= error_mark_node
;
7521 return postfix_expression
;
7524 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7525 dereference of incomplete type, returns true if error_mark_node should
7526 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7527 and *DEPENDENT_P. */
7530 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7533 /* In a template, be permissive by treating an object expression
7534 of incomplete type as dependent (after a pedwarn). */
7535 diagnostic_t kind
= (processing_template_decl
7536 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7538 switch (TREE_CODE (*postfix_expression
))
7541 case REINTERPRET_CAST_EXPR
:
7542 case CONST_CAST_EXPR
:
7543 case STATIC_CAST_EXPR
:
7544 case DYNAMIC_CAST_EXPR
:
7545 case IMPLICIT_CONV_EXPR
:
7546 case VIEW_CONVERT_EXPR
:
7547 case NON_LVALUE_EXPR
:
7551 /* Don't emit any diagnostic for OVERLOADs. */
7555 /* Avoid clobbering e.g. DECLs. */
7556 if (!EXPR_P (*postfix_expression
))
7561 if (kind
== DK_IGNORED
)
7564 location_t exploc
= location_of (*postfix_expression
);
7565 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7566 if (!MAYBE_CLASS_TYPE_P (*scope
))
7568 if (kind
== DK_ERROR
)
7569 *scope
= *postfix_expression
= error_mark_node
;
7570 else if (processing_template_decl
)
7572 *dependent_p
= true;
7573 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7578 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7579 by cp_parser_builtin_offsetof. We're looking for
7581 postfix-expression . template [opt] id-expression
7582 postfix-expression . pseudo-destructor-name
7583 postfix-expression -> template [opt] id-expression
7584 postfix-expression -> pseudo-destructor-name
7586 FOR_OFFSETOF is set if we're being called in that context. That sorta
7587 limits what of the above we'll actually accept, but nevermind.
7588 TOKEN_TYPE is the "." or "->" token, which will already have been
7589 removed from the stream. */
7592 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7593 enum cpp_ttype token_type
,
7594 cp_expr postfix_expression
,
7595 bool for_offsetof
, cp_id_kind
*idk
,
7596 location_t location
)
7600 bool pseudo_destructor_p
;
7601 tree scope
= NULL_TREE
;
7602 location_t start_loc
= postfix_expression
.get_start ();
7604 /* If this is a `->' operator, dereference the pointer. */
7605 if (token_type
== CPP_DEREF
)
7606 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7607 tf_warning_or_error
);
7608 /* Check to see whether or not the expression is type-dependent and
7609 not the current instantiation. */
7610 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7611 /* The identifier following the `->' or `.' is not qualified. */
7612 parser
->scope
= NULL_TREE
;
7613 parser
->qualifying_scope
= NULL_TREE
;
7614 parser
->object_scope
= NULL_TREE
;
7615 *idk
= CP_ID_KIND_NONE
;
7617 /* Enter the scope corresponding to the type of the object
7618 given by the POSTFIX_EXPRESSION. */
7621 scope
= TREE_TYPE (postfix_expression
);
7622 /* According to the standard, no expression should ever have
7623 reference type. Unfortunately, we do not currently match
7624 the standard in this respect in that our internal representation
7625 of an expression may have reference type even when the standard
7626 says it does not. Therefore, we have to manually obtain the
7627 underlying type here. */
7628 scope
= non_reference (scope
);
7629 /* The type of the POSTFIX_EXPRESSION must be complete. */
7630 /* Unlike the object expression in other contexts, *this is not
7631 required to be of complete type for purposes of class member
7632 access (5.2.5) outside the member function body. */
7633 if (postfix_expression
!= current_class_ref
7634 && scope
!= error_mark_node
7635 && !currently_open_class (scope
))
7637 scope
= complete_type (scope
);
7638 if (!COMPLETE_TYPE_P (scope
)
7639 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7641 return error_mark_node
;
7646 /* Let the name lookup machinery know that we are processing a
7647 class member access expression. */
7648 parser
->context
->object_type
= scope
;
7649 /* If something went wrong, we want to be able to discern that case,
7650 as opposed to the case where there was no SCOPE due to the type
7651 of expression being dependent. */
7653 scope
= error_mark_node
;
7654 /* If the SCOPE was erroneous, make the various semantic analysis
7655 functions exit quickly -- and without issuing additional error
7657 if (scope
== error_mark_node
)
7658 postfix_expression
= error_mark_node
;
7663 /* Tell cp_parser_lookup_name that there was an object, even though it's
7665 parser
->context
->object_type
= unknown_type_node
;
7667 /* Assume this expression is not a pseudo-destructor access. */
7668 pseudo_destructor_p
= false;
7670 /* If the SCOPE is a scalar type, then, if this is a valid program,
7671 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7672 is type dependent, it can be pseudo-destructor-name or something else.
7673 Try to parse it as pseudo-destructor-name first. */
7674 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7679 cp_parser_parse_tentatively (parser
);
7680 /* Parse the pseudo-destructor-name. */
7682 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7685 && (cp_parser_error_occurred (parser
)
7686 || !SCALAR_TYPE_P (type
)))
7687 cp_parser_abort_tentative_parse (parser
);
7688 else if (cp_parser_parse_definitely (parser
))
7690 pseudo_destructor_p
= true;
7692 = finish_pseudo_destructor_expr (postfix_expression
,
7697 if (!pseudo_destructor_p
)
7699 /* If the SCOPE is not a scalar type, we are looking at an
7700 ordinary class member access expression, rather than a
7701 pseudo-destructor-name. */
7703 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7704 /* Parse the id-expression. */
7705 name
= (cp_parser_id_expression
7707 cp_parser_optional_template_keyword (parser
),
7708 /*check_dependency_p=*/true,
7710 /*declarator_p=*/false,
7711 /*optional_p=*/false));
7712 /* In general, build a SCOPE_REF if the member name is qualified.
7713 However, if the name was not dependent and has already been
7714 resolved; there is no need to build the SCOPE_REF. For example;
7716 struct X { void f(); };
7717 template <typename T> void f(T* t) { t->X::f(); }
7719 Even though "t" is dependent, "X::f" is not and has been resolved
7720 to a BASELINK; there is no need to include scope information. */
7722 /* But we do need to remember that there was an explicit scope for
7723 virtual function calls. */
7725 *idk
= CP_ID_KIND_QUALIFIED
;
7727 /* If the name is a template-id that names a type, we will get a
7728 TYPE_DECL here. That is invalid code. */
7729 if (TREE_CODE (name
) == TYPE_DECL
)
7731 error_at (token
->location
, "invalid use of %qD", name
);
7732 postfix_expression
= error_mark_node
;
7736 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7738 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7740 error_at (token
->location
, "%<%D::%D%> is not a class member",
7741 parser
->scope
, name
);
7742 postfix_expression
= error_mark_node
;
7745 name
= build_qualified_name (/*type=*/NULL_TREE
,
7749 parser
->scope
= NULL_TREE
;
7750 parser
->qualifying_scope
= NULL_TREE
;
7751 parser
->object_scope
= NULL_TREE
;
7753 if (parser
->scope
&& name
&& BASELINK_P (name
))
7754 adjust_result_of_qualified_name_lookup
7755 (name
, parser
->scope
, scope
);
7757 = finish_class_member_access_expr (postfix_expression
, name
,
7759 tf_warning_or_error
);
7760 /* Build a location e.g.:
7763 where the caret is at the deref token, ranging from
7764 the start of postfix_expression to the end of the access expr. */
7766 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7767 location_t combined_loc
7768 = make_location (input_location
, start_loc
, end_loc
);
7769 protected_set_expr_location (postfix_expression
, combined_loc
);
7773 /* We no longer need to look up names in the scope of the object on
7774 the left-hand side of the `.' or `->' operator. */
7775 parser
->context
->object_type
= NULL_TREE
;
7777 /* Outside of offsetof, these operators may not appear in
7778 constant-expressions. */
7780 && (cp_parser_non_integral_constant_expression
7781 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7782 postfix_expression
= error_mark_node
;
7784 return postfix_expression
;
7787 /* Parse a parenthesized expression-list.
7790 assignment-expression
7791 expression-list, assignment-expression
7796 identifier, expression-list
7798 CAST_P is true if this expression is the target of a cast.
7800 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7803 WRAP_LOCATIONS_P is true if expressions within this list for which
7804 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7805 their source locations.
7807 Returns a vector of trees. Each element is a representation of an
7808 assignment-expression. NULL is returned if the ( and or ) are
7809 missing. An empty, but allocated, vector is returned on no
7810 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7811 if we are parsing an attribute list for an attribute that wants a
7812 plain identifier argument, normal_attr for an attribute that wants
7813 an expression, or non_attr if we aren't parsing an attribute list. If
7814 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7815 not all of the expressions in the list were constant.
7816 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7817 will be written to with the location of the closing parenthesis. If
7818 an error occurs, it may or may not be written to. */
7820 static vec
<tree
, va_gc
> *
7821 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7822 int is_attribute_list
,
7824 bool allow_expansion_p
,
7825 bool *non_constant_p
,
7826 location_t
*close_paren_loc
,
7827 bool wrap_locations_p
)
7829 vec
<tree
, va_gc
> *expression_list
;
7830 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7831 tree identifier
= NULL_TREE
;
7832 bool saved_greater_than_is_operator_p
;
7834 /* Assume all the expressions will be constant. */
7836 *non_constant_p
= false;
7838 matching_parens parens
;
7839 if (!parens
.require_open (parser
))
7842 expression_list
= make_tree_vector ();
7844 /* Within a parenthesized expression, a `>' token is always
7845 the greater-than operator. */
7846 saved_greater_than_is_operator_p
7847 = parser
->greater_than_is_operator_p
;
7848 parser
->greater_than_is_operator_p
= true;
7850 cp_expr
expr (NULL_TREE
);
7852 /* Consume expressions until there are no more. */
7853 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7856 /* At the beginning of attribute lists, check to see if the
7857 next token is an identifier. */
7858 if (is_attribute_list
== id_attr
7859 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7863 /* Consume the identifier. */
7864 token
= cp_lexer_consume_token (parser
->lexer
);
7865 /* Save the identifier. */
7866 identifier
= token
->u
.value
;
7870 bool expr_non_constant_p
;
7872 /* Parse the next assignment-expression. */
7873 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7875 /* A braced-init-list. */
7876 cp_lexer_set_source_position (parser
->lexer
);
7877 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7878 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7879 if (non_constant_p
&& expr_non_constant_p
)
7880 *non_constant_p
= true;
7882 else if (non_constant_p
)
7884 expr
= (cp_parser_constant_expression
7885 (parser
, /*allow_non_constant_p=*/true,
7886 &expr_non_constant_p
));
7887 if (expr_non_constant_p
)
7888 *non_constant_p
= true;
7891 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7895 expr
= instantiate_non_dependent_expr (expr
);
7897 /* If we have an ellipsis, then this is an expression
7899 if (allow_expansion_p
7900 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7902 /* Consume the `...'. */
7903 cp_lexer_consume_token (parser
->lexer
);
7905 /* Build the argument pack. */
7906 expr
= make_pack_expansion (expr
);
7909 if (wrap_locations_p
)
7910 expr
.maybe_add_location_wrapper ();
7912 /* Add it to the list. We add error_mark_node
7913 expressions to the list, so that we can still tell if
7914 the correct form for a parenthesized expression-list
7915 is found. That gives better errors. */
7916 vec_safe_push (expression_list
, expr
.get_value ());
7918 if (expr
== error_mark_node
)
7922 /* After the first item, attribute lists look the same as
7923 expression lists. */
7924 is_attribute_list
= non_attr
;
7927 /* If the next token isn't a `,', then we are done. */
7928 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7931 /* Otherwise, consume the `,' and keep going. */
7932 cp_lexer_consume_token (parser
->lexer
);
7935 if (close_paren_loc
)
7936 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7938 if (!parens
.require_close (parser
))
7943 /* We try and resync to an unnested comma, as that will give the
7944 user better diagnostics. */
7945 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7946 /*recovering=*/true,
7948 /*consume_paren=*/true);
7953 parser
->greater_than_is_operator_p
7954 = saved_greater_than_is_operator_p
;
7959 parser
->greater_than_is_operator_p
7960 = saved_greater_than_is_operator_p
;
7963 vec_safe_insert (expression_list
, 0, identifier
);
7965 return expression_list
;
7968 /* Parse a pseudo-destructor-name.
7970 pseudo-destructor-name:
7971 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7972 :: [opt] nested-name-specifier template template-id :: ~ type-name
7973 :: [opt] nested-name-specifier [opt] ~ type-name
7975 If either of the first two productions is used, sets *SCOPE to the
7976 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7977 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7978 or ERROR_MARK_NODE if the parse fails. */
7981 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7986 bool nested_name_specifier_p
;
7989 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7990 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7991 && !type_dependent_expression_p (object
))
7993 if (cxx_dialect
< cxx14
)
7994 pedwarn (input_location
, 0,
7995 "%<~auto%> only available with "
7996 "-std=c++14 or -std=gnu++14");
7997 cp_lexer_consume_token (parser
->lexer
);
7998 cp_lexer_consume_token (parser
->lexer
);
8000 *type
= TREE_TYPE (object
);
8004 /* Assume that things will not work out. */
8005 *type
= error_mark_node
;
8007 /* Look for the optional `::' operator. */
8008 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
8009 /* Look for the optional nested-name-specifier. */
8010 nested_name_specifier_p
8011 = (cp_parser_nested_name_specifier_opt (parser
,
8012 /*typename_keyword_p=*/false,
8013 /*check_dependency_p=*/true,
8015 /*is_declaration=*/false)
8017 /* Now, if we saw a nested-name-specifier, we might be doing the
8018 second production. */
8019 if (nested_name_specifier_p
8020 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8022 /* Consume the `template' keyword. */
8023 cp_lexer_consume_token (parser
->lexer
);
8024 /* Parse the template-id. */
8025 cp_parser_template_id (parser
,
8026 /*template_keyword_p=*/true,
8027 /*check_dependency_p=*/false,
8029 /*is_declaration=*/true);
8030 /* Look for the `::' token. */
8031 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8033 /* If the next token is not a `~', then there might be some
8034 additional qualification. */
8035 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
8037 /* At this point, we're looking for "type-name :: ~". The type-name
8038 must not be a class-name, since this is a pseudo-destructor. So,
8039 it must be either an enum-name, or a typedef-name -- both of which
8040 are just identifiers. So, we peek ahead to check that the "::"
8041 and "~" tokens are present; if they are not, then we can avoid
8042 calling type_name. */
8043 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
8044 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
8045 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
8047 cp_parser_error (parser
, "non-scalar type");
8051 /* Look for the type-name. */
8052 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8053 if (*scope
== error_mark_node
)
8056 /* Look for the `::' token. */
8057 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8062 /* Look for the `~'. */
8063 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
8065 /* Once we see the ~, this has to be a pseudo-destructor. */
8066 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
8067 cp_parser_commit_to_topmost_tentative_parse (parser
);
8069 /* Look for the type-name again. We are not responsible for
8070 checking that it matches the first type-name. */
8071 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8074 /* Parse a unary-expression.
8080 unary-operator cast-expression
8081 sizeof unary-expression
8083 alignof ( type-id ) [C++0x]
8090 __extension__ cast-expression
8091 __alignof__ unary-expression
8092 __alignof__ ( type-id )
8093 alignof unary-expression [C++0x]
8094 __real__ cast-expression
8095 __imag__ cast-expression
8097 sizeof ( type-id ) { initializer-list , [opt] }
8098 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8099 __alignof__ ( type-id ) { initializer-list , [opt] }
8101 ADDRESS_P is true iff the unary-expression is appearing as the
8102 operand of the `&' operator. CAST_P is true if this expression is
8103 the target of a cast.
8105 Returns a representation of the expression. */
8108 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8109 bool address_p
, bool cast_p
, bool decltype_p
)
8112 enum tree_code unary_operator
;
8114 /* Peek at the next token. */
8115 token
= cp_lexer_peek_token (parser
->lexer
);
8116 /* Some keywords give away the kind of expression. */
8117 if (token
->type
== CPP_KEYWORD
)
8119 enum rid keyword
= token
->keyword
;
8128 location_t start_loc
= token
->location
;
8130 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8131 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8133 /* Consume the token. */
8134 cp_lexer_consume_token (parser
->lexer
);
8135 /* Parse the operand. */
8136 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8138 if (TYPE_P (operand
))
8139 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8143 /* ISO C++ defines alignof only with types, not with
8144 expressions. So pedwarn if alignof is used with a non-
8145 type expression. However, __alignof__ is ok. */
8147 pedwarn (token
->location
, OPT_Wpedantic
,
8148 "ISO C++ does not allow %<alignof%> "
8151 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8153 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8154 SIZEOF_EXPR with the original operand. */
8155 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8157 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8159 if (!processing_template_decl
&& TYPE_P (operand
))
8161 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8162 build1 (NOP_EXPR
, operand
,
8164 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8167 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8168 TREE_SIDE_EFFECTS (ret
) = 0;
8169 TREE_READONLY (ret
) = 1;
8173 /* Construct a location e.g. :
8176 with start == caret at the start of the "alignof"/"sizeof"
8177 token, with the endpoint at the final closing paren. */
8178 location_t finish_loc
8179 = cp_lexer_previous_token (parser
->lexer
)->location
;
8180 location_t compound_loc
8181 = make_location (start_loc
, start_loc
, finish_loc
);
8183 cp_expr
ret_expr (ret
);
8184 ret_expr
.set_location (compound_loc
);
8185 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8189 case RID_BUILTIN_HAS_ATTRIBUTE
:
8190 return cp_parser_has_attribute_expression (parser
);
8193 return cp_parser_new_expression (parser
);
8196 return cp_parser_delete_expression (parser
);
8200 /* The saved value of the PEDANTIC flag. */
8204 /* Save away the PEDANTIC flag. */
8205 cp_parser_extension_opt (parser
, &saved_pedantic
);
8206 /* Parse the cast-expression. */
8207 expr
= cp_parser_simple_cast_expression (parser
);
8208 /* Restore the PEDANTIC flag. */
8209 pedantic
= saved_pedantic
;
8219 /* Consume the `__real__' or `__imag__' token. */
8220 cp_lexer_consume_token (parser
->lexer
);
8221 /* Parse the cast-expression. */
8222 expression
= cp_parser_simple_cast_expression (parser
);
8223 /* Create the complete representation. */
8224 return build_x_unary_op (token
->location
,
8225 (keyword
== RID_REALPART
8226 ? REALPART_EXPR
: IMAGPART_EXPR
),
8228 tf_warning_or_error
);
8232 case RID_TRANSACTION_ATOMIC
:
8233 case RID_TRANSACTION_RELAXED
:
8234 return cp_parser_transaction_expression (parser
, keyword
);
8239 const char *saved_message
;
8240 bool saved_integral_constant_expression_p
;
8241 bool saved_non_integral_constant_expression_p
;
8242 bool saved_greater_than_is_operator_p
;
8244 location_t start_loc
= token
->location
;
8246 cp_lexer_consume_token (parser
->lexer
);
8247 matching_parens parens
;
8248 parens
.require_open (parser
);
8250 saved_message
= parser
->type_definition_forbidden_message
;
8251 parser
->type_definition_forbidden_message
8252 = G_("types may not be defined in %<noexcept%> expressions");
8254 saved_integral_constant_expression_p
8255 = parser
->integral_constant_expression_p
;
8256 saved_non_integral_constant_expression_p
8257 = parser
->non_integral_constant_expression_p
;
8258 parser
->integral_constant_expression_p
= false;
8260 saved_greater_than_is_operator_p
8261 = parser
->greater_than_is_operator_p
;
8262 parser
->greater_than_is_operator_p
= true;
8264 ++cp_unevaluated_operand
;
8265 ++c_inhibit_evaluation_warnings
;
8266 ++cp_noexcept_operand
;
8267 expr
= cp_parser_expression (parser
);
8268 --cp_noexcept_operand
;
8269 --c_inhibit_evaluation_warnings
;
8270 --cp_unevaluated_operand
;
8272 parser
->greater_than_is_operator_p
8273 = saved_greater_than_is_operator_p
;
8275 parser
->integral_constant_expression_p
8276 = saved_integral_constant_expression_p
;
8277 parser
->non_integral_constant_expression_p
8278 = saved_non_integral_constant_expression_p
;
8280 parser
->type_definition_forbidden_message
= saved_message
;
8282 location_t finish_loc
8283 = cp_lexer_peek_token (parser
->lexer
)->location
;
8284 parens
.require_close (parser
);
8286 /* Construct a location of the form:
8289 with start == caret, finishing at the close-paren. */
8290 location_t noexcept_loc
8291 = make_location (start_loc
, start_loc
, finish_loc
);
8293 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8302 /* Look for the `:: new' and `:: delete', which also signal the
8303 beginning of a new-expression, or delete-expression,
8304 respectively. If the next token is `::', then it might be one of
8306 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8310 /* See if the token after the `::' is one of the keywords in
8311 which we're interested. */
8312 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8313 /* If it's `new', we have a new-expression. */
8314 if (keyword
== RID_NEW
)
8315 return cp_parser_new_expression (parser
);
8316 /* Similarly, for `delete'. */
8317 else if (keyword
== RID_DELETE
)
8318 return cp_parser_delete_expression (parser
);
8321 /* Look for a unary operator. */
8322 unary_operator
= cp_parser_unary_operator (token
);
8323 /* The `++' and `--' operators can be handled similarly, even though
8324 they are not technically unary-operators in the grammar. */
8325 if (unary_operator
== ERROR_MARK
)
8327 if (token
->type
== CPP_PLUS_PLUS
)
8328 unary_operator
= PREINCREMENT_EXPR
;
8329 else if (token
->type
== CPP_MINUS_MINUS
)
8330 unary_operator
= PREDECREMENT_EXPR
;
8331 /* Handle the GNU address-of-label extension. */
8332 else if (cp_parser_allow_gnu_extensions_p (parser
)
8333 && token
->type
== CPP_AND_AND
)
8337 location_t start_loc
= token
->location
;
8339 /* Consume the '&&' token. */
8340 cp_lexer_consume_token (parser
->lexer
);
8341 /* Look for the identifier. */
8342 location_t finish_loc
8343 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8344 identifier
= cp_parser_identifier (parser
);
8345 /* Construct a location of the form:
8348 with caret==start at the "&&", finish at the end of the label. */
8349 location_t combined_loc
8350 = make_location (start_loc
, start_loc
, finish_loc
);
8351 /* Create an expression representing the address. */
8352 expression
= finish_label_address_expr (identifier
, combined_loc
);
8353 if (cp_parser_non_integral_constant_expression (parser
,
8355 expression
= error_mark_node
;
8359 if (unary_operator
!= ERROR_MARK
)
8361 cp_expr cast_expression
;
8362 cp_expr expression
= error_mark_node
;
8363 non_integral_constant non_constant_p
= NIC_NONE
;
8364 location_t loc
= token
->location
;
8365 tsubst_flags_t complain
= complain_flags (decltype_p
);
8367 /* Consume the operator token. */
8368 token
= cp_lexer_consume_token (parser
->lexer
);
8369 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8371 /* Parse the cast-expression. */
8373 = cp_parser_cast_expression (parser
,
8374 unary_operator
== ADDR_EXPR
,
8380 OP_TOKEN CAST_EXPRESSION
8381 ^~~~~~~~~~~~~~~~~~~~~~~~~
8382 with start==caret at the operator token, and
8383 extending to the end of the cast_expression. */
8384 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8386 /* Now, build an appropriate representation. */
8387 switch (unary_operator
)
8390 non_constant_p
= NIC_STAR
;
8391 expression
= build_x_indirect_ref (loc
, cast_expression
,
8394 /* TODO: build_x_indirect_ref does not always honor the
8395 location, so ensure it is set. */
8396 expression
.set_location (loc
);
8400 non_constant_p
= NIC_ADDR
;
8403 expression
= build_x_unary_op (loc
, unary_operator
,
8406 /* TODO: build_x_unary_op does not always honor the location,
8407 so ensure it is set. */
8408 expression
.set_location (loc
);
8411 case PREINCREMENT_EXPR
:
8412 case PREDECREMENT_EXPR
:
8413 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8414 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8417 /* Immediately fold negation of a constant, unless the constant is 0
8418 (since -0 == 0) or it would overflow. */
8419 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
)
8422 = tree_strip_any_location_wrapper (cast_expression
);
8423 if (CONSTANT_CLASS_P (stripped_expr
)
8424 && !integer_zerop (stripped_expr
)
8425 && !TREE_OVERFLOW (stripped_expr
))
8427 tree folded
= fold_build1 (unary_operator
,
8428 TREE_TYPE (stripped_expr
),
8430 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8432 expression
= maybe_wrap_with_location (folded
, loc
);
8438 case UNARY_PLUS_EXPR
:
8439 case TRUTH_NOT_EXPR
:
8440 expression
= finish_unary_op_expr (loc
, unary_operator
,
8441 cast_expression
, complain
);
8448 if (non_constant_p
!= NIC_NONE
8449 && cp_parser_non_integral_constant_expression (parser
,
8451 expression
= error_mark_node
;
8456 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8457 /*member_access_only_p=*/false,
8462 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8463 unary-operator, the corresponding tree code is returned. */
8465 static enum tree_code
8466 cp_parser_unary_operator (cp_token
* token
)
8468 switch (token
->type
)
8471 return INDIRECT_REF
;
8477 return UNARY_PLUS_EXPR
;
8483 return TRUTH_NOT_EXPR
;
8486 return BIT_NOT_EXPR
;
8493 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8494 Returns a representation of the expression. */
8497 cp_parser_has_attribute_expression (cp_parser
*parser
)
8499 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8501 /* Consume the __builtin_has_attribute token. */
8502 cp_lexer_consume_token (parser
->lexer
);
8504 matching_parens parens
;
8505 if (!parens
.require_open (parser
))
8506 return error_mark_node
;
8508 /* Types cannot be defined in a `sizeof' expression. Save away the
8510 const char *saved_message
= parser
->type_definition_forbidden_message
;
8511 /* And create the new one. */
8512 const int kwd
= RID_BUILTIN_HAS_ATTRIBUTE
;
8513 char *tmp
= concat ("types may not be defined in %<",
8514 IDENTIFIER_POINTER (ridpointers
[kwd
]),
8515 "%> expressions", NULL
);
8516 parser
->type_definition_forbidden_message
= tmp
;
8518 /* The restrictions on constant-expressions do not apply inside
8519 sizeof expressions. */
8520 bool saved_integral_constant_expression_p
8521 = parser
->integral_constant_expression_p
;
8522 bool saved_non_integral_constant_expression_p
8523 = parser
->non_integral_constant_expression_p
;
8524 parser
->integral_constant_expression_p
= false;
8526 /* Do not actually evaluate the expression. */
8527 ++cp_unevaluated_operand
;
8528 ++c_inhibit_evaluation_warnings
;
8530 tree oper
= NULL_TREE
;
8532 /* We can't be sure yet whether we're looking at a type-id or an
8534 cp_parser_parse_tentatively (parser
);
8536 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8537 parser
->in_type_id_in_expr_p
= true;
8538 /* Look for the type-id. */
8539 oper
= cp_parser_type_id (parser
);
8540 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8542 cp_parser_parse_definitely (parser
);
8544 /* If the type-id production did not work out, then we must be
8545 looking at the unary-expression production. */
8546 if (!oper
|| oper
== error_mark_node
)
8547 oper
= cp_parser_unary_expression (parser
);
8549 STRIP_ANY_LOCATION_WRAPPER (oper
);
8551 /* Go back to evaluating expressions. */
8552 --cp_unevaluated_operand
;
8553 --c_inhibit_evaluation_warnings
;
8555 /* Free the message we created. */
8557 /* And restore the old one. */
8558 parser
->type_definition_forbidden_message
= saved_message
;
8559 parser
->integral_constant_expression_p
8560 = saved_integral_constant_expression_p
;
8561 parser
->non_integral_constant_expression_p
8562 = saved_non_integral_constant_expression_p
;
8564 /* Consume the comma if it's there. */
8565 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
8567 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8568 /*consume_paren=*/true);
8569 return error_mark_node
;
8572 /* Parse the attribute specification. */
8574 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8575 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
8577 if (oper
!= error_mark_node
)
8579 /* Fold constant expressions used in attributes first. */
8580 cp_check_const_attributes (attr
);
8582 /* Finally, see if OPER has been declared with ATTR. */
8583 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
8586 parens
.require_close (parser
);
8590 error_at (atloc
, "expected identifier");
8591 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8594 /* Construct a location e.g. :
8595 __builtin_has_attribute (oper, attr)
8596 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8597 with start == caret at the start of the built-in token,
8598 and with the endpoint at the final closing paren. */
8599 location_t finish_loc
8600 = cp_lexer_previous_token (parser
->lexer
)->location
;
8601 location_t compound_loc
8602 = make_location (start_loc
, start_loc
, finish_loc
);
8604 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
8605 ret_expr
.set_location (compound_loc
);
8606 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8610 /* Parse a new-expression.
8613 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8614 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8616 Returns a representation of the expression. */
8619 cp_parser_new_expression (cp_parser
* parser
)
8621 bool global_scope_p
;
8622 vec
<tree
, va_gc
> *placement
;
8624 vec
<tree
, va_gc
> *initializer
;
8625 tree nelts
= NULL_TREE
;
8628 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8630 /* Look for the optional `::' operator. */
8632 = (cp_parser_global_scope_opt (parser
,
8633 /*current_scope_valid_p=*/false)
8635 /* Look for the `new' operator. */
8636 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8637 /* There's no easy way to tell a new-placement from the
8638 `( type-id )' construct. */
8639 cp_parser_parse_tentatively (parser
);
8640 /* Look for a new-placement. */
8641 placement
= cp_parser_new_placement (parser
);
8642 /* If that didn't work out, there's no new-placement. */
8643 if (!cp_parser_parse_definitely (parser
))
8645 if (placement
!= NULL
)
8646 release_tree_vector (placement
);
8650 /* If the next token is a `(', then we have a parenthesized
8652 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8655 const char *saved_message
= parser
->type_definition_forbidden_message
;
8657 /* Consume the `('. */
8658 matching_parens parens
;
8659 parens
.consume_open (parser
);
8661 /* Parse the type-id. */
8662 parser
->type_definition_forbidden_message
8663 = G_("types may not be defined in a new-expression");
8665 type_id_in_expr_sentinel
s (parser
);
8666 type
= cp_parser_type_id (parser
);
8668 parser
->type_definition_forbidden_message
= saved_message
;
8670 /* Look for the closing `)'. */
8671 parens
.require_close (parser
);
8672 token
= cp_lexer_peek_token (parser
->lexer
);
8673 /* There should not be a direct-new-declarator in this production,
8674 but GCC used to allowed this, so we check and emit a sensible error
8675 message for this case. */
8676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8678 error_at (token
->location
,
8679 "array bound forbidden after parenthesized type-id");
8680 inform (token
->location
,
8681 "try removing the parentheses around the type-id");
8682 cp_parser_direct_new_declarator (parser
);
8685 /* Otherwise, there must be a new-type-id. */
8687 type
= cp_parser_new_type_id (parser
, &nelts
);
8689 /* If the next token is a `(' or '{', then we have a new-initializer. */
8690 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8691 if (token
->type
== CPP_OPEN_PAREN
8692 || token
->type
== CPP_OPEN_BRACE
)
8693 initializer
= cp_parser_new_initializer (parser
);
8697 /* A new-expression may not appear in an integral constant
8699 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8700 ret
= error_mark_node
;
8701 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8702 of a new-type-id or type-id of a new-expression, the new-expression shall
8703 contain a new-initializer of the form ( assignment-expression )".
8704 Additionally, consistently with the spirit of DR 1467, we want to accept
8705 'new auto { 2 }' too. */
8706 else if ((ret
= type_uses_auto (type
))
8707 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8708 && (vec_safe_length (initializer
) != 1
8709 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8710 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8712 error_at (token
->location
,
8713 "initialization of new-expression for type %<auto%> "
8714 "requires exactly one element");
8715 ret
= error_mark_node
;
8719 /* Construct a location e.g.:
8722 with caret == start at the start of the "new" token, and the end
8723 at the end of the final token we consumed. */
8724 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8725 location_t end_loc
= get_finish (end_tok
->location
);
8726 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8728 /* Create a representation of the new-expression. */
8729 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8730 tf_warning_or_error
);
8731 protected_set_expr_location (ret
, combined_loc
);
8734 if (placement
!= NULL
)
8735 release_tree_vector (placement
);
8736 if (initializer
!= NULL
)
8737 release_tree_vector (initializer
);
8742 /* Parse a new-placement.
8747 Returns the same representation as for an expression-list. */
8749 static vec
<tree
, va_gc
> *
8750 cp_parser_new_placement (cp_parser
* parser
)
8752 vec
<tree
, va_gc
> *expression_list
;
8754 /* Parse the expression-list. */
8755 expression_list
= (cp_parser_parenthesized_expression_list
8756 (parser
, non_attr
, /*cast_p=*/false,
8757 /*allow_expansion_p=*/true,
8758 /*non_constant_p=*/NULL
));
8760 if (expression_list
&& expression_list
->is_empty ())
8761 error ("expected expression-list or type-id");
8763 return expression_list
;
8766 /* Parse a new-type-id.
8769 type-specifier-seq new-declarator [opt]
8771 Returns the TYPE allocated. If the new-type-id indicates an array
8772 type, *NELTS is set to the number of elements in the last array
8773 bound; the TYPE will not include the last array bound. */
8776 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8778 cp_decl_specifier_seq type_specifier_seq
;
8779 cp_declarator
*new_declarator
;
8780 cp_declarator
*declarator
;
8781 cp_declarator
*outer_declarator
;
8782 const char *saved_message
;
8784 /* The type-specifier sequence must not contain type definitions.
8785 (It cannot contain declarations of new types either, but if they
8786 are not definitions we will catch that because they are not
8788 saved_message
= parser
->type_definition_forbidden_message
;
8789 parser
->type_definition_forbidden_message
8790 = G_("types may not be defined in a new-type-id");
8791 /* Parse the type-specifier-seq. */
8792 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
8793 /*is_declaration=*/false,
8794 /*is_trailing_return=*/false,
8795 &type_specifier_seq
);
8796 /* Restore the old message. */
8797 parser
->type_definition_forbidden_message
= saved_message
;
8799 if (type_specifier_seq
.type
== error_mark_node
)
8800 return error_mark_node
;
8802 /* Parse the new-declarator. */
8803 new_declarator
= cp_parser_new_declarator_opt (parser
);
8805 /* Determine the number of elements in the last array dimension, if
8808 /* Skip down to the last array dimension. */
8809 declarator
= new_declarator
;
8810 outer_declarator
= NULL
;
8811 while (declarator
&& (declarator
->kind
== cdk_pointer
8812 || declarator
->kind
== cdk_ptrmem
))
8814 outer_declarator
= declarator
;
8815 declarator
= declarator
->declarator
;
8818 && declarator
->kind
== cdk_array
8819 && declarator
->declarator
8820 && declarator
->declarator
->kind
== cdk_array
)
8822 outer_declarator
= declarator
;
8823 declarator
= declarator
->declarator
;
8826 if (declarator
&& declarator
->kind
== cdk_array
)
8828 *nelts
= declarator
->u
.array
.bounds
;
8829 if (*nelts
== error_mark_node
)
8830 *nelts
= integer_one_node
;
8832 if (outer_declarator
)
8833 outer_declarator
->declarator
= declarator
->declarator
;
8835 new_declarator
= NULL
;
8838 return groktypename (&type_specifier_seq
, new_declarator
, false);
8841 /* Parse an (optional) new-declarator.
8844 ptr-operator new-declarator [opt]
8845 direct-new-declarator
8847 Returns the declarator. */
8849 static cp_declarator
*
8850 cp_parser_new_declarator_opt (cp_parser
* parser
)
8852 enum tree_code code
;
8853 tree type
, std_attributes
= NULL_TREE
;
8854 cp_cv_quals cv_quals
;
8856 /* We don't know if there's a ptr-operator next, or not. */
8857 cp_parser_parse_tentatively (parser
);
8858 /* Look for a ptr-operator. */
8859 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8860 /* If that worked, look for more new-declarators. */
8861 if (cp_parser_parse_definitely (parser
))
8863 cp_declarator
*declarator
;
8865 /* Parse another optional declarator. */
8866 declarator
= cp_parser_new_declarator_opt (parser
);
8868 declarator
= cp_parser_make_indirect_declarator
8869 (code
, type
, cv_quals
, declarator
, std_attributes
);
8874 /* If the next token is a `[', there is a direct-new-declarator. */
8875 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8876 return cp_parser_direct_new_declarator (parser
);
8881 /* Parse a direct-new-declarator.
8883 direct-new-declarator:
8885 direct-new-declarator [constant-expression]
8889 static cp_declarator
*
8890 cp_parser_direct_new_declarator (cp_parser
* parser
)
8892 cp_declarator
*declarator
= NULL
;
8899 /* Look for the opening `['. */
8900 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8902 token
= cp_lexer_peek_token (parser
->lexer
);
8903 expression
= cp_parser_expression (parser
);
8904 /* The standard requires that the expression have integral
8905 type. DR 74 adds enumeration types. We believe that the
8906 real intent is that these expressions be handled like the
8907 expression in a `switch' condition, which also allows
8908 classes with a single conversion to integral or
8909 enumeration type. */
8910 if (!processing_template_decl
)
8913 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8918 error_at (token
->location
,
8919 "expression in new-declarator must have integral "
8920 "or enumeration type");
8921 expression
= error_mark_node
;
8925 /* Look for the closing `]'. */
8926 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8928 /* Add this bound to the declarator. */
8929 declarator
= make_array_declarator (declarator
, expression
);
8931 /* If the next token is not a `[', then there are no more
8933 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8940 /* Parse a new-initializer.
8943 ( expression-list [opt] )
8946 Returns a representation of the expression-list. */
8948 static vec
<tree
, va_gc
> *
8949 cp_parser_new_initializer (cp_parser
* parser
)
8951 vec
<tree
, va_gc
> *expression_list
;
8953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8956 bool expr_non_constant_p
;
8957 cp_lexer_set_source_position (parser
->lexer
);
8958 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8959 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8960 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8961 expression_list
= make_tree_vector_single (t
);
8964 expression_list
= (cp_parser_parenthesized_expression_list
8965 (parser
, non_attr
, /*cast_p=*/false,
8966 /*allow_expansion_p=*/true,
8967 /*non_constant_p=*/NULL
));
8969 return expression_list
;
8972 /* Parse a delete-expression.
8975 :: [opt] delete cast-expression
8976 :: [opt] delete [ ] cast-expression
8978 Returns a representation of the expression. */
8981 cp_parser_delete_expression (cp_parser
* parser
)
8983 bool global_scope_p
;
8987 /* Look for the optional `::' operator. */
8989 = (cp_parser_global_scope_opt (parser
,
8990 /*current_scope_valid_p=*/false)
8992 /* Look for the `delete' keyword. */
8993 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8994 /* See if the array syntax is in use. */
8995 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8997 /* Consume the `[' token. */
8998 cp_lexer_consume_token (parser
->lexer
);
8999 /* Look for the `]' token. */
9000 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9001 /* Remember that this is the `[]' construct. */
9007 /* Parse the cast-expression. */
9008 expression
= cp_parser_simple_cast_expression (parser
);
9010 /* A delete-expression may not appear in an integral constant
9012 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
9013 return error_mark_node
;
9015 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
9016 tf_warning_or_error
);
9019 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9020 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9024 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
9026 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9027 switch (token
->type
)
9033 case CPP_CLOSE_SQUARE
:
9034 case CPP_CLOSE_PAREN
:
9035 case CPP_CLOSE_BRACE
:
9036 case CPP_OPEN_BRACE
:
9040 case CPP_DEREF_STAR
:
9048 case CPP_GREATER_EQ
:
9069 case CPP_OPEN_PAREN
:
9070 /* In ((type ()) () the last () isn't a valid cast-expression,
9071 so the whole must be parsed as postfix-expression. */
9072 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
9075 case CPP_OPEN_SQUARE
:
9076 /* '[' may start a primary-expression in obj-c++ and in C++11,
9077 as a lambda-expression, eg, '(void)[]{}'. */
9078 if (cxx_dialect
>= cxx11
)
9080 return c_dialect_objc ();
9083 case CPP_MINUS_MINUS
:
9084 /* '++' and '--' may or may not start a cast-expression:
9086 struct T { void operator++(int); };
9087 void f() { (T())++; }
9100 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9101 in the order: const_cast, static_cast, reinterpret_cast.
9103 Don't suggest dynamic_cast.
9105 Return the first legal cast kind found, or NULL otherwise. */
9108 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9112 /* Reuse the parser logic by attempting to build the various kinds of
9113 cast, with "complain" disabled.
9114 Identify the first such cast that is valid. */
9116 /* Don't attempt to run such logic within template processing. */
9117 if (processing_template_decl
)
9120 /* First try const_cast. */
9121 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
9122 if (trial
!= error_mark_node
)
9123 return "const_cast";
9125 /* If that fails, try static_cast. */
9126 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
9127 if (trial
!= error_mark_node
)
9128 return "static_cast";
9130 /* Finally, try reinterpret_cast. */
9131 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
9132 if (trial
!= error_mark_node
)
9133 return "reinterpret_cast";
9135 /* No such cast possible. */
9139 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9140 suggesting how to convert a C-style cast of the form:
9144 to a C++-style cast.
9146 The primary range of RICHLOC is asssumed to be that of the original
9147 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9148 of the parens in the C-style cast. */
9151 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9152 location_t close_paren_loc
, tree orig_expr
,
9155 /* This function is non-trivial, so bail out now if the warning isn't
9156 going to be emitted. */
9157 if (!warn_old_style_cast
)
9160 /* Try to find a legal C++ cast, trying them in order:
9161 const_cast, static_cast, reinterpret_cast. */
9162 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9163 if (!cast_suggestion
)
9166 /* Replace the open paren with "CAST_SUGGESTION<". */
9168 pp_printf (&pp
, "%s<", cast_suggestion
);
9169 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9171 /* Replace the close paren with "> (". */
9172 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9174 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9175 rich_loc
->add_fixit_insert_after (")");
9179 /* Parse a cast-expression.
9183 ( type-id ) cast-expression
9185 ADDRESS_P is true iff the unary-expression is appearing as the
9186 operand of the `&' operator. CAST_P is true if this expression is
9187 the target of a cast.
9189 Returns a representation of the expression. */
9192 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9193 bool decltype_p
, cp_id_kind
* pidk
)
9195 /* If it's a `(', then we might be looking at a cast. */
9196 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9198 tree type
= NULL_TREE
;
9199 cp_expr
expr (NULL_TREE
);
9200 int cast_expression
= 0;
9201 const char *saved_message
;
9203 /* There's no way to know yet whether or not this is a cast.
9204 For example, `(int (3))' is a unary-expression, while `(int)
9205 3' is a cast. So, we resort to parsing tentatively. */
9206 cp_parser_parse_tentatively (parser
);
9207 /* Types may not be defined in a cast. */
9208 saved_message
= parser
->type_definition_forbidden_message
;
9209 parser
->type_definition_forbidden_message
9210 = G_("types may not be defined in casts");
9211 /* Consume the `('. */
9212 matching_parens parens
;
9213 cp_token
*open_paren
= parens
.consume_open (parser
);
9214 location_t open_paren_loc
= open_paren
->location
;
9215 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9217 /* A very tricky bit is that `(struct S) { 3 }' is a
9218 compound-literal (which we permit in C++ as an extension).
9219 But, that construct is not a cast-expression -- it is a
9220 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9221 is legal; if the compound-literal were a cast-expression,
9222 you'd need an extra set of parentheses.) But, if we parse
9223 the type-id, and it happens to be a class-specifier, then we
9224 will commit to the parse at that point, because we cannot
9225 undo the action that is done when creating a new class. So,
9226 then we cannot back up and do a postfix-expression.
9228 Another tricky case is the following (c++/29234):
9230 struct S { void operator () (); };
9237 As a type-id we parse the parenthesized S()() as a function
9238 returning a function, groktypename complains and we cannot
9239 back up in this case either.
9241 Therefore, we scan ahead to the closing `)', and check to see
9242 if the tokens after the `)' can start a cast-expression. Otherwise
9243 we are dealing with an unary-expression, a postfix-expression
9246 Yet another tricky case, in C++11, is the following (c++/54891):
9250 The issue is that usually, besides the case of lambda-expressions,
9251 the parenthesized type-id cannot be followed by '[', and, eg, we
9252 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9253 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9254 we don't commit, we try a cast-expression, then an unary-expression.
9256 Save tokens so that we can put them back. */
9257 cp_lexer_save_tokens (parser
->lexer
);
9259 /* We may be looking at a cast-expression. */
9260 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9261 /*consume_paren=*/true))
9263 = cp_parser_tokens_start_cast_expression (parser
);
9265 /* Roll back the tokens we skipped. */
9266 cp_lexer_rollback_tokens (parser
->lexer
);
9267 /* If we aren't looking at a cast-expression, simulate an error so
9268 that the call to cp_parser_error_occurred below returns true. */
9269 if (!cast_expression
)
9270 cp_parser_simulate_error (parser
);
9273 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9274 parser
->in_type_id_in_expr_p
= true;
9275 /* Look for the type-id. */
9276 type
= cp_parser_type_id (parser
);
9277 /* Look for the closing `)'. */
9278 cp_token
*close_paren
= parens
.require_close (parser
);
9280 close_paren_loc
= close_paren
->location
;
9281 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9284 /* Restore the saved message. */
9285 parser
->type_definition_forbidden_message
= saved_message
;
9287 /* At this point this can only be either a cast or a
9288 parenthesized ctor such as `(T ())' that looks like a cast to
9289 function returning T. */
9290 if (!cp_parser_error_occurred (parser
))
9292 /* Only commit if the cast-expression doesn't start with
9293 '++', '--', or '[' in C++11. */
9294 if (cast_expression
> 0)
9295 cp_parser_commit_to_topmost_tentative_parse (parser
);
9297 expr
= cp_parser_cast_expression (parser
,
9298 /*address_p=*/false,
9300 /*decltype_p=*/false,
9303 if (cp_parser_parse_definitely (parser
))
9305 /* Warn about old-style casts, if so requested. */
9306 if (warn_old_style_cast
9307 && !in_system_header_at (input_location
)
9308 && !VOID_TYPE_P (type
)
9309 && current_lang_name
!= lang_name_c
)
9311 gcc_rich_location
rich_loc (input_location
);
9312 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9314 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9315 "use of old-style cast to %q#T", type
);
9318 /* Only type conversions to integral or enumeration types
9319 can be used in constant-expressions. */
9320 if (!cast_valid_in_integral_constant_expression_p (type
)
9321 && cp_parser_non_integral_constant_expression (parser
,
9323 return error_mark_node
;
9325 /* Perform the cast. */
9329 with start==caret at the open paren, extending to the
9331 location_t cast_loc
= make_location (open_paren_loc
,
9333 expr
.get_finish ());
9334 expr
= build_c_cast (cast_loc
, type
, expr
);
9339 cp_parser_abort_tentative_parse (parser
);
9342 /* If we get here, then it's not a cast, so it must be a
9343 unary-expression. */
9344 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9345 cast_p
, decltype_p
);
9348 /* Parse a binary expression of the general form:
9352 pm-expression .* cast-expression
9353 pm-expression ->* cast-expression
9355 multiplicative-expression:
9357 multiplicative-expression * pm-expression
9358 multiplicative-expression / pm-expression
9359 multiplicative-expression % pm-expression
9361 additive-expression:
9362 multiplicative-expression
9363 additive-expression + multiplicative-expression
9364 additive-expression - multiplicative-expression
9368 shift-expression << additive-expression
9369 shift-expression >> additive-expression
9371 relational-expression:
9373 relational-expression < shift-expression
9374 relational-expression > shift-expression
9375 relational-expression <= shift-expression
9376 relational-expression >= shift-expression
9380 relational-expression:
9381 relational-expression <? shift-expression
9382 relational-expression >? shift-expression
9384 equality-expression:
9385 relational-expression
9386 equality-expression == relational-expression
9387 equality-expression != relational-expression
9391 and-expression & equality-expression
9393 exclusive-or-expression:
9395 exclusive-or-expression ^ and-expression
9397 inclusive-or-expression:
9398 exclusive-or-expression
9399 inclusive-or-expression | exclusive-or-expression
9401 logical-and-expression:
9402 inclusive-or-expression
9403 logical-and-expression && inclusive-or-expression
9405 logical-or-expression:
9406 logical-and-expression
9407 logical-or-expression || logical-and-expression
9409 All these are implemented with a single function like:
9412 simple-cast-expression
9413 binary-expression <token> binary-expression
9415 CAST_P is true if this expression is the target of a cast.
9417 The binops_by_token map is used to get the tree codes for each <token> type.
9418 binary-expressions are associated according to a precedence table. */
9420 #define TOKEN_PRECEDENCE(token) \
9421 (((token->type == CPP_GREATER \
9422 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9423 && !parser->greater_than_is_operator_p) \
9424 ? PREC_NOT_OPERATOR \
9425 : binops_by_token[token->type].prec)
9428 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9429 bool no_toplevel_fold_p
,
9431 enum cp_parser_prec prec
,
9434 cp_parser_expression_stack stack
;
9435 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9436 cp_parser_expression_stack_entry current
;
9439 enum tree_code rhs_type
;
9440 enum cp_parser_prec new_prec
, lookahead_prec
;
9443 /* Parse the first expression. */
9444 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9445 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9446 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9447 cast_p
, decltype_p
, pidk
);
9448 current
.prec
= prec
;
9450 if (cp_parser_error_occurred (parser
))
9451 return error_mark_node
;
9455 /* Get an operator token. */
9456 token
= cp_lexer_peek_token (parser
->lexer
);
9458 if (warn_cxx11_compat
9459 && token
->type
== CPP_RSHIFT
9460 && !parser
->greater_than_is_operator_p
)
9462 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9463 "%<>>%> operator is treated"
9464 " as two right angle brackets in C++11"))
9465 inform (token
->location
,
9466 "suggest parentheses around %<>>%> expression");
9469 new_prec
= TOKEN_PRECEDENCE (token
);
9470 if (new_prec
!= PREC_NOT_OPERATOR
9471 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9472 /* This is a fold-expression; handle it later. */
9473 new_prec
= PREC_NOT_OPERATOR
;
9475 /* Popping an entry off the stack means we completed a subexpression:
9476 - either we found a token which is not an operator (`>' where it is not
9477 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9478 will happen repeatedly;
9479 - or, we found an operator which has lower priority. This is the case
9480 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9482 if (new_prec
<= current
.prec
)
9491 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9492 current
.loc
= token
->location
;
9494 /* We used the operator token. */
9495 cp_lexer_consume_token (parser
->lexer
);
9497 /* For "false && x" or "true || x", x will never be executed;
9498 disable warnings while evaluating it. */
9499 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9500 c_inhibit_evaluation_warnings
+=
9501 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9502 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9503 c_inhibit_evaluation_warnings
+=
9504 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9506 /* Extract another operand. It may be the RHS of this expression
9507 or the LHS of a new, higher priority expression. */
9508 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9509 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9510 rhs
= cp_parser_simple_cast_expression (parser
);
9512 /* Get another operator token. Look up its precedence to avoid
9513 building a useless (immediately popped) stack entry for common
9514 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9515 token
= cp_lexer_peek_token (parser
->lexer
);
9516 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9517 if (lookahead_prec
!= PREC_NOT_OPERATOR
9518 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9519 lookahead_prec
= PREC_NOT_OPERATOR
;
9520 if (lookahead_prec
> new_prec
)
9522 /* ... and prepare to parse the RHS of the new, higher priority
9523 expression. Since precedence levels on the stack are
9524 monotonically increasing, we do not have to care about
9529 current
.lhs_type
= rhs_type
;
9530 current
.prec
= new_prec
;
9531 new_prec
= lookahead_prec
;
9535 lookahead_prec
= new_prec
;
9536 /* If the stack is not empty, we have parsed into LHS the right side
9537 (`4' in the example above) of an expression we had suspended.
9538 We can use the information on the stack to recover the LHS (`3')
9539 from the stack together with the tree code (`MULT_EXPR'), and
9540 the precedence of the higher level subexpression
9541 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9542 which will be used to actually build the additive expression. */
9544 rhs_type
= current
.lhs_type
;
9549 /* Undo the disabling of warnings done above. */
9550 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9551 c_inhibit_evaluation_warnings
-=
9552 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9553 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9554 c_inhibit_evaluation_warnings
-=
9555 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9557 if (warn_logical_not_paren
9558 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9559 && current
.lhs_type
== TRUTH_NOT_EXPR
9560 /* Avoid warning for !!x == y. */
9561 && (TREE_CODE (current
.lhs
) != NE_EXPR
9562 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9563 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9564 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9565 /* Avoid warning for !b == y where b is boolean. */
9566 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9567 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9569 /* Avoid warning for !!b == y where b is boolean. */
9570 && (!DECL_P (tree_strip_any_location_wrapper (current
.lhs
))
9571 || TREE_TYPE (current
.lhs
) == NULL_TREE
9572 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9573 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9574 current
.lhs
, maybe_constant_value (rhs
));
9578 location_t combined_loc
= make_location (current
.loc
,
9579 current
.lhs
.get_start (),
9582 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9583 ERROR_MARK for everything that is not a binary expression.
9584 This makes warn_about_parentheses miss some warnings that
9585 involve unary operators. For unary expressions we should
9586 pass the correct tree_code unless the unary expression was
9587 surrounded by parentheses.
9589 if (no_toplevel_fold_p
9590 && lookahead_prec
<= current
.prec
9593 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9594 current
.lhs
= error_mark_node
;
9598 = build_min (current
.tree_type
,
9599 TREE_CODE_CLASS (current
.tree_type
)
9601 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9602 current
.lhs
.get_value (), rhs
.get_value ());
9603 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9608 op_location_t
op_loc (current
.loc
, combined_loc
);
9609 current
.lhs
= build_x_binary_op (op_loc
, current
.tree_type
,
9610 current
.lhs
, current
.lhs_type
,
9611 rhs
, rhs_type
, &overload
,
9612 complain_flags (decltype_p
));
9613 /* TODO: build_x_binary_op doesn't always honor the location. */
9614 current
.lhs
.set_location (combined_loc
);
9616 current
.lhs_type
= current
.tree_type
;
9618 /* If the binary operator required the use of an overloaded operator,
9619 then this expression cannot be an integral constant-expression.
9620 An overloaded operator can be used even if both operands are
9621 otherwise permissible in an integral constant-expression if at
9622 least one of the operands is of enumeration type. */
9625 && cp_parser_non_integral_constant_expression (parser
,
9627 return error_mark_node
;
9634 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9635 bool no_toplevel_fold_p
,
9636 enum cp_parser_prec prec
,
9639 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9640 /*decltype*/false, prec
, pidk
);
9643 /* Parse the `? expression : assignment-expression' part of a
9644 conditional-expression. The LOGICAL_OR_EXPR is the
9645 logical-or-expression that started the conditional-expression.
9646 Returns a representation of the entire conditional-expression.
9648 This routine is used by cp_parser_assignment_expression.
9650 ? expression : assignment-expression
9654 ? : assignment-expression */
9657 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9659 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9660 cp_expr assignment_expr
;
9661 struct cp_token
*token
;
9662 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9664 /* Consume the `?' token. */
9665 cp_lexer_consume_token (parser
->lexer
);
9666 token
= cp_lexer_peek_token (parser
->lexer
);
9667 if (cp_parser_allow_gnu_extensions_p (parser
)
9668 && token
->type
== CPP_COLON
)
9670 pedwarn (token
->location
, OPT_Wpedantic
,
9671 "ISO C++ does not allow ?: with omitted middle operand");
9672 /* Implicit true clause. */
9674 c_inhibit_evaluation_warnings
+=
9675 folded_logical_or_expr
== truthvalue_true_node
;
9676 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9680 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9681 parser
->colon_corrects_to_scope_p
= false;
9682 /* Parse the expression. */
9683 c_inhibit_evaluation_warnings
+=
9684 folded_logical_or_expr
== truthvalue_false_node
;
9685 expr
= cp_parser_expression (parser
);
9686 c_inhibit_evaluation_warnings
+=
9687 ((folded_logical_or_expr
== truthvalue_true_node
)
9688 - (folded_logical_or_expr
== truthvalue_false_node
));
9689 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9692 /* The next token should be a `:'. */
9693 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9694 /* Parse the assignment-expression. */
9695 assignment_expr
= cp_parser_assignment_expression (parser
);
9696 c_inhibit_evaluation_warnings
-=
9697 folded_logical_or_expr
== truthvalue_true_node
;
9700 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9701 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9702 with the caret at the "?", ranging from the start of
9703 the logical_or_expr to the end of the assignment_expr. */
9704 loc
= make_location (loc
,
9705 logical_or_expr
.get_start (),
9706 assignment_expr
.get_finish ());
9708 /* Build the conditional-expression. */
9709 return build_x_conditional_expr (loc
, logical_or_expr
,
9712 tf_warning_or_error
);
9715 /* Parse an assignment-expression.
9717 assignment-expression:
9718 conditional-expression
9719 logical-or-expression assignment-operator assignment_expression
9722 CAST_P is true if this expression is the target of a cast.
9723 DECLTYPE_P is true if this expression is the operand of decltype.
9725 Returns a representation for the expression. */
9728 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9729 bool cast_p
, bool decltype_p
)
9733 /* If the next token is the `throw' keyword, then we're looking at
9734 a throw-expression. */
9735 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9736 expr
= cp_parser_throw_expression (parser
);
9737 /* Otherwise, it must be that we are looking at a
9738 logical-or-expression. */
9741 /* Parse the binary expressions (logical-or-expression). */
9742 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9744 PREC_NOT_OPERATOR
, pidk
);
9745 /* If the next token is a `?' then we're actually looking at a
9746 conditional-expression. */
9747 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9748 return cp_parser_question_colon_clause (parser
, expr
);
9751 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9753 /* If it's an assignment-operator, we're using the second
9755 enum tree_code assignment_operator
9756 = cp_parser_assignment_operator_opt (parser
);
9757 if (assignment_operator
!= ERROR_MARK
)
9759 bool non_constant_p
;
9761 /* Parse the right-hand side of the assignment. */
9762 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9765 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9766 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9768 /* An assignment may not appear in a
9769 constant-expression. */
9770 if (cp_parser_non_integral_constant_expression (parser
,
9772 return error_mark_node
;
9773 /* Build the assignment expression. Its default
9777 is the location of the '=' token as the
9778 caret, ranging from the start of the lhs to the
9780 loc
= make_location (loc
,
9783 expr
= build_x_modify_expr (loc
, expr
,
9784 assignment_operator
,
9786 complain_flags (decltype_p
));
9787 /* TODO: build_x_modify_expr doesn't honor the location,
9788 so we must set it here. */
9789 expr
.set_location (loc
);
9797 /* Parse an (optional) assignment-operator.
9799 assignment-operator: one of
9800 = *= /= %= += -= >>= <<= &= ^= |=
9804 assignment-operator: one of
9807 If the next token is an assignment operator, the corresponding tree
9808 code is returned, and the token is consumed. For example, for
9809 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9810 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9811 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9812 operator, ERROR_MARK is returned. */
9814 static enum tree_code
9815 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9820 /* Peek at the next token. */
9821 token
= cp_lexer_peek_token (parser
->lexer
);
9823 switch (token
->type
)
9834 op
= TRUNC_DIV_EXPR
;
9838 op
= TRUNC_MOD_EXPR
;
9870 /* Nothing else is an assignment operator. */
9874 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9875 if (op
!= ERROR_MARK
9876 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9879 /* If it was an assignment operator, consume it. */
9880 if (op
!= ERROR_MARK
)
9881 cp_lexer_consume_token (parser
->lexer
);
9886 /* Parse an expression.
9889 assignment-expression
9890 expression , assignment-expression
9892 CAST_P is true if this expression is the target of a cast.
9893 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9894 except possibly parenthesized or on the RHS of a comma (N3276).
9896 Returns a representation of the expression. */
9899 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9900 bool cast_p
, bool decltype_p
)
9902 cp_expr expression
= NULL_TREE
;
9903 location_t loc
= UNKNOWN_LOCATION
;
9907 cp_expr assignment_expression
;
9909 /* Parse the next assignment-expression. */
9910 assignment_expression
9911 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9913 /* We don't create a temporary for a call that is the immediate operand
9914 of decltype or on the RHS of a comma. But when we see a comma, we
9915 need to create a temporary for a call on the LHS. */
9916 if (decltype_p
&& !processing_template_decl
9917 && TREE_CODE (assignment_expression
) == CALL_EXPR
9918 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9919 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9920 assignment_expression
9921 = build_cplus_new (TREE_TYPE (assignment_expression
),
9922 assignment_expression
, tf_warning_or_error
);
9924 /* If this is the first assignment-expression, we can just
9927 expression
= assignment_expression
;
9930 /* Create a location with caret at the comma, ranging
9931 from the start of the LHS to the end of the RHS. */
9932 loc
= make_location (loc
,
9933 expression
.get_start (),
9934 assignment_expression
.get_finish ());
9935 expression
= build_x_compound_expr (loc
, expression
,
9936 assignment_expression
,
9937 complain_flags (decltype_p
));
9938 expression
.set_location (loc
);
9940 /* If the next token is not a comma, or we're in a fold-expression, then
9941 we are done with the expression. */
9942 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9943 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9945 /* Consume the `,'. */
9946 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9947 cp_lexer_consume_token (parser
->lexer
);
9948 /* A comma operator cannot appear in a constant-expression. */
9949 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9950 expression
= error_mark_node
;
9956 /* Parse a constant-expression.
9958 constant-expression:
9959 conditional-expression
9961 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9962 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9963 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9964 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9965 only parse a conditional-expression, otherwise parse an
9966 assignment-expression. See below for rationale. */
9969 cp_parser_constant_expression (cp_parser
* parser
,
9970 bool allow_non_constant_p
,
9971 bool *non_constant_p
,
9974 bool saved_integral_constant_expression_p
;
9975 bool saved_allow_non_integral_constant_expression_p
;
9976 bool saved_non_integral_constant_expression_p
;
9979 /* It might seem that we could simply parse the
9980 conditional-expression, and then check to see if it were
9981 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9982 one that the compiler can figure out is constant, possibly after
9983 doing some simplifications or optimizations. The standard has a
9984 precise definition of constant-expression, and we must honor
9985 that, even though it is somewhat more restrictive.
9991 is not a legal declaration, because `(2, 3)' is not a
9992 constant-expression. The `,' operator is forbidden in a
9993 constant-expression. However, GCC's constant-folding machinery
9994 will fold this operation to an INTEGER_CST for `3'. */
9996 /* Save the old settings. */
9997 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9998 saved_allow_non_integral_constant_expression_p
9999 = parser
->allow_non_integral_constant_expression_p
;
10000 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
10001 /* We are now parsing a constant-expression. */
10002 parser
->integral_constant_expression_p
= true;
10003 parser
->allow_non_integral_constant_expression_p
10004 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
10005 parser
->non_integral_constant_expression_p
= false;
10006 /* Although the grammar says "conditional-expression", when not STRICT_P,
10007 we parse an "assignment-expression", which also permits
10008 "throw-expression" and the use of assignment operators. In the case
10009 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10010 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10011 actually essential that we look for an assignment-expression.
10012 For example, cp_parser_initializer_clauses uses this function to
10013 determine whether a particular assignment-expression is in fact
10017 /* Parse the binary expressions (logical-or-expression). */
10018 expression
= cp_parser_binary_expression (parser
, false, false, false,
10019 PREC_NOT_OPERATOR
, NULL
);
10020 /* If the next token is a `?' then we're actually looking at
10021 a conditional-expression; otherwise we're done. */
10022 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10023 expression
= cp_parser_question_colon_clause (parser
, expression
);
10026 expression
= cp_parser_assignment_expression (parser
);
10027 /* Restore the old settings. */
10028 parser
->integral_constant_expression_p
10029 = saved_integral_constant_expression_p
;
10030 parser
->allow_non_integral_constant_expression_p
10031 = saved_allow_non_integral_constant_expression_p
;
10032 if (cxx_dialect
>= cxx11
)
10034 /* Require an rvalue constant expression here; that's what our
10035 callers expect. Reference constant expressions are handled
10036 separately in e.g. cp_parser_template_argument. */
10037 tree decay
= expression
;
10038 if (TREE_TYPE (expression
)
10039 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
10040 decay
= build_address (expression
);
10041 bool is_const
= potential_rvalue_constant_expression (decay
);
10042 parser
->non_integral_constant_expression_p
= !is_const
;
10043 if (!is_const
&& !allow_non_constant_p
)
10044 require_potential_rvalue_constant_expression (decay
);
10046 if (allow_non_constant_p
)
10047 *non_constant_p
= parser
->non_integral_constant_expression_p
;
10048 parser
->non_integral_constant_expression_p
10049 = saved_non_integral_constant_expression_p
;
10054 /* Parse __builtin_offsetof.
10056 offsetof-expression:
10057 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10059 offsetof-member-designator:
10061 | offsetof-member-designator "." id-expression
10062 | offsetof-member-designator "[" expression "]"
10063 | offsetof-member-designator "->" id-expression */
10066 cp_parser_builtin_offsetof (cp_parser
*parser
)
10068 int save_ice_p
, save_non_ice_p
;
10073 location_t finish_loc
;
10075 /* We're about to accept non-integral-constant things, but will
10076 definitely yield an integral constant expression. Save and
10077 restore these values around our local parsing. */
10078 save_ice_p
= parser
->integral_constant_expression_p
;
10079 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10081 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10083 /* Consume the "__builtin_offsetof" token. */
10084 cp_lexer_consume_token (parser
->lexer
);
10085 /* Consume the opening `('. */
10086 matching_parens parens
;
10087 parens
.require_open (parser
);
10088 /* Parse the type-id. */
10089 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10091 const char *saved_message
= parser
->type_definition_forbidden_message
;
10092 parser
->type_definition_forbidden_message
10093 = G_("types may not be defined within __builtin_offsetof");
10094 type
= cp_parser_type_id (parser
);
10095 parser
->type_definition_forbidden_message
= saved_message
;
10097 /* Look for the `,'. */
10098 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10099 token
= cp_lexer_peek_token (parser
->lexer
);
10101 /* Build the (type *)null that begins the traditional offsetof macro. */
10103 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
10104 tf_warning_or_error
);
10106 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10107 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10108 true, &dummy
, token
->location
);
10111 token
= cp_lexer_peek_token (parser
->lexer
);
10112 switch (token
->type
)
10114 case CPP_OPEN_SQUARE
:
10115 /* offsetof-member-designator "[" expression "]" */
10116 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10121 /* offsetof-member-designator "->" identifier */
10122 expr
= grok_array_decl (token
->location
, expr
,
10123 integer_zero_node
, false);
10127 /* offsetof-member-designator "." identifier */
10128 cp_lexer_consume_token (parser
->lexer
);
10129 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10130 expr
, true, &dummy
,
10134 case CPP_CLOSE_PAREN
:
10135 /* Consume the ")" token. */
10136 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10137 cp_lexer_consume_token (parser
->lexer
);
10141 /* Error. We know the following require will fail, but
10142 that gives the proper error message. */
10143 parens
.require_close (parser
);
10144 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10145 expr
= error_mark_node
;
10151 /* Make a location of the form:
10152 __builtin_offsetof (struct s, f)
10153 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10154 with caret at the type-id, ranging from the start of the
10155 "_builtin_offsetof" token to the close paren. */
10156 loc
= make_location (loc
, start_loc
, finish_loc
);
10157 /* The result will be an INTEGER_CST, so we need to explicitly
10158 preserve the location. */
10159 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10162 parser
->integral_constant_expression_p
= save_ice_p
;
10163 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10165 expr
= expr
.maybe_add_location_wrapper ();
10169 /* Parse a trait expression.
10171 Returns a representation of the expression, the underlying type
10172 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10175 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10177 cp_trait_kind kind
;
10178 tree type1
, type2
= NULL_TREE
;
10179 bool binary
= false;
10180 bool variadic
= false;
10184 case RID_HAS_NOTHROW_ASSIGN
:
10185 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10187 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10188 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10190 case RID_HAS_NOTHROW_COPY
:
10191 kind
= CPTK_HAS_NOTHROW_COPY
;
10193 case RID_HAS_TRIVIAL_ASSIGN
:
10194 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10196 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10197 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10199 case RID_HAS_TRIVIAL_COPY
:
10200 kind
= CPTK_HAS_TRIVIAL_COPY
;
10202 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10203 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10205 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10206 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10208 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10209 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10211 case RID_IS_ABSTRACT
:
10212 kind
= CPTK_IS_ABSTRACT
;
10214 case RID_IS_AGGREGATE
:
10215 kind
= CPTK_IS_AGGREGATE
;
10217 case RID_IS_BASE_OF
:
10218 kind
= CPTK_IS_BASE_OF
;
10222 kind
= CPTK_IS_CLASS
;
10225 kind
= CPTK_IS_EMPTY
;
10228 kind
= CPTK_IS_ENUM
;
10231 kind
= CPTK_IS_FINAL
;
10233 case RID_IS_LITERAL_TYPE
:
10234 kind
= CPTK_IS_LITERAL_TYPE
;
10237 kind
= CPTK_IS_POD
;
10239 case RID_IS_POLYMORPHIC
:
10240 kind
= CPTK_IS_POLYMORPHIC
;
10242 case RID_IS_SAME_AS
:
10243 kind
= CPTK_IS_SAME_AS
;
10246 case RID_IS_STD_LAYOUT
:
10247 kind
= CPTK_IS_STD_LAYOUT
;
10249 case RID_IS_TRIVIAL
:
10250 kind
= CPTK_IS_TRIVIAL
;
10252 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10253 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10256 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10257 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10260 case RID_IS_TRIVIALLY_COPYABLE
:
10261 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10264 kind
= CPTK_IS_UNION
;
10266 case RID_UNDERLYING_TYPE
:
10267 kind
= CPTK_UNDERLYING_TYPE
;
10272 case RID_DIRECT_BASES
:
10273 kind
= CPTK_DIRECT_BASES
;
10275 case RID_IS_ASSIGNABLE
:
10276 kind
= CPTK_IS_ASSIGNABLE
;
10279 case RID_IS_CONSTRUCTIBLE
:
10280 kind
= CPTK_IS_CONSTRUCTIBLE
;
10284 gcc_unreachable ();
10287 /* Get location of initial token. */
10288 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10290 /* Consume the token. */
10291 cp_lexer_consume_token (parser
->lexer
);
10293 matching_parens parens
;
10294 parens
.require_open (parser
);
10297 type_id_in_expr_sentinel
s (parser
);
10298 type1
= cp_parser_type_id (parser
);
10301 if (type1
== error_mark_node
)
10302 return error_mark_node
;
10306 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10309 type_id_in_expr_sentinel
s (parser
);
10310 type2
= cp_parser_type_id (parser
);
10313 if (type2
== error_mark_node
)
10314 return error_mark_node
;
10318 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10320 cp_lexer_consume_token (parser
->lexer
);
10321 tree elt
= cp_parser_type_id (parser
);
10322 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10324 cp_lexer_consume_token (parser
->lexer
);
10325 elt
= make_pack_expansion (elt
);
10327 if (elt
== error_mark_node
)
10328 return error_mark_node
;
10329 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10333 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10334 parens
.require_close (parser
);
10336 /* Construct a location of the form:
10337 __is_trivially_copyable(_Tp)
10338 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10339 with start == caret, finishing at the close-paren. */
10340 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10342 /* Complete the trait expression, which may mean either processing
10343 the trait expr now or saving it for template instantiation. */
10346 case CPTK_UNDERLYING_TYPE
:
10347 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10349 return cp_expr (finish_bases (type1
, false), trait_loc
);
10350 case CPTK_DIRECT_BASES
:
10351 return cp_expr (finish_bases (type1
, true), trait_loc
);
10353 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10357 /* Parse a lambda expression.
10360 lambda-introducer lambda-declarator [opt] compound-statement
10362 Returns a representation of the expression. */
10365 cp_parser_lambda_expression (cp_parser
* parser
)
10367 tree lambda_expr
= build_lambda_expr ();
10370 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10371 cp_token_position start
= 0;
10373 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10375 if (cxx_dialect
>= cxx2a
)
10376 /* C++20 allows lambdas in unevaluated context. */;
10377 else if (cp_unevaluated_operand
)
10379 if (!token
->error_reported
)
10381 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10382 "lambda-expression in unevaluated context"
10383 " only available with -std=c++2a or -std=gnu++2a");
10384 token
->error_reported
= true;
10388 else if (parser
->in_template_argument_list_p
)
10390 if (!token
->error_reported
)
10392 error_at (token
->location
, "lambda-expression in template-argument"
10393 " only available with -std=c++2a or -std=gnu++2a");
10394 token
->error_reported
= true;
10399 /* We may be in the middle of deferred access check. Disable
10401 push_deferring_access_checks (dk_no_deferred
);
10403 cp_parser_lambda_introducer (parser
, lambda_expr
);
10404 if (cp_parser_error_occurred (parser
))
10405 return error_mark_node
;
10407 type
= begin_lambda_type (lambda_expr
);
10408 if (type
== error_mark_node
)
10409 return error_mark_node
;
10411 record_lambda_scope (lambda_expr
);
10413 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10414 determine_visibility (TYPE_NAME (type
));
10416 /* Now that we've started the type, add the capture fields for any
10417 explicit captures. */
10418 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10421 /* Inside the class, surrounding template-parameter-lists do not apply. */
10422 unsigned int saved_num_template_parameter_lists
10423 = parser
->num_template_parameter_lists
;
10424 unsigned char in_statement
= parser
->in_statement
;
10425 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10426 bool fully_implicit_function_template_p
10427 = parser
->fully_implicit_function_template_p
;
10428 tree implicit_template_parms
= parser
->implicit_template_parms
;
10429 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10430 bool auto_is_implicit_function_template_parm_p
10431 = parser
->auto_is_implicit_function_template_parm_p
;
10433 parser
->num_template_parameter_lists
= 0;
10434 parser
->in_statement
= 0;
10435 parser
->in_switch_statement_p
= false;
10436 parser
->fully_implicit_function_template_p
= false;
10437 parser
->implicit_template_parms
= 0;
10438 parser
->implicit_template_scope
= 0;
10439 parser
->auto_is_implicit_function_template_parm_p
= false;
10441 /* By virtue of defining a local class, a lambda expression has access to
10442 the private variables of enclosing classes. */
10444 if (cp_parser_start_tentative_firewall (parser
))
10447 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10449 if (ok
&& cp_parser_error_occurred (parser
))
10454 cp_parser_lambda_body (parser
, lambda_expr
);
10456 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10458 if (cp_parser_skip_to_closing_brace (parser
))
10459 cp_lexer_consume_token (parser
->lexer
);
10462 /* The capture list was built up in reverse order; fix that now. */
10463 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10464 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10467 maybe_add_lambda_conv_op (type
);
10469 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10471 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10472 parser
->in_statement
= in_statement
;
10473 parser
->in_switch_statement_p
= in_switch_statement_p
;
10474 parser
->fully_implicit_function_template_p
10475 = fully_implicit_function_template_p
;
10476 parser
->implicit_template_parms
= implicit_template_parms
;
10477 parser
->implicit_template_scope
= implicit_template_scope
;
10478 parser
->auto_is_implicit_function_template_parm_p
10479 = auto_is_implicit_function_template_parm_p
;
10482 /* This field is only used during parsing of the lambda. */
10483 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10485 /* This lambda shouldn't have any proxies left at this point. */
10486 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10487 /* And now that we're done, push proxies for an enclosing lambda. */
10488 insert_pending_capture_proxies ();
10490 /* Update the lambda expression to a range. */
10491 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10492 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10494 end_tok
->location
);
10497 lambda_expr
= build_lambda_object (lambda_expr
);
10499 lambda_expr
= error_mark_node
;
10501 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10503 pop_deferring_access_checks ();
10505 return lambda_expr
;
10508 /* Parse the beginning of a lambda expression.
10511 [ lambda-capture [opt] ]
10513 LAMBDA_EXPR is the current representation of the lambda expression. */
10516 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10518 /* Need commas after the first capture. */
10521 /* Eat the leading `['. */
10522 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10524 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10525 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10526 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10527 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10528 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10529 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10531 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10533 cp_lexer_consume_token (parser
->lexer
);
10536 if (!(at_function_scope_p () || parsing_nsdmi ()))
10537 error ("non-local lambda expression cannot have a capture-default");
10540 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10542 cp_token
* capture_token
;
10544 tree capture_init_expr
;
10545 cp_id_kind idk
= CP_ID_KIND_NONE
;
10546 bool explicit_init_p
= false;
10548 enum capture_kind_type
10553 enum capture_kind_type capture_kind
= BY_COPY
;
10555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10557 error ("expected end of capture-list");
10564 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10566 /* Possibly capture `this'. */
10567 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10569 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10570 if (cxx_dialect
< cxx2a
10571 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10572 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10573 "with by-copy capture default");
10574 cp_lexer_consume_token (parser
->lexer
);
10575 add_capture (lambda_expr
,
10576 /*id=*/this_identifier
,
10577 /*initializer=*/finish_this_expr (),
10578 /*by_reference_p=*/true,
10583 /* Possibly capture `*this'. */
10584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10585 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10587 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10588 if (cxx_dialect
< cxx17
)
10589 pedwarn (loc
, 0, "%<*this%> capture only available with "
10590 "-std=c++17 or -std=gnu++17");
10591 cp_lexer_consume_token (parser
->lexer
);
10592 cp_lexer_consume_token (parser
->lexer
);
10593 add_capture (lambda_expr
,
10594 /*id=*/this_identifier
,
10595 /*initializer=*/finish_this_expr (),
10596 /*by_reference_p=*/false,
10601 bool init_pack_expansion
= false;
10602 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10604 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10605 if (cxx_dialect
< cxx2a
)
10606 pedwarn (loc
, 0, "pack init-capture only available with "
10607 "-std=c++2a or -std=gnu++2a");
10608 cp_lexer_consume_token (parser
->lexer
);
10609 init_pack_expansion
= true;
10612 /* Remember whether we want to capture as a reference or not. */
10613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10615 capture_kind
= BY_REFERENCE
;
10616 cp_lexer_consume_token (parser
->lexer
);
10619 /* Get the identifier. */
10620 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10621 capture_id
= cp_parser_identifier (parser
);
10623 if (capture_id
== error_mark_node
)
10624 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10625 delimiters, but I modified this to stop on unnested ']' as well. It
10626 was already changed to stop on unnested '}', so the
10627 "closing_parenthesis" name is no more misleading with my change. */
10629 cp_parser_skip_to_closing_parenthesis (parser
,
10630 /*recovering=*/true,
10632 /*consume_paren=*/true);
10636 /* Find the initializer for this capture. */
10637 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10638 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10639 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10641 bool direct
, non_constant
;
10642 /* An explicit initializer exists. */
10643 if (cxx_dialect
< cxx14
)
10644 pedwarn (input_location
, 0,
10645 "lambda capture initializers "
10646 "only available with -std=c++14 or -std=gnu++14");
10647 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10648 &non_constant
, true);
10649 explicit_init_p
= true;
10650 if (capture_init_expr
== NULL_TREE
)
10652 error ("empty initializer for lambda init-capture");
10653 capture_init_expr
= error_mark_node
;
10655 if (init_pack_expansion
)
10656 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10660 const char* error_msg
;
10662 /* Turn the identifier into an id-expression. */
10664 = cp_parser_lookup_name_simple (parser
, capture_id
,
10665 capture_token
->location
);
10667 if (capture_init_expr
== error_mark_node
)
10669 unqualified_name_lookup_error (capture_id
);
10672 else if (!VAR_P (capture_init_expr
)
10673 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10675 error_at (capture_token
->location
,
10676 "capture of non-variable %qE",
10677 capture_init_expr
);
10678 if (DECL_P (capture_init_expr
))
10679 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10680 "%q#D declared here", capture_init_expr
);
10683 if (VAR_P (capture_init_expr
)
10684 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10686 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10687 "%qD with non-automatic storage duration",
10688 capture_init_expr
))
10689 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10690 "%q#D declared here", capture_init_expr
);
10695 = finish_id_expression
10700 /*integral_constant_expression_p=*/false,
10701 /*allow_non_integral_constant_expression_p=*/false,
10702 /*non_integral_constant_expression_p=*/NULL
,
10703 /*template_p=*/false,
10705 /*address_p=*/false,
10706 /*template_arg_p=*/false,
10708 capture_token
->location
);
10710 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10712 cp_lexer_consume_token (parser
->lexer
);
10713 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10717 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10718 && !explicit_init_p
)
10720 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10721 && capture_kind
== BY_COPY
)
10722 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10723 "of %qD redundant with by-copy capture default",
10725 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10726 && capture_kind
== BY_REFERENCE
)
10727 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10728 "capture of %qD redundant with by-reference capture "
10729 "default", capture_id
);
10732 add_capture (lambda_expr
,
10735 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10738 /* If there is any qualification still in effect, clear it
10739 now; we will be starting fresh with the next capture. */
10740 parser
->scope
= NULL_TREE
;
10741 parser
->qualifying_scope
= NULL_TREE
;
10742 parser
->object_scope
= NULL_TREE
;
10745 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10748 /* Parse the (optional) middle of a lambda expression.
10751 < template-parameter-list [opt] >
10752 ( parameter-declaration-clause [opt] )
10753 attribute-specifier [opt]
10754 decl-specifier-seq [opt]
10755 exception-specification [opt]
10756 lambda-return-type-clause [opt]
10758 LAMBDA_EXPR is the current representation of the lambda expression. */
10761 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10763 /* 5.1.1.4 of the standard says:
10764 If a lambda-expression does not include a lambda-declarator, it is as if
10765 the lambda-declarator were ().
10766 This means an empty parameter list, no attributes, and no exception
10768 tree param_list
= void_list_node
;
10769 tree attributes
= NULL_TREE
;
10770 tree exception_spec
= NULL_TREE
;
10771 tree template_param_list
= NULL_TREE
;
10772 tree tx_qual
= NULL_TREE
;
10773 tree return_type
= NULL_TREE
;
10774 cp_decl_specifier_seq lambda_specs
;
10775 clear_decl_specs (&lambda_specs
);
10777 /* The template-parameter-list is optional, but must begin with
10778 an opening angle if present. */
10779 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10781 if (cxx_dialect
< cxx14
)
10782 pedwarn (parser
->lexer
->next_token
->location
, 0,
10783 "lambda templates are only available with "
10784 "-std=c++14 or -std=gnu++14");
10785 else if (cxx_dialect
< cxx2a
)
10786 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10787 "lambda templates are only available with "
10788 "-std=c++2a or -std=gnu++2a");
10790 cp_lexer_consume_token (parser
->lexer
);
10792 template_param_list
= cp_parser_template_parameter_list (parser
);
10794 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10796 /* We just processed one more parameter list. */
10797 ++parser
->num_template_parameter_lists
;
10800 /* The parameter-declaration-clause is optional (unless
10801 template-parameter-list was given), but must begin with an
10802 opening parenthesis if present. */
10803 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10805 matching_parens parens
;
10806 parens
.consume_open (parser
);
10808 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10810 /* Parse parameters. */
10812 = cp_parser_parameter_declaration_clause
10813 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
);
10815 /* Default arguments shall not be specified in the
10816 parameter-declaration-clause of a lambda-declarator. */
10817 if (cxx_dialect
< cxx14
)
10818 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10819 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10820 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10821 "default argument specified for lambda parameter");
10823 parens
.require_close (parser
);
10825 /* In the decl-specifier-seq of the lambda-declarator, each
10826 decl-specifier shall either be mutable or constexpr. */
10827 int declares_class_or_enum
;
10828 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10829 cp_parser_decl_specifier_seq (parser
,
10830 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10831 &lambda_specs
, &declares_class_or_enum
);
10832 if (lambda_specs
.storage_class
== sc_mutable
)
10834 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10835 if (lambda_specs
.conflicting_specifiers_p
)
10836 error_at (lambda_specs
.locations
[ds_storage_class
],
10837 "duplicate %<mutable%>");
10840 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10842 /* Parse optional exception specification. */
10843 exception_spec
= cp_parser_exception_specification_opt (parser
);
10845 attributes
= cp_parser_std_attribute_spec_seq (parser
);
10847 /* Parse optional trailing return type. */
10848 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10850 cp_lexer_consume_token (parser
->lexer
);
10851 return_type
= cp_parser_trailing_type_id (parser
);
10854 /* The function parameters must be in scope all the way until after the
10855 trailing-return-type in case of decltype. */
10856 pop_bindings_and_leave_scope ();
10858 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10859 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10861 /* Create the function call operator.
10863 Messing with declarators like this is no uglier than building up the
10864 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10867 cp_decl_specifier_seq return_type_specs
;
10868 cp_declarator
* declarator
;
10873 clear_decl_specs (&return_type_specs
);
10874 return_type_specs
.type
= make_auto ();
10876 if (lambda_specs
.locations
[ds_constexpr
])
10878 if (cxx_dialect
>= cxx17
)
10879 return_type_specs
.locations
[ds_constexpr
]
10880 = lambda_specs
.locations
[ds_constexpr
];
10882 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10883 "lambda only available with -std=c++17 or -std=gnu++17");
10886 p
= obstack_alloc (&declarator_obstack
, 0);
10888 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
10889 LAMBDA_EXPR_LOCATION (lambda_expr
));
10891 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10892 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10893 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10894 VIRT_SPEC_UNSPECIFIED
,
10899 /*requires_clause*/NULL_TREE
);
10900 declarator
->std_attributes
= attributes
;
10902 fco
= grokmethod (&return_type_specs
,
10905 if (fco
!= error_mark_node
)
10907 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10908 DECL_ARTIFICIAL (fco
) = 1;
10909 /* Give the object parameter a different name. */
10910 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10911 DECL_LAMBDA_FUNCTION (fco
) = 1;
10913 if (template_param_list
)
10915 fco
= finish_member_template_decl (fco
);
10916 finish_template_decl (template_param_list
);
10917 --parser
->num_template_parameter_lists
;
10919 else if (parser
->fully_implicit_function_template_p
)
10920 fco
= finish_fully_implicit_template (parser
, fco
);
10922 finish_member_declaration (fco
);
10924 obstack_free (&declarator_obstack
, p
);
10926 return (fco
!= error_mark_node
);
10930 /* Parse the body of a lambda expression, which is simply
10934 but which requires special handling.
10935 LAMBDA_EXPR is the current representation of the lambda expression. */
10938 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10940 bool nested
= (current_function_decl
!= NULL_TREE
);
10941 unsigned char local_variables_forbidden_p
10942 = parser
->local_variables_forbidden_p
;
10943 bool in_function_body
= parser
->in_function_body
;
10945 /* The body of a lambda-expression is not a subexpression of the enclosing
10950 push_function_context ();
10952 /* Still increment function_depth so that we don't GC in the
10953 middle of an expression. */
10956 vec
<tree
> omp_privatization_save
;
10957 save_omp_privatization_clauses (omp_privatization_save
);
10958 /* Clear this in case we're in the middle of a default argument. */
10959 parser
->local_variables_forbidden_p
= 0;
10960 parser
->in_function_body
= true;
10963 local_specialization_stack
s (lss_copy
);
10964 tree fco
= lambda_function (lambda_expr
);
10965 tree body
= start_lambda_function (fco
, lambda_expr
);
10966 matching_braces braces
;
10968 if (braces
.require_open (parser
))
10970 tree compound_stmt
= begin_compound_stmt (0);
10972 /* Originally C++11 required us to peek for 'return expr'; and
10973 process it specially here to deduce the return type. N3638
10974 removed the need for that. */
10976 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10977 cp_parser_label_declaration (parser
);
10978 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10979 braces
.require_close (parser
);
10981 finish_compound_stmt (compound_stmt
);
10984 finish_lambda_function (body
);
10987 restore_omp_privatization_clauses (omp_privatization_save
);
10988 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10989 parser
->in_function_body
= in_function_body
;
10991 pop_function_context();
10996 /* Statements [gram.stmt.stmt] */
10998 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11001 add_debug_begin_stmt (location_t loc
)
11003 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
11005 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
11006 /* A concept is never expanded normally. */
11009 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
11010 SET_EXPR_LOCATION (stmt
, loc
);
11014 /* Parse a statement.
11018 expression-statement
11020 selection-statement
11021 iteration-statement
11023 declaration-statement
11030 attribute-specifier-seq (opt) expression-statement
11031 attribute-specifier-seq (opt) compound-statement
11032 attribute-specifier-seq (opt) selection-statement
11033 attribute-specifier-seq (opt) iteration-statement
11034 attribute-specifier-seq (opt) jump-statement
11035 declaration-statement
11036 attribute-specifier-seq (opt) try-block
11039 expression-statement
11047 IN_COMPOUND is true when the statement is nested inside a
11048 cp_parser_compound_statement; this matters for certain pragmas.
11050 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11051 is a (possibly labeled) if statement which is not enclosed in braces
11052 and has an else clause. This is used to implement -Wparentheses.
11054 CHAIN is a vector of if-else-if conditions. */
11057 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
11058 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
11059 location_t
*loc_after_labels
)
11061 tree statement
, std_attrs
= NULL_TREE
;
11063 location_t statement_location
, attrs_loc
;
11068 /* There is no statement yet. */
11069 statement
= NULL_TREE
;
11071 saved_token_sentinel
saved_tokens (parser
->lexer
);
11072 attrs_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11073 if (c_dialect_objc ())
11074 /* In obj-c++, seeing '[[' might be the either the beginning of
11075 c++11 attributes, or a nested objc-message-expression. So
11076 let's parse the c++11 attributes tentatively. */
11077 cp_parser_parse_tentatively (parser
);
11078 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11082 = cp_lexer_previous_token (parser
->lexer
)->location
;
11083 attrs_loc
= make_location (attrs_loc
, attrs_loc
, end_loc
);
11085 if (c_dialect_objc ())
11087 if (!cp_parser_parse_definitely (parser
))
11088 std_attrs
= NULL_TREE
;
11091 /* Peek at the next token. */
11092 token
= cp_lexer_peek_token (parser
->lexer
);
11093 /* Remember the location of the first token in the statement. */
11094 cp_token
*statement_token
= token
;
11095 statement_location
= token
->location
;
11096 add_debug_begin_stmt (statement_location
);
11097 /* If this is a keyword, then that will often determine what kind of
11098 statement we have. */
11099 if (token
->type
== CPP_KEYWORD
)
11101 enum rid keyword
= token
->keyword
;
11107 /* Looks like a labeled-statement with a case label.
11108 Parse the label, and then use tail recursion to parse
11110 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11111 in_compound
= false;
11116 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11117 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
11123 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11124 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
11131 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11132 statement
= cp_parser_jump_statement (parser
);
11135 /* Objective-C++ exception-handling constructs. */
11138 case RID_AT_FINALLY
:
11139 case RID_AT_SYNCHRONIZED
:
11141 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11142 statement
= cp_parser_objc_statement (parser
);
11146 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11147 statement
= cp_parser_try_block (parser
);
11150 case RID_NAMESPACE
:
11151 /* This must be a namespace alias definition. */
11152 if (std_attrs
!= NULL_TREE
)
11154 /* Attributes should be parsed as part of the the
11155 declaration, so let's un-parse them. */
11156 saved_tokens
.rollback();
11157 std_attrs
= NULL_TREE
;
11159 cp_parser_declaration_statement (parser
);
11162 case RID_TRANSACTION_ATOMIC
:
11163 case RID_TRANSACTION_RELAXED
:
11164 case RID_SYNCHRONIZED
:
11165 case RID_ATOMIC_NOEXCEPT
:
11166 case RID_ATOMIC_CANCEL
:
11167 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11168 statement
= cp_parser_transaction (parser
, token
);
11170 case RID_TRANSACTION_CANCEL
:
11171 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11172 statement
= cp_parser_transaction_cancel (parser
);
11176 /* It might be a keyword like `int' that can start a
11177 declaration-statement. */
11181 else if (token
->type
== CPP_NAME
)
11183 /* If the next token is a `:', then we are looking at a
11184 labeled-statement. */
11185 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11186 if (token
->type
== CPP_COLON
)
11188 /* Looks like a labeled-statement with an ordinary label.
11189 Parse the label, and then use tail recursion to parse
11192 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11193 in_compound
= false;
11197 /* Anything that starts with a `{' must be a compound-statement. */
11198 else if (token
->type
== CPP_OPEN_BRACE
)
11199 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11200 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11201 a statement all its own. */
11202 else if (token
->type
== CPP_PRAGMA
)
11204 /* Only certain OpenMP pragmas are attached to statements, and thus
11205 are considered statements themselves. All others are not. In
11206 the context of a compound, accept the pragma as a "statement" and
11207 return so that we can check for a close brace. Otherwise we
11208 require a real statement and must go back and read one. */
11210 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11211 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11215 else if (token
->type
== CPP_EOF
)
11217 cp_parser_error (parser
, "expected statement");
11221 /* Everything else must be a declaration-statement or an
11222 expression-statement. Try for the declaration-statement
11223 first, unless we are looking at a `;', in which case we know that
11224 we have an expression-statement. */
11227 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11229 if (std_attrs
!= NULL_TREE
)
11230 /* Attributes should be parsed as part of the declaration,
11231 so let's un-parse them. */
11232 saved_tokens
.rollback();
11234 cp_parser_parse_tentatively (parser
);
11235 /* Try to parse the declaration-statement. */
11236 cp_parser_declaration_statement (parser
);
11237 /* If that worked, we're done. */
11238 if (cp_parser_parse_definitely (parser
))
11240 /* It didn't work, restore the post-attribute position. */
11242 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11244 /* All preceding labels have been parsed at this point. */
11245 if (loc_after_labels
!= NULL
)
11246 *loc_after_labels
= statement_location
;
11248 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11250 /* Look for an expression-statement instead. */
11251 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11253 /* Handle [[fallthrough]];. */
11254 if (attribute_fallthrough_p (std_attrs
))
11256 /* The next token after the fallthrough attribute is ';'. */
11257 if (statement
== NULL_TREE
)
11259 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11260 statement
= build_call_expr_internal_loc (statement_location
,
11262 void_type_node
, 0);
11263 finish_expr_stmt (statement
);
11266 warning_at (statement_location
, OPT_Wattributes
,
11267 "%<fallthrough%> attribute not followed by %<;%>");
11268 std_attrs
= NULL_TREE
;
11272 /* Set the line number for the statement. */
11273 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11274 SET_EXPR_LOCATION (statement
, statement_location
);
11276 /* Allow "[[fallthrough]];", but warn otherwise. */
11277 if (std_attrs
!= NULL_TREE
)
11278 warning_at (attrs_loc
,
11280 "attributes at the beginning of statement are ignored");
11283 /* Append ATTR to attribute list ATTRS. */
11286 attr_chainon (tree attrs
, tree attr
)
11288 if (attrs
== error_mark_node
)
11289 return error_mark_node
;
11290 if (attr
== error_mark_node
)
11291 return error_mark_node
;
11292 return chainon (attrs
, attr
);
11295 /* Parse the label for a labeled-statement, i.e.
11298 case constant-expression :
11302 case constant-expression ... constant-expression : statement
11304 When a label is parsed without errors, the label is added to the
11305 parse tree by the finish_* functions, so this function doesn't
11306 have to return the label. */
11309 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11312 tree label
= NULL_TREE
;
11313 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11315 /* The next token should be an identifier. */
11316 token
= cp_lexer_peek_token (parser
->lexer
);
11317 if (token
->type
!= CPP_NAME
11318 && token
->type
!= CPP_KEYWORD
)
11320 cp_parser_error (parser
, "expected labeled-statement");
11324 /* Remember whether this case or a user-defined label is allowed to fall
11326 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11328 parser
->colon_corrects_to_scope_p
= false;
11329 switch (token
->keyword
)
11333 tree expr
, expr_hi
;
11334 cp_token
*ellipsis
;
11336 /* Consume the `case' token. */
11337 cp_lexer_consume_token (parser
->lexer
);
11338 /* Parse the constant-expression. */
11339 expr
= cp_parser_constant_expression (parser
);
11340 if (check_for_bare_parameter_packs (expr
))
11341 expr
= error_mark_node
;
11343 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11344 if (ellipsis
->type
== CPP_ELLIPSIS
)
11346 /* Consume the `...' token. */
11347 cp_lexer_consume_token (parser
->lexer
);
11348 expr_hi
= cp_parser_constant_expression (parser
);
11349 if (check_for_bare_parameter_packs (expr_hi
))
11350 expr_hi
= error_mark_node
;
11352 /* We don't need to emit warnings here, as the common code
11353 will do this for us. */
11356 expr_hi
= NULL_TREE
;
11358 if (parser
->in_switch_statement_p
)
11360 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11361 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11363 label
= CASE_LABEL (l
);
11364 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11368 error_at (token
->location
,
11369 "case label %qE not within a switch statement",
11375 /* Consume the `default' token. */
11376 cp_lexer_consume_token (parser
->lexer
);
11378 if (parser
->in_switch_statement_p
)
11380 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11381 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11383 label
= CASE_LABEL (l
);
11384 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11388 error_at (token
->location
, "case label not within a switch statement");
11392 /* Anything else must be an ordinary label. */
11393 label
= finish_label_stmt (cp_parser_identifier (parser
));
11394 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11395 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11399 /* Require the `:' token. */
11400 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11402 /* An ordinary label may optionally be followed by attributes.
11403 However, this is only permitted if the attributes are then
11404 followed by a semicolon. This is because, for backward
11405 compatibility, when parsing
11406 lab: __attribute__ ((unused)) int i;
11407 we want the attribute to attach to "i", not "lab". */
11408 if (label
!= NULL_TREE
11409 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11412 cp_parser_parse_tentatively (parser
);
11413 attrs
= cp_parser_gnu_attributes_opt (parser
);
11414 if (attrs
== NULL_TREE
11415 /* And fallthrough always binds to the expression-statement. */
11416 || attribute_fallthrough_p (attrs
)
11417 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11418 cp_parser_abort_tentative_parse (parser
);
11419 else if (!cp_parser_parse_definitely (parser
))
11422 attributes
= attr_chainon (attributes
, attrs
);
11425 if (attributes
!= NULL_TREE
)
11426 cplus_decl_attributes (&label
, attributes
, 0);
11428 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11431 /* Parse an expression-statement.
11433 expression-statement:
11436 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11437 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11438 indicates whether this expression-statement is part of an
11439 expression statement. */
11442 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11444 tree statement
= NULL_TREE
;
11445 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11446 location_t loc
= token
->location
;
11448 /* There might be attribute fallthrough. */
11449 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11451 /* If the next token is a ';', then there is no expression
11453 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11455 statement
= cp_parser_expression (parser
);
11456 if (statement
== error_mark_node
11457 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11459 cp_parser_skip_to_end_of_block_or_statement (parser
);
11460 return error_mark_node
;
11464 /* Handle [[fallthrough]];. */
11465 if (attribute_fallthrough_p (attr
))
11467 /* The next token after the fallthrough attribute is ';'. */
11468 if (statement
== NULL_TREE
)
11469 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11470 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11471 void_type_node
, 0);
11473 warning_at (loc
, OPT_Wattributes
,
11474 "%<fallthrough%> attribute not followed by %<;%>");
11478 /* Allow "[[fallthrough]];", but warn otherwise. */
11479 if (attr
!= NULL_TREE
)
11480 warning_at (loc
, OPT_Wattributes
,
11481 "attributes at the beginning of statement are ignored");
11483 /* Give a helpful message for "A<T>::type t;" and the like. */
11484 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11485 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11487 if (TREE_CODE (statement
) == SCOPE_REF
)
11488 error_at (token
->location
, "need %<typename%> before %qE because "
11489 "%qT is a dependent scope",
11490 statement
, TREE_OPERAND (statement
, 0));
11491 else if (is_overloaded_fn (statement
)
11492 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11495 tree fn
= get_first_fn (statement
);
11496 error_at (token
->location
,
11497 "%<%T::%D%> names the constructor, not the type",
11498 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11502 /* Consume the final `;'. */
11503 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11505 if (in_statement_expr
11506 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11507 /* This is the final expression statement of a statement
11509 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11510 else if (statement
)
11511 statement
= finish_expr_stmt (statement
);
11516 /* Parse a compound-statement.
11518 compound-statement:
11519 { statement-seq [opt] }
11523 compound-statement:
11524 { label-declaration-seq [opt] statement-seq [opt] }
11526 label-declaration-seq:
11528 label-declaration-seq label-declaration
11530 Returns a tree representing the statement. */
11533 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11534 int bcs_flags
, bool function_body
)
11536 tree compound_stmt
;
11537 matching_braces braces
;
11539 /* Consume the `{'. */
11540 if (!braces
.require_open (parser
))
11541 return error_mark_node
;
11542 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11543 && !function_body
&& cxx_dialect
< cxx14
)
11544 pedwarn (input_location
, OPT_Wpedantic
,
11545 "compound-statement in %<constexpr%> function");
11546 /* Begin the compound-statement. */
11547 compound_stmt
= begin_compound_stmt (bcs_flags
);
11548 /* If the next keyword is `__label__' we have a label declaration. */
11549 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11550 cp_parser_label_declaration (parser
);
11551 /* Parse an (optional) statement-seq. */
11552 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11553 /* Finish the compound-statement. */
11554 finish_compound_stmt (compound_stmt
);
11555 /* Consume the `}'. */
11556 braces
.require_close (parser
);
11558 return compound_stmt
;
11561 /* Parse an (optional) statement-seq.
11565 statement-seq [opt] statement */
11568 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11570 /* Scan statements until there aren't any more. */
11573 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11575 /* If we are looking at a `}', then we have run out of
11576 statements; the same is true if we have reached the end
11577 of file, or have stumbled upon a stray '@end'. */
11578 if (token
->type
== CPP_CLOSE_BRACE
11579 || token
->type
== CPP_EOF
11580 || token
->type
== CPP_PRAGMA_EOL
11581 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11584 /* If we are in a compound statement and find 'else' then
11585 something went wrong. */
11586 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11588 if (parser
->in_statement
& IN_IF_STMT
)
11592 token
= cp_lexer_consume_token (parser
->lexer
);
11593 error_at (token
->location
, "%<else%> without a previous %<if%>");
11597 /* Parse the statement. */
11598 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11602 /* Return true if this is the C++20 version of range-based-for with
11606 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11610 /* Save tokens so that we can put them back. */
11611 cp_lexer_save_tokens (parser
->lexer
);
11613 /* There has to be an unnested ; followed by an unnested :. */
11614 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11615 /*recovering=*/false,
11617 /*consume_paren=*/false) != -1)
11620 /* We found the semicolon, eat it now. */
11621 cp_lexer_consume_token (parser
->lexer
);
11623 /* Now look for ':' that is not nested in () or {}. */
11624 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11625 /*recovering=*/false,
11627 /*consume_paren=*/false) == -1);
11630 /* Roll back the tokens we skipped. */
11631 cp_lexer_rollback_tokens (parser
->lexer
);
11636 /* Return true if we're looking at (init; cond), false otherwise. */
11639 cp_parser_init_statement_p (cp_parser
*parser
)
11641 /* Save tokens so that we can put them back. */
11642 cp_lexer_save_tokens (parser
->lexer
);
11644 /* Look for ';' that is not nested in () or {}. */
11645 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11646 /*recovering=*/false,
11648 /*consume_paren=*/false);
11650 /* Roll back the tokens we skipped. */
11651 cp_lexer_rollback_tokens (parser
->lexer
);
11656 /* Parse a selection-statement.
11658 selection-statement:
11659 if ( init-statement [opt] condition ) statement
11660 if ( init-statement [opt] condition ) statement else statement
11661 switch ( init-statement [opt] condition ) statement
11663 Returns the new IF_STMT or SWITCH_STMT.
11665 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11666 is a (possibly labeled) if statement which is not enclosed in
11667 braces and has an else clause. This is used to implement
11670 CHAIN is a vector of if-else-if conditions. This is used to implement
11671 -Wduplicated-cond. */
11674 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11679 token_indent_info guard_tinfo
;
11684 /* Peek at the next token. */
11685 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11686 guard_tinfo
= get_token_indent_info (token
);
11688 /* See what kind of keyword it is. */
11689 keyword
= token
->keyword
;
11699 if (keyword
== RID_IF
11700 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11704 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11705 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11706 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11707 "with -std=c++17 or -std=gnu++17");
11710 /* Look for the `('. */
11711 matching_parens parens
;
11712 if (!parens
.require_open (parser
))
11714 cp_parser_skip_to_end_of_statement (parser
);
11715 return error_mark_node
;
11718 /* Begin the selection-statement. */
11719 if (keyword
== RID_IF
)
11721 statement
= begin_if_stmt ();
11722 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11725 statement
= begin_switch_stmt ();
11727 /* Parse the optional init-statement. */
11728 if (cp_parser_init_statement_p (parser
))
11731 if (cxx_dialect
< cxx17
)
11732 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11733 "init-statement in selection statements only available "
11734 "with -std=c++17 or -std=gnu++17");
11735 cp_parser_init_statement (parser
, &decl
);
11738 /* Parse the condition. */
11739 condition
= cp_parser_condition (parser
);
11740 /* Look for the `)'. */
11741 if (!parens
.require_close (parser
))
11742 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11743 /*consume_paren=*/true);
11745 if (keyword
== RID_IF
)
11748 unsigned char in_statement
;
11750 /* Add the condition. */
11751 condition
= finish_if_stmt_cond (condition
, statement
);
11753 if (warn_duplicated_cond
)
11754 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11757 /* Parse the then-clause. */
11758 in_statement
= parser
->in_statement
;
11759 parser
->in_statement
|= IN_IF_STMT
;
11761 /* Outside a template, the non-selected branch of a constexpr
11762 if is a 'discarded statement', i.e. unevaluated. */
11763 bool was_discarded
= in_discarded_stmt
;
11764 bool discard_then
= (cx
&& !processing_template_decl
11765 && integer_zerop (condition
));
11768 in_discarded_stmt
= true;
11769 ++c_inhibit_evaluation_warnings
;
11772 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11775 parser
->in_statement
= in_statement
;
11777 finish_then_clause (statement
);
11781 THEN_CLAUSE (statement
) = NULL_TREE
;
11782 in_discarded_stmt
= was_discarded
;
11783 --c_inhibit_evaluation_warnings
;
11786 /* If the next token is `else', parse the else-clause. */
11787 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11790 bool discard_else
= (cx
&& !processing_template_decl
11791 && integer_nonzerop (condition
));
11794 in_discarded_stmt
= true;
11795 ++c_inhibit_evaluation_warnings
;
11799 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11800 /* Consume the `else' keyword. */
11801 cp_lexer_consume_token (parser
->lexer
);
11802 if (warn_duplicated_cond
)
11804 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11808 /* We've got "if (COND) else if (COND2)". Start
11809 the condition chain and add COND as the first
11811 chain
= new vec
<tree
> ();
11812 if (!CONSTANT_CLASS_P (condition
)
11813 && !TREE_SIDE_EFFECTS (condition
))
11815 /* Wrap it in a NOP_EXPR so that we can set the
11816 location of the condition. */
11817 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11819 SET_EXPR_LOCATION (e
, token
->location
);
11820 chain
->safe_push (e
);
11823 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11826 /* This is if-else without subsequent if. Zap the
11827 condition chain; we would have already warned at
11833 begin_else_clause (statement
);
11834 /* Parse the else-clause. */
11835 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11836 guard_tinfo
, chain
);
11838 finish_else_clause (statement
);
11840 /* If we are currently parsing a then-clause, then
11841 IF_P will not be NULL. We set it to true to
11842 indicate that this if statement has an else clause.
11843 This may trigger the Wparentheses warning below
11844 when we get back up to the parent if statement. */
11850 ELSE_CLAUSE (statement
) = NULL_TREE
;
11851 in_discarded_stmt
= was_discarded
;
11852 --c_inhibit_evaluation_warnings
;
11857 /* This if statement does not have an else clause. If
11858 NESTED_IF is true, then the then-clause has an if
11859 statement which does have an else clause. We warn
11860 about the potential ambiguity. */
11862 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11863 "suggest explicit braces to avoid ambiguous"
11865 if (warn_duplicated_cond
)
11867 /* We don't need the condition chain anymore. */
11873 /* Now we're all done with the if-statement. */
11874 finish_if_stmt (statement
);
11878 bool in_switch_statement_p
;
11879 unsigned char in_statement
;
11881 /* Add the condition. */
11882 finish_switch_cond (condition
, statement
);
11884 /* Parse the body of the switch-statement. */
11885 in_switch_statement_p
= parser
->in_switch_statement_p
;
11886 in_statement
= parser
->in_statement
;
11887 parser
->in_switch_statement_p
= true;
11888 parser
->in_statement
|= IN_SWITCH_STMT
;
11889 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11891 parser
->in_switch_statement_p
= in_switch_statement_p
;
11892 parser
->in_statement
= in_statement
;
11894 /* Now we're all done with the switch-statement. */
11895 finish_switch_stmt (statement
);
11903 cp_parser_error (parser
, "expected selection-statement");
11904 return error_mark_node
;
11908 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11909 If we have seen at least one decl-specifier, and the next token
11910 is not a parenthesis, then we must be looking at a declaration.
11911 (After "int (" we might be looking at a functional cast.) */
11914 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11915 bool any_specifiers_p
)
11917 if (any_specifiers_p
11918 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11919 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11920 && !cp_parser_error_occurred (parser
))
11921 cp_parser_commit_to_tentative_parse (parser
);
11924 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11925 The declarator shall not specify a function or an array. Returns
11926 TRUE if the declarator is valid, FALSE otherwise. */
11929 cp_parser_check_condition_declarator (cp_parser
* parser
,
11930 cp_declarator
*declarator
,
11933 if (declarator
== cp_error_declarator
11934 || function_declarator_p (declarator
)
11935 || declarator
->kind
== cdk_array
)
11937 if (declarator
== cp_error_declarator
)
11938 /* Already complained. */;
11939 else if (declarator
->kind
== cdk_array
)
11940 error_at (loc
, "condition declares an array");
11942 error_at (loc
, "condition declares a function");
11943 if (parser
->fully_implicit_function_template_p
)
11944 abort_fully_implicit_template (parser
);
11945 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11946 /*or_comma=*/false,
11947 /*consume_paren=*/false);
11954 /* Parse a condition.
11958 type-specifier-seq declarator = initializer-clause
11959 type-specifier-seq declarator braced-init-list
11964 type-specifier-seq declarator asm-specification [opt]
11965 attributes [opt] = assignment-expression
11967 Returns the expression that should be tested. */
11970 cp_parser_condition (cp_parser
* parser
)
11972 cp_decl_specifier_seq type_specifiers
;
11973 const char *saved_message
;
11974 int declares_class_or_enum
;
11976 /* Try the declaration first. */
11977 cp_parser_parse_tentatively (parser
);
11978 /* New types are not allowed in the type-specifier-seq for a
11980 saved_message
= parser
->type_definition_forbidden_message
;
11981 parser
->type_definition_forbidden_message
11982 = G_("types may not be defined in conditions");
11983 /* Parse the type-specifier-seq. */
11984 cp_parser_decl_specifier_seq (parser
,
11985 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11987 &declares_class_or_enum
);
11988 /* Restore the saved message. */
11989 parser
->type_definition_forbidden_message
= saved_message
;
11991 cp_parser_maybe_commit_to_declaration (parser
,
11992 type_specifiers
.any_specifiers_p
);
11994 /* If all is well, we might be looking at a declaration. */
11995 if (!cp_parser_error_occurred (parser
))
11998 tree asm_specification
;
12000 cp_declarator
*declarator
;
12001 tree initializer
= NULL_TREE
;
12002 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12004 /* Parse the declarator. */
12005 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12006 CP_PARSER_FLAGS_NONE
,
12007 /*ctor_dtor_or_conv_p=*/NULL
,
12008 /*parenthesized_p=*/NULL
,
12009 /*member_p=*/false,
12010 /*friend_p=*/false,
12011 /*static_p=*/false);
12012 /* Parse the attributes. */
12013 attributes
= cp_parser_attributes_opt (parser
);
12014 /* Parse the asm-specification. */
12015 asm_specification
= cp_parser_asm_specification_opt (parser
);
12016 /* If the next token is not an `=' or '{', then we might still be
12017 looking at an expression. For example:
12021 looks like a decl-specifier-seq and a declarator -- but then
12022 there is no `=', so this is an expression. */
12023 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
12024 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12025 cp_parser_simulate_error (parser
);
12027 /* If we did see an `=' or '{', then we are looking at a declaration
12029 if (cp_parser_parse_definitely (parser
))
12032 bool non_constant_p
= false;
12033 int flags
= LOOKUP_ONLYCONVERTING
;
12035 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
12036 return error_mark_node
;
12038 /* Create the declaration. */
12039 decl
= start_decl (declarator
, &type_specifiers
,
12040 /*initialized_p=*/true,
12041 attributes
, /*prefix_attributes=*/NULL_TREE
,
12044 /* Parse the initializer. */
12045 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12047 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
12048 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
12051 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12053 /* Consume the `='. */
12054 cp_lexer_consume_token (parser
->lexer
);
12055 initializer
= cp_parser_initializer_clause (parser
,
12060 cp_parser_error (parser
, "expected initializer");
12061 initializer
= error_mark_node
;
12063 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
12064 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12066 /* Process the initializer. */
12067 cp_finish_decl (decl
,
12068 initializer
, !non_constant_p
,
12073 pop_scope (pushed_scope
);
12075 return convert_from_reference (decl
);
12078 /* If we didn't even get past the declarator successfully, we are
12079 definitely not looking at a declaration. */
12081 cp_parser_abort_tentative_parse (parser
);
12083 /* Otherwise, we are looking at an expression. */
12084 return cp_parser_expression (parser
);
12087 /* Parses a for-statement or range-for-statement until the closing ')',
12091 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
12093 tree init
, scope
, decl
;
12096 /* Begin the for-statement. */
12097 scope
= begin_for_scope (&init
);
12099 /* Parse the initialization. */
12100 is_range_for
= cp_parser_init_statement (parser
, &decl
);
12103 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
12106 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
12110 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
12111 unsigned short unroll
)
12113 /* Normal for loop */
12114 tree condition
= NULL_TREE
;
12115 tree expression
= NULL_TREE
;
12118 stmt
= begin_for_stmt (scope
, init
);
12119 /* The init-statement has already been parsed in
12120 cp_parser_init_statement, so no work is needed here. */
12121 finish_init_stmt (stmt
);
12123 /* If there's a condition, process it. */
12124 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12125 condition
= cp_parser_condition (parser
);
12128 cp_parser_error (parser
, "missing loop condition in loop with "
12129 "%<GCC ivdep%> pragma");
12130 condition
= error_mark_node
;
12134 cp_parser_error (parser
, "missing loop condition in loop with "
12135 "%<GCC unroll%> pragma");
12136 condition
= error_mark_node
;
12138 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
12139 /* Look for the `;'. */
12140 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12142 /* If there's an expression, process it. */
12143 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
12144 expression
= cp_parser_expression (parser
);
12145 finish_for_expr (expression
, stmt
);
12150 /* Tries to parse a range-based for-statement:
12153 decl-specifier-seq declarator : expression
12155 The decl-specifier-seq declarator and the `:' are already parsed by
12156 cp_parser_init_statement. If processing_template_decl it returns a
12157 newly created RANGE_FOR_STMT; if not, it is converted to a
12158 regular FOR_STMT. */
12161 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
12162 bool ivdep
, unsigned short unroll
, bool is_omp
)
12164 tree stmt
, range_expr
;
12165 auto_vec
<cxx_binding
*, 16> bindings
;
12166 auto_vec
<tree
, 16> names
;
12167 tree decomp_first_name
= NULL_TREE
;
12168 unsigned int decomp_cnt
= 0;
12170 /* Get the range declaration momentarily out of the way so that
12171 the range expression doesn't clash with it. */
12172 if (range_decl
!= error_mark_node
)
12174 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
12176 tree v
= DECL_VALUE_EXPR (range_decl
);
12177 /* For decomposition declaration get all of the corresponding
12178 declarations out of the way. */
12179 if (TREE_CODE (v
) == ARRAY_REF
12180 && VAR_P (TREE_OPERAND (v
, 0))
12181 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
12183 tree d
= range_decl
;
12184 range_decl
= TREE_OPERAND (v
, 0);
12185 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
12186 decomp_first_name
= d
;
12187 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
12189 tree name
= DECL_NAME (d
);
12190 names
.safe_push (name
);
12191 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12192 IDENTIFIER_BINDING (name
)
12193 = IDENTIFIER_BINDING (name
)->previous
;
12197 if (names
.is_empty ())
12199 tree name
= DECL_NAME (range_decl
);
12200 names
.safe_push (name
);
12201 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12202 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
12206 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12208 bool expr_non_constant_p
;
12209 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12212 range_expr
= cp_parser_expression (parser
);
12214 /* Put the range declaration(s) back into scope. */
12215 for (unsigned int i
= 0; i
< names
.length (); i
++)
12217 cxx_binding
*binding
= bindings
[i
];
12218 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12219 IDENTIFIER_BINDING (names
[i
]) = binding
;
12222 /* finish_omp_for has its own code for the following, so just
12223 return the range_expr instead. */
12227 /* If in template, STMT is converted to a normal for-statement
12228 at instantiation. If not, it is done just ahead. */
12229 if (processing_template_decl
)
12231 if (check_for_bare_parameter_packs (range_expr
))
12232 range_expr
= error_mark_node
;
12233 stmt
= begin_range_for_stmt (scope
, init
);
12235 RANGE_FOR_IVDEP (stmt
) = 1;
12237 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12238 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12239 if (!type_dependent_expression_p (range_expr
)
12240 /* do_auto_deduction doesn't mess with template init-lists. */
12241 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12242 do_range_for_auto_deduction (range_decl
, range_expr
);
12246 stmt
= begin_for_stmt (scope
, init
);
12247 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12248 decomp_first_name
, decomp_cnt
, ivdep
,
12254 /* Subroutine of cp_convert_range_for: given the initializer expression,
12255 builds up the range temporary. */
12258 build_range_temp (tree range_expr
)
12260 tree range_type
, range_temp
;
12262 /* Find out the type deduced by the declaration
12263 `auto &&__range = range_expr'. */
12264 range_type
= cp_build_reference_type (make_auto (), true);
12265 range_type
= do_auto_deduction (range_type
, range_expr
,
12266 type_uses_auto (range_type
));
12268 /* Create the __range variable. */
12269 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12271 TREE_USED (range_temp
) = 1;
12272 DECL_ARTIFICIAL (range_temp
) = 1;
12277 /* Used by cp_parser_range_for in template context: we aren't going to
12278 do a full conversion yet, but we still need to resolve auto in the
12279 type of the for-range-declaration if present. This is basically
12280 a shortcut version of cp_convert_range_for. */
12283 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12285 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12288 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12289 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12290 iter_type
= (cp_parser_perform_range_for_lookup
12291 (range_temp
, &begin_dummy
, &end_dummy
));
12294 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12296 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12298 tf_warning_or_error
);
12299 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12300 iter_decl
, auto_node
);
12305 /* Converts a range-based for-statement into a normal
12306 for-statement, as per the definition.
12308 for (RANGE_DECL : RANGE_EXPR)
12311 should be equivalent to:
12314 auto &&__range = RANGE_EXPR;
12315 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12319 RANGE_DECL = *__begin;
12324 If RANGE_EXPR is an array:
12325 BEGIN_EXPR = __range
12326 END_EXPR = __range + ARRAY_SIZE(__range)
12327 Else if RANGE_EXPR has a member 'begin' or 'end':
12328 BEGIN_EXPR = __range.begin()
12329 END_EXPR = __range.end()
12331 BEGIN_EXPR = begin(__range)
12332 END_EXPR = end(__range);
12334 If __range has a member 'begin' but not 'end', or vice versa, we must
12335 still use the second alternative (it will surely fail, however).
12336 When calling begin()/end() in the third alternative we must use
12337 argument dependent lookup, but always considering 'std' as an associated
12341 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12342 tree decomp_first_name
, unsigned int decomp_cnt
,
12343 bool ivdep
, unsigned short unroll
)
12346 tree iter_type
, begin_expr
, end_expr
;
12347 tree condition
, expression
;
12349 range_expr
= mark_lvalue_use (range_expr
);
12351 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12352 /* If an error happened previously do nothing or else a lot of
12353 unhelpful errors would be issued. */
12354 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12359 if (VAR_P (range_expr
)
12360 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12361 /* Can't bind a reference to an array of runtime bound. */
12362 range_temp
= range_expr
;
12365 range_temp
= build_range_temp (range_expr
);
12366 pushdecl (range_temp
);
12367 cp_finish_decl (range_temp
, range_expr
,
12368 /*is_constant_init*/false, NULL_TREE
,
12369 LOOKUP_ONLYCONVERTING
);
12370 range_temp
= convert_from_reference (range_temp
);
12372 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12373 &begin_expr
, &end_expr
);
12376 /* The new for initialization statement. */
12377 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12379 TREE_USED (begin
) = 1;
12380 DECL_ARTIFICIAL (begin
) = 1;
12382 cp_finish_decl (begin
, begin_expr
,
12383 /*is_constant_init*/false, NULL_TREE
,
12384 LOOKUP_ONLYCONVERTING
);
12386 if (cxx_dialect
>= cxx17
)
12387 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12388 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12389 TREE_USED (end
) = 1;
12390 DECL_ARTIFICIAL (end
) = 1;
12392 cp_finish_decl (end
, end_expr
,
12393 /*is_constant_init*/false, NULL_TREE
,
12394 LOOKUP_ONLYCONVERTING
);
12396 finish_init_stmt (statement
);
12398 /* The new for condition. */
12399 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12402 NULL
, tf_warning_or_error
);
12403 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12405 /* The new increment expression. */
12406 expression
= finish_unary_op_expr (input_location
,
12407 PREINCREMENT_EXPR
, begin
,
12408 tf_warning_or_error
);
12409 finish_for_expr (expression
, statement
);
12411 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12412 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12414 /* The declaration is initialized with *__begin inside the loop body. */
12415 cp_finish_decl (range_decl
,
12416 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12417 tf_warning_or_error
),
12418 /*is_constant_init*/false, NULL_TREE
,
12419 LOOKUP_ONLYCONVERTING
);
12420 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12421 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12426 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12427 We need to solve both at the same time because the method used
12428 depends on the existence of members begin or end.
12429 Returns the type deduced for the iterator expression. */
12432 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12434 if (error_operand_p (range
))
12436 *begin
= *end
= error_mark_node
;
12437 return error_mark_node
;
12440 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12442 error ("range-based %<for%> expression of type %qT "
12443 "has incomplete type", TREE_TYPE (range
));
12444 *begin
= *end
= error_mark_node
;
12445 return error_mark_node
;
12447 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12449 /* If RANGE is an array, we will use pointer arithmetic. */
12450 *begin
= decay_conversion (range
, tf_warning_or_error
);
12451 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12453 array_type_nelts_top (TREE_TYPE (range
)),
12455 return TREE_TYPE (*begin
);
12459 /* If it is not an array, we must do a bit of magic. */
12460 tree id_begin
, id_end
;
12461 tree member_begin
, member_end
;
12463 *begin
= *end
= error_mark_node
;
12465 id_begin
= get_identifier ("begin");
12466 id_end
= get_identifier ("end");
12467 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12468 /*protect=*/2, /*want_type=*/false,
12469 tf_warning_or_error
);
12470 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12471 /*protect=*/2, /*want_type=*/false,
12472 tf_warning_or_error
);
12474 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12476 /* Use the member functions. */
12477 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12478 *end
= cp_parser_range_for_member_function (range
, id_end
);
12482 /* Use global functions with ADL. */
12483 vec
<tree
, va_gc
> *vec
;
12484 vec
= make_tree_vector ();
12486 vec_safe_push (vec
, range
);
12488 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12489 tf_warning_or_error
);
12490 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12491 tf_warning_or_error
);
12492 member_end
= perform_koenig_lookup (id_end
, vec
,
12493 tf_warning_or_error
);
12494 *end
= finish_call_expr (member_end
, &vec
, false, true,
12495 tf_warning_or_error
);
12497 release_tree_vector (vec
);
12500 /* Last common checks. */
12501 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12503 /* If one of the expressions is an error do no more checks. */
12504 *begin
= *end
= error_mark_node
;
12505 return error_mark_node
;
12507 else if (type_dependent_expression_p (*begin
)
12508 || type_dependent_expression_p (*end
))
12509 /* Can happen, when, eg, in a template context, Koenig lookup
12510 can't resolve begin/end (c++/58503). */
12514 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12515 /* The unqualified type of the __begin and __end temporaries should
12516 be the same, as required by the multiple auto declaration. */
12517 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12519 if (cxx_dialect
>= cxx17
12520 && (build_x_binary_op (input_location
, NE_EXPR
,
12521 *begin
, ERROR_MARK
,
12524 != error_mark_node
))
12525 /* P0184R0 allows __begin and __end to have different types,
12526 but make sure they are comparable so we can give a better
12529 error ("inconsistent begin/end types in range-based %<for%> "
12530 "statement: %qT and %qT",
12531 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12538 /* Helper function for cp_parser_perform_range_for_lookup.
12539 Builds a tree for RANGE.IDENTIFIER(). */
12542 cp_parser_range_for_member_function (tree range
, tree identifier
)
12545 vec
<tree
, va_gc
> *vec
;
12547 member
= finish_class_member_access_expr (range
, identifier
,
12548 false, tf_warning_or_error
);
12549 if (member
== error_mark_node
)
12550 return error_mark_node
;
12552 vec
= make_tree_vector ();
12553 res
= finish_call_expr (member
, &vec
,
12554 /*disallow_virtual=*/false,
12555 /*koenig_p=*/false,
12556 tf_warning_or_error
);
12557 release_tree_vector (vec
);
12561 /* Parse an iteration-statement.
12563 iteration-statement:
12564 while ( condition ) statement
12565 do statement while ( expression ) ;
12566 for ( init-statement condition [opt] ; expression [opt] )
12569 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12572 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12573 unsigned short unroll
)
12578 unsigned char in_statement
;
12579 token_indent_info guard_tinfo
;
12581 /* Peek at the next token. */
12582 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12584 return error_mark_node
;
12586 guard_tinfo
= get_token_indent_info (token
);
12588 /* Remember whether or not we are already within an iteration
12590 in_statement
= parser
->in_statement
;
12592 /* See what kind of keyword it is. */
12593 keyword
= token
->keyword
;
12600 /* Begin the while-statement. */
12601 statement
= begin_while_stmt ();
12602 /* Look for the `('. */
12603 matching_parens parens
;
12604 parens
.require_open (parser
);
12605 /* Parse the condition. */
12606 condition
= cp_parser_condition (parser
);
12607 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12608 /* Look for the `)'. */
12609 parens
.require_close (parser
);
12610 /* Parse the dependent statement. */
12611 parser
->in_statement
= IN_ITERATION_STMT
;
12612 bool prev
= note_iteration_stmt_body_start ();
12613 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12614 note_iteration_stmt_body_end (prev
);
12615 parser
->in_statement
= in_statement
;
12616 /* We're done with the while-statement. */
12617 finish_while_stmt (statement
);
12625 /* Begin the do-statement. */
12626 statement
= begin_do_stmt ();
12627 /* Parse the body of the do-statement. */
12628 parser
->in_statement
= IN_ITERATION_STMT
;
12629 bool prev
= note_iteration_stmt_body_start ();
12630 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12631 note_iteration_stmt_body_end (prev
);
12632 parser
->in_statement
= in_statement
;
12633 finish_do_body (statement
);
12634 /* Look for the `while' keyword. */
12635 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12636 /* Look for the `('. */
12637 matching_parens parens
;
12638 parens
.require_open (parser
);
12639 /* Parse the expression. */
12640 expression
= cp_parser_expression (parser
);
12641 /* We're done with the do-statement. */
12642 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12643 /* Look for the `)'. */
12644 parens
.require_close (parser
);
12645 /* Look for the `;'. */
12646 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12652 /* Look for the `('. */
12653 matching_parens parens
;
12654 parens
.require_open (parser
);
12656 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12658 /* Look for the `)'. */
12659 parens
.require_close (parser
);
12661 /* Parse the body of the for-statement. */
12662 parser
->in_statement
= IN_ITERATION_STMT
;
12663 bool prev
= note_iteration_stmt_body_start ();
12664 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12665 note_iteration_stmt_body_end (prev
);
12666 parser
->in_statement
= in_statement
;
12668 /* We're done with the for-statement. */
12669 finish_for_stmt (statement
);
12674 cp_parser_error (parser
, "expected iteration-statement");
12675 statement
= error_mark_node
;
12682 /* Parse a init-statement or the declarator of a range-based-for.
12683 Returns true if a range-based-for declaration is seen.
12686 expression-statement
12687 simple-declaration */
12690 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12692 /* If the next token is a `;', then we have an empty
12693 expression-statement. Grammatically, this is also a
12694 simple-declaration, but an invalid one, because it does not
12695 declare anything. Therefore, if we did not handle this case
12696 specially, we would issue an error message about an invalid
12698 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12700 bool is_range_for
= false;
12701 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12703 /* Try to parse the init-statement. */
12704 if (cp_parser_range_based_for_with_init_p (parser
))
12707 cp_parser_parse_tentatively (parser
);
12708 /* Parse the declaration. */
12709 cp_parser_simple_declaration (parser
,
12710 /*function_definition_allowed_p=*/false,
12712 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12713 if (!cp_parser_parse_definitely (parser
))
12714 /* That didn't work, try to parse it as an expression-statement. */
12715 cp_parser_expression_statement (parser
, NULL_TREE
);
12717 if (cxx_dialect
< cxx2a
)
12719 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12720 "range-based %<for%> loops with initializer only "
12721 "available with -std=c++2a or -std=gnu++2a");
12722 *decl
= error_mark_node
;
12726 /* A colon is used in range-based for. */
12727 parser
->colon_corrects_to_scope_p
= false;
12729 /* We're going to speculatively look for a declaration, falling back
12730 to an expression, if necessary. */
12731 cp_parser_parse_tentatively (parser
);
12732 /* Parse the declaration. */
12733 cp_parser_simple_declaration (parser
,
12734 /*function_definition_allowed_p=*/false,
12736 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12739 /* It is a range-for, consume the ':'. */
12740 cp_lexer_consume_token (parser
->lexer
);
12741 is_range_for
= true;
12742 if (cxx_dialect
< cxx11
)
12743 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12744 "range-based %<for%> loops only available with "
12745 "-std=c++11 or -std=gnu++11");
12748 /* The ';' is not consumed yet because we told
12749 cp_parser_simple_declaration not to. */
12750 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12752 if (cp_parser_parse_definitely (parser
))
12753 return is_range_for
;
12754 /* If the tentative parse failed, then we shall need to look for an
12755 expression-statement. */
12757 /* If we are here, it is an expression-statement. */
12758 cp_parser_expression_statement (parser
, NULL_TREE
);
12762 /* Parse a jump-statement.
12767 return expression [opt] ;
12768 return braced-init-list ;
12774 goto * expression ;
12776 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12779 cp_parser_jump_statement (cp_parser
* parser
)
12781 tree statement
= error_mark_node
;
12784 unsigned char in_statement
;
12786 /* Peek at the next token. */
12787 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12789 return error_mark_node
;
12791 /* See what kind of keyword it is. */
12792 keyword
= token
->keyword
;
12796 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12797 switch (in_statement
)
12800 error_at (token
->location
, "break statement not within loop or switch");
12803 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12804 || in_statement
== IN_ITERATION_STMT
);
12805 statement
= finish_break_stmt ();
12806 if (in_statement
== IN_ITERATION_STMT
)
12807 break_maybe_infinite_loop ();
12810 error_at (token
->location
, "invalid exit from OpenMP structured block");
12813 error_at (token
->location
, "break statement used with OpenMP for loop");
12816 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12820 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12823 error_at (token
->location
, "continue statement not within a loop");
12825 /* Fall through. */
12826 case IN_ITERATION_STMT
:
12828 statement
= finish_continue_stmt ();
12831 error_at (token
->location
, "invalid exit from OpenMP structured block");
12834 gcc_unreachable ();
12836 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12842 bool expr_non_constant_p
;
12844 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12846 cp_lexer_set_source_position (parser
->lexer
);
12847 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12848 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12850 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12851 expr
= cp_parser_expression (parser
);
12853 /* If the next token is a `;', then there is no
12856 /* Build the return-statement. */
12857 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12858 /* Don't deduce from a discarded return statement. */;
12860 statement
= finish_return_stmt (expr
);
12861 /* Look for the final `;'. */
12862 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12867 if (parser
->in_function_body
12868 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12870 error ("%<goto%> in %<constexpr%> function");
12871 cp_function_chain
->invalid_constexpr
= true;
12874 /* Create the goto-statement. */
12875 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12877 /* Issue a warning about this use of a GNU extension. */
12878 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12879 /* Consume the '*' token. */
12880 cp_lexer_consume_token (parser
->lexer
);
12881 /* Parse the dependent expression. */
12882 finish_goto_stmt (cp_parser_expression (parser
));
12885 finish_goto_stmt (cp_parser_identifier (parser
));
12886 /* Look for the final `;'. */
12887 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12891 cp_parser_error (parser
, "expected jump-statement");
12898 /* Parse a declaration-statement.
12900 declaration-statement:
12901 block-declaration */
12904 cp_parser_declaration_statement (cp_parser
* parser
)
12908 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12909 p
= obstack_alloc (&declarator_obstack
, 0);
12911 /* Parse the block-declaration. */
12912 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12914 /* Free any declarators allocated. */
12915 obstack_free (&declarator_obstack
, p
);
12918 /* Some dependent statements (like `if (cond) statement'), are
12919 implicitly in their own scope. In other words, if the statement is
12920 a single statement (as opposed to a compound-statement), it is
12921 none-the-less treated as if it were enclosed in braces. Any
12922 declarations appearing in the dependent statement are out of scope
12923 after control passes that point. This function parses a statement,
12924 but ensures that is in its own scope, even if it is not a
12925 compound-statement.
12927 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12928 is a (possibly labeled) if statement which is not enclosed in
12929 braces and has an else clause. This is used to implement
12932 CHAIN is a vector of if-else-if conditions. This is used to implement
12935 Returns the new statement. */
12938 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12939 const token_indent_info
&guard_tinfo
,
12943 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12944 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12945 token_indent_info body_tinfo
12946 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12951 /* Mark if () ; with a special NOP_EXPR. */
12952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12954 cp_lexer_consume_token (parser
->lexer
);
12955 statement
= add_stmt (build_empty_stmt (body_loc
));
12957 if (guard_tinfo
.keyword
== RID_IF
12958 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12959 warning_at (body_loc
, OPT_Wempty_body
,
12960 "suggest braces around empty body in an %<if%> statement");
12961 else if (guard_tinfo
.keyword
== RID_ELSE
)
12962 warning_at (body_loc
, OPT_Wempty_body
,
12963 "suggest braces around empty body in an %<else%> statement");
12965 /* if a compound is opened, we simply parse the statement directly. */
12966 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12967 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12968 /* If the token is not a `{', then we must take special action. */
12971 /* Create a compound-statement. */
12972 statement
= begin_compound_stmt (0);
12973 /* Parse the dependent-statement. */
12974 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12975 &body_loc_after_labels
);
12976 /* Finish the dummy compound-statement. */
12977 finish_compound_stmt (statement
);
12980 token_indent_info next_tinfo
12981 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12982 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12984 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12985 && next_tinfo
.type
!= CPP_SEMICOLON
)
12986 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12987 guard_tinfo
.location
, guard_tinfo
.keyword
);
12989 /* Return the statement. */
12993 /* For some dependent statements (like `while (cond) statement'), we
12994 have already created a scope. Therefore, even if the dependent
12995 statement is a compound-statement, we do not want to create another
12999 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
13000 const token_indent_info
&guard_tinfo
)
13002 /* If the token is a `{', then we must take special action. */
13003 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13005 token_indent_info body_tinfo
13006 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13007 location_t loc_after_labels
= UNKNOWN_LOCATION
;
13009 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
13010 &loc_after_labels
);
13011 token_indent_info next_tinfo
13012 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13013 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13015 if (loc_after_labels
!= UNKNOWN_LOCATION
13016 && next_tinfo
.type
!= CPP_SEMICOLON
)
13017 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
13018 guard_tinfo
.location
,
13019 guard_tinfo
.keyword
);
13023 /* Avoid calling cp_parser_compound_statement, so that we
13024 don't create a new scope. Do everything else by hand. */
13025 matching_braces braces
;
13026 braces
.require_open (parser
);
13027 /* If the next keyword is `__label__' we have a label declaration. */
13028 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
13029 cp_parser_label_declaration (parser
);
13030 /* Parse an (optional) statement-seq. */
13031 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
13032 braces
.require_close (parser
);
13036 /* Declarations [gram.dcl.dcl] */
13038 /* Parse an optional declaration-sequence.
13042 declaration-seq declaration */
13045 cp_parser_declaration_seq_opt (cp_parser
* parser
)
13049 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13051 if (token
->type
== CPP_CLOSE_BRACE
13052 || token
->type
== CPP_EOF
)
13055 cp_parser_toplevel_declaration (parser
);
13059 /* Parse a declaration.
13063 function-definition
13064 template-declaration
13065 explicit-instantiation
13066 explicit-specialization
13067 linkage-specification
13068 namespace-definition
13076 __extension__ declaration */
13079 cp_parser_declaration (cp_parser
* parser
)
13083 int saved_pedantic
;
13085 tree attributes
= NULL_TREE
;
13087 /* Check for the `__extension__' keyword. */
13088 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13090 /* Parse the qualified declaration. */
13091 cp_parser_declaration (parser
);
13092 /* Restore the PEDANTIC flag. */
13093 pedantic
= saved_pedantic
;
13098 /* Try to figure out what kind of declaration is present. */
13099 token1
= *cp_lexer_peek_token (parser
->lexer
);
13101 if (token1
.type
!= CPP_EOF
)
13102 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
13105 token2
.type
= CPP_EOF
;
13106 token2
.keyword
= RID_MAX
;
13109 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13110 p
= obstack_alloc (&declarator_obstack
, 0);
13112 /* If the next token is `extern' and the following token is a string
13113 literal, then we have a linkage specification. */
13114 if (token1
.keyword
== RID_EXTERN
13115 && cp_parser_is_pure_string_literal (&token2
))
13116 cp_parser_linkage_specification (parser
);
13117 /* If the next token is `template', then we have either a template
13118 declaration, an explicit instantiation, or an explicit
13120 else if (token1
.keyword
== RID_TEMPLATE
)
13122 /* `template <>' indicates a template specialization. */
13123 if (token2
.type
== CPP_LESS
13124 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13125 cp_parser_explicit_specialization (parser
);
13126 /* `template <' indicates a template declaration. */
13127 else if (token2
.type
== CPP_LESS
)
13128 cp_parser_template_declaration (parser
, /*member_p=*/false);
13129 /* Anything else must be an explicit instantiation. */
13131 cp_parser_explicit_instantiation (parser
);
13133 /* If the next token is `export', then we have a template
13135 else if (token1
.keyword
== RID_EXPORT
)
13136 cp_parser_template_declaration (parser
, /*member_p=*/false);
13137 /* If the next token is `extern', 'static' or 'inline' and the one
13138 after that is `template', we have a GNU extended explicit
13139 instantiation directive. */
13140 else if (cp_parser_allow_gnu_extensions_p (parser
)
13141 && (token1
.keyword
== RID_EXTERN
13142 || token1
.keyword
== RID_STATIC
13143 || token1
.keyword
== RID_INLINE
)
13144 && token2
.keyword
== RID_TEMPLATE
)
13145 cp_parser_explicit_instantiation (parser
);
13146 /* If the next token is `namespace', check for a named or unnamed
13147 namespace definition. */
13148 else if (token1
.keyword
== RID_NAMESPACE
13149 && (/* A named namespace definition. */
13150 (token2
.type
== CPP_NAME
13151 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13153 || (token2
.type
== CPP_OPEN_SQUARE
13154 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13155 == CPP_OPEN_SQUARE
)
13156 /* An unnamed namespace definition. */
13157 || token2
.type
== CPP_OPEN_BRACE
13158 || token2
.keyword
== RID_ATTRIBUTE
))
13159 cp_parser_namespace_definition (parser
);
13160 /* An inline (associated) namespace definition. */
13161 else if (token1
.keyword
== RID_INLINE
13162 && token2
.keyword
== RID_NAMESPACE
)
13163 cp_parser_namespace_definition (parser
);
13164 /* Objective-C++ declaration/definition. */
13165 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
13166 cp_parser_objc_declaration (parser
, NULL_TREE
);
13167 else if (c_dialect_objc ()
13168 && token1
.keyword
== RID_ATTRIBUTE
13169 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
13170 cp_parser_objc_declaration (parser
, attributes
);
13171 /* At this point we may have a template declared by a concept
13173 else if (flag_concepts
13174 && cp_parser_template_declaration_after_export (parser
,
13175 /*member_p=*/false))
13178 /* Try to parse a block-declaration, or a function-definition. */
13179 cp_parser_block_declaration (parser
, /*statement_p=*/false);
13181 /* Free any declarators allocated. */
13182 obstack_free (&declarator_obstack
, p
);
13185 /* Parse a namespace-scope declaration. */
13188 cp_parser_toplevel_declaration (cp_parser
* parser
)
13190 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13192 if (token
->type
== CPP_PRAGMA
)
13193 /* A top-level declaration can consist solely of a #pragma. A
13194 nested declaration cannot, so this is done here and not in
13195 cp_parser_declaration. (A #pragma at block scope is
13196 handled in cp_parser_statement.) */
13197 cp_parser_pragma (parser
, pragma_external
, NULL
);
13198 else if (token
->type
== CPP_SEMICOLON
)
13200 /* A declaration consisting of a single semicolon is
13201 invalid. Allow it unless we're being pedantic. */
13202 cp_lexer_consume_token (parser
->lexer
);
13203 if (!in_system_header_at (input_location
))
13204 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
13207 /* Parse the declaration itself. */
13208 cp_parser_declaration (parser
);
13211 /* Parse a block-declaration.
13216 namespace-alias-definition
13223 __extension__ block-declaration
13228 static_assert-declaration
13230 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13231 part of a declaration-statement. */
13234 cp_parser_block_declaration (cp_parser
*parser
,
13238 int saved_pedantic
;
13240 /* Check for the `__extension__' keyword. */
13241 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13243 /* Parse the qualified declaration. */
13244 cp_parser_block_declaration (parser
, statement_p
);
13245 /* Restore the PEDANTIC flag. */
13246 pedantic
= saved_pedantic
;
13251 /* Peek at the next token to figure out which kind of declaration is
13253 token1
= cp_lexer_peek_token (parser
->lexer
);
13255 /* If the next keyword is `asm', we have an asm-definition. */
13256 if (token1
->keyword
== RID_ASM
)
13259 cp_parser_commit_to_tentative_parse (parser
);
13260 cp_parser_asm_definition (parser
);
13262 /* If the next keyword is `namespace', we have a
13263 namespace-alias-definition. */
13264 else if (token1
->keyword
== RID_NAMESPACE
)
13265 cp_parser_namespace_alias_definition (parser
);
13266 /* If the next keyword is `using', we have a
13267 using-declaration, a using-directive, or an alias-declaration. */
13268 else if (token1
->keyword
== RID_USING
)
13273 cp_parser_commit_to_tentative_parse (parser
);
13274 /* If the token after `using' is `namespace', then we have a
13275 using-directive. */
13276 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13277 if (token2
->keyword
== RID_NAMESPACE
)
13278 cp_parser_using_directive (parser
);
13279 /* If the second token after 'using' is '=', then we have an
13280 alias-declaration. */
13281 else if (cxx_dialect
>= cxx11
13282 && token2
->type
== CPP_NAME
13283 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13284 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13285 cp_parser_alias_declaration (parser
);
13286 /* Otherwise, it's a using-declaration. */
13288 cp_parser_using_declaration (parser
,
13289 /*access_declaration_p=*/false);
13291 /* If the next keyword is `__label__' we have a misplaced label
13293 else if (token1
->keyword
== RID_LABEL
)
13295 cp_lexer_consume_token (parser
->lexer
);
13296 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13297 cp_parser_skip_to_end_of_statement (parser
);
13298 /* If the next token is now a `;', consume it. */
13299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13300 cp_lexer_consume_token (parser
->lexer
);
13302 /* If the next token is `static_assert' we have a static assertion. */
13303 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13304 cp_parser_static_assert (parser
, /*member_p=*/false);
13305 /* Anything else must be a simple-declaration. */
13307 cp_parser_simple_declaration (parser
, !statement_p
,
13308 /*maybe_range_for_decl*/NULL
);
13311 /* Parse a simple-declaration.
13313 simple-declaration:
13314 decl-specifier-seq [opt] init-declarator-list [opt] ;
13315 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13316 brace-or-equal-initializer ;
13318 init-declarator-list:
13320 init-declarator-list , init-declarator
13322 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13323 function-definition as a simple-declaration.
13325 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13326 parsed declaration if it is an uninitialized single declarator not followed
13327 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13328 if present, will not be consumed. */
13331 cp_parser_simple_declaration (cp_parser
* parser
,
13332 bool function_definition_allowed_p
,
13333 tree
*maybe_range_for_decl
)
13335 cp_decl_specifier_seq decl_specifiers
;
13336 int declares_class_or_enum
;
13337 bool saw_declarator
;
13338 location_t comma_loc
= UNKNOWN_LOCATION
;
13339 location_t init_loc
= UNKNOWN_LOCATION
;
13341 if (maybe_range_for_decl
)
13342 *maybe_range_for_decl
= NULL_TREE
;
13344 /* Defer access checks until we know what is being declared; the
13345 checks for names appearing in the decl-specifier-seq should be
13346 done as if we were in the scope of the thing being declared. */
13347 push_deferring_access_checks (dk_deferred
);
13349 /* Parse the decl-specifier-seq. We have to keep track of whether
13350 or not the decl-specifier-seq declares a named class or
13351 enumeration type, since that is the only case in which the
13352 init-declarator-list is allowed to be empty.
13356 In a simple-declaration, the optional init-declarator-list can be
13357 omitted only when declaring a class or enumeration, that is when
13358 the decl-specifier-seq contains either a class-specifier, an
13359 elaborated-type-specifier, or an enum-specifier. */
13360 cp_parser_decl_specifier_seq (parser
,
13361 CP_PARSER_FLAGS_OPTIONAL
,
13363 &declares_class_or_enum
);
13364 /* We no longer need to defer access checks. */
13365 stop_deferring_access_checks ();
13367 /* In a block scope, a valid declaration must always have a
13368 decl-specifier-seq. By not trying to parse declarators, we can
13369 resolve the declaration/expression ambiguity more quickly. */
13370 if (!function_definition_allowed_p
13371 && !decl_specifiers
.any_specifiers_p
)
13373 cp_parser_error (parser
, "expected declaration");
13377 /* If the next two tokens are both identifiers, the code is
13378 erroneous. The usual cause of this situation is code like:
13382 where "T" should name a type -- but does not. */
13383 if (!decl_specifiers
.any_type_specifiers_p
13384 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13386 /* If parsing tentatively, we should commit; we really are
13387 looking at a declaration. */
13388 cp_parser_commit_to_tentative_parse (parser
);
13393 cp_parser_maybe_commit_to_declaration (parser
,
13394 decl_specifiers
.any_specifiers_p
);
13396 /* Look for C++17 decomposition declaration. */
13397 for (size_t n
= 1; ; n
++)
13398 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13399 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13401 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13402 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13403 && decl_specifiers
.any_specifiers_p
)
13406 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13407 maybe_range_for_decl
,
13410 /* The next token should be either a `,' or a `;'. */
13411 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13412 /* If it's a `;', we are done. */
13413 if (token
->type
== CPP_SEMICOLON
)
13415 else if (maybe_range_for_decl
)
13417 if (*maybe_range_for_decl
== NULL_TREE
)
13418 *maybe_range_for_decl
= error_mark_node
;
13421 /* Anything else is an error. */
13424 /* If we have already issued an error message we don't need
13425 to issue another one. */
13426 if ((decl
!= error_mark_node
13427 && DECL_INITIAL (decl
) != error_mark_node
)
13428 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13429 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13430 /* Skip tokens until we reach the end of the statement. */
13431 cp_parser_skip_to_end_of_statement (parser
);
13432 /* If the next token is now a `;', consume it. */
13433 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13434 cp_lexer_consume_token (parser
->lexer
);
13442 bool auto_specifier_p
;
13443 /* NULL_TREE if both variable and function declaration are allowed,
13444 error_mark_node if function declaration are not allowed and
13445 a FUNCTION_DECL that should be diagnosed if it is followed by
13446 variable declarations. */
13447 tree auto_function_declaration
;
13449 last_type
= NULL_TREE
;
13451 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13452 auto_function_declaration
= NULL_TREE
;
13454 /* Keep going until we hit the `;' at the end of the simple
13456 saw_declarator
= false;
13457 while (cp_lexer_next_token_is_not (parser
->lexer
,
13461 bool function_definition_p
;
13463 tree auto_result
= NULL_TREE
;
13465 if (saw_declarator
)
13467 /* If we are processing next declarator, comma is expected */
13468 token
= cp_lexer_peek_token (parser
->lexer
);
13469 gcc_assert (token
->type
== CPP_COMMA
);
13470 cp_lexer_consume_token (parser
->lexer
);
13471 if (maybe_range_for_decl
)
13473 *maybe_range_for_decl
= error_mark_node
;
13474 if (comma_loc
== UNKNOWN_LOCATION
)
13475 comma_loc
= token
->location
;
13479 saw_declarator
= true;
13481 /* Parse the init-declarator. */
13482 decl
= cp_parser_init_declarator (parser
,
13483 CP_PARSER_FLAGS_NONE
,
13486 function_definition_allowed_p
,
13487 /*member_p=*/false,
13488 declares_class_or_enum
,
13489 &function_definition_p
,
13490 maybe_range_for_decl
,
13493 /* If an error occurred while parsing tentatively, exit quickly.
13494 (That usually happens when in the body of a function; each
13495 statement is treated as a declaration-statement until proven
13497 if (cp_parser_error_occurred (parser
))
13500 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13502 /* If the init-declarator-list contains more than one
13503 init-declarator, they shall all form declarations of
13505 if (auto_function_declaration
== NULL_TREE
)
13506 auto_function_declaration
13507 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13508 else if (TREE_CODE (decl
) == FUNCTION_DECL
13509 || auto_function_declaration
!= error_mark_node
)
13511 error_at (decl_specifiers
.locations
[ds_type_spec
],
13512 "non-variable %qD in declaration with more than one "
13513 "declarator with placeholder type",
13514 TREE_CODE (decl
) == FUNCTION_DECL
13515 ? decl
: auto_function_declaration
);
13516 auto_function_declaration
= error_mark_node
;
13521 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13524 && last_type
!= error_mark_node
13525 && !same_type_p (auto_result
, last_type
))
13527 /* If the list of declarators contains more than one declarator,
13528 the type of each declared variable is determined as described
13529 above. If the type deduced for the template parameter U is not
13530 the same in each deduction, the program is ill-formed. */
13531 error_at (decl_specifiers
.locations
[ds_type_spec
],
13532 "inconsistent deduction for %qT: %qT and then %qT",
13533 decl_specifiers
.type
, last_type
, auto_result
);
13534 last_type
= error_mark_node
;
13537 last_type
= auto_result
;
13540 /* Handle function definitions specially. */
13541 if (function_definition_p
)
13543 /* If the next token is a `,', then we are probably
13544 processing something like:
13548 which is erroneous. */
13549 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13551 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13552 error_at (token
->location
,
13554 " declarations and function-definitions is forbidden");
13556 /* Otherwise, we're done with the list of declarators. */
13559 pop_deferring_access_checks ();
13563 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13564 *maybe_range_for_decl
= decl
;
13565 /* The next token should be either a `,' or a `;'. */
13566 token
= cp_lexer_peek_token (parser
->lexer
);
13567 /* If it's a `,', there are more declarators to come. */
13568 if (token
->type
== CPP_COMMA
)
13569 /* will be consumed next time around */;
13570 /* If it's a `;', we are done. */
13571 else if (token
->type
== CPP_SEMICOLON
)
13573 else if (maybe_range_for_decl
)
13575 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13576 permerror (decl_specifiers
.locations
[ds_type_spec
],
13577 "types may not be defined in a for-range-declaration");
13580 /* Anything else is an error. */
13583 /* If we have already issued an error message we don't need
13584 to issue another one. */
13585 if ((decl
!= error_mark_node
13586 && DECL_INITIAL (decl
) != error_mark_node
)
13587 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13588 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13589 /* Skip tokens until we reach the end of the statement. */
13590 cp_parser_skip_to_end_of_statement (parser
);
13591 /* If the next token is now a `;', consume it. */
13592 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13593 cp_lexer_consume_token (parser
->lexer
);
13596 /* After the first time around, a function-definition is not
13597 allowed -- even if it was OK at first. For example:
13602 function_definition_allowed_p
= false;
13605 /* Issue an error message if no declarators are present, and the
13606 decl-specifier-seq does not itself declare a class or
13607 enumeration: [dcl.dcl]/3. */
13608 if (!saw_declarator
)
13610 if (cp_parser_declares_only_class_p (parser
))
13612 if (!declares_class_or_enum
13613 && decl_specifiers
.type
13614 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13615 /* Ensure an error is issued anyway when finish_decltype_type,
13616 called via cp_parser_decl_specifier_seq, returns a class or
13617 an enumeration (c++/51786). */
13618 decl_specifiers
.type
= NULL_TREE
;
13619 shadow_tag (&decl_specifiers
);
13621 /* Perform any deferred access checks. */
13622 perform_deferred_access_checks (tf_warning_or_error
);
13625 /* Consume the `;'. */
13627 if (!maybe_range_for_decl
)
13628 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13629 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13631 if (init_loc
!= UNKNOWN_LOCATION
)
13632 error_at (init_loc
, "initializer in range-based %<for%> loop");
13633 if (comma_loc
!= UNKNOWN_LOCATION
)
13634 error_at (comma_loc
,
13635 "multiple declarations in range-based %<for%> loop");
13639 pop_deferring_access_checks ();
13642 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13643 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13647 cp_parser_decomposition_declaration (cp_parser
*parser
,
13648 cp_decl_specifier_seq
*decl_specifiers
,
13649 tree
*maybe_range_for_decl
,
13650 location_t
*init_loc
)
13652 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13653 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13654 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13656 /* Parse the identifier-list. */
13657 auto_vec
<cp_expr
, 10> v
;
13658 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13661 cp_expr e
= cp_parser_identifier (parser
);
13662 if (e
.get_value () == error_mark_node
)
13665 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13667 cp_lexer_consume_token (parser
->lexer
);
13670 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13671 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13673 end_loc
= UNKNOWN_LOCATION
;
13674 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13677 cp_lexer_consume_token (parser
->lexer
);
13680 cp_parser_skip_to_end_of_statement (parser
);
13681 return error_mark_node
;
13685 if (cxx_dialect
< cxx17
)
13686 pedwarn (loc
, 0, "structured bindings only available with "
13687 "-std=c++17 or -std=gnu++17");
13690 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13691 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13692 declarator
->id_loc
= loc
;
13693 if (ref_qual
!= REF_QUAL_NONE
)
13694 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13695 ref_qual
== REF_QUAL_RVALUE
,
13697 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13698 NULL_TREE
, decl_specifiers
->attributes
,
13700 tree orig_decl
= decl
;
13704 cp_decl_specifier_seq decl_specs
;
13705 clear_decl_specs (&decl_specs
);
13706 decl_specs
.type
= make_auto ();
13708 FOR_EACH_VEC_ELT (v
, i
, e
)
13711 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
13712 sfk_none
, e
.get_location ());
13715 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13716 declarator
->id_loc
= e
.get_location ();
13718 tree elt_pushed_scope
;
13719 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13720 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13721 if (decl2
== error_mark_node
)
13722 decl
= error_mark_node
;
13723 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13725 /* Ensure we've diagnosed redeclaration if we aren't creating
13727 gcc_assert (errorcount
);
13728 decl
= error_mark_node
;
13732 if (elt_pushed_scope
)
13733 pop_scope (elt_pushed_scope
);
13738 error_at (loc
, "empty structured binding declaration");
13739 decl
= error_mark_node
;
13742 if (maybe_range_for_decl
== NULL
13743 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13745 bool non_constant_p
= false, is_direct_init
= false;
13746 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13747 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13749 if (initializer
== NULL_TREE
13750 || (TREE_CODE (initializer
) == TREE_LIST
13751 && TREE_CHAIN (initializer
))
13753 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13754 && CONSTRUCTOR_NELTS (initializer
) != 1))
13756 error_at (loc
, "invalid initializer for structured binding "
13758 initializer
= error_mark_node
;
13761 if (decl
!= error_mark_node
)
13763 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13764 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13765 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13766 cp_finish_decomp (decl
, prev
, v
.length ());
13769 else if (decl
!= error_mark_node
)
13771 *maybe_range_for_decl
= prev
;
13772 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13773 the underlying DECL. */
13774 cp_finish_decomp (decl
, prev
, v
.length ());
13778 pop_scope (pushed_scope
);
13780 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13782 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13783 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13789 /* Parse a decl-specifier-seq.
13791 decl-specifier-seq:
13792 decl-specifier-seq [opt] decl-specifier
13793 decl-specifier attribute-specifier-seq [opt] (C++11)
13796 storage-class-specifier
13807 Concepts Extension:
13812 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13814 The parser flags FLAGS is used to control type-specifier parsing.
13816 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13819 1: one of the decl-specifiers is an elaborated-type-specifier
13820 (i.e., a type declaration)
13821 2: one of the decl-specifiers is an enum-specifier or a
13822 class-specifier (i.e., a type definition)
13827 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13828 cp_parser_flags flags
,
13829 cp_decl_specifier_seq
*decl_specs
,
13830 int* declares_class_or_enum
)
13832 bool constructor_possible_p
= !parser
->in_declarator_p
;
13833 bool found_decl_spec
= false;
13834 cp_token
*start_token
= NULL
;
13837 /* Clear DECL_SPECS. */
13838 clear_decl_specs (decl_specs
);
13840 /* Assume no class or enumeration type is declared. */
13841 *declares_class_or_enum
= 0;
13843 /* Keep reading specifiers until there are no more to read. */
13846 bool constructor_p
;
13850 /* Peek at the next token. */
13851 token
= cp_lexer_peek_token (parser
->lexer
);
13853 /* Save the first token of the decl spec list for error
13856 start_token
= token
;
13857 /* Handle attributes. */
13858 if (cp_next_tokens_can_be_attribute_p (parser
))
13860 /* Parse the attributes. */
13861 tree attrs
= cp_parser_attributes_opt (parser
);
13863 /* In a sequence of declaration specifiers, c++11 attributes
13864 appertain to the type that precede them. In that case
13867 The attribute-specifier-seq affects the type only for
13868 the declaration it appears in, not other declarations
13869 involving the same type.
13871 But for now let's force the user to position the
13872 attribute either at the beginning of the declaration or
13873 after the declarator-id, which would clearly mean that it
13874 applies to the declarator. */
13875 if (cxx11_attribute_p (attrs
))
13877 if (!found_decl_spec
)
13878 /* The c++11 attribute is at the beginning of the
13879 declaration. It appertains to the entity being
13883 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13885 /* This is an attribute following a
13886 class-specifier. */
13887 if (decl_specs
->type_definition_p
)
13888 warn_misplaced_attr_for_class_type (token
->location
,
13894 decl_specs
->std_attributes
13895 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13896 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13897 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13903 decl_specs
->attributes
13904 = attr_chainon (decl_specs
->attributes
, attrs
);
13905 if (decl_specs
->locations
[ds_attribute
] == 0)
13906 decl_specs
->locations
[ds_attribute
] = token
->location
;
13909 /* Assume we will find a decl-specifier keyword. */
13910 found_decl_spec
= true;
13911 /* If the next token is an appropriate keyword, we can simply
13912 add it to the list. */
13913 switch (token
->keyword
)
13919 if (!at_class_scope_p ())
13921 gcc_rich_location
richloc (token
->location
);
13922 richloc
.add_fixit_remove ();
13923 error_at (&richloc
, "%<friend%> used outside of class");
13924 cp_lexer_purge_token (parser
->lexer
);
13929 /* Consume the token. */
13930 cp_lexer_consume_token (parser
->lexer
);
13934 case RID_CONSTEXPR
:
13936 cp_lexer_consume_token (parser
->lexer
);
13941 cp_lexer_consume_token (parser
->lexer
);
13944 /* function-specifier:
13951 cp_parser_function_specifier_opt (parser
, decl_specs
);
13958 /* Consume the token. */
13959 cp_lexer_consume_token (parser
->lexer
);
13960 /* A constructor declarator cannot appear in a typedef. */
13961 constructor_possible_p
= false;
13962 /* The "typedef" keyword can only occur in a declaration; we
13963 may as well commit at this point. */
13964 cp_parser_commit_to_tentative_parse (parser
);
13966 if (decl_specs
->storage_class
!= sc_none
)
13967 decl_specs
->conflicting_specifiers_p
= true;
13970 /* storage-class-specifier:
13980 if (cxx_dialect
== cxx98
)
13982 /* Consume the token. */
13983 cp_lexer_consume_token (parser
->lexer
);
13985 /* Complain about `auto' as a storage specifier, if
13986 we're complaining about C++0x compatibility. */
13987 gcc_rich_location
richloc (token
->location
);
13988 richloc
.add_fixit_remove ();
13989 warning_at (&richloc
, OPT_Wc__11_compat
,
13990 "%<auto%> changes meaning in C++11; "
13991 "please remove it");
13993 /* Set the storage class anyway. */
13994 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13998 /* C++0x auto type-specifier. */
13999 found_decl_spec
= false;
14006 /* Consume the token. */
14007 cp_lexer_consume_token (parser
->lexer
);
14008 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
14012 /* Consume the token. */
14014 cp_lexer_consume_token (parser
->lexer
);
14018 /* We did not yet find a decl-specifier yet. */
14019 found_decl_spec
= false;
14023 if (found_decl_spec
14024 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
14025 && token
->keyword
!= RID_CONSTEXPR
)
14026 error ("decl-specifier invalid in condition");
14028 if (found_decl_spec
14029 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14030 && token
->keyword
!= RID_MUTABLE
14031 && token
->keyword
!= RID_CONSTEXPR
)
14032 error_at (token
->location
, "%qD invalid in lambda",
14033 ridpointers
[token
->keyword
]);
14036 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14038 /* Constructors are a special case. The `S' in `S()' is not a
14039 decl-specifier; it is the beginning of the declarator. */
14041 = (!found_decl_spec
14042 && constructor_possible_p
14043 && (cp_parser_constructor_declarator_p
14044 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
14046 /* If we don't have a DECL_SPEC yet, then we must be looking at
14047 a type-specifier. */
14048 if (!found_decl_spec
&& !constructor_p
)
14050 int decl_spec_declares_class_or_enum
;
14051 bool is_cv_qualifier
;
14055 = cp_parser_type_specifier (parser
, flags
,
14057 /*is_declaration=*/true,
14058 &decl_spec_declares_class_or_enum
,
14060 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
14062 /* If this type-specifier referenced a user-defined type
14063 (a typedef, class-name, etc.), then we can't allow any
14064 more such type-specifiers henceforth.
14068 The longest sequence of decl-specifiers that could
14069 possibly be a type name is taken as the
14070 decl-specifier-seq of a declaration. The sequence shall
14071 be self-consistent as described below.
14075 As a general rule, at most one type-specifier is allowed
14076 in the complete decl-specifier-seq of a declaration. The
14077 only exceptions are the following:
14079 -- const or volatile can be combined with any other
14082 -- signed or unsigned can be combined with char, long,
14090 void g (const int Pc);
14092 Here, Pc is *not* part of the decl-specifier seq; it's
14093 the declarator. Therefore, once we see a type-specifier
14094 (other than a cv-qualifier), we forbid any additional
14095 user-defined types. We *do* still allow things like `int
14096 int' to be considered a decl-specifier-seq, and issue the
14097 error message later. */
14098 if (type_spec
&& !is_cv_qualifier
)
14099 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
14100 /* A constructor declarator cannot follow a type-specifier. */
14103 constructor_possible_p
= false;
14104 found_decl_spec
= true;
14105 if (!is_cv_qualifier
)
14106 decl_specs
->any_type_specifiers_p
= true;
14108 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
14109 error_at (token
->location
, "type-specifier invalid in lambda");
14113 /* If we still do not have a DECL_SPEC, then there are no more
14114 decl-specifiers. */
14115 if (!found_decl_spec
)
14118 decl_specs
->any_specifiers_p
= true;
14119 /* After we see one decl-specifier, further decl-specifiers are
14120 always optional. */
14121 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
14124 /* Don't allow a friend specifier with a class definition. */
14125 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
14126 && (*declares_class_or_enum
& 2))
14127 error_at (decl_specs
->locations
[ds_friend
],
14128 "class definition may not be declared a friend");
14131 /* Parse an (optional) storage-class-specifier.
14133 storage-class-specifier:
14142 storage-class-specifier:
14145 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14148 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
14150 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
14153 if (cxx_dialect
!= cxx98
)
14155 /* Fall through for C++98. */
14156 gcc_fallthrough ();
14163 /* Consume the token. */
14164 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14171 /* Parse an (optional) function-specifier.
14173 function-specifier:
14179 explicit(constant-expression)
14181 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14182 Updates DECL_SPECS, if it is non-NULL. */
14185 cp_parser_function_specifier_opt (cp_parser
* parser
,
14186 cp_decl_specifier_seq
*decl_specs
)
14188 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14189 switch (token
->keyword
)
14192 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
14196 /* 14.5.2.3 [temp.mem]
14198 A member function template shall not be virtual. */
14199 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14200 && current_class_type
)
14201 error_at (token
->location
, "templates may not be %<virtual%>");
14203 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
14208 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14209 /* If we see '(', it's C++20 explicit(bool). */
14211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14213 matching_parens parens
;
14214 parens
.consume_open (parser
);
14216 /* New types are not allowed in an explicit-specifier. */
14217 const char *saved_message
14218 = parser
->type_definition_forbidden_message
;
14219 parser
->type_definition_forbidden_message
14220 = G_("types may not be defined in explicit-specifier");
14222 if (cxx_dialect
< cxx2a
)
14223 pedwarn (token
->location
, 0,
14224 "%<explicit(bool)%> only available with -std=c++2a "
14225 "or -std=gnu++2a");
14227 /* Parse the constant-expression. */
14228 expr
= cp_parser_constant_expression (parser
);
14230 /* Restore the saved message. */
14231 parser
->type_definition_forbidden_message
= saved_message
;
14232 parens
.require_close (parser
);
14235 /* The explicit-specifier explicit without a constant-expression is
14236 equivalent to the explicit-specifier explicit(true). */
14237 expr
= boolean_true_node
;
14240 "the constant-expression, if supplied, shall be a contextually
14241 converted constant expression of type bool." */
14242 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14243 /* We could evaluate it -- mark the decl as appropriate. */
14244 if (expr
== boolean_true_node
)
14245 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14246 else if (expr
== boolean_false_node
)
14247 /* Don't mark the decl as explicit. */;
14248 else if (decl_specs
)
14249 /* The expression was value-dependent. Remember it so that we can
14250 substitute it later. */
14251 decl_specs
->explicit_specifier
= expr
;
14259 /* Consume the token. */
14260 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14263 /* Parse a linkage-specification.
14265 linkage-specification:
14266 extern string-literal { declaration-seq [opt] }
14267 extern string-literal declaration */
14270 cp_parser_linkage_specification (cp_parser
* parser
)
14274 /* Look for the `extern' keyword. */
14275 cp_token
*extern_token
14276 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14278 /* Look for the string-literal. */
14279 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14280 linkage
= cp_parser_string_literal (parser
, false, false);
14282 /* Transform the literal into an identifier. If the literal is a
14283 wide-character string, or contains embedded NULs, then we can't
14284 handle it as the user wants. */
14285 if (strlen (TREE_STRING_POINTER (linkage
))
14286 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14288 cp_parser_error (parser
, "invalid linkage-specification");
14289 /* Assume C++ linkage. */
14290 linkage
= lang_name_cplusplus
;
14293 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14295 /* We're now using the new linkage. */
14296 push_lang_context (linkage
);
14298 /* Preserve the location of the the innermost linkage specification,
14299 tracking the locations of nested specifications via a local. */
14300 location_t saved_location
14301 = parser
->innermost_linkage_specification_location
;
14302 /* Construct a location ranging from the start of the "extern" to
14303 the end of the string-literal, with the caret at the start, e.g.:
14307 parser
->innermost_linkage_specification_location
14308 = make_location (extern_token
->location
,
14309 extern_token
->location
,
14310 get_finish (string_token
->location
));
14312 /* If the next token is a `{', then we're using the first
14314 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14316 cp_ensure_no_omp_declare_simd (parser
);
14317 cp_ensure_no_oacc_routine (parser
);
14319 /* Consume the `{' token. */
14320 matching_braces braces
;
14321 braces
.consume_open (parser
);
14322 /* Parse the declarations. */
14323 cp_parser_declaration_seq_opt (parser
);
14324 /* Look for the closing `}'. */
14325 braces
.require_close (parser
);
14327 /* Otherwise, there's just one declaration. */
14330 bool saved_in_unbraced_linkage_specification_p
;
14332 saved_in_unbraced_linkage_specification_p
14333 = parser
->in_unbraced_linkage_specification_p
;
14334 parser
->in_unbraced_linkage_specification_p
= true;
14335 cp_parser_declaration (parser
);
14336 parser
->in_unbraced_linkage_specification_p
14337 = saved_in_unbraced_linkage_specification_p
;
14340 /* We're done with the linkage-specification. */
14341 pop_lang_context ();
14343 /* Restore location of parent linkage specification, if any. */
14344 parser
->innermost_linkage_specification_location
= saved_location
;
14347 /* Parse a static_assert-declaration.
14349 static_assert-declaration:
14350 static_assert ( constant-expression , string-literal ) ;
14351 static_assert ( constant-expression ) ; (C++17)
14353 If MEMBER_P, this static_assert is a class member. */
14356 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14359 location_t token_loc
;
14363 /* Peek at the `static_assert' token so we can keep track of exactly
14364 where the static assertion started. */
14365 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14367 /* Look for the `static_assert' keyword. */
14368 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14372 /* We know we are in a static assertion; commit to any tentative
14374 if (cp_parser_parsing_tentatively (parser
))
14375 cp_parser_commit_to_tentative_parse (parser
);
14377 /* Parse the `(' starting the static assertion condition. */
14378 matching_parens parens
;
14379 parens
.require_open (parser
);
14381 /* Parse the constant-expression. Allow a non-constant expression
14382 here in order to give better diagnostics in finish_static_assert. */
14384 cp_parser_constant_expression (parser
,
14385 /*allow_non_constant_p=*/true,
14386 /*non_constant_p=*/&dummy
);
14388 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14390 if (cxx_dialect
< cxx17
)
14391 pedwarn (input_location
, OPT_Wpedantic
,
14392 "static_assert without a message "
14393 "only available with -std=c++17 or -std=gnu++17");
14395 cp_lexer_consume_token (parser
->lexer
);
14396 message
= build_string (1, "");
14397 TREE_TYPE (message
) = char_array_type_node
;
14398 fix_string_type (message
);
14402 /* Parse the separating `,'. */
14403 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14405 /* Parse the string-literal message. */
14406 message
= cp_parser_string_literal (parser
,
14407 /*translate=*/false,
14410 /* A `)' completes the static assertion. */
14411 if (!parens
.require_close (parser
))
14412 cp_parser_skip_to_closing_parenthesis (parser
,
14413 /*recovering=*/true,
14414 /*or_comma=*/false,
14415 /*consume_paren=*/true);
14418 /* A semicolon terminates the declaration. */
14419 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14421 /* Get the location for the static assertion. Use that of the
14422 condition if available, otherwise, use that of the "static_assert"
14424 location_t assert_loc
= condition
.get_location ();
14425 if (assert_loc
== UNKNOWN_LOCATION
)
14426 assert_loc
= token_loc
;
14428 /* Complete the static assertion, which may mean either processing
14429 the static assert now or saving it for template instantiation. */
14430 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14433 /* Parse the expression in decltype ( expression ). */
14436 cp_parser_decltype_expr (cp_parser
*parser
,
14437 bool &id_expression_or_member_access_p
)
14439 cp_token
*id_expr_start_token
;
14442 /* Since we're going to preserve any side-effects from this parse, set up a
14443 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14444 in the expression. */
14445 tentative_firewall
firewall (parser
);
14447 /* First, try parsing an id-expression. */
14448 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14449 cp_parser_parse_tentatively (parser
);
14450 expr
= cp_parser_id_expression (parser
,
14451 /*template_keyword_p=*/false,
14452 /*check_dependency_p=*/true,
14453 /*template_p=*/NULL
,
14454 /*declarator_p=*/false,
14455 /*optional_p=*/false);
14457 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14459 bool non_integral_constant_expression_p
= false;
14460 tree id_expression
= expr
;
14462 const char *error_msg
;
14464 if (identifier_p (expr
))
14465 /* Lookup the name we got back from the id-expression. */
14466 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14467 id_expr_start_token
->location
);
14469 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14470 /* A template without args is not a complete id-expression. */
14471 expr
= error_mark_node
;
14474 && expr
!= error_mark_node
14475 && TREE_CODE (expr
) != TYPE_DECL
14476 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14477 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14478 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14480 /* Complete lookup of the id-expression. */
14481 expr
= (finish_id_expression
14482 (id_expression
, expr
, parser
->scope
, &idk
,
14483 /*integral_constant_expression_p=*/false,
14484 /*allow_non_integral_constant_expression_p=*/true,
14485 &non_integral_constant_expression_p
,
14486 /*template_p=*/false,
14488 /*address_p=*/false,
14489 /*template_arg_p=*/false,
14491 id_expr_start_token
->location
));
14493 if (expr
== error_mark_node
)
14494 /* We found an id-expression, but it was something that we
14495 should not have found. This is an error, not something
14496 we can recover from, so note that we found an
14497 id-expression and we'll recover as gracefully as
14499 id_expression_or_member_access_p
= true;
14503 && expr
!= error_mark_node
14504 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14505 /* We have an id-expression. */
14506 id_expression_or_member_access_p
= true;
14509 if (!id_expression_or_member_access_p
)
14511 /* Abort the id-expression parse. */
14512 cp_parser_abort_tentative_parse (parser
);
14514 /* Parsing tentatively, again. */
14515 cp_parser_parse_tentatively (parser
);
14517 /* Parse a class member access. */
14518 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14519 /*cast_p=*/false, /*decltype*/true,
14520 /*member_access_only_p=*/true, NULL
);
14523 && expr
!= error_mark_node
14524 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14525 /* We have an id-expression. */
14526 id_expression_or_member_access_p
= true;
14529 if (id_expression_or_member_access_p
)
14530 /* We have parsed the complete id-expression or member access. */
14531 cp_parser_parse_definitely (parser
);
14534 /* Abort our attempt to parse an id-expression or member access
14536 cp_parser_abort_tentative_parse (parser
);
14538 /* Commit to the tentative_firewall so we get syntax errors. */
14539 cp_parser_commit_to_tentative_parse (parser
);
14541 /* Parse a full expression. */
14542 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14543 /*decltype_p=*/true);
14549 /* Parse a `decltype' type. Returns the type.
14551 simple-type-specifier:
14552 decltype ( expression )
14554 decltype ( auto ) */
14557 cp_parser_decltype (cp_parser
*parser
)
14559 bool id_expression_or_member_access_p
= false;
14560 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14562 if (start_token
->type
== CPP_DECLTYPE
)
14564 /* Already parsed. */
14565 cp_lexer_consume_token (parser
->lexer
);
14566 return saved_checks_value (start_token
->u
.tree_check_value
);
14569 /* Look for the `decltype' token. */
14570 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14571 return error_mark_node
;
14573 /* Parse the opening `('. */
14574 matching_parens parens
;
14575 if (!parens
.require_open (parser
))
14576 return error_mark_node
;
14578 push_deferring_access_checks (dk_deferred
);
14580 tree expr
= NULL_TREE
;
14582 if (cxx_dialect
>= cxx14
14583 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14584 /* decltype (auto) */
14585 cp_lexer_consume_token (parser
->lexer
);
14588 /* decltype (expression) */
14590 /* Types cannot be defined in a `decltype' expression. Save away the
14591 old message and set the new one. */
14592 const char *saved_message
= parser
->type_definition_forbidden_message
;
14593 parser
->type_definition_forbidden_message
14594 = G_("types may not be defined in %<decltype%> expressions");
14596 /* The restrictions on constant-expressions do not apply inside
14597 decltype expressions. */
14598 bool saved_integral_constant_expression_p
14599 = parser
->integral_constant_expression_p
;
14600 bool saved_non_integral_constant_expression_p
14601 = parser
->non_integral_constant_expression_p
;
14602 parser
->integral_constant_expression_p
= false;
14604 /* Within a parenthesized expression, a `>' token is always
14605 the greater-than operator. */
14606 bool saved_greater_than_is_operator_p
14607 = parser
->greater_than_is_operator_p
;
14608 parser
->greater_than_is_operator_p
= true;
14610 /* Do not actually evaluate the expression. */
14611 ++cp_unevaluated_operand
;
14613 /* Do not warn about problems with the expression. */
14614 ++c_inhibit_evaluation_warnings
;
14616 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14617 STRIP_ANY_LOCATION_WRAPPER (expr
);
14619 /* Go back to evaluating expressions. */
14620 --cp_unevaluated_operand
;
14621 --c_inhibit_evaluation_warnings
;
14623 /* The `>' token might be the end of a template-id or
14624 template-parameter-list now. */
14625 parser
->greater_than_is_operator_p
14626 = saved_greater_than_is_operator_p
;
14628 /* Restore the old message and the integral constant expression
14630 parser
->type_definition_forbidden_message
= saved_message
;
14631 parser
->integral_constant_expression_p
14632 = saved_integral_constant_expression_p
;
14633 parser
->non_integral_constant_expression_p
14634 = saved_non_integral_constant_expression_p
;
14637 /* Parse to the closing `)'. */
14638 if (!parens
.require_close (parser
))
14640 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14641 /*consume_paren=*/true);
14642 pop_deferring_access_checks ();
14643 return error_mark_node
;
14649 expr
= make_decltype_auto ();
14650 AUTO_IS_DECLTYPE (expr
) = true;
14653 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14654 tf_warning_or_error
);
14656 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14658 start_token
->type
= CPP_DECLTYPE
;
14659 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14660 start_token
->u
.tree_check_value
->value
= expr
;
14661 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14662 start_token
->keyword
= RID_MAX
;
14663 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14665 pop_to_parent_deferring_access_checks ();
14670 /* Special member functions [gram.special] */
14672 /* Parse a conversion-function-id.
14674 conversion-function-id:
14675 operator conversion-type-id
14677 Returns an IDENTIFIER_NODE representing the operator. */
14680 cp_parser_conversion_function_id (cp_parser
* parser
)
14684 tree saved_qualifying_scope
;
14685 tree saved_object_scope
;
14686 tree pushed_scope
= NULL_TREE
;
14688 /* Look for the `operator' token. */
14689 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14690 return error_mark_node
;
14691 /* When we parse the conversion-type-id, the current scope will be
14692 reset. However, we need that information in able to look up the
14693 conversion function later, so we save it here. */
14694 saved_scope
= parser
->scope
;
14695 saved_qualifying_scope
= parser
->qualifying_scope
;
14696 saved_object_scope
= parser
->object_scope
;
14697 /* We must enter the scope of the class so that the names of
14698 entities declared within the class are available in the
14699 conversion-type-id. For example, consider:
14706 S::operator I() { ... }
14708 In order to see that `I' is a type-name in the definition, we
14709 must be in the scope of `S'. */
14711 pushed_scope
= push_scope (saved_scope
);
14712 /* Parse the conversion-type-id. */
14713 type
= cp_parser_conversion_type_id (parser
);
14714 /* Leave the scope of the class, if any. */
14716 pop_scope (pushed_scope
);
14717 /* Restore the saved scope. */
14718 parser
->scope
= saved_scope
;
14719 parser
->qualifying_scope
= saved_qualifying_scope
;
14720 parser
->object_scope
= saved_object_scope
;
14721 /* If the TYPE is invalid, indicate failure. */
14722 if (type
== error_mark_node
)
14723 return error_mark_node
;
14724 return make_conv_op_name (type
);
14727 /* Parse a conversion-type-id:
14729 conversion-type-id:
14730 type-specifier-seq conversion-declarator [opt]
14732 Returns the TYPE specified. */
14735 cp_parser_conversion_type_id (cp_parser
* parser
)
14738 cp_decl_specifier_seq type_specifiers
;
14739 cp_declarator
*declarator
;
14740 tree type_specified
;
14741 const char *saved_message
;
14743 /* Parse the attributes. */
14744 attributes
= cp_parser_attributes_opt (parser
);
14746 saved_message
= parser
->type_definition_forbidden_message
;
14747 parser
->type_definition_forbidden_message
14748 = G_("types may not be defined in a conversion-type-id");
14750 /* Parse the type-specifiers. */
14751 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
14752 /*is_declaration=*/false,
14753 /*is_trailing_return=*/false,
14756 parser
->type_definition_forbidden_message
= saved_message
;
14758 /* If that didn't work, stop. */
14759 if (type_specifiers
.type
== error_mark_node
)
14760 return error_mark_node
;
14761 /* Parse the conversion-declarator. */
14762 declarator
= cp_parser_conversion_declarator_opt (parser
);
14764 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14765 /*initialized=*/0, &attributes
);
14767 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14769 /* Don't give this error when parsing tentatively. This happens to
14770 work because we always parse this definitively once. */
14771 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14772 && type_uses_auto (type_specified
))
14774 if (cxx_dialect
< cxx14
)
14776 error ("invalid use of %<auto%> in conversion operator");
14777 return error_mark_node
;
14779 else if (template_parm_scope_p ())
14780 warning (0, "use of %<auto%> in member template "
14781 "conversion operator can never be deduced");
14784 return type_specified
;
14787 /* Parse an (optional) conversion-declarator.
14789 conversion-declarator:
14790 ptr-operator conversion-declarator [opt]
14794 static cp_declarator
*
14795 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14797 enum tree_code code
;
14798 tree class_type
, std_attributes
= NULL_TREE
;
14799 cp_cv_quals cv_quals
;
14801 /* We don't know if there's a ptr-operator next, or not. */
14802 cp_parser_parse_tentatively (parser
);
14803 /* Try the ptr-operator. */
14804 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14806 /* If it worked, look for more conversion-declarators. */
14807 if (cp_parser_parse_definitely (parser
))
14809 cp_declarator
*declarator
;
14811 /* Parse another optional declarator. */
14812 declarator
= cp_parser_conversion_declarator_opt (parser
);
14814 declarator
= cp_parser_make_indirect_declarator
14815 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14823 /* Parse an (optional) ctor-initializer.
14826 : mem-initializer-list */
14829 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14831 /* If the next token is not a `:', then there is no
14832 ctor-initializer. */
14833 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14835 /* Do default initialization of any bases and members. */
14836 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14837 finish_mem_initializers (NULL_TREE
);
14841 /* Consume the `:' token. */
14842 cp_lexer_consume_token (parser
->lexer
);
14843 /* And the mem-initializer-list. */
14844 cp_parser_mem_initializer_list (parser
);
14847 /* Parse a mem-initializer-list.
14849 mem-initializer-list:
14850 mem-initializer ... [opt]
14851 mem-initializer ... [opt] , mem-initializer-list */
14854 cp_parser_mem_initializer_list (cp_parser
* parser
)
14856 tree mem_initializer_list
= NULL_TREE
;
14857 tree target_ctor
= error_mark_node
;
14858 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14860 /* Let the semantic analysis code know that we are starting the
14861 mem-initializer-list. */
14862 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14863 error_at (token
->location
,
14864 "only constructors take member initializers");
14866 /* Loop through the list. */
14869 tree mem_initializer
;
14871 token
= cp_lexer_peek_token (parser
->lexer
);
14872 /* Parse the mem-initializer. */
14873 mem_initializer
= cp_parser_mem_initializer (parser
);
14874 /* If the next token is a `...', we're expanding member initializers. */
14875 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14877 || (mem_initializer
!= error_mark_node
14878 && check_for_bare_parameter_packs (TREE_PURPOSE
14879 (mem_initializer
))))
14881 /* Consume the `...'. */
14883 cp_lexer_consume_token (parser
->lexer
);
14885 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14886 can be expanded but members cannot. */
14887 if (mem_initializer
!= error_mark_node
14888 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14890 error_at (token
->location
,
14891 "cannot expand initializer for member %qD",
14892 TREE_PURPOSE (mem_initializer
));
14893 mem_initializer
= error_mark_node
;
14896 /* Construct the pack expansion type. */
14897 if (mem_initializer
!= error_mark_node
)
14898 mem_initializer
= make_pack_expansion (mem_initializer
);
14900 if (target_ctor
!= error_mark_node
14901 && mem_initializer
!= error_mark_node
)
14903 error ("mem-initializer for %qD follows constructor delegation",
14904 TREE_PURPOSE (mem_initializer
));
14905 mem_initializer
= error_mark_node
;
14907 /* Look for a target constructor. */
14908 if (mem_initializer
!= error_mark_node
14909 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14910 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14912 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14913 if (mem_initializer_list
)
14915 error ("constructor delegation follows mem-initializer for %qD",
14916 TREE_PURPOSE (mem_initializer_list
));
14917 mem_initializer
= error_mark_node
;
14919 target_ctor
= mem_initializer
;
14921 /* Add it to the list, unless it was erroneous. */
14922 if (mem_initializer
!= error_mark_node
)
14924 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14925 mem_initializer_list
= mem_initializer
;
14927 /* If the next token is not a `,', we're done. */
14928 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14930 /* Consume the `,' token. */
14931 cp_lexer_consume_token (parser
->lexer
);
14934 /* Perform semantic analysis. */
14935 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14936 finish_mem_initializers (mem_initializer_list
);
14939 /* Parse a mem-initializer.
14942 mem-initializer-id ( expression-list [opt] )
14943 mem-initializer-id braced-init-list
14948 ( expression-list [opt] )
14950 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14951 class) or FIELD_DECL (for a non-static data member) to initialize;
14952 the TREE_VALUE is the expression-list. An empty initialization
14953 list is represented by void_list_node. */
14956 cp_parser_mem_initializer (cp_parser
* parser
)
14958 tree mem_initializer_id
;
14959 tree expression_list
;
14961 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14963 /* Find out what is being initialized. */
14964 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14966 permerror (token
->location
,
14967 "anachronistic old-style base class initializer");
14968 mem_initializer_id
= NULL_TREE
;
14972 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14973 if (mem_initializer_id
== error_mark_node
)
14974 return mem_initializer_id
;
14976 member
= expand_member_init (mem_initializer_id
);
14977 if (member
&& !DECL_P (member
))
14978 in_base_initializer
= 1;
14980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14982 bool expr_non_constant_p
;
14983 cp_lexer_set_source_position (parser
->lexer
);
14984 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14985 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14986 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14987 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14991 vec
<tree
, va_gc
> *vec
;
14992 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14994 /*allow_expansion_p=*/true,
14995 /*non_constant_p=*/NULL
,
14996 /*close_paren_loc=*/NULL
,
14997 /*wrap_locations_p=*/true);
14999 return error_mark_node
;
15000 expression_list
= build_tree_list_vec (vec
);
15001 release_tree_vector (vec
);
15004 if (expression_list
== error_mark_node
)
15005 return error_mark_node
;
15006 if (!expression_list
)
15007 expression_list
= void_type_node
;
15009 in_base_initializer
= 0;
15011 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
15014 /* Parse a mem-initializer-id.
15016 mem-initializer-id:
15017 :: [opt] nested-name-specifier [opt] class-name
15018 decltype-specifier (C++11)
15021 Returns a TYPE indicating the class to be initialized for the first
15022 production (and the second in C++11). Returns an IDENTIFIER_NODE
15023 indicating the data member to be initialized for the last production. */
15026 cp_parser_mem_initializer_id (cp_parser
* parser
)
15028 bool global_scope_p
;
15029 bool nested_name_specifier_p
;
15030 bool template_p
= false;
15033 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15035 /* `typename' is not allowed in this context ([temp.res]). */
15036 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
15038 error_at (token
->location
,
15039 "keyword %<typename%> not allowed in this context (a qualified "
15040 "member initializer is implicitly a type)");
15041 cp_lexer_consume_token (parser
->lexer
);
15043 /* Look for the optional `::' operator. */
15045 = (cp_parser_global_scope_opt (parser
,
15046 /*current_scope_valid_p=*/false)
15048 /* Look for the optional nested-name-specifier. The simplest way to
15053 The keyword `typename' is not permitted in a base-specifier or
15054 mem-initializer; in these contexts a qualified name that
15055 depends on a template-parameter is implicitly assumed to be a
15058 is to assume that we have seen the `typename' keyword at this
15060 nested_name_specifier_p
15061 = (cp_parser_nested_name_specifier_opt (parser
,
15062 /*typename_keyword_p=*/true,
15063 /*check_dependency_p=*/true,
15065 /*is_declaration=*/true)
15067 if (nested_name_specifier_p
)
15068 template_p
= cp_parser_optional_template_keyword (parser
);
15069 /* If there is a `::' operator or a nested-name-specifier, then we
15070 are definitely looking for a class-name. */
15071 if (global_scope_p
|| nested_name_specifier_p
)
15072 return cp_parser_class_name (parser
,
15073 /*typename_keyword_p=*/true,
15074 /*template_keyword_p=*/template_p
,
15076 /*check_dependency_p=*/true,
15077 /*class_head_p=*/false,
15078 /*is_declaration=*/true);
15079 /* Otherwise, we could also be looking for an ordinary identifier. */
15080 cp_parser_parse_tentatively (parser
);
15081 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
15082 /* Try a decltype-specifier. */
15083 id
= cp_parser_decltype (parser
);
15085 /* Otherwise, try a class-name. */
15086 id
= cp_parser_class_name (parser
,
15087 /*typename_keyword_p=*/true,
15088 /*template_keyword_p=*/false,
15090 /*check_dependency_p=*/true,
15091 /*class_head_p=*/false,
15092 /*is_declaration=*/true);
15093 /* If we found one, we're done. */
15094 if (cp_parser_parse_definitely (parser
))
15096 /* Otherwise, look for an ordinary identifier. */
15097 return cp_parser_identifier (parser
);
15100 /* Overloading [gram.over] */
15102 /* Parse an operator-function-id.
15104 operator-function-id:
15107 Returns an IDENTIFIER_NODE for the operator which is a
15108 human-readable spelling of the identifier, e.g., `operator +'. */
15111 cp_parser_operator_function_id (cp_parser
* parser
)
15113 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15114 /* Look for the `operator' keyword. */
15115 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15116 return error_mark_node
;
15117 /* And then the name of the operator itself. */
15118 return cp_parser_operator (parser
, start_loc
);
15121 /* Return an identifier node for a user-defined literal operator.
15122 The suffix identifier is chained to the operator name identifier. */
15125 cp_literal_operator_id (const char* name
)
15128 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
15129 + strlen (name
) + 10);
15130 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
15131 identifier
= get_identifier (buffer
);
15136 /* Parse an operator.
15139 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15140 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15141 || ++ -- , ->* -> () []
15148 Returns an IDENTIFIER_NODE for the operator which is a
15149 human-readable spelling of the identifier, e.g., `operator +'. */
15152 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
15154 tree id
= NULL_TREE
;
15158 /* Peek at the next token. */
15159 token
= cp_lexer_peek_token (parser
->lexer
);
15161 location_t end_loc
= token
->location
;
15163 /* Figure out which operator we have. */
15164 enum tree_code op
= ERROR_MARK
;
15165 bool assop
= false;
15166 bool consumed
= false;
15167 switch (token
->type
)
15171 /* The keyword should be either `new' or `delete'. */
15172 if (token
->keyword
== RID_NEW
)
15174 else if (token
->keyword
== RID_DELETE
)
15179 /* Consume the `new' or `delete' token. */
15180 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
15182 /* Peek at the next token. */
15183 token
= cp_lexer_peek_token (parser
->lexer
);
15184 /* If it's a `[' token then this is the array variant of the
15186 if (token
->type
== CPP_OPEN_SQUARE
)
15188 /* Consume the `[' token. */
15189 cp_lexer_consume_token (parser
->lexer
);
15190 /* Look for the `]' token. */
15191 if (cp_token
*close_token
15192 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15193 end_loc
= close_token
->location
;
15194 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
15213 op
= TRUNC_DIV_EXPR
;
15217 op
= TRUNC_MOD_EXPR
;
15237 op
= TRUTH_NOT_EXPR
;
15270 op
= TRUNC_DIV_EXPR
;
15275 op
= TRUNC_MOD_EXPR
;
15301 case CPP_LSHIFT_EQ
:
15306 case CPP_RSHIFT_EQ
:
15323 case CPP_GREATER_EQ
:
15328 op
= TRUTH_ANDIF_EXPR
;
15332 op
= TRUTH_ORIF_EXPR
;
15335 case CPP_PLUS_PLUS
:
15336 op
= POSTINCREMENT_EXPR
;
15339 case CPP_MINUS_MINUS
:
15340 op
= PREDECREMENT_EXPR
;
15344 op
= COMPOUND_EXPR
;
15347 case CPP_DEREF_STAR
:
15352 op
= COMPONENT_REF
;
15355 case CPP_OPEN_PAREN
:
15357 /* Consume the `('. */
15358 matching_parens parens
;
15359 parens
.consume_open (parser
);
15360 /* Look for the matching `)'. */
15361 token
= parens
.require_close (parser
);
15363 end_loc
= token
->location
;
15369 case CPP_OPEN_SQUARE
:
15370 /* Consume the `['. */
15371 cp_lexer_consume_token (parser
->lexer
);
15372 /* Look for the matching `]'. */
15373 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15375 end_loc
= token
->location
;
15380 case CPP_UTF8STRING
:
15381 case CPP_UTF8STRING_USERDEF
:
15388 case CPP_STRING_USERDEF
:
15389 case CPP_WSTRING_USERDEF
:
15390 case CPP_STRING16_USERDEF
:
15391 case CPP_STRING32_USERDEF
:
15397 if (cxx_dialect
== cxx98
)
15398 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15400 /* Consume the string. */
15401 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15402 /*wide_ok=*/true, /*lookup_udlit=*/false);
15403 if (str
== error_mark_node
)
15404 return error_mark_node
;
15405 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15407 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
15408 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
15409 end_loc
= str
.get_location ();
15414 /* Look for the suffix identifier. */
15415 token
= cp_lexer_peek_token (parser
->lexer
);
15416 if (token
->type
== CPP_NAME
)
15418 id
= cp_parser_identifier (parser
);
15419 end_loc
= token
->location
;
15421 else if (token
->type
== CPP_KEYWORD
)
15423 error ("unexpected keyword;"
15424 " remove space between quotes and suffix identifier");
15425 return error_mark_node
;
15429 error ("expected suffix identifier");
15430 return error_mark_node
;
15433 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15434 (TREE_TYPE (TREE_TYPE (string_tree
))));
15435 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15438 error ("expected empty string after %<operator%> keyword");
15439 return error_mark_node
;
15441 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15444 error ("invalid encoding prefix in literal operator");
15445 return error_mark_node
;
15447 if (id
!= error_mark_node
)
15449 const char *name
= IDENTIFIER_POINTER (id
);
15450 id
= cp_literal_operator_id (name
);
15452 /* Generate a location of the form:
15453 "" _suffix_identifier
15454 ^~~~~~~~~~~~~~~~~~~~~
15455 with caret == start at the start token, finish at the end of the
15456 suffix identifier. */
15457 location_t finish_loc
15458 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15459 location_t combined_loc
15460 = make_location (start_loc
, start_loc
, finish_loc
);
15461 return cp_expr (id
, combined_loc
);
15465 /* Anything else is an error. */
15469 /* If we have selected an identifier, we need to consume the
15471 if (op
!= ERROR_MARK
)
15473 id
= ovl_op_identifier (assop
, op
);
15475 cp_lexer_consume_token (parser
->lexer
);
15477 /* Otherwise, no valid operator name was present. */
15480 cp_parser_error (parser
, "expected operator");
15481 id
= error_mark_node
;
15484 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15485 return cp_expr (id
, start_loc
);
15488 /* Parse a template-declaration.
15490 template-declaration:
15491 export [opt] template < template-parameter-list > declaration
15493 If MEMBER_P is TRUE, this template-declaration occurs within a
15496 The grammar rule given by the standard isn't correct. What
15497 is really meant is:
15499 template-declaration:
15500 export [opt] template-parameter-list-seq
15501 decl-specifier-seq [opt] init-declarator [opt] ;
15502 export [opt] template-parameter-list-seq
15503 function-definition
15505 template-parameter-list-seq:
15506 template-parameter-list-seq [opt]
15507 template < template-parameter-list >
15509 Concept Extensions:
15511 template-parameter-list-seq:
15512 template < template-parameter-list > requires-clause [opt]
15515 requires logical-or-expression */
15518 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15520 /* Check for `export'. */
15521 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15523 /* Consume the `export' token. */
15524 cp_lexer_consume_token (parser
->lexer
);
15525 /* Warn that we do not support `export'. */
15526 warning (0, "keyword %<export%> not implemented, and will be ignored");
15529 cp_parser_template_declaration_after_export (parser
, member_p
);
15532 /* Parse a template-parameter-list.
15534 template-parameter-list:
15536 template-parameter-list , template-parameter
15538 Returns a TREE_LIST. Each node represents a template parameter.
15539 The nodes are connected via their TREE_CHAINs. */
15542 cp_parser_template_parameter_list (cp_parser
* parser
)
15544 tree parameter_list
= NULL_TREE
;
15546 /* Don't create wrapper nodes within a template-parameter-list,
15547 since we don't want to have different types based on the
15548 spelling location of constants and decls within them. */
15549 auto_suppress_location_wrappers sentinel
;
15551 begin_template_parm_list ();
15553 /* The loop below parses the template parms. We first need to know
15554 the total number of template parms to be able to compute proper
15555 canonical types of each dependent type. So after the loop, when
15556 we know the total number of template parms,
15557 end_template_parm_list computes the proper canonical types and
15558 fixes up the dependent types accordingly. */
15563 bool is_parameter_pack
;
15564 location_t parm_loc
;
15566 /* Parse the template-parameter. */
15567 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15568 parameter
= cp_parser_template_parameter (parser
,
15570 &is_parameter_pack
);
15571 /* Add it to the list. */
15572 if (parameter
!= error_mark_node
)
15573 parameter_list
= process_template_parm (parameter_list
,
15577 is_parameter_pack
);
15580 tree err_parm
= build_tree_list (parameter
, parameter
);
15581 parameter_list
= chainon (parameter_list
, err_parm
);
15584 /* If the next token is not a `,', we're done. */
15585 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15587 /* Otherwise, consume the `,' token. */
15588 cp_lexer_consume_token (parser
->lexer
);
15591 return end_template_parm_list (parameter_list
);
15594 /* Parse a introduction-list.
15597 introduced-parameter
15598 introduction-list , introduced-parameter
15600 introduced-parameter:
15601 ...[opt] identifier
15603 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15604 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15605 WILDCARD_DECL will also have DECL_NAME set and token location in
15606 DECL_SOURCE_LOCATION. */
15609 cp_parser_introduction_list (cp_parser
*parser
)
15611 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15615 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15617 cp_lexer_consume_token (parser
->lexer
);
15619 tree identifier
= cp_parser_identifier (parser
);
15620 if (identifier
== error_mark_node
)
15623 /* Build placeholder. */
15624 tree parm
= build_nt (WILDCARD_DECL
);
15625 DECL_SOURCE_LOCATION (parm
)
15626 = cp_lexer_peek_token (parser
->lexer
)->location
;
15627 DECL_NAME (parm
) = identifier
;
15628 WILDCARD_PACK_P (parm
) = is_pack
;
15629 vec_safe_push (introduction_vec
, parm
);
15631 /* If the next token is not a `,', we're done. */
15632 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15634 /* Otherwise, consume the `,' token. */
15635 cp_lexer_consume_token (parser
->lexer
);
15638 /* Convert the vec into a TREE_VEC. */
15639 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15642 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15643 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15645 release_tree_vector (introduction_vec
);
15646 return introduction_list
;
15649 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15650 is an abstract declarator. */
15652 static inline cp_declarator
*
15653 get_id_declarator (cp_declarator
*declarator
)
15655 cp_declarator
*d
= declarator
;
15656 while (d
&& d
->kind
!= cdk_id
)
15661 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15662 is an abstract declarator. */
15665 get_unqualified_id (cp_declarator
*declarator
)
15667 declarator
= get_id_declarator (declarator
);
15669 return declarator
->u
.id
.unqualified_name
;
15674 /* Returns true if DECL represents a constrained-parameter. */
15677 is_constrained_parameter (tree decl
)
15680 && TREE_CODE (decl
) == TYPE_DECL
15681 && CONSTRAINED_PARM_CONCEPT (decl
)
15682 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15685 /* Returns true if PARM declares a constrained-parameter. */
15688 is_constrained_parameter (cp_parameter_declarator
*parm
)
15690 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15693 /* Check that the type parameter is only a declarator-id, and that its
15694 type is not cv-qualified. */
15697 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15698 cp_parameter_declarator
*parm
)
15700 if (!parm
->declarator
)
15703 if (parm
->declarator
->kind
!= cdk_id
)
15705 cp_parser_error (parser
, "invalid constrained type parameter");
15709 /* Don't allow cv-qualified type parameters. */
15710 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15711 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15713 cp_parser_error (parser
, "cv-qualified type parameter");
15720 /* Finish parsing/processing a template type parameter and checking
15721 various restrictions. */
15724 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15726 cp_parameter_declarator
* parmdecl
)
15728 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15729 return finish_template_type_parm (class_type_node
, id
);
15731 return error_mark_node
;
15735 finish_constrained_template_template_parm (tree proto
, tree id
)
15737 /* FIXME: This should probably be copied, and we may need to adjust
15738 the template parameter depths. */
15739 tree saved_parms
= current_template_parms
;
15740 begin_template_parm_list ();
15741 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15742 end_template_parm_list ();
15744 tree parm
= finish_template_template_parm (class_type_node
, id
);
15745 current_template_parms
= saved_parms
;
15750 /* Finish parsing/processing a template template parameter by borrowing
15751 the template parameter list from the prototype parameter. */
15754 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15757 cp_parameter_declarator
*parmdecl
)
15759 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15760 return error_mark_node
;
15761 return finish_constrained_template_template_parm (proto
, id
);
15764 /* Create a new non-type template parameter from the given PARM
15768 constrained_non_type_template_parm (bool *is_non_type
,
15769 cp_parameter_declarator
*parm
)
15771 *is_non_type
= true;
15772 cp_declarator
*decl
= parm
->declarator
;
15773 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15774 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15775 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15778 /* Build a constrained template parameter based on the PARMDECL
15779 declarator. The type of PARMDECL is the constrained type, which
15780 refers to the prototype template parameter that ultimately
15781 specifies the type of the declared parameter. */
15784 finish_constrained_parameter (cp_parser
*parser
,
15785 cp_parameter_declarator
*parmdecl
,
15787 bool *is_parameter_pack
)
15789 tree decl
= parmdecl
->decl_specifiers
.type
;
15790 tree id
= get_unqualified_id (parmdecl
->declarator
);
15791 tree def
= parmdecl
->default_argument
;
15792 tree proto
= DECL_INITIAL (decl
);
15794 /* A template parameter constrained by a variadic concept shall also
15795 be declared as a template parameter pack. */
15796 bool is_variadic
= template_parameter_pack_p (proto
);
15797 if (is_variadic
&& !*is_parameter_pack
)
15798 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15800 /* Build the parameter. Return an error if the declarator was invalid. */
15802 if (TREE_CODE (proto
) == TYPE_DECL
)
15803 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15804 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15805 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15808 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15809 if (parm
== error_mark_node
)
15810 return error_mark_node
;
15812 /* Finish the parameter decl and create a node attaching the
15813 default argument and constraint. */
15814 parm
= build_tree_list (def
, parm
);
15815 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15820 /* Returns true if the parsed type actually represents the declaration
15821 of a type template-parameter. */
15824 declares_constrained_type_template_parameter (tree type
)
15826 return (is_constrained_parameter (type
)
15827 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15831 /* Returns true if the parsed type actually represents the declaration of
15832 a template template-parameter. */
15835 declares_constrained_template_template_parameter (tree type
)
15837 return (is_constrained_parameter (type
)
15838 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15841 /* Parse a default argument for a type template-parameter.
15842 Note that diagnostics are handled in cp_parser_template_parameter. */
15845 cp_parser_default_type_template_argument (cp_parser
*parser
)
15847 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15849 /* Consume the `=' token. */
15850 cp_lexer_consume_token (parser
->lexer
);
15852 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15854 /* Parse the default-argument. */
15855 push_deferring_access_checks (dk_no_deferred
);
15856 tree default_argument
= cp_parser_type_id (parser
,
15857 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15859 pop_deferring_access_checks ();
15861 if (flag_concepts
&& type_uses_auto (default_argument
))
15863 error_at (token
->location
,
15864 "invalid use of %<auto%> in default template argument");
15865 return error_mark_node
;
15868 return default_argument
;
15871 /* Parse a default argument for a template template-parameter. */
15874 cp_parser_default_template_template_argument (cp_parser
*parser
)
15876 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15880 /* Consume the `='. */
15881 cp_lexer_consume_token (parser
->lexer
);
15882 /* Parse the id-expression. */
15883 push_deferring_access_checks (dk_no_deferred
);
15884 /* save token before parsing the id-expression, for error
15886 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15887 tree default_argument
15888 = cp_parser_id_expression (parser
,
15889 /*template_keyword_p=*/false,
15890 /*check_dependency_p=*/true,
15891 /*template_p=*/&is_template
,
15892 /*declarator_p=*/false,
15893 /*optional_p=*/false);
15894 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15895 /* If the id-expression was a template-id that refers to
15896 a template-class, we already have the declaration here,
15897 so no further lookup is needed. */
15900 /* Look up the name. */
15902 = cp_parser_lookup_name (parser
, default_argument
,
15904 /*is_template=*/is_template
,
15905 /*is_namespace=*/false,
15906 /*check_dependency=*/true,
15907 /*ambiguous_decls=*/NULL
,
15909 /* See if the default argument is valid. */
15910 default_argument
= check_template_template_default_arg (default_argument
);
15911 pop_deferring_access_checks ();
15912 return default_argument
;
15915 /* Parse a template-parameter.
15917 template-parameter:
15919 parameter-declaration
15921 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15922 the parameter. The TREE_PURPOSE is the default value, if any.
15923 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15924 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15925 set to true iff this parameter is a parameter pack. */
15928 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15929 bool *is_parameter_pack
)
15932 cp_parameter_declarator
*parameter_declarator
;
15935 /* Assume it is a type parameter or a template parameter. */
15936 *is_non_type
= false;
15937 /* Assume it not a parameter pack. */
15938 *is_parameter_pack
= false;
15939 /* Peek at the next token. */
15940 token
= cp_lexer_peek_token (parser
->lexer
);
15941 /* If it is `template', we have a type-parameter. */
15942 if (token
->keyword
== RID_TEMPLATE
)
15943 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15944 /* If it is `class' or `typename' we do not know yet whether it is a
15945 type parameter or a non-type parameter. Consider:
15947 template <typename T, typename T::X X> ...
15951 template <class C, class D*> ...
15953 Here, the first parameter is a type parameter, and the second is
15954 a non-type parameter. We can tell by looking at the token after
15955 the identifier -- if it is a `,', `=', or `>' then we have a type
15957 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15959 /* Peek at the token after `class' or `typename'. */
15960 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15961 /* If it's an ellipsis, we have a template type parameter
15963 if (token
->type
== CPP_ELLIPSIS
)
15964 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15965 /* If it's an identifier, skip it. */
15966 if (token
->type
== CPP_NAME
)
15967 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15968 /* Now, see if the token looks like the end of a template
15970 if (token
->type
== CPP_COMMA
15971 || token
->type
== CPP_EQ
15972 || token
->type
== CPP_GREATER
)
15973 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15976 /* Otherwise, it is a non-type parameter or a constrained parameter.
15980 When parsing a default template-argument for a non-type
15981 template-parameter, the first non-nested `>' is taken as the end
15982 of the template parameter-list rather than a greater-than
15984 parameter_declarator
15985 = cp_parser_parameter_declaration (parser
,
15986 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15987 /*template_parm_p=*/true,
15988 /*parenthesized_p=*/NULL
);
15990 if (!parameter_declarator
)
15991 return error_mark_node
;
15993 /* If the parameter declaration is marked as a parameter pack, set
15994 *IS_PARAMETER_PACK to notify the caller. */
15995 if (parameter_declarator
->template_parameter_pack_p
)
15996 *is_parameter_pack
= true;
15998 if (parameter_declarator
->default_argument
)
16000 /* Can happen in some cases of erroneous input (c++/34892). */
16001 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16002 /* Consume the `...' for better error recovery. */
16003 cp_lexer_consume_token (parser
->lexer
);
16006 // The parameter may have been constrained.
16007 if (is_constrained_parameter (parameter_declarator
))
16008 return finish_constrained_parameter (parser
,
16009 parameter_declarator
,
16011 is_parameter_pack
);
16013 // Now we're sure that the parameter is a non-type parameter.
16014 *is_non_type
= true;
16016 parm
= grokdeclarator (parameter_declarator
->declarator
,
16017 ¶meter_declarator
->decl_specifiers
,
16018 TPARM
, /*initialized=*/0,
16019 /*attrlist=*/NULL
);
16020 if (parm
== error_mark_node
)
16021 return error_mark_node
;
16023 return build_tree_list (parameter_declarator
->default_argument
, parm
);
16026 /* Parse a type-parameter.
16029 class identifier [opt]
16030 class identifier [opt] = type-id
16031 typename identifier [opt]
16032 typename identifier [opt] = type-id
16033 template < template-parameter-list > class identifier [opt]
16034 template < template-parameter-list > class identifier [opt]
16037 GNU Extension (variadic templates):
16040 class ... identifier [opt]
16041 typename ... identifier [opt]
16043 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16044 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16045 the declaration of the parameter.
16047 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16050 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
16055 /* Look for a keyword to tell us what kind of parameter this is. */
16056 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
16058 return error_mark_node
;
16060 switch (token
->keyword
)
16066 tree default_argument
;
16068 /* If the next token is an ellipsis, we have a template
16070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16072 /* Consume the `...' token. */
16073 cp_lexer_consume_token (parser
->lexer
);
16074 maybe_warn_variadic_templates ();
16076 *is_parameter_pack
= true;
16079 /* If the next token is an identifier, then it names the
16081 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16082 identifier
= cp_parser_identifier (parser
);
16084 identifier
= NULL_TREE
;
16086 /* Create the parameter. */
16087 parameter
= finish_template_type_parm (class_type_node
, identifier
);
16089 /* If the next token is an `=', we have a default argument. */
16090 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16093 = cp_parser_default_type_template_argument (parser
);
16095 /* Template parameter packs cannot have default
16097 if (*is_parameter_pack
)
16100 error_at (token
->location
,
16101 "template parameter pack %qD cannot have a "
16102 "default argument", identifier
);
16104 error_at (token
->location
,
16105 "template parameter packs cannot have "
16106 "default arguments");
16107 default_argument
= NULL_TREE
;
16109 else if (check_for_bare_parameter_packs (default_argument
))
16110 default_argument
= error_mark_node
;
16113 default_argument
= NULL_TREE
;
16115 /* Create the combined representation of the parameter and the
16116 default argument. */
16117 parameter
= build_tree_list (default_argument
, parameter
);
16124 tree default_argument
;
16126 /* Look for the `<'. */
16127 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16128 /* Parse the template-parameter-list. */
16129 cp_parser_template_parameter_list (parser
);
16130 /* Look for the `>'. */
16131 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16133 // If template requirements are present, parse them.
16136 tree reqs
= get_shorthand_constraints (current_template_parms
);
16137 if (tree r
= cp_parser_requires_clause_opt (parser
))
16138 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
16139 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
16142 /* Look for the `class' or 'typename' keywords. */
16143 cp_parser_type_parameter_key (parser
);
16144 /* If the next token is an ellipsis, we have a template
16146 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16148 /* Consume the `...' token. */
16149 cp_lexer_consume_token (parser
->lexer
);
16150 maybe_warn_variadic_templates ();
16152 *is_parameter_pack
= true;
16154 /* If the next token is an `=', then there is a
16155 default-argument. If the next token is a `>', we are at
16156 the end of the parameter-list. If the next token is a `,',
16157 then we are at the end of this parameter. */
16158 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16159 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
16160 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16162 identifier
= cp_parser_identifier (parser
);
16163 /* Treat invalid names as if the parameter were nameless. */
16164 if (identifier
== error_mark_node
)
16165 identifier
= NULL_TREE
;
16168 identifier
= NULL_TREE
;
16170 /* Create the template parameter. */
16171 parameter
= finish_template_template_parm (class_type_node
,
16174 /* If the next token is an `=', then there is a
16175 default-argument. */
16176 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16179 = cp_parser_default_template_template_argument (parser
);
16181 /* Template parameter packs cannot have default
16183 if (*is_parameter_pack
)
16186 error_at (token
->location
,
16187 "template parameter pack %qD cannot "
16188 "have a default argument",
16191 error_at (token
->location
, "template parameter packs cannot "
16192 "have default arguments");
16193 default_argument
= NULL_TREE
;
16197 default_argument
= NULL_TREE
;
16199 /* Create the combined representation of the parameter and the
16200 default argument. */
16201 parameter
= build_tree_list (default_argument
, parameter
);
16206 gcc_unreachable ();
16213 /* Parse a template-id.
16216 template-name < template-argument-list [opt] >
16218 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16219 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16220 returned. Otherwise, if the template-name names a function, or set
16221 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16222 names a class, returns a TYPE_DECL for the specialization.
16224 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16225 uninstantiated templates. */
16228 cp_parser_template_id (cp_parser
*parser
,
16229 bool template_keyword_p
,
16230 bool check_dependency_p
,
16231 enum tag_types tag_type
,
16232 bool is_declaration
)
16237 cp_token_position start_of_id
= 0;
16238 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
16239 bool is_identifier
;
16241 /* If the next token corresponds to a template-id, there is no need
16243 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16244 if (token
->type
== CPP_TEMPLATE_ID
)
16246 cp_lexer_consume_token (parser
->lexer
);
16247 return saved_checks_value (token
->u
.tree_check_value
);
16250 /* Avoid performing name lookup if there is no possibility of
16251 finding a template-id. */
16252 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
16253 || (token
->type
== CPP_NAME
16254 && !cp_parser_nth_token_starts_template_argument_list_p
16257 cp_parser_error (parser
, "expected template-id");
16258 return error_mark_node
;
16261 /* Remember where the template-id starts. */
16262 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
16263 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
16265 push_deferring_access_checks (dk_deferred
);
16267 /* Parse the template-name. */
16268 is_identifier
= false;
16269 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16270 check_dependency_p
,
16275 /* Push any access checks inside the firewall we're about to create. */
16276 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
16277 pop_deferring_access_checks ();
16278 if (templ
== error_mark_node
|| is_identifier
)
16281 /* Since we're going to preserve any side-effects from this parse, set up a
16282 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16283 in the template arguments. */
16284 tentative_firewall
firewall (parser
);
16285 reopen_deferring_access_checks (checks
);
16287 /* If we find the sequence `[:' after a template-name, it's probably
16288 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16289 parse correctly the argument list. */
16290 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16291 == CPP_OPEN_SQUARE
)
16292 && next_token
->flags
& DIGRAPH
16293 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16295 && !(next_token_2
->flags
& PREV_WHITE
))
16297 cp_parser_parse_tentatively (parser
);
16298 /* Change `:' into `::'. */
16299 next_token_2
->type
= CPP_SCOPE
;
16300 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16302 cp_lexer_consume_token (parser
->lexer
);
16304 /* Parse the arguments. */
16305 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16306 if (!cp_parser_parse_definitely (parser
))
16308 /* If we couldn't parse an argument list, then we revert our changes
16309 and return simply an error. Maybe this is not a template-id
16311 next_token_2
->type
= CPP_COLON
;
16312 cp_parser_error (parser
, "expected %<<%>");
16313 pop_deferring_access_checks ();
16314 return error_mark_node
;
16316 /* Otherwise, emit an error about the invalid digraph, but continue
16317 parsing because we got our argument list. */
16318 if (permerror (next_token
->location
,
16319 "%<<::%> cannot begin a template-argument list"))
16321 static bool hint
= false;
16322 inform (next_token
->location
,
16323 "%<<:%> is an alternate spelling for %<[%>."
16324 " Insert whitespace between %<<%> and %<::%>");
16325 if (!hint
&& !flag_permissive
)
16327 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16328 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16329 "accept your code)");
16336 /* Look for the `<' that starts the template-argument-list. */
16337 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16339 pop_deferring_access_checks ();
16340 return error_mark_node
;
16342 /* Parse the arguments. */
16343 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16345 if ((cxx_dialect
> cxx17
)
16346 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16347 && !template_keyword_p
16348 && (cp_parser_error_occurred (parser
)
16349 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16351 /* This didn't go well. */
16352 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16354 /* C++2A says that "function-name < a;" is now ill-formed. */
16355 if (cp_parser_error_occurred (parser
))
16357 error_at (token
->location
, "invalid template-argument-list");
16358 inform (token
->location
, "function name as the left hand "
16359 "operand of %<<%> is ill-formed in C++2a; wrap the "
16360 "function name in %<()%>");
16363 /* We expect "f<targs>" to be followed by "(args)". */
16364 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16365 "expected %<(%> after template-argument-list");
16367 /* Purge all subsequent tokens. */
16368 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16371 cp_parser_simulate_error (parser
);
16372 pop_deferring_access_checks ();
16373 return error_mark_node
;
16377 /* Set the location to be of the form:
16378 template-name < template-argument-list [opt] >
16379 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16380 with caret == start at the start of the template-name,
16381 ranging until the closing '>'. */
16382 location_t finish_loc
16383 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
16384 location_t combined_loc
16385 = make_location (token
->location
, token
->location
, finish_loc
);
16387 /* Check for concepts autos where they don't belong. We could
16388 identify types in some cases of idnetifier TEMPL, looking ahead
16389 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16390 types. We reject them in functions, but if what we have is an
16391 identifier, even with none_type we can't conclude it's NOT a
16392 type, we have to wait for template substitution. */
16393 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16394 template_id
= error_mark_node
;
16395 /* Build a representation of the specialization. */
16396 else if (identifier_p (templ
))
16397 template_id
= build_min_nt_loc (combined_loc
,
16400 else if (DECL_TYPE_TEMPLATE_P (templ
)
16401 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16403 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16404 template (rather than some instantiation thereof) only if
16405 is not nested within some other construct. For example, in
16406 "template <typename T> void f(T) { A<T>::", A<T> is just an
16407 instantiation of A. */
16408 bool entering_scope
16409 = (template_parm_scope_p ()
16410 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16412 = finish_template_type (templ
, arguments
, entering_scope
);
16414 /* A template-like identifier may be a partial concept id. */
16415 else if (flag_concepts
16416 && (template_id
= (cp_parser_maybe_partial_concept_id
16417 (parser
, templ
, arguments
))))
16418 return template_id
;
16419 else if (variable_template_p (templ
))
16421 template_id
= lookup_template_variable (templ
, arguments
);
16422 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16423 SET_EXPR_LOCATION (template_id
, combined_loc
);
16427 /* If it's not a class-template or a template-template, it should be
16428 a function-template. */
16429 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16430 || TREE_CODE (templ
) == OVERLOAD
16431 || TREE_CODE (templ
) == FUNCTION_DECL
16432 || BASELINK_P (templ
)));
16434 template_id
= lookup_template_function (templ
, arguments
);
16435 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16436 SET_EXPR_LOCATION (template_id
, combined_loc
);
16439 /* If parsing tentatively, replace the sequence of tokens that makes
16440 up the template-id with a CPP_TEMPLATE_ID token. That way,
16441 should we re-parse the token stream, we will not have to repeat
16442 the effort required to do the parse, nor will we issue duplicate
16443 error messages about problems during instantiation of the
16446 /* Don't do this if we had a parse error in a declarator; re-parsing
16447 might succeed if a name changes meaning (60361). */
16448 && !(cp_parser_error_occurred (parser
)
16449 && cp_parser_parsing_tentatively (parser
)
16450 && parser
->in_declarator_p
))
16452 /* Reset the contents of the START_OF_ID token. */
16453 token
->type
= CPP_TEMPLATE_ID
;
16454 token
->location
= combined_loc
;
16456 /* Retrieve any deferred checks. Do not pop this access checks yet
16457 so the memory will not be reclaimed during token replacing below. */
16458 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16459 token
->u
.tree_check_value
->value
= template_id
;
16460 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16461 token
->keyword
= RID_MAX
;
16463 /* Purge all subsequent tokens. */
16464 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16466 /* ??? Can we actually assume that, if template_id ==
16467 error_mark_node, we will have issued a diagnostic to the
16468 user, as opposed to simply marking the tentative parse as
16470 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16471 error_at (token
->location
, "parse error in template argument list");
16474 pop_to_parent_deferring_access_checks ();
16475 return template_id
;
16478 /* Parse a template-name.
16483 The standard should actually say:
16487 operator-function-id
16489 A defect report has been filed about this issue.
16491 A conversion-function-id cannot be a template name because they cannot
16492 be part of a template-id. In fact, looking at this code:
16494 a.operator K<int>()
16496 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16497 It is impossible to call a templated conversion-function-id with an
16498 explicit argument list, since the only allowed template parameter is
16499 the type to which it is converting.
16501 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16502 `template' keyword, in a construction like:
16506 In that case `f' is taken to be a template-name, even though there
16507 is no way of knowing for sure.
16509 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16510 name refers to a set of overloaded functions, at least one of which
16511 is a template, or an IDENTIFIER_NODE with the name of the template,
16512 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16513 names are looked up inside uninstantiated templates. */
16516 cp_parser_template_name (cp_parser
* parser
,
16517 bool template_keyword_p
,
16518 bool check_dependency_p
,
16519 bool is_declaration
,
16520 enum tag_types tag_type
,
16521 bool *is_identifier
)
16525 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16527 /* If the next token is `operator', then we have either an
16528 operator-function-id or a conversion-function-id. */
16529 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16531 /* We don't know whether we're looking at an
16532 operator-function-id or a conversion-function-id. */
16533 cp_parser_parse_tentatively (parser
);
16534 /* Try an operator-function-id. */
16535 identifier
= cp_parser_operator_function_id (parser
);
16536 /* If that didn't work, try a conversion-function-id. */
16537 if (!cp_parser_parse_definitely (parser
))
16539 cp_parser_error (parser
, "expected template-name");
16540 return error_mark_node
;
16543 /* Look for the identifier. */
16545 identifier
= cp_parser_identifier (parser
);
16547 /* If we didn't find an identifier, we don't have a template-id. */
16548 if (identifier
== error_mark_node
)
16549 return error_mark_node
;
16551 /* If the name immediately followed the `template' keyword, then it
16552 is a template-name. However, if the next token is not `<', then
16553 we do not treat it as a template-name, since it is not being used
16554 as part of a template-id. This enables us to handle constructs
16557 template <typename T> struct S { S(); };
16558 template <typename T> S<T>::S();
16560 correctly. We would treat `S' as a template -- if it were `S<T>'
16561 -- but we do not if there is no `<'. */
16563 if (processing_template_decl
16564 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16566 /* In a declaration, in a dependent context, we pretend that the
16567 "template" keyword was present in order to improve error
16568 recovery. For example, given:
16570 template <typename T> void f(T::X<int>);
16572 we want to treat "X<int>" as a template-id. */
16574 && !template_keyword_p
16575 && parser
->scope
&& TYPE_P (parser
->scope
)
16576 && check_dependency_p
16577 && dependent_scope_p (parser
->scope
)
16578 /* Do not do this for dtors (or ctors), since they never
16579 need the template keyword before their name. */
16580 && !constructor_name_p (identifier
, parser
->scope
))
16582 cp_token_position start
= 0;
16584 /* Explain what went wrong. */
16585 error_at (token
->location
, "non-template %qD used as template",
16587 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16588 parser
->scope
, identifier
);
16589 /* If parsing tentatively, find the location of the "<" token. */
16590 if (cp_parser_simulate_error (parser
))
16591 start
= cp_lexer_token_position (parser
->lexer
, true);
16592 /* Parse the template arguments so that we can issue error
16593 messages about them. */
16594 cp_lexer_consume_token (parser
->lexer
);
16595 cp_parser_enclosed_template_argument_list (parser
);
16596 /* Skip tokens until we find a good place from which to
16597 continue parsing. */
16598 cp_parser_skip_to_closing_parenthesis (parser
,
16599 /*recovering=*/true,
16601 /*consume_paren=*/false);
16602 /* If parsing tentatively, permanently remove the
16603 template argument list. That will prevent duplicate
16604 error messages from being issued about the missing
16605 "template" keyword. */
16607 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16609 *is_identifier
= true;
16610 parser
->context
->object_type
= NULL_TREE
;
16614 /* If the "template" keyword is present, then there is generally
16615 no point in doing name-lookup, so we just return IDENTIFIER.
16616 But, if the qualifying scope is non-dependent then we can
16617 (and must) do name-lookup normally. */
16618 if (template_keyword_p
)
16620 tree scope
= (parser
->scope
? parser
->scope
16621 : parser
->context
->object_type
);
16622 if (scope
&& TYPE_P (scope
)
16623 && (!CLASS_TYPE_P (scope
)
16624 || (check_dependency_p
&& dependent_type_p (scope
))))
16626 /* We're optimizing away the call to cp_parser_lookup_name, but
16627 we still need to do this. */
16628 parser
->context
->object_type
= NULL_TREE
;
16634 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16635 const bool scoped_p
= ((parser
->scope
? parser
->scope
16636 : parser
->context
->object_type
) != NULL_TREE
);
16638 /* Look up the name. */
16639 decl
= cp_parser_lookup_name (parser
, identifier
,
16641 /*is_template=*/true,
16642 /*is_namespace=*/false,
16643 check_dependency_p
,
16644 /*ambiguous_decls=*/NULL
,
16647 decl
= strip_using_decl (decl
);
16649 /* If DECL is a template, then the name was a template-name. */
16650 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16652 if (TREE_DEPRECATED (decl
)
16653 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16654 warn_deprecated_use (decl
, NULL_TREE
);
16658 /* The standard does not explicitly indicate whether a name that
16659 names a set of overloaded declarations, some of which are
16660 templates, is a template-name. However, such a name should
16661 be a template-name; otherwise, there is no way to form a
16662 template-id for the overloaded templates. */
16663 bool found
= false;
16665 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16666 !found
&& iter
; ++iter
)
16667 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16671 && (cxx_dialect
> cxx17
)
16673 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
)
16674 && tag_type
== none_type
)
16676 /* [temp.names] says "A name is also considered to refer to a template
16677 if it is an unqualified-id followed by a < and name lookup finds
16678 either one or more functions or finds nothing." */
16680 /* The "more functions" case. Just use the OVERLOAD as normally.
16681 We don't use is_overloaded_fn here to avoid considering
16683 if (TREE_CODE (decl
) == OVERLOAD
16684 /* Name lookup found one function. */
16685 || TREE_CODE (decl
) == FUNCTION_DECL
)
16687 /* Name lookup found nothing. */
16688 else if (decl
== error_mark_node
)
16694 /* The name does not name a template. */
16695 cp_parser_error (parser
, "expected template-name");
16696 return error_mark_node
;
16703 /* Parse a template-argument-list.
16705 template-argument-list:
16706 template-argument ... [opt]
16707 template-argument-list , template-argument ... [opt]
16709 Returns a TREE_VEC containing the arguments. */
16712 cp_parser_template_argument_list (cp_parser
* parser
)
16714 tree fixed_args
[10];
16715 unsigned n_args
= 0;
16716 unsigned alloced
= 10;
16717 tree
*arg_ary
= fixed_args
;
16719 bool saved_in_template_argument_list_p
;
16721 bool saved_non_ice_p
;
16723 /* Don't create location wrapper nodes within a template-argument-list. */
16724 auto_suppress_location_wrappers sentinel
;
16726 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16727 parser
->in_template_argument_list_p
= true;
16728 /* Even if the template-id appears in an integral
16729 constant-expression, the contents of the argument list do
16731 saved_ice_p
= parser
->integral_constant_expression_p
;
16732 parser
->integral_constant_expression_p
= false;
16733 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16734 parser
->non_integral_constant_expression_p
= false;
16736 /* Parse the arguments. */
16742 /* Consume the comma. */
16743 cp_lexer_consume_token (parser
->lexer
);
16745 /* Parse the template-argument. */
16746 argument
= cp_parser_template_argument (parser
);
16748 /* If the next token is an ellipsis, we're expanding a template
16750 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16752 if (argument
== error_mark_node
)
16754 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16755 error_at (token
->location
,
16756 "expected parameter pack before %<...%>");
16758 /* Consume the `...' token. */
16759 cp_lexer_consume_token (parser
->lexer
);
16761 /* Make the argument into a TYPE_PACK_EXPANSION or
16762 EXPR_PACK_EXPANSION. */
16763 argument
= make_pack_expansion (argument
);
16766 if (n_args
== alloced
)
16770 if (arg_ary
== fixed_args
)
16772 arg_ary
= XNEWVEC (tree
, alloced
);
16773 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16776 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16778 arg_ary
[n_args
++] = argument
;
16780 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16782 vec
= make_tree_vec (n_args
);
16785 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16787 if (arg_ary
!= fixed_args
)
16789 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16790 parser
->integral_constant_expression_p
= saved_ice_p
;
16791 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16793 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16797 /* Parse a template-argument.
16800 assignment-expression
16804 The representation is that of an assignment-expression, type-id, or
16805 id-expression -- except that the qualified id-expression is
16806 evaluated, so that the value returned is either a DECL or an
16809 Although the standard says "assignment-expression", it forbids
16810 throw-expressions or assignments in the template argument.
16811 Therefore, we use "conditional-expression" instead. */
16814 cp_parser_template_argument (cp_parser
* parser
)
16819 bool maybe_type_id
= false;
16820 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16821 location_t loc
= 0;
16824 /* There's really no way to know what we're looking at, so we just
16825 try each alternative in order.
16829 In a template-argument, an ambiguity between a type-id and an
16830 expression is resolved to a type-id, regardless of the form of
16831 the corresponding template-parameter.
16833 Therefore, we try a type-id first. */
16834 cp_parser_parse_tentatively (parser
);
16835 argument
= cp_parser_template_type_arg (parser
);
16836 /* If there was no error parsing the type-id but the next token is a
16837 '>>', our behavior depends on which dialect of C++ we're
16838 parsing. In C++98, we probably found a typo for '> >'. But there
16839 are type-id which are also valid expressions. For instance:
16841 struct X { int operator >> (int); };
16842 template <int V> struct Foo {};
16845 Here 'X()' is a valid type-id of a function type, but the user just
16846 wanted to write the expression "X() >> 5". Thus, we remember that we
16847 found a valid type-id, but we still try to parse the argument as an
16848 expression to see what happens.
16850 In C++0x, the '>>' will be considered two separate '>'
16852 if (!cp_parser_error_occurred (parser
)
16853 && cxx_dialect
== cxx98
16854 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16856 maybe_type_id
= true;
16857 cp_parser_abort_tentative_parse (parser
);
16861 /* If the next token isn't a `,' or a `>', then this argument wasn't
16862 really finished. This means that the argument is not a valid
16864 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16865 cp_parser_error (parser
, "expected template-argument");
16866 /* If that worked, we're done. */
16867 if (cp_parser_parse_definitely (parser
))
16870 /* We're still not sure what the argument will be. */
16871 cp_parser_parse_tentatively (parser
);
16872 /* Try a template. */
16873 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16874 argument
= cp_parser_id_expression (parser
,
16875 /*template_keyword_p=*/false,
16876 /*check_dependency_p=*/true,
16878 /*declarator_p=*/false,
16879 /*optional_p=*/false);
16880 /* If the next token isn't a `,' or a `>', then this argument wasn't
16881 really finished. */
16882 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16883 cp_parser_error (parser
, "expected template-argument");
16884 if (!cp_parser_error_occurred (parser
))
16886 /* Figure out what is being referred to. If the id-expression
16887 was for a class template specialization, then we will have a
16888 TYPE_DECL at this point. There is no need to do name lookup
16889 at this point in that case. */
16890 if (TREE_CODE (argument
) != TYPE_DECL
)
16891 argument
= cp_parser_lookup_name (parser
, argument
,
16893 /*is_template=*/template_p
,
16894 /*is_namespace=*/false,
16895 /*check_dependency=*/true,
16896 /*ambiguous_decls=*/NULL
,
16897 argument_start_token
->location
);
16898 /* Handle a constrained-type-specifier for a non-type template
16900 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16902 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16903 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16904 cp_parser_error (parser
, "expected template-name");
16906 if (cp_parser_parse_definitely (parser
))
16908 if (TREE_DEPRECATED (argument
))
16909 warn_deprecated_use (argument
, NULL_TREE
);
16912 /* It must be a non-type argument. In C++17 any constant-expression is
16914 if (cxx_dialect
> cxx14
)
16917 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16919 -- an integral constant-expression of integral or enumeration
16922 -- the name of a non-type template-parameter; or
16924 -- the name of an object or function with external linkage...
16926 -- the address of an object or function with external linkage...
16928 -- a pointer to member... */
16929 /* Look for a non-type template parameter. */
16930 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16932 cp_parser_parse_tentatively (parser
);
16933 argument
= cp_parser_primary_expression (parser
,
16934 /*address_p=*/false,
16936 /*template_arg_p=*/true,
16938 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16939 || !cp_parser_next_token_ends_template_argument_p (parser
))
16940 cp_parser_simulate_error (parser
);
16941 if (cp_parser_parse_definitely (parser
))
16945 /* If the next token is "&", the argument must be the address of an
16946 object or function with external linkage. */
16947 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16950 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16951 cp_lexer_consume_token (parser
->lexer
);
16953 /* See if we might have an id-expression. */
16954 token
= cp_lexer_peek_token (parser
->lexer
);
16955 if (token
->type
== CPP_NAME
16956 || token
->keyword
== RID_OPERATOR
16957 || token
->type
== CPP_SCOPE
16958 || token
->type
== CPP_TEMPLATE_ID
16959 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16961 cp_parser_parse_tentatively (parser
);
16962 argument
= cp_parser_primary_expression (parser
,
16965 /*template_arg_p=*/true,
16967 if (cp_parser_error_occurred (parser
)
16968 || !cp_parser_next_token_ends_template_argument_p (parser
))
16969 cp_parser_abort_tentative_parse (parser
);
16974 if (INDIRECT_REF_P (argument
))
16976 /* Strip the dereference temporarily. */
16977 gcc_assert (REFERENCE_REF_P (argument
));
16978 argument
= TREE_OPERAND (argument
, 0);
16981 /* If we're in a template, we represent a qualified-id referring
16982 to a static data member as a SCOPE_REF even if the scope isn't
16983 dependent so that we can check access control later. */
16985 if (TREE_CODE (probe
) == SCOPE_REF
)
16986 probe
= TREE_OPERAND (probe
, 1);
16989 /* A variable without external linkage might still be a
16990 valid constant-expression, so no error is issued here
16991 if the external-linkage check fails. */
16992 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16993 cp_parser_simulate_error (parser
);
16995 else if (is_overloaded_fn (argument
))
16996 /* All overloaded functions are allowed; if the external
16997 linkage test does not pass, an error will be issued
17001 && (TREE_CODE (argument
) == OFFSET_REF
17002 || TREE_CODE (argument
) == SCOPE_REF
))
17003 /* A pointer-to-member. */
17005 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
17008 cp_parser_simulate_error (parser
);
17010 if (cp_parser_parse_definitely (parser
))
17013 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
17014 tf_warning_or_error
);
17016 argument
= convert_from_reference (argument
);
17021 /* If the argument started with "&", there are no other valid
17022 alternatives at this point. */
17025 cp_parser_error (parser
, "invalid non-type template argument");
17026 return error_mark_node
;
17030 /* If the argument wasn't successfully parsed as a type-id followed
17031 by '>>', the argument can only be a constant expression now.
17032 Otherwise, we try parsing the constant-expression tentatively,
17033 because the argument could really be a type-id. */
17035 cp_parser_parse_tentatively (parser
);
17037 if (cxx_dialect
<= cxx14
)
17038 argument
= cp_parser_constant_expression (parser
);
17041 /* In C++20, we can encounter a braced-init-list. */
17042 if (cxx_dialect
>= cxx2a
17043 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17045 bool expr_non_constant_p
;
17046 return cp_parser_braced_list (parser
, &expr_non_constant_p
);
17049 /* With C++17 generalized non-type template arguments we need to handle
17050 lvalue constant expressions, too. */
17051 argument
= cp_parser_assignment_expression (parser
);
17052 require_potential_constant_expression (argument
);
17055 if (!maybe_type_id
)
17057 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17058 cp_parser_error (parser
, "expected template-argument");
17059 if (cp_parser_parse_definitely (parser
))
17061 /* We did our best to parse the argument as a non type-id, but that
17062 was the only alternative that matched (albeit with a '>' after
17063 it). We can assume it's just a typo from the user, and a
17064 diagnostic will then be issued. */
17065 return cp_parser_template_type_arg (parser
);
17068 /* Parse an explicit-instantiation.
17070 explicit-instantiation:
17071 template declaration
17073 Although the standard says `declaration', what it really means is:
17075 explicit-instantiation:
17076 template decl-specifier-seq [opt] declarator [opt] ;
17078 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17079 supposed to be allowed. A defect report has been filed about this
17084 explicit-instantiation:
17085 storage-class-specifier template
17086 decl-specifier-seq [opt] declarator [opt] ;
17087 function-specifier template
17088 decl-specifier-seq [opt] declarator [opt] ; */
17091 cp_parser_explicit_instantiation (cp_parser
* parser
)
17093 int declares_class_or_enum
;
17094 cp_decl_specifier_seq decl_specifiers
;
17095 tree extension_specifier
= NULL_TREE
;
17097 timevar_push (TV_TEMPLATE_INST
);
17099 /* Look for an (optional) storage-class-specifier or
17100 function-specifier. */
17101 if (cp_parser_allow_gnu_extensions_p (parser
))
17103 extension_specifier
17104 = cp_parser_storage_class_specifier_opt (parser
);
17105 if (!extension_specifier
)
17106 extension_specifier
17107 = cp_parser_function_specifier_opt (parser
,
17108 /*decl_specs=*/NULL
);
17111 /* Look for the `template' keyword. */
17112 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17113 /* Let the front end know that we are processing an explicit
17115 begin_explicit_instantiation ();
17116 /* [temp.explicit] says that we are supposed to ignore access
17117 control while processing explicit instantiation directives. */
17118 push_deferring_access_checks (dk_no_check
);
17119 /* Parse a decl-specifier-seq. */
17120 cp_parser_decl_specifier_seq (parser
,
17121 CP_PARSER_FLAGS_OPTIONAL
,
17123 &declares_class_or_enum
);
17124 /* If there was exactly one decl-specifier, and it declared a class,
17125 and there's no declarator, then we have an explicit type
17127 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
17131 type
= check_tag_decl (&decl_specifiers
,
17132 /*explicit_type_instantiation_p=*/true);
17133 /* Turn access control back on for names used during
17134 template instantiation. */
17135 pop_deferring_access_checks ();
17137 do_type_instantiation (type
, extension_specifier
,
17138 /*complain=*/tf_error
);
17142 cp_declarator
*declarator
;
17145 /* Parse the declarator. */
17147 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17148 CP_PARSER_FLAGS_NONE
,
17149 /*ctor_dtor_or_conv_p=*/NULL
,
17150 /*parenthesized_p=*/NULL
,
17151 /*member_p=*/false,
17152 /*friend_p=*/false,
17153 /*static_p=*/false);
17154 if (declares_class_or_enum
& 2)
17155 cp_parser_check_for_definition_in_return_type (declarator
,
17156 decl_specifiers
.type
,
17157 decl_specifiers
.locations
[ds_type_spec
]);
17158 if (declarator
!= cp_error_declarator
)
17160 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
17161 permerror (decl_specifiers
.locations
[ds_inline
],
17162 "explicit instantiation shall not use"
17163 " %<inline%> specifier");
17164 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
17165 permerror (decl_specifiers
.locations
[ds_constexpr
],
17166 "explicit instantiation shall not use"
17167 " %<constexpr%> specifier");
17169 decl
= grokdeclarator (declarator
, &decl_specifiers
,
17170 NORMAL
, 0, &decl_specifiers
.attributes
);
17171 /* Turn access control back on for names used during
17172 template instantiation. */
17173 pop_deferring_access_checks ();
17174 /* Do the explicit instantiation. */
17175 do_decl_instantiation (decl
, extension_specifier
);
17179 pop_deferring_access_checks ();
17180 /* Skip the body of the explicit instantiation. */
17181 cp_parser_skip_to_end_of_statement (parser
);
17184 /* We're done with the instantiation. */
17185 end_explicit_instantiation ();
17187 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17189 timevar_pop (TV_TEMPLATE_INST
);
17192 /* Parse an explicit-specialization.
17194 explicit-specialization:
17195 template < > declaration
17197 Although the standard says `declaration', what it really means is:
17199 explicit-specialization:
17200 template <> decl-specifier [opt] init-declarator [opt] ;
17201 template <> function-definition
17202 template <> explicit-specialization
17203 template <> template-declaration */
17206 cp_parser_explicit_specialization (cp_parser
* parser
)
17208 bool need_lang_pop
;
17209 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17211 /* Look for the `template' keyword. */
17212 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17213 /* Look for the `<'. */
17214 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
17215 /* Look for the `>'. */
17216 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
17217 /* We have processed another parameter list. */
17218 ++parser
->num_template_parameter_lists
;
17221 A template ... explicit specialization ... shall not have C
17223 if (current_lang_name
== lang_name_c
)
17225 error_at (token
->location
, "template specialization with C linkage");
17226 maybe_show_extern_c_location ();
17227 /* Give it C++ linkage to avoid confusing other parts of the
17229 push_lang_context (lang_name_cplusplus
);
17230 need_lang_pop
= true;
17233 need_lang_pop
= false;
17234 /* Let the front end know that we are beginning a specialization. */
17235 if (!begin_specialization ())
17237 end_specialization ();
17241 /* If the next keyword is `template', we need to figure out whether
17242 or not we're looking a template-declaration. */
17243 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17245 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17246 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
17247 cp_parser_template_declaration_after_export (parser
,
17248 /*member_p=*/false);
17250 cp_parser_explicit_specialization (parser
);
17253 /* Parse the dependent declaration. */
17254 cp_parser_single_declaration (parser
,
17256 /*member_p=*/false,
17257 /*explicit_specialization_p=*/true,
17258 /*friend_p=*/NULL
);
17259 /* We're done with the specialization. */
17260 end_specialization ();
17261 /* For the erroneous case of a template with C linkage, we pushed an
17262 implicit C++ linkage scope; exit that scope now. */
17264 pop_lang_context ();
17265 /* We're done with this parameter list. */
17266 --parser
->num_template_parameter_lists
;
17269 /* Parse a type-specifier.
17272 simple-type-specifier
17275 elaborated-type-specifier
17283 Returns a representation of the type-specifier. For a
17284 class-specifier, enum-specifier, or elaborated-type-specifier, a
17285 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17287 The parser flags FLAGS is used to control type-specifier parsing.
17289 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17290 in a decl-specifier-seq.
17292 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17293 class-specifier, enum-specifier, or elaborated-type-specifier, then
17294 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17295 if a type is declared; 2 if it is defined. Otherwise, it is set to
17298 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17299 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17300 is set to FALSE. */
17303 cp_parser_type_specifier (cp_parser
* parser
,
17304 cp_parser_flags flags
,
17305 cp_decl_specifier_seq
*decl_specs
,
17306 bool is_declaration
,
17307 int* declares_class_or_enum
,
17308 bool* is_cv_qualifier
)
17310 tree type_spec
= NULL_TREE
;
17313 cp_decl_spec ds
= ds_last
;
17315 /* Assume this type-specifier does not declare a new type. */
17316 if (declares_class_or_enum
)
17317 *declares_class_or_enum
= 0;
17318 /* And that it does not specify a cv-qualifier. */
17319 if (is_cv_qualifier
)
17320 *is_cv_qualifier
= false;
17321 /* Peek at the next token. */
17322 token
= cp_lexer_peek_token (parser
->lexer
);
17324 /* If we're looking at a keyword, we can use that to guide the
17325 production we choose. */
17326 keyword
= token
->keyword
;
17330 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17331 goto elaborated_type_specifier
;
17333 /* Look for the enum-specifier. */
17334 type_spec
= cp_parser_enum_specifier (parser
);
17335 /* If that worked, we're done. */
17338 if (declares_class_or_enum
)
17339 *declares_class_or_enum
= 2;
17341 cp_parser_set_decl_spec_type (decl_specs
,
17344 /*type_definition_p=*/true);
17348 goto elaborated_type_specifier
;
17350 /* Any of these indicate either a class-specifier, or an
17351 elaborated-type-specifier. */
17355 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17356 goto elaborated_type_specifier
;
17358 /* Parse tentatively so that we can back up if we don't find a
17359 class-specifier. */
17360 cp_parser_parse_tentatively (parser
);
17361 /* Look for the class-specifier. */
17362 type_spec
= cp_parser_class_specifier (parser
);
17363 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17364 /* If that worked, we're done. */
17365 if (cp_parser_parse_definitely (parser
))
17367 if (declares_class_or_enum
)
17368 *declares_class_or_enum
= 2;
17370 cp_parser_set_decl_spec_type (decl_specs
,
17373 /*type_definition_p=*/true);
17377 /* Fall through. */
17378 elaborated_type_specifier
:
17379 /* We're declaring (not defining) a class or enum. */
17380 if (declares_class_or_enum
)
17381 *declares_class_or_enum
= 1;
17383 /* Fall through. */
17385 /* Look for an elaborated-type-specifier. */
17387 = (cp_parser_elaborated_type_specifier
17389 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17392 cp_parser_set_decl_spec_type (decl_specs
,
17395 /*type_definition_p=*/false);
17400 if (is_cv_qualifier
)
17401 *is_cv_qualifier
= true;
17406 if (is_cv_qualifier
)
17407 *is_cv_qualifier
= true;
17412 if (is_cv_qualifier
)
17413 *is_cv_qualifier
= true;
17417 /* The `__complex__' keyword is a GNU extension. */
17425 /* Handle simple keywords. */
17430 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17431 decl_specs
->any_specifiers_p
= true;
17433 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17436 /* If we do not already have a type-specifier, assume we are looking
17437 at a simple-type-specifier. */
17438 type_spec
= cp_parser_simple_type_specifier (parser
,
17442 /* If we didn't find a type-specifier, and a type-specifier was not
17443 optional in this context, issue an error message. */
17444 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17446 cp_parser_error (parser
, "expected type specifier");
17447 return error_mark_node
;
17453 /* Parse a simple-type-specifier.
17455 simple-type-specifier:
17456 :: [opt] nested-name-specifier [opt] type-name
17457 :: [opt] nested-name-specifier template template-id
17472 simple-type-specifier:
17474 decltype ( expression )
17477 __underlying_type ( type-id )
17481 nested-name-specifier(opt) template-name
17485 simple-type-specifier:
17487 __typeof__ unary-expression
17488 __typeof__ ( type-id )
17489 __typeof__ ( type-id ) { initializer-list , [opt] }
17491 Concepts Extension:
17493 simple-type-specifier:
17494 constrained-type-specifier
17496 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17497 appropriately updated. */
17500 cp_parser_simple_type_specifier (cp_parser
* parser
,
17501 cp_decl_specifier_seq
*decl_specs
,
17502 cp_parser_flags flags
)
17504 tree type
= NULL_TREE
;
17508 /* Peek at the next token. */
17509 token
= cp_lexer_peek_token (parser
->lexer
);
17511 /* If we're looking at a keyword, things are easy. */
17512 switch (token
->keyword
)
17516 decl_specs
->explicit_char_p
= true;
17517 type
= char_type_node
;
17520 type
= char8_type_node
;
17523 type
= char16_type_node
;
17526 type
= char32_type_node
;
17529 type
= wchar_type_node
;
17532 type
= boolean_type_node
;
17535 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17536 type
= short_integer_type_node
;
17540 decl_specs
->explicit_int_p
= true;
17541 type
= integer_type_node
;
17547 idx
= token
->keyword
- RID_INT_N_0
;
17548 if (! int_n_enabled_p
[idx
])
17552 decl_specs
->explicit_intN_p
= true;
17553 decl_specs
->int_n_idx
= idx
;
17555 type
= int_n_trees
[idx
].signed_type
;
17559 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17560 type
= long_integer_type_node
;
17563 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17564 type
= integer_type_node
;
17567 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17568 type
= unsigned_type_node
;
17571 type
= float_type_node
;
17574 type
= double_type_node
;
17577 type
= void_type_node
;
17581 maybe_warn_cpp0x (CPP0X_AUTO
);
17582 if (parser
->auto_is_implicit_function_template_parm_p
)
17584 /* The 'auto' might be the placeholder return type for a function decl
17585 with trailing return type. */
17586 bool have_trailing_return_fn_decl
= false;
17588 cp_parser_parse_tentatively (parser
);
17589 cp_lexer_consume_token (parser
->lexer
);
17590 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17591 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17592 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17593 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17595 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17597 cp_lexer_consume_token (parser
->lexer
);
17598 cp_parser_skip_to_closing_parenthesis (parser
,
17599 /*recovering*/false,
17601 /*consume_paren*/true);
17605 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17607 have_trailing_return_fn_decl
= true;
17611 cp_lexer_consume_token (parser
->lexer
);
17613 cp_parser_abort_tentative_parse (parser
);
17615 if (have_trailing_return_fn_decl
)
17617 type
= make_auto ();
17621 if (cxx_dialect
>= cxx14
)
17623 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17624 type
= TREE_TYPE (type
);
17627 type
= error_mark_node
;
17629 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17631 if (cxx_dialect
< cxx14
)
17632 error_at (token
->location
,
17633 "use of %<auto%> in lambda parameter declaration "
17634 "only available with "
17635 "-std=c++14 or -std=gnu++14");
17637 else if (cxx_dialect
< cxx14
)
17638 error_at (token
->location
,
17639 "use of %<auto%> in parameter declaration "
17640 "only available with "
17641 "-std=c++14 or -std=gnu++14");
17642 else if (!flag_concepts
)
17643 pedwarn (token
->location
, 0,
17644 "use of %<auto%> in parameter declaration "
17645 "only available with -fconcepts");
17648 type
= make_auto ();
17652 /* Since DR 743, decltype can either be a simple-type-specifier by
17653 itself or begin a nested-name-specifier. Parsing it will replace
17654 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17655 handling below decide what to do. */
17656 cp_parser_decltype (parser
);
17657 cp_lexer_set_token_position (parser
->lexer
, token
);
17661 /* Consume the `typeof' token. */
17662 cp_lexer_consume_token (parser
->lexer
);
17663 /* Parse the operand to `typeof'. */
17664 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17665 /* If it is not already a TYPE, take its type. */
17666 if (!TYPE_P (type
))
17667 type
= finish_typeof (type
);
17670 cp_parser_set_decl_spec_type (decl_specs
, type
,
17672 /*type_definition_p=*/false);
17676 case RID_UNDERLYING_TYPE
:
17677 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17679 cp_parser_set_decl_spec_type (decl_specs
, type
,
17681 /*type_definition_p=*/false);
17686 case RID_DIRECT_BASES
:
17687 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17689 cp_parser_set_decl_spec_type (decl_specs
, type
,
17691 /*type_definition_p=*/false);
17697 /* If token is an already-parsed decltype not followed by ::,
17698 it's a simple-type-specifier. */
17699 if (token
->type
== CPP_DECLTYPE
17700 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17702 type
= saved_checks_value (token
->u
.tree_check_value
);
17705 cp_parser_set_decl_spec_type (decl_specs
, type
,
17707 /*type_definition_p=*/false);
17708 /* Remember that we are handling a decltype in order to
17709 implement the resolution of DR 1510 when the argument
17710 isn't instantiation dependent. */
17711 decl_specs
->decltype_p
= true;
17713 cp_lexer_consume_token (parser
->lexer
);
17717 /* If the type-specifier was for a built-in type, we're done. */
17720 /* Record the type. */
17722 && (token
->keyword
!= RID_SIGNED
17723 && token
->keyword
!= RID_UNSIGNED
17724 && token
->keyword
!= RID_SHORT
17725 && token
->keyword
!= RID_LONG
))
17726 cp_parser_set_decl_spec_type (decl_specs
,
17729 /*type_definition_p=*/false);
17731 decl_specs
->any_specifiers_p
= true;
17733 /* Consume the token. */
17734 cp_lexer_consume_token (parser
->lexer
);
17736 if (type
== error_mark_node
)
17737 return error_mark_node
;
17739 /* There is no valid C++ program where a non-template type is
17740 followed by a "<". That usually indicates that the user thought
17741 that the type was a template. */
17742 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17745 return TYPE_NAME (type
);
17748 /* The type-specifier must be a user-defined type. */
17749 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17753 const bool typename_p
= (cxx_dialect
>= cxx2a
17754 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
17756 /* Don't gobble tokens or issue error messages if this is an
17757 optional type-specifier. */
17758 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17759 cp_parser_parse_tentatively (parser
);
17761 token
= cp_lexer_peek_token (parser
->lexer
);
17763 /* Look for the optional `::' operator. */
17765 = (cp_parser_global_scope_opt (parser
,
17766 /*current_scope_valid_p=*/false)
17768 /* Look for the nested-name specifier. */
17770 = (cp_parser_nested_name_specifier_opt (parser
,
17771 /*typename_keyword_p=*/false,
17772 /*check_dependency_p=*/true,
17774 /*is_declaration=*/false)
17776 /* If we have seen a nested-name-specifier, and the next token
17777 is `template', then we are using the template-id production. */
17779 && cp_parser_optional_template_keyword (parser
))
17781 /* Look for the template-id. */
17782 type
= cp_parser_template_id (parser
,
17783 /*template_keyword_p=*/true,
17784 /*check_dependency_p=*/true,
17786 /*is_declaration=*/false);
17787 /* If the template-id did not name a type, we are out of
17789 if (TREE_CODE (type
) != TYPE_DECL
)
17791 /* ...unless we pretend we have seen 'typename'. */
17793 type
= cp_parser_make_typename_type (parser
, type
,
17797 cp_parser_error (parser
, "expected template-id for type");
17802 /* Otherwise, look for a type-name. */
17804 type
= cp_parser_type_name (parser
, (qualified_p
&& typename_p
));
17806 /* Keep track of all name-lookups performed in class scopes. */
17810 && TREE_CODE (type
) == TYPE_DECL
17811 && identifier_p (DECL_NAME (type
)))
17812 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17813 /* If it didn't work out, we don't have a TYPE. */
17814 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17815 && !cp_parser_parse_definitely (parser
))
17817 if (!type
&& cxx_dialect
>= cxx17
)
17819 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17820 cp_parser_parse_tentatively (parser
);
17822 cp_parser_global_scope_opt (parser
,
17823 /*current_scope_valid_p=*/false);
17824 cp_parser_nested_name_specifier_opt (parser
,
17825 /*typename_keyword_p=*/false,
17826 /*check_dependency_p=*/true,
17828 /*is_declaration=*/false);
17829 tree name
= cp_parser_identifier (parser
);
17830 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17831 && parser
->scope
!= error_mark_node
)
17833 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17835 /*is_template=*/false,
17836 /*is_namespace=*/false,
17837 /*check_dependency=*/true,
17838 /*ambiguous_decls=*/NULL
,
17840 if (tmpl
&& tmpl
!= error_mark_node
17841 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17842 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17843 type
= make_template_placeholder (tmpl
);
17846 type
= error_mark_node
;
17847 if (!cp_parser_simulate_error (parser
))
17848 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17849 NLE_TYPE
, token
->location
);
17853 type
= error_mark_node
;
17855 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17856 && !cp_parser_parse_definitely (parser
))
17859 if (type
&& decl_specs
)
17860 cp_parser_set_decl_spec_type (decl_specs
, type
,
17862 /*type_definition_p=*/false);
17865 /* If we didn't get a type-name, issue an error message. */
17866 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17868 cp_parser_error (parser
, "expected type-name");
17869 return error_mark_node
;
17872 if (type
&& type
!= error_mark_node
)
17874 /* See if TYPE is an Objective-C type, and if so, parse and
17875 accept any protocol references following it. Do this before
17876 the cp_parser_check_for_invalid_template_id() call, because
17877 Objective-C types can be followed by '<...>' which would
17878 enclose protocol names rather than template arguments, and so
17879 everything is fine. */
17880 if (c_dialect_objc () && !parser
->scope
17881 && (objc_is_id (type
) || objc_is_class_name (type
)))
17883 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17884 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17886 /* Clobber the "unqualified" type previously entered into
17887 DECL_SPECS with the new, improved protocol-qualified version. */
17889 decl_specs
->type
= qual_type
;
17894 /* There is no valid C++ program where a non-template type is
17895 followed by a "<". That usually indicates that the user
17896 thought that the type was a template. */
17897 cp_parser_check_for_invalid_template_id (parser
, type
,
17905 /* Parse a type-name.
17911 simple-template-id [in c++0x]
17928 Returns a TYPE_DECL for the type. */
17931 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17935 /* We can't know yet whether it is a class-name or not. */
17936 cp_parser_parse_tentatively (parser
);
17937 /* Try a class-name. */
17938 type_decl
= cp_parser_class_name (parser
,
17939 typename_keyword_p
,
17940 /*template_keyword_p=*/false,
17942 /*check_dependency_p=*/true,
17943 /*class_head_p=*/false,
17944 /*is_declaration=*/false);
17945 /* If it's not a class-name, keep looking. */
17946 if (!cp_parser_parse_definitely (parser
))
17948 if (cxx_dialect
< cxx11
)
17949 /* It must be a typedef-name or an enum-name. */
17950 return cp_parser_nonclass_name (parser
);
17952 cp_parser_parse_tentatively (parser
);
17953 /* It is either a simple-template-id representing an
17954 instantiation of an alias template... */
17955 type_decl
= cp_parser_template_id (parser
,
17956 /*template_keyword_p=*/false,
17957 /*check_dependency_p=*/true,
17959 /*is_declaration=*/false);
17960 /* Note that this must be an instantiation of an alias template
17961 because [temp.names]/6 says:
17963 A template-id that names an alias template specialization
17966 Whereas [temp.names]/7 says:
17968 A simple-template-id that names a class template
17969 specialization is a class-name.
17971 With concepts, this could also be a partial-concept-id that
17972 declares a non-type template parameter. */
17973 if (type_decl
!= NULL_TREE
17974 && TREE_CODE (type_decl
) == TYPE_DECL
17975 && TYPE_DECL_ALIAS_P (type_decl
))
17976 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17977 else if (is_constrained_parameter (type_decl
))
17978 /* Don't do anything. */ ;
17980 cp_parser_simulate_error (parser
);
17982 if (!cp_parser_parse_definitely (parser
))
17983 /* ... Or a typedef-name or an enum-name. */
17984 return cp_parser_nonclass_name (parser
);
17990 /* Check if DECL and ARGS can form a constrained-type-specifier.
17991 If ARGS is non-null, we try to form a concept check of the
17992 form DECL<?, ARGS> where ? is a wildcard that matches any
17993 kind of template argument. If ARGS is NULL, then we try to
17994 form a concept check of the form DECL<?>. */
17997 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17998 tree decl
, tree args
)
18000 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
18002 /* If we a constrained-type-specifier cannot be deduced. */
18003 if (parser
->prevent_constrained_type_specifiers
)
18006 /* A constrained type specifier can only be found in an
18007 overload set or as a reference to a template declaration.
18009 FIXME: This might be masking a bug. It's possible that
18010 that the deduction below is causing template specializations
18011 to be formed with the wildcard as an argument. */
18012 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
18015 /* Try to build a call expression that evaluates the
18016 concept. This can fail if the overload set refers
18017 only to non-templates. */
18018 tree placeholder
= build_nt (WILDCARD_DECL
);
18019 tree check
= build_concept_check (decl
, placeholder
, args
);
18020 if (check
== error_mark_node
)
18023 /* Deduce the checked constraint and the prototype parameter.
18025 FIXME: In certain cases, failure to deduce should be a
18026 diagnosable error. */
18029 if (!deduce_constrained_parameter (check
, conc
, proto
))
18032 /* In template parameter scope, this results in a constrained
18033 parameter. Return a descriptor of that parm. */
18034 if (processing_template_parmlist
)
18035 return build_constrained_parameter (conc
, proto
, args
);
18037 /* In a parameter-declaration-clause, constrained-type
18038 specifiers result in invented template parameters. */
18039 if (parser
->auto_is_implicit_function_template_parm_p
)
18041 tree x
= build_constrained_parameter (conc
, proto
, args
);
18042 return synthesize_implicit_template_parm (parser
, x
);
18046 /* Otherwise, we're in a context where the constrained
18047 type name is deduced and the constraint applies
18048 after deduction. */
18049 return make_constrained_auto (conc
, args
);
18055 /* If DECL refers to a concept, return a TYPE_DECL representing
18056 the result of using the constrained type specifier in the
18057 current context. DECL refers to a concept if
18059 - it is an overload set containing a function concept taking a single
18062 - it is a variable concept taking a single type argument. */
18065 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
18068 && (TREE_CODE (decl
) == OVERLOAD
18069 || BASELINK_P (decl
)
18070 || variable_concept_p (decl
)))
18071 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
18076 /* Check if DECL and ARGS form a partial-concept-id. If so,
18077 assign ID to the resulting constrained placeholder.
18079 Returns true if the partial-concept-id designates a placeholder
18080 and false otherwise. Note that *id is set to NULL_TREE in
18084 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
18086 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
18089 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18101 Returns a TYPE_DECL for the type. */
18104 cp_parser_nonclass_name (cp_parser
* parser
)
18109 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18110 identifier
= cp_parser_identifier (parser
);
18111 if (identifier
== error_mark_node
)
18112 return error_mark_node
;
18114 /* Look up the type-name. */
18115 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
18117 type_decl
= strip_using_decl (type_decl
);
18119 /* If we found an overload set, then it may refer to a concept-name. */
18120 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
18123 if (TREE_CODE (type_decl
) != TYPE_DECL
18124 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
18126 /* See if this is an Objective-C type. */
18127 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18128 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
18130 type_decl
= TYPE_NAME (type
);
18133 /* Issue an error if we did not find a type-name. */
18134 if (TREE_CODE (type_decl
) != TYPE_DECL
18135 /* In Objective-C, we have the complication that class names are
18136 normally type names and start declarations (eg, the
18137 "NSObject" in "NSObject *object;"), but can be used in an
18138 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18139 is an expression. So, a classname followed by a dot is not a
18140 valid type-name. */
18141 || (objc_is_class_name (TREE_TYPE (type_decl
))
18142 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
18144 if (!cp_parser_simulate_error (parser
))
18145 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
18146 NLE_TYPE
, token
->location
);
18147 return error_mark_node
;
18149 /* Remember that the name was used in the definition of the
18150 current class so that we can check later to see if the
18151 meaning would have been different after the class was
18152 entirely defined. */
18153 else if (type_decl
!= error_mark_node
18155 maybe_note_name_used_in_class (identifier
, type_decl
);
18160 /* Parse an elaborated-type-specifier. Note that the grammar given
18161 here incorporates the resolution to DR68.
18163 elaborated-type-specifier:
18164 class-key :: [opt] nested-name-specifier [opt] identifier
18165 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18166 enum-key :: [opt] nested-name-specifier [opt] identifier
18167 typename :: [opt] nested-name-specifier identifier
18168 typename :: [opt] nested-name-specifier template [opt]
18173 elaborated-type-specifier:
18174 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18175 class-key attributes :: [opt] nested-name-specifier [opt]
18176 template [opt] template-id
18177 enum attributes :: [opt] nested-name-specifier [opt] identifier
18179 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18180 declared `friend'. If IS_DECLARATION is TRUE, then this
18181 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18182 something is being declared.
18184 Returns the TYPE specified. */
18187 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
18189 bool is_declaration
)
18191 enum tag_types tag_type
;
18193 tree type
= NULL_TREE
;
18194 tree attributes
= NULL_TREE
;
18196 cp_token
*token
= NULL
;
18198 /* See if we're looking at the `enum' keyword. */
18199 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
18201 /* Consume the `enum' token. */
18202 cp_lexer_consume_token (parser
->lexer
);
18203 /* Remember that it's an enumeration type. */
18204 tag_type
= enum_type
;
18205 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18206 enums) is used here. */
18207 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18208 if (cp_parser_is_keyword (token
, RID_CLASS
)
18209 || cp_parser_is_keyword (token
, RID_STRUCT
))
18211 gcc_rich_location
richloc (token
->location
);
18212 richloc
.add_range (input_location
);
18213 richloc
.add_fixit_remove ();
18214 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
18215 "a scoped enum must not use the %qD keyword",
18217 /* Consume the `struct' or `class' and parse it anyway. */
18218 cp_lexer_consume_token (parser
->lexer
);
18220 /* Parse the attributes. */
18221 attributes
= cp_parser_attributes_opt (parser
);
18223 /* Or, it might be `typename'. */
18224 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
18227 /* Consume the `typename' token. */
18228 cp_lexer_consume_token (parser
->lexer
);
18229 /* Remember that it's a `typename' type. */
18230 tag_type
= typename_type
;
18232 /* Otherwise it must be a class-key. */
18235 tag_type
= cp_parser_class_key (parser
);
18236 if (tag_type
== none_type
)
18237 return error_mark_node
;
18238 /* Parse the attributes. */
18239 attributes
= cp_parser_attributes_opt (parser
);
18242 /* Look for the `::' operator. */
18243 globalscope
= cp_parser_global_scope_opt (parser
,
18244 /*current_scope_valid_p=*/false);
18245 /* Look for the nested-name-specifier. */
18246 tree nested_name_specifier
;
18247 if (tag_type
== typename_type
&& !globalscope
)
18249 nested_name_specifier
18250 = cp_parser_nested_name_specifier (parser
,
18251 /*typename_keyword_p=*/true,
18252 /*check_dependency_p=*/true,
18255 if (!nested_name_specifier
)
18256 return error_mark_node
;
18259 /* Even though `typename' is not present, the proposed resolution
18260 to Core Issue 180 says that in `class A<T>::B', `B' should be
18261 considered a type-name, even if `A<T>' is dependent. */
18262 nested_name_specifier
18263 = cp_parser_nested_name_specifier_opt (parser
,
18264 /*typename_keyword_p=*/true,
18265 /*check_dependency_p=*/true,
18268 /* For everything but enumeration types, consider a template-id.
18269 For an enumeration type, consider only a plain identifier. */
18270 if (tag_type
!= enum_type
)
18272 bool template_p
= false;
18275 /* Allow the `template' keyword. */
18276 template_p
= cp_parser_optional_template_keyword (parser
);
18277 /* If we didn't see `template', we don't know if there's a
18278 template-id or not. */
18280 cp_parser_parse_tentatively (parser
);
18281 /* The `template' keyword must follow a nested-name-specifier. */
18282 else if (!nested_name_specifier
)
18284 cp_parser_error (parser
, "%<template%> must follow a nested-"
18286 return error_mark_node
;
18289 /* Parse the template-id. */
18290 token
= cp_lexer_peek_token (parser
->lexer
);
18291 decl
= cp_parser_template_id (parser
, template_p
,
18292 /*check_dependency_p=*/true,
18295 /* If we didn't find a template-id, look for an ordinary
18297 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18299 /* We can get here when cp_parser_template_id, called by
18300 cp_parser_class_name with tag_type == none_type, succeeds
18301 and caches a BASELINK. Then, when called again here,
18302 instead of failing and returning an error_mark_node
18303 returns it (see template/typename17.C in C++11).
18304 ??? Could we diagnose this earlier? */
18305 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18307 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18308 type
= error_mark_node
;
18310 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18311 in effect, then we must assume that, upon instantiation, the
18312 template will correspond to a class. */
18313 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18314 && tag_type
== typename_type
)
18315 type
= make_typename_type (parser
->scope
, decl
,
18317 /*complain=*/tf_error
);
18318 /* If the `typename' keyword is in effect and DECL is not a type
18319 decl, then type is non existent. */
18320 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18322 else if (TREE_CODE (decl
) == TYPE_DECL
)
18324 type
= check_elaborated_type_specifier (tag_type
, decl
,
18325 /*allow_template_p=*/true);
18327 /* If the next token is a semicolon, this must be a specialization,
18328 instantiation, or friend declaration. Check the scope while we
18329 still know whether or not we had a nested-name-specifier. */
18330 if (type
!= error_mark_node
18331 && !nested_name_specifier
&& !is_friend
18332 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18333 check_unqualified_spec_or_inst (type
, token
->location
);
18335 else if (decl
== error_mark_node
)
18336 type
= error_mark_node
;
18341 token
= cp_lexer_peek_token (parser
->lexer
);
18342 identifier
= cp_parser_identifier (parser
);
18344 if (identifier
== error_mark_node
)
18346 parser
->scope
= NULL_TREE
;
18347 return error_mark_node
;
18350 /* For a `typename', we needn't call xref_tag. */
18351 if (tag_type
== typename_type
18352 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18353 return cp_parser_make_typename_type (parser
, identifier
,
18356 /* Template parameter lists apply only if we are not within a
18357 function parameter list. */
18358 bool template_parm_lists_apply
18359 = parser
->num_template_parameter_lists
;
18360 if (template_parm_lists_apply
)
18361 for (cp_binding_level
*s
= current_binding_level
;
18362 s
&& s
->kind
!= sk_template_parms
;
18363 s
= s
->level_chain
)
18364 if (s
->kind
== sk_function_parms
)
18365 template_parm_lists_apply
= false;
18367 /* Look up a qualified name in the usual way. */
18371 tree ambiguous_decls
;
18373 decl
= cp_parser_lookup_name (parser
, identifier
,
18375 /*is_template=*/false,
18376 /*is_namespace=*/false,
18377 /*check_dependency=*/true,
18381 /* If the lookup was ambiguous, an error will already have been
18383 if (ambiguous_decls
)
18384 return error_mark_node
;
18386 /* If we are parsing friend declaration, DECL may be a
18387 TEMPLATE_DECL tree node here. However, we need to check
18388 whether this TEMPLATE_DECL results in valid code. Consider
18389 the following example:
18392 template <class T> class C {};
18395 template <class T> friend class N::C; // #1, valid code
18397 template <class T> class Y {
18398 friend class N::C; // #2, invalid code
18401 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18402 name lookup of `N::C'. We see that friend declaration must
18403 be template for the code to be valid. Note that
18404 processing_template_decl does not work here since it is
18405 always 1 for the above two cases. */
18407 decl
= (cp_parser_maybe_treat_template_as_class
18408 (decl
, /*tag_name_p=*/is_friend
18409 && template_parm_lists_apply
));
18411 if (TREE_CODE (decl
) != TYPE_DECL
)
18413 cp_parser_diagnose_invalid_type_name (parser
,
18416 return error_mark_node
;
18419 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18421 bool allow_template
= (template_parm_lists_apply
18422 || DECL_SELF_REFERENCE_P (decl
));
18423 type
= check_elaborated_type_specifier (tag_type
, decl
,
18426 if (type
== error_mark_node
)
18427 return error_mark_node
;
18430 /* Forward declarations of nested types, such as
18435 are invalid unless all components preceding the final '::'
18436 are complete. If all enclosing types are complete, these
18437 declarations become merely pointless.
18439 Invalid forward declarations of nested types are errors
18440 caught elsewhere in parsing. Those that are pointless arrive
18443 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18444 && !is_friend
&& !processing_explicit_instantiation
)
18445 warning (0, "declaration %qD does not declare anything", decl
);
18447 type
= TREE_TYPE (decl
);
18451 /* An elaborated-type-specifier sometimes introduces a new type and
18452 sometimes names an existing type. Normally, the rule is that it
18453 introduces a new type only if there is not an existing type of
18454 the same name already in scope. For example, given:
18457 void f() { struct S s; }
18459 the `struct S' in the body of `f' is the same `struct S' as in
18460 the global scope; the existing definition is used. However, if
18461 there were no global declaration, this would introduce a new
18462 local class named `S'.
18464 An exception to this rule applies to the following code:
18466 namespace N { struct S; }
18468 Here, the elaborated-type-specifier names a new type
18469 unconditionally; even if there is already an `S' in the
18470 containing scope this declaration names a new type.
18471 This exception only applies if the elaborated-type-specifier
18472 forms the complete declaration:
18476 A declaration consisting solely of `class-key identifier ;' is
18477 either a redeclaration of the name in the current scope or a
18478 forward declaration of the identifier as a class name. It
18479 introduces the name into the current scope.
18481 We are in this situation precisely when the next token is a `;'.
18483 An exception to the exception is that a `friend' declaration does
18484 *not* name a new type; i.e., given:
18486 struct S { friend struct T; };
18488 `T' is not a new type in the scope of `S'.
18490 Also, `new struct S' or `sizeof (struct S)' never results in the
18491 definition of a new type; a new type can only be declared in a
18492 declaration context. */
18498 /* Friends have special name lookup rules. */
18499 ts
= ts_within_enclosing_non_class
;
18500 else if (is_declaration
18501 && cp_lexer_next_token_is (parser
->lexer
,
18503 /* This is a `class-key identifier ;' */
18509 (template_parm_lists_apply
18510 && (cp_parser_next_token_starts_class_definition_p (parser
)
18511 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18512 /* An unqualified name was used to reference this type, so
18513 there were no qualifying templates. */
18514 if (template_parm_lists_apply
18515 && !cp_parser_check_template_parameters (parser
,
18516 /*num_templates=*/0,
18517 /*template_id*/false,
18519 /*declarator=*/NULL
))
18520 return error_mark_node
;
18521 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18525 if (type
== error_mark_node
)
18526 return error_mark_node
;
18528 /* Allow attributes on forward declarations of classes. */
18531 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18532 warning (OPT_Wattributes
,
18533 "attributes ignored on uninstantiated type");
18534 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18535 && ! processing_explicit_instantiation
)
18536 warning (OPT_Wattributes
,
18537 "attributes ignored on template instantiation");
18538 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18539 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18541 warning (OPT_Wattributes
,
18542 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18545 if (tag_type
!= enum_type
)
18547 /* Indicate whether this class was declared as a `class' or as a
18549 if (CLASS_TYPE_P (type
))
18550 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18551 cp_parser_check_class_key (tag_type
, type
);
18554 /* A "<" cannot follow an elaborated type specifier. If that
18555 happens, the user was probably trying to form a template-id. */
18556 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18562 /* Parse an enum-specifier.
18565 enum-head { enumerator-list [opt] }
18566 enum-head { enumerator-list , } [C++0x]
18569 enum-key identifier [opt] enum-base [opt]
18570 enum-key nested-name-specifier identifier enum-base [opt]
18575 enum struct [C++0x]
18578 : type-specifier-seq
18580 opaque-enum-specifier:
18581 enum-key identifier enum-base [opt] ;
18584 enum-key attributes[opt] identifier [opt] enum-base [opt]
18585 { enumerator-list [opt] }attributes[opt]
18586 enum-key attributes[opt] identifier [opt] enum-base [opt]
18587 { enumerator-list, }attributes[opt] [C++0x]
18589 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18590 if the token stream isn't an enum-specifier after all. */
18593 cp_parser_enum_specifier (cp_parser
* parser
)
18596 tree type
= NULL_TREE
;
18598 tree nested_name_specifier
= NULL_TREE
;
18600 bool scoped_enum_p
= false;
18601 bool has_underlying_type
= false;
18602 bool nested_being_defined
= false;
18603 bool new_value_list
= false;
18604 bool is_new_type
= false;
18605 bool is_unnamed
= false;
18606 tree underlying_type
= NULL_TREE
;
18607 cp_token
*type_start_token
= NULL
;
18608 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18610 parser
->colon_corrects_to_scope_p
= false;
18612 /* Parse tentatively so that we can back up if we don't find a
18614 cp_parser_parse_tentatively (parser
);
18616 /* Caller guarantees that the current token is 'enum', an identifier
18617 possibly follows, and the token after that is an opening brace.
18618 If we don't have an identifier, fabricate an anonymous name for
18619 the enumeration being defined. */
18620 cp_lexer_consume_token (parser
->lexer
);
18622 /* Parse the "class" or "struct", which indicates a scoped
18623 enumeration type in C++0x. */
18624 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18625 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18627 if (cxx_dialect
< cxx11
)
18628 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18630 /* Consume the `struct' or `class' token. */
18631 cp_lexer_consume_token (parser
->lexer
);
18633 scoped_enum_p
= true;
18636 attributes
= cp_parser_attributes_opt (parser
);
18638 /* Clear the qualification. */
18639 parser
->scope
= NULL_TREE
;
18640 parser
->qualifying_scope
= NULL_TREE
;
18641 parser
->object_scope
= NULL_TREE
;
18643 /* Figure out in what scope the declaration is being placed. */
18644 prev_scope
= current_scope ();
18646 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18648 push_deferring_access_checks (dk_no_check
);
18649 nested_name_specifier
18650 = cp_parser_nested_name_specifier_opt (parser
,
18651 /*typename_keyword_p=*/true,
18652 /*check_dependency_p=*/false,
18654 /*is_declaration=*/false);
18656 if (nested_name_specifier
)
18660 identifier
= cp_parser_identifier (parser
);
18661 name
= cp_parser_lookup_name (parser
, identifier
,
18663 /*is_template=*/false,
18664 /*is_namespace=*/false,
18665 /*check_dependency=*/true,
18666 /*ambiguous_decls=*/NULL
,
18668 if (name
&& name
!= error_mark_node
)
18670 type
= TREE_TYPE (name
);
18671 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18673 /* Are template enums allowed in ISO? */
18674 if (template_parm_scope_p ())
18675 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18676 "%qD is an enumeration template", name
);
18677 /* ignore a typename reference, for it will be solved by name
18682 else if (nested_name_specifier
== error_mark_node
)
18683 /* We already issued an error. */;
18686 error_at (type_start_token
->location
,
18687 "%qD does not name an enumeration in %qT",
18688 identifier
, nested_name_specifier
);
18689 nested_name_specifier
= error_mark_node
;
18694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18695 identifier
= cp_parser_identifier (parser
);
18698 identifier
= make_anon_name ();
18701 error_at (type_start_token
->location
,
18702 "unnamed scoped enum is not allowed");
18705 pop_deferring_access_checks ();
18707 /* Check for the `:' that denotes a specified underlying type in C++0x.
18708 Note that a ':' could also indicate a bitfield width, however. */
18709 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18711 cp_decl_specifier_seq type_specifiers
;
18713 /* Consume the `:'. */
18714 cp_lexer_consume_token (parser
->lexer
);
18716 /* Parse the type-specifier-seq. */
18717 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
18718 /*is_declaration=*/false,
18719 /*is_trailing_return=*/false,
18722 /* At this point this is surely not elaborated type specifier. */
18723 if (!cp_parser_parse_definitely (parser
))
18726 if (cxx_dialect
< cxx11
)
18727 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18729 has_underlying_type
= true;
18731 /* If that didn't work, stop. */
18732 if (type_specifiers
.type
!= error_mark_node
)
18734 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18735 /*initialized=*/0, NULL
);
18736 if (underlying_type
== error_mark_node
18737 || check_for_bare_parameter_packs (underlying_type
))
18738 underlying_type
= NULL_TREE
;
18742 /* Look for the `{' but don't consume it yet. */
18743 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18745 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18747 cp_parser_error (parser
, "expected %<{%>");
18748 if (has_underlying_type
)
18754 /* An opaque-enum-specifier must have a ';' here. */
18755 if ((scoped_enum_p
|| underlying_type
)
18756 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18758 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18759 if (has_underlying_type
)
18767 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18770 if (nested_name_specifier
)
18772 if (CLASS_TYPE_P (nested_name_specifier
))
18774 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18775 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18776 push_scope (nested_name_specifier
);
18778 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18780 push_nested_namespace (nested_name_specifier
);
18784 /* Issue an error message if type-definitions are forbidden here. */
18785 if (!cp_parser_check_type_definition (parser
))
18786 type
= error_mark_node
;
18788 /* Create the new type. We do this before consuming the opening
18789 brace so the enum will be recorded as being on the line of its
18790 tag (or the 'enum' keyword, if there is no tag). */
18791 type
= start_enum (identifier
, type
, underlying_type
,
18792 attributes
, scoped_enum_p
, &is_new_type
);
18794 /* If the next token is not '{' it is an opaque-enum-specifier or an
18795 elaborated-type-specifier. */
18796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18798 timevar_push (TV_PARSE_ENUM
);
18799 if (nested_name_specifier
18800 && nested_name_specifier
!= error_mark_node
)
18802 /* The following catches invalid code such as:
18803 enum class S<int>::E { A, B, C }; */
18804 if (!processing_specialization
18805 && CLASS_TYPE_P (nested_name_specifier
)
18806 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18807 error_at (type_start_token
->location
, "cannot add an enumerator "
18808 "list to a template instantiation");
18810 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18812 error_at (type_start_token
->location
,
18813 "%<%T::%E%> has not been declared",
18814 TYPE_CONTEXT (nested_name_specifier
),
18815 nested_name_specifier
);
18816 type
= error_mark_node
;
18818 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18819 && !CLASS_TYPE_P (nested_name_specifier
))
18821 error_at (type_start_token
->location
, "nested name specifier "
18822 "%qT for enum declaration does not name a class "
18823 "or namespace", nested_name_specifier
);
18824 type
= error_mark_node
;
18826 /* If that scope does not contain the scope in which the
18827 class was originally declared, the program is invalid. */
18828 else if (prev_scope
&& !is_ancestor (prev_scope
,
18829 nested_name_specifier
))
18831 if (at_namespace_scope_p ())
18832 error_at (type_start_token
->location
,
18833 "declaration of %qD in namespace %qD which does not "
18835 type
, prev_scope
, nested_name_specifier
);
18837 error_at (type_start_token
->location
,
18838 "declaration of %qD in %qD which does not "
18840 type
, prev_scope
, nested_name_specifier
);
18841 type
= error_mark_node
;
18843 /* If that scope is the scope where the declaration is being placed
18844 the program is invalid. */
18845 else if (CLASS_TYPE_P (nested_name_specifier
)
18846 && CLASS_TYPE_P (prev_scope
)
18847 && same_type_p (nested_name_specifier
, prev_scope
))
18849 permerror (type_start_token
->location
,
18850 "extra qualification not allowed");
18851 nested_name_specifier
= NULL_TREE
;
18856 begin_scope (sk_scoped_enum
, type
);
18858 /* Consume the opening brace. */
18859 matching_braces braces
;
18860 braces
.consume_open (parser
);
18862 if (type
== error_mark_node
)
18863 ; /* Nothing to add */
18864 else if (OPAQUE_ENUM_P (type
)
18865 || (cxx_dialect
> cxx98
&& processing_specialization
))
18867 new_value_list
= true;
18868 SET_OPAQUE_ENUM_P (type
, false);
18869 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18873 error_at (type_start_token
->location
,
18874 "multiple definition of %q#T", type
);
18875 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18876 "previous definition here");
18877 type
= error_mark_node
;
18880 if (type
== error_mark_node
)
18881 cp_parser_skip_to_end_of_block_or_statement (parser
);
18882 /* If the next token is not '}', then there are some enumerators. */
18883 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18885 if (is_unnamed
&& !scoped_enum_p
)
18886 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18887 "ISO C++ forbids empty unnamed enum");
18890 cp_parser_enumerator_list (parser
, type
);
18892 /* Consume the final '}'. */
18893 braces
.require_close (parser
);
18897 timevar_pop (TV_PARSE_ENUM
);
18901 /* If a ';' follows, then it is an opaque-enum-specifier
18902 and additional restrictions apply. */
18903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18906 error_at (type_start_token
->location
,
18907 "opaque-enum-specifier without name");
18908 else if (nested_name_specifier
)
18909 error_at (type_start_token
->location
,
18910 "opaque-enum-specifier must use a simple identifier");
18914 /* Look for trailing attributes to apply to this enumeration, and
18915 apply them if appropriate. */
18916 if (cp_parser_allow_gnu_extensions_p (parser
))
18918 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18919 cplus_decl_attributes (&type
,
18921 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18924 /* Finish up the enumeration. */
18925 if (type
!= error_mark_node
)
18927 if (new_value_list
)
18928 finish_enum_value_list (type
);
18930 finish_enum (type
);
18933 if (nested_name_specifier
)
18935 if (CLASS_TYPE_P (nested_name_specifier
))
18937 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18938 pop_scope (nested_name_specifier
);
18940 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18942 pop_nested_namespace (nested_name_specifier
);
18946 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18950 /* Parse an enumerator-list. The enumerators all have the indicated
18954 enumerator-definition
18955 enumerator-list , enumerator-definition */
18958 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18962 /* Parse an enumerator-definition. */
18963 cp_parser_enumerator_definition (parser
, type
);
18965 /* If the next token is not a ',', we've reached the end of
18967 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18969 /* Otherwise, consume the `,' and keep going. */
18970 cp_lexer_consume_token (parser
->lexer
);
18971 /* If the next token is a `}', there is a trailing comma. */
18972 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18974 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18975 pedwarn (input_location
, OPT_Wpedantic
,
18976 "comma at end of enumerator list");
18982 /* Parse an enumerator-definition. The enumerator has the indicated
18985 enumerator-definition:
18987 enumerator = constant-expression
18994 enumerator-definition:
18995 enumerator attributes [opt]
18996 enumerator attributes [opt] = constant-expression */
18999 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
19005 /* Save the input location because we are interested in the location
19006 of the identifier and not the location of the explicit value. */
19007 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
19009 /* Look for the identifier. */
19010 identifier
= cp_parser_identifier (parser
);
19011 if (identifier
== error_mark_node
)
19014 /* Parse any specified attributes. */
19015 tree attrs
= cp_parser_attributes_opt (parser
);
19017 /* If the next token is an '=', then there is an explicit value. */
19018 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19020 /* Consume the `=' token. */
19021 cp_lexer_consume_token (parser
->lexer
);
19022 /* Parse the value. */
19023 value
= cp_parser_constant_expression (parser
);
19028 /* If we are processing a template, make sure the initializer of the
19029 enumerator doesn't contain any bare template parameter pack. */
19030 if (check_for_bare_parameter_packs (value
))
19031 value
= error_mark_node
;
19033 /* Create the enumerator. */
19034 build_enumerator (identifier
, value
, type
, attrs
, loc
);
19037 /* Parse a namespace-name.
19040 original-namespace-name
19043 Returns the NAMESPACE_DECL for the namespace. */
19046 cp_parser_namespace_name (cp_parser
* parser
)
19049 tree namespace_decl
;
19051 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19053 /* Get the name of the namespace. */
19054 identifier
= cp_parser_identifier (parser
);
19055 if (identifier
== error_mark_node
)
19056 return error_mark_node
;
19058 /* Look up the identifier in the currently active scope. Look only
19059 for namespaces, due to:
19061 [basic.lookup.udir]
19063 When looking up a namespace-name in a using-directive or alias
19064 definition, only namespace names are considered.
19068 [basic.lookup.qual]
19070 During the lookup of a name preceding the :: scope resolution
19071 operator, object, function, and enumerator names are ignored.
19073 (Note that cp_parser_qualifying_entity only calls this
19074 function if the token after the name is the scope resolution
19076 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
19078 /*is_template=*/false,
19079 /*is_namespace=*/true,
19080 /*check_dependency=*/true,
19081 /*ambiguous_decls=*/NULL
,
19083 /* If it's not a namespace, issue an error. */
19084 if (namespace_decl
== error_mark_node
19085 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
19087 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
19089 auto_diagnostic_group d
;
19091 if (namespace_decl
== error_mark_node
19092 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
19093 hint
= suggest_alternative_in_explicit_scope (token
->location
,
19096 if (const char *suggestion
= hint
.suggestion ())
19098 gcc_rich_location
richloc (token
->location
);
19099 richloc
.add_fixit_replace (suggestion
);
19100 error_at (&richloc
,
19101 "%qD is not a namespace-name; did you mean %qs?",
19102 identifier
, suggestion
);
19105 error_at (token
->location
, "%qD is not a namespace-name",
19109 cp_parser_error (parser
, "expected namespace-name");
19110 namespace_decl
= error_mark_node
;
19113 return namespace_decl
;
19116 /* Parse a namespace-definition.
19118 namespace-definition:
19119 named-namespace-definition
19120 unnamed-namespace-definition
19122 named-namespace-definition:
19123 original-namespace-definition
19124 extension-namespace-definition
19126 original-namespace-definition:
19127 namespace identifier { namespace-body }
19129 extension-namespace-definition:
19130 namespace original-namespace-name { namespace-body }
19132 unnamed-namespace-definition:
19133 namespace { namespace-body } */
19136 cp_parser_namespace_definition (cp_parser
* parser
)
19139 int nested_definition_count
= 0;
19141 cp_ensure_no_omp_declare_simd (parser
);
19142 cp_ensure_no_oacc_routine (parser
);
19144 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
19145 const bool topmost_inline_p
= is_inline
;
19149 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
19150 cp_lexer_consume_token (parser
->lexer
);
19153 /* Look for the `namespace' keyword. */
19155 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19157 /* Parse any specified attributes before the identifier. */
19158 tree attribs
= cp_parser_attributes_opt (parser
);
19162 identifier
= NULL_TREE
;
19164 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
19166 if (nested_inline_p
&& nested_definition_count
!= 0)
19168 if (cxx_dialect
< cxx2a
)
19169 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
19170 OPT_Wpedantic
, "nested inline namespace definitions only "
19171 "available with -std=c++2a or -std=gnu++2a");
19172 cp_lexer_consume_token (parser
->lexer
);
19175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19177 identifier
= cp_parser_identifier (parser
);
19179 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19180 pedwarn (input_location
, OPT_Wpedantic
,
19181 "standard attributes on namespaces must precede "
19182 "the namespace name");
19184 /* Parse any attributes specified after the identifier. */
19185 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
19188 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
19190 /* Don't forget that the innermost namespace might have been
19191 marked as inline. Use |= because we cannot overwrite
19192 IS_INLINE in case the outermost namespace is inline, but
19193 there are no nested inlines. */
19194 is_inline
|= nested_inline_p
;
19198 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
19199 pedwarn (input_location
, OPT_Wpedantic
,
19200 "nested namespace definitions only available with "
19201 "-std=c++17 or -std=gnu++17");
19203 /* Nested namespace names can create new namespaces (unlike
19204 other qualified-ids). */
19205 if (int count
= (identifier
19206 ? push_namespace (identifier
, nested_inline_p
)
19208 nested_definition_count
+= count
;
19210 cp_parser_error (parser
, "nested namespace name required");
19211 cp_lexer_consume_token (parser
->lexer
);
19214 if (nested_definition_count
&& !identifier
)
19215 cp_parser_error (parser
, "namespace name required");
19217 if (nested_definition_count
&& attribs
)
19218 error_at (token
->location
,
19219 "a nested namespace definition cannot have attributes");
19220 if (nested_definition_count
&& topmost_inline_p
)
19221 error_at (token
->location
,
19222 "a nested namespace definition cannot be inline");
19224 /* Start the namespace. */
19225 nested_definition_count
+= push_namespace (identifier
, is_inline
);
19227 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
19229 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
19231 /* Look for the `{' to validate starting the namespace. */
19232 matching_braces braces
;
19233 if (braces
.require_open (parser
))
19235 /* Parse the body of the namespace. */
19236 cp_parser_namespace_body (parser
);
19238 /* Look for the final `}'. */
19239 braces
.require_close (parser
);
19242 if (has_visibility
)
19243 pop_visibility (1);
19245 /* Pop the nested namespace definitions. */
19246 while (nested_definition_count
--)
19250 /* Parse a namespace-body.
19253 declaration-seq [opt] */
19256 cp_parser_namespace_body (cp_parser
* parser
)
19258 cp_parser_declaration_seq_opt (parser
);
19261 /* Parse a namespace-alias-definition.
19263 namespace-alias-definition:
19264 namespace identifier = qualified-namespace-specifier ; */
19267 cp_parser_namespace_alias_definition (cp_parser
* parser
)
19270 tree namespace_specifier
;
19272 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19274 /* Look for the `namespace' keyword. */
19275 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19276 /* Look for the identifier. */
19277 identifier
= cp_parser_identifier (parser
);
19278 if (identifier
== error_mark_node
)
19280 /* Look for the `=' token. */
19281 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
19282 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19284 error_at (token
->location
, "%<namespace%> definition is not allowed here");
19285 /* Skip the definition. */
19286 cp_lexer_consume_token (parser
->lexer
);
19287 if (cp_parser_skip_to_closing_brace (parser
))
19288 cp_lexer_consume_token (parser
->lexer
);
19291 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19292 /* Look for the qualified-namespace-specifier. */
19293 namespace_specifier
19294 = cp_parser_qualified_namespace_specifier (parser
);
19295 /* Look for the `;' token. */
19296 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19298 /* Register the alias in the symbol table. */
19299 do_namespace_alias (identifier
, namespace_specifier
);
19302 /* Parse a qualified-namespace-specifier.
19304 qualified-namespace-specifier:
19305 :: [opt] nested-name-specifier [opt] namespace-name
19307 Returns a NAMESPACE_DECL corresponding to the specified
19311 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
19313 /* Look for the optional `::'. */
19314 cp_parser_global_scope_opt (parser
,
19315 /*current_scope_valid_p=*/false);
19317 /* Look for the optional nested-name-specifier. */
19318 cp_parser_nested_name_specifier_opt (parser
,
19319 /*typename_keyword_p=*/false,
19320 /*check_dependency_p=*/true,
19322 /*is_declaration=*/true);
19324 return cp_parser_namespace_name (parser
);
19327 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19328 access declaration.
19331 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19332 using :: unqualified-id ;
19334 access-declaration:
19340 cp_parser_using_declaration (cp_parser
* parser
,
19341 bool access_declaration_p
)
19344 bool typename_p
= false;
19345 bool global_scope_p
;
19349 int oldcount
= errorcount
;
19350 cp_token
*diag_token
= NULL
;
19352 if (access_declaration_p
)
19354 diag_token
= cp_lexer_peek_token (parser
->lexer
);
19355 cp_parser_parse_tentatively (parser
);
19359 /* Look for the `using' keyword. */
19360 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19363 /* Peek at the next token. */
19364 token
= cp_lexer_peek_token (parser
->lexer
);
19365 /* See if it's `typename'. */
19366 if (token
->keyword
== RID_TYPENAME
)
19368 /* Remember that we've seen it. */
19370 /* Consume the `typename' token. */
19371 cp_lexer_consume_token (parser
->lexer
);
19375 /* Look for the optional global scope qualification. */
19377 = (cp_parser_global_scope_opt (parser
,
19378 /*current_scope_valid_p=*/false)
19381 /* If we saw `typename', or didn't see `::', then there must be a
19382 nested-name-specifier present. */
19383 if (typename_p
|| !global_scope_p
)
19385 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19386 /*check_dependency_p=*/true,
19388 /*is_declaration=*/true);
19389 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19391 cp_parser_skip_to_end_of_block_or_statement (parser
);
19395 /* Otherwise, we could be in either of the two productions. In that
19396 case, treat the nested-name-specifier as optional. */
19398 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19399 /*typename_keyword_p=*/false,
19400 /*check_dependency_p=*/true,
19402 /*is_declaration=*/true);
19404 qscope
= global_namespace
;
19405 else if (UNSCOPED_ENUM_P (qscope
))
19406 qscope
= CP_TYPE_CONTEXT (qscope
);
19408 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19409 /* Something has already gone wrong; there's no need to parse
19410 further. Since an error has occurred, the return value of
19411 cp_parser_parse_definitely will be false, as required. */
19412 return cp_parser_parse_definitely (parser
);
19414 token
= cp_lexer_peek_token (parser
->lexer
);
19415 /* Parse the unqualified-id. */
19416 identifier
= cp_parser_unqualified_id (parser
,
19417 /*template_keyword_p=*/false,
19418 /*check_dependency_p=*/true,
19419 /*declarator_p=*/true,
19420 /*optional_p=*/false);
19422 if (access_declaration_p
)
19424 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19425 cp_parser_simulate_error (parser
);
19426 if (!cp_parser_parse_definitely (parser
))
19429 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19431 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19432 if (cxx_dialect
< cxx17
19433 && !in_system_header_at (ell
->location
))
19434 pedwarn (ell
->location
, 0,
19435 "pack expansion in using-declaration only available "
19436 "with -std=c++17 or -std=gnu++17");
19437 qscope
= make_pack_expansion (qscope
);
19440 /* The function we call to handle a using-declaration is different
19441 depending on what scope we are in. */
19442 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19444 else if (!identifier_p (identifier
)
19445 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19446 /* [namespace.udecl]
19448 A using declaration shall not name a template-id. */
19449 error_at (token
->location
,
19450 "a template-id may not appear in a using-declaration");
19453 if (at_class_scope_p ())
19455 /* Create the USING_DECL. */
19456 decl
= do_class_using_decl (qscope
, identifier
);
19458 if (decl
&& typename_p
)
19459 USING_DECL_TYPENAME_P (decl
) = 1;
19461 if (check_for_bare_parameter_packs (decl
))
19463 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19467 /* Add it to the list of members in this class. */
19468 finish_member_declaration (decl
);
19472 decl
= cp_parser_lookup_name_simple (parser
,
19475 if (decl
== error_mark_node
)
19476 cp_parser_name_lookup_error (parser
, identifier
,
19479 else if (check_for_bare_parameter_packs (decl
))
19481 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19484 else if (!at_namespace_scope_p ())
19485 finish_local_using_decl (decl
, qscope
, identifier
);
19487 finish_namespace_using_decl (decl
, qscope
, identifier
);
19491 if (!access_declaration_p
19492 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19494 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19495 if (cxx_dialect
< cxx17
)
19496 pedwarn (comma
->location
, 0,
19497 "comma-separated list in using-declaration only available "
19498 "with -std=c++17 or -std=gnu++17");
19502 /* Look for the final `;'. */
19503 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19505 if (access_declaration_p
&& errorcount
== oldcount
)
19506 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19507 "access declarations are deprecated "
19508 "in favour of using-declarations; "
19509 "suggestion: add the %<using%> keyword");
19514 /* Parse an alias-declaration.
19517 using identifier attribute-specifier-seq [opt] = type-id */
19520 cp_parser_alias_declaration (cp_parser
* parser
)
19522 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19523 location_t id_location
, type_location
;
19524 cp_declarator
*declarator
;
19525 cp_decl_specifier_seq decl_specs
;
19527 const char *saved_message
= NULL
;
19529 /* Look for the `using' keyword. */
19530 cp_token
*using_token
19531 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19532 if (using_token
== NULL
)
19533 return error_mark_node
;
19535 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19536 id
= cp_parser_identifier (parser
);
19537 if (id
== error_mark_node
)
19538 return error_mark_node
;
19540 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19541 attributes
= cp_parser_attributes_opt (parser
);
19542 if (attributes
== error_mark_node
)
19543 return error_mark_node
;
19545 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19547 if (cp_parser_error_occurred (parser
))
19548 return error_mark_node
;
19550 cp_parser_commit_to_tentative_parse (parser
);
19552 /* Now we are going to parse the type-id of the declaration. */
19557 "A type-specifier-seq shall not define a class or enumeration
19558 unless it appears in the type-id of an alias-declaration (7.1.3) that
19559 is not the declaration of a template-declaration."
19561 In other words, if we currently are in an alias template, the
19562 type-id should not define a type.
19564 So let's set parser->type_definition_forbidden_message in that
19565 case; cp_parser_check_type_definition (called by
19566 cp_parser_class_specifier) will then emit an error if a type is
19567 defined in the type-id. */
19568 if (parser
->num_template_parameter_lists
)
19570 saved_message
= parser
->type_definition_forbidden_message
;
19571 parser
->type_definition_forbidden_message
=
19572 G_("types may not be defined in alias template declarations");
19575 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
19578 /* Restore the error message if need be. */
19579 if (parser
->num_template_parameter_lists
)
19580 parser
->type_definition_forbidden_message
= saved_message
;
19582 if (type
== error_mark_node
19583 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19585 cp_parser_skip_to_end_of_block_or_statement (parser
);
19586 return error_mark_node
;
19589 /* A typedef-name can also be introduced by an alias-declaration. The
19590 identifier following the using keyword becomes a typedef-name. It has
19591 the same semantics as if it were introduced by the typedef
19592 specifier. In particular, it does not define a new type and it shall
19593 not appear in the type-id. */
19595 clear_decl_specs (&decl_specs
);
19596 decl_specs
.type
= type
;
19597 if (attributes
!= NULL_TREE
)
19599 decl_specs
.attributes
= attributes
;
19600 set_and_check_decl_spec_loc (&decl_specs
,
19604 set_and_check_decl_spec_loc (&decl_specs
,
19607 set_and_check_decl_spec_loc (&decl_specs
,
19610 decl_specs
.locations
[ds_type_spec
] = type_location
;
19612 if (parser
->num_template_parameter_lists
19613 && !cp_parser_check_template_parameters (parser
,
19614 /*num_templates=*/0,
19615 /*template_id*/false,
19617 /*declarator=*/NULL
))
19618 return error_mark_node
;
19620 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
19622 member_p
= at_class_scope_p ();
19624 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19625 NULL_TREE
, attributes
);
19627 decl
= start_decl (declarator
, &decl_specs
, 0,
19628 attributes
, NULL_TREE
, &pushed_scope
);
19629 if (decl
== error_mark_node
)
19632 // Attach constraints to the alias declaration.
19633 if (flag_concepts
&& current_template_parms
)
19635 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19636 tree constr
= build_constraints (reqs
, NULL_TREE
);
19637 set_constraints (decl
, constr
);
19640 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19643 pop_scope (pushed_scope
);
19645 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19646 added into the symbol table; otherwise, return the TYPE_DECL. */
19647 if (DECL_LANG_SPECIFIC (decl
)
19648 && DECL_TEMPLATE_INFO (decl
)
19649 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19651 decl
= DECL_TI_TEMPLATE (decl
);
19653 check_member_template (decl
);
19659 /* Parse a using-directive.
19662 using namespace :: [opt] nested-name-specifier [opt]
19663 namespace-name ; */
19666 cp_parser_using_directive (cp_parser
* parser
)
19668 tree namespace_decl
;
19671 /* Look for the `using' keyword. */
19672 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19673 /* And the `namespace' keyword. */
19674 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19675 /* Look for the optional `::' operator. */
19676 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19677 /* And the optional nested-name-specifier. */
19678 cp_parser_nested_name_specifier_opt (parser
,
19679 /*typename_keyword_p=*/false,
19680 /*check_dependency_p=*/true,
19682 /*is_declaration=*/true);
19683 /* Get the namespace being used. */
19684 namespace_decl
= cp_parser_namespace_name (parser
);
19685 /* And any specified attributes. */
19686 attribs
= cp_parser_attributes_opt (parser
);
19688 /* Update the symbol table. */
19689 if (namespace_bindings_p ())
19690 finish_namespace_using_directive (namespace_decl
, attribs
);
19692 finish_local_using_directive (namespace_decl
, attribs
);
19694 /* Look for the final `;'. */
19695 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19698 /* Parse an asm-definition.
19705 asm-qualifier-list:
19707 asm-qualifier-list asm-qualifier
19710 asm ( string-literal ) ;
19715 asm asm-qualifier-list [opt] ( string-literal ) ;
19716 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19717 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19718 : asm-operand-list [opt] ) ;
19719 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19720 : asm-operand-list [opt]
19721 : asm-clobber-list [opt] ) ;
19722 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19723 : asm-clobber-list [opt]
19724 : asm-goto-list ) ;
19726 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19727 contains goto, and is the only allowed form in that case. No duplicates are
19728 allowed in an asm-qualifier-list. */
19731 cp_parser_asm_definition (cp_parser
* parser
)
19734 tree outputs
= NULL_TREE
;
19735 tree inputs
= NULL_TREE
;
19736 tree clobbers
= NULL_TREE
;
19737 tree labels
= NULL_TREE
;
19739 bool extended_p
= false;
19740 bool invalid_inputs_p
= false;
19741 bool invalid_outputs_p
= false;
19742 required_token missing
= RT_NONE
;
19744 /* Look for the `asm' keyword. */
19745 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19747 if (parser
->in_function_body
19748 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19750 error ("%<asm%> in %<constexpr%> function");
19751 cp_function_chain
->invalid_constexpr
= true;
19754 /* Handle the asm-qualifier-list. */
19755 location_t volatile_loc
= UNKNOWN_LOCATION
;
19756 location_t inline_loc
= UNKNOWN_LOCATION
;
19757 location_t goto_loc
= UNKNOWN_LOCATION
;
19759 if (cp_parser_allow_gnu_extensions_p (parser
) && parser
->in_function_body
)
19762 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19763 location_t loc
= token
->location
;
19764 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
19769 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19770 inform (volatile_loc
, "first seen here");
19773 volatile_loc
= loc
;
19774 cp_lexer_consume_token (parser
->lexer
);
19780 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19781 inform (inline_loc
, "first seen here");
19785 cp_lexer_consume_token (parser
->lexer
);
19791 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19792 inform (goto_loc
, "first seen here");
19796 cp_lexer_consume_token (parser
->lexer
);
19801 error_at (loc
, "%qT is not an asm qualifier", token
->u
.value
);
19802 cp_lexer_consume_token (parser
->lexer
);
19811 bool volatile_p
= (volatile_loc
!= UNKNOWN_LOCATION
);
19812 bool inline_p
= (inline_loc
!= UNKNOWN_LOCATION
);
19813 bool goto_p
= (goto_loc
!= UNKNOWN_LOCATION
);
19815 /* Look for the opening `('. */
19816 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19818 /* Look for the string. */
19819 string
= cp_parser_string_literal (parser
, false, false);
19820 if (string
== error_mark_node
)
19822 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19823 /*consume_paren=*/true);
19827 /* If we're allowing GNU extensions, check for the extended assembly
19828 syntax. Unfortunately, the `:' tokens need not be separated by
19829 a space in C, and so, for compatibility, we tolerate that here
19830 too. Doing that means that we have to treat the `::' operator as
19832 if (cp_parser_allow_gnu_extensions_p (parser
)
19833 && parser
->in_function_body
19834 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19835 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19837 bool inputs_p
= false;
19838 bool clobbers_p
= false;
19839 bool labels_p
= false;
19841 /* The extended syntax was used. */
19844 /* Look for outputs. */
19845 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19847 /* Consume the `:'. */
19848 cp_lexer_consume_token (parser
->lexer
);
19849 /* Parse the output-operands. */
19850 if (cp_lexer_next_token_is_not (parser
->lexer
,
19852 && cp_lexer_next_token_is_not (parser
->lexer
,
19854 && cp_lexer_next_token_is_not (parser
->lexer
,
19858 outputs
= cp_parser_asm_operand_list (parser
);
19859 if (outputs
== error_mark_node
)
19860 invalid_outputs_p
= true;
19863 /* If the next token is `::', there are no outputs, and the
19864 next token is the beginning of the inputs. */
19865 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19866 /* The inputs are coming next. */
19869 /* Look for inputs. */
19871 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19873 /* Consume the `:' or `::'. */
19874 cp_lexer_consume_token (parser
->lexer
);
19875 /* Parse the output-operands. */
19876 if (cp_lexer_next_token_is_not (parser
->lexer
,
19878 && cp_lexer_next_token_is_not (parser
->lexer
,
19880 && cp_lexer_next_token_is_not (parser
->lexer
,
19883 inputs
= cp_parser_asm_operand_list (parser
);
19884 if (inputs
== error_mark_node
)
19885 invalid_inputs_p
= true;
19888 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19889 /* The clobbers are coming next. */
19892 /* Look for clobbers. */
19894 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19897 /* Consume the `:' or `::'. */
19898 cp_lexer_consume_token (parser
->lexer
);
19899 /* Parse the clobbers. */
19900 if (cp_lexer_next_token_is_not (parser
->lexer
,
19902 && cp_lexer_next_token_is_not (parser
->lexer
,
19904 clobbers
= cp_parser_asm_clobber_list (parser
);
19906 else if (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19907 /* The labels are coming next. */
19910 /* Look for labels. */
19912 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19915 /* Consume the `:' or `::'. */
19916 cp_lexer_consume_token (parser
->lexer
);
19917 /* Parse the labels. */
19918 labels
= cp_parser_asm_label_list (parser
);
19921 if (goto_p
&& !labels_p
)
19922 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19925 missing
= RT_COLON_SCOPE
;
19927 /* Look for the closing `)'. */
19928 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19929 missing
? missing
: RT_CLOSE_PAREN
))
19930 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19931 /*consume_paren=*/true);
19932 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19934 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19936 /* Create the ASM_EXPR. */
19937 if (parser
->in_function_body
)
19939 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19940 inputs
, clobbers
, labels
, inline_p
);
19941 /* If the extended syntax was not used, mark the ASM_EXPR. */
19944 tree temp
= asm_stmt
;
19945 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19946 temp
= TREE_OPERAND (temp
, 0);
19948 ASM_INPUT_P (temp
) = 1;
19952 symtab
->finalize_toplevel_asm (string
);
19956 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19957 type that comes from the decl-specifier-seq. */
19960 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19962 for (cp_declarator
*d
= declarator
; d
;)
19972 if (TYPE_PTRMEMFUNC_P (type
))
19973 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19974 type
= TREE_TYPE (type
);
19982 /* Declarators [gram.dcl.decl] */
19984 /* Parse an init-declarator.
19987 declarator initializer [opt]
19992 declarator asm-specification [opt] attributes [opt] initializer [opt]
19994 function-definition:
19995 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19997 decl-specifier-seq [opt] declarator function-try-block
20001 function-definition:
20002 __extension__ function-definition
20006 function-definition:
20007 decl-specifier-seq [opt] declarator function-transaction-block
20009 The parser flags FLAGS is used to control type-specifier parsing.
20011 The DECL_SPECIFIERS apply to this declarator. Returns a
20012 representation of the entity declared. If MEMBER_P is TRUE, then
20013 this declarator appears in a class scope. The new DECL created by
20014 this declarator is returned.
20016 The CHECKS are access checks that should be performed once we know
20017 what entity is being declared (and, therefore, what classes have
20020 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20021 for a function-definition here as well. If the declarator is a
20022 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20023 be TRUE upon return. By that point, the function-definition will
20024 have been completely parsed.
20026 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20029 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20030 parsed declaration if it is an uninitialized single declarator not followed
20031 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20032 if present, will not be consumed. If returned, this declarator will be
20033 created with SD_INITIALIZED but will not call cp_finish_decl.
20035 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20036 and there is an initializer, the pointed location_t is set to the
20037 location of the '=' or `(', or '{' in C++11 token introducing the
20041 cp_parser_init_declarator (cp_parser
* parser
,
20042 cp_parser_flags flags
,
20043 cp_decl_specifier_seq
*decl_specifiers
,
20044 vec
<deferred_access_check
, va_gc
> *checks
,
20045 bool function_definition_allowed_p
,
20047 int declares_class_or_enum
,
20048 bool* function_definition_p
,
20049 tree
* maybe_range_for_decl
,
20050 location_t
* init_loc
,
20053 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
20054 *attributes_start_token
= NULL
;
20055 cp_declarator
*declarator
;
20056 tree prefix_attributes
;
20057 tree attributes
= NULL
;
20058 tree asm_specification
;
20060 tree decl
= NULL_TREE
;
20062 int is_initialized
;
20063 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20064 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20066 enum cpp_ttype initialization_kind
;
20067 bool is_direct_init
= false;
20068 bool is_non_constant_init
;
20069 int ctor_dtor_or_conv_p
;
20070 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
20071 tree pushed_scope
= NULL_TREE
;
20072 bool range_for_decl_p
= false;
20073 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20074 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
20076 /* Gather the attributes that were provided with the
20077 decl-specifiers. */
20078 prefix_attributes
= decl_specifiers
->attributes
;
20080 /* Assume that this is not the declarator for a function
20082 if (function_definition_p
)
20083 *function_definition_p
= false;
20085 /* Default arguments are only permitted for function parameters. */
20086 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
20087 parser
->default_arg_ok_p
= false;
20089 /* Defer access checks while parsing the declarator; we cannot know
20090 what names are accessible until we know what is being
20092 resume_deferring_access_checks ();
20094 token
= cp_lexer_peek_token (parser
->lexer
);
20096 /* Parse the declarator. */
20098 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20099 flags
, &ctor_dtor_or_conv_p
,
20100 /*parenthesized_p=*/NULL
,
20101 member_p
, friend_p
, /*static_p=*/false);
20102 /* Gather up the deferred checks. */
20103 stop_deferring_access_checks ();
20105 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20107 /* If the DECLARATOR was erroneous, there's no need to go
20109 if (declarator
== cp_error_declarator
)
20110 return error_mark_node
;
20112 /* Check that the number of template-parameter-lists is OK. */
20113 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
20115 return error_mark_node
;
20117 if (declares_class_or_enum
& 2)
20118 cp_parser_check_for_definition_in_return_type (declarator
,
20119 decl_specifiers
->type
,
20120 decl_specifiers
->locations
[ds_type_spec
]);
20122 /* Figure out what scope the entity declared by the DECLARATOR is
20123 located in. `grokdeclarator' sometimes changes the scope, so
20124 we compute it now. */
20125 scope
= get_scope_of_declarator (declarator
);
20127 /* Perform any lookups in the declared type which were thought to be
20128 dependent, but are not in the scope of the declarator. */
20129 decl_specifiers
->type
20130 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
20132 /* If we're allowing GNU extensions, look for an
20133 asm-specification. */
20134 if (cp_parser_allow_gnu_extensions_p (parser
))
20136 /* Look for an asm-specification. */
20137 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
20138 asm_specification
= cp_parser_asm_specification_opt (parser
);
20141 asm_specification
= NULL_TREE
;
20143 /* Look for attributes. */
20144 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
20145 attributes
= cp_parser_attributes_opt (parser
);
20147 /* Peek at the next token. */
20148 token
= cp_lexer_peek_token (parser
->lexer
);
20150 bool bogus_implicit_tmpl
= false;
20152 if (function_declarator_p (declarator
))
20154 /* Handle C++17 deduction guides. */
20155 if (!decl_specifiers
->type
20156 && ctor_dtor_or_conv_p
<= 0
20157 && cxx_dialect
>= cxx17
)
20159 cp_declarator
*id
= get_id_declarator (declarator
);
20160 tree name
= id
->u
.id
.unqualified_name
;
20161 parser
->scope
= id
->u
.id
.qualifying_scope
;
20162 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
20164 && (DECL_CLASS_TEMPLATE_P (tmpl
)
20165 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
20167 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
20168 id
->u
.id
.sfk
= sfk_deduction_guide
;
20169 ctor_dtor_or_conv_p
= 1;
20173 /* Check to see if the token indicates the start of a
20174 function-definition. */
20175 if (cp_parser_token_starts_function_definition_p (token
))
20177 if (!function_definition_allowed_p
)
20179 /* If a function-definition should not appear here, issue an
20181 cp_parser_error (parser
,
20182 "a function-definition is not allowed here");
20183 return error_mark_node
;
20186 location_t func_brace_location
20187 = cp_lexer_peek_token (parser
->lexer
)->location
;
20189 /* Neither attributes nor an asm-specification are allowed
20190 on a function-definition. */
20191 if (asm_specification
)
20192 error_at (asm_spec_start_token
->location
,
20193 "an asm-specification is not allowed "
20194 "on a function-definition");
20196 error_at (attributes_start_token
->location
,
20197 "attributes are not allowed "
20198 "on a function-definition");
20199 /* This is a function-definition. */
20200 *function_definition_p
= true;
20202 /* Parse the function definition. */
20204 decl
= cp_parser_save_member_function_body (parser
,
20207 prefix_attributes
);
20210 (cp_parser_function_definition_from_specifiers_and_declarator
20211 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
20213 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
20215 /* This is where the prologue starts... */
20216 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
20217 = func_brace_location
;
20223 else if (parser
->fully_implicit_function_template_p
)
20225 /* A non-template declaration involving a function parameter list
20226 containing an implicit template parameter will be made into a
20227 template. If the resulting declaration is not going to be an
20228 actual function then finish the template scope here to prevent it.
20229 An error message will be issued once we have a decl to talk about.
20231 FIXME probably we should do type deduction rather than create an
20232 implicit template, but the standard currently doesn't allow it. */
20233 bogus_implicit_tmpl
= true;
20234 finish_fully_implicit_template (parser
, NULL_TREE
);
20239 Only in function declarations for constructors, destructors, type
20240 conversions, and deduction guides can the decl-specifier-seq be omitted.
20242 We explicitly postpone this check past the point where we handle
20243 function-definitions because we tolerate function-definitions
20244 that are missing their return types in some modes. */
20245 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
20247 cp_parser_error (parser
,
20248 "expected constructor, destructor, or type conversion");
20249 return error_mark_node
;
20252 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20253 if (token
->type
== CPP_EQ
20254 || token
->type
== CPP_OPEN_PAREN
20255 || token
->type
== CPP_OPEN_BRACE
)
20257 is_initialized
= SD_INITIALIZED
;
20258 initialization_kind
= token
->type
;
20259 if (maybe_range_for_decl
)
20260 *maybe_range_for_decl
= error_mark_node
;
20261 tmp_init_loc
= token
->location
;
20262 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
20263 *init_loc
= tmp_init_loc
;
20265 if (token
->type
== CPP_EQ
20266 && function_declarator_p (declarator
))
20268 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20269 if (t2
->keyword
== RID_DEFAULT
)
20270 is_initialized
= SD_DEFAULTED
;
20271 else if (t2
->keyword
== RID_DELETE
)
20272 is_initialized
= SD_DELETED
;
20277 /* If the init-declarator isn't initialized and isn't followed by a
20278 `,' or `;', it's not a valid init-declarator. */
20279 if (token
->type
!= CPP_COMMA
20280 && token
->type
!= CPP_SEMICOLON
)
20282 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
20283 range_for_decl_p
= true;
20286 if (!maybe_range_for_decl
)
20287 cp_parser_error (parser
, "expected initializer");
20288 return error_mark_node
;
20291 is_initialized
= SD_UNINITIALIZED
;
20292 initialization_kind
= CPP_EOF
;
20295 /* Because start_decl has side-effects, we should only call it if we
20296 know we're going ahead. By this point, we know that we cannot
20297 possibly be looking at any other construct. */
20298 cp_parser_commit_to_tentative_parse (parser
);
20300 /* Enter the newly declared entry in the symbol table. If we're
20301 processing a declaration in a class-specifier, we wait until
20302 after processing the initializer. */
20305 if (parser
->in_unbraced_linkage_specification_p
)
20306 decl_specifiers
->storage_class
= sc_extern
;
20307 decl
= start_decl (declarator
, decl_specifiers
,
20308 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
20309 attributes
, prefix_attributes
, &pushed_scope
);
20310 cp_finalize_omp_declare_simd (parser
, decl
);
20311 cp_finalize_oacc_routine (parser
, decl
, false);
20312 /* Adjust location of decl if declarator->id_loc is more appropriate:
20313 set, and decl wasn't merged with another decl, in which case its
20314 location would be different from input_location, and more accurate. */
20316 && declarator
->id_loc
!= UNKNOWN_LOCATION
20317 && DECL_SOURCE_LOCATION (decl
) == input_location
)
20318 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
20321 /* Enter the SCOPE. That way unqualified names appearing in the
20322 initializer will be looked up in SCOPE. */
20323 pushed_scope
= push_scope (scope
);
20325 /* Perform deferred access control checks, now that we know in which
20326 SCOPE the declared entity resides. */
20327 if (!member_p
&& decl
)
20329 tree saved_current_function_decl
= NULL_TREE
;
20331 /* If the entity being declared is a function, pretend that we
20332 are in its scope. If it is a `friend', it may have access to
20333 things that would not otherwise be accessible. */
20334 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20336 saved_current_function_decl
= current_function_decl
;
20337 current_function_decl
= decl
;
20340 /* Perform access checks for template parameters. */
20341 cp_parser_perform_template_parameter_access_checks (checks
);
20343 /* Perform the access control checks for the declarator and the
20344 decl-specifiers. */
20345 perform_deferred_access_checks (tf_warning_or_error
);
20347 /* Restore the saved value. */
20348 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20349 current_function_decl
= saved_current_function_decl
;
20352 /* Parse the initializer. */
20353 initializer
= NULL_TREE
;
20354 is_direct_init
= false;
20355 is_non_constant_init
= true;
20356 if (is_initialized
)
20358 if (function_declarator_p (declarator
))
20360 if (initialization_kind
== CPP_EQ
)
20361 initializer
= cp_parser_pure_specifier (parser
);
20364 /* If the declaration was erroneous, we don't really
20365 know what the user intended, so just silently
20366 consume the initializer. */
20367 if (decl
!= error_mark_node
)
20368 error_at (tmp_init_loc
, "initializer provided for function");
20369 cp_parser_skip_to_closing_parenthesis (parser
,
20370 /*recovering=*/true,
20371 /*or_comma=*/false,
20372 /*consume_paren=*/true);
20377 /* We want to record the extra mangling scope for in-class
20378 initializers of class members and initializers of static data
20379 member templates. The former involves deferring
20380 parsing of the initializer until end of class as with default
20381 arguments. So right here we only handle the latter. */
20382 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20383 start_lambda_scope (decl
);
20384 initializer
= cp_parser_initializer (parser
,
20386 &is_non_constant_init
);
20387 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20388 finish_lambda_scope ();
20389 if (initializer
== error_mark_node
)
20390 cp_parser_skip_to_end_of_statement (parser
);
20394 /* The old parser allows attributes to appear after a parenthesized
20395 initializer. Mark Mitchell proposed removing this functionality
20396 on the GCC mailing lists on 2002-08-13. This parser accepts the
20397 attributes -- but ignores them. Made a permerror in GCC 8. */
20398 if (cp_parser_allow_gnu_extensions_p (parser
)
20399 && initialization_kind
== CPP_OPEN_PAREN
20400 && cp_parser_attributes_opt (parser
)
20401 && permerror (input_location
,
20402 "attributes after parenthesized initializer ignored"))
20405 if (flag_permissive
&& !hint
)
20408 inform (input_location
,
20409 "this flexibility is deprecated and will be removed");
20413 /* And now complain about a non-function implicit template. */
20414 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20415 error_at (DECL_SOURCE_LOCATION (decl
),
20416 "non-function %qD declared as implicit template", decl
);
20418 /* For an in-class declaration, use `grokfield' to create the
20424 pop_scope (pushed_scope
);
20425 pushed_scope
= NULL_TREE
;
20427 decl
= grokfield (declarator
, decl_specifiers
,
20428 initializer
, !is_non_constant_init
,
20429 /*asmspec=*/NULL_TREE
,
20430 attr_chainon (attributes
, prefix_attributes
));
20431 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20432 cp_parser_save_default_args (parser
, decl
);
20433 cp_finalize_omp_declare_simd (parser
, decl
);
20434 cp_finalize_oacc_routine (parser
, decl
, false);
20437 /* Finish processing the declaration. But, skip member
20439 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20441 cp_finish_decl (decl
,
20442 initializer
, !is_non_constant_init
,
20444 /* If the initializer is in parentheses, then this is
20445 a direct-initialization, which means that an
20446 `explicit' constructor is OK. Otherwise, an
20447 `explicit' constructor cannot be used. */
20448 ((is_direct_init
|| !is_initialized
)
20449 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
20451 else if ((cxx_dialect
!= cxx98
) && friend_p
20452 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20453 /* Core issue #226 (C++0x only): A default template-argument
20454 shall not be specified in a friend class template
20456 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20457 /*is_partial=*/false, /*is_friend_decl=*/1);
20459 if (!friend_p
&& pushed_scope
)
20460 pop_scope (pushed_scope
);
20462 if (function_declarator_p (declarator
)
20463 && parser
->fully_implicit_function_template_p
)
20466 decl
= finish_fully_implicit_template (parser
, decl
);
20468 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
20471 if (auto_result
&& is_initialized
&& decl_specifiers
->type
20472 && type_uses_auto (decl_specifiers
->type
))
20473 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
20478 /* Parse a declarator.
20482 ptr-operator declarator
20484 abstract-declarator:
20485 ptr-operator abstract-declarator [opt]
20486 direct-abstract-declarator
20491 attributes [opt] direct-declarator
20492 attributes [opt] ptr-operator declarator
20494 abstract-declarator:
20495 attributes [opt] ptr-operator abstract-declarator [opt]
20496 attributes [opt] direct-abstract-declarator
20498 The parser flags FLAGS is used to control type-specifier parsing.
20500 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20501 detect constructors, destructors, deduction guides, or conversion operators.
20502 It is set to -1 if the declarator is a name, and +1 if it is a
20503 function. Otherwise it is set to zero. Usually you just want to
20504 test for >0, but internally the negative value is used.
20506 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20507 a decl-specifier-seq unless it declares a constructor, destructor,
20508 or conversion. It might seem that we could check this condition in
20509 semantic analysis, rather than parsing, but that makes it difficult
20510 to handle something like `f()'. We want to notice that there are
20511 no decl-specifiers, and therefore realize that this is an
20512 expression, not a declaration.)
20514 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20515 the declarator is a direct-declarator of the form "(...)".
20517 MEMBER_P is true iff this declarator is a member-declarator.
20519 FRIEND_P is true iff this declarator is a friend.
20521 STATIC_P is true iff the keyword static was seen. */
20523 static cp_declarator
*
20524 cp_parser_declarator (cp_parser
* parser
,
20525 cp_parser_declarator_kind dcl_kind
,
20526 cp_parser_flags flags
,
20527 int* ctor_dtor_or_conv_p
,
20528 bool* parenthesized_p
,
20529 bool member_p
, bool friend_p
, bool static_p
)
20531 cp_declarator
*declarator
;
20532 enum tree_code code
;
20533 cp_cv_quals cv_quals
;
20535 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
20537 /* Assume this is not a constructor, destructor, or type-conversion
20539 if (ctor_dtor_or_conv_p
)
20540 *ctor_dtor_or_conv_p
= 0;
20542 if (cp_parser_allow_gnu_extensions_p (parser
))
20543 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
20545 /* Check for the ptr-operator production. */
20546 cp_parser_parse_tentatively (parser
);
20547 /* Parse the ptr-operator. */
20548 code
= cp_parser_ptr_operator (parser
,
20553 /* If that worked, then we have a ptr-operator. */
20554 if (cp_parser_parse_definitely (parser
))
20556 /* If a ptr-operator was found, then this declarator was not
20558 if (parenthesized_p
)
20559 *parenthesized_p
= true;
20560 /* The dependent declarator is optional if we are parsing an
20561 abstract-declarator. */
20562 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20563 cp_parser_parse_tentatively (parser
);
20565 /* Parse the dependent declarator. */
20566 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20567 CP_PARSER_FLAGS_NONE
,
20568 /*ctor_dtor_or_conv_p=*/NULL
,
20569 /*parenthesized_p=*/NULL
,
20570 /*member_p=*/false,
20571 friend_p
, /*static_p=*/false);
20573 /* If we are parsing an abstract-declarator, we must handle the
20574 case where the dependent declarator is absent. */
20575 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20576 && !cp_parser_parse_definitely (parser
))
20579 declarator
= cp_parser_make_indirect_declarator
20580 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20582 /* Everything else is a direct-declarator. */
20585 if (parenthesized_p
)
20586 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20588 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20589 flags
, ctor_dtor_or_conv_p
,
20590 member_p
, friend_p
, static_p
);
20593 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20594 declarator
->attributes
= gnu_attributes
;
20598 /* Parse a direct-declarator or direct-abstract-declarator.
20602 direct-declarator ( parameter-declaration-clause )
20603 cv-qualifier-seq [opt]
20604 ref-qualifier [opt]
20605 exception-specification [opt]
20606 direct-declarator [ constant-expression [opt] ]
20609 direct-abstract-declarator:
20610 direct-abstract-declarator [opt]
20611 ( parameter-declaration-clause )
20612 cv-qualifier-seq [opt]
20613 ref-qualifier [opt]
20614 exception-specification [opt]
20615 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20616 ( abstract-declarator )
20618 Returns a representation of the declarator. DCL_KIND is
20619 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20620 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20621 we are parsing a direct-declarator. It is
20622 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20623 of ambiguity we prefer an abstract declarator, as per
20625 The parser flags FLAGS is used to control type-specifier parsing.
20626 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20627 as for cp_parser_declarator. */
20629 static cp_declarator
*
20630 cp_parser_direct_declarator (cp_parser
* parser
,
20631 cp_parser_declarator_kind dcl_kind
,
20632 cp_parser_flags flags
,
20633 int* ctor_dtor_or_conv_p
,
20634 bool member_p
, bool friend_p
, bool static_p
)
20637 cp_declarator
*declarator
= NULL
;
20638 tree scope
= NULL_TREE
;
20639 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20640 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20642 tree pushed_scope
= NULL_TREE
;
20643 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20647 /* Peek at the next token. */
20648 token
= cp_lexer_peek_token (parser
->lexer
);
20649 if (token
->type
== CPP_OPEN_PAREN
)
20651 /* This is either a parameter-declaration-clause, or a
20652 parenthesized declarator. When we know we are parsing a
20653 named declarator, it must be a parenthesized declarator
20654 if FIRST is true. For instance, `(int)' is a
20655 parameter-declaration-clause, with an omitted
20656 direct-abstract-declarator. But `((*))', is a
20657 parenthesized abstract declarator. Finally, when T is a
20658 template parameter `(T)' is a
20659 parameter-declaration-clause, and not a parenthesized
20662 We first try and parse a parameter-declaration-clause,
20663 and then try a nested declarator (if FIRST is true).
20665 It is not an error for it not to be a
20666 parameter-declaration-clause, even when FIRST is
20672 The first is the declaration of a function while the
20673 second is the definition of a variable, including its
20676 Having seen only the parenthesis, we cannot know which of
20677 these two alternatives should be selected. Even more
20678 complex are examples like:
20683 The former is a function-declaration; the latter is a
20684 variable initialization.
20686 Thus again, we try a parameter-declaration-clause, and if
20687 that fails, we back out and return. */
20689 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20692 bool is_declarator
= false;
20696 /* In a member-declarator, the only valid interpretation
20697 of a parenthesis is the start of a
20698 parameter-declaration-clause. (It is invalid to
20699 initialize a static data member with a parenthesized
20700 initializer; only the "=" form of initialization is
20703 cp_parser_parse_tentatively (parser
);
20705 /* Consume the `('. */
20706 matching_parens parens
;
20707 parens
.consume_open (parser
);
20710 /* If this is going to be an abstract declarator, we're
20711 in a declarator and we can't have default args. */
20712 parser
->default_arg_ok_p
= false;
20713 parser
->in_declarator_p
= true;
20716 begin_scope (sk_function_parms
, NULL_TREE
);
20718 /* Parse the parameter-declaration-clause. */
20720 = cp_parser_parameter_declaration_clause (parser
, flags
);
20722 /* Consume the `)'. */
20723 parens
.require_close (parser
);
20725 /* If all went well, parse the cv-qualifier-seq,
20726 ref-qualifier and the exception-specification. */
20727 if (member_p
|| cp_parser_parse_definitely (parser
))
20729 cp_cv_quals cv_quals
;
20730 cp_virt_specifiers virt_specifiers
;
20731 cp_ref_qualifier ref_qual
;
20732 tree exception_specification
;
20735 bool memfn
= (member_p
|| (pushed_scope
20736 && CLASS_TYPE_P (pushed_scope
)));
20737 unsigned char local_variables_forbidden_p
20738 = parser
->local_variables_forbidden_p
;
20739 /* 'this' is not allowed in static member functions. */
20740 if (static_p
|| friend_p
)
20741 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
20743 is_declarator
= true;
20745 if (ctor_dtor_or_conv_p
)
20746 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20749 /* Parse the cv-qualifier-seq. */
20750 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20751 /* Parse the ref-qualifier. */
20752 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20753 /* Parse the tx-qualifier. */
20754 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20755 /* And the exception-specification. */
20756 exception_specification
20757 = cp_parser_exception_specification_opt (parser
);
20759 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20761 /* In here, we handle cases where attribute is used after
20762 the function declaration. For example:
20763 void func (int x) __attribute__((vector(..))); */
20764 tree gnu_attrs
= NULL_TREE
;
20765 tree requires_clause
= NULL_TREE
;
20766 late_return
= (cp_parser_late_return_type_opt
20767 (parser
, declarator
, requires_clause
,
20768 memfn
? cv_quals
: -1));
20770 /* Parse the virt-specifier-seq. */
20771 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20773 /* Create the function-declarator. */
20774 declarator
= make_call_declarator (declarator
,
20780 exception_specification
,
20783 declarator
->std_attributes
= attrs
;
20784 declarator
->attributes
= gnu_attrs
;
20785 /* Any subsequent parameter lists are to do with
20786 return type, so are not those of the declared
20788 parser
->default_arg_ok_p
= false;
20790 /* Restore the state of local_variables_forbidden_p. */
20791 parser
->local_variables_forbidden_p
20792 = local_variables_forbidden_p
;
20795 /* Remove the function parms from scope. */
20796 pop_bindings_and_leave_scope ();
20799 /* Repeat the main loop. */
20803 /* If this is the first, we can try a parenthesized
20807 bool saved_in_type_id_in_expr_p
;
20809 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20810 parser
->in_declarator_p
= saved_in_declarator_p
;
20812 open_paren
= token
;
20813 /* Consume the `('. */
20814 matching_parens parens
;
20815 parens
.consume_open (parser
);
20816 /* Parse the nested declarator. */
20817 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20818 parser
->in_type_id_in_expr_p
= true;
20820 = cp_parser_declarator (parser
, dcl_kind
, flags
,
20821 ctor_dtor_or_conv_p
,
20822 /*parenthesized_p=*/NULL
,
20823 member_p
, friend_p
,
20824 /*static_p=*/false);
20825 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20827 /* Expect a `)'. */
20828 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20829 if (!parens
.require_close (parser
))
20830 declarator
= cp_error_declarator
;
20831 if (declarator
== cp_error_declarator
)
20834 goto handle_declarator
;
20836 /* Otherwise, we must be done. */
20840 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20841 && token
->type
== CPP_OPEN_SQUARE
20842 && !cp_next_tokens_can_be_attribute_p (parser
))
20844 /* Parse an array-declarator. */
20845 tree bounds
, attrs
;
20847 if (ctor_dtor_or_conv_p
)
20848 *ctor_dtor_or_conv_p
= 0;
20852 parser
->default_arg_ok_p
= false;
20853 parser
->in_declarator_p
= true;
20854 /* Consume the `['. */
20855 cp_lexer_consume_token (parser
->lexer
);
20856 /* Peek at the next token. */
20857 token
= cp_lexer_peek_token (parser
->lexer
);
20858 /* If the next token is `]', then there is no
20859 constant-expression. */
20860 if (token
->type
!= CPP_CLOSE_SQUARE
)
20862 bool non_constant_p
;
20864 = cp_parser_constant_expression (parser
,
20865 /*allow_non_constant=*/true,
20867 if (!non_constant_p
)
20869 else if (error_operand_p (bounds
))
20870 /* Already gave an error. */;
20871 else if (!parser
->in_function_body
20872 || current_binding_level
->kind
== sk_function_parms
)
20874 /* Normally, the array bound must be an integral constant
20875 expression. However, as an extension, we allow VLAs
20876 in function scopes as long as they aren't part of a
20877 parameter declaration. */
20878 cp_parser_error (parser
,
20879 "array bound is not an integer constant");
20880 bounds
= error_mark_node
;
20882 else if (processing_template_decl
20883 && !type_dependent_expression_p (bounds
))
20885 /* Remember this wasn't a constant-expression. */
20886 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20887 TREE_SIDE_EFFECTS (bounds
) = 1;
20891 bounds
= NULL_TREE
;
20892 /* Look for the closing `]'. */
20893 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20895 declarator
= cp_error_declarator
;
20899 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20900 declarator
= make_array_declarator (declarator
, bounds
);
20901 declarator
->std_attributes
= attrs
;
20903 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20906 tree qualifying_scope
;
20907 tree unqualified_name
;
20909 special_function_kind sfk
;
20911 bool pack_expansion_p
= false;
20912 cp_token
*declarator_id_start_token
;
20914 /* Parse a declarator-id */
20915 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20918 cp_parser_parse_tentatively (parser
);
20920 /* If we see an ellipsis, we should be looking at a
20922 if (token
->type
== CPP_ELLIPSIS
)
20924 /* Consume the `...' */
20925 cp_lexer_consume_token (parser
->lexer
);
20927 pack_expansion_p
= true;
20931 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20933 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20934 qualifying_scope
= parser
->scope
;
20939 if (!unqualified_name
&& pack_expansion_p
)
20941 /* Check whether an error occurred. */
20942 okay
= !cp_parser_error_occurred (parser
);
20944 /* We already consumed the ellipsis to mark a
20945 parameter pack, but we have no way to report it,
20946 so abort the tentative parse. We will be exiting
20947 immediately anyway. */
20948 cp_parser_abort_tentative_parse (parser
);
20951 okay
= cp_parser_parse_definitely (parser
);
20954 unqualified_name
= error_mark_node
;
20955 else if (unqualified_name
20956 && (qualifying_scope
20957 || (!identifier_p (unqualified_name
))))
20959 cp_parser_error (parser
, "expected unqualified-id");
20960 unqualified_name
= error_mark_node
;
20964 if (!unqualified_name
)
20966 if (unqualified_name
== error_mark_node
)
20968 declarator
= cp_error_declarator
;
20969 pack_expansion_p
= false;
20970 declarator
->parameter_pack_p
= false;
20974 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20976 if (qualifying_scope
&& at_namespace_scope_p ()
20977 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20979 /* In the declaration of a member of a template class
20980 outside of the class itself, the SCOPE will sometimes
20981 be a TYPENAME_TYPE. For example, given:
20983 template <typename T>
20984 int S<T>::R::i = 3;
20986 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20987 this context, we must resolve S<T>::R to an ordinary
20988 type, rather than a typename type.
20990 The reason we normally avoid resolving TYPENAME_TYPEs
20991 is that a specialization of `S' might render
20992 `S<T>::R' not a type. However, if `S' is
20993 specialized, then this `i' will not be used, so there
20994 is no harm in resolving the types here. */
20997 /* Resolve the TYPENAME_TYPE. */
20998 type
= resolve_typename_type (qualifying_scope
,
20999 /*only_current_p=*/false);
21000 /* If that failed, the declarator is invalid. */
21001 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21003 if (typedef_variant_p (type
))
21004 error_at (declarator_id_start_token
->location
,
21005 "cannot define member of dependent typedef "
21008 error_at (declarator_id_start_token
->location
,
21009 "%<%T::%E%> is not a type",
21010 TYPE_CONTEXT (qualifying_scope
),
21011 TYPE_IDENTIFIER (qualifying_scope
));
21013 qualifying_scope
= type
;
21018 if (unqualified_name
)
21022 if (qualifying_scope
21023 && CLASS_TYPE_P (qualifying_scope
))
21024 class_type
= qualifying_scope
;
21026 class_type
= current_class_type
;
21028 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
21030 tree name_type
= TREE_TYPE (unqualified_name
);
21032 if (!class_type
|| !same_type_p (name_type
, class_type
))
21034 /* We do not attempt to print the declarator
21035 here because we do not have enough
21036 information about its original syntactic
21038 cp_parser_error (parser
, "invalid declarator");
21039 declarator
= cp_error_declarator
;
21042 else if (qualifying_scope
21043 && CLASSTYPE_USE_TEMPLATE (name_type
))
21045 error_at (declarator_id_start_token
->location
,
21046 "invalid use of constructor as a template");
21047 inform (declarator_id_start_token
->location
,
21048 "use %<%T::%D%> instead of %<%T::%D%> to "
21049 "name the constructor in a qualified name",
21051 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
21052 class_type
, name_type
);
21053 declarator
= cp_error_declarator
;
21056 unqualified_name
= constructor_name (class_type
);
21061 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
21062 sfk
= sfk_destructor
;
21063 else if (identifier_p (unqualified_name
)
21064 && IDENTIFIER_CONV_OP_P (unqualified_name
))
21065 sfk
= sfk_conversion
;
21066 else if (/* There's no way to declare a constructor
21067 for an unnamed type, even if the type
21068 got a name for linkage purposes. */
21069 !TYPE_WAS_UNNAMED (class_type
)
21070 /* Handle correctly (c++/19200):
21084 friend void N::S();
21086 && (!friend_p
|| class_type
== qualifying_scope
)
21087 && constructor_name_p (unqualified_name
,
21089 sfk
= sfk_constructor
;
21090 else if (is_overloaded_fn (unqualified_name
)
21091 && DECL_CONSTRUCTOR_P (get_first_fn
21092 (unqualified_name
)))
21093 sfk
= sfk_constructor
;
21095 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
21096 *ctor_dtor_or_conv_p
= -1;
21099 declarator
= make_id_declarator (qualifying_scope
,
21101 sfk
, token
->location
);
21102 declarator
->std_attributes
= attrs
;
21103 declarator
->parameter_pack_p
= pack_expansion_p
;
21105 if (pack_expansion_p
)
21106 maybe_warn_variadic_templates ();
21108 /* We're looking for this case in [temp.res]:
21109 A qualified-id is assumed to name a type if [...]
21110 - it is a decl-specifier of the decl-specifier-seq of a
21111 parameter-declaration in a declarator of a function or
21112 function template declaration, ... */
21113 if (cxx_dialect
>= cxx2a
21114 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
)
21115 && declarator
->kind
== cdk_id
21116 && !at_class_scope_p ()
21117 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21119 /* ...whose declarator-id is qualified. If it isn't, never
21120 assume the parameters to refer to types. */
21121 if (qualifying_scope
== NULL_TREE
)
21122 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21125 /* Now we have something like
21126 template <typename T> int C::x(S::p);
21127 which can be a function template declaration or a
21128 variable template definition. If name lookup for
21129 the declarator-id C::x finds one or more function
21130 templates, assume S::p to name a type. Otherwise,
21133 = cp_parser_lookup_name_simple (parser
, unqualified_name
,
21135 if (!is_overloaded_fn (decl
))
21136 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21141 handle_declarator
:;
21142 scope
= get_scope_of_declarator (declarator
);
21145 /* Any names that appear after the declarator-id for a
21146 member are looked up in the containing scope. */
21147 if (at_function_scope_p ())
21149 /* But declarations with qualified-ids can't appear in a
21151 cp_parser_error (parser
, "qualified-id in declaration");
21152 declarator
= cp_error_declarator
;
21155 pushed_scope
= push_scope (scope
);
21157 parser
->in_declarator_p
= true;
21158 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
21159 || (declarator
&& declarator
->kind
== cdk_id
))
21160 /* Default args are only allowed on function
21162 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21164 parser
->default_arg_ok_p
= false;
21173 /* For an abstract declarator, we might wind up with nothing at this
21174 point. That's an error; the declarator is not optional. */
21176 cp_parser_error (parser
, "expected declarator");
21177 else if (open_paren
)
21179 /* Record overly parenthesized declarator so we can give a
21180 diagnostic about confusing decl/expr disambiguation. */
21181 if (declarator
->kind
== cdk_array
)
21183 /* If the open and close parens are on different lines, this
21184 is probably a formatting thing, so ignore. */
21185 expanded_location open
= expand_location (open_paren
->location
);
21186 expanded_location close
= expand_location (close_paren
->location
);
21187 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
21191 declarator
->parenthesized
= open_paren
->location
;
21194 /* If we entered a scope, we must exit it now. */
21196 pop_scope (pushed_scope
);
21198 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21199 parser
->in_declarator_p
= saved_in_declarator_p
;
21204 /* Parse a ptr-operator.
21207 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21208 * cv-qualifier-seq [opt]
21210 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21211 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21216 & cv-qualifier-seq [opt]
21218 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21219 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21220 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21221 filled in with the TYPE containing the member. *CV_QUALS is
21222 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21223 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21224 Note that the tree codes returned by this function have nothing
21225 to do with the types of trees that will be eventually be created
21226 to represent the pointer or reference type being parsed. They are
21227 just constants with suggestive names. */
21228 static enum tree_code
21229 cp_parser_ptr_operator (cp_parser
* parser
,
21231 cp_cv_quals
*cv_quals
,
21234 enum tree_code code
= ERROR_MARK
;
21236 tree attrs
= NULL_TREE
;
21238 /* Assume that it's not a pointer-to-member. */
21240 /* And that there are no cv-qualifiers. */
21241 *cv_quals
= TYPE_UNQUALIFIED
;
21243 /* Peek at the next token. */
21244 token
= cp_lexer_peek_token (parser
->lexer
);
21246 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21247 if (token
->type
== CPP_MULT
)
21248 code
= INDIRECT_REF
;
21249 else if (token
->type
== CPP_AND
)
21251 else if ((cxx_dialect
!= cxx98
) &&
21252 token
->type
== CPP_AND_AND
) /* C++0x only */
21253 code
= NON_LVALUE_EXPR
;
21255 if (code
!= ERROR_MARK
)
21257 /* Consume the `*', `&' or `&&'. */
21258 cp_lexer_consume_token (parser
->lexer
);
21260 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21261 `&', if we are allowing GNU extensions. (The only qualifier
21262 that can legally appear after `&' is `restrict', but that is
21263 enforced during semantic analysis. */
21264 if (code
== INDIRECT_REF
21265 || cp_parser_allow_gnu_extensions_p (parser
))
21266 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21268 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21269 if (attributes
!= NULL
)
21270 *attributes
= attrs
;
21274 /* Try the pointer-to-member case. */
21275 cp_parser_parse_tentatively (parser
);
21276 /* Look for the optional `::' operator. */
21277 cp_parser_global_scope_opt (parser
,
21278 /*current_scope_valid_p=*/false);
21279 /* Look for the nested-name specifier. */
21280 token
= cp_lexer_peek_token (parser
->lexer
);
21281 cp_parser_nested_name_specifier (parser
,
21282 /*typename_keyword_p=*/false,
21283 /*check_dependency_p=*/true,
21285 /*is_declaration=*/false);
21286 /* If we found it, and the next token is a `*', then we are
21287 indeed looking at a pointer-to-member operator. */
21288 if (!cp_parser_error_occurred (parser
)
21289 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
21291 /* Indicate that the `*' operator was used. */
21292 code
= INDIRECT_REF
;
21294 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
21295 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
21296 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
21297 error_at (token
->location
, "cannot form pointer to member of "
21298 "non-class %q#T", parser
->scope
);
21301 /* The type of which the member is a member is given by the
21303 *type
= parser
->scope
;
21304 /* The next name will not be qualified. */
21305 parser
->scope
= NULL_TREE
;
21306 parser
->qualifying_scope
= NULL_TREE
;
21307 parser
->object_scope
= NULL_TREE
;
21308 /* Look for optional c++11 attributes. */
21309 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21310 if (attributes
!= NULL
)
21311 *attributes
= attrs
;
21312 /* Look for the optional cv-qualifier-seq. */
21313 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21316 /* If that didn't work we don't have a ptr-operator. */
21317 if (!cp_parser_parse_definitely (parser
))
21318 cp_parser_error (parser
, "expected ptr-operator");
21324 /* Parse an (optional) cv-qualifier-seq.
21327 cv-qualifier cv-qualifier-seq [opt]
21338 Returns a bitmask representing the cv-qualifiers. */
21341 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
21343 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
21348 cp_cv_quals cv_qualifier
;
21350 /* Peek at the next token. */
21351 token
= cp_lexer_peek_token (parser
->lexer
);
21352 /* See if it's a cv-qualifier. */
21353 switch (token
->keyword
)
21356 cv_qualifier
= TYPE_QUAL_CONST
;
21360 cv_qualifier
= TYPE_QUAL_VOLATILE
;
21364 cv_qualifier
= TYPE_QUAL_RESTRICT
;
21368 cv_qualifier
= TYPE_UNQUALIFIED
;
21375 if (cv_quals
& cv_qualifier
)
21377 gcc_rich_location
richloc (token
->location
);
21378 richloc
.add_fixit_remove ();
21379 error_at (&richloc
, "duplicate cv-qualifier");
21380 cp_lexer_purge_token (parser
->lexer
);
21384 cp_lexer_consume_token (parser
->lexer
);
21385 cv_quals
|= cv_qualifier
;
21392 /* Parse an (optional) ref-qualifier
21398 Returns cp_ref_qualifier representing ref-qualifier. */
21400 static cp_ref_qualifier
21401 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
21403 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
21405 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21406 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
21411 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
21412 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21414 switch (token
->type
)
21417 curr_ref_qual
= REF_QUAL_LVALUE
;
21421 curr_ref_qual
= REF_QUAL_RVALUE
;
21425 curr_ref_qual
= REF_QUAL_NONE
;
21429 if (!curr_ref_qual
)
21433 error_at (token
->location
, "multiple ref-qualifiers");
21434 cp_lexer_purge_token (parser
->lexer
);
21438 ref_qual
= curr_ref_qual
;
21439 cp_lexer_consume_token (parser
->lexer
);
21446 /* Parse an optional tx-qualifier.
21450 transaction_safe_dynamic */
21453 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
21455 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21456 if (token
->type
== CPP_NAME
)
21458 tree name
= token
->u
.value
;
21459 const char *p
= IDENTIFIER_POINTER (name
);
21460 const int len
= strlen ("transaction_safe");
21461 if (!strncmp (p
, "transaction_safe", len
))
21465 || !strcmp (p
, "_dynamic"))
21467 cp_lexer_consume_token (parser
->lexer
);
21470 error ("%qE requires %<-fgnu-tm%>", name
);
21481 /* Parse an (optional) virt-specifier-seq.
21483 virt-specifier-seq:
21484 virt-specifier virt-specifier-seq [opt]
21490 Returns a bitmask representing the virt-specifiers. */
21492 static cp_virt_specifiers
21493 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
21495 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21500 cp_virt_specifiers virt_specifier
;
21502 /* Peek at the next token. */
21503 token
= cp_lexer_peek_token (parser
->lexer
);
21504 /* See if it's a virt-specifier-qualifier. */
21505 if (token
->type
!= CPP_NAME
)
21507 if (id_equal (token
->u
.value
, "override"))
21509 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21510 virt_specifier
= VIRT_SPEC_OVERRIDE
;
21512 else if (id_equal (token
->u
.value
, "final"))
21514 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21515 virt_specifier
= VIRT_SPEC_FINAL
;
21517 else if (id_equal (token
->u
.value
, "__final"))
21519 virt_specifier
= VIRT_SPEC_FINAL
;
21524 if (virt_specifiers
& virt_specifier
)
21526 gcc_rich_location
richloc (token
->location
);
21527 richloc
.add_fixit_remove ();
21528 error_at (&richloc
, "duplicate virt-specifier");
21529 cp_lexer_purge_token (parser
->lexer
);
21533 cp_lexer_consume_token (parser
->lexer
);
21534 virt_specifiers
|= virt_specifier
;
21537 return virt_specifiers
;
21540 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21541 is in scope even though it isn't real. */
21544 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
21548 if (current_class_ptr
)
21550 /* We don't clear this between NSDMIs. Is it already what we want? */
21551 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
21552 if (DECL_P (current_class_ptr
)
21553 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
21554 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
21555 && cp_type_quals (type
) == quals
)
21559 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
21560 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21561 current_class_ptr
= NULL_TREE
;
21563 = cp_build_fold_indirect_ref (this_parm
);
21564 current_class_ptr
= this_parm
;
21567 /* Return true iff our current scope is a non-static data member
21571 parsing_nsdmi (void)
21573 /* We recognize NSDMI context by the context-less 'this' pointer set up
21574 by the function above. */
21575 if (current_class_ptr
21576 && TREE_CODE (current_class_ptr
) == PARM_DECL
21577 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
21582 /* Parse a late-specified return type, if any. This is not a separate
21583 non-terminal, but part of a function declarator, which looks like
21585 -> trailing-type-specifier-seq abstract-declarator(opt)
21587 Returns the type indicated by the type-id.
21589 In addition to this, parse any queued up #pragma omp declare simd
21590 clauses, and #pragma acc routine clauses.
21592 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21596 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
21597 tree
& requires_clause
, cp_cv_quals quals
)
21600 tree type
= NULL_TREE
;
21601 bool declare_simd_p
= (parser
->omp_declare_simd
21603 && declarator
->kind
== cdk_id
);
21605 bool oacc_routine_p
= (parser
->oacc_routine
21607 && declarator
->kind
== cdk_id
);
21609 /* Peek at the next token. */
21610 token
= cp_lexer_peek_token (parser
->lexer
);
21611 /* A late-specified return type is indicated by an initial '->'. */
21612 if (token
->type
!= CPP_DEREF
21613 && token
->keyword
!= RID_REQUIRES
21614 && !(token
->type
== CPP_NAME
21615 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21616 && !(declare_simd_p
|| oacc_routine_p
))
21619 tree save_ccp
= current_class_ptr
;
21620 tree save_ccr
= current_class_ref
;
21623 /* DR 1207: 'this' is in scope in the trailing return type. */
21624 inject_this_parameter (current_class_type
, quals
);
21627 if (token
->type
== CPP_DEREF
)
21629 /* Consume the ->. */
21630 cp_lexer_consume_token (parser
->lexer
);
21632 type
= cp_parser_trailing_type_id (parser
);
21635 /* Function declarations may be followed by a trailing
21636 requires-clause. */
21637 requires_clause
= cp_parser_requires_clause_opt (parser
);
21639 if (declare_simd_p
)
21640 declarator
->attributes
21641 = cp_parser_late_parsing_omp_declare_simd (parser
,
21642 declarator
->attributes
);
21643 if (oacc_routine_p
)
21644 declarator
->attributes
21645 = cp_parser_late_parsing_oacc_routine (parser
,
21646 declarator
->attributes
);
21650 current_class_ptr
= save_ccp
;
21651 current_class_ref
= save_ccr
;
21657 /* Parse a declarator-id.
21661 :: [opt] nested-name-specifier [opt] type-name
21663 In the `id-expression' case, the value returned is as for
21664 cp_parser_id_expression if the id-expression was an unqualified-id.
21665 If the id-expression was a qualified-id, then a SCOPE_REF is
21666 returned. The first operand is the scope (either a NAMESPACE_DECL
21667 or TREE_TYPE), but the second is still just a representation of an
21671 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21674 /* The expression must be an id-expression. Assume that qualified
21675 names are the names of types so that:
21678 int S<T>::R::i = 3;
21680 will work; we must treat `S<T>::R' as the name of a type.
21681 Similarly, assume that qualified names are templates, where
21685 int S<T>::R<T>::i = 3;
21688 id
= cp_parser_id_expression (parser
,
21689 /*template_keyword_p=*/false,
21690 /*check_dependency_p=*/false,
21691 /*template_p=*/NULL
,
21692 /*declarator_p=*/true,
21694 if (id
&& BASELINK_P (id
))
21695 id
= BASELINK_FUNCTIONS (id
);
21699 /* Parse a type-id.
21702 type-specifier-seq abstract-declarator [opt]
21704 The parser flags FLAGS is used to control type-specifier parsing.
21706 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21708 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21709 i.e. we've just seen "->".
21711 Returns the TYPE specified. */
21714 cp_parser_type_id_1 (cp_parser
*parser
, cp_parser_flags flags
,
21715 bool is_template_arg
, bool is_trailing_return
,
21716 location_t
*type_location
)
21718 cp_decl_specifier_seq type_specifier_seq
;
21719 cp_declarator
*abstract_declarator
;
21721 /* Parse the type-specifier-seq. */
21722 cp_parser_type_specifier_seq (parser
, flags
,
21723 /*is_declaration=*/false,
21724 is_trailing_return
,
21725 &type_specifier_seq
);
21727 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
21729 if (is_template_arg
&& type_specifier_seq
.type
21730 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21731 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21732 /* A bare template name as a template argument is a template template
21733 argument, not a placeholder, so fail parsing it as a type argument. */
21735 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21736 cp_parser_simulate_error (parser
);
21737 return error_mark_node
;
21739 if (type_specifier_seq
.type
== error_mark_node
)
21740 return error_mark_node
;
21742 /* There might or might not be an abstract declarator. */
21743 cp_parser_parse_tentatively (parser
);
21744 /* Look for the declarator. */
21745 abstract_declarator
21746 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
,
21747 CP_PARSER_FLAGS_NONE
, NULL
,
21748 /*parenthesized_p=*/NULL
,
21749 /*member_p=*/false,
21750 /*friend_p=*/false,
21751 /*static_p=*/false);
21752 /* Check to see if there really was a declarator. */
21753 if (!cp_parser_parse_definitely (parser
))
21754 abstract_declarator
= NULL
;
21756 if (type_specifier_seq
.type
21757 /* The concepts TS allows 'auto' as a type-id. */
21758 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21759 /* None of the valid uses of 'auto' in C++14 involve the type-id
21760 nonterminal, but it is valid in a trailing-return-type. */
21761 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21762 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21764 /* A type-id with type 'auto' is only ok if the abstract declarator
21765 is a function declarator with a late-specified return type.
21767 A type-id with 'auto' is also valid in a trailing-return-type
21768 in a compound-requirement. */
21769 if (abstract_declarator
21770 && abstract_declarator
->kind
== cdk_function
21771 && abstract_declarator
->u
.function
.late_return_type
)
21773 else if (parser
->in_result_type_constraint_p
)
21777 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21778 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21780 error_at (loc
, "missing template arguments after %qT",
21782 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21786 error_at (loc
, "invalid use of %qT", auto_node
);
21787 return error_mark_node
;
21791 return groktypename (&type_specifier_seq
, abstract_declarator
,
21795 /* Wrapper for cp_parser_type_id_1. */
21798 cp_parser_type_id (cp_parser
*parser
, cp_parser_flags flags
,
21799 location_t
*type_location
)
21801 return cp_parser_type_id_1 (parser
, flags
, false, false, type_location
);
21804 /* Wrapper for cp_parser_type_id_1. */
21807 cp_parser_template_type_arg (cp_parser
*parser
)
21810 const char *saved_message
= parser
->type_definition_forbidden_message
;
21811 parser
->type_definition_forbidden_message
21812 = G_("types may not be defined in template arguments");
21813 r
= cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_NONE
, true, false, NULL
);
21814 parser
->type_definition_forbidden_message
= saved_message
;
21815 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21817 error ("invalid use of %<auto%> in template argument");
21818 r
= error_mark_node
;
21823 /* Wrapper for cp_parser_type_id_1. */
21826 cp_parser_trailing_type_id (cp_parser
*parser
)
21828 return cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
21829 false, true, NULL
);
21832 /* Parse a type-specifier-seq.
21834 type-specifier-seq:
21835 type-specifier type-specifier-seq [opt]
21839 type-specifier-seq:
21840 attributes type-specifier-seq [opt]
21842 The parser flags FLAGS is used to control type-specifier parsing.
21844 If IS_DECLARATION is true, we are at the start of a "condition" or
21845 exception-declaration, so we might be followed by a declarator-id.
21847 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21848 i.e. we've just seen "->".
21850 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21853 cp_parser_type_specifier_seq (cp_parser
* parser
,
21854 cp_parser_flags flags
,
21855 bool is_declaration
,
21856 bool is_trailing_return
,
21857 cp_decl_specifier_seq
*type_specifier_seq
)
21859 bool seen_type_specifier
= false;
21860 cp_token
*start_token
= NULL
;
21862 /* Clear the TYPE_SPECIFIER_SEQ. */
21863 clear_decl_specs (type_specifier_seq
);
21865 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
21866 /* In the context of a trailing return type, enum E { } is an
21867 elaborated-type-specifier followed by a function-body, not an
21869 if (is_trailing_return
)
21870 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21872 /* Parse the type-specifiers and attributes. */
21875 tree type_specifier
;
21876 bool is_cv_qualifier
;
21878 /* Check for attributes first. */
21879 if (cp_next_tokens_can_be_attribute_p (parser
))
21881 type_specifier_seq
->attributes
21882 = attr_chainon (type_specifier_seq
->attributes
,
21883 cp_parser_attributes_opt (parser
));
21887 /* record the token of the beginning of the type specifier seq,
21888 for error reporting purposes*/
21890 start_token
= cp_lexer_peek_token (parser
->lexer
);
21892 /* Look for the type-specifier. */
21893 type_specifier
= cp_parser_type_specifier (parser
,
21895 type_specifier_seq
,
21896 /*is_declaration=*/false,
21899 if (!type_specifier
)
21901 /* If the first type-specifier could not be found, this is not a
21902 type-specifier-seq at all. */
21903 if (!seen_type_specifier
)
21905 /* Set in_declarator_p to avoid skipping to the semicolon. */
21906 int in_decl
= parser
->in_declarator_p
;
21907 parser
->in_declarator_p
= true;
21909 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21910 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21911 cp_parser_error (parser
, "expected type-specifier");
21913 parser
->in_declarator_p
= in_decl
;
21915 type_specifier_seq
->type
= error_mark_node
;
21918 /* If subsequent type-specifiers could not be found, the
21919 type-specifier-seq is complete. */
21923 seen_type_specifier
= true;
21924 /* The standard says that a condition can be:
21926 type-specifier-seq declarator = assignment-expression
21933 we should treat the "S" as a declarator, not as a
21934 type-specifier. The standard doesn't say that explicitly for
21935 type-specifier-seq, but it does say that for
21936 decl-specifier-seq in an ordinary declaration. Perhaps it
21937 would be clearer just to allow a decl-specifier-seq here, and
21938 then add a semantic restriction that if any decl-specifiers
21939 that are not type-specifiers appear, the program is invalid. */
21940 if (is_declaration
&& !is_cv_qualifier
)
21941 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21945 /* Return whether the function currently being declared has an associated
21946 template parameter list. */
21949 function_being_declared_is_template_p (cp_parser
* parser
)
21951 if (!current_template_parms
|| processing_template_parmlist
)
21954 if (parser
->implicit_template_scope
)
21957 if (at_class_scope_p ()
21958 && TYPE_BEING_DEFINED (current_class_type
))
21959 return parser
->num_template_parameter_lists
!= 0;
21961 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21962 (current_class_type
));
21965 /* Parse a parameter-declaration-clause.
21967 parameter-declaration-clause:
21968 parameter-declaration-list [opt] ... [opt]
21969 parameter-declaration-list , ...
21971 The parser flags FLAGS is used to control type-specifier parsing.
21973 Returns a representation for the parameter declarations. A return
21974 value of NULL indicates a parameter-declaration-clause consisting
21975 only of an ellipsis. */
21978 cp_parser_parameter_declaration_clause (cp_parser
* parser
,
21979 cp_parser_flags flags
)
21985 temp_override
<bool> cleanup
21986 (parser
->auto_is_implicit_function_template_parm_p
);
21988 if (!processing_specialization
21989 && !processing_template_parmlist
21990 && !processing_explicit_instantiation
21991 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21992 actual function or a random abstract declarator. */
21993 && parser
->default_arg_ok_p
)
21994 if (!current_function_decl
21995 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21996 parser
->auto_is_implicit_function_template_parm_p
= true;
21998 /* Peek at the next token. */
21999 token
= cp_lexer_peek_token (parser
->lexer
);
22000 /* Check for trivial parameter-declaration-clauses. */
22001 if (token
->type
== CPP_ELLIPSIS
)
22003 /* Consume the `...' token. */
22004 cp_lexer_consume_token (parser
->lexer
);
22007 else if (token
->type
== CPP_CLOSE_PAREN
)
22008 /* There are no parameters. */
22009 return void_list_node
;
22010 /* Check for `(void)', too, which is a special case. */
22011 else if (token
->keyword
== RID_VOID
22012 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22013 == CPP_CLOSE_PAREN
))
22015 /* Consume the `void' token. */
22016 cp_lexer_consume_token (parser
->lexer
);
22017 /* There are no parameters. */
22018 return void_list_node
;
22021 /* Parse the parameter-declaration-list. */
22022 parameters
= cp_parser_parameter_declaration_list (parser
, flags
);
22023 /* If a parse error occurred while parsing the
22024 parameter-declaration-list, then the entire
22025 parameter-declaration-clause is erroneous. */
22026 if (parameters
== error_mark_node
)
22029 /* Peek at the next token. */
22030 token
= cp_lexer_peek_token (parser
->lexer
);
22031 /* If it's a `,', the clause should terminate with an ellipsis. */
22032 if (token
->type
== CPP_COMMA
)
22034 /* Consume the `,'. */
22035 cp_lexer_consume_token (parser
->lexer
);
22036 /* Expect an ellipsis. */
22038 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
22040 /* It might also be `...' if the optional trailing `,' was
22042 else if (token
->type
== CPP_ELLIPSIS
)
22044 /* Consume the `...' token. */
22045 cp_lexer_consume_token (parser
->lexer
);
22046 /* And remember that we saw it. */
22050 ellipsis_p
= false;
22052 /* Finish the parameter list. */
22054 parameters
= chainon (parameters
, void_list_node
);
22059 /* Parse a parameter-declaration-list.
22061 parameter-declaration-list:
22062 parameter-declaration
22063 parameter-declaration-list , parameter-declaration
22065 The parser flags FLAGS is used to control type-specifier parsing.
22067 Returns a representation of the parameter-declaration-list, as for
22068 cp_parser_parameter_declaration_clause. However, the
22069 `void_list_node' is never appended to the list. */
22072 cp_parser_parameter_declaration_list (cp_parser
* parser
, cp_parser_flags flags
)
22074 tree parameters
= NULL_TREE
;
22075 tree
*tail
= ¶meters
;
22076 bool saved_in_unbraced_linkage_specification_p
;
22079 /* The special considerations that apply to a function within an
22080 unbraced linkage specifications do not apply to the parameters
22081 to the function. */
22082 saved_in_unbraced_linkage_specification_p
22083 = parser
->in_unbraced_linkage_specification_p
;
22084 parser
->in_unbraced_linkage_specification_p
= false;
22086 /* Look for more parameters. */
22089 cp_parameter_declarator
*parameter
;
22090 tree decl
= error_mark_node
;
22091 bool parenthesized_p
= false;
22093 /* Parse the parameter. */
22095 = cp_parser_parameter_declaration (parser
, flags
,
22096 /*template_parm_p=*/false,
22099 /* We don't know yet if the enclosing context is deprecated, so wait
22100 and warn in grokparms if appropriate. */
22101 deprecated_state
= DEPRECATED_SUPPRESS
;
22105 decl
= grokdeclarator (parameter
->declarator
,
22106 ¶meter
->decl_specifiers
,
22108 parameter
->default_argument
!= NULL_TREE
,
22109 ¶meter
->decl_specifiers
.attributes
);
22110 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
22111 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
22114 deprecated_state
= DEPRECATED_NORMAL
;
22116 /* If a parse error occurred parsing the parameter declaration,
22117 then the entire parameter-declaration-list is erroneous. */
22118 if (decl
== error_mark_node
)
22120 parameters
= error_mark_node
;
22124 if (parameter
->decl_specifiers
.attributes
)
22125 cplus_decl_attributes (&decl
,
22126 parameter
->decl_specifiers
.attributes
,
22128 if (DECL_NAME (decl
))
22129 decl
= pushdecl (decl
);
22131 if (decl
!= error_mark_node
)
22133 retrofit_lang_decl (decl
);
22134 DECL_PARM_INDEX (decl
) = ++index
;
22135 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
22138 /* Add the new parameter to the list. */
22139 *tail
= build_tree_list (parameter
->default_argument
, decl
);
22140 tail
= &TREE_CHAIN (*tail
);
22142 /* Peek at the next token. */
22143 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
22144 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
22145 /* These are for Objective-C++ */
22146 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22147 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22148 /* The parameter-declaration-list is complete. */
22150 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22154 /* Peek at the next token. */
22155 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22156 /* If it's an ellipsis, then the list is complete. */
22157 if (token
->type
== CPP_ELLIPSIS
)
22159 /* Otherwise, there must be more parameters. Consume the
22161 cp_lexer_consume_token (parser
->lexer
);
22162 /* When parsing something like:
22164 int i(float f, double d)
22166 we can tell after seeing the declaration for "f" that we
22167 are not looking at an initialization of a variable "i",
22168 but rather at the declaration of a function "i".
22170 Due to the fact that the parsing of template arguments
22171 (as specified to a template-id) requires backtracking we
22172 cannot use this technique when inside a template argument
22174 if (!parser
->in_template_argument_list_p
22175 && !parser
->in_type_id_in_expr_p
22176 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22177 /* However, a parameter-declaration of the form
22178 "float(f)" (which is a valid declaration of a
22179 parameter "f") can also be interpreted as an
22180 expression (the conversion of "f" to "float"). */
22181 && !parenthesized_p
)
22182 cp_parser_commit_to_tentative_parse (parser
);
22186 cp_parser_error (parser
, "expected %<,%> or %<...%>");
22187 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
22188 cp_parser_skip_to_closing_parenthesis (parser
,
22189 /*recovering=*/true,
22190 /*or_comma=*/false,
22191 /*consume_paren=*/false);
22196 parser
->in_unbraced_linkage_specification_p
22197 = saved_in_unbraced_linkage_specification_p
;
22199 /* Reset implicit_template_scope if we are about to leave the function
22200 parameter list that introduced it. Note that for out-of-line member
22201 definitions, there will be one or more class scopes before we get to
22202 the template parameter scope. */
22204 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
22205 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
22207 while (maybe_its
->kind
== sk_class
)
22208 maybe_its
= maybe_its
->level_chain
;
22209 if (maybe_its
== its
)
22211 parser
->implicit_template_parms
= 0;
22212 parser
->implicit_template_scope
= 0;
22219 /* Parse a parameter declaration.
22221 parameter-declaration:
22222 decl-specifier-seq ... [opt] declarator
22223 decl-specifier-seq declarator = assignment-expression
22224 decl-specifier-seq ... [opt] abstract-declarator [opt]
22225 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22227 The parser flags FLAGS is used to control type-specifier parsing.
22229 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22230 declares a template parameter. (In that case, a non-nested `>'
22231 token encountered during the parsing of the assignment-expression
22232 is not interpreted as a greater-than operator.)
22234 Returns a representation of the parameter, or NULL if an error
22235 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22236 true iff the declarator is of the form "(p)". */
22238 static cp_parameter_declarator
*
22239 cp_parser_parameter_declaration (cp_parser
*parser
,
22240 cp_parser_flags flags
,
22241 bool template_parm_p
,
22242 bool *parenthesized_p
)
22244 int declares_class_or_enum
;
22245 cp_decl_specifier_seq decl_specifiers
;
22246 cp_declarator
*declarator
;
22247 tree default_argument
;
22248 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
22249 const char *saved_message
;
22250 bool template_parameter_pack_p
= false;
22252 /* In a template parameter, `>' is not an operator.
22256 When parsing a default template-argument for a non-type
22257 template-parameter, the first non-nested `>' is taken as the end
22258 of the template parameter-list rather than a greater-than
22261 /* Type definitions may not appear in parameter types. */
22262 saved_message
= parser
->type_definition_forbidden_message
;
22263 parser
->type_definition_forbidden_message
22264 = G_("types may not be defined in parameter types");
22266 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
22267 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22268 (current_template_parms
)) : 0);
22270 /* Parse the declaration-specifiers. */
22271 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
22272 cp_parser_decl_specifier_seq (parser
,
22275 &declares_class_or_enum
);
22277 /* Complain about missing 'typename' or other invalid type names. */
22278 if (!decl_specifiers
.any_type_specifiers_p
22279 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22280 decl_specifiers
.type
= error_mark_node
;
22282 /* If an error occurred, there's no reason to attempt to parse the
22283 rest of the declaration. */
22284 if (cp_parser_error_occurred (parser
))
22286 parser
->type_definition_forbidden_message
= saved_message
;
22290 /* Peek at the next token. */
22291 token
= cp_lexer_peek_token (parser
->lexer
);
22293 /* If the next token is a `)', `,', `=', `>', or `...', then there
22294 is no declarator. However, when variadic templates are enabled,
22295 there may be a declarator following `...'. */
22296 if (token
->type
== CPP_CLOSE_PAREN
22297 || token
->type
== CPP_COMMA
22298 || token
->type
== CPP_EQ
22299 || token
->type
== CPP_GREATER
)
22302 if (parenthesized_p
)
22303 *parenthesized_p
= false;
22305 /* Otherwise, there should be a declarator. */
22308 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22309 parser
->default_arg_ok_p
= false;
22311 /* After seeing a decl-specifier-seq, if the next token is not a
22312 "(", there is no possibility that the code is a valid
22313 expression. Therefore, if parsing tentatively, we commit at
22315 if (!parser
->in_template_argument_list_p
22316 /* In an expression context, having seen:
22320 we cannot be sure whether we are looking at a
22321 function-type (taking a "char" as a parameter) or a cast
22322 of some object of type "char" to "int". */
22323 && !parser
->in_type_id_in_expr_p
22324 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22325 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
22326 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
22327 cp_parser_commit_to_tentative_parse (parser
);
22328 /* Parse the declarator. */
22329 declarator_token_start
= token
;
22330 declarator
= cp_parser_declarator (parser
,
22331 CP_PARSER_DECLARATOR_EITHER
,
22332 CP_PARSER_FLAGS_NONE
,
22333 /*ctor_dtor_or_conv_p=*/NULL
,
22335 /*member_p=*/false,
22336 /*friend_p=*/false,
22337 /*static_p=*/false);
22338 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22339 /* After the declarator, allow more attributes. */
22340 decl_specifiers
.attributes
22341 = attr_chainon (decl_specifiers
.attributes
,
22342 cp_parser_attributes_opt (parser
));
22344 /* If the declarator is a template parameter pack, remember that and
22345 clear the flag in the declarator itself so we don't get errors
22346 from grokdeclarator. */
22347 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
22349 declarator
->parameter_pack_p
= false;
22350 template_parameter_pack_p
= true;
22354 /* If the next token is an ellipsis, and we have not seen a declarator
22355 name, and if either the type of the declarator contains parameter
22356 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22357 for, eg, abbreviated integral type names), then we actually have a
22358 parameter pack expansion expression. Otherwise, leave the ellipsis
22359 for a C-style variadic function. */
22360 token
= cp_lexer_peek_token (parser
->lexer
);
22362 /* If a function parameter pack was specified and an implicit template
22363 parameter was introduced during cp_parser_parameter_declaration,
22364 change any implicit parameters introduced into packs. */
22365 if (parser
->implicit_template_parms
22366 && ((token
->type
== CPP_ELLIPSIS
22367 && declarator_can_be_parameter_pack (declarator
))
22368 || (declarator
&& declarator
->parameter_pack_p
)))
22370 int latest_template_parm_idx
= TREE_VEC_LENGTH
22371 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
22373 if (latest_template_parm_idx
!= template_parm_idx
)
22374 decl_specifiers
.type
= convert_generic_types_to_packs
22375 (decl_specifiers
.type
,
22376 template_parm_idx
, latest_template_parm_idx
);
22379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22381 tree type
= decl_specifiers
.type
;
22383 if (type
&& DECL_P (type
))
22384 type
= TREE_TYPE (type
);
22387 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
22388 && (template_parm_p
|| uses_parameter_packs (type
)))
22389 || (!type
&& template_parm_p
))
22390 && declarator_can_be_parameter_pack (declarator
))
22392 /* Consume the `...'. */
22393 cp_lexer_consume_token (parser
->lexer
);
22394 maybe_warn_variadic_templates ();
22396 /* Build a pack expansion type */
22397 if (template_parm_p
)
22398 template_parameter_pack_p
= true;
22399 else if (declarator
)
22400 declarator
->parameter_pack_p
= true;
22402 decl_specifiers
.type
= make_pack_expansion (type
);
22406 /* The restriction on defining new types applies only to the type
22407 of the parameter, not to the default argument. */
22408 parser
->type_definition_forbidden_message
= saved_message
;
22410 /* If the next token is `=', then process a default argument. */
22411 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22413 tree type
= decl_specifiers
.type
;
22414 token
= cp_lexer_peek_token (parser
->lexer
);
22415 /* If we are defining a class, then the tokens that make up the
22416 default argument must be saved and processed later. */
22417 if (!template_parm_p
&& at_class_scope_p ()
22418 && TYPE_BEING_DEFINED (current_class_type
)
22419 && !LAMBDA_TYPE_P (current_class_type
))
22420 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
22422 // A constrained-type-specifier may declare a type template-parameter.
22423 else if (declares_constrained_type_template_parameter (type
))
22425 = cp_parser_default_type_template_argument (parser
);
22427 // A constrained-type-specifier may declare a template-template-parameter.
22428 else if (declares_constrained_template_template_parameter (type
))
22430 = cp_parser_default_template_template_argument (parser
);
22432 /* Outside of a class definition, we can just parse the
22433 assignment-expression. */
22436 = cp_parser_default_argument (parser
, template_parm_p
);
22438 if (!parser
->default_arg_ok_p
)
22440 permerror (token
->location
,
22441 "default arguments are only "
22442 "permitted for function parameters");
22444 else if ((declarator
&& declarator
->parameter_pack_p
)
22445 || template_parameter_pack_p
22446 || (decl_specifiers
.type
22447 && PACK_EXPANSION_P (decl_specifiers
.type
)))
22449 /* Find the name of the parameter pack. */
22450 cp_declarator
*id_declarator
= declarator
;
22451 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
22452 id_declarator
= id_declarator
->declarator
;
22454 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
22455 error_at (declarator_token_start
->location
,
22457 ? G_("template parameter pack %qD "
22458 "cannot have a default argument")
22459 : G_("parameter pack %qD cannot have "
22460 "a default argument"),
22461 id_declarator
->u
.id
.unqualified_name
);
22463 error_at (declarator_token_start
->location
,
22465 ? G_("template parameter pack cannot have "
22466 "a default argument")
22467 : G_("parameter pack cannot have a "
22468 "default argument"));
22470 default_argument
= NULL_TREE
;
22474 default_argument
= NULL_TREE
;
22476 if (default_argument
)
22477 STRIP_ANY_LOCATION_WRAPPER (default_argument
);
22479 /* Generate a location for the parameter, ranging from the start of the
22480 initial token to the end of the final token (using input_location for
22481 the latter, set up by cp_lexer_set_source_position_from_token when
22484 If we have a identifier, then use it for the caret location, e.g.
22486 extern int callee (int one, int (*two)(int, int), float three);
22487 ~~~~~~^~~~~~~~~~~~~~
22489 otherwise, reuse the start location for the caret location e.g.:
22491 extern int callee (int one, int (*)(int, int), float three);
22495 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
22496 ? declarator
->id_loc
22497 : decl_spec_token_start
->location
);
22498 location_t param_loc
= make_location (caret_loc
,
22499 decl_spec_token_start
->location
,
22502 return make_parameter_declarator (&decl_specifiers
,
22506 template_parameter_pack_p
);
22509 /* Parse a default argument and return it.
22511 TEMPLATE_PARM_P is true if this is a default argument for a
22512 non-type template parameter. */
22514 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
22516 tree default_argument
= NULL_TREE
;
22517 bool saved_greater_than_is_operator_p
;
22518 unsigned char saved_local_variables_forbidden_p
;
22519 bool non_constant_p
, is_direct_init
;
22521 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22523 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
22524 parser
->greater_than_is_operator_p
= !template_parm_p
;
22525 /* Local variable names (and the `this' keyword) may not
22526 appear in a default argument. */
22527 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22528 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
22529 /* Parse the assignment-expression. */
22530 if (template_parm_p
)
22531 push_deferring_access_checks (dk_no_deferred
);
22532 tree saved_class_ptr
= NULL_TREE
;
22533 tree saved_class_ref
= NULL_TREE
;
22534 /* The "this" pointer is not valid in a default argument. */
22537 saved_class_ptr
= current_class_ptr
;
22538 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
22539 saved_class_ref
= current_class_ref
;
22540 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
22543 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
22544 /* Restore the "this" pointer. */
22547 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
22548 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
22550 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
22551 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22552 if (template_parm_p
)
22553 pop_deferring_access_checks ();
22554 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
22555 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22557 return default_argument
;
22560 /* Parse a function-body.
22563 compound_statement */
22566 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
22568 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
22569 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
22573 /* Parse a ctor-initializer-opt followed by a function-body. Return
22574 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22575 is true we are parsing a function-try-block. */
22578 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
22579 bool in_function_try_block
)
22582 const bool check_body_p
=
22583 DECL_CONSTRUCTOR_P (current_function_decl
)
22584 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
22587 /* Begin the function body. */
22588 body
= begin_function_body ();
22589 /* Parse the optional ctor-initializer. */
22590 cp_parser_ctor_initializer_opt (parser
);
22592 /* If we're parsing a constexpr constructor definition, we need
22593 to check that the constructor body is indeed empty. However,
22594 before we get to cp_parser_function_body lot of junk has been
22595 generated, so we can't just check that we have an empty block.
22596 Rather we take a snapshot of the outermost block, and check whether
22597 cp_parser_function_body changed its state. */
22600 list
= cur_stmt_list
;
22601 if (STATEMENT_LIST_TAIL (list
))
22602 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
22604 /* Parse the function-body. */
22605 cp_parser_function_body (parser
, in_function_try_block
);
22607 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
22608 /* Finish the function body. */
22609 finish_function_body (body
);
22612 /* Parse an initializer.
22615 = initializer-clause
22616 ( expression-list )
22618 Returns an expression representing the initializer. If no
22619 initializer is present, NULL_TREE is returned.
22621 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22622 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22623 set to TRUE if there is no initializer present. If there is an
22624 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22625 is set to true; otherwise it is set to false. */
22628 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
22629 bool* non_constant_p
, bool subexpression_p
)
22634 /* Peek at the next token. */
22635 token
= cp_lexer_peek_token (parser
->lexer
);
22637 /* Let our caller know whether or not this initializer was
22639 *is_direct_init
= (token
->type
!= CPP_EQ
);
22640 /* Assume that the initializer is constant. */
22641 *non_constant_p
= false;
22643 if (token
->type
== CPP_EQ
)
22645 /* Consume the `='. */
22646 cp_lexer_consume_token (parser
->lexer
);
22647 /* Parse the initializer-clause. */
22648 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22650 else if (token
->type
== CPP_OPEN_PAREN
)
22652 vec
<tree
, va_gc
> *vec
;
22653 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22655 /*allow_expansion_p=*/true,
22658 return error_mark_node
;
22659 init
= build_tree_list_vec (vec
);
22660 release_tree_vector (vec
);
22662 else if (token
->type
== CPP_OPEN_BRACE
)
22664 cp_lexer_set_source_position (parser
->lexer
);
22665 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22666 init
= cp_parser_braced_list (parser
, non_constant_p
);
22667 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22671 /* Anything else is an error. */
22672 cp_parser_error (parser
, "expected initializer");
22673 init
= error_mark_node
;
22676 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22677 init
= error_mark_node
;
22682 /* Parse an initializer-clause.
22684 initializer-clause:
22685 assignment-expression
22688 Returns an expression representing the initializer.
22690 If the `assignment-expression' production is used the value
22691 returned is simply a representation for the expression.
22693 Otherwise, calls cp_parser_braced_list. */
22696 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22698 cp_expr initializer
;
22700 /* Assume the expression is constant. */
22701 *non_constant_p
= false;
22703 /* If it is not a `{', then we are looking at an
22704 assignment-expression. */
22705 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22708 = cp_parser_constant_expression (parser
,
22709 /*allow_non_constant_p=*/true,
22713 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22715 return initializer
;
22718 /* Parse a brace-enclosed initializer list.
22721 { initializer-list , [opt] }
22722 { designated-initializer-list , [opt] }
22725 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22726 the elements of the initializer-list (or NULL, if the last
22727 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22728 NULL_TREE. There is no way to detect whether or not the optional
22729 trailing `,' was provided. NON_CONSTANT_P is as for
22730 cp_parser_initializer. */
22733 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22736 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22738 /* Consume the `{' token. */
22739 matching_braces braces
;
22740 braces
.require_open (parser
);
22741 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22742 initializer
= make_node (CONSTRUCTOR
);
22743 /* If it's not a `}', then there is a non-trivial initializer. */
22744 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22746 /* Parse the initializer list. */
22747 CONSTRUCTOR_ELTS (initializer
)
22748 = cp_parser_initializer_list (parser
, non_constant_p
);
22749 /* A trailing `,' token is allowed. */
22750 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22751 cp_lexer_consume_token (parser
->lexer
);
22754 *non_constant_p
= false;
22755 /* Now, there should be a trailing `}'. */
22756 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22757 braces
.require_close (parser
);
22758 TREE_TYPE (initializer
) = init_list_type_node
;
22760 cp_expr
result (initializer
);
22761 /* Build a location of the form:
22764 with caret==start at the open brace, finish at the close brace. */
22765 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22766 result
.set_location (combined_loc
);
22770 /* Consume tokens up to, and including, the next non-nested closing `]'.
22771 Returns true iff we found a closing `]'. */
22774 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22776 unsigned square_depth
= 0;
22780 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22782 switch (token
->type
)
22784 case CPP_PRAGMA_EOL
:
22785 if (!parser
->lexer
->in_pragma
)
22789 /* If we've run out of tokens, then there is no closing `]'. */
22792 case CPP_OPEN_SQUARE
:
22796 case CPP_CLOSE_SQUARE
:
22797 if (!square_depth
--)
22799 cp_lexer_consume_token (parser
->lexer
);
22808 /* Consume the token. */
22809 cp_lexer_consume_token (parser
->lexer
);
22813 /* Return true if we are looking at an array-designator, false otherwise. */
22816 cp_parser_array_designator_p (cp_parser
*parser
)
22818 /* Consume the `['. */
22819 cp_lexer_consume_token (parser
->lexer
);
22821 cp_lexer_save_tokens (parser
->lexer
);
22823 /* Skip tokens until the next token is a closing square bracket.
22824 If we find the closing `]', and the next token is a `=', then
22825 we are looking at an array designator. */
22826 bool array_designator_p
22827 = (cp_parser_skip_to_closing_square_bracket (parser
)
22828 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22830 /* Roll back the tokens we skipped. */
22831 cp_lexer_rollback_tokens (parser
->lexer
);
22833 return array_designator_p
;
22836 /* Parse an initializer-list.
22839 initializer-clause ... [opt]
22840 initializer-list , initializer-clause ... [opt]
22844 designated-initializer-list:
22845 designated-initializer-clause
22846 designated-initializer-list , designated-initializer-clause
22848 designated-initializer-clause:
22849 designator brace-or-equal-initializer
22857 designation initializer-clause ...[opt]
22858 initializer-list , designation initializer-clause ...[opt]
22863 [ constant-expression ] =
22865 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22866 for the initializer. If the INDEX of the elt is non-NULL, it is the
22867 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22868 as for cp_parser_initializer. */
22870 static vec
<constructor_elt
, va_gc
> *
22871 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22873 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22874 bool first_p
= true;
22875 tree first_designator
= NULL_TREE
;
22877 /* Assume all of the expressions are constant. */
22878 *non_constant_p
= false;
22880 /* Parse the rest of the list. */
22886 bool clause_non_constant_p
;
22887 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22889 /* Handle the C++2A syntax, '. id ='. */
22890 if ((cxx_dialect
>= cxx2a
22891 || cp_parser_allow_gnu_extensions_p (parser
))
22892 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22893 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22894 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22895 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22896 == CPP_OPEN_BRACE
)))
22898 if (cxx_dialect
< cxx2a
)
22899 pedwarn (loc
, OPT_Wpedantic
,
22900 "C++ designated initializers only available with "
22901 "-std=c++2a or -std=gnu++2a");
22902 /* Consume the `.'. */
22903 cp_lexer_consume_token (parser
->lexer
);
22904 /* Consume the identifier. */
22905 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22906 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22907 /* Consume the `='. */
22908 cp_lexer_consume_token (parser
->lexer
);
22910 /* Also, if the next token is an identifier and the following one is a
22911 colon, we are looking at the GNU designated-initializer
22913 else if (cp_parser_allow_gnu_extensions_p (parser
)
22914 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22915 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22918 /* Warn the user that they are using an extension. */
22919 pedwarn (loc
, OPT_Wpedantic
,
22920 "ISO C++ does not allow GNU designated initializers");
22921 /* Consume the identifier. */
22922 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22923 /* Consume the `:'. */
22924 cp_lexer_consume_token (parser
->lexer
);
22926 /* Also handle C99 array designators, '[ const ] ='. */
22927 else if (cp_parser_allow_gnu_extensions_p (parser
)
22928 && !c_dialect_objc ()
22929 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22931 /* In C++11, [ could start a lambda-introducer. */
22932 bool non_const
= false;
22934 cp_parser_parse_tentatively (parser
);
22936 if (!cp_parser_array_designator_p (parser
))
22938 cp_parser_simulate_error (parser
);
22939 designator
= NULL_TREE
;
22943 designator
= cp_parser_constant_expression (parser
, true,
22945 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22946 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22949 if (!cp_parser_parse_definitely (parser
))
22950 designator
= NULL_TREE
;
22952 && (!require_potential_rvalue_constant_expression
22954 designator
= NULL_TREE
;
22956 /* Warn the user that they are using an extension. */
22957 pedwarn (loc
, OPT_Wpedantic
,
22958 "ISO C++ does not allow C99 designated initializers");
22961 designator
= NULL_TREE
;
22965 first_designator
= designator
;
22968 else if (cxx_dialect
>= cxx2a
22969 && first_designator
!= error_mark_node
22970 && (!first_designator
!= !designator
))
22972 error_at (loc
, "either all initializer clauses should be designated "
22973 "or none of them should be");
22974 first_designator
= error_mark_node
;
22976 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22977 first_designator
= designator
;
22979 /* Parse the initializer. */
22980 initializer
= cp_parser_initializer_clause (parser
,
22981 &clause_non_constant_p
);
22982 /* If any clause is non-constant, so is the entire initializer. */
22983 if (clause_non_constant_p
)
22984 *non_constant_p
= true;
22986 /* If we have an ellipsis, this is an initializer pack
22988 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22990 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22992 /* Consume the `...'. */
22993 cp_lexer_consume_token (parser
->lexer
);
22995 if (designator
&& cxx_dialect
>= cxx2a
)
22997 "%<...%> not allowed in designated initializer list");
22999 /* Turn the initializer into an initializer expansion. */
23000 initializer
= make_pack_expansion (initializer
);
23003 /* Add it to the vector. */
23004 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
23006 /* If the next token is not a comma, we have reached the end of
23008 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23011 /* Peek at the next token. */
23012 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23013 /* If the next token is a `}', then we're still done. An
23014 initializer-clause can have a trailing `,' after the
23015 initializer-list and before the closing `}'. */
23016 if (token
->type
== CPP_CLOSE_BRACE
)
23019 /* Consume the `,' token. */
23020 cp_lexer_consume_token (parser
->lexer
);
23023 /* The same identifier shall not appear in multiple designators
23024 of a designated-initializer-list. */
23025 if (first_designator
)
23028 tree designator
, val
;
23029 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23030 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23032 if (IDENTIFIER_MARKED (designator
))
23034 error_at (cp_expr_loc_or_loc (val
, input_location
),
23035 "%<.%s%> designator used multiple times in "
23036 "the same initializer list",
23037 IDENTIFIER_POINTER (designator
));
23038 (*v
)[i
].index
= error_mark_node
;
23041 IDENTIFIER_MARKED (designator
) = 1;
23043 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23044 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23045 IDENTIFIER_MARKED (designator
) = 0;
23051 /* Classes [gram.class] */
23053 /* Parse a class-name.
23059 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23060 to indicate that names looked up in dependent types should be
23061 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23062 keyword has been used to indicate that the name that appears next
23063 is a template. TAG_TYPE indicates the explicit tag given before
23064 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23065 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23066 is the class being defined in a class-head. If ENUM_OK is TRUE,
23067 enum-names are also accepted.
23069 Returns the TYPE_DECL representing the class. */
23072 cp_parser_class_name (cp_parser
*parser
,
23073 bool typename_keyword_p
,
23074 bool template_keyword_p
,
23075 enum tag_types tag_type
,
23076 bool check_dependency_p
,
23078 bool is_declaration
,
23085 tree identifier
= NULL_TREE
;
23087 /* All class-names start with an identifier. */
23088 token
= cp_lexer_peek_token (parser
->lexer
);
23089 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
23091 cp_parser_error (parser
, "expected class-name");
23092 return error_mark_node
;
23095 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23096 to a template-id, so we save it here. */
23097 scope
= parser
->scope
;
23098 if (scope
== error_mark_node
)
23099 return error_mark_node
;
23101 /* Any name names a type if we're following the `typename' keyword
23102 in a qualified name where the enclosing scope is type-dependent. */
23103 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
23104 && dependent_type_p (scope
));
23105 /* Handle the common case (an identifier, but not a template-id)
23107 if (token
->type
== CPP_NAME
23108 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
23110 cp_token
*identifier_token
;
23113 /* Look for the identifier. */
23114 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
23115 ambiguous_p
= identifier_token
->error_reported
;
23116 identifier
= cp_parser_identifier (parser
);
23117 /* If the next token isn't an identifier, we are certainly not
23118 looking at a class-name. */
23119 if (identifier
== error_mark_node
)
23120 decl
= error_mark_node
;
23121 /* If we know this is a type-name, there's no need to look it
23123 else if (typename_p
)
23127 tree ambiguous_decls
;
23128 /* If we already know that this lookup is ambiguous, then
23129 we've already issued an error message; there's no reason
23133 cp_parser_simulate_error (parser
);
23134 return error_mark_node
;
23136 /* If the next token is a `::', then the name must be a type
23139 [basic.lookup.qual]
23141 During the lookup for a name preceding the :: scope
23142 resolution operator, object, function, and enumerator
23143 names are ignored. */
23144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23145 tag_type
= scope_type
;
23146 /* Look up the name. */
23147 decl
= cp_parser_lookup_name (parser
, identifier
,
23149 /*is_template=*/false,
23150 /*is_namespace=*/false,
23151 check_dependency_p
,
23153 identifier_token
->location
);
23154 if (ambiguous_decls
)
23156 if (cp_parser_parsing_tentatively (parser
))
23157 cp_parser_simulate_error (parser
);
23158 return error_mark_node
;
23164 /* Try a template-id. */
23165 decl
= cp_parser_template_id (parser
, template_keyword_p
,
23166 check_dependency_p
,
23169 if (decl
== error_mark_node
)
23170 return error_mark_node
;
23173 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
23175 /* If this is a typename, create a TYPENAME_TYPE. */
23177 && decl
!= error_mark_node
23178 && !is_overloaded_fn (decl
))
23180 decl
= make_typename_type (scope
, decl
, typename_type
,
23181 /*complain=*/tf_error
);
23182 if (decl
!= error_mark_node
)
23183 decl
= TYPE_NAME (decl
);
23186 decl
= strip_using_decl (decl
);
23188 /* Check to see that it is really the name of a class. */
23189 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
23190 && identifier_p (TREE_OPERAND (decl
, 0))
23191 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23192 /* Situations like this:
23194 template <typename T> struct A {
23195 typename T::template X<int>::I i;
23198 are problematic. Is `T::template X<int>' a class-name? The
23199 standard does not seem to be definitive, but there is no other
23200 valid interpretation of the following `::'. Therefore, those
23201 names are considered class-names. */
23203 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
23204 if (decl
!= error_mark_node
)
23205 decl
= TYPE_NAME (decl
);
23207 else if (TREE_CODE (decl
) != TYPE_DECL
23208 || TREE_TYPE (decl
) == error_mark_node
23209 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
23210 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
23211 /* In Objective-C 2.0, a classname followed by '.' starts a
23212 dot-syntax expression, and it's not a type-name. */
23213 || (c_dialect_objc ()
23214 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
23215 && objc_is_class_name (decl
)))
23216 decl
= error_mark_node
;
23218 if (decl
== error_mark_node
)
23219 cp_parser_error (parser
, "expected class-name");
23220 else if (identifier
&& !parser
->scope
)
23221 maybe_note_name_used_in_class (identifier
, decl
);
23226 /* Parse a class-specifier.
23229 class-head { member-specification [opt] }
23231 Returns the TREE_TYPE representing the class. */
23234 cp_parser_class_specifier_1 (cp_parser
* parser
)
23237 tree attributes
= NULL_TREE
;
23238 bool nested_name_specifier_p
;
23239 unsigned saved_num_template_parameter_lists
;
23240 bool saved_in_function_body
;
23241 unsigned char in_statement
;
23242 bool in_switch_statement_p
;
23243 bool saved_in_unbraced_linkage_specification_p
;
23244 tree old_scope
= NULL_TREE
;
23245 tree scope
= NULL_TREE
;
23246 cp_token
*closing_brace
;
23248 push_deferring_access_checks (dk_no_deferred
);
23250 /* Parse the class-head. */
23251 type
= cp_parser_class_head (parser
,
23252 &nested_name_specifier_p
);
23253 /* If the class-head was a semantic disaster, skip the entire body
23257 cp_parser_skip_to_end_of_block_or_statement (parser
);
23258 pop_deferring_access_checks ();
23259 return error_mark_node
;
23262 /* Look for the `{'. */
23263 matching_braces braces
;
23264 if (!braces
.require_open (parser
))
23266 pop_deferring_access_checks ();
23267 return error_mark_node
;
23270 cp_ensure_no_omp_declare_simd (parser
);
23271 cp_ensure_no_oacc_routine (parser
);
23273 /* Issue an error message if type-definitions are forbidden here. */
23274 bool type_definition_ok_p
= cp_parser_check_type_definition (parser
);
23275 /* Remember that we are defining one more class. */
23276 ++parser
->num_classes_being_defined
;
23277 /* Inside the class, surrounding template-parameter-lists do not
23279 saved_num_template_parameter_lists
23280 = parser
->num_template_parameter_lists
;
23281 parser
->num_template_parameter_lists
= 0;
23282 /* We are not in a function body. */
23283 saved_in_function_body
= parser
->in_function_body
;
23284 parser
->in_function_body
= false;
23285 /* Or in a loop. */
23286 in_statement
= parser
->in_statement
;
23287 parser
->in_statement
= 0;
23288 /* Or in a switch. */
23289 in_switch_statement_p
= parser
->in_switch_statement_p
;
23290 parser
->in_switch_statement_p
= false;
23291 /* We are not immediately inside an extern "lang" block. */
23292 saved_in_unbraced_linkage_specification_p
23293 = parser
->in_unbraced_linkage_specification_p
;
23294 parser
->in_unbraced_linkage_specification_p
= false;
23296 // Associate constraints with the type.
23298 type
= associate_classtype_constraints (type
);
23300 /* Start the class. */
23301 if (nested_name_specifier_p
)
23303 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
23304 old_scope
= push_inner_scope (scope
);
23306 type
= begin_class_definition (type
);
23308 if (type
== error_mark_node
)
23309 /* If the type is erroneous, skip the entire body of the class. */
23310 cp_parser_skip_to_closing_brace (parser
);
23312 /* Parse the member-specification. */
23313 cp_parser_member_specification_opt (parser
);
23315 /* Look for the trailing `}'. */
23316 closing_brace
= braces
.require_close (parser
);
23317 /* Look for trailing attributes to apply to this class. */
23318 if (cp_parser_allow_gnu_extensions_p (parser
))
23319 attributes
= cp_parser_gnu_attributes_opt (parser
);
23320 if (type
!= error_mark_node
)
23321 type
= finish_struct (type
, attributes
);
23322 if (nested_name_specifier_p
)
23323 pop_inner_scope (old_scope
, scope
);
23325 /* We've finished a type definition. Check for the common syntax
23326 error of forgetting a semicolon after the definition. We need to
23327 be careful, as we can't just check for not-a-semicolon and be done
23328 with it; the user might have typed:
23330 class X { } c = ...;
23331 class X { } *p = ...;
23333 and so forth. Instead, enumerate all the possible tokens that
23334 might follow this production; if we don't see one of them, then
23335 complain and silently insert the semicolon. */
23337 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23338 bool want_semicolon
= true;
23340 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23341 /* Don't try to parse c++11 attributes here. As per the
23342 grammar, that should be a task for
23343 cp_parser_decl_specifier_seq. */
23344 want_semicolon
= false;
23346 switch (token
->type
)
23349 case CPP_SEMICOLON
:
23352 case CPP_OPEN_PAREN
:
23353 case CPP_CLOSE_PAREN
:
23355 want_semicolon
= false;
23358 /* While it's legal for type qualifiers and storage class
23359 specifiers to follow type definitions in the grammar, only
23360 compiler testsuites contain code like that. Assume that if
23361 we see such code, then what we're really seeing is a case
23365 const <type> var = ...;
23370 static <type> func (...) ...
23372 i.e. the qualifier or specifier applies to the next
23373 declaration. To do so, however, we need to look ahead one
23374 more token to see if *that* token is a type specifier.
23376 This code could be improved to handle:
23379 static const <type> var = ...; */
23381 if (keyword_is_decl_specifier (token
->keyword
))
23383 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23385 /* Handling user-defined types here would be nice, but very
23388 = (lookahead
->type
== CPP_KEYWORD
23389 && keyword_begins_type_specifier (lookahead
->keyword
));
23396 /* If we don't have a type, then something is very wrong and we
23397 shouldn't try to do anything clever. Likewise for not seeing the
23399 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
23401 /* Locate the closing brace. */
23402 cp_token_position prev
23403 = cp_lexer_previous_token_position (parser
->lexer
);
23404 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
23405 location_t loc
= prev_token
->location
;
23407 /* We want to suggest insertion of a ';' immediately *after* the
23408 closing brace, so, if we can, offset the location by 1 column. */
23409 location_t next_loc
= loc
;
23410 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
23411 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
23413 rich_location
richloc (line_table
, next_loc
);
23415 /* If we successfully offset the location, suggest the fix-it. */
23416 if (next_loc
!= loc
)
23417 richloc
.add_fixit_insert_before (next_loc
, ";");
23419 if (CLASSTYPE_DECLARED_CLASS (type
))
23420 error_at (&richloc
,
23421 "expected %<;%> after class definition");
23422 else if (TREE_CODE (type
) == RECORD_TYPE
)
23423 error_at (&richloc
,
23424 "expected %<;%> after struct definition");
23425 else if (TREE_CODE (type
) == UNION_TYPE
)
23426 error_at (&richloc
,
23427 "expected %<;%> after union definition");
23429 gcc_unreachable ();
23431 /* Unget one token and smash it to look as though we encountered
23432 a semicolon in the input stream. */
23433 cp_lexer_set_token_position (parser
->lexer
, prev
);
23434 token
= cp_lexer_peek_token (parser
->lexer
);
23435 token
->type
= CPP_SEMICOLON
;
23436 token
->keyword
= RID_MAX
;
23440 /* If this class is not itself within the scope of another class,
23441 then we need to parse the bodies of all of the queued function
23442 definitions. Note that the queued functions defined in a class
23443 are not always processed immediately following the
23444 class-specifier for that class. Consider:
23447 struct B { void f() { sizeof (A); } };
23450 If `f' were processed before the processing of `A' were
23451 completed, there would be no way to compute the size of `A'.
23452 Note that the nesting we are interested in here is lexical --
23453 not the semantic nesting given by TYPE_CONTEXT. In particular,
23456 struct A { struct B; };
23457 struct A::B { void f() { } };
23459 there is no need to delay the parsing of `A::B::f'. */
23460 if (--parser
->num_classes_being_defined
== 0)
23463 tree class_type
= NULL_TREE
;
23464 tree pushed_scope
= NULL_TREE
;
23466 cp_default_arg_entry
*e
;
23467 tree save_ccp
, save_ccr
;
23469 if (!type_definition_ok_p
|| any_erroneous_template_args_p (type
))
23471 /* Skip default arguments, NSDMIs, etc, in order to improve
23472 error recovery (c++/71169, c++/71832). */
23473 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23474 vec_safe_truncate (unparsed_nsdmis
, 0);
23475 vec_safe_truncate (unparsed_classes
, 0);
23476 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23479 /* In a first pass, parse default arguments to the functions.
23480 Then, in a second pass, parse the bodies of the functions.
23481 This two-phased approach handles cases like:
23489 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
23492 /* If there are default arguments that have not yet been processed,
23493 take care of them now. */
23494 if (class_type
!= e
->class_type
)
23497 pop_scope (pushed_scope
);
23498 class_type
= e
->class_type
;
23499 pushed_scope
= push_scope (class_type
);
23501 /* Make sure that any template parameters are in scope. */
23502 maybe_begin_member_template_processing (decl
);
23503 /* Parse the default argument expressions. */
23504 cp_parser_late_parsing_default_args (parser
, decl
);
23505 /* Remove any template parameters from the symbol table. */
23506 maybe_end_member_template_processing ();
23508 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23509 /* Now parse any NSDMIs. */
23510 save_ccp
= current_class_ptr
;
23511 save_ccr
= current_class_ref
;
23512 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
23514 if (class_type
!= DECL_CONTEXT (decl
))
23517 pop_scope (pushed_scope
);
23518 class_type
= DECL_CONTEXT (decl
);
23519 pushed_scope
= push_scope (class_type
);
23521 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
23522 cp_parser_late_parsing_nsdmi (parser
, decl
);
23524 vec_safe_truncate (unparsed_nsdmis
, 0);
23525 current_class_ptr
= save_ccp
;
23526 current_class_ref
= save_ccr
;
23528 pop_scope (pushed_scope
);
23530 /* Now do some post-NSDMI bookkeeping. */
23531 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
23532 after_nsdmi_defaulted_late_checks (class_type
);
23533 vec_safe_truncate (unparsed_classes
, 0);
23534 after_nsdmi_defaulted_late_checks (type
);
23536 /* Now parse the body of the functions. */
23539 /* OpenMP UDRs need to be parsed before all other functions. */
23540 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23541 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
23542 cp_parser_late_parsing_for_member (parser
, decl
);
23543 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23544 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
23545 cp_parser_late_parsing_for_member (parser
, decl
);
23548 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23549 cp_parser_late_parsing_for_member (parser
, decl
);
23550 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23553 vec_safe_push (unparsed_classes
, type
);
23555 /* Put back any saved access checks. */
23556 pop_deferring_access_checks ();
23558 /* Restore saved state. */
23559 parser
->in_switch_statement_p
= in_switch_statement_p
;
23560 parser
->in_statement
= in_statement
;
23561 parser
->in_function_body
= saved_in_function_body
;
23562 parser
->num_template_parameter_lists
23563 = saved_num_template_parameter_lists
;
23564 parser
->in_unbraced_linkage_specification_p
23565 = saved_in_unbraced_linkage_specification_p
;
23571 cp_parser_class_specifier (cp_parser
* parser
)
23574 timevar_push (TV_PARSE_STRUCT
);
23575 ret
= cp_parser_class_specifier_1 (parser
);
23576 timevar_pop (TV_PARSE_STRUCT
);
23580 /* Parse a class-head.
23583 class-key identifier [opt] base-clause [opt]
23584 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23585 class-key nested-name-specifier [opt] template-id
23588 class-virt-specifier:
23592 class-key attributes identifier [opt] base-clause [opt]
23593 class-key attributes nested-name-specifier identifier base-clause [opt]
23594 class-key attributes nested-name-specifier [opt] template-id
23597 Upon return BASES is initialized to the list of base classes (or
23598 NULL, if there are none) in the same form returned by
23599 cp_parser_base_clause.
23601 Returns the TYPE of the indicated class. Sets
23602 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23603 involving a nested-name-specifier was used, and FALSE otherwise.
23605 Returns error_mark_node if this is not a class-head.
23607 Returns NULL_TREE if the class-head is syntactically valid, but
23608 semantically invalid in a way that means we should skip the entire
23609 body of the class. */
23612 cp_parser_class_head (cp_parser
* parser
,
23613 bool* nested_name_specifier_p
)
23615 tree nested_name_specifier
;
23616 enum tag_types class_key
;
23617 tree id
= NULL_TREE
;
23618 tree type
= NULL_TREE
;
23621 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
23622 bool template_id_p
= false;
23623 bool qualified_p
= false;
23624 bool invalid_nested_name_p
= false;
23625 bool invalid_explicit_specialization_p
= false;
23626 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23627 tree pushed_scope
= NULL_TREE
;
23628 unsigned num_templates
;
23629 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
23630 /* Assume no nested-name-specifier will be present. */
23631 *nested_name_specifier_p
= false;
23632 /* Assume no template parameter lists will be used in defining the
23635 parser
->colon_corrects_to_scope_p
= false;
23637 /* Look for the class-key. */
23638 class_key
= cp_parser_class_key (parser
);
23639 if (class_key
== none_type
)
23640 return error_mark_node
;
23642 location_t class_head_start_location
= input_location
;
23644 /* Parse the attributes. */
23645 attributes
= cp_parser_attributes_opt (parser
);
23647 /* If the next token is `::', that is invalid -- but sometimes
23648 people do try to write:
23652 Handle this gracefully by accepting the extra qualifier, and then
23653 issuing an error about it later if this really is a
23654 class-head. If it turns out just to be an elaborated type
23655 specifier, remain silent. */
23656 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23657 qualified_p
= true;
23659 push_deferring_access_checks (dk_no_check
);
23661 /* Determine the name of the class. Begin by looking for an
23662 optional nested-name-specifier. */
23663 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23664 nested_name_specifier
23665 = cp_parser_nested_name_specifier_opt (parser
,
23666 /*typename_keyword_p=*/false,
23667 /*check_dependency_p=*/false,
23669 /*is_declaration=*/false);
23670 /* If there was a nested-name-specifier, then there *must* be an
23673 cp_token
*bad_template_keyword
= NULL
;
23675 if (nested_name_specifier
)
23677 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23678 /* Although the grammar says `identifier', it really means
23679 `class-name' or `template-name'. You are only allowed to
23680 define a class that has already been declared with this
23683 The proposed resolution for Core Issue 180 says that wherever
23684 you see `class T::X' you should treat `X' as a type-name.
23686 It is OK to define an inaccessible class; for example:
23688 class A { class B; };
23691 We do not know if we will see a class-name, or a
23692 template-name. We look for a class-name first, in case the
23693 class-name is a template-id; if we looked for the
23694 template-name first we would stop after the template-name. */
23695 cp_parser_parse_tentatively (parser
);
23696 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23697 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23698 type
= cp_parser_class_name (parser
,
23699 /*typename_keyword_p=*/false,
23700 /*template_keyword_p=*/false,
23702 /*check_dependency_p=*/false,
23703 /*class_head_p=*/true,
23704 /*is_declaration=*/false);
23705 /* If that didn't work, ignore the nested-name-specifier. */
23706 if (!cp_parser_parse_definitely (parser
))
23708 invalid_nested_name_p
= true;
23709 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23710 id
= cp_parser_identifier (parser
);
23711 if (id
== error_mark_node
)
23714 /* If we could not find a corresponding TYPE, treat this
23715 declaration like an unqualified declaration. */
23716 if (type
== error_mark_node
)
23717 nested_name_specifier
= NULL_TREE
;
23718 /* Otherwise, count the number of templates used in TYPE and its
23719 containing scopes. */
23721 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23723 /* Otherwise, the identifier is optional. */
23726 /* We don't know whether what comes next is a template-id,
23727 an identifier, or nothing at all. */
23728 cp_parser_parse_tentatively (parser
);
23729 /* Check for a template-id. */
23730 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23731 id
= cp_parser_template_id (parser
,
23732 /*template_keyword_p=*/false,
23733 /*check_dependency_p=*/true,
23735 /*is_declaration=*/true);
23736 /* If that didn't work, it could still be an identifier. */
23737 if (!cp_parser_parse_definitely (parser
))
23739 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23741 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23742 id
= cp_parser_identifier (parser
);
23749 template_id_p
= true;
23754 pop_deferring_access_checks ();
23758 cp_parser_check_for_invalid_template_id (parser
, id
,
23760 type_start_token
->location
);
23762 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23764 /* If it's not a `:' or a `{' then we can't really be looking at a
23765 class-head, since a class-head only appears as part of a
23766 class-specifier. We have to detect this situation before calling
23767 xref_tag, since that has irreversible side-effects. */
23768 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23770 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23771 type
= error_mark_node
;
23775 /* At this point, we're going ahead with the class-specifier, even
23776 if some other problem occurs. */
23777 cp_parser_commit_to_tentative_parse (parser
);
23778 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23780 cp_parser_error (parser
,
23781 "cannot specify %<override%> for a class");
23782 type
= error_mark_node
;
23785 /* Issue the error about the overly-qualified name now. */
23788 cp_parser_error (parser
,
23789 "global qualification of class name is invalid");
23790 type
= error_mark_node
;
23793 else if (invalid_nested_name_p
)
23795 cp_parser_error (parser
,
23796 "qualified name does not name a class");
23797 type
= error_mark_node
;
23800 else if (nested_name_specifier
)
23804 if (bad_template_keyword
)
23805 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23806 keyword template shall not appear at the top level. */
23807 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23808 "keyword %<template%> not allowed in class-head-name");
23810 /* Reject typedef-names in class heads. */
23811 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23813 error_at (type_start_token
->location
,
23814 "invalid class name in declaration of %qD",
23820 /* Figure out in what scope the declaration is being placed. */
23821 scope
= current_scope ();
23822 /* If that scope does not contain the scope in which the
23823 class was originally declared, the program is invalid. */
23824 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23826 if (at_namespace_scope_p ())
23827 error_at (type_start_token
->location
,
23828 "declaration of %qD in namespace %qD which does not "
23830 type
, scope
, nested_name_specifier
);
23832 error_at (type_start_token
->location
,
23833 "declaration of %qD in %qD which does not enclose %qD",
23834 type
, scope
, nested_name_specifier
);
23840 A declarator-id shall not be qualified except for the
23841 definition of a ... nested class outside of its class
23842 ... [or] the definition or explicit instantiation of a
23843 class member of a namespace outside of its namespace. */
23844 if (scope
== nested_name_specifier
)
23846 permerror (nested_name_specifier_token_start
->location
,
23847 "extra qualification not allowed");
23848 nested_name_specifier
= NULL_TREE
;
23852 /* An explicit-specialization must be preceded by "template <>". If
23853 it is not, try to recover gracefully. */
23854 if (at_namespace_scope_p ()
23855 && parser
->num_template_parameter_lists
== 0
23856 && !processing_template_parmlist
23859 /* Build a location of this form:
23860 struct typename <ARGS>
23861 ^~~~~~~~~~~~~~~~~~~~~~
23862 with caret==start at the start token, and
23863 finishing at the end of the type. */
23864 location_t reported_loc
23865 = make_location (class_head_start_location
,
23866 class_head_start_location
,
23867 get_finish (type_start_token
->location
));
23868 rich_location
richloc (line_table
, reported_loc
);
23869 richloc
.add_fixit_insert_before (class_head_start_location
,
23871 error_at (&richloc
,
23872 "an explicit specialization must be preceded by"
23873 " %<template <>%>");
23874 invalid_explicit_specialization_p
= true;
23875 /* Take the same action that would have been taken by
23876 cp_parser_explicit_specialization. */
23877 ++parser
->num_template_parameter_lists
;
23878 begin_specialization ();
23880 /* There must be no "return" statements between this point and the
23881 end of this function; set "type "to the correct return value and
23882 use "goto done;" to return. */
23883 /* Make sure that the right number of template parameters were
23885 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23887 type_start_token
->location
,
23888 /*declarator=*/NULL
))
23890 /* If something went wrong, there is no point in even trying to
23891 process the class-definition. */
23896 /* Look up the type. */
23899 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23900 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23901 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23903 error_at (type_start_token
->location
,
23904 "function template %qD redeclared as a class template", id
);
23905 type
= error_mark_node
;
23909 type
= TREE_TYPE (id
);
23910 type
= maybe_process_partial_specialization (type
);
23912 /* Check the scope while we still know whether or not we had a
23913 nested-name-specifier. */
23914 if (type
!= error_mark_node
)
23915 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23917 if (nested_name_specifier
)
23918 pushed_scope
= push_scope (nested_name_specifier
);
23920 else if (nested_name_specifier
)
23926 template <typename T> struct S { struct T };
23927 template <typename T> struct S<T>::T { };
23929 we will get a TYPENAME_TYPE when processing the definition of
23930 `S::T'. We need to resolve it to the actual type before we
23931 try to define it. */
23932 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23934 class_type
= resolve_typename_type (TREE_TYPE (type
),
23935 /*only_current_p=*/false);
23936 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23937 type
= TYPE_NAME (class_type
);
23940 cp_parser_error (parser
, "could not resolve typename type");
23941 type
= error_mark_node
;
23945 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23946 == error_mark_node
)
23952 class_type
= current_class_type
;
23953 /* Enter the scope indicated by the nested-name-specifier. */
23954 pushed_scope
= push_scope (nested_name_specifier
);
23955 /* Get the canonical version of this type. */
23956 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23957 /* Call push_template_decl if it seems like we should be defining a
23958 template either from the template headers or the type we're
23959 defining, so that we diagnose both extra and missing headers. */
23960 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23961 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23962 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23964 type
= push_template_decl (type
);
23965 if (type
== error_mark_node
)
23972 type
= TREE_TYPE (type
);
23973 *nested_name_specifier_p
= true;
23975 else /* The name is not a nested name. */
23977 /* If the class was unnamed, create a dummy name. */
23979 id
= make_anon_name ();
23980 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23981 ? ts_within_enclosing_non_class
23983 type
= xref_tag (class_key
, id
, tag_scope
,
23984 parser
->num_template_parameter_lists
);
23987 /* Indicate whether this class was declared as a `class' or as a
23989 if (TREE_CODE (type
) == RECORD_TYPE
)
23990 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23991 cp_parser_check_class_key (class_key
, type
);
23993 /* If this type was already complete, and we see another definition,
23994 that's an error. */
23995 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23997 error_at (type_start_token
->location
, "redefinition of %q#T",
23999 inform (location_of (type
), "previous definition of %q#T",
24004 else if (type
== error_mark_node
)
24009 /* Apply attributes now, before any use of the class as a template
24010 argument in its base list. */
24011 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
24012 fixup_attribute_variants (type
);
24015 /* We will have entered the scope containing the class; the names of
24016 base classes should be looked up in that context. For example:
24018 struct A { struct B {}; struct C; };
24019 struct A::C : B {};
24023 /* Get the list of base-classes, if there is one. */
24024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
24026 /* PR59482: enter the class scope so that base-specifiers are looked
24030 bases
= cp_parser_base_clause (parser
);
24031 /* PR59482: get out of the previously pushed class scope so that the
24032 subsequent pops pop the right thing. */
24039 /* If we're really defining a class, process the base classes.
24040 If they're invalid, fail. */
24041 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24042 xref_basetypes (type
, bases
);
24045 /* Leave the scope given by the nested-name-specifier. We will
24046 enter the class scope itself while processing the members. */
24048 pop_scope (pushed_scope
);
24050 if (invalid_explicit_specialization_p
)
24052 end_specialization ();
24053 --parser
->num_template_parameter_lists
;
24057 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
24058 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
24059 CLASSTYPE_FINAL (type
) = 1;
24061 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24065 /* Parse a class-key.
24072 Returns the kind of class-key specified, or none_type to indicate
24075 static enum tag_types
24076 cp_parser_class_key (cp_parser
* parser
)
24079 enum tag_types tag_type
;
24081 /* Look for the class-key. */
24082 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
24086 /* Check to see if the TOKEN is a class-key. */
24087 tag_type
= cp_parser_token_is_class_key (token
);
24089 cp_parser_error (parser
, "expected class-key");
24093 /* Parse a type-parameter-key.
24095 type-parameter-key:
24101 cp_parser_type_parameter_key (cp_parser
* parser
)
24103 /* Look for the type-parameter-key. */
24104 enum tag_types tag_type
= none_type
;
24105 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24106 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
24108 cp_lexer_consume_token (parser
->lexer
);
24109 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
24110 /* typename is not allowed in a template template parameter
24111 by the standard until C++17. */
24112 pedwarn (token
->location
, OPT_Wpedantic
,
24113 "ISO C++ forbids typename key in template template parameter;"
24114 " use -std=c++17 or -std=gnu++17");
24117 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
24122 /* Parse an (optional) member-specification.
24124 member-specification:
24125 member-declaration member-specification [opt]
24126 access-specifier : member-specification [opt] */
24129 cp_parser_member_specification_opt (cp_parser
* parser
)
24136 /* Peek at the next token. */
24137 token
= cp_lexer_peek_token (parser
->lexer
);
24138 /* If it's a `}', or EOF then we've seen all the members. */
24139 if (token
->type
== CPP_CLOSE_BRACE
24140 || token
->type
== CPP_EOF
24141 || token
->type
== CPP_PRAGMA_EOL
)
24144 /* See if this token is a keyword. */
24145 keyword
= token
->keyword
;
24149 case RID_PROTECTED
:
24151 /* Consume the access-specifier. */
24152 cp_lexer_consume_token (parser
->lexer
);
24153 /* Remember which access-specifier is active. */
24154 current_access_specifier
= token
->u
.value
;
24155 /* Look for the `:'. */
24156 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24160 /* Accept #pragmas at class scope. */
24161 if (token
->type
== CPP_PRAGMA
)
24163 cp_parser_pragma (parser
, pragma_member
, NULL
);
24167 /* Otherwise, the next construction must be a
24168 member-declaration. */
24169 cp_parser_member_declaration (parser
);
24174 /* Parse a member-declaration.
24176 member-declaration:
24177 decl-specifier-seq [opt] member-declarator-list [opt] ;
24178 function-definition ; [opt]
24179 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24181 template-declaration
24184 member-declarator-list:
24186 member-declarator-list , member-declarator
24189 declarator pure-specifier [opt]
24190 declarator constant-initializer [opt]
24191 identifier [opt] : constant-expression
24195 member-declaration:
24196 __extension__ member-declaration
24199 declarator attributes [opt] pure-specifier [opt]
24200 declarator attributes [opt] constant-initializer [opt]
24201 identifier [opt] attributes [opt] : constant-expression
24205 member-declaration:
24206 static_assert-declaration */
24209 cp_parser_member_declaration (cp_parser
* parser
)
24211 cp_decl_specifier_seq decl_specifiers
;
24212 tree prefix_attributes
;
24214 int declares_class_or_enum
;
24216 cp_token
*token
= NULL
;
24217 cp_token
*decl_spec_token_start
= NULL
;
24218 cp_token
*initializer_token_start
= NULL
;
24219 int saved_pedantic
;
24220 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
24222 /* Check for the `__extension__' keyword. */
24223 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
24226 cp_parser_member_declaration (parser
);
24227 /* Restore the old value of the PEDANTIC flag. */
24228 pedantic
= saved_pedantic
;
24233 /* Check for a template-declaration. */
24234 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24236 /* An explicit specialization here is an error condition, and we
24237 expect the specialization handler to detect and report this. */
24238 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
24239 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
24240 cp_parser_explicit_specialization (parser
);
24242 cp_parser_template_declaration (parser
, /*member_p=*/true);
24246 /* Check for a template introduction. */
24247 else if (cp_parser_template_declaration_after_export (parser
, true))
24250 /* Check for a using-declaration. */
24251 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24253 if (cxx_dialect
< cxx11
)
24255 /* Parse the using-declaration. */
24256 cp_parser_using_declaration (parser
,
24257 /*access_declaration_p=*/false);
24263 bool alias_decl_expected
;
24264 cp_parser_parse_tentatively (parser
);
24265 decl
= cp_parser_alias_declaration (parser
);
24266 /* Note that if we actually see the '=' token after the
24267 identifier, cp_parser_alias_declaration commits the
24268 tentative parse. In that case, we really expect an
24269 alias-declaration. Otherwise, we expect a using
24271 alias_decl_expected
=
24272 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
24273 cp_parser_parse_definitely (parser
);
24275 if (alias_decl_expected
)
24276 finish_member_declaration (decl
);
24278 cp_parser_using_declaration (parser
,
24279 /*access_declaration_p=*/false);
24284 /* Check for @defs. */
24285 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
24288 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
24289 ivar
= ivar_chains
;
24293 ivar
= TREE_CHAIN (member
);
24294 TREE_CHAIN (member
) = NULL_TREE
;
24295 finish_member_declaration (member
);
24300 /* If the next token is `static_assert' we have a static assertion. */
24301 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
24303 cp_parser_static_assert (parser
, /*member_p=*/true);
24307 parser
->colon_corrects_to_scope_p
= false;
24309 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
24312 /* Parse the decl-specifier-seq. */
24313 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
24314 cp_parser_decl_specifier_seq (parser
,
24315 (CP_PARSER_FLAGS_OPTIONAL
24316 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
24318 &declares_class_or_enum
);
24319 /* Check for an invalid type-name. */
24320 if (!decl_specifiers
.any_type_specifiers_p
24321 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24323 /* If there is no declarator, then the decl-specifier-seq should
24325 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24327 /* If there was no decl-specifier-seq, and the next token is a
24328 `;', then we have something like:
24334 Each member-declaration shall declare at least one member
24335 name of the class. */
24336 if (!decl_specifiers
.any_specifiers_p
)
24338 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24339 if (!in_system_header_at (token
->location
))
24341 gcc_rich_location
richloc (token
->location
);
24342 richloc
.add_fixit_remove ();
24343 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
24350 /* See if this declaration is a friend. */
24351 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24352 /* If there were decl-specifiers, check to see if there was
24353 a class-declaration. */
24354 type
= check_tag_decl (&decl_specifiers
,
24355 /*explicit_type_instantiation_p=*/false);
24356 /* Nested classes have already been added to the class, but
24357 a `friend' needs to be explicitly registered. */
24360 /* If the `friend' keyword was present, the friend must
24361 be introduced with a class-key. */
24362 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
24363 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
24364 "in C++03 a class-key must be used "
24365 "when declaring a friend");
24368 template <typename T> struct A {
24369 friend struct A<T>::B;
24372 A<T>::B will be represented by a TYPENAME_TYPE, and
24373 therefore not recognized by check_tag_decl. */
24376 type
= decl_specifiers
.type
;
24377 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
24378 type
= TREE_TYPE (type
);
24380 if (!type
|| !TYPE_P (type
))
24381 error_at (decl_spec_token_start
->location
,
24382 "friend declaration does not name a class or "
24385 make_friend_class (current_class_type
, type
,
24386 /*complain=*/true);
24388 /* If there is no TYPE, an error message will already have
24390 else if (!type
|| type
== error_mark_node
)
24392 /* An anonymous aggregate has to be handled specially; such
24393 a declaration really declares a data member (with a
24394 particular type), as opposed to a nested class. */
24395 else if (ANON_AGGR_TYPE_P (type
))
24398 if (decl_specifiers
.storage_class
!= sc_none
)
24399 error_at (decl_spec_token_start
->location
,
24400 "a storage class on an anonymous aggregate "
24401 "in class scope is not allowed");
24403 /* Remove constructors and such from TYPE, now that we
24404 know it is an anonymous aggregate. */
24405 fixup_anonymous_aggr (type
);
24406 /* And make the corresponding data member. */
24407 decl
= build_decl (decl_spec_token_start
->location
,
24408 FIELD_DECL
, NULL_TREE
, type
);
24409 /* Add it to the class. */
24410 finish_member_declaration (decl
);
24413 cp_parser_check_access_in_redeclaration
24415 decl_spec_token_start
->location
);
24420 bool assume_semicolon
= false;
24422 /* Clear attributes from the decl_specifiers but keep them
24423 around as prefix attributes that apply them to the entity
24425 prefix_attributes
= decl_specifiers
.attributes
;
24426 decl_specifiers
.attributes
= NULL_TREE
;
24428 /* See if these declarations will be friends. */
24429 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24431 /* Keep going until we hit the `;' at the end of the
24433 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24435 tree attributes
= NULL_TREE
;
24436 tree first_attribute
;
24438 bool named_bitfld
= false;
24440 /* Peek at the next token. */
24441 token
= cp_lexer_peek_token (parser
->lexer
);
24443 /* The following code wants to know early if it is a bit-field
24444 or some other declaration. Attributes can appear before
24445 the `:' token. Skip over them without consuming any tokens
24446 to peek if they are followed by `:'. */
24447 if (cp_next_tokens_can_be_attribute_p (parser
)
24448 || (token
->type
== CPP_NAME
24449 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
24450 && (named_bitfld
= true)))
24453 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
24454 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24457 /* Check for a bitfield declaration. */
24458 if (token
->type
== CPP_COLON
24459 || (token
->type
== CPP_NAME
24460 && token
== cp_lexer_peek_token (parser
->lexer
)
24461 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
24462 && (named_bitfld
= true)))
24466 tree late_attributes
= NULL_TREE
;
24467 location_t id_location
24468 = cp_lexer_peek_token (parser
->lexer
)->location
;
24471 identifier
= cp_parser_identifier (parser
);
24473 identifier
= NULL_TREE
;
24475 /* Look for attributes that apply to the bitfield. */
24476 attributes
= cp_parser_attributes_opt (parser
);
24478 /* Consume the `:' token. */
24479 cp_lexer_consume_token (parser
->lexer
);
24481 /* Get the width of the bitfield. */
24482 width
= cp_parser_constant_expression (parser
, false, NULL
,
24483 cxx_dialect
>= cxx11
);
24485 /* In C++2A and as extension for C++11 and above we allow
24486 default member initializers for bit-fields. */
24487 initializer
= NULL_TREE
;
24488 if (cxx_dialect
>= cxx11
24489 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
24490 || cp_lexer_next_token_is (parser
->lexer
,
24494 = cp_lexer_peek_token (parser
->lexer
)->location
;
24495 if (cxx_dialect
< cxx2a
24496 && !in_system_header_at (loc
)
24497 && identifier
!= NULL_TREE
)
24499 "default member initializers for bit-fields "
24500 "only available with -std=c++2a or "
24503 initializer
= cp_parser_save_nsdmi (parser
);
24504 if (identifier
== NULL_TREE
)
24506 error_at (loc
, "default member initializer for "
24507 "unnamed bit-field");
24508 initializer
= NULL_TREE
;
24513 /* Look for attributes that apply to the bitfield after
24514 the `:' token and width. This is where GCC used to
24515 parse attributes in the past, pedwarn if there is
24516 a std attribute. */
24517 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24518 pedwarn (input_location
, OPT_Wpedantic
,
24519 "ISO C++ allows bit-field attributes only "
24520 "before the %<:%> token");
24522 late_attributes
= cp_parser_attributes_opt (parser
);
24525 attributes
= attr_chainon (attributes
, late_attributes
);
24527 /* Remember which attributes are prefix attributes and
24529 first_attribute
= attributes
;
24530 /* Combine the attributes. */
24531 attributes
= attr_chainon (prefix_attributes
, attributes
);
24533 /* Create the bitfield declaration. */
24534 decl
= grokbitfield (identifier
24535 ? make_id_declarator (NULL_TREE
,
24541 width
, initializer
,
24546 cp_declarator
*declarator
;
24547 tree asm_specification
;
24548 int ctor_dtor_or_conv_p
;
24549 bool static_p
= (decl_specifiers
.storage_class
== sc_static
);
24551 /* Parse the declarator. */
24553 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24554 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
24555 &ctor_dtor_or_conv_p
,
24556 /*parenthesized_p=*/NULL
,
24558 friend_p
, static_p
);
24560 /* If something went wrong parsing the declarator, make sure
24561 that we at least consume some tokens. */
24562 if (declarator
== cp_error_declarator
)
24564 /* Skip to the end of the statement. */
24565 cp_parser_skip_to_end_of_statement (parser
);
24566 /* If the next token is not a semicolon, that is
24567 probably because we just skipped over the body of
24568 a function. So, we consume a semicolon if
24569 present, but do not issue an error message if it
24571 if (cp_lexer_next_token_is (parser
->lexer
,
24573 cp_lexer_consume_token (parser
->lexer
);
24577 if (declares_class_or_enum
& 2)
24578 cp_parser_check_for_definition_in_return_type
24579 (declarator
, decl_specifiers
.type
,
24580 decl_specifiers
.locations
[ds_type_spec
]);
24582 /* Look for an asm-specification. */
24583 asm_specification
= cp_parser_asm_specification_opt (parser
);
24584 /* Look for attributes that apply to the declaration. */
24585 attributes
= cp_parser_attributes_opt (parser
);
24586 /* Remember which attributes are prefix attributes and
24588 first_attribute
= attributes
;
24589 /* Combine the attributes. */
24590 attributes
= attr_chainon (prefix_attributes
, attributes
);
24592 /* If it's an `=', then we have a constant-initializer or a
24593 pure-specifier. It is not correct to parse the
24594 initializer before registering the member declaration
24595 since the member declaration should be in scope while
24596 its initializer is processed. However, the rest of the
24597 front end does not yet provide an interface that allows
24598 us to handle this correctly. */
24599 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24603 A pure-specifier shall be used only in the declaration of
24604 a virtual function.
24606 A member-declarator can contain a constant-initializer
24607 only if it declares a static member of integral or
24610 Therefore, if the DECLARATOR is for a function, we look
24611 for a pure-specifier; otherwise, we look for a
24612 constant-initializer. When we call `grokfield', it will
24613 perform more stringent semantics checks. */
24614 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
24615 if (function_declarator_p (declarator
)
24616 || (decl_specifiers
.type
24617 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
24618 && declarator
->kind
== cdk_id
24619 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
24620 == FUNCTION_TYPE
)))
24621 initializer
= cp_parser_pure_specifier (parser
);
24622 else if (decl_specifiers
.storage_class
!= sc_static
)
24623 initializer
= cp_parser_save_nsdmi (parser
);
24624 else if (cxx_dialect
>= cxx11
)
24627 /* Don't require a constant rvalue in C++11, since we
24628 might want a reference constant. We'll enforce
24629 constancy later. */
24630 cp_lexer_consume_token (parser
->lexer
);
24631 /* Parse the initializer. */
24632 initializer
= cp_parser_initializer_clause (parser
,
24636 /* Parse the initializer. */
24637 initializer
= cp_parser_constant_initializer (parser
);
24639 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
24640 && !function_declarator_p (declarator
))
24643 if (decl_specifiers
.storage_class
!= sc_static
)
24644 initializer
= cp_parser_save_nsdmi (parser
);
24646 initializer
= cp_parser_initializer (parser
, &x
, &x
);
24648 /* Otherwise, there is no initializer. */
24650 initializer
= NULL_TREE
;
24652 /* See if we are probably looking at a function
24653 definition. We are certainly not looking at a
24654 member-declarator. Calling `grokfield' has
24655 side-effects, so we must not do it unless we are sure
24656 that we are looking at a member-declarator. */
24657 if (cp_parser_token_starts_function_definition_p
24658 (cp_lexer_peek_token (parser
->lexer
)))
24660 /* The grammar does not allow a pure-specifier to be
24661 used when a member function is defined. (It is
24662 possible that this fact is an oversight in the
24663 standard, since a pure function may be defined
24664 outside of the class-specifier. */
24665 if (initializer
&& initializer_token_start
)
24666 error_at (initializer_token_start
->location
,
24667 "pure-specifier on function-definition");
24668 decl
= cp_parser_save_member_function_body (parser
,
24672 if (parser
->fully_implicit_function_template_p
)
24673 decl
= finish_fully_implicit_template (parser
, decl
);
24674 /* If the member was not a friend, declare it here. */
24676 finish_member_declaration (decl
);
24677 /* Peek at the next token. */
24678 token
= cp_lexer_peek_token (parser
->lexer
);
24679 /* If the next token is a semicolon, consume it. */
24680 if (token
->type
== CPP_SEMICOLON
)
24682 location_t semicolon_loc
24683 = cp_lexer_consume_token (parser
->lexer
)->location
;
24684 gcc_rich_location
richloc (semicolon_loc
);
24685 richloc
.add_fixit_remove ();
24686 warning_at (&richloc
, OPT_Wextra_semi
,
24687 "extra %<;%> after in-class "
24688 "function definition");
24693 if (declarator
->kind
== cdk_function
)
24694 declarator
->id_loc
= token
->location
;
24695 /* Create the declaration. */
24696 decl
= grokfield (declarator
, &decl_specifiers
,
24697 initializer
, /*init_const_expr_p=*/true,
24698 asm_specification
, attributes
);
24699 if (parser
->fully_implicit_function_template_p
)
24702 finish_fully_implicit_template (parser
, 0);
24704 decl
= finish_fully_implicit_template (parser
, decl
);
24708 cp_finalize_omp_declare_simd (parser
, decl
);
24709 cp_finalize_oacc_routine (parser
, decl
, false);
24711 /* Reset PREFIX_ATTRIBUTES. */
24712 if (attributes
!= error_mark_node
)
24714 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24715 attributes
= TREE_CHAIN (attributes
);
24717 TREE_CHAIN (attributes
) = NULL_TREE
;
24720 /* If there is any qualification still in effect, clear it
24721 now; we will be starting fresh with the next declarator. */
24722 parser
->scope
= NULL_TREE
;
24723 parser
->qualifying_scope
= NULL_TREE
;
24724 parser
->object_scope
= NULL_TREE
;
24725 /* If it's a `,', then there are more declarators. */
24726 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24728 cp_lexer_consume_token (parser
->lexer
);
24729 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24731 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24732 gcc_rich_location
richloc (token
->location
);
24733 richloc
.add_fixit_remove ();
24734 error_at (&richloc
, "stray %<,%> at end of "
24735 "member declaration");
24738 /* If the next token isn't a `;', then we have a parse error. */
24739 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24742 /* The next token might be a ways away from where the
24743 actual semicolon is missing. Find the previous token
24744 and use that for our error position. */
24745 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24746 gcc_rich_location
richloc (token
->location
);
24747 richloc
.add_fixit_insert_after (";");
24748 error_at (&richloc
, "expected %<;%> at end of "
24749 "member declaration");
24751 /* Assume that the user meant to provide a semicolon. If
24752 we were to cp_parser_skip_to_end_of_statement, we might
24753 skip to a semicolon inside a member function definition
24754 and issue nonsensical error messages. */
24755 assume_semicolon
= true;
24760 /* Add DECL to the list of members. */
24762 /* Explicitly include, eg, NSDMIs, for better error
24763 recovery (c++/58650). */
24764 || !DECL_DECLARES_FUNCTION_P (decl
))
24765 finish_member_declaration (decl
);
24767 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24768 cp_parser_save_default_args (parser
, decl
);
24769 else if (TREE_CODE (decl
) == FIELD_DECL
24770 && DECL_INITIAL (decl
))
24771 /* Add DECL to the queue of NSDMI to be parsed later. */
24772 vec_safe_push (unparsed_nsdmis
, decl
);
24775 if (assume_semicolon
)
24780 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24782 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24785 /* Parse a pure-specifier.
24790 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24791 Otherwise, ERROR_MARK_NODE is returned. */
24794 cp_parser_pure_specifier (cp_parser
* parser
)
24798 /* Look for the `=' token. */
24799 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24800 return error_mark_node
;
24801 /* Look for the `0' token. */
24802 token
= cp_lexer_peek_token (parser
->lexer
);
24804 if (token
->type
== CPP_EOF
24805 || token
->type
== CPP_PRAGMA_EOL
)
24806 return error_mark_node
;
24808 cp_lexer_consume_token (parser
->lexer
);
24810 /* Accept = default or = delete in c++0x mode. */
24811 if (token
->keyword
== RID_DEFAULT
24812 || token
->keyword
== RID_DELETE
)
24814 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24815 return token
->u
.value
;
24818 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24819 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24821 cp_parser_error (parser
,
24822 "invalid pure specifier (only %<= 0%> is allowed)");
24823 cp_parser_skip_to_end_of_statement (parser
);
24824 return error_mark_node
;
24826 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24828 error_at (token
->location
, "templates may not be %<virtual%>");
24829 return error_mark_node
;
24832 return integer_zero_node
;
24835 /* Parse a constant-initializer.
24837 constant-initializer:
24838 = constant-expression
24840 Returns a representation of the constant-expression. */
24843 cp_parser_constant_initializer (cp_parser
* parser
)
24845 /* Look for the `=' token. */
24846 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24847 return error_mark_node
;
24849 /* It is invalid to write:
24851 struct S { static const int i = { 7 }; };
24854 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24856 cp_parser_error (parser
,
24857 "a brace-enclosed initializer is not allowed here");
24858 /* Consume the opening brace. */
24859 matching_braces braces
;
24860 braces
.consume_open (parser
);
24861 /* Skip the initializer. */
24862 cp_parser_skip_to_closing_brace (parser
);
24863 /* Look for the trailing `}'. */
24864 braces
.require_close (parser
);
24866 return error_mark_node
;
24869 return cp_parser_constant_expression (parser
);
24872 /* Derived classes [gram.class.derived] */
24874 /* Parse a base-clause.
24877 : base-specifier-list
24879 base-specifier-list:
24880 base-specifier ... [opt]
24881 base-specifier-list , base-specifier ... [opt]
24883 Returns a TREE_LIST representing the base-classes, in the order in
24884 which they were declared. The representation of each node is as
24885 described by cp_parser_base_specifier.
24887 In the case that no bases are specified, this function will return
24888 NULL_TREE, not ERROR_MARK_NODE. */
24891 cp_parser_base_clause (cp_parser
* parser
)
24893 tree bases
= NULL_TREE
;
24895 /* Look for the `:' that begins the list. */
24896 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24898 /* Scan the base-specifier-list. */
24903 bool pack_expansion_p
= false;
24905 /* Look for the base-specifier. */
24906 base
= cp_parser_base_specifier (parser
);
24907 /* Look for the (optional) ellipsis. */
24908 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24910 /* Consume the `...'. */
24911 cp_lexer_consume_token (parser
->lexer
);
24913 pack_expansion_p
= true;
24916 /* Add BASE to the front of the list. */
24917 if (base
&& base
!= error_mark_node
)
24919 if (pack_expansion_p
)
24920 /* Make this a pack expansion type. */
24921 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24923 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24925 TREE_CHAIN (base
) = bases
;
24929 /* Peek at the next token. */
24930 token
= cp_lexer_peek_token (parser
->lexer
);
24931 /* If it's not a comma, then the list is complete. */
24932 if (token
->type
!= CPP_COMMA
)
24934 /* Consume the `,'. */
24935 cp_lexer_consume_token (parser
->lexer
);
24938 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24939 base class had a qualified name. However, the next name that
24940 appears is certainly not qualified. */
24941 parser
->scope
= NULL_TREE
;
24942 parser
->qualifying_scope
= NULL_TREE
;
24943 parser
->object_scope
= NULL_TREE
;
24945 return nreverse (bases
);
24948 /* Parse a base-specifier.
24951 :: [opt] nested-name-specifier [opt] class-name
24952 virtual access-specifier [opt] :: [opt] nested-name-specifier
24954 access-specifier virtual [opt] :: [opt] nested-name-specifier
24957 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24958 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24959 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24960 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24963 cp_parser_base_specifier (cp_parser
* parser
)
24967 bool virtual_p
= false;
24968 bool duplicate_virtual_error_issued_p
= false;
24969 bool duplicate_access_error_issued_p
= false;
24970 bool class_scope_p
, template_p
;
24971 tree access
= access_default_node
;
24974 /* Process the optional `virtual' and `access-specifier'. */
24977 /* Peek at the next token. */
24978 token
= cp_lexer_peek_token (parser
->lexer
);
24979 /* Process `virtual'. */
24980 switch (token
->keyword
)
24983 /* If `virtual' appears more than once, issue an error. */
24984 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24986 cp_parser_error (parser
,
24987 "%<virtual%> specified more than once in base-specifier");
24988 duplicate_virtual_error_issued_p
= true;
24993 /* Consume the `virtual' token. */
24994 cp_lexer_consume_token (parser
->lexer
);
24999 case RID_PROTECTED
:
25001 /* If more than one access specifier appears, issue an
25003 if (access
!= access_default_node
25004 && !duplicate_access_error_issued_p
)
25006 cp_parser_error (parser
,
25007 "more than one access specifier in base-specifier");
25008 duplicate_access_error_issued_p
= true;
25011 access
= ridpointers
[(int) token
->keyword
];
25013 /* Consume the access-specifier. */
25014 cp_lexer_consume_token (parser
->lexer
);
25023 /* It is not uncommon to see programs mechanically, erroneously, use
25024 the 'typename' keyword to denote (dependent) qualified types
25025 as base classes. */
25026 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25028 token
= cp_lexer_peek_token (parser
->lexer
);
25029 if (!processing_template_decl
)
25030 error_at (token
->location
,
25031 "keyword %<typename%> not allowed outside of templates");
25033 error_at (token
->location
,
25034 "keyword %<typename%> not allowed in this context "
25035 "(the base class is implicitly a type)");
25036 cp_lexer_consume_token (parser
->lexer
);
25039 /* Look for the optional `::' operator. */
25040 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
25041 /* Look for the nested-name-specifier. The simplest way to
25046 The keyword `typename' is not permitted in a base-specifier or
25047 mem-initializer; in these contexts a qualified name that
25048 depends on a template-parameter is implicitly assumed to be a
25051 is to pretend that we have seen the `typename' keyword at this
25053 cp_parser_nested_name_specifier_opt (parser
,
25054 /*typename_keyword_p=*/true,
25055 /*check_dependency_p=*/true,
25057 /*is_declaration=*/true);
25058 /* If the base class is given by a qualified name, assume that names
25059 we see are type names or templates, as appropriate. */
25060 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
25061 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
25064 && cp_lexer_next_token_is_decltype (parser
->lexer
))
25065 /* DR 950 allows decltype as a base-specifier. */
25066 type
= cp_parser_decltype (parser
);
25069 /* Otherwise, look for the class-name. */
25070 type
= cp_parser_class_name (parser
,
25074 /*check_dependency_p=*/true,
25075 /*class_head_p=*/false,
25076 /*is_declaration=*/true);
25077 type
= TREE_TYPE (type
);
25080 if (type
== error_mark_node
)
25081 return error_mark_node
;
25083 return finish_base_specifier (type
, access
, virtual_p
);
25086 /* Exception handling [gram.exception] */
25088 /* Parse an (optional) noexcept-specification.
25090 noexcept-specification:
25091 noexcept ( constant-expression ) [opt]
25093 If no noexcept-specification is present, returns NULL_TREE.
25094 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25095 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25096 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25097 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25098 in which case a boolean condition is returned instead. */
25101 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
25102 bool require_constexpr
,
25103 bool* consumed_expr
,
25107 const char *saved_message
;
25109 /* Peek at the next token. */
25110 token
= cp_lexer_peek_token (parser
->lexer
);
25112 /* Is it a noexcept-specification? */
25113 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
25116 cp_lexer_consume_token (parser
->lexer
);
25118 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
25120 matching_parens parens
;
25121 parens
.consume_open (parser
);
25123 tree save_ccp
= current_class_ptr
;
25124 tree save_ccr
= current_class_ref
;
25126 if (current_class_type
)
25127 inject_this_parameter (current_class_type
, TYPE_UNQUALIFIED
);
25129 if (require_constexpr
)
25131 /* Types may not be defined in an exception-specification. */
25132 saved_message
= parser
->type_definition_forbidden_message
;
25133 parser
->type_definition_forbidden_message
25134 = G_("types may not be defined in an exception-specification");
25136 expr
= cp_parser_constant_expression (parser
);
25138 /* Restore the saved message. */
25139 parser
->type_definition_forbidden_message
= saved_message
;
25143 expr
= cp_parser_expression (parser
);
25144 *consumed_expr
= true;
25147 parens
.require_close (parser
);
25149 current_class_ptr
= save_ccp
;
25150 current_class_ref
= save_ccr
;
25154 expr
= boolean_true_node
;
25155 if (!require_constexpr
)
25156 *consumed_expr
= false;
25159 /* We cannot build a noexcept-spec right away because this will check
25160 that expr is a constexpr. */
25162 return build_noexcept_spec (expr
, tf_warning_or_error
);
25170 /* Parse an (optional) exception-specification.
25172 exception-specification:
25173 throw ( type-id-list [opt] )
25175 Returns a TREE_LIST representing the exception-specification. The
25176 TREE_VALUE of each node is a type. */
25179 cp_parser_exception_specification_opt (cp_parser
* parser
)
25183 const char *saved_message
;
25185 /* Peek at the next token. */
25186 token
= cp_lexer_peek_token (parser
->lexer
);
25188 /* Is it a noexcept-specification? */
25189 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
25191 if (type_id_list
!= NULL_TREE
)
25192 return type_id_list
;
25194 /* If it's not `throw', then there's no exception-specification. */
25195 if (!cp_parser_is_keyword (token
, RID_THROW
))
25198 location_t loc
= token
->location
;
25200 /* Consume the `throw'. */
25201 cp_lexer_consume_token (parser
->lexer
);
25203 /* Look for the `('. */
25204 matching_parens parens
;
25205 parens
.require_open (parser
);
25207 /* Peek at the next token. */
25208 token
= cp_lexer_peek_token (parser
->lexer
);
25209 /* If it's not a `)', then there is a type-id-list. */
25210 if (token
->type
!= CPP_CLOSE_PAREN
)
25212 /* Types may not be defined in an exception-specification. */
25213 saved_message
= parser
->type_definition_forbidden_message
;
25214 parser
->type_definition_forbidden_message
25215 = G_("types may not be defined in an exception-specification");
25216 /* Parse the type-id-list. */
25217 type_id_list
= cp_parser_type_id_list (parser
);
25218 /* Restore the saved message. */
25219 parser
->type_definition_forbidden_message
= saved_message
;
25221 if (cxx_dialect
>= cxx17
)
25223 error_at (loc
, "ISO C++17 does not allow dynamic exception "
25225 type_id_list
= NULL_TREE
;
25227 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
25228 warning_at (loc
, OPT_Wdeprecated
,
25229 "dynamic exception specifications are deprecated in "
25232 /* In C++17, throw() is equivalent to noexcept (true). throw()
25233 is deprecated in C++11 and above as well, but is still widely used,
25234 so don't warn about it yet. */
25235 else if (cxx_dialect
>= cxx17
)
25236 type_id_list
= noexcept_true_spec
;
25238 type_id_list
= empty_except_spec
;
25240 /* Look for the `)'. */
25241 parens
.require_close (parser
);
25243 return type_id_list
;
25246 /* Parse an (optional) type-id-list.
25250 type-id-list , type-id ... [opt]
25252 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25253 in the order that the types were presented. */
25256 cp_parser_type_id_list (cp_parser
* parser
)
25258 tree types
= NULL_TREE
;
25265 token
= cp_lexer_peek_token (parser
->lexer
);
25267 /* Get the next type-id. */
25268 type
= cp_parser_type_id (parser
);
25269 /* Check for invalid 'auto'. */
25270 if (flag_concepts
&& type_uses_auto (type
))
25272 error_at (token
->location
,
25273 "invalid use of %<auto%> in exception-specification");
25274 type
= error_mark_node
;
25276 /* Parse the optional ellipsis. */
25277 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25279 /* Consume the `...'. */
25280 cp_lexer_consume_token (parser
->lexer
);
25282 /* Turn the type into a pack expansion expression. */
25283 type
= make_pack_expansion (type
);
25285 /* Add it to the list. */
25286 types
= add_exception_specifier (types
, type
, /*complain=*/1);
25287 /* Peek at the next token. */
25288 token
= cp_lexer_peek_token (parser
->lexer
);
25289 /* If it is not a `,', we are done. */
25290 if (token
->type
!= CPP_COMMA
)
25292 /* Consume the `,'. */
25293 cp_lexer_consume_token (parser
->lexer
);
25296 return nreverse (types
);
25299 /* Parse a try-block.
25302 try compound-statement handler-seq */
25305 cp_parser_try_block (cp_parser
* parser
)
25309 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
25310 if (parser
->in_function_body
25311 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
25312 error ("%<try%> in %<constexpr%> function");
25314 try_block
= begin_try_block ();
25315 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
25316 finish_try_block (try_block
);
25317 cp_parser_handler_seq (parser
);
25318 finish_handler_sequence (try_block
);
25323 /* Parse a function-try-block.
25325 function-try-block:
25326 try ctor-initializer [opt] function-body handler-seq */
25329 cp_parser_function_try_block (cp_parser
* parser
)
25331 tree compound_stmt
;
25334 /* Look for the `try' keyword. */
25335 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
25337 /* Let the rest of the front end know where we are. */
25338 try_block
= begin_function_try_block (&compound_stmt
);
25339 /* Parse the function-body. */
25340 cp_parser_ctor_initializer_opt_and_function_body
25341 (parser
, /*in_function_try_block=*/true);
25342 /* We're done with the `try' part. */
25343 finish_function_try_block (try_block
);
25344 /* Parse the handlers. */
25345 cp_parser_handler_seq (parser
);
25346 /* We're done with the handlers. */
25347 finish_function_handler_sequence (try_block
, compound_stmt
);
25350 /* Parse a handler-seq.
25353 handler handler-seq [opt] */
25356 cp_parser_handler_seq (cp_parser
* parser
)
25362 /* Parse the handler. */
25363 cp_parser_handler (parser
);
25364 /* Peek at the next token. */
25365 token
= cp_lexer_peek_token (parser
->lexer
);
25366 /* If it's not `catch' then there are no more handlers. */
25367 if (!cp_parser_is_keyword (token
, RID_CATCH
))
25372 /* Parse a handler.
25375 catch ( exception-declaration ) compound-statement */
25378 cp_parser_handler (cp_parser
* parser
)
25383 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
25384 handler
= begin_handler ();
25385 matching_parens parens
;
25386 parens
.require_open (parser
);
25387 declaration
= cp_parser_exception_declaration (parser
);
25388 finish_handler_parms (declaration
, handler
);
25389 parens
.require_close (parser
);
25390 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
25391 finish_handler (handler
);
25394 /* Parse an exception-declaration.
25396 exception-declaration:
25397 type-specifier-seq declarator
25398 type-specifier-seq abstract-declarator
25402 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25403 ellipsis variant is used. */
25406 cp_parser_exception_declaration (cp_parser
* parser
)
25408 cp_decl_specifier_seq type_specifiers
;
25409 cp_declarator
*declarator
;
25410 const char *saved_message
;
25412 /* If it's an ellipsis, it's easy to handle. */
25413 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25415 /* Consume the `...' token. */
25416 cp_lexer_consume_token (parser
->lexer
);
25420 /* Types may not be defined in exception-declarations. */
25421 saved_message
= parser
->type_definition_forbidden_message
;
25422 parser
->type_definition_forbidden_message
25423 = G_("types may not be defined in exception-declarations");
25425 /* Parse the type-specifier-seq. */
25426 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
25427 /*is_declaration=*/true,
25428 /*is_trailing_return=*/false,
25430 /* If it's a `)', then there is no declarator. */
25431 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25434 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
25435 CP_PARSER_FLAGS_NONE
,
25436 /*ctor_dtor_or_conv_p=*/NULL
,
25437 /*parenthesized_p=*/NULL
,
25438 /*member_p=*/false,
25439 /*friend_p=*/false,
25440 /*static_p=*/false);
25442 /* Restore the saved message. */
25443 parser
->type_definition_forbidden_message
= saved_message
;
25445 if (!type_specifiers
.any_specifiers_p
)
25446 return error_mark_node
;
25448 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
25451 /* Parse a throw-expression.
25454 throw assignment-expression [opt]
25456 Returns a THROW_EXPR representing the throw-expression. */
25459 cp_parser_throw_expression (cp_parser
* parser
)
25464 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
25465 token
= cp_lexer_peek_token (parser
->lexer
);
25466 /* Figure out whether or not there is an assignment-expression
25467 following the "throw" keyword. */
25468 if (token
->type
== CPP_COMMA
25469 || token
->type
== CPP_SEMICOLON
25470 || token
->type
== CPP_CLOSE_PAREN
25471 || token
->type
== CPP_CLOSE_SQUARE
25472 || token
->type
== CPP_CLOSE_BRACE
25473 || token
->type
== CPP_COLON
)
25474 expression
= NULL_TREE
;
25476 expression
= cp_parser_assignment_expression (parser
);
25478 return build_throw (expression
);
25481 /* GNU Extensions */
25483 /* Parse an (optional) asm-specification.
25486 asm ( string-literal )
25488 If the asm-specification is present, returns a STRING_CST
25489 corresponding to the string-literal. Otherwise, returns
25493 cp_parser_asm_specification_opt (cp_parser
* parser
)
25496 tree asm_specification
;
25498 /* Peek at the next token. */
25499 token
= cp_lexer_peek_token (parser
->lexer
);
25500 /* If the next token isn't the `asm' keyword, then there's no
25501 asm-specification. */
25502 if (!cp_parser_is_keyword (token
, RID_ASM
))
25505 /* Consume the `asm' token. */
25506 cp_lexer_consume_token (parser
->lexer
);
25507 /* Look for the `('. */
25508 matching_parens parens
;
25509 parens
.require_open (parser
);
25511 /* Look for the string-literal. */
25512 asm_specification
= cp_parser_string_literal (parser
, false, false);
25514 /* Look for the `)'. */
25515 parens
.require_close (parser
);
25517 return asm_specification
;
25520 /* Parse an asm-operand-list.
25524 asm-operand-list , asm-operand
25527 string-literal ( expression )
25528 [ string-literal ] string-literal ( expression )
25530 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25531 each node is the expression. The TREE_PURPOSE is itself a
25532 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25533 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25534 is a STRING_CST for the string literal before the parenthesis. Returns
25535 ERROR_MARK_NODE if any of the operands are invalid. */
25538 cp_parser_asm_operand_list (cp_parser
* parser
)
25540 tree asm_operands
= NULL_TREE
;
25541 bool invalid_operands
= false;
25545 tree string_literal
;
25549 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25551 /* Consume the `[' token. */
25552 cp_lexer_consume_token (parser
->lexer
);
25553 /* Read the operand name. */
25554 name
= cp_parser_identifier (parser
);
25555 if (name
!= error_mark_node
)
25556 name
= build_string (IDENTIFIER_LENGTH (name
),
25557 IDENTIFIER_POINTER (name
));
25558 /* Look for the closing `]'. */
25559 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25563 /* Look for the string-literal. */
25564 string_literal
= cp_parser_string_literal (parser
, false, false);
25566 /* Look for the `('. */
25567 matching_parens parens
;
25568 parens
.require_open (parser
);
25569 /* Parse the expression. */
25570 expression
= cp_parser_expression (parser
);
25571 /* Look for the `)'. */
25572 parens
.require_close (parser
);
25574 if (name
== error_mark_node
25575 || string_literal
== error_mark_node
25576 || expression
== error_mark_node
)
25577 invalid_operands
= true;
25579 /* Add this operand to the list. */
25580 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
25583 /* If the next token is not a `,', there are no more
25585 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25587 /* Consume the `,'. */
25588 cp_lexer_consume_token (parser
->lexer
);
25591 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
25594 /* Parse an asm-clobber-list.
25598 asm-clobber-list , string-literal
25600 Returns a TREE_LIST, indicating the clobbers in the order that they
25601 appeared. The TREE_VALUE of each node is a STRING_CST. */
25604 cp_parser_asm_clobber_list (cp_parser
* parser
)
25606 tree clobbers
= NULL_TREE
;
25610 tree string_literal
;
25612 /* Look for the string literal. */
25613 string_literal
= cp_parser_string_literal (parser
, false, false);
25614 /* Add it to the list. */
25615 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
25616 /* If the next token is not a `,', then the list is
25618 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25620 /* Consume the `,' token. */
25621 cp_lexer_consume_token (parser
->lexer
);
25627 /* Parse an asm-label-list.
25631 asm-label-list , identifier
25633 Returns a TREE_LIST, indicating the labels in the order that they
25634 appeared. The TREE_VALUE of each node is a label. */
25637 cp_parser_asm_label_list (cp_parser
* parser
)
25639 tree labels
= NULL_TREE
;
25643 tree identifier
, label
, name
;
25645 /* Look for the identifier. */
25646 identifier
= cp_parser_identifier (parser
);
25647 if (!error_operand_p (identifier
))
25649 label
= lookup_label (identifier
);
25650 if (TREE_CODE (label
) == LABEL_DECL
)
25652 TREE_USED (label
) = 1;
25653 check_goto (label
);
25654 name
= build_string (IDENTIFIER_LENGTH (identifier
),
25655 IDENTIFIER_POINTER (identifier
));
25656 labels
= tree_cons (name
, label
, labels
);
25659 /* If the next token is not a `,', then the list is
25661 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25663 /* Consume the `,' token. */
25664 cp_lexer_consume_token (parser
->lexer
);
25667 return nreverse (labels
);
25670 /* Return TRUE iff the next tokens in the stream are possibly the
25671 beginning of a GNU extension attribute. */
25674 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25676 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25679 /* Return TRUE iff the next tokens in the stream are possibly the
25680 beginning of a standard C++-11 attribute specifier. */
25683 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25685 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25688 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25689 beginning of a standard C++-11 attribute specifier. */
25692 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25694 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25696 return (cxx_dialect
>= cxx11
25697 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25698 || (token
->type
== CPP_OPEN_SQUARE
25699 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25700 && token
->type
== CPP_OPEN_SQUARE
)));
25703 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25704 beginning of a GNU extension attribute. */
25707 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25709 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25711 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25714 /* Return true iff the next tokens can be the beginning of either a
25715 GNU attribute list, or a standard C++11 attribute sequence. */
25718 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25720 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25721 || cp_next_tokens_can_be_std_attribute_p (parser
));
25724 /* Return true iff the next Nth tokens can be the beginning of either
25725 a GNU attribute list, or a standard C++11 attribute sequence. */
25728 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25730 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25731 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25734 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25735 of GNU attributes, or return NULL. */
25738 cp_parser_attributes_opt (cp_parser
*parser
)
25740 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25741 return cp_parser_gnu_attributes_opt (parser
);
25742 return cp_parser_std_attribute_spec_seq (parser
);
25745 /* Parse an (optional) series of attributes.
25748 attributes attribute
25751 __attribute__ (( attribute-list [opt] ))
25753 The return value is as for cp_parser_gnu_attribute_list. */
25756 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25758 tree attributes
= NULL_TREE
;
25760 temp_override
<bool> cleanup
25761 (parser
->auto_is_implicit_function_template_parm_p
, false);
25766 tree attribute_list
;
25769 /* Peek at the next token. */
25770 token
= cp_lexer_peek_token (parser
->lexer
);
25771 /* If it's not `__attribute__', then we're done. */
25772 if (token
->keyword
!= RID_ATTRIBUTE
)
25775 /* Consume the `__attribute__' keyword. */
25776 cp_lexer_consume_token (parser
->lexer
);
25777 /* Look for the two `(' tokens. */
25778 matching_parens outer_parens
;
25779 if (!outer_parens
.require_open (parser
))
25781 matching_parens inner_parens
;
25782 if (!inner_parens
.require_open (parser
))
25785 /* Peek at the next token. */
25786 token
= cp_lexer_peek_token (parser
->lexer
);
25787 if (token
->type
!= CPP_CLOSE_PAREN
)
25788 /* Parse the attribute-list. */
25789 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25791 /* If the next token is a `)', then there is no attribute
25793 attribute_list
= NULL
;
25795 /* Look for the two `)' tokens. */
25796 if (!inner_parens
.require_close (parser
))
25798 if (!outer_parens
.require_close (parser
))
25801 cp_parser_skip_to_end_of_statement (parser
);
25803 /* Add these new attributes to the list. */
25804 attributes
= attr_chainon (attributes
, attribute_list
);
25810 /* Parse a GNU attribute-list.
25814 attribute-list , attribute
25818 identifier ( identifier )
25819 identifier ( identifier , expression-list )
25820 identifier ( expression-list )
25822 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25823 to an attribute. The TREE_PURPOSE of each node is the identifier
25824 indicating which attribute is in use. The TREE_VALUE represents
25825 the arguments, if any. */
25828 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
25830 tree attribute_list
= NULL_TREE
;
25831 bool save_translate_strings_p
= parser
->translate_strings_p
;
25833 /* Don't create wrapper nodes within attributes: the
25834 handlers don't know how to handle them. */
25835 auto_suppress_location_wrappers sentinel
;
25837 parser
->translate_strings_p
= false;
25844 /* Look for the identifier. We also allow keywords here; for
25845 example `__attribute__ ((const))' is legal. */
25846 token
= cp_lexer_peek_token (parser
->lexer
);
25847 if (token
->type
== CPP_NAME
25848 || token
->type
== CPP_KEYWORD
)
25850 tree arguments
= NULL_TREE
;
25852 /* Consume the token, but save it since we need it for the
25853 SIMD enabled function parsing. */
25854 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25856 /* Save away the identifier that indicates which attribute
25858 identifier
= (token
->type
== CPP_KEYWORD
)
25859 /* For keywords, use the canonical spelling, not the
25860 parsed identifier. */
25861 ? ridpointers
[(int) token
->keyword
]
25862 : id_token
->u
.value
;
25864 identifier
= canonicalize_attr_name (identifier
);
25865 attribute
= build_tree_list (identifier
, NULL_TREE
);
25867 /* Peek at the next token. */
25868 token
= cp_lexer_peek_token (parser
->lexer
);
25869 /* If it's an `(', then parse the attribute arguments. */
25870 if (token
->type
== CPP_OPEN_PAREN
)
25872 vec
<tree
, va_gc
> *vec
;
25873 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25874 ? id_attr
: normal_attr
);
25875 vec
= cp_parser_parenthesized_expression_list
25876 (parser
, attr_flag
, /*cast_p=*/false,
25877 /*allow_expansion_p=*/false,
25878 /*non_constant_p=*/NULL
);
25880 arguments
= error_mark_node
;
25883 arguments
= build_tree_list_vec (vec
);
25884 release_tree_vector (vec
);
25886 /* Save the arguments away. */
25887 TREE_VALUE (attribute
) = arguments
;
25890 if (arguments
!= error_mark_node
)
25892 /* Add this attribute to the list. */
25893 TREE_CHAIN (attribute
) = attribute_list
;
25894 attribute_list
= attribute
;
25897 token
= cp_lexer_peek_token (parser
->lexer
);
25899 /* Unless EXACTLY_ONE is set look for more attributes.
25900 If the next token isn't a `,', we're done. */
25901 if (exactly_one
|| token
->type
!= CPP_COMMA
)
25904 /* Consume the comma and keep going. */
25905 cp_lexer_consume_token (parser
->lexer
);
25907 parser
->translate_strings_p
= save_translate_strings_p
;
25909 /* We built up the list in reverse order. */
25910 return nreverse (attribute_list
);
25913 /* Parse a standard C++11 attribute.
25915 The returned representation is a TREE_LIST which TREE_PURPOSE is
25916 the scoped name of the attribute, and the TREE_VALUE is its
25919 Note that the scoped name of the attribute is itself a TREE_LIST
25920 which TREE_PURPOSE is the namespace of the attribute, and
25921 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25922 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25923 and which TREE_PURPOSE is directly the attribute name.
25925 Clients of the attribute code should use get_attribute_namespace
25926 and get_attribute_name to get the actual namespace and name of
25927 attributes, regardless of their being GNU or C++11 attributes.
25930 attribute-token attribute-argument-clause [opt]
25934 attribute-scoped-token
25936 attribute-scoped-token:
25937 attribute-namespace :: identifier
25939 attribute-namespace:
25942 attribute-argument-clause:
25943 ( balanced-token-seq )
25945 balanced-token-seq:
25946 balanced-token [opt]
25947 balanced-token-seq balanced-token
25950 ( balanced-token-seq )
25951 [ balanced-token-seq ]
25952 { balanced-token-seq }. */
25955 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25957 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25960 temp_override
<bool> cleanup
25961 (parser
->auto_is_implicit_function_template_parm_p
, false);
25963 /* First, parse name of the attribute, a.k.a attribute-token. */
25965 token
= cp_lexer_peek_token (parser
->lexer
);
25966 if (token
->type
== CPP_NAME
)
25967 attr_id
= token
->u
.value
;
25968 else if (token
->type
== CPP_KEYWORD
)
25969 attr_id
= ridpointers
[(int) token
->keyword
];
25970 else if (token
->flags
& NAMED_OP
)
25971 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25973 if (attr_id
== NULL_TREE
)
25976 cp_lexer_consume_token (parser
->lexer
);
25978 token
= cp_lexer_peek_token (parser
->lexer
);
25979 if (token
->type
== CPP_SCOPE
)
25981 /* We are seeing a scoped attribute token. */
25983 cp_lexer_consume_token (parser
->lexer
);
25985 error_at (token
->location
, "attribute using prefix used together "
25986 "with scoped attribute token");
25989 token
= cp_lexer_consume_token (parser
->lexer
);
25990 if (token
->type
== CPP_NAME
)
25991 attr_id
= token
->u
.value
;
25992 else if (token
->type
== CPP_KEYWORD
)
25993 attr_id
= ridpointers
[(int) token
->keyword
];
25994 else if (token
->flags
& NAMED_OP
)
25995 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25998 error_at (token
->location
,
25999 "expected an identifier for the attribute name");
26000 return error_mark_node
;
26003 attr_ns
= canonicalize_attr_name (attr_ns
);
26004 attr_id
= canonicalize_attr_name (attr_id
);
26005 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
26007 token
= cp_lexer_peek_token (parser
->lexer
);
26011 attr_ns
= canonicalize_attr_name (attr_ns
);
26012 attr_id
= canonicalize_attr_name (attr_id
);
26013 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
26018 attr_id
= canonicalize_attr_name (attr_id
);
26019 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
26021 /* C++11 noreturn attribute is equivalent to GNU's. */
26022 if (is_attribute_p ("noreturn", attr_id
))
26023 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26024 /* C++14 deprecated attribute is equivalent to GNU's. */
26025 else if (is_attribute_p ("deprecated", attr_id
))
26026 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26027 /* C++17 fallthrough attribute is equivalent to GNU's. */
26028 else if (is_attribute_p ("fallthrough", attr_id
))
26029 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26030 /* Transactional Memory TS optimize_for_synchronized attribute is
26031 equivalent to GNU transaction_callable. */
26032 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
26033 TREE_PURPOSE (attribute
)
26034 = get_identifier ("transaction_callable");
26035 /* Transactional Memory attributes are GNU attributes. */
26036 else if (tm_attr_to_mask (attr_id
))
26037 TREE_PURPOSE (attribute
) = attr_id
;
26040 /* Now parse the optional argument clause of the attribute. */
26042 if (token
->type
!= CPP_OPEN_PAREN
)
26046 vec
<tree
, va_gc
> *vec
;
26047 int attr_flag
= normal_attr
;
26049 if (attr_ns
== gnu_identifier
26050 && attribute_takes_identifier_p (attr_id
))
26051 /* A GNU attribute that takes an identifier in parameter. */
26052 attr_flag
= id_attr
;
26054 vec
= cp_parser_parenthesized_expression_list
26055 (parser
, attr_flag
, /*cast_p=*/false,
26056 /*allow_expansion_p=*/true,
26057 /*non_constant_p=*/NULL
);
26059 arguments
= error_mark_node
;
26062 arguments
= build_tree_list_vec (vec
);
26063 release_tree_vector (vec
);
26066 if (arguments
== error_mark_node
)
26067 attribute
= error_mark_node
;
26069 TREE_VALUE (attribute
) = arguments
;
26075 /* Check that the attribute ATTRIBUTE appears at most once in the
26076 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26077 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26078 isn't implemented yet in GCC. */
26081 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
26085 tree name
= get_attribute_name (attribute
);
26086 if (is_attribute_p ("noreturn", name
)
26087 && lookup_attribute ("noreturn", attributes
))
26088 error ("attribute %<noreturn%> can appear at most once "
26089 "in an attribute-list");
26090 else if (is_attribute_p ("deprecated", name
)
26091 && lookup_attribute ("deprecated", attributes
))
26092 error ("attribute %<deprecated%> can appear at most once "
26093 "in an attribute-list");
26097 /* Parse a list of standard C++-11 attributes.
26101 attribute-list , attribute[opt]
26103 attribute-list , attribute ...
26107 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
26109 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
26110 cp_token
*token
= NULL
;
26114 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
26115 if (attribute
== error_mark_node
)
26117 if (attribute
!= NULL_TREE
)
26119 cp_parser_check_std_attribute (attributes
, attribute
);
26120 TREE_CHAIN (attribute
) = attributes
;
26121 attributes
= attribute
;
26123 token
= cp_lexer_peek_token (parser
->lexer
);
26124 if (token
->type
== CPP_ELLIPSIS
)
26126 cp_lexer_consume_token (parser
->lexer
);
26127 if (attribute
== NULL_TREE
)
26128 error_at (token
->location
,
26129 "expected attribute before %<...%>");
26132 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
26133 if (pack
== error_mark_node
)
26134 return error_mark_node
;
26135 TREE_VALUE (attribute
) = pack
;
26137 token
= cp_lexer_peek_token (parser
->lexer
);
26139 if (token
->type
!= CPP_COMMA
)
26141 cp_lexer_consume_token (parser
->lexer
);
26143 attributes
= nreverse (attributes
);
26147 /* Parse a standard C++-11 attribute specifier.
26149 attribute-specifier:
26150 [ [ attribute-using-prefix [opt] attribute-list ] ]
26151 alignment-specifier
26153 attribute-using-prefix:
26154 using attribute-namespace :
26156 alignment-specifier:
26157 alignas ( type-id ... [opt] )
26158 alignas ( alignment-expression ... [opt] ). */
26161 cp_parser_std_attribute_spec (cp_parser
*parser
)
26163 tree attributes
= NULL_TREE
;
26164 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26166 if (token
->type
== CPP_OPEN_SQUARE
26167 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
26169 tree attr_ns
= NULL_TREE
;
26171 cp_lexer_consume_token (parser
->lexer
);
26172 cp_lexer_consume_token (parser
->lexer
);
26174 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26176 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26177 if (token
->type
== CPP_NAME
)
26178 attr_ns
= token
->u
.value
;
26179 else if (token
->type
== CPP_KEYWORD
)
26180 attr_ns
= ridpointers
[(int) token
->keyword
];
26181 else if (token
->flags
& NAMED_OP
)
26182 attr_ns
= get_identifier (cpp_type2name (token
->type
,
26185 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
26187 if (cxx_dialect
< cxx17
26188 && !in_system_header_at (input_location
))
26189 pedwarn (input_location
, 0,
26190 "attribute using prefix only available "
26191 "with -std=c++17 or -std=gnu++17");
26193 cp_lexer_consume_token (parser
->lexer
);
26194 cp_lexer_consume_token (parser
->lexer
);
26195 cp_lexer_consume_token (parser
->lexer
);
26198 attr_ns
= NULL_TREE
;
26201 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
26203 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
26204 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
26205 cp_parser_skip_to_end_of_statement (parser
);
26207 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26208 when we are sure that we have actually parsed them. */
26209 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26215 /* Look for an alignment-specifier. */
26217 token
= cp_lexer_peek_token (parser
->lexer
);
26219 if (token
->type
!= CPP_KEYWORD
26220 || token
->keyword
!= RID_ALIGNAS
)
26223 cp_lexer_consume_token (parser
->lexer
);
26224 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26226 matching_parens parens
;
26227 if (!parens
.require_open (parser
))
26228 return error_mark_node
;
26230 cp_parser_parse_tentatively (parser
);
26231 alignas_expr
= cp_parser_type_id (parser
);
26233 if (!cp_parser_parse_definitely (parser
))
26235 alignas_expr
= cp_parser_assignment_expression (parser
);
26236 if (alignas_expr
== error_mark_node
)
26237 cp_parser_skip_to_end_of_statement (parser
);
26238 if (alignas_expr
== NULL_TREE
26239 || alignas_expr
== error_mark_node
)
26240 return alignas_expr
;
26243 alignas_expr
= cxx_alignas_expr (alignas_expr
);
26244 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
26246 /* Handle alignas (pack...). */
26247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26249 cp_lexer_consume_token (parser
->lexer
);
26250 alignas_expr
= make_pack_expansion (alignas_expr
);
26253 /* Something went wrong, so don't build the attribute. */
26254 if (alignas_expr
== error_mark_node
)
26255 return error_mark_node
;
26257 if (!parens
.require_close (parser
))
26258 return error_mark_node
;
26260 /* Build the C++-11 representation of an 'aligned'
26263 = build_tree_list (build_tree_list (gnu_identifier
,
26264 aligned_identifier
), alignas_expr
);
26270 /* Parse a standard C++-11 attribute-specifier-seq.
26272 attribute-specifier-seq:
26273 attribute-specifier-seq [opt] attribute-specifier
26277 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
26279 tree attr_specs
= NULL_TREE
;
26280 tree attr_last
= NULL_TREE
;
26282 /* Don't create wrapper nodes within attributes: the
26283 handlers don't know how to handle them. */
26284 auto_suppress_location_wrappers sentinel
;
26288 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
26289 if (attr_spec
== NULL_TREE
)
26291 if (attr_spec
== error_mark_node
)
26292 return error_mark_node
;
26295 TREE_CHAIN (attr_last
) = attr_spec
;
26297 attr_specs
= attr_last
= attr_spec
;
26298 attr_last
= tree_last (attr_last
);
26304 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26305 return index of the first token after balanced-token, or N on failure. */
26308 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
26311 int nparens
= 0, nbraces
= 0, nsquares
= 0;
26313 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
26315 case CPP_PRAGMA_EOL
:
26316 if (!parser
->lexer
->in_pragma
)
26320 /* Ran out of tokens. */
26322 case CPP_OPEN_PAREN
:
26325 case CPP_OPEN_BRACE
:
26328 case CPP_OPEN_SQUARE
:
26331 case CPP_CLOSE_PAREN
:
26334 case CPP_CLOSE_BRACE
:
26337 case CPP_CLOSE_SQUARE
:
26343 while (nparens
|| nbraces
|| nsquares
);
26347 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26348 return index of the first token after the GNU attribute tokens, or N on
26352 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
26356 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
26357 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
26358 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
26361 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
26364 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
26371 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26372 next token), return index of the first token after the standard C++11
26373 attribute tokens, or N on failure. */
26376 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
26380 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
26381 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
26383 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26386 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
26390 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
26391 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
26393 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26404 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26405 as the next token), return index of the first token after the attribute
26406 tokens, or N on failure. */
26409 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
26411 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
26412 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
26413 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
26416 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26417 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26418 current value of the PEDANTIC flag, regardless of whether or not
26419 the `__extension__' keyword is present. The caller is responsible
26420 for restoring the value of the PEDANTIC flag. */
26423 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
26425 /* Save the old value of the PEDANTIC flag. */
26426 *saved_pedantic
= pedantic
;
26428 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
26430 /* Consume the `__extension__' token. */
26431 cp_lexer_consume_token (parser
->lexer
);
26432 /* We're not being pedantic while the `__extension__' keyword is
26442 /* Parse a label declaration.
26445 __label__ label-declarator-seq ;
26447 label-declarator-seq:
26448 identifier , label-declarator-seq
26452 cp_parser_label_declaration (cp_parser
* parser
)
26454 /* Look for the `__label__' keyword. */
26455 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
26461 /* Look for an identifier. */
26462 identifier
= cp_parser_identifier (parser
);
26463 /* If we failed, stop. */
26464 if (identifier
== error_mark_node
)
26466 /* Declare it as a label. */
26467 finish_label_decl (identifier
);
26468 /* If the next token is a `;', stop. */
26469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26471 /* Look for the `,' separating the label declarations. */
26472 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
26475 /* Look for the final `;'. */
26476 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
26479 // -------------------------------------------------------------------------- //
26482 // Parse a requires clause.
26484 // requires-clause:
26485 // 'requires' logical-or-expression
26487 // The required logical-or-expression must be a constant expression. Note
26488 // that we don't check that the expression is constepxr here. We defer until
26489 // we analyze constraints and then, we only check atomic constraints.
26491 cp_parser_requires_clause (cp_parser
*parser
)
26493 // Parse the requires clause so that it is not automatically folded.
26494 ++processing_template_decl
;
26495 tree expr
= cp_parser_binary_expression (parser
, false, false,
26496 PREC_NOT_OPERATOR
, NULL
);
26497 if (check_for_bare_parameter_packs (expr
))
26498 expr
= error_mark_node
;
26499 --processing_template_decl
;
26503 // Optionally parse a requires clause:
26505 cp_parser_requires_clause_opt (cp_parser
*parser
)
26507 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
26508 if (tok
->keyword
!= RID_REQUIRES
)
26510 if (!flag_concepts
&& tok
->type
== CPP_NAME
26511 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
26513 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26514 "%<requires%> only available with -fconcepts");
26515 /* Parse and discard the requires-clause. */
26516 cp_lexer_consume_token (parser
->lexer
);
26517 cp_parser_requires_clause (parser
);
26521 cp_lexer_consume_token (parser
->lexer
);
26522 return cp_parser_requires_clause (parser
);
26526 /*---------------------------------------------------------------------------
26527 Requires expressions
26528 ---------------------------------------------------------------------------*/
26530 /* Parse a requires expression
26532 requirement-expression:
26533 'requires' requirement-parameter-list [opt] requirement-body */
26535 cp_parser_requires_expression (cp_parser
*parser
)
26537 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
26538 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26540 /* A requires-expression shall appear only within a concept
26541 definition or a requires-clause.
26543 TODO: Implement this diagnostic correctly. */
26544 if (!processing_template_decl
)
26546 error_at (loc
, "a requires expression cannot appear outside a template");
26547 cp_parser_skip_to_end_of_statement (parser
);
26548 return error_mark_node
;
26553 /* Local parameters are delared as variables within the scope
26554 of the expression. They are not visible past the end of
26555 the expression. Expressions within the requires-expression
26556 are unevaluated. */
26557 struct scope_sentinel
26561 ++cp_unevaluated_operand
;
26562 begin_scope (sk_block
, NULL_TREE
);
26567 pop_bindings_and_leave_scope ();
26568 --cp_unevaluated_operand
;
26572 /* Parse the optional parameter list. */
26573 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26575 parms
= cp_parser_requirement_parameter_list (parser
);
26576 if (parms
== error_mark_node
)
26577 return error_mark_node
;
26582 /* Parse the requirement body. */
26583 reqs
= cp_parser_requirement_body (parser
);
26584 if (reqs
== error_mark_node
)
26585 return error_mark_node
;
26588 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26590 grokparms (parms
, &parms
);
26591 return finish_requires_expr (parms
, reqs
);
26594 /* Parse a parameterized requirement.
26596 requirement-parameter-list:
26597 '(' parameter-declaration-clause ')' */
26599 cp_parser_requirement_parameter_list (cp_parser
*parser
)
26601 matching_parens parens
;
26602 if (!parens
.require_open (parser
))
26603 return error_mark_node
;
26606 = cp_parser_parameter_declaration_clause (parser
, CP_PARSER_FLAGS_NONE
);
26608 if (!parens
.require_close (parser
))
26609 return error_mark_node
;
26614 /* Parse the body of a requirement.
26617 '{' requirement-list '}' */
26619 cp_parser_requirement_body (cp_parser
*parser
)
26621 matching_braces braces
;
26622 if (!braces
.require_open (parser
))
26623 return error_mark_node
;
26625 tree reqs
= cp_parser_requirement_list (parser
);
26627 if (!braces
.require_close (parser
))
26628 return error_mark_node
;
26633 /* Parse a list of requirements.
26637 requirement-list ';' requirement[opt] */
26639 cp_parser_requirement_list (cp_parser
*parser
)
26641 tree result
= NULL_TREE
;
26644 tree req
= cp_parser_requirement (parser
);
26645 if (req
== error_mark_node
)
26646 return error_mark_node
;
26648 result
= tree_cons (NULL_TREE
, req
, result
);
26650 /* If we see a semi-colon, consume it. */
26651 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26652 cp_lexer_consume_token (parser
->lexer
);
26654 /* Stop processing at the end of the list. */
26655 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26659 /* Reverse the order of requirements so they are analyzed in
26660 declaration order. */
26661 return nreverse (result
);
26664 /* Parse a syntactic requirement or type requirement.
26668 compound-requirement
26670 nested-requirement */
26672 cp_parser_requirement (cp_parser
*parser
)
26674 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26675 return cp_parser_compound_requirement (parser
);
26676 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26677 return cp_parser_type_requirement (parser
);
26678 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
26679 return cp_parser_nested_requirement (parser
);
26681 return cp_parser_simple_requirement (parser
);
26684 /* Parse a simple requirement.
26686 simple-requirement:
26689 cp_parser_simple_requirement (cp_parser
*parser
)
26691 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26692 if (!expr
|| expr
== error_mark_node
)
26693 return error_mark_node
;
26695 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26696 return error_mark_node
;
26698 return finish_simple_requirement (expr
);
26701 /* Parse a type requirement
26704 nested-name-specifier [opt] required-type-name ';'
26706 required-type-name:
26708 'template' [opt] simple-template-id */
26710 cp_parser_type_requirement (cp_parser
*parser
)
26712 cp_lexer_consume_token (parser
->lexer
);
26714 // Save the scope before parsing name specifiers.
26715 tree saved_scope
= parser
->scope
;
26716 tree saved_object_scope
= parser
->object_scope
;
26717 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26718 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26719 cp_parser_nested_name_specifier_opt (parser
,
26720 /*typename_keyword_p=*/true,
26721 /*check_dependency_p=*/false,
26723 /*is_declaration=*/false);
26726 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26728 cp_lexer_consume_token (parser
->lexer
);
26729 type
= cp_parser_template_id (parser
,
26730 /*template_keyword_p=*/true,
26731 /*check_dependency=*/false,
26732 /*tag_type=*/none_type
,
26733 /*is_declaration=*/false);
26734 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26735 /*complain=*/tf_error
);
26738 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26740 if (TREE_CODE (type
) == TYPE_DECL
)
26741 type
= TREE_TYPE (type
);
26743 parser
->scope
= saved_scope
;
26744 parser
->object_scope
= saved_object_scope
;
26745 parser
->qualifying_scope
= saved_qualifying_scope
;
26747 if (type
== error_mark_node
)
26748 cp_parser_skip_to_end_of_statement (parser
);
26750 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26751 return error_mark_node
;
26752 if (type
== error_mark_node
)
26753 return error_mark_node
;
26755 return finish_type_requirement (type
);
26758 /* Parse a compound requirement
26760 compound-requirement:
26761 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26763 cp_parser_compound_requirement (cp_parser
*parser
)
26765 /* Parse an expression enclosed in '{ }'s. */
26766 matching_braces braces
;
26767 if (!braces
.require_open (parser
))
26768 return error_mark_node
;
26770 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26771 if (!expr
|| expr
== error_mark_node
)
26772 return error_mark_node
;
26774 if (!braces
.require_close (parser
))
26775 return error_mark_node
;
26777 /* Parse the optional noexcept. */
26778 bool noexcept_p
= false;
26779 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26781 cp_lexer_consume_token (parser
->lexer
);
26785 /* Parse the optional trailing return type. */
26786 tree type
= NULL_TREE
;
26787 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26789 cp_lexer_consume_token (parser
->lexer
);
26790 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26791 parser
->in_result_type_constraint_p
= true;
26792 type
= cp_parser_trailing_type_id (parser
);
26793 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26794 if (type
== error_mark_node
)
26795 return error_mark_node
;
26798 return finish_compound_requirement (expr
, type
, noexcept_p
);
26801 /* Parse a nested requirement. This is the same as a requires clause.
26803 nested-requirement:
26806 cp_parser_nested_requirement (cp_parser
*parser
)
26808 cp_lexer_consume_token (parser
->lexer
);
26809 tree req
= cp_parser_requires_clause (parser
);
26810 if (req
== error_mark_node
)
26811 return error_mark_node
;
26812 return finish_nested_requirement (req
);
26815 /* Support Functions */
26817 /* Return the appropriate prefer_type argument for lookup_name_real based on
26818 tag_type and template_mem_access. */
26821 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26823 /* DR 141: When looking in the current enclosing context for a template-name
26824 after -> or ., only consider class templates. */
26825 if (template_mem_access
)
26829 case none_type
: return 0; // No preference.
26830 case scope_type
: return 1; // Type or namespace.
26831 default: return 2; // Type only.
26835 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26836 NAME should have one of the representations used for an
26837 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26838 is returned. If PARSER->SCOPE is a dependent type, then a
26839 SCOPE_REF is returned.
26841 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26842 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26843 was formed. Abstractly, such entities should not be passed to this
26844 function, because they do not need to be looked up, but it is
26845 simpler to check for this special case here, rather than at the
26848 In cases not explicitly covered above, this function returns a
26849 DECL, OVERLOAD, or baselink representing the result of the lookup.
26850 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26853 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26854 (e.g., "struct") that was used. In that case bindings that do not
26855 refer to types are ignored.
26857 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26860 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26863 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26866 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26867 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26868 NULL_TREE otherwise. */
26871 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26872 enum tag_types tag_type
,
26875 bool check_dependency
,
26876 tree
*ambiguous_decls
,
26877 location_t name_location
)
26880 tree object_type
= parser
->context
->object_type
;
26882 /* Assume that the lookup will be unambiguous. */
26883 if (ambiguous_decls
)
26884 *ambiguous_decls
= NULL_TREE
;
26886 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26887 no longer valid. Note that if we are parsing tentatively, and
26888 the parse fails, OBJECT_TYPE will be automatically restored. */
26889 parser
->context
->object_type
= NULL_TREE
;
26891 if (name
== error_mark_node
)
26892 return error_mark_node
;
26894 /* A template-id has already been resolved; there is no lookup to
26896 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26898 if (BASELINK_P (name
))
26900 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26901 == TEMPLATE_ID_EXPR
);
26905 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26906 it should already have been checked to make sure that the name
26907 used matches the type being destroyed. */
26908 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26912 /* Figure out to which type this destructor applies. */
26914 type
= parser
->scope
;
26915 else if (object_type
)
26916 type
= object_type
;
26918 type
= current_class_type
;
26919 /* If that's not a class type, there is no destructor. */
26920 if (!type
|| !CLASS_TYPE_P (type
))
26921 return error_mark_node
;
26923 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26924 lazily_declare_fn (sfk_destructor
, type
);
26926 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26929 return error_mark_node
;
26932 /* By this point, the NAME should be an ordinary identifier. If
26933 the id-expression was a qualified name, the qualifying scope is
26934 stored in PARSER->SCOPE at this point. */
26935 gcc_assert (identifier_p (name
));
26937 /* Perform the lookup. */
26942 if (parser
->scope
== error_mark_node
)
26943 return error_mark_node
;
26945 /* If the SCOPE is dependent, the lookup must be deferred until
26946 the template is instantiated -- unless we are explicitly
26947 looking up names in uninstantiated templates. Even then, we
26948 cannot look up the name if the scope is not a class type; it
26949 might, for example, be a template type parameter. */
26950 dependent_p
= (TYPE_P (parser
->scope
)
26951 && dependent_scope_p (parser
->scope
));
26952 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26954 /* Defer lookup. */
26955 decl
= error_mark_node
;
26958 tree pushed_scope
= NULL_TREE
;
26960 /* If PARSER->SCOPE is a dependent type, then it must be a
26961 class type, and we must not be checking dependencies;
26962 otherwise, we would have processed this lookup above. So
26963 that PARSER->SCOPE is not considered a dependent base by
26964 lookup_member, we must enter the scope here. */
26966 pushed_scope
= push_scope (parser
->scope
);
26968 /* If the PARSER->SCOPE is a template specialization, it
26969 may be instantiated during name lookup. In that case,
26970 errors may be issued. Even if we rollback the current
26971 tentative parse, those errors are valid. */
26972 decl
= lookup_qualified_name (parser
->scope
, name
,
26973 prefer_type_arg (tag_type
),
26974 /*complain=*/true);
26976 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26977 lookup result and the nested-name-specifier nominates a class C:
26978 * if the name specified after the nested-name-specifier, when
26979 looked up in C, is the injected-class-name of C (Clause 9), or
26980 * if the name specified after the nested-name-specifier is the
26981 same as the identifier or the simple-template-id's template-
26982 name in the last component of the nested-name-specifier,
26983 the name is instead considered to name the constructor of
26984 class C. [ Note: for example, the constructor is not an
26985 acceptable lookup result in an elaborated-type-specifier so
26986 the constructor would not be used in place of the
26987 injected-class-name. --end note ] Such a constructor name
26988 shall be used only in the declarator-id of a declaration that
26989 names a constructor or in a using-declaration. */
26990 if (tag_type
== none_type
26991 && DECL_SELF_REFERENCE_P (decl
)
26992 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26993 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26994 prefer_type_arg (tag_type
),
26995 /*complain=*/true);
26997 /* If we have a single function from a using decl, pull it out. */
26998 if (TREE_CODE (decl
) == OVERLOAD
26999 && !really_overloaded_fn (decl
))
27000 decl
= OVL_FUNCTION (decl
);
27003 pop_scope (pushed_scope
);
27006 /* If the scope is a dependent type and either we deferred lookup or
27007 we did lookup but didn't find the name, rememeber the name. */
27008 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
27009 && dependent_type_p (parser
->scope
))
27015 /* The resolution to Core Issue 180 says that `struct
27016 A::B' should be considered a type-name, even if `A'
27018 type
= make_typename_type (parser
->scope
, name
, tag_type
,
27019 /*complain=*/tf_error
);
27020 if (type
!= error_mark_node
)
27021 decl
= TYPE_NAME (type
);
27023 else if (is_template
27024 && (cp_parser_next_token_ends_template_argument_p (parser
)
27025 || cp_lexer_next_token_is (parser
->lexer
,
27027 decl
= make_unbound_class_template (parser
->scope
,
27029 /*complain=*/tf_error
);
27031 decl
= build_qualified_name (/*type=*/NULL_TREE
,
27032 parser
->scope
, name
,
27035 parser
->qualifying_scope
= parser
->scope
;
27036 parser
->object_scope
= NULL_TREE
;
27038 else if (object_type
)
27040 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27041 OBJECT_TYPE is not a class. */
27042 if (CLASS_TYPE_P (object_type
))
27043 /* If the OBJECT_TYPE is a template specialization, it may
27044 be instantiated during name lookup. In that case, errors
27045 may be issued. Even if we rollback the current tentative
27046 parse, those errors are valid. */
27047 decl
= lookup_member (object_type
,
27050 prefer_type_arg (tag_type
),
27051 tf_warning_or_error
);
27056 /* Look it up in the enclosing context. DR 141: When looking for a
27057 template-name after -> or ., only consider class templates. */
27058 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
27060 /*block_p=*/true, is_namespace
, 0);
27061 if (object_type
== unknown_type_node
)
27062 /* The object is type-dependent, so we can't look anything up; we used
27063 this to get the DR 141 behavior. */
27064 object_type
= NULL_TREE
;
27065 parser
->object_scope
= object_type
;
27066 parser
->qualifying_scope
= NULL_TREE
;
27070 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
27072 /*block_p=*/true, is_namespace
, 0);
27073 parser
->qualifying_scope
= NULL_TREE
;
27074 parser
->object_scope
= NULL_TREE
;
27077 /* If the lookup failed, let our caller know. */
27078 if (!decl
|| decl
== error_mark_node
)
27079 return error_mark_node
;
27081 /* Pull out the template from an injected-class-name (or multiple). */
27083 decl
= maybe_get_template_decl_from_type_decl (decl
);
27085 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27086 if (TREE_CODE (decl
) == TREE_LIST
)
27088 if (ambiguous_decls
)
27089 *ambiguous_decls
= decl
;
27090 /* The error message we have to print is too complicated for
27091 cp_parser_error, so we incorporate its actions directly. */
27092 if (!cp_parser_simulate_error (parser
))
27094 error_at (name_location
, "reference to %qD is ambiguous",
27096 print_candidates (decl
);
27098 return error_mark_node
;
27101 gcc_assert (DECL_P (decl
)
27102 || TREE_CODE (decl
) == OVERLOAD
27103 || TREE_CODE (decl
) == SCOPE_REF
27104 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
27105 || BASELINK_P (decl
));
27107 /* If we have resolved the name of a member declaration, check to
27108 see if the declaration is accessible. When the name resolves to
27109 set of overloaded functions, accessibility is checked when
27110 overload resolution is done.
27112 During an explicit instantiation, access is not checked at all,
27113 as per [temp.explicit]. */
27115 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
27117 maybe_record_typedef_use (decl
);
27119 return cp_expr (decl
, name_location
);
27122 /* Like cp_parser_lookup_name, but for use in the typical case where
27123 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27124 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27127 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
27129 return cp_parser_lookup_name (parser
, name
,
27131 /*is_template=*/false,
27132 /*is_namespace=*/false,
27133 /*check_dependency=*/true,
27134 /*ambiguous_decls=*/NULL
,
27138 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27139 the current context, return the TYPE_DECL. If TAG_NAME_P is
27140 true, the DECL indicates the class being defined in a class-head,
27141 or declared in an elaborated-type-specifier.
27143 Otherwise, return DECL. */
27146 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
27148 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27149 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27152 template <typename T> struct B;
27155 template <typename T> struct A::B {};
27157 Similarly, in an elaborated-type-specifier:
27159 namespace N { struct X{}; }
27162 template <typename T> friend struct N::X;
27165 However, if the DECL refers to a class type, and we are in
27166 the scope of the class, then the name lookup automatically
27167 finds the TYPE_DECL created by build_self_reference rather
27168 than a TEMPLATE_DECL. For example, in:
27170 template <class T> struct S {
27174 there is no need to handle such case. */
27176 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
27177 return DECL_TEMPLATE_RESULT (decl
);
27182 /* If too many, or too few, template-parameter lists apply to the
27183 declarator, issue an error message. Returns TRUE if all went well,
27184 and FALSE otherwise. */
27187 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
27188 cp_declarator
*declarator
,
27189 location_t declarator_location
)
27191 switch (declarator
->kind
)
27195 unsigned num_templates
= 0;
27196 tree scope
= declarator
->u
.id
.qualifying_scope
;
27197 bool template_id_p
= false;
27200 num_templates
= num_template_headers_for_class (scope
);
27201 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
27202 == TEMPLATE_ID_EXPR
)
27204 /* If the DECLARATOR has the form `X<y>' then it uses one
27205 additional level of template parameters. */
27207 template_id_p
= true;
27210 return cp_parser_check_template_parameters
27211 (parser
, num_templates
, template_id_p
, declarator_location
,
27218 case cdk_reference
:
27220 return (cp_parser_check_declarator_template_parameters
27221 (parser
, declarator
->declarator
, declarator_location
));
27228 gcc_unreachable ();
27233 /* NUM_TEMPLATES were used in the current declaration. If that is
27234 invalid, return FALSE and issue an error messages. Otherwise,
27235 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27236 declarator and we can print more accurate diagnostics. */
27239 cp_parser_check_template_parameters (cp_parser
* parser
,
27240 unsigned num_templates
,
27241 bool template_id_p
,
27242 location_t location
,
27243 cp_declarator
*declarator
)
27245 /* If there are the same number of template classes and parameter
27246 lists, that's OK. */
27247 if (parser
->num_template_parameter_lists
== num_templates
)
27249 /* If there are more, but only one more, and the name ends in an identifier,
27250 then we are declaring a primary template. That's OK too. */
27252 && parser
->num_template_parameter_lists
== num_templates
+ 1)
27254 /* If there are more template classes than parameter lists, we have
27257 template <class T> void S<T>::R<T>::f (); */
27258 if (parser
->num_template_parameter_lists
< num_templates
)
27260 if (declarator
&& !current_function_decl
)
27261 error_at (location
, "specializing member %<%T::%E%> "
27262 "requires %<template<>%> syntax",
27263 declarator
->u
.id
.qualifying_scope
,
27264 declarator
->u
.id
.unqualified_name
);
27265 else if (declarator
)
27266 error_at (location
, "invalid declaration of %<%T::%E%>",
27267 declarator
->u
.id
.qualifying_scope
,
27268 declarator
->u
.id
.unqualified_name
);
27270 error_at (location
, "too few template-parameter-lists");
27273 /* Otherwise, there are too many template parameter lists. We have
27276 template <class T> template <class U> void S::f(); */
27277 error_at (location
, "too many template-parameter-lists");
27281 /* Parse an optional `::' token indicating that the following name is
27282 from the global namespace. If so, PARSER->SCOPE is set to the
27283 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27284 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27285 Returns the new value of PARSER->SCOPE, if the `::' token is
27286 present, and NULL_TREE otherwise. */
27289 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
27293 /* Peek at the next token. */
27294 token
= cp_lexer_peek_token (parser
->lexer
);
27295 /* If we're looking at a `::' token then we're starting from the
27296 global namespace, not our current location. */
27297 if (token
->type
== CPP_SCOPE
)
27299 /* Consume the `::' token. */
27300 cp_lexer_consume_token (parser
->lexer
);
27301 /* Set the SCOPE so that we know where to start the lookup. */
27302 parser
->scope
= global_namespace
;
27303 parser
->qualifying_scope
= global_namespace
;
27304 parser
->object_scope
= NULL_TREE
;
27306 return parser
->scope
;
27308 else if (!current_scope_valid_p
)
27310 parser
->scope
= NULL_TREE
;
27311 parser
->qualifying_scope
= NULL_TREE
;
27312 parser
->object_scope
= NULL_TREE
;
27318 /* Returns TRUE if the upcoming token sequence is the start of a
27319 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27320 declarator is preceded by the `friend' specifier. */
27323 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
27325 bool constructor_p
;
27326 bool outside_class_specifier_p
;
27327 tree nested_name_specifier
;
27328 cp_token
*next_token
;
27330 /* The common case is that this is not a constructor declarator, so
27331 try to avoid doing lots of work if at all possible. It's not
27332 valid declare a constructor at function scope. */
27333 if (parser
->in_function_body
)
27335 /* And only certain tokens can begin a constructor declarator. */
27336 next_token
= cp_lexer_peek_token (parser
->lexer
);
27337 if (next_token
->type
!= CPP_NAME
27338 && next_token
->type
!= CPP_SCOPE
27339 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
27340 && next_token
->type
!= CPP_TEMPLATE_ID
)
27343 /* Parse tentatively; we are going to roll back all of the tokens
27345 cp_parser_parse_tentatively (parser
);
27346 /* Assume that we are looking at a constructor declarator. */
27347 constructor_p
= true;
27349 /* Look for the optional `::' operator. */
27350 cp_parser_global_scope_opt (parser
,
27351 /*current_scope_valid_p=*/false);
27352 /* Look for the nested-name-specifier. */
27353 nested_name_specifier
27354 = (cp_parser_nested_name_specifier_opt (parser
,
27355 /*typename_keyword_p=*/false,
27356 /*check_dependency_p=*/false,
27358 /*is_declaration=*/false));
27360 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27361 if (nested_name_specifier
27362 && TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
27364 tree s
= resolve_typename_type (nested_name_specifier
,
27365 /*only_current_p=*/false);
27366 if (TREE_CODE (s
) != TYPENAME_TYPE
)
27367 nested_name_specifier
= s
;
27370 outside_class_specifier_p
= (!at_class_scope_p ()
27371 || !TYPE_BEING_DEFINED (current_class_type
)
27374 /* Outside of a class-specifier, there must be a
27375 nested-name-specifier. Except in C++17 mode, where we
27376 might be declaring a guiding declaration. */
27377 if (!nested_name_specifier
&& outside_class_specifier_p
27378 && cxx_dialect
< cxx17
)
27379 constructor_p
= false;
27380 else if (nested_name_specifier
== error_mark_node
)
27381 constructor_p
= false;
27383 /* If we have a class scope, this is easy; DR 147 says that S::S always
27384 names the constructor, and no other qualified name could. */
27385 if (constructor_p
&& nested_name_specifier
27386 && CLASS_TYPE_P (nested_name_specifier
))
27388 tree id
= cp_parser_unqualified_id (parser
,
27389 /*template_keyword_p=*/false,
27390 /*check_dependency_p=*/false,
27391 /*declarator_p=*/true,
27392 /*optional_p=*/false);
27393 if (is_overloaded_fn (id
))
27394 id
= DECL_NAME (get_first_fn (id
));
27395 if (!constructor_name_p (id
, nested_name_specifier
))
27396 constructor_p
= false;
27398 /* If we still think that this might be a constructor-declarator,
27399 look for a class-name. */
27400 else if (constructor_p
)
27404 template <typename T> struct S {
27408 we must recognize that the nested `S' names a class. */
27409 if (cxx_dialect
>= cxx17
)
27410 cp_parser_parse_tentatively (parser
);
27413 type_decl
= cp_parser_class_name (parser
,
27414 /*typename_keyword_p=*/false,
27415 /*template_keyword_p=*/false,
27417 /*check_dependency_p=*/false,
27418 /*class_head_p=*/false,
27419 /*is_declaration=*/false);
27421 if (cxx_dialect
>= cxx17
27422 && !cp_parser_parse_definitely (parser
))
27424 type_decl
= NULL_TREE
;
27425 tree tmpl
= cp_parser_template_name (parser
,
27426 /*template_keyword*/false,
27427 /*check_dependency_p*/false,
27428 /*is_declaration*/false,
27430 /*is_identifier*/NULL
);
27431 if (DECL_CLASS_TEMPLATE_P (tmpl
)
27432 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
27433 /* It's a deduction guide, return true. */;
27435 cp_parser_simulate_error (parser
);
27438 /* If there was no class-name, then this is not a constructor.
27439 Otherwise, if we are in a class-specifier and we aren't
27440 handling a friend declaration, check that its type matches
27441 current_class_type (c++/38313). Note: error_mark_node
27442 is left alone for error recovery purposes. */
27443 constructor_p
= (!cp_parser_error_occurred (parser
)
27444 && (outside_class_specifier_p
27445 || type_decl
== NULL_TREE
27446 || type_decl
== error_mark_node
27447 || same_type_p (current_class_type
,
27448 TREE_TYPE (type_decl
))));
27450 /* If we're still considering a constructor, we have to see a `(',
27451 to begin the parameter-declaration-clause, followed by either a
27452 `)', an `...', or a decl-specifier. We need to check for a
27453 type-specifier to avoid being fooled into thinking that:
27457 is a constructor. (It is actually a function named `f' that
27458 takes one parameter (of type `int') and returns a value of type
27461 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27462 constructor_p
= false;
27465 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
27466 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
27467 /* A parameter declaration begins with a decl-specifier,
27468 which is either the "attribute" keyword, a storage class
27469 specifier, or (usually) a type-specifier. */
27470 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
27473 tree pushed_scope
= NULL_TREE
;
27474 unsigned saved_num_template_parameter_lists
;
27476 /* Names appearing in the type-specifier should be looked up
27477 in the scope of the class. */
27478 if (current_class_type
)
27480 else if (type_decl
)
27482 type
= TREE_TYPE (type_decl
);
27483 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27485 type
= resolve_typename_type (type
,
27486 /*only_current_p=*/false);
27487 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27489 cp_parser_abort_tentative_parse (parser
);
27493 pushed_scope
= push_scope (type
);
27496 /* Inside the constructor parameter list, surrounding
27497 template-parameter-lists do not apply. */
27498 saved_num_template_parameter_lists
27499 = parser
->num_template_parameter_lists
;
27500 parser
->num_template_parameter_lists
= 0;
27502 /* Look for the type-specifier. */
27503 cp_parser_type_specifier (parser
,
27504 CP_PARSER_FLAGS_NONE
,
27505 /*decl_specs=*/NULL
,
27506 /*is_declarator=*/true,
27507 /*declares_class_or_enum=*/NULL
,
27508 /*is_cv_qualifier=*/NULL
);
27510 parser
->num_template_parameter_lists
27511 = saved_num_template_parameter_lists
;
27513 /* Leave the scope of the class. */
27515 pop_scope (pushed_scope
);
27517 constructor_p
= !cp_parser_error_occurred (parser
);
27521 /* We did not really want to consume any tokens. */
27522 cp_parser_abort_tentative_parse (parser
);
27524 return constructor_p
;
27527 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27528 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27529 they must be performed once we are in the scope of the function.
27531 Returns the function defined. */
27534 cp_parser_function_definition_from_specifiers_and_declarator
27535 (cp_parser
* parser
,
27536 cp_decl_specifier_seq
*decl_specifiers
,
27538 const cp_declarator
*declarator
)
27543 /* Begin the function-definition. */
27544 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
27546 /* The things we're about to see are not directly qualified by any
27547 template headers we've seen thus far. */
27548 reset_specialization ();
27550 /* If there were names looked up in the decl-specifier-seq that we
27551 did not check, check them now. We must wait until we are in the
27552 scope of the function to perform the checks, since the function
27553 might be a friend. */
27554 perform_deferred_access_checks (tf_warning_or_error
);
27558 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
27559 parser
->omp_declare_simd
= NULL
;
27560 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
27561 parser
->oacc_routine
= NULL
;
27566 /* Skip the entire function. */
27567 cp_parser_skip_to_end_of_block_or_statement (parser
);
27568 fn
= error_mark_node
;
27570 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
27572 /* Seen already, skip it. An error message has already been output. */
27573 cp_parser_skip_to_end_of_block_or_statement (parser
);
27574 fn
= current_function_decl
;
27575 current_function_decl
= NULL_TREE
;
27576 /* If this is a function from a class, pop the nested class. */
27577 if (current_class_name
)
27578 pop_nested_class ();
27583 if (DECL_DECLARED_INLINE_P (current_function_decl
))
27584 tv
= TV_PARSE_INLINE
;
27586 tv
= TV_PARSE_FUNC
;
27588 fn
= cp_parser_function_definition_after_declarator (parser
,
27589 /*inline_p=*/false);
27596 /* Parse the part of a function-definition that follows the
27597 declarator. INLINE_P is TRUE iff this function is an inline
27598 function defined within a class-specifier.
27600 Returns the function defined. */
27603 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
27607 bool saved_in_unbraced_linkage_specification_p
;
27608 bool saved_in_function_body
;
27609 unsigned saved_num_template_parameter_lists
;
27611 bool fully_implicit_function_template_p
27612 = parser
->fully_implicit_function_template_p
;
27613 parser
->fully_implicit_function_template_p
= false;
27614 tree implicit_template_parms
27615 = parser
->implicit_template_parms
;
27616 parser
->implicit_template_parms
= 0;
27617 cp_binding_level
* implicit_template_scope
27618 = parser
->implicit_template_scope
;
27619 parser
->implicit_template_scope
= 0;
27621 saved_in_function_body
= parser
->in_function_body
;
27622 parser
->in_function_body
= true;
27623 /* If the next token is `return', then the code may be trying to
27624 make use of the "named return value" extension that G++ used to
27626 token
= cp_lexer_peek_token (parser
->lexer
);
27627 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
27629 /* Consume the `return' keyword. */
27630 cp_lexer_consume_token (parser
->lexer
);
27631 /* Look for the identifier that indicates what value is to be
27633 cp_parser_identifier (parser
);
27634 /* Issue an error message. */
27635 error_at (token
->location
,
27636 "named return values are no longer supported");
27637 /* Skip tokens until we reach the start of the function body. */
27640 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27641 if (token
->type
== CPP_OPEN_BRACE
27642 || token
->type
== CPP_EOF
27643 || token
->type
== CPP_PRAGMA_EOL
)
27645 cp_lexer_consume_token (parser
->lexer
);
27648 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27649 anything declared inside `f'. */
27650 saved_in_unbraced_linkage_specification_p
27651 = parser
->in_unbraced_linkage_specification_p
;
27652 parser
->in_unbraced_linkage_specification_p
= false;
27653 /* Inside the function, surrounding template-parameter-lists do not
27655 saved_num_template_parameter_lists
27656 = parser
->num_template_parameter_lists
;
27657 parser
->num_template_parameter_lists
= 0;
27659 /* If the next token is `try', `__transaction_atomic', or
27660 `__transaction_relaxed`, then we are looking at either function-try-block
27661 or function-transaction-block. Note that all of these include the
27663 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
27664 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
27665 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27666 RID_TRANSACTION_RELAXED
))
27667 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
27668 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27669 cp_parser_function_try_block (parser
);
27671 cp_parser_ctor_initializer_opt_and_function_body
27672 (parser
, /*in_function_try_block=*/false);
27674 /* Finish the function. */
27675 fn
= finish_function (inline_p
);
27676 /* Generate code for it, if necessary. */
27677 expand_or_defer_fn (fn
);
27678 /* Restore the saved values. */
27679 parser
->in_unbraced_linkage_specification_p
27680 = saved_in_unbraced_linkage_specification_p
;
27681 parser
->num_template_parameter_lists
27682 = saved_num_template_parameter_lists
;
27683 parser
->in_function_body
= saved_in_function_body
;
27685 parser
->fully_implicit_function_template_p
27686 = fully_implicit_function_template_p
;
27687 parser
->implicit_template_parms
27688 = implicit_template_parms
;
27689 parser
->implicit_template_scope
27690 = implicit_template_scope
;
27692 if (parser
->fully_implicit_function_template_p
)
27693 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27698 /* Parse a template-declaration body (following argument list). */
27701 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27702 tree parameter_list
,
27705 tree decl
= NULL_TREE
;
27706 bool friend_p
= false;
27708 /* We just processed one more parameter list. */
27709 ++parser
->num_template_parameter_lists
;
27711 /* Get the deferred access checks from the parameter list. These
27712 will be checked once we know what is being declared, as for a
27713 member template the checks must be performed in the scope of the
27714 class containing the member. */
27715 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27717 /* Tentatively parse for a new template parameter list, which can either be
27718 the template keyword or a template introduction. */
27719 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27721 else if (cxx_dialect
>= cxx11
27722 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27723 decl
= cp_parser_alias_declaration (parser
);
27726 /* There are no access checks when parsing a template, as we do not
27727 know if a specialization will be a friend. */
27728 push_deferring_access_checks (dk_no_check
);
27729 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27730 decl
= cp_parser_single_declaration (parser
,
27733 /*explicit_specialization_p=*/false,
27735 pop_deferring_access_checks ();
27737 /* If this is a member template declaration, let the front
27739 if (member_p
&& !friend_p
&& decl
)
27741 if (TREE_CODE (decl
) == TYPE_DECL
)
27742 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27744 decl
= finish_member_template_decl (decl
);
27746 else if (friend_p
&& decl
27747 && DECL_DECLARES_TYPE_P (decl
))
27748 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27749 /*complain=*/true);
27751 /* We are done with the current parameter list. */
27752 --parser
->num_template_parameter_lists
;
27754 pop_deferring_access_checks ();
27757 finish_template_decl (parameter_list
);
27759 /* Check the template arguments for a literal operator template. */
27761 && DECL_DECLARES_FUNCTION_P (decl
)
27762 && UDLIT_OPER_P (DECL_NAME (decl
)))
27765 if (parameter_list
== NULL_TREE
)
27769 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27770 if (num_parms
== 1)
27772 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27773 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27774 if (CLASS_TYPE_P (TREE_TYPE (parm
)))
27775 /* OK, C++20 string literal operator template. We don't need
27776 to warn in lower dialects here because we will have already
27777 warned about the template parameter. */;
27778 else if (TREE_TYPE (parm
) != char_type_node
27779 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27782 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27784 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27785 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27786 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27787 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27788 if (parm
== error_mark_node
27789 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27790 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27793 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27794 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
27795 "ISO C++ did not adopt string literal operator templa"
27796 "tes taking an argument pack of characters");
27803 if (cxx_dialect
> cxx17
)
27804 error ("literal operator template %qD has invalid parameter list;"
27805 " Expected non-type template parameter pack <char...> "
27806 " or single non-type parameter of class type",
27809 error ("literal operator template %qD has invalid parameter list."
27810 " Expected non-type template parameter pack <char...>",
27815 /* Register member declarations. */
27816 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27817 finish_member_declaration (decl
);
27818 /* If DECL is a function template, we must return to parse it later.
27819 (Even though there is no definition, there might be default
27820 arguments that need handling.) */
27821 if (member_p
&& decl
27822 && DECL_DECLARES_FUNCTION_P (decl
))
27823 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27826 /* Parse a template introduction header for a template-declaration. Returns
27827 false if tentative parse fails. */
27830 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27832 cp_parser_parse_tentatively (parser
);
27834 tree saved_scope
= parser
->scope
;
27835 tree saved_object_scope
= parser
->object_scope
;
27836 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27838 /* Look for the optional `::' operator. */
27839 cp_parser_global_scope_opt (parser
,
27840 /*current_scope_valid_p=*/false);
27841 /* Look for the nested-name-specifier. */
27842 cp_parser_nested_name_specifier_opt (parser
,
27843 /*typename_keyword_p=*/false,
27844 /*check_dependency_p=*/true,
27846 /*is_declaration=*/false);
27848 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27849 tree concept_name
= cp_parser_identifier (parser
);
27851 /* Look up the concept for which we will be matching
27852 template parameters. */
27853 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27855 parser
->scope
= saved_scope
;
27856 parser
->object_scope
= saved_object_scope
;
27857 parser
->qualifying_scope
= saved_qualifying_scope
;
27859 if (concept_name
== error_mark_node
)
27860 cp_parser_simulate_error (parser
);
27862 /* Look for opening brace for introduction. */
27863 matching_braces braces
;
27864 braces
.require_open (parser
);
27866 if (!cp_parser_parse_definitely (parser
))
27869 push_deferring_access_checks (dk_deferred
);
27871 /* Build vector of placeholder parameters and grab
27872 matching identifiers. */
27873 tree introduction_list
= cp_parser_introduction_list (parser
);
27875 /* Look for closing brace for introduction. */
27876 if (!braces
.require_close (parser
))
27879 /* The introduction-list shall not be empty. */
27880 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27883 /* In cp_parser_introduction_list we have already issued an error. */
27887 if (tmpl_decl
== error_mark_node
)
27889 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27894 /* Build and associate the constraint. */
27895 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27896 if (parms
&& parms
!= error_mark_node
)
27898 cp_parser_template_declaration_after_parameters (parser
, parms
,
27903 error_at (token
->location
, "no matching concept for template-introduction");
27907 /* Parse a normal template-declaration following the template keyword. */
27910 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27912 tree parameter_list
;
27913 bool need_lang_pop
;
27914 location_t location
= input_location
;
27916 /* Look for the `<' token. */
27917 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27919 if (at_class_scope_p () && current_function_decl
)
27921 /* 14.5.2.2 [temp.mem]
27923 A local class shall not have member templates. */
27924 error_at (location
,
27925 "invalid declaration of member template in local class");
27926 cp_parser_skip_to_end_of_block_or_statement (parser
);
27931 A template ... shall not have C linkage. */
27932 if (current_lang_name
== lang_name_c
)
27934 error_at (location
, "template with C linkage");
27935 maybe_show_extern_c_location ();
27936 /* Give it C++ linkage to avoid confusing other parts of the
27938 push_lang_context (lang_name_cplusplus
);
27939 need_lang_pop
= true;
27942 need_lang_pop
= false;
27944 /* We cannot perform access checks on the template parameter
27945 declarations until we know what is being declared, just as we
27946 cannot check the decl-specifier list. */
27947 push_deferring_access_checks (dk_deferred
);
27949 /* If the next token is `>', then we have an invalid
27950 specialization. Rather than complain about an invalid template
27951 parameter, issue an error message here. */
27952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27954 cp_parser_error (parser
, "invalid explicit specialization");
27955 begin_specialization ();
27956 parameter_list
= NULL_TREE
;
27960 /* Parse the template parameters. */
27961 parameter_list
= cp_parser_template_parameter_list (parser
);
27964 /* Look for the `>'. */
27965 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27967 /* Manage template requirements */
27970 tree reqs
= get_shorthand_constraints (current_template_parms
);
27971 if (tree r
= cp_parser_requires_clause_opt (parser
))
27972 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27973 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27976 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27979 /* For the erroneous case of a template with C linkage, we pushed an
27980 implicit C++ linkage scope; exit that scope now. */
27982 pop_lang_context ();
27985 /* Parse a template-declaration, assuming that the `export' (and
27986 `extern') keywords, if present, has already been scanned. MEMBER_P
27987 is as for cp_parser_template_declaration. */
27990 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27992 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27994 cp_lexer_consume_token (parser
->lexer
);
27995 cp_parser_explicit_template_declaration (parser
, member_p
);
27998 else if (flag_concepts
)
27999 return cp_parser_template_introduction (parser
, member_p
);
28004 /* Perform the deferred access checks from a template-parameter-list.
28005 CHECKS is a TREE_LIST of access checks, as returned by
28006 get_deferred_access_checks. */
28009 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
28011 ++processing_template_parmlist
;
28012 perform_access_checks (checks
, tf_warning_or_error
);
28013 --processing_template_parmlist
;
28016 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28017 `function-definition' sequence that follows a template header.
28018 If MEMBER_P is true, this declaration appears in a class scope.
28020 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28021 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28024 cp_parser_single_declaration (cp_parser
* parser
,
28025 vec
<deferred_access_check
, va_gc
> *checks
,
28027 bool explicit_specialization_p
,
28030 int declares_class_or_enum
;
28031 tree decl
= NULL_TREE
;
28032 cp_decl_specifier_seq decl_specifiers
;
28033 bool function_definition_p
= false;
28034 cp_token
*decl_spec_token_start
;
28036 /* This function is only used when processing a template
28038 gcc_assert (innermost_scope_kind () == sk_template_parms
28039 || innermost_scope_kind () == sk_template_spec
);
28041 /* Defer access checks until we know what is being declared. */
28042 push_deferring_access_checks (dk_deferred
);
28044 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28046 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
28047 cp_parser_decl_specifier_seq (parser
,
28048 (CP_PARSER_FLAGS_OPTIONAL
28049 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
28051 &declares_class_or_enum
);
28053 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
28055 /* There are no template typedefs. */
28056 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
28058 error_at (decl_spec_token_start
->location
,
28059 "template declaration of %<typedef%>");
28060 decl
= error_mark_node
;
28063 /* Gather up the access checks that occurred the
28064 decl-specifier-seq. */
28065 stop_deferring_access_checks ();
28067 /* Check for the declaration of a template class. */
28068 if (declares_class_or_enum
)
28070 if (cp_parser_declares_only_class_p (parser
)
28071 || (declares_class_or_enum
& 2))
28073 // If this is a declaration, but not a definition, associate
28074 // any constraints with the type declaration. Constraints
28075 // are associated with definitions in cp_parser_class_specifier.
28076 if (declares_class_or_enum
== 1)
28077 associate_classtype_constraints (decl_specifiers
.type
);
28079 decl
= shadow_tag (&decl_specifiers
);
28084 friend template <typename T> struct A<T>::B;
28087 A<T>::B will be represented by a TYPENAME_TYPE, and
28088 therefore not recognized by shadow_tag. */
28089 if (friend_p
&& *friend_p
28091 && decl_specifiers
.type
28092 && TYPE_P (decl_specifiers
.type
))
28093 decl
= decl_specifiers
.type
;
28095 if (decl
&& decl
!= error_mark_node
)
28096 decl
= TYPE_NAME (decl
);
28098 decl
= error_mark_node
;
28100 /* Perform access checks for template parameters. */
28101 cp_parser_perform_template_parameter_access_checks (checks
);
28103 /* Give a helpful diagnostic for
28104 template <class T> struct A { } a;
28105 if we aren't already recovering from an error. */
28106 if (!cp_parser_declares_only_class_p (parser
)
28109 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
28110 "a class template declaration must not declare "
28112 cp_parser_skip_to_end_of_block_or_statement (parser
);
28118 /* Complain about missing 'typename' or other invalid type names. */
28119 if (!decl_specifiers
.any_type_specifiers_p
28120 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
28122 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28123 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28124 the rest of this declaration. */
28125 decl
= error_mark_node
;
28129 /* If it's not a template class, try for a template function. If
28130 the next token is a `;', then this declaration does not declare
28131 anything. But, if there were errors in the decl-specifiers, then
28132 the error might well have come from an attempted class-specifier.
28133 In that case, there's no need to warn about a missing declarator. */
28135 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
28136 || decl_specifiers
.type
!= error_mark_node
))
28138 decl
= cp_parser_init_declarator (parser
,
28139 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
28142 /*function_definition_allowed_p=*/true,
28144 declares_class_or_enum
,
28145 &function_definition_p
,
28148 /* 7.1.1-1 [dcl.stc]
28150 A storage-class-specifier shall not be specified in an explicit
28151 specialization... */
28153 && explicit_specialization_p
28154 && decl_specifiers
.storage_class
!= sc_none
)
28156 error_at (decl_spec_token_start
->location
,
28157 "explicit template specialization cannot have a storage class");
28158 decl
= error_mark_node
;
28161 if (decl
&& VAR_P (decl
))
28162 check_template_variable (decl
);
28165 /* Look for a trailing `;' after the declaration. */
28166 if (!function_definition_p
28167 && (decl
== error_mark_node
28168 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
28169 cp_parser_skip_to_end_of_block_or_statement (parser
);
28172 pop_deferring_access_checks ();
28174 /* Clear any current qualification; whatever comes next is the start
28175 of something new. */
28176 parser
->scope
= NULL_TREE
;
28177 parser
->qualifying_scope
= NULL_TREE
;
28178 parser
->object_scope
= NULL_TREE
;
28183 /* Parse a cast-expression that is not the operand of a unary "&". */
28186 cp_parser_simple_cast_expression (cp_parser
*parser
)
28188 return cp_parser_cast_expression (parser
, /*address_p=*/false,
28189 /*cast_p=*/false, /*decltype*/false, NULL
);
28192 /* Parse a functional cast to TYPE. Returns an expression
28193 representing the cast. */
28196 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
28198 vec
<tree
, va_gc
> *vec
;
28199 tree expression_list
;
28203 location_t start_loc
= input_location
;
28206 type
= error_mark_node
;
28208 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28210 cp_lexer_set_source_position (parser
->lexer
);
28211 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28212 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
28213 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
28214 if (TREE_CODE (type
) == TYPE_DECL
)
28215 type
= TREE_TYPE (type
);
28217 cast
= finish_compound_literal (type
, expression_list
,
28218 tf_warning_or_error
, fcl_functional
);
28219 /* Create a location of the form:
28222 with caret == start at the start of the type name,
28223 finishing at the closing brace. */
28224 location_t finish_loc
28225 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28226 location_t combined_loc
= make_location (start_loc
, start_loc
,
28228 cast
.set_location (combined_loc
);
28233 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
28235 /*allow_expansion_p=*/true,
28236 /*non_constant_p=*/NULL
);
28238 expression_list
= error_mark_node
;
28241 expression_list
= build_tree_list_vec (vec
);
28242 release_tree_vector (vec
);
28245 cast
= build_functional_cast (type
, expression_list
,
28246 tf_warning_or_error
);
28247 /* [expr.const]/1: In an integral constant expression "only type
28248 conversions to integral or enumeration type can be used". */
28249 if (TREE_CODE (type
) == TYPE_DECL
)
28250 type
= TREE_TYPE (type
);
28251 if (cast
!= error_mark_node
28252 && !cast_valid_in_integral_constant_expression_p (type
)
28253 && cp_parser_non_integral_constant_expression (parser
,
28255 return error_mark_node
;
28257 /* Create a location of the form:
28260 with caret == start at the start of the type name,
28261 finishing at the closing paren. */
28262 location_t finish_loc
28263 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28264 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
28265 cast
.set_location (combined_loc
);
28269 /* Save the tokens that make up the body of a member function defined
28270 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28271 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28272 specifiers applied to the declaration. Returns the FUNCTION_DECL
28273 for the member function. */
28276 cp_parser_save_member_function_body (cp_parser
* parser
,
28277 cp_decl_specifier_seq
*decl_specifiers
,
28278 cp_declarator
*declarator
,
28284 bool function_try_block
= false;
28286 /* Create the FUNCTION_DECL. */
28287 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
28288 cp_finalize_omp_declare_simd (parser
, fn
);
28289 cp_finalize_oacc_routine (parser
, fn
, true);
28290 /* If something went badly wrong, bail out now. */
28291 if (fn
== error_mark_node
)
28293 /* If there's a function-body, skip it. */
28294 if (cp_parser_token_starts_function_definition_p
28295 (cp_lexer_peek_token (parser
->lexer
)))
28296 cp_parser_skip_to_end_of_block_or_statement (parser
);
28297 return error_mark_node
;
28300 /* Remember it, if there default args to post process. */
28301 cp_parser_save_default_args (parser
, fn
);
28303 /* Save away the tokens that make up the body of the
28305 first
= parser
->lexer
->next_token
;
28307 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
28308 cp_lexer_consume_token (parser
->lexer
);
28309 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
28310 RID_TRANSACTION_ATOMIC
))
28312 cp_lexer_consume_token (parser
->lexer
);
28313 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28314 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
28315 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
28316 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
28317 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
28318 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
28319 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
28321 cp_lexer_consume_token (parser
->lexer
);
28322 cp_lexer_consume_token (parser
->lexer
);
28323 cp_lexer_consume_token (parser
->lexer
);
28324 cp_lexer_consume_token (parser
->lexer
);
28325 cp_lexer_consume_token (parser
->lexer
);
28328 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
28329 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
28331 cp_lexer_consume_token (parser
->lexer
);
28332 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28337 /* Handle function try blocks. */
28338 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
28340 cp_lexer_consume_token (parser
->lexer
);
28341 function_try_block
= true;
28343 /* We can have braced-init-list mem-initializers before the fn body. */
28344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
28346 cp_lexer_consume_token (parser
->lexer
);
28347 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
28349 /* cache_group will stop after an un-nested { } pair, too. */
28350 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28353 /* variadic mem-inits have ... after the ')'. */
28354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28355 cp_lexer_consume_token (parser
->lexer
);
28358 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28359 /* Handle function try blocks. */
28360 if (function_try_block
)
28361 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
28362 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28363 last
= parser
->lexer
->next_token
;
28365 /* Save away the inline definition; we will process it when the
28366 class is complete. */
28367 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
28368 DECL_PENDING_INLINE_P (fn
) = 1;
28370 /* We need to know that this was defined in the class, so that
28371 friend templates are handled correctly. */
28372 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
28374 /* Add FN to the queue of functions to be parsed later. */
28375 vec_safe_push (unparsed_funs_with_definitions
, fn
);
28380 /* Save the tokens that make up the in-class initializer for a non-static
28381 data member. Returns a DEFAULT_ARG. */
28384 cp_parser_save_nsdmi (cp_parser
* parser
)
28386 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
28389 /* Parse a template-argument-list, as well as the trailing ">" (but
28390 not the opening "<"). See cp_parser_template_argument_list for the
28394 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
28398 tree saved_qualifying_scope
;
28399 tree saved_object_scope
;
28400 bool saved_greater_than_is_operator_p
;
28404 When parsing a template-id, the first non-nested `>' is taken as
28405 the end of the template-argument-list rather than a greater-than
28407 saved_greater_than_is_operator_p
28408 = parser
->greater_than_is_operator_p
;
28409 parser
->greater_than_is_operator_p
= false;
28410 /* Parsing the argument list may modify SCOPE, so we save it
28412 saved_scope
= parser
->scope
;
28413 saved_qualifying_scope
= parser
->qualifying_scope
;
28414 saved_object_scope
= parser
->object_scope
;
28415 /* We need to evaluate the template arguments, even though this
28416 template-id may be nested within a "sizeof". */
28418 /* Parse the template-argument-list itself. */
28419 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
28420 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28421 arguments
= NULL_TREE
;
28423 arguments
= cp_parser_template_argument_list (parser
);
28424 /* Look for the `>' that ends the template-argument-list. If we find
28425 a '>>' instead, it's probably just a typo. */
28426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28428 if (cxx_dialect
!= cxx98
)
28430 /* In C++0x, a `>>' in a template argument list or cast
28431 expression is considered to be two separate `>'
28432 tokens. So, change the current token to a `>', but don't
28433 consume it: it will be consumed later when the outer
28434 template argument list (or cast expression) is parsed.
28435 Note that this replacement of `>' for `>>' is necessary
28436 even if we are parsing tentatively: in the tentative
28437 case, after calling
28438 cp_parser_enclosed_template_argument_list we will always
28439 throw away all of the template arguments and the first
28440 closing `>', either because the template argument list
28441 was erroneous or because we are replacing those tokens
28442 with a CPP_TEMPLATE_ID token. The second `>' (which will
28443 not have been thrown away) is needed either to close an
28444 outer template argument list or to complete a new-style
28446 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28447 token
->type
= CPP_GREATER
;
28449 else if (!saved_greater_than_is_operator_p
)
28451 /* If we're in a nested template argument list, the '>>' has
28452 to be a typo for '> >'. We emit the error message, but we
28453 continue parsing and we push a '>' as next token, so that
28454 the argument list will be parsed correctly. Note that the
28455 global source location is still on the token before the
28456 '>>', so we need to say explicitly where we want it. */
28457 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28458 gcc_rich_location
richloc (token
->location
);
28459 richloc
.add_fixit_replace ("> >");
28460 error_at (&richloc
, "%<>>%> should be %<> >%> "
28461 "within a nested template argument list");
28463 token
->type
= CPP_GREATER
;
28467 /* If this is not a nested template argument list, the '>>'
28468 is a typo for '>'. Emit an error message and continue.
28469 Same deal about the token location, but here we can get it
28470 right by consuming the '>>' before issuing the diagnostic. */
28471 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
28472 error_at (token
->location
,
28473 "spurious %<>>%>, use %<>%> to terminate "
28474 "a template argument list");
28478 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28479 /* The `>' token might be a greater-than operator again now. */
28480 parser
->greater_than_is_operator_p
28481 = saved_greater_than_is_operator_p
;
28482 /* Restore the SAVED_SCOPE. */
28483 parser
->scope
= saved_scope
;
28484 parser
->qualifying_scope
= saved_qualifying_scope
;
28485 parser
->object_scope
= saved_object_scope
;
28490 /* MEMBER_FUNCTION is a member function, or a friend. If default
28491 arguments, or the body of the function have not yet been parsed,
28495 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
28497 timevar_push (TV_PARSE_INMETH
);
28498 /* If this member is a template, get the underlying
28500 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
28501 member_function
= DECL_TEMPLATE_RESULT (member_function
);
28503 /* There should not be any class definitions in progress at this
28504 point; the bodies of members are only parsed outside of all class
28506 gcc_assert (parser
->num_classes_being_defined
== 0);
28507 /* While we're parsing the member functions we might encounter more
28508 classes. We want to handle them right away, but we don't want
28509 them getting mixed up with functions that are currently in the
28511 push_unparsed_function_queues (parser
);
28513 /* Make sure that any template parameters are in scope. */
28514 maybe_begin_member_template_processing (member_function
);
28516 /* If the body of the function has not yet been parsed, parse it
28518 if (DECL_PENDING_INLINE_P (member_function
))
28520 tree function_scope
;
28521 cp_token_cache
*tokens
;
28523 /* The function is no longer pending; we are processing it. */
28524 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
28525 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
28526 DECL_PENDING_INLINE_P (member_function
) = 0;
28528 /* If this is a local class, enter the scope of the containing
28530 function_scope
= current_function_decl
;
28531 if (function_scope
)
28532 push_function_context ();
28534 /* Push the body of the function onto the lexer stack. */
28535 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28537 /* Let the front end know that we going to be defining this
28539 start_preparsed_function (member_function
, NULL_TREE
,
28540 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
28542 /* Don't do access checking if it is a templated function. */
28543 if (processing_template_decl
)
28544 push_deferring_access_checks (dk_no_check
);
28546 /* #pragma omp declare reduction needs special parsing. */
28547 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
28549 parser
->lexer
->in_pragma
= true;
28550 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
28551 finish_function (/*inline_p=*/true);
28552 cp_check_omp_declare_reduction (member_function
);
28555 /* Now, parse the body of the function. */
28556 cp_parser_function_definition_after_declarator (parser
,
28557 /*inline_p=*/true);
28559 if (processing_template_decl
)
28560 pop_deferring_access_checks ();
28562 /* Leave the scope of the containing function. */
28563 if (function_scope
)
28564 pop_function_context ();
28565 cp_parser_pop_lexer (parser
);
28568 /* Remove any template parameters from the symbol table. */
28569 maybe_end_member_template_processing ();
28571 /* Restore the queue. */
28572 pop_unparsed_function_queues (parser
);
28573 timevar_pop (TV_PARSE_INMETH
);
28576 /* If DECL contains any default args, remember it on the unparsed
28577 functions queue. */
28580 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
28584 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
28586 probe
= TREE_CHAIN (probe
))
28587 if (TREE_PURPOSE (probe
))
28589 cp_default_arg_entry entry
= {current_class_type
, decl
};
28590 vec_safe_push (unparsed_funs_with_default_args
, entry
);
28595 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28596 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28597 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28598 from the parameter-type-list. */
28601 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
28602 tree default_arg
, tree parmtype
)
28604 cp_token_cache
*tokens
;
28608 if (default_arg
== error_mark_node
)
28609 return error_mark_node
;
28611 /* Push the saved tokens for the default argument onto the parser's
28613 tokens
= DEFARG_TOKENS (default_arg
);
28614 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28616 start_lambda_scope (decl
);
28618 /* Parse the default argument. */
28619 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
28620 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
28621 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28623 finish_lambda_scope ();
28625 if (parsed_arg
== error_mark_node
)
28626 cp_parser_skip_to_end_of_statement (parser
);
28628 if (!processing_template_decl
)
28630 /* In a non-template class, check conversions now. In a template,
28631 we'll wait and instantiate these as needed. */
28632 if (TREE_CODE (decl
) == PARM_DECL
)
28633 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
28634 tf_warning_or_error
);
28635 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
28636 parsed_arg
= error_mark_node
;
28638 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
28641 /* If the token stream has not been completely used up, then
28642 there was extra junk after the end of the default
28644 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
28646 if (TREE_CODE (decl
) == PARM_DECL
)
28647 cp_parser_error (parser
, "expected %<,%>");
28649 cp_parser_error (parser
, "expected %<;%>");
28652 /* Revert to the main lexer. */
28653 cp_parser_pop_lexer (parser
);
28658 /* FIELD is a non-static data member with an initializer which we saved for
28659 later; parse it now. */
28662 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
28666 maybe_begin_member_template_processing (field
);
28668 push_unparsed_function_queues (parser
);
28669 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
28670 DECL_INITIAL (field
),
28672 pop_unparsed_function_queues (parser
);
28674 maybe_end_member_template_processing ();
28676 DECL_INITIAL (field
) = def
;
28679 /* FN is a FUNCTION_DECL which may contains a parameter with an
28680 unparsed DEFAULT_ARG. Parse the default args now. This function
28681 assumes that the current scope is the scope in which the default
28682 argument should be processed. */
28685 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
28687 unsigned char saved_local_variables_forbidden_p
;
28688 tree parm
, parmdecl
;
28690 /* While we're parsing the default args, we might (due to the
28691 statement expression extension) encounter more classes. We want
28692 to handle them right away, but we don't want them getting mixed
28693 up with default args that are currently in the queue. */
28694 push_unparsed_function_queues (parser
);
28696 /* Local variable names (and the `this' keyword) may not appear
28697 in a default argument. */
28698 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28699 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
28701 push_defarg_context (fn
);
28703 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28704 parmdecl
= DECL_ARGUMENTS (fn
);
28705 parm
&& parm
!= void_list_node
;
28706 parm
= TREE_CHAIN (parm
),
28707 parmdecl
= DECL_CHAIN (parmdecl
))
28709 tree default_arg
= TREE_PURPOSE (parm
);
28711 vec
<tree
, va_gc
> *insts
;
28718 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28719 /* This can happen for a friend declaration for a function
28720 already declared with default arguments. */
28724 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28726 TREE_VALUE (parm
));
28727 TREE_PURPOSE (parm
) = parsed_arg
;
28729 /* Update any instantiations we've already created. */
28730 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28731 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28732 TREE_PURPOSE (copy
) = parsed_arg
;
28735 pop_defarg_context ();
28737 /* Make sure no default arg is missing. */
28738 check_default_args (fn
);
28740 /* Restore the state of local_variables_forbidden_p. */
28741 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28743 /* Restore the queue. */
28744 pop_unparsed_function_queues (parser
);
28747 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28749 sizeof ... ( identifier )
28751 where the 'sizeof' token has already been consumed. */
28754 cp_parser_sizeof_pack (cp_parser
*parser
)
28756 /* Consume the `...'. */
28757 cp_lexer_consume_token (parser
->lexer
);
28758 maybe_warn_variadic_templates ();
28760 matching_parens parens
;
28761 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28763 parens
.consume_open (parser
);
28765 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28766 "%<sizeof...%> argument must be surrounded by parentheses");
28768 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28769 tree name
= cp_parser_identifier (parser
);
28770 if (name
== error_mark_node
)
28771 return error_mark_node
;
28772 /* The name is not qualified. */
28773 parser
->scope
= NULL_TREE
;
28774 parser
->qualifying_scope
= NULL_TREE
;
28775 parser
->object_scope
= NULL_TREE
;
28776 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28777 if (expr
== error_mark_node
)
28778 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28780 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28781 expr
= TREE_TYPE (expr
);
28782 else if (TREE_CODE (expr
) == CONST_DECL
)
28783 expr
= DECL_INITIAL (expr
);
28784 expr
= make_pack_expansion (expr
);
28785 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28788 parens
.require_close (parser
);
28793 /* Parse the operand of `sizeof' (or a similar operator). Returns
28794 either a TYPE or an expression, depending on the form of the
28795 input. The KEYWORD indicates which kind of expression we have
28799 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28801 tree expr
= NULL_TREE
;
28802 const char *saved_message
;
28804 bool saved_integral_constant_expression_p
;
28805 bool saved_non_integral_constant_expression_p
;
28807 /* If it's a `...', then we are computing the length of a parameter
28809 if (keyword
== RID_SIZEOF
28810 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28811 return cp_parser_sizeof_pack (parser
);
28813 /* Types cannot be defined in a `sizeof' expression. Save away the
28815 saved_message
= parser
->type_definition_forbidden_message
;
28816 /* And create the new one. */
28817 tmp
= concat ("types may not be defined in %<",
28818 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28819 "%> expressions", NULL
);
28820 parser
->type_definition_forbidden_message
= tmp
;
28822 /* The restrictions on constant-expressions do not apply inside
28823 sizeof expressions. */
28824 saved_integral_constant_expression_p
28825 = parser
->integral_constant_expression_p
;
28826 saved_non_integral_constant_expression_p
28827 = parser
->non_integral_constant_expression_p
;
28828 parser
->integral_constant_expression_p
= false;
28830 /* Do not actually evaluate the expression. */
28831 ++cp_unevaluated_operand
;
28832 ++c_inhibit_evaluation_warnings
;
28833 /* If it's a `(', then we might be looking at the type-id
28835 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28837 tree type
= NULL_TREE
;
28839 /* We can't be sure yet whether we're looking at a type-id or an
28841 cp_parser_parse_tentatively (parser
);
28843 matching_parens parens
;
28844 parens
.consume_open (parser
);
28846 /* Note: as a GNU Extension, compound literals are considered
28847 postfix-expressions as they are in C99, so they are valid
28848 arguments to sizeof. See comment in cp_parser_cast_expression
28850 if (cp_parser_compound_literal_p (parser
))
28851 cp_parser_simulate_error (parser
);
28854 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28855 parser
->in_type_id_in_expr_p
= true;
28856 /* Look for the type-id. */
28857 type
= cp_parser_type_id (parser
);
28858 /* Look for the closing `)'. */
28859 parens
.require_close (parser
);
28860 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28863 /* If all went well, then we're done. */
28864 if (cp_parser_parse_definitely (parser
))
28868 /* If the type-id production did not work out, then we must be
28869 looking at the unary-expression production. */
28871 expr
= cp_parser_unary_expression (parser
);
28873 /* Go back to evaluating expressions. */
28874 --cp_unevaluated_operand
;
28875 --c_inhibit_evaluation_warnings
;
28877 /* Free the message we created. */
28879 /* And restore the old one. */
28880 parser
->type_definition_forbidden_message
= saved_message
;
28881 parser
->integral_constant_expression_p
28882 = saved_integral_constant_expression_p
;
28883 parser
->non_integral_constant_expression_p
28884 = saved_non_integral_constant_expression_p
;
28889 /* If the current declaration has no declarator, return true. */
28892 cp_parser_declares_only_class_p (cp_parser
*parser
)
28894 /* If the next token is a `;' or a `,' then there is no
28896 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28897 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28900 /* Update the DECL_SPECS to reflect the storage class indicated by
28904 cp_parser_set_storage_class (cp_parser
*parser
,
28905 cp_decl_specifier_seq
*decl_specs
,
28909 cp_storage_class storage_class
;
28911 if (parser
->in_unbraced_linkage_specification_p
)
28913 error_at (token
->location
, "invalid use of %qD in linkage specification",
28914 ridpointers
[keyword
]);
28917 else if (decl_specs
->storage_class
!= sc_none
)
28919 decl_specs
->conflicting_specifiers_p
= true;
28923 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28924 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28925 && decl_specs
->gnu_thread_keyword_p
)
28927 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28928 "%<__thread%> before %qD", ridpointers
[keyword
]);
28934 storage_class
= sc_auto
;
28937 storage_class
= sc_register
;
28940 storage_class
= sc_static
;
28943 storage_class
= sc_extern
;
28946 storage_class
= sc_mutable
;
28949 gcc_unreachable ();
28951 decl_specs
->storage_class
= storage_class
;
28952 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28954 /* A storage class specifier cannot be applied alongside a typedef
28955 specifier. If there is a typedef specifier present then set
28956 conflicting_specifiers_p which will trigger an error later
28957 on in grokdeclarator. */
28958 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28959 decl_specs
->conflicting_specifiers_p
= true;
28962 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28963 is true, the type is a class or enum definition. */
28966 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28969 bool type_definition_p
)
28971 decl_specs
->any_specifiers_p
= true;
28973 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
28974 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
28975 this is what happened. In system headers, we ignore these
28976 declarations so that G++ can work with system headers that are not
28978 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28979 && !type_definition_p
28980 && (type_spec
== boolean_type_node
28981 || type_spec
== char8_type_node
28982 || type_spec
== char16_type_node
28983 || type_spec
== char32_type_node
28984 || type_spec
== wchar_type_node
)
28985 && (decl_specs
->type
28986 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28987 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28988 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28989 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28991 decl_specs
->redefined_builtin_type
= type_spec
;
28992 set_and_check_decl_spec_loc (decl_specs
,
28993 ds_redefined_builtin_type_spec
,
28995 if (!decl_specs
->type
)
28997 decl_specs
->type
= type_spec
;
28998 decl_specs
->type_definition_p
= false;
28999 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
29002 else if (decl_specs
->type
)
29003 decl_specs
->multiple_types_p
= true;
29006 decl_specs
->type
= type_spec
;
29007 decl_specs
->type_definition_p
= type_definition_p
;
29008 decl_specs
->redefined_builtin_type
= NULL_TREE
;
29009 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
29013 /* True iff TOKEN is the GNU keyword __thread. */
29016 token_is__thread (cp_token
*token
)
29018 gcc_assert (token
->keyword
== RID_THREAD
);
29019 return id_equal (token
->u
.value
, "__thread");
29022 /* Set the location for a declarator specifier and check if it is
29025 DECL_SPECS is the sequence of declarator specifiers onto which to
29028 DS is the single declarator specifier to set which location is to
29029 be set onto the existing sequence of declarators.
29031 LOCATION is the location for the declarator specifier to
29035 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
29036 cp_decl_spec ds
, cp_token
*token
)
29038 gcc_assert (ds
< ds_last
);
29040 if (decl_specs
== NULL
)
29043 location_t location
= token
->location
;
29045 if (decl_specs
->locations
[ds
] == 0)
29047 decl_specs
->locations
[ds
] = location
;
29048 if (ds
== ds_thread
)
29049 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
29055 if (decl_specs
->locations
[ds_long_long
] != 0)
29056 error_at (location
,
29057 "%<long long long%> is too long for GCC");
29060 decl_specs
->locations
[ds_long_long
] = location
;
29061 pedwarn_cxx98 (location
,
29063 "ISO C++ 1998 does not support %<long long%>");
29066 else if (ds
== ds_thread
)
29068 bool gnu
= token_is__thread (token
);
29069 gcc_rich_location
richloc (location
);
29070 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
29072 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
29073 error_at (&richloc
,
29074 "both %<__thread%> and %<thread_local%> specified");
29078 richloc
.add_fixit_remove ();
29079 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
29084 static const char *const decl_spec_names
[] = {
29101 gcc_rich_location
richloc (location
);
29102 richloc
.add_fixit_remove ();
29103 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
29108 /* Return true iff the declarator specifier DS is present in the
29109 sequence of declarator specifiers DECL_SPECS. */
29112 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
29115 gcc_assert (ds
< ds_last
);
29117 if (decl_specs
== NULL
)
29120 return decl_specs
->locations
[ds
] != 0;
29123 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29124 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29127 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
29129 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
29132 /* Issue an error message indicating that TOKEN_DESC was expected.
29133 If KEYWORD is true, it indicated this function is called by
29134 cp_parser_require_keword and the required token can only be
29135 a indicated keyword.
29137 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29138 within any error as the location of an "opening" token matching
29139 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29140 RT_CLOSE_PAREN). */
29143 cp_parser_required_error (cp_parser
*parser
,
29144 required_token token_desc
,
29146 location_t matching_location
)
29148 if (cp_parser_simulate_error (parser
))
29151 const char *gmsgid
= NULL
;
29152 switch (token_desc
)
29155 gmsgid
= G_("expected %<new%>");
29158 gmsgid
= G_("expected %<delete%>");
29161 gmsgid
= G_("expected %<return%>");
29164 gmsgid
= G_("expected %<while%>");
29167 gmsgid
= G_("expected %<extern%>");
29169 case RT_STATIC_ASSERT
:
29170 gmsgid
= G_("expected %<static_assert%>");
29173 gmsgid
= G_("expected %<decltype%>");
29176 gmsgid
= G_("expected %<operator%>");
29179 gmsgid
= G_("expected %<class%>");
29182 gmsgid
= G_("expected %<template%>");
29185 gmsgid
= G_("expected %<namespace%>");
29188 gmsgid
= G_("expected %<using%>");
29191 gmsgid
= G_("expected %<asm%>");
29194 gmsgid
= G_("expected %<try%>");
29197 gmsgid
= G_("expected %<catch%>");
29200 gmsgid
= G_("expected %<throw%>");
29203 gmsgid
= G_("expected %<__label__%>");
29206 gmsgid
= G_("expected %<@try%>");
29208 case RT_AT_SYNCHRONIZED
:
29209 gmsgid
= G_("expected %<@synchronized%>");
29212 gmsgid
= G_("expected %<@throw%>");
29214 case RT_TRANSACTION_ATOMIC
:
29215 gmsgid
= G_("expected %<__transaction_atomic%>");
29217 case RT_TRANSACTION_RELAXED
:
29218 gmsgid
= G_("expected %<__transaction_relaxed%>");
29224 if (!gmsgid
&& !keyword
)
29226 switch (token_desc
)
29229 gmsgid
= G_("expected %<;%>");
29231 case RT_OPEN_PAREN
:
29232 gmsgid
= G_("expected %<(%>");
29234 case RT_CLOSE_BRACE
:
29235 gmsgid
= G_("expected %<}%>");
29237 case RT_OPEN_BRACE
:
29238 gmsgid
= G_("expected %<{%>");
29240 case RT_CLOSE_SQUARE
:
29241 gmsgid
= G_("expected %<]%>");
29243 case RT_OPEN_SQUARE
:
29244 gmsgid
= G_("expected %<[%>");
29247 gmsgid
= G_("expected %<,%>");
29250 gmsgid
= G_("expected %<::%>");
29253 gmsgid
= G_("expected %<<%>");
29256 gmsgid
= G_("expected %<>%>");
29259 gmsgid
= G_("expected %<=%>");
29262 gmsgid
= G_("expected %<...%>");
29265 gmsgid
= G_("expected %<*%>");
29268 gmsgid
= G_("expected %<~%>");
29271 gmsgid
= G_("expected %<:%>");
29273 case RT_COLON_SCOPE
:
29274 gmsgid
= G_("expected %<:%> or %<::%>");
29276 case RT_CLOSE_PAREN
:
29277 gmsgid
= G_("expected %<)%>");
29279 case RT_COMMA_CLOSE_PAREN
:
29280 gmsgid
= G_("expected %<,%> or %<)%>");
29282 case RT_PRAGMA_EOL
:
29283 gmsgid
= G_("expected end of line");
29286 gmsgid
= G_("expected identifier");
29289 gmsgid
= G_("expected selection-statement");
29292 gmsgid
= G_("expected iteration-statement");
29295 gmsgid
= G_("expected jump-statement");
29298 gmsgid
= G_("expected class-key");
29300 case RT_CLASS_TYPENAME_TEMPLATE
:
29301 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
29304 gcc_unreachable ();
29309 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
29313 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29314 issue an error message indicating that TOKEN_DESC was expected.
29316 Returns the token consumed, if the token had the appropriate type.
29317 Otherwise, returns NULL.
29319 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29320 within any error as the location of an "opening" token matching
29321 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29322 RT_CLOSE_PAREN). */
29325 cp_parser_require (cp_parser
* parser
,
29326 enum cpp_ttype type
,
29327 required_token token_desc
,
29328 location_t matching_location
)
29330 if (cp_lexer_next_token_is (parser
->lexer
, type
))
29331 return cp_lexer_consume_token (parser
->lexer
);
29334 /* Output the MESSAGE -- unless we're parsing tentatively. */
29335 if (!cp_parser_simulate_error (parser
))
29336 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
29337 matching_location
);
29342 /* An error message is produced if the next token is not '>'.
29343 All further tokens are skipped until the desired token is
29344 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29347 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
29349 /* Current level of '< ... >'. */
29350 unsigned level
= 0;
29351 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29352 unsigned nesting_depth
= 0;
29354 /* Are we ready, yet? If not, issue error message. */
29355 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
29358 /* Skip tokens until the desired token is found. */
29361 /* Peek at the next token. */
29362 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29365 if (!nesting_depth
)
29370 if (cxx_dialect
== cxx98
)
29371 /* C++0x views the `>>' operator as two `>' tokens, but
29374 else if (!nesting_depth
&& level
-- == 0)
29376 /* We've hit a `>>' where the first `>' closes the
29377 template argument list, and the second `>' is
29378 spurious. Just consume the `>>' and stop; we've
29379 already produced at least one error. */
29380 cp_lexer_consume_token (parser
->lexer
);
29383 /* Fall through for C++0x, so we handle the second `>' in
29385 gcc_fallthrough ();
29388 if (!nesting_depth
&& level
-- == 0)
29390 /* We've reached the token we want, consume it and stop. */
29391 cp_lexer_consume_token (parser
->lexer
);
29396 case CPP_OPEN_PAREN
:
29397 case CPP_OPEN_SQUARE
:
29401 case CPP_CLOSE_PAREN
:
29402 case CPP_CLOSE_SQUARE
:
29403 if (nesting_depth
-- == 0)
29408 case CPP_PRAGMA_EOL
:
29409 case CPP_SEMICOLON
:
29410 case CPP_OPEN_BRACE
:
29411 case CPP_CLOSE_BRACE
:
29412 /* The '>' was probably forgotten, don't look further. */
29419 /* Consume this token. */
29420 cp_lexer_consume_token (parser
->lexer
);
29424 /* If the next token is the indicated keyword, consume it. Otherwise,
29425 issue an error message indicating that TOKEN_DESC was expected.
29427 Returns the token consumed, if the token had the appropriate type.
29428 Otherwise, returns NULL. */
29431 cp_parser_require_keyword (cp_parser
* parser
,
29433 required_token token_desc
)
29435 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
29437 if (token
&& token
->keyword
!= keyword
)
29439 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
29447 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29448 function-definition. */
29451 cp_parser_token_starts_function_definition_p (cp_token
* token
)
29453 return (/* An ordinary function-body begins with an `{'. */
29454 token
->type
== CPP_OPEN_BRACE
29455 /* A ctor-initializer begins with a `:'. */
29456 || token
->type
== CPP_COLON
29457 /* A function-try-block begins with `try'. */
29458 || token
->keyword
== RID_TRY
29459 /* A function-transaction-block begins with `__transaction_atomic'
29460 or `__transaction_relaxed'. */
29461 || token
->keyword
== RID_TRANSACTION_ATOMIC
29462 || token
->keyword
== RID_TRANSACTION_RELAXED
29463 /* The named return value extension begins with `return'. */
29464 || token
->keyword
== RID_RETURN
);
29467 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29471 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
29475 token
= cp_lexer_peek_token (parser
->lexer
);
29476 return (token
->type
== CPP_OPEN_BRACE
29477 || (token
->type
== CPP_COLON
29478 && !parser
->colon_doesnt_start_class_def_p
));
29481 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29482 C++0x) ending a template-argument. */
29485 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
29489 token
= cp_lexer_peek_token (parser
->lexer
);
29490 return (token
->type
== CPP_COMMA
29491 || token
->type
== CPP_GREATER
29492 || token
->type
== CPP_ELLIPSIS
29493 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
29496 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29497 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29500 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
29505 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
29506 if (token
->type
== CPP_LESS
)
29508 /* Check for the sequence `<::' in the original code. It would be lexed as
29509 `[:', where `[' is a digraph, and there is no whitespace before
29511 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
29514 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
29515 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
29521 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29522 or none_type otherwise. */
29524 static enum tag_types
29525 cp_parser_token_is_class_key (cp_token
* token
)
29527 switch (token
->keyword
)
29532 return record_type
;
29541 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29542 or none_type otherwise or if the token is null. */
29544 static enum tag_types
29545 cp_parser_token_is_type_parameter_key (cp_token
* token
)
29550 switch (token
->keyword
)
29555 return typename_type
;
29562 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29565 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
29567 if (type
== error_mark_node
)
29569 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
29571 if (permerror (input_location
, "%qs tag used in naming %q#T",
29572 class_key
== union_type
? "union"
29573 : class_key
== record_type
? "struct" : "class",
29575 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
29576 "%q#T was previously declared here", type
);
29580 /* Issue an error message if DECL is redeclared with different
29581 access than its original declaration [class.access.spec/3].
29582 This applies to nested classes, nested class templates and
29583 enumerations [class.mem/1]. */
29586 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
29589 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
29590 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
29593 if ((TREE_PRIVATE (decl
)
29594 != (current_access_specifier
== access_private_node
))
29595 || (TREE_PROTECTED (decl
)
29596 != (current_access_specifier
== access_protected_node
)))
29597 error_at (location
, "%qD redeclared with different access", decl
);
29600 /* Look for the `template' keyword, as a syntactic disambiguator.
29601 Return TRUE iff it is present, in which case it will be
29605 cp_parser_optional_template_keyword (cp_parser
*parser
)
29607 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29609 /* In C++98 the `template' keyword can only be used within templates;
29610 outside templates the parser can always figure out what is a
29611 template and what is not. In C++11, per the resolution of DR 468,
29612 `template' is allowed in cases where it is not strictly necessary. */
29613 if (!processing_template_decl
29614 && pedantic
&& cxx_dialect
== cxx98
)
29616 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29617 pedwarn (token
->location
, OPT_Wpedantic
,
29618 "in C++98 %<template%> (as a disambiguator) is only "
29619 "allowed within templates");
29620 /* If this part of the token stream is rescanned, the same
29621 error message would be generated. So, we purge the token
29622 from the stream. */
29623 cp_lexer_purge_token (parser
->lexer
);
29628 /* Consume the `template' keyword. */
29629 cp_lexer_consume_token (parser
->lexer
);
29636 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29637 set PARSER->SCOPE, and perform other related actions. */
29640 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
29642 struct tree_check
*check_value
;
29644 /* Get the stored value. */
29645 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
29646 /* Set the scope from the stored value. */
29647 parser
->scope
= saved_checks_value (check_value
);
29648 parser
->qualifying_scope
= check_value
->qualifying_scope
;
29649 parser
->object_scope
= NULL_TREE
;
29652 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29653 encounter the end of a block before what we were looking for. */
29656 cp_parser_cache_group (cp_parser
*parser
,
29657 enum cpp_ttype end
,
29662 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29664 /* Abort a parenthesized expression if we encounter a semicolon. */
29665 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
29666 && token
->type
== CPP_SEMICOLON
)
29668 /* If we've reached the end of the file, stop. */
29669 if (token
->type
== CPP_EOF
29670 || (end
!= CPP_PRAGMA_EOL
29671 && token
->type
== CPP_PRAGMA_EOL
))
29673 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
29674 /* We've hit the end of an enclosing block, so there's been some
29675 kind of syntax error. */
29678 /* Consume the token. */
29679 cp_lexer_consume_token (parser
->lexer
);
29680 /* See if it starts a new group. */
29681 if (token
->type
== CPP_OPEN_BRACE
)
29683 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29684 /* In theory this should probably check end == '}', but
29685 cp_parser_save_member_function_body needs it to exit
29686 after either '}' or ')' when called with ')'. */
29690 else if (token
->type
== CPP_OPEN_PAREN
)
29692 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29693 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29696 else if (token
->type
== CPP_PRAGMA
)
29697 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29698 else if (token
->type
== end
)
29703 /* Like above, for caching a default argument or NSDMI. Both of these are
29704 terminated by a non-nested comma, but it can be unclear whether or not a
29705 comma is nested in a template argument list unless we do more parsing.
29706 In order to handle this ambiguity, when we encounter a ',' after a '<'
29707 we try to parse what follows as a parameter-declaration-list (in the
29708 case of a default argument) or a member-declarator (in the case of an
29709 NSDMI). If that succeeds, then we stop caching. */
29712 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29714 unsigned depth
= 0;
29715 int maybe_template_id
= 0;
29716 cp_token
*first_token
;
29718 tree default_argument
;
29720 /* Add tokens until we have processed the entire default
29721 argument. We add the range [first_token, token). */
29722 first_token
= cp_lexer_peek_token (parser
->lexer
);
29723 if (first_token
->type
== CPP_OPEN_BRACE
)
29725 /* For list-initialization, this is straightforward. */
29726 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29727 token
= cp_lexer_peek_token (parser
->lexer
);
29733 /* Peek at the next token. */
29734 token
= cp_lexer_peek_token (parser
->lexer
);
29735 /* What we do depends on what token we have. */
29736 switch (token
->type
)
29738 /* In valid code, a default argument must be
29739 immediately followed by a `,' `)', or `...'. */
29741 if (depth
== 0 && maybe_template_id
)
29743 /* If we've seen a '<', we might be in a
29744 template-argument-list. Until Core issue 325 is
29745 resolved, we don't know how this situation ought
29746 to be handled, so try to DTRT. We check whether
29747 what comes after the comma is a valid parameter
29748 declaration list. If it is, then the comma ends
29749 the default argument; otherwise the default
29750 argument continues. */
29751 bool error
= false;
29754 /* Set ITALP so cp_parser_parameter_declaration_list
29755 doesn't decide to commit to this parse. */
29756 bool saved_italp
= parser
->in_template_argument_list_p
;
29757 parser
->in_template_argument_list_p
= true;
29759 cp_parser_parse_tentatively (parser
);
29763 /* Parse declarators until we reach a non-comma or
29764 somthing that cannot be an initializer.
29765 Just checking whether we're looking at a single
29766 declarator is insufficient. Consider:
29767 int var = tuple<T,U>::x;
29768 The template parameter 'U' looks exactly like a
29772 int ctor_dtor_or_conv_p
;
29773 cp_lexer_consume_token (parser
->lexer
);
29774 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29775 CP_PARSER_FLAGS_NONE
,
29776 &ctor_dtor_or_conv_p
,
29777 /*parenthesized_p=*/NULL
,
29779 /*friend_p=*/false,
29780 /*static_p=*/false);
29781 peek
= cp_lexer_peek_token (parser
->lexer
);
29782 if (cp_parser_error_occurred (parser
))
29785 while (peek
->type
== CPP_COMMA
);
29786 /* If we met an '=' or ';' then the original comma
29787 was the end of the NSDMI. Otherwise assume
29788 we're still in the NSDMI. */
29789 error
= (peek
->type
!= CPP_EQ
29790 && peek
->type
!= CPP_SEMICOLON
);
29794 cp_lexer_consume_token (parser
->lexer
);
29795 begin_scope (sk_function_parms
, NULL_TREE
);
29796 tree t
= cp_parser_parameter_declaration_list
29797 (parser
, CP_PARSER_FLAGS_NONE
);
29798 if (t
== error_mark_node
)
29800 pop_bindings_and_leave_scope ();
29802 if (!cp_parser_error_occurred (parser
) && !error
)
29804 cp_parser_abort_tentative_parse (parser
);
29806 parser
->in_template_argument_list_p
= saved_italp
;
29810 case CPP_CLOSE_PAREN
:
29812 /* If we run into a non-nested `;', `}', or `]',
29813 then the code is invalid -- but the default
29814 argument is certainly over. */
29815 case CPP_SEMICOLON
:
29816 case CPP_CLOSE_BRACE
:
29817 case CPP_CLOSE_SQUARE
:
29819 /* Handle correctly int n = sizeof ... ( p ); */
29820 && token
->type
!= CPP_ELLIPSIS
)
29822 /* Update DEPTH, if necessary. */
29823 else if (token
->type
== CPP_CLOSE_PAREN
29824 || token
->type
== CPP_CLOSE_BRACE
29825 || token
->type
== CPP_CLOSE_SQUARE
)
29829 case CPP_OPEN_PAREN
:
29830 case CPP_OPEN_SQUARE
:
29831 case CPP_OPEN_BRACE
:
29837 /* This might be the comparison operator, or it might
29838 start a template argument list. */
29839 ++maybe_template_id
;
29843 if (cxx_dialect
== cxx98
)
29845 /* Fall through for C++0x, which treats the `>>'
29846 operator like two `>' tokens in certain
29848 gcc_fallthrough ();
29853 /* This might be an operator, or it might close a
29854 template argument list. But if a previous '<'
29855 started a template argument list, this will have
29856 closed it, so we can't be in one anymore. */
29857 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29858 if (maybe_template_id
< 0)
29859 maybe_template_id
= 0;
29863 /* If we run out of tokens, issue an error message. */
29865 case CPP_PRAGMA_EOL
:
29866 error_at (token
->location
, "file ends in default argument");
29867 return error_mark_node
;
29871 /* In these cases, we should look for template-ids.
29872 For example, if the default argument is
29873 `X<int, double>()', we need to do name lookup to
29874 figure out whether or not `X' is a template; if
29875 so, the `,' does not end the default argument.
29877 That is not yet done. */
29884 /* If we've reached the end, stop. */
29888 /* Add the token to the token block. */
29889 token
= cp_lexer_consume_token (parser
->lexer
);
29892 /* Create a DEFAULT_ARG to represent the unparsed default
29894 default_argument
= make_node (DEFAULT_ARG
);
29895 DEFARG_TOKENS (default_argument
)
29896 = cp_token_cache_new (first_token
, token
);
29897 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29899 return default_argument
;
29902 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29905 defarg_location (tree default_argument
)
29907 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29908 location_t start
= tokens
->first
->location
;
29909 location_t end
= tokens
->last
->location
;
29910 return make_location (start
, start
, end
);
29913 /* Begin parsing tentatively. We always save tokens while parsing
29914 tentatively so that if the tentative parsing fails we can restore the
29918 cp_parser_parse_tentatively (cp_parser
* parser
)
29920 /* Enter a new parsing context. */
29921 parser
->context
= cp_parser_context_new (parser
->context
);
29922 /* Begin saving tokens. */
29923 cp_lexer_save_tokens (parser
->lexer
);
29924 /* In order to avoid repetitive access control error messages,
29925 access checks are queued up until we are no longer parsing
29927 push_deferring_access_checks (dk_deferred
);
29930 /* Commit to the currently active tentative parse. */
29933 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29935 cp_parser_context
*context
;
29938 /* Mark all of the levels as committed. */
29939 lexer
= parser
->lexer
;
29940 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29942 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29944 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29945 while (!cp_lexer_saving_tokens (lexer
))
29946 lexer
= lexer
->next
;
29947 cp_lexer_commit_tokens (lexer
);
29951 /* Commit to the topmost currently active tentative parse.
29953 Note that this function shouldn't be called when there are
29954 irreversible side-effects while in a tentative state. For
29955 example, we shouldn't create a permanent entry in the symbol
29956 table, or issue an error message that might not apply if the
29957 tentative parse is aborted. */
29960 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29962 cp_parser_context
*context
= parser
->context
;
29963 cp_lexer
*lexer
= parser
->lexer
;
29967 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29969 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29971 while (!cp_lexer_saving_tokens (lexer
))
29972 lexer
= lexer
->next
;
29973 cp_lexer_commit_tokens (lexer
);
29977 /* Abort the currently active tentative parse. All consumed tokens
29978 will be rolled back, and no diagnostics will be issued. */
29981 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29983 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29984 || errorcount
> 0);
29985 cp_parser_simulate_error (parser
);
29986 /* Now, pretend that we want to see if the construct was
29987 successfully parsed. */
29988 cp_parser_parse_definitely (parser
);
29991 /* Stop parsing tentatively. If a parse error has occurred, restore the
29992 token stream. Otherwise, commit to the tokens we have consumed.
29993 Returns true if no error occurred; false otherwise. */
29996 cp_parser_parse_definitely (cp_parser
* parser
)
29998 bool error_occurred
;
29999 cp_parser_context
*context
;
30001 /* Remember whether or not an error occurred, since we are about to
30002 destroy that information. */
30003 error_occurred
= cp_parser_error_occurred (parser
);
30004 /* Remove the topmost context from the stack. */
30005 context
= parser
->context
;
30006 parser
->context
= context
->next
;
30007 /* If no parse errors occurred, commit to the tentative parse. */
30008 if (!error_occurred
)
30010 /* Commit to the tokens read tentatively, unless that was
30012 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
30013 cp_lexer_commit_tokens (parser
->lexer
);
30015 pop_to_parent_deferring_access_checks ();
30017 /* Otherwise, if errors occurred, roll back our state so that things
30018 are just as they were before we began the tentative parse. */
30021 cp_lexer_rollback_tokens (parser
->lexer
);
30022 pop_deferring_access_checks ();
30024 /* Add the context to the front of the free list. */
30025 context
->next
= cp_parser_context_free_list
;
30026 cp_parser_context_free_list
= context
;
30028 return !error_occurred
;
30031 /* Returns true if we are parsing tentatively and are not committed to
30032 this tentative parse. */
30035 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
30037 return (cp_parser_parsing_tentatively (parser
)
30038 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
30041 /* Returns nonzero iff an error has occurred during the most recent
30042 tentative parse. */
30045 cp_parser_error_occurred (cp_parser
* parser
)
30047 return (cp_parser_parsing_tentatively (parser
)
30048 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
30051 /* Returns nonzero if GNU extensions are allowed. */
30054 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
30056 return parser
->allow_gnu_extensions_p
;
30059 /* Objective-C++ Productions */
30062 /* Parse an Objective-C expression, which feeds into a primary-expression
30066 objc-message-expression
30067 objc-string-literal
30068 objc-encode-expression
30069 objc-protocol-expression
30070 objc-selector-expression
30072 Returns a tree representation of the expression. */
30075 cp_parser_objc_expression (cp_parser
* parser
)
30077 /* Try to figure out what kind of declaration is present. */
30078 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30082 case CPP_OPEN_SQUARE
:
30083 return cp_parser_objc_message_expression (parser
);
30085 case CPP_OBJC_STRING
:
30086 kwd
= cp_lexer_consume_token (parser
->lexer
);
30087 return objc_build_string_object (kwd
->u
.value
);
30090 switch (kwd
->keyword
)
30092 case RID_AT_ENCODE
:
30093 return cp_parser_objc_encode_expression (parser
);
30095 case RID_AT_PROTOCOL
:
30096 return cp_parser_objc_protocol_expression (parser
);
30098 case RID_AT_SELECTOR
:
30099 return cp_parser_objc_selector_expression (parser
);
30106 error_at (kwd
->location
,
30107 "misplaced %<@%D%> Objective-C++ construct",
30109 cp_parser_skip_to_end_of_block_or_statement (parser
);
30112 return error_mark_node
;
30115 /* Parse an Objective-C message expression.
30117 objc-message-expression:
30118 [ objc-message-receiver objc-message-args ]
30120 Returns a representation of an Objective-C message. */
30123 cp_parser_objc_message_expression (cp_parser
* parser
)
30125 tree receiver
, messageargs
;
30127 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30128 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
30129 receiver
= cp_parser_objc_message_receiver (parser
);
30130 messageargs
= cp_parser_objc_message_args (parser
);
30131 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30132 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
30134 tree result
= objc_build_message_expr (receiver
, messageargs
);
30136 /* Construct a location e.g.
30139 ranging from the '[' to the ']', with the caret at the start. */
30140 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
30141 protected_set_expr_location (result
, combined_loc
);
30146 /* Parse an objc-message-receiver.
30148 objc-message-receiver:
30150 simple-type-specifier
30152 Returns a representation of the type or expression. */
30155 cp_parser_objc_message_receiver (cp_parser
* parser
)
30159 /* An Objective-C message receiver may be either (1) a type
30160 or (2) an expression. */
30161 cp_parser_parse_tentatively (parser
);
30162 rcv
= cp_parser_expression (parser
);
30164 /* If that worked out, fine. */
30165 if (cp_parser_parse_definitely (parser
))
30168 cp_parser_parse_tentatively (parser
);
30169 rcv
= cp_parser_simple_type_specifier (parser
,
30170 /*decl_specs=*/NULL
,
30171 CP_PARSER_FLAGS_NONE
);
30173 if (cp_parser_parse_definitely (parser
))
30174 return objc_get_class_reference (rcv
);
30176 cp_parser_error (parser
, "objective-c++ message receiver expected");
30177 return error_mark_node
;
30180 /* Parse the arguments and selectors comprising an Objective-C message.
30185 objc-selector-args , objc-comma-args
30187 objc-selector-args:
30188 objc-selector [opt] : assignment-expression
30189 objc-selector-args objc-selector [opt] : assignment-expression
30192 assignment-expression
30193 objc-comma-args , assignment-expression
30195 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30196 selector arguments and TREE_VALUE containing a list of comma
30200 cp_parser_objc_message_args (cp_parser
* parser
)
30202 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
30203 bool maybe_unary_selector_p
= true;
30204 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30206 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30208 tree selector
= NULL_TREE
, arg
;
30210 if (token
->type
!= CPP_COLON
)
30211 selector
= cp_parser_objc_selector (parser
);
30213 /* Detect if we have a unary selector. */
30214 if (maybe_unary_selector_p
30215 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30216 return build_tree_list (selector
, NULL_TREE
);
30218 maybe_unary_selector_p
= false;
30219 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30220 arg
= cp_parser_assignment_expression (parser
);
30223 = chainon (sel_args
,
30224 build_tree_list (selector
, arg
));
30226 token
= cp_lexer_peek_token (parser
->lexer
);
30229 /* Handle non-selector arguments, if any. */
30230 while (token
->type
== CPP_COMMA
)
30234 cp_lexer_consume_token (parser
->lexer
);
30235 arg
= cp_parser_assignment_expression (parser
);
30238 = chainon (addl_args
,
30239 build_tree_list (NULL_TREE
, arg
));
30241 token
= cp_lexer_peek_token (parser
->lexer
);
30244 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
30246 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
30247 return build_tree_list (error_mark_node
, error_mark_node
);
30250 return build_tree_list (sel_args
, addl_args
);
30253 /* Parse an Objective-C encode expression.
30255 objc-encode-expression:
30256 @encode objc-typename
30258 Returns an encoded representation of the type argument. */
30261 cp_parser_objc_encode_expression (cp_parser
* parser
)
30265 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30267 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
30268 matching_parens parens
;
30269 parens
.require_open (parser
);
30270 token
= cp_lexer_peek_token (parser
->lexer
);
30271 type
= complete_type (cp_parser_type_id (parser
));
30272 parens
.require_close (parser
);
30276 error_at (token
->location
,
30277 "%<@encode%> must specify a type as an argument");
30278 return error_mark_node
;
30281 /* This happens if we find @encode(T) (where T is a template
30282 typename or something dependent on a template typename) when
30283 parsing a template. In that case, we can't compile it
30284 immediately, but we rather create an AT_ENCODE_EXPR which will
30285 need to be instantiated when the template is used.
30287 if (dependent_type_p (type
))
30289 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
30290 TREE_READONLY (value
) = 1;
30295 /* Build a location of the form:
30298 with caret==start at the @ token, finishing at the close paren. */
30299 location_t combined_loc
30300 = make_location (start_loc
, start_loc
,
30301 cp_lexer_previous_token (parser
->lexer
)->location
);
30303 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
30306 /* Parse an Objective-C @defs expression. */
30309 cp_parser_objc_defs_expression (cp_parser
*parser
)
30313 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
30314 matching_parens parens
;
30315 parens
.require_open (parser
);
30316 name
= cp_parser_identifier (parser
);
30317 parens
.require_close (parser
);
30319 return objc_get_class_ivars (name
);
30322 /* Parse an Objective-C protocol expression.
30324 objc-protocol-expression:
30325 @protocol ( identifier )
30327 Returns a representation of the protocol expression. */
30330 cp_parser_objc_protocol_expression (cp_parser
* parser
)
30333 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30335 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30336 matching_parens parens
;
30337 parens
.require_open (parser
);
30338 proto
= cp_parser_identifier (parser
);
30339 parens
.require_close (parser
);
30341 /* Build a location of the form:
30344 with caret==start at the @ token, finishing at the close paren. */
30345 location_t combined_loc
30346 = make_location (start_loc
, start_loc
,
30347 cp_lexer_previous_token (parser
->lexer
)->location
);
30348 tree result
= objc_build_protocol_expr (proto
);
30349 protected_set_expr_location (result
, combined_loc
);
30353 /* Parse an Objective-C selector expression.
30355 objc-selector-expression:
30356 @selector ( objc-method-signature )
30358 objc-method-signature:
30364 objc-selector-seq objc-selector :
30366 Returns a representation of the method selector. */
30369 cp_parser_objc_selector_expression (cp_parser
* parser
)
30371 tree sel_seq
= NULL_TREE
;
30372 bool maybe_unary_selector_p
= true;
30374 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30376 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
30377 matching_parens parens
;
30378 parens
.require_open (parser
);
30379 token
= cp_lexer_peek_token (parser
->lexer
);
30381 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
30382 || token
->type
== CPP_SCOPE
)
30384 tree selector
= NULL_TREE
;
30386 if (token
->type
!= CPP_COLON
30387 || token
->type
== CPP_SCOPE
)
30388 selector
= cp_parser_objc_selector (parser
);
30390 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
30391 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
30393 /* Detect if we have a unary selector. */
30394 if (maybe_unary_selector_p
)
30396 sel_seq
= selector
;
30397 goto finish_selector
;
30401 cp_parser_error (parser
, "expected %<:%>");
30404 maybe_unary_selector_p
= false;
30405 token
= cp_lexer_consume_token (parser
->lexer
);
30407 if (token
->type
== CPP_SCOPE
)
30410 = chainon (sel_seq
,
30411 build_tree_list (selector
, NULL_TREE
));
30413 = chainon (sel_seq
,
30414 build_tree_list (NULL_TREE
, NULL_TREE
));
30418 = chainon (sel_seq
,
30419 build_tree_list (selector
, NULL_TREE
));
30421 token
= cp_lexer_peek_token (parser
->lexer
);
30425 parens
.require_close (parser
);
30428 /* Build a location of the form:
30431 with caret==start at the @ token, finishing at the close paren. */
30432 location_t combined_loc
30433 = make_location (loc
, loc
,
30434 cp_lexer_previous_token (parser
->lexer
)->location
);
30435 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
30436 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30437 protected_set_expr_location (result
, combined_loc
);
30441 /* Parse a list of identifiers.
30443 objc-identifier-list:
30445 objc-identifier-list , identifier
30447 Returns a TREE_LIST of identifier nodes. */
30450 cp_parser_objc_identifier_list (cp_parser
* parser
)
30456 identifier
= cp_parser_identifier (parser
);
30457 if (identifier
== error_mark_node
)
30458 return error_mark_node
;
30460 list
= build_tree_list (NULL_TREE
, identifier
);
30461 sep
= cp_lexer_peek_token (parser
->lexer
);
30463 while (sep
->type
== CPP_COMMA
)
30465 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30466 identifier
= cp_parser_identifier (parser
);
30467 if (identifier
== error_mark_node
)
30470 list
= chainon (list
, build_tree_list (NULL_TREE
,
30472 sep
= cp_lexer_peek_token (parser
->lexer
);
30478 /* Parse an Objective-C alias declaration.
30480 objc-alias-declaration:
30481 @compatibility_alias identifier identifier ;
30483 This function registers the alias mapping with the Objective-C front end.
30484 It returns nothing. */
30487 cp_parser_objc_alias_declaration (cp_parser
* parser
)
30491 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
30492 alias
= cp_parser_identifier (parser
);
30493 orig
= cp_parser_identifier (parser
);
30494 objc_declare_alias (alias
, orig
);
30495 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30498 /* Parse an Objective-C class forward-declaration.
30500 objc-class-declaration:
30501 @class objc-identifier-list ;
30503 The function registers the forward declarations with the Objective-C
30504 front end. It returns nothing. */
30507 cp_parser_objc_class_declaration (cp_parser
* parser
)
30509 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
30514 id
= cp_parser_identifier (parser
);
30515 if (id
== error_mark_node
)
30518 objc_declare_class (id
);
30520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30521 cp_lexer_consume_token (parser
->lexer
);
30525 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30528 /* Parse a list of Objective-C protocol references.
30530 objc-protocol-refs-opt:
30531 objc-protocol-refs [opt]
30533 objc-protocol-refs:
30534 < objc-identifier-list >
30536 Returns a TREE_LIST of identifiers, if any. */
30539 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
30541 tree protorefs
= NULL_TREE
;
30543 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
30545 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
30546 protorefs
= cp_parser_objc_identifier_list (parser
);
30547 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
30553 /* Parse a Objective-C visibility specification. */
30556 cp_parser_objc_visibility_spec (cp_parser
* parser
)
30558 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
30560 switch (vis
->keyword
)
30562 case RID_AT_PRIVATE
:
30563 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
30565 case RID_AT_PROTECTED
:
30566 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
30568 case RID_AT_PUBLIC
:
30569 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
30571 case RID_AT_PACKAGE
:
30572 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
30578 /* Eat '@private'/'@protected'/'@public'. */
30579 cp_lexer_consume_token (parser
->lexer
);
30582 /* Parse an Objective-C method type. Return 'true' if it is a class
30583 (+) method, and 'false' if it is an instance (-) method. */
30586 cp_parser_objc_method_type (cp_parser
* parser
)
30588 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
30594 /* Parse an Objective-C protocol qualifier. */
30597 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
30599 tree quals
= NULL_TREE
, node
;
30600 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30602 node
= token
->u
.value
;
30604 while (node
&& identifier_p (node
)
30605 && (node
== ridpointers
[(int) RID_IN
]
30606 || node
== ridpointers
[(int) RID_OUT
]
30607 || node
== ridpointers
[(int) RID_INOUT
]
30608 || node
== ridpointers
[(int) RID_BYCOPY
]
30609 || node
== ridpointers
[(int) RID_BYREF
]
30610 || node
== ridpointers
[(int) RID_ONEWAY
]))
30612 quals
= tree_cons (NULL_TREE
, node
, quals
);
30613 cp_lexer_consume_token (parser
->lexer
);
30614 token
= cp_lexer_peek_token (parser
->lexer
);
30615 node
= token
->u
.value
;
30621 /* Parse an Objective-C typename. */
30624 cp_parser_objc_typename (cp_parser
* parser
)
30626 tree type_name
= NULL_TREE
;
30628 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30630 tree proto_quals
, cp_type
= NULL_TREE
;
30632 matching_parens parens
;
30633 parens
.consume_open (parser
); /* Eat '('. */
30634 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
30636 /* An ObjC type name may consist of just protocol qualifiers, in which
30637 case the type shall default to 'id'. */
30638 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30640 cp_type
= cp_parser_type_id (parser
);
30642 /* If the type could not be parsed, an error has already
30643 been produced. For error recovery, behave as if it had
30644 not been specified, which will use the default type
30646 if (cp_type
== error_mark_node
)
30648 cp_type
= NULL_TREE
;
30649 /* We need to skip to the closing parenthesis as
30650 cp_parser_type_id() does not seem to do it for
30652 cp_parser_skip_to_closing_parenthesis (parser
,
30653 /*recovering=*/true,
30654 /*or_comma=*/false,
30655 /*consume_paren=*/false);
30659 parens
.require_close (parser
);
30660 type_name
= build_tree_list (proto_quals
, cp_type
);
30666 /* Check to see if TYPE refers to an Objective-C selector name. */
30669 cp_parser_objc_selector_p (enum cpp_ttype type
)
30671 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
30672 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
30673 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
30674 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
30675 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
30678 /* Parse an Objective-C selector. */
30681 cp_parser_objc_selector (cp_parser
* parser
)
30683 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30685 if (!cp_parser_objc_selector_p (token
->type
))
30687 error_at (token
->location
, "invalid Objective-C++ selector name");
30688 return error_mark_node
;
30691 /* C++ operator names are allowed to appear in ObjC selectors. */
30692 switch (token
->type
)
30694 case CPP_AND_AND
: return get_identifier ("and");
30695 case CPP_AND_EQ
: return get_identifier ("and_eq");
30696 case CPP_AND
: return get_identifier ("bitand");
30697 case CPP_OR
: return get_identifier ("bitor");
30698 case CPP_COMPL
: return get_identifier ("compl");
30699 case CPP_NOT
: return get_identifier ("not");
30700 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30701 case CPP_OR_OR
: return get_identifier ("or");
30702 case CPP_OR_EQ
: return get_identifier ("or_eq");
30703 case CPP_XOR
: return get_identifier ("xor");
30704 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30705 default: return token
->u
.value
;
30709 /* Parse an Objective-C params list. */
30712 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30714 tree params
= NULL_TREE
;
30715 bool maybe_unary_selector_p
= true;
30716 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30718 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30720 tree selector
= NULL_TREE
, type_name
, identifier
;
30721 tree parm_attr
= NULL_TREE
;
30723 if (token
->keyword
== RID_ATTRIBUTE
)
30726 if (token
->type
!= CPP_COLON
)
30727 selector
= cp_parser_objc_selector (parser
);
30729 /* Detect if we have a unary selector. */
30730 if (maybe_unary_selector_p
30731 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30733 params
= selector
; /* Might be followed by attributes. */
30737 maybe_unary_selector_p
= false;
30738 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30740 /* Something went quite wrong. There should be a colon
30741 here, but there is not. Stop parsing parameters. */
30744 type_name
= cp_parser_objc_typename (parser
);
30745 /* New ObjC allows attributes on parameters too. */
30746 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30747 parm_attr
= cp_parser_attributes_opt (parser
);
30748 identifier
= cp_parser_identifier (parser
);
30752 objc_build_keyword_decl (selector
,
30757 token
= cp_lexer_peek_token (parser
->lexer
);
30760 if (params
== NULL_TREE
)
30762 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30763 return error_mark_node
;
30766 /* We allow tail attributes for the method. */
30767 if (token
->keyword
== RID_ATTRIBUTE
)
30769 *attributes
= cp_parser_attributes_opt (parser
);
30770 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30771 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30773 cp_parser_error (parser
,
30774 "method attributes must be specified at the end");
30775 return error_mark_node
;
30778 if (params
== NULL_TREE
)
30780 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30781 return error_mark_node
;
30786 /* Parse the non-keyword Objective-C params. */
30789 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30792 tree params
= make_node (TREE_LIST
);
30793 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30794 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30796 while (token
->type
== CPP_COMMA
)
30798 cp_parameter_declarator
*parmdecl
;
30801 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30802 token
= cp_lexer_peek_token (parser
->lexer
);
30804 if (token
->type
== CPP_ELLIPSIS
)
30806 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30808 token
= cp_lexer_peek_token (parser
->lexer
);
30812 /* TODO: parse attributes for tail parameters. */
30813 parmdecl
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
30815 parm
= grokdeclarator (parmdecl
->declarator
,
30816 &parmdecl
->decl_specifiers
,
30817 PARM
, /*initialized=*/0,
30818 /*attrlist=*/NULL
);
30820 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30821 token
= cp_lexer_peek_token (parser
->lexer
);
30824 /* We allow tail attributes for the method. */
30825 if (token
->keyword
== RID_ATTRIBUTE
)
30827 if (*attributes
== NULL_TREE
)
30829 *attributes
= cp_parser_attributes_opt (parser
);
30830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30831 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30835 /* We have an error, but parse the attributes, so that we can
30837 *attributes
= cp_parser_attributes_opt (parser
);
30839 cp_parser_error (parser
,
30840 "method attributes must be specified at the end");
30841 return error_mark_node
;
30847 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30850 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30852 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30854 /* If the next token is `extern' and the following token is a string
30855 literal, then we have a linkage specification. */
30856 if (token
->keyword
== RID_EXTERN
30857 && cp_parser_is_pure_string_literal
30858 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30859 cp_parser_linkage_specification (parser
);
30860 /* Handle #pragma, if any. */
30861 else if (token
->type
== CPP_PRAGMA
)
30862 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30863 /* Allow stray semicolons. */
30864 else if (token
->type
== CPP_SEMICOLON
)
30865 cp_lexer_consume_token (parser
->lexer
);
30866 /* Mark methods as optional or required, when building protocols. */
30867 else if (token
->keyword
== RID_AT_OPTIONAL
)
30869 cp_lexer_consume_token (parser
->lexer
);
30870 objc_set_method_opt (true);
30872 else if (token
->keyword
== RID_AT_REQUIRED
)
30874 cp_lexer_consume_token (parser
->lexer
);
30875 objc_set_method_opt (false);
30877 else if (token
->keyword
== RID_NAMESPACE
)
30878 cp_parser_namespace_definition (parser
);
30879 /* Other stray characters must generate errors. */
30880 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30882 cp_lexer_consume_token (parser
->lexer
);
30883 error ("stray %qs between Objective-C++ methods",
30884 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30886 /* Finally, try to parse a block-declaration, or a function-definition. */
30888 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30891 /* Parse a method signature. */
30894 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30896 tree rettype
, kwdparms
, optparms
;
30897 bool ellipsis
= false;
30898 bool is_class_method
;
30900 is_class_method
= cp_parser_objc_method_type (parser
);
30901 rettype
= cp_parser_objc_typename (parser
);
30902 *attributes
= NULL_TREE
;
30903 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30904 if (kwdparms
== error_mark_node
)
30905 return error_mark_node
;
30906 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30907 if (optparms
== error_mark_node
)
30908 return error_mark_node
;
30910 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30914 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30917 cp_lexer_save_tokens (parser
->lexer
);
30918 tattr
= cp_parser_attributes_opt (parser
);
30919 gcc_assert (tattr
) ;
30921 /* If the attributes are followed by a method introducer, this is not allowed.
30922 Dump the attributes and flag the situation. */
30923 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30924 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30927 /* Otherwise, the attributes introduce some interstitial code, possibly so
30928 rewind to allow that check. */
30929 cp_lexer_rollback_tokens (parser
->lexer
);
30933 /* Parse an Objective-C method prototype list. */
30936 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30938 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30940 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30942 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30944 tree attributes
, sig
;
30945 bool is_class_method
;
30946 if (token
->type
== CPP_PLUS
)
30947 is_class_method
= true;
30949 is_class_method
= false;
30950 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30951 if (sig
== error_mark_node
)
30953 cp_parser_skip_to_end_of_block_or_statement (parser
);
30954 token
= cp_lexer_peek_token (parser
->lexer
);
30957 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30958 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30960 else if (token
->keyword
== RID_AT_PROPERTY
)
30961 cp_parser_objc_at_property_declaration (parser
);
30962 else if (token
->keyword
== RID_ATTRIBUTE
30963 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30964 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30966 "prefix attributes are ignored for methods");
30968 /* Allow for interspersed non-ObjC++ code. */
30969 cp_parser_objc_interstitial_code (parser
);
30971 token
= cp_lexer_peek_token (parser
->lexer
);
30974 if (token
->type
!= CPP_EOF
)
30975 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30977 cp_parser_error (parser
, "expected %<@end%>");
30979 objc_finish_interface ();
30982 /* Parse an Objective-C method definition list. */
30985 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30987 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30989 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30993 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30996 tree sig
, attribute
;
30997 bool is_class_method
;
30998 if (token
->type
== CPP_PLUS
)
30999 is_class_method
= true;
31001 is_class_method
= false;
31002 push_deferring_access_checks (dk_deferred
);
31003 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
31004 if (sig
== error_mark_node
)
31006 cp_parser_skip_to_end_of_block_or_statement (parser
);
31007 token
= cp_lexer_peek_token (parser
->lexer
);
31010 objc_start_method_definition (is_class_method
, sig
, attribute
,
31013 /* For historical reasons, we accept an optional semicolon. */
31014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31015 cp_lexer_consume_token (parser
->lexer
);
31017 ptk
= cp_lexer_peek_token (parser
->lexer
);
31018 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
31019 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
31021 perform_deferred_access_checks (tf_warning_or_error
);
31022 stop_deferring_access_checks ();
31023 meth
= cp_parser_function_definition_after_declarator (parser
,
31025 pop_deferring_access_checks ();
31026 objc_finish_method_definition (meth
);
31029 /* The following case will be removed once @synthesize is
31030 completely implemented. */
31031 else if (token
->keyword
== RID_AT_PROPERTY
)
31032 cp_parser_objc_at_property_declaration (parser
);
31033 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
31034 cp_parser_objc_at_synthesize_declaration (parser
);
31035 else if (token
->keyword
== RID_AT_DYNAMIC
)
31036 cp_parser_objc_at_dynamic_declaration (parser
);
31037 else if (token
->keyword
== RID_ATTRIBUTE
31038 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
31039 warning_at (token
->location
, OPT_Wattributes
,
31040 "prefix attributes are ignored for methods");
31042 /* Allow for interspersed non-ObjC++ code. */
31043 cp_parser_objc_interstitial_code (parser
);
31045 token
= cp_lexer_peek_token (parser
->lexer
);
31048 if (token
->type
!= CPP_EOF
)
31049 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31051 cp_parser_error (parser
, "expected %<@end%>");
31053 objc_finish_implementation ();
31056 /* Parse Objective-C ivars. */
31059 cp_parser_objc_class_ivars (cp_parser
* parser
)
31061 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31063 if (token
->type
!= CPP_OPEN_BRACE
)
31064 return; /* No ivars specified. */
31066 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
31067 token
= cp_lexer_peek_token (parser
->lexer
);
31069 while (token
->type
!= CPP_CLOSE_BRACE
31070 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31072 cp_decl_specifier_seq declspecs
;
31073 int decl_class_or_enum_p
;
31074 tree prefix_attributes
;
31076 cp_parser_objc_visibility_spec (parser
);
31078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
31081 cp_parser_decl_specifier_seq (parser
,
31082 CP_PARSER_FLAGS_OPTIONAL
,
31084 &decl_class_or_enum_p
);
31086 /* auto, register, static, extern, mutable. */
31087 if (declspecs
.storage_class
!= sc_none
)
31089 cp_parser_error (parser
, "invalid type for instance variable");
31090 declspecs
.storage_class
= sc_none
;
31093 /* thread_local. */
31094 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31096 cp_parser_error (parser
, "invalid type for instance variable");
31097 declspecs
.locations
[ds_thread
] = 0;
31101 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31103 cp_parser_error (parser
, "invalid type for instance variable");
31104 declspecs
.locations
[ds_typedef
] = 0;
31107 prefix_attributes
= declspecs
.attributes
;
31108 declspecs
.attributes
= NULL_TREE
;
31110 /* Keep going until we hit the `;' at the end of the
31112 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31114 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
31115 cp_declarator
*declarator
= NULL
;
31116 int ctor_dtor_or_conv_p
;
31118 /* Check for a (possibly unnamed) bitfield declaration. */
31119 token
= cp_lexer_peek_token (parser
->lexer
);
31120 if (token
->type
== CPP_COLON
)
31123 if (token
->type
== CPP_NAME
31124 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
31127 /* Get the name of the bitfield. */
31128 declarator
= make_id_declarator (NULL_TREE
,
31129 cp_parser_identifier (parser
),
31130 sfk_none
, token
->location
);
31133 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
31134 /* Get the width of the bitfield. */
31136 = cp_parser_constant_expression (parser
);
31140 /* Parse the declarator. */
31142 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31143 CP_PARSER_FLAGS_NONE
,
31144 &ctor_dtor_or_conv_p
,
31145 /*parenthesized_p=*/NULL
,
31146 /*member_p=*/false,
31147 /*friend_p=*/false,
31148 /*static_p=*/false);
31151 /* Look for attributes that apply to the ivar. */
31152 attributes
= cp_parser_attributes_opt (parser
);
31153 /* Remember which attributes are prefix attributes and
31155 first_attribute
= attributes
;
31156 /* Combine the attributes. */
31157 attributes
= attr_chainon (prefix_attributes
, attributes
);
31160 /* Create the bitfield declaration. */
31161 decl
= grokbitfield (declarator
, &declspecs
,
31162 width
, NULL_TREE
, attributes
);
31164 decl
= grokfield (declarator
, &declspecs
,
31165 NULL_TREE
, /*init_const_expr_p=*/false,
31166 NULL_TREE
, attributes
);
31168 /* Add the instance variable. */
31169 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
31170 objc_add_instance_variable (decl
);
31172 /* Reset PREFIX_ATTRIBUTES. */
31173 if (attributes
!= error_mark_node
)
31175 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31176 attributes
= TREE_CHAIN (attributes
);
31178 TREE_CHAIN (attributes
) = NULL_TREE
;
31181 token
= cp_lexer_peek_token (parser
->lexer
);
31183 if (token
->type
== CPP_COMMA
)
31185 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31191 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31192 token
= cp_lexer_peek_token (parser
->lexer
);
31195 if (token
->keyword
== RID_AT_END
)
31196 cp_parser_error (parser
, "expected %<}%>");
31198 /* Do not consume the RID_AT_END, so it will be read again as terminating
31199 the @interface of @implementation. */
31200 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31201 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
31203 /* For historical reasons, we accept an optional semicolon. */
31204 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31205 cp_lexer_consume_token (parser
->lexer
);
31208 /* Parse an Objective-C protocol declaration. */
31211 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
31213 tree proto
, protorefs
;
31216 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
31217 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31219 tok
= cp_lexer_peek_token (parser
->lexer
);
31220 error_at (tok
->location
, "identifier expected after %<@protocol%>");
31221 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31225 /* See if we have a forward declaration or a definition. */
31226 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
31228 /* Try a forward declaration first. */
31229 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
31235 id
= cp_parser_identifier (parser
);
31236 if (id
== error_mark_node
)
31239 objc_declare_protocol (id
, attributes
);
31241 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31242 cp_lexer_consume_token (parser
->lexer
);
31246 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31249 /* Ok, we got a full-fledged definition (or at least should). */
31252 proto
= cp_parser_identifier (parser
);
31253 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
31254 objc_start_protocol (proto
, protorefs
, attributes
);
31255 cp_parser_objc_method_prototype_list (parser
);
31259 /* Parse an Objective-C superclass or category. */
31262 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
31265 tree
*categ
, bool *is_class_extension
)
31267 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
31269 *super
= *categ
= NULL_TREE
;
31270 *is_class_extension
= false;
31271 if (next
->type
== CPP_COLON
)
31273 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
31274 *super
= cp_parser_identifier (parser
);
31276 else if (next
->type
== CPP_OPEN_PAREN
)
31278 matching_parens parens
;
31279 parens
.consume_open (parser
); /* Eat '('. */
31281 /* If there is no category name, and this is an @interface, we
31282 have a class extension. */
31283 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31285 *categ
= NULL_TREE
;
31286 *is_class_extension
= true;
31289 *categ
= cp_parser_identifier (parser
);
31291 parens
.require_close (parser
);
31295 /* Parse an Objective-C class interface. */
31298 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
31300 tree name
, super
, categ
, protos
;
31301 bool is_class_extension
;
31303 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
31304 name
= cp_parser_identifier (parser
);
31305 if (name
== error_mark_node
)
31307 /* It's hard to recover because even if valid @interface stuff
31308 is to follow, we can't compile it (or validate it) if we
31309 don't even know which class it refers to. Let's assume this
31310 was a stray '@interface' token in the stream and skip it.
31314 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
31315 &is_class_extension
);
31316 protos
= cp_parser_objc_protocol_refs_opt (parser
);
31318 /* We have either a class or a category on our hands. */
31319 if (categ
|| is_class_extension
)
31320 objc_start_category_interface (name
, categ
, protos
, attributes
);
31323 objc_start_class_interface (name
, super
, protos
, attributes
);
31324 /* Handle instance variable declarations, if any. */
31325 cp_parser_objc_class_ivars (parser
);
31326 objc_continue_interface ();
31329 cp_parser_objc_method_prototype_list (parser
);
31332 /* Parse an Objective-C class implementation. */
31335 cp_parser_objc_class_implementation (cp_parser
* parser
)
31337 tree name
, super
, categ
;
31338 bool is_class_extension
;
31340 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
31341 name
= cp_parser_identifier (parser
);
31342 if (name
== error_mark_node
)
31344 /* It's hard to recover because even if valid @implementation
31345 stuff is to follow, we can't compile it (or validate it) if
31346 we don't even know which class it refers to. Let's assume
31347 this was a stray '@implementation' token in the stream and
31352 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
31353 &is_class_extension
);
31355 /* We have either a class or a category on our hands. */
31357 objc_start_category_implementation (name
, categ
);
31360 objc_start_class_implementation (name
, super
);
31361 /* Handle instance variable declarations, if any. */
31362 cp_parser_objc_class_ivars (parser
);
31363 objc_continue_implementation ();
31366 cp_parser_objc_method_definition_list (parser
);
31369 /* Consume the @end token and finish off the implementation. */
31372 cp_parser_objc_end_implementation (cp_parser
* parser
)
31374 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31375 objc_finish_implementation ();
31378 /* Parse an Objective-C declaration. */
31381 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
31383 /* Try to figure out what kind of declaration is present. */
31384 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31387 switch (kwd
->keyword
)
31392 error_at (kwd
->location
, "attributes may not be specified before"
31393 " the %<@%D%> Objective-C++ keyword",
31397 case RID_AT_IMPLEMENTATION
:
31398 warning_at (kwd
->location
, OPT_Wattributes
,
31399 "prefix attributes are ignored before %<@%D%>",
31406 switch (kwd
->keyword
)
31409 cp_parser_objc_alias_declaration (parser
);
31412 cp_parser_objc_class_declaration (parser
);
31414 case RID_AT_PROTOCOL
:
31415 cp_parser_objc_protocol_declaration (parser
, attributes
);
31417 case RID_AT_INTERFACE
:
31418 cp_parser_objc_class_interface (parser
, attributes
);
31420 case RID_AT_IMPLEMENTATION
:
31421 cp_parser_objc_class_implementation (parser
);
31424 cp_parser_objc_end_implementation (parser
);
31427 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31429 cp_parser_skip_to_end_of_block_or_statement (parser
);
31433 /* Parse an Objective-C try-catch-finally statement.
31435 objc-try-catch-finally-stmt:
31436 @try compound-statement objc-catch-clause-seq [opt]
31437 objc-finally-clause [opt]
31439 objc-catch-clause-seq:
31440 objc-catch-clause objc-catch-clause-seq [opt]
31443 @catch ( objc-exception-declaration ) compound-statement
31445 objc-finally-clause:
31446 @finally compound-statement
31448 objc-exception-declaration:
31449 parameter-declaration
31452 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31456 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31457 for C. Keep them in sync. */
31460 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
31462 location_t location
;
31465 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
31466 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31467 objc_maybe_warn_exceptions (location
);
31468 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31469 node, lest it get absorbed into the surrounding block. */
31470 stmt
= push_stmt_list ();
31471 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31472 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
31474 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
31476 cp_parameter_declarator
*parm
;
31477 tree parameter_declaration
= error_mark_node
;
31478 bool seen_open_paren
= false;
31479 matching_parens parens
;
31481 cp_lexer_consume_token (parser
->lexer
);
31482 if (parens
.require_open (parser
))
31483 seen_open_paren
= true;
31484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
31486 /* We have "@catch (...)" (where the '...' are literally
31487 what is in the code). Skip the '...'.
31488 parameter_declaration is set to NULL_TREE, and
31489 objc_being_catch_clauses() knows that that means
31491 cp_lexer_consume_token (parser
->lexer
);
31492 parameter_declaration
= NULL_TREE
;
31496 /* We have "@catch (NSException *exception)" or something
31497 like that. Parse the parameter declaration. */
31498 parm
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
31501 parameter_declaration
= error_mark_node
;
31503 parameter_declaration
= grokdeclarator (parm
->declarator
,
31504 &parm
->decl_specifiers
,
31505 PARM
, /*initialized=*/0,
31506 /*attrlist=*/NULL
);
31508 if (seen_open_paren
)
31509 parens
.require_close (parser
);
31512 /* If there was no open parenthesis, we are recovering from
31513 an error, and we are trying to figure out what mistake
31514 the user has made. */
31516 /* If there is an immediate closing parenthesis, the user
31517 probably forgot the opening one (ie, they typed "@catch
31518 NSException *e)". Parse the closing parenthesis and keep
31520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31521 cp_lexer_consume_token (parser
->lexer
);
31523 /* If these is no immediate closing parenthesis, the user
31524 probably doesn't know that parenthesis are required at
31525 all (ie, they typed "@catch NSException *e"). So, just
31526 forget about the closing parenthesis and keep going. */
31528 objc_begin_catch_clause (parameter_declaration
);
31529 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31530 objc_finish_catch_clause ();
31532 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
31534 cp_lexer_consume_token (parser
->lexer
);
31535 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31536 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31537 node, lest it get absorbed into the surrounding block. */
31538 stmt
= push_stmt_list ();
31539 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31540 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
31543 return objc_finish_try_stmt ();
31546 /* Parse an Objective-C synchronized statement.
31548 objc-synchronized-stmt:
31549 @synchronized ( expression ) compound-statement
31551 Returns NULL_TREE. */
31554 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
31556 location_t location
;
31559 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
31561 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31562 objc_maybe_warn_exceptions (location
);
31563 matching_parens parens
;
31564 parens
.require_open (parser
);
31565 lock
= cp_parser_expression (parser
);
31566 parens
.require_close (parser
);
31568 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31569 node, lest it get absorbed into the surrounding block. */
31570 stmt
= push_stmt_list ();
31571 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31573 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
31576 /* Parse an Objective-C throw statement.
31579 @throw assignment-expression [opt] ;
31581 Returns a constructed '@throw' statement. */
31584 cp_parser_objc_throw_statement (cp_parser
*parser
)
31586 tree expr
= NULL_TREE
;
31587 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31589 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
31591 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31592 expr
= cp_parser_expression (parser
);
31594 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31596 return objc_build_throw_stmt (loc
, expr
);
31599 /* Parse an Objective-C statement. */
31602 cp_parser_objc_statement (cp_parser
* parser
)
31604 /* Try to figure out what kind of declaration is present. */
31605 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31607 switch (kwd
->keyword
)
31610 return cp_parser_objc_try_catch_finally_statement (parser
);
31611 case RID_AT_SYNCHRONIZED
:
31612 return cp_parser_objc_synchronized_statement (parser
);
31614 return cp_parser_objc_throw_statement (parser
);
31616 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31618 cp_parser_skip_to_end_of_block_or_statement (parser
);
31621 return error_mark_node
;
31624 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31625 look ahead to see if an objc keyword follows the attributes. This
31626 is to detect the use of prefix attributes on ObjC @interface and
31630 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
31632 cp_lexer_save_tokens (parser
->lexer
);
31633 *attrib
= cp_parser_attributes_opt (parser
);
31634 gcc_assert (*attrib
);
31635 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
31637 cp_lexer_commit_tokens (parser
->lexer
);
31640 cp_lexer_rollback_tokens (parser
->lexer
);
31644 /* This routine is a minimal replacement for
31645 c_parser_struct_declaration () used when parsing the list of
31646 types/names or ObjC++ properties. For example, when parsing the
31649 @property (readonly) int a, b, c;
31651 this function is responsible for parsing "int a, int b, int c" and
31652 returning the declarations as CHAIN of DECLs.
31654 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31655 similar parsing. */
31657 cp_parser_objc_struct_declaration (cp_parser
*parser
)
31659 tree decls
= NULL_TREE
;
31660 cp_decl_specifier_seq declspecs
;
31661 int decl_class_or_enum_p
;
31662 tree prefix_attributes
;
31664 cp_parser_decl_specifier_seq (parser
,
31665 CP_PARSER_FLAGS_NONE
,
31667 &decl_class_or_enum_p
);
31669 if (declspecs
.type
== error_mark_node
)
31670 return error_mark_node
;
31672 /* auto, register, static, extern, mutable. */
31673 if (declspecs
.storage_class
!= sc_none
)
31675 cp_parser_error (parser
, "invalid type for property");
31676 declspecs
.storage_class
= sc_none
;
31679 /* thread_local. */
31680 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31682 cp_parser_error (parser
, "invalid type for property");
31683 declspecs
.locations
[ds_thread
] = 0;
31687 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31689 cp_parser_error (parser
, "invalid type for property");
31690 declspecs
.locations
[ds_typedef
] = 0;
31693 prefix_attributes
= declspecs
.attributes
;
31694 declspecs
.attributes
= NULL_TREE
;
31696 /* Keep going until we hit the `;' at the end of the declaration. */
31697 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31699 tree attributes
, first_attribute
, decl
;
31700 cp_declarator
*declarator
;
31703 /* Parse the declarator. */
31704 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31705 CP_PARSER_FLAGS_NONE
,
31706 NULL
, NULL
, false, false, false);
31708 /* Look for attributes that apply to the ivar. */
31709 attributes
= cp_parser_attributes_opt (parser
);
31710 /* Remember which attributes are prefix attributes and
31712 first_attribute
= attributes
;
31713 /* Combine the attributes. */
31714 attributes
= attr_chainon (prefix_attributes
, attributes
);
31716 decl
= grokfield (declarator
, &declspecs
,
31717 NULL_TREE
, /*init_const_expr_p=*/false,
31718 NULL_TREE
, attributes
);
31720 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31721 return error_mark_node
;
31723 /* Reset PREFIX_ATTRIBUTES. */
31724 if (attributes
!= error_mark_node
)
31726 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31727 attributes
= TREE_CHAIN (attributes
);
31729 TREE_CHAIN (attributes
) = NULL_TREE
;
31732 DECL_CHAIN (decl
) = decls
;
31735 token
= cp_lexer_peek_token (parser
->lexer
);
31736 if (token
->type
== CPP_COMMA
)
31738 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31747 /* Parse an Objective-C @property declaration. The syntax is:
31749 objc-property-declaration:
31750 '@property' objc-property-attributes[opt] struct-declaration ;
31752 objc-property-attributes:
31753 '(' objc-property-attribute-list ')'
31755 objc-property-attribute-list:
31756 objc-property-attribute
31757 objc-property-attribute-list, objc-property-attribute
31759 objc-property-attribute
31760 'getter' = identifier
31761 'setter' = identifier
31770 @property NSString *name;
31771 @property (readonly) id object;
31772 @property (retain, nonatomic, getter=getTheName) id name;
31773 @property int a, b, c;
31775 PS: This function is identical to
31776 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31778 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31780 /* The following variables hold the attributes of the properties as
31781 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31782 seen. When we see an attribute, we set them to 'true' (if they
31783 are boolean properties) or to the identifier (if they have an
31784 argument, ie, for getter and setter). Note that here we only
31785 parse the list of attributes, check the syntax and accumulate the
31786 attributes that we find. objc_add_property_declaration() will
31787 then process the information. */
31788 bool property_assign
= false;
31789 bool property_copy
= false;
31790 tree property_getter_ident
= NULL_TREE
;
31791 bool property_nonatomic
= false;
31792 bool property_readonly
= false;
31793 bool property_readwrite
= false;
31794 bool property_retain
= false;
31795 tree property_setter_ident
= NULL_TREE
;
31797 /* 'properties' is the list of properties that we read. Usually a
31798 single one, but maybe more (eg, in "@property int a, b, c;" there
31803 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31805 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31807 /* Parse the optional attribute list... */
31808 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31811 matching_parens parens
;
31812 parens
.consume_open (parser
);
31816 bool syntax_error
= false;
31817 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31820 if (token
->type
!= CPP_NAME
)
31822 cp_parser_error (parser
, "expected identifier");
31825 keyword
= C_RID_CODE (token
->u
.value
);
31826 cp_lexer_consume_token (parser
->lexer
);
31829 case RID_ASSIGN
: property_assign
= true; break;
31830 case RID_COPY
: property_copy
= true; break;
31831 case RID_NONATOMIC
: property_nonatomic
= true; break;
31832 case RID_READONLY
: property_readonly
= true; break;
31833 case RID_READWRITE
: property_readwrite
= true; break;
31834 case RID_RETAIN
: property_retain
= true; break;
31838 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31840 if (keyword
== RID_GETTER
)
31841 cp_parser_error (parser
,
31842 "missing %<=%> (after %<getter%> attribute)");
31844 cp_parser_error (parser
,
31845 "missing %<=%> (after %<setter%> attribute)");
31846 syntax_error
= true;
31849 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31850 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31852 cp_parser_error (parser
, "expected identifier");
31853 syntax_error
= true;
31856 if (keyword
== RID_SETTER
)
31858 if (property_setter_ident
!= NULL_TREE
)
31860 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31861 cp_lexer_consume_token (parser
->lexer
);
31864 property_setter_ident
= cp_parser_objc_selector (parser
);
31865 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31866 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31868 cp_lexer_consume_token (parser
->lexer
);
31872 if (property_getter_ident
!= NULL_TREE
)
31874 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31875 cp_lexer_consume_token (parser
->lexer
);
31878 property_getter_ident
= cp_parser_objc_selector (parser
);
31882 cp_parser_error (parser
, "unknown property attribute");
31883 syntax_error
= true;
31890 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31891 cp_lexer_consume_token (parser
->lexer
);
31896 /* FIXME: "@property (setter, assign);" will generate a spurious
31897 "error: expected ‘)’ before ‘,’ token". This is because
31898 cp_parser_require, unlike the C counterpart, will produce an
31899 error even if we are in error recovery. */
31900 if (!parens
.require_close (parser
))
31902 cp_parser_skip_to_closing_parenthesis (parser
,
31903 /*recovering=*/true,
31904 /*or_comma=*/false,
31905 /*consume_paren=*/true);
31909 /* ... and the property declaration(s). */
31910 properties
= cp_parser_objc_struct_declaration (parser
);
31912 if (properties
== error_mark_node
)
31914 cp_parser_skip_to_end_of_statement (parser
);
31915 /* If the next token is now a `;', consume it. */
31916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31917 cp_lexer_consume_token (parser
->lexer
);
31921 if (properties
== NULL_TREE
)
31922 cp_parser_error (parser
, "expected identifier");
31925 /* Comma-separated properties are chained together in
31926 reverse order; add them one by one. */
31927 properties
= nreverse (properties
);
31929 for (; properties
; properties
= TREE_CHAIN (properties
))
31930 objc_add_property_declaration (loc
, copy_node (properties
),
31931 property_readonly
, property_readwrite
,
31932 property_assign
, property_retain
,
31933 property_copy
, property_nonatomic
,
31934 property_getter_ident
, property_setter_ident
);
31937 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31940 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31942 objc-synthesize-declaration:
31943 @synthesize objc-synthesize-identifier-list ;
31945 objc-synthesize-identifier-list:
31946 objc-synthesize-identifier
31947 objc-synthesize-identifier-list, objc-synthesize-identifier
31949 objc-synthesize-identifier
31951 identifier = identifier
31954 @synthesize MyProperty;
31955 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31957 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31958 for C. Keep them in sync.
31961 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31963 tree list
= NULL_TREE
;
31965 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31967 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31970 tree property
, ivar
;
31971 property
= cp_parser_identifier (parser
);
31972 if (property
== error_mark_node
)
31974 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31977 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31979 cp_lexer_consume_token (parser
->lexer
);
31980 ivar
= cp_parser_identifier (parser
);
31981 if (ivar
== error_mark_node
)
31983 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31989 list
= chainon (list
, build_tree_list (ivar
, property
));
31990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31991 cp_lexer_consume_token (parser
->lexer
);
31995 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31996 objc_add_synthesize_declaration (loc
, list
);
31999 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
32001 objc-dynamic-declaration:
32002 @dynamic identifier-list ;
32005 @dynamic MyProperty;
32006 @dynamic MyProperty, AnotherProperty;
32008 PS: This function is identical to c_parser_objc_at_dynamic_declaration
32009 for C. Keep them in sync.
32012 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
32014 tree list
= NULL_TREE
;
32016 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32018 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
32022 property
= cp_parser_identifier (parser
);
32023 if (property
== error_mark_node
)
32025 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32028 list
= chainon (list
, build_tree_list (NULL
, property
));
32029 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32030 cp_lexer_consume_token (parser
->lexer
);
32034 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32035 objc_add_dynamic_declaration (loc
, list
);
32039 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32041 /* Returns name of the next clause.
32042 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32043 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32044 returned and the token is consumed. */
32046 static pragma_omp_clause
32047 cp_parser_omp_clause_name (cp_parser
*parser
)
32049 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
32051 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32052 result
= PRAGMA_OACC_CLAUSE_AUTO
;
32053 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
32054 result
= PRAGMA_OMP_CLAUSE_IF
;
32055 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
32056 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
32057 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
32058 result
= PRAGMA_OACC_CLAUSE_DELETE
;
32059 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
32060 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
32061 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
32062 result
= PRAGMA_OMP_CLAUSE_FOR
;
32063 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32065 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32066 const char *p
= IDENTIFIER_POINTER (id
);
32071 if (!strcmp ("aligned", p
))
32072 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
32073 else if (!strcmp ("async", p
))
32074 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
32077 if (!strcmp ("collapse", p
))
32078 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
32079 else if (!strcmp ("copy", p
))
32080 result
= PRAGMA_OACC_CLAUSE_COPY
;
32081 else if (!strcmp ("copyin", p
))
32082 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
32083 else if (!strcmp ("copyout", p
))
32084 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
32085 else if (!strcmp ("copyprivate", p
))
32086 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
32087 else if (!strcmp ("create", p
))
32088 result
= PRAGMA_OACC_CLAUSE_CREATE
;
32091 if (!strcmp ("defaultmap", p
))
32092 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
32093 else if (!strcmp ("depend", p
))
32094 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
32095 else if (!strcmp ("device", p
))
32096 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
32097 else if (!strcmp ("deviceptr", p
))
32098 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
32099 else if (!strcmp ("device_resident", p
))
32100 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
32101 else if (!strcmp ("dist_schedule", p
))
32102 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
32105 if (!strcmp ("final", p
))
32106 result
= PRAGMA_OMP_CLAUSE_FINAL
;
32107 else if (!strcmp ("finalize", p
))
32108 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
32109 else if (!strcmp ("firstprivate", p
))
32110 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
32111 else if (!strcmp ("from", p
))
32112 result
= PRAGMA_OMP_CLAUSE_FROM
;
32115 if (!strcmp ("gang", p
))
32116 result
= PRAGMA_OACC_CLAUSE_GANG
;
32117 else if (!strcmp ("grainsize", p
))
32118 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
32121 if (!strcmp ("hint", p
))
32122 result
= PRAGMA_OMP_CLAUSE_HINT
;
32123 else if (!strcmp ("host", p
))
32124 result
= PRAGMA_OACC_CLAUSE_HOST
;
32127 if (!strcmp ("if_present", p
))
32128 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
32129 else if (!strcmp ("in_reduction", p
))
32130 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
32131 else if (!strcmp ("inbranch", p
))
32132 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
32133 else if (!strcmp ("independent", p
))
32134 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
32135 else if (!strcmp ("is_device_ptr", p
))
32136 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
32139 if (!strcmp ("lastprivate", p
))
32140 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
32141 else if (!strcmp ("linear", p
))
32142 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
32143 else if (!strcmp ("link", p
))
32144 result
= PRAGMA_OMP_CLAUSE_LINK
;
32147 if (!strcmp ("map", p
))
32148 result
= PRAGMA_OMP_CLAUSE_MAP
;
32149 else if (!strcmp ("mergeable", p
))
32150 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
32153 if (!strcmp ("nogroup", p
))
32154 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
32155 else if (!strcmp ("nontemporal", p
))
32156 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
32157 else if (!strcmp ("notinbranch", p
))
32158 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
32159 else if (!strcmp ("nowait", p
))
32160 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
32161 else if (!strcmp ("num_gangs", p
))
32162 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
32163 else if (!strcmp ("num_tasks", p
))
32164 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
32165 else if (!strcmp ("num_teams", p
))
32166 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
32167 else if (!strcmp ("num_threads", p
))
32168 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
32169 else if (!strcmp ("num_workers", p
))
32170 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
32173 if (!strcmp ("ordered", p
))
32174 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
32177 if (!strcmp ("parallel", p
))
32178 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
32179 else if (!strcmp ("present", p
))
32180 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
32181 else if (!strcmp ("present_or_copy", p
)
32182 || !strcmp ("pcopy", p
))
32183 result
= PRAGMA_OACC_CLAUSE_COPY
;
32184 else if (!strcmp ("present_or_copyin", p
)
32185 || !strcmp ("pcopyin", p
))
32186 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
32187 else if (!strcmp ("present_or_copyout", p
)
32188 || !strcmp ("pcopyout", p
))
32189 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
32190 else if (!strcmp ("present_or_create", p
)
32191 || !strcmp ("pcreate", p
))
32192 result
= PRAGMA_OACC_CLAUSE_CREATE
;
32193 else if (!strcmp ("priority", p
))
32194 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
32195 else if (!strcmp ("proc_bind", p
))
32196 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
32199 if (!strcmp ("reduction", p
))
32200 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
32203 if (!strcmp ("safelen", p
))
32204 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
32205 else if (!strcmp ("schedule", p
))
32206 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
32207 else if (!strcmp ("sections", p
))
32208 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
32209 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
32210 result
= PRAGMA_OACC_CLAUSE_HOST
;
32211 else if (!strcmp ("seq", p
))
32212 result
= PRAGMA_OACC_CLAUSE_SEQ
;
32213 else if (!strcmp ("shared", p
))
32214 result
= PRAGMA_OMP_CLAUSE_SHARED
;
32215 else if (!strcmp ("simd", p
))
32216 result
= PRAGMA_OMP_CLAUSE_SIMD
;
32217 else if (!strcmp ("simdlen", p
))
32218 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
32221 if (!strcmp ("task_reduction", p
))
32222 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
32223 else if (!strcmp ("taskgroup", p
))
32224 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
32225 else if (!strcmp ("thread_limit", p
))
32226 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
32227 else if (!strcmp ("threads", p
))
32228 result
= PRAGMA_OMP_CLAUSE_THREADS
;
32229 else if (!strcmp ("tile", p
))
32230 result
= PRAGMA_OACC_CLAUSE_TILE
;
32231 else if (!strcmp ("to", p
))
32232 result
= PRAGMA_OMP_CLAUSE_TO
;
32235 if (!strcmp ("uniform", p
))
32236 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
32237 else if (!strcmp ("untied", p
))
32238 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
32239 else if (!strcmp ("use_device", p
))
32240 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
32241 else if (!strcmp ("use_device_ptr", p
))
32242 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
32245 if (!strcmp ("vector", p
))
32246 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
32247 else if (!strcmp ("vector_length", p
))
32248 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
32251 if (!strcmp ("wait", p
))
32252 result
= PRAGMA_OACC_CLAUSE_WAIT
;
32253 else if (!strcmp ("worker", p
))
32254 result
= PRAGMA_OACC_CLAUSE_WORKER
;
32259 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
32260 cp_lexer_consume_token (parser
->lexer
);
32265 /* Validate that a clause of the given type does not already exist. */
32268 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
32269 const char *name
, location_t location
)
32273 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32274 if (OMP_CLAUSE_CODE (c
) == code
)
32276 error_at (location
, "too many %qs clauses", name
);
32284 variable-list , identifier
32286 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32287 colon). An opening parenthesis will have been consumed by the caller.
32289 If KIND is nonzero, create the appropriate node and install the decl
32290 in OMP_CLAUSE_DECL and add the node to the head of the list.
32292 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32293 return the list created.
32295 COLON can be NULL if only closing parenthesis should end the list,
32296 or pointer to bool which will receive false if the list is terminated
32297 by closing parenthesis or true if the list is terminated by colon. */
32300 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
32301 tree list
, bool *colon
)
32304 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32307 parser
->colon_corrects_to_scope_p
= false;
32314 if (kind
== OMP_CLAUSE_DEPEND
)
32315 cp_parser_parse_tentatively (parser
);
32316 token
= cp_lexer_peek_token (parser
->lexer
);
32318 && current_class_ptr
32319 && cp_parser_is_keyword (token
, RID_THIS
))
32321 decl
= finish_this_expr ();
32322 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
32323 || CONVERT_EXPR_P (decl
))
32324 decl
= TREE_OPERAND (decl
, 0);
32325 cp_lexer_consume_token (parser
->lexer
);
32329 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
32330 /*check_dependency_p=*/true,
32331 /*template_p=*/NULL
,
32332 /*declarator_p=*/false,
32333 /*optional_p=*/false);
32334 if (name
== error_mark_node
)
32336 if (kind
== OMP_CLAUSE_DEPEND
32337 && cp_parser_simulate_error (parser
))
32338 goto depend_lvalue
;
32342 if (identifier_p (name
))
32343 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
32346 if (decl
== error_mark_node
)
32348 if (kind
== OMP_CLAUSE_DEPEND
32349 && cp_parser_simulate_error (parser
))
32350 goto depend_lvalue
;
32351 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
32355 if (decl
== error_mark_node
)
32357 else if (kind
!= 0)
32361 case OMP_CLAUSE__CACHE_
:
32362 /* The OpenACC cache directive explicitly only allows "array
32363 elements or subarrays". */
32364 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
32366 error_at (token
->location
, "expected %<[%>");
32367 decl
= error_mark_node
;
32371 case OMP_CLAUSE_MAP
:
32372 case OMP_CLAUSE_FROM
:
32373 case OMP_CLAUSE_TO
:
32374 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
32377 = cp_lexer_peek_token (parser
->lexer
)->location
;
32378 cp_id_kind idk
= CP_ID_KIND_NONE
;
32379 cp_lexer_consume_token (parser
->lexer
);
32380 decl
= convert_from_reference (decl
);
32382 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
32387 case OMP_CLAUSE_DEPEND
:
32388 case OMP_CLAUSE_REDUCTION
:
32389 case OMP_CLAUSE_IN_REDUCTION
:
32390 case OMP_CLAUSE_TASK_REDUCTION
:
32391 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
32393 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
32395 parser
->colon_corrects_to_scope_p
= false;
32396 cp_lexer_consume_token (parser
->lexer
);
32397 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32398 low_bound
= cp_parser_expression (parser
);
32400 parser
->colon_corrects_to_scope_p
32401 = saved_colon_corrects_to_scope_p
;
32402 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
32403 length
= integer_one_node
;
32406 /* Look for `:'. */
32407 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32409 if (kind
== OMP_CLAUSE_DEPEND
32410 && cp_parser_simulate_error (parser
))
32411 goto depend_lvalue
;
32414 if (kind
== OMP_CLAUSE_DEPEND
)
32415 cp_parser_commit_to_tentative_parse (parser
);
32416 if (!cp_lexer_next_token_is (parser
->lexer
,
32418 length
= cp_parser_expression (parser
);
32420 /* Look for the closing `]'. */
32421 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
32424 if (kind
== OMP_CLAUSE_DEPEND
32425 && cp_parser_simulate_error (parser
))
32426 goto depend_lvalue
;
32430 decl
= tree_cons (low_bound
, length
, decl
);
32437 if (kind
== OMP_CLAUSE_DEPEND
)
32439 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
32440 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
32441 && cp_parser_simulate_error (parser
))
32444 cp_parser_abort_tentative_parse (parser
);
32445 decl
= cp_parser_assignment_expression (parser
, NULL
,
32449 cp_parser_parse_definitely (parser
);
32452 tree u
= build_omp_clause (token
->location
, kind
);
32453 OMP_CLAUSE_DECL (u
) = decl
;
32454 OMP_CLAUSE_CHAIN (u
) = list
;
32458 list
= tree_cons (decl
, NULL_TREE
, list
);
32461 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
32463 cp_lexer_consume_token (parser
->lexer
);
32467 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32469 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32472 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32476 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32480 /* Try to resync to an unnested comma. Copied from
32481 cp_parser_parenthesized_expression_list. */
32484 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32485 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
32486 /*recovering=*/true,
32488 /*consume_paren=*/true);
32496 /* Similarly, but expect leading and trailing parenthesis. This is a very
32497 common case for omp clauses. */
32500 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
32502 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32503 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
32508 copy ( variable-list )
32509 copyin ( variable-list )
32510 copyout ( variable-list )
32511 create ( variable-list )
32512 delete ( variable-list )
32513 present ( variable-list ) */
32516 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
32519 enum gomp_map_kind kind
;
32522 case PRAGMA_OACC_CLAUSE_COPY
:
32523 kind
= GOMP_MAP_TOFROM
;
32525 case PRAGMA_OACC_CLAUSE_COPYIN
:
32526 kind
= GOMP_MAP_TO
;
32528 case PRAGMA_OACC_CLAUSE_COPYOUT
:
32529 kind
= GOMP_MAP_FROM
;
32531 case PRAGMA_OACC_CLAUSE_CREATE
:
32532 kind
= GOMP_MAP_ALLOC
;
32534 case PRAGMA_OACC_CLAUSE_DELETE
:
32535 kind
= GOMP_MAP_RELEASE
;
32537 case PRAGMA_OACC_CLAUSE_DEVICE
:
32538 kind
= GOMP_MAP_FORCE_TO
;
32540 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
32541 kind
= GOMP_MAP_DEVICE_RESIDENT
;
32543 case PRAGMA_OACC_CLAUSE_HOST
:
32544 kind
= GOMP_MAP_FORCE_FROM
;
32546 case PRAGMA_OACC_CLAUSE_LINK
:
32547 kind
= GOMP_MAP_LINK
;
32549 case PRAGMA_OACC_CLAUSE_PRESENT
:
32550 kind
= GOMP_MAP_FORCE_PRESENT
;
32553 gcc_unreachable ();
32556 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
32558 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32559 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32565 deviceptr ( variable-list ) */
32568 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
32570 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32573 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32574 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32575 variable-list must only allow for pointer variables. */
32576 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
32577 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
32579 tree v
= TREE_PURPOSE (t
);
32580 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
32581 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
32582 OMP_CLAUSE_DECL (u
) = v
;
32583 OMP_CLAUSE_CHAIN (u
) = list
;
32598 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
32599 enum omp_clause_code code
,
32600 tree list
, location_t location
)
32602 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32603 tree c
= build_omp_clause (location
, code
);
32604 OMP_CLAUSE_CHAIN (c
) = list
;
32609 num_gangs ( expression )
32610 num_workers ( expression )
32611 vector_length ( expression ) */
32614 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
32615 const char *str
, tree list
)
32617 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32619 matching_parens parens
;
32620 if (!parens
.require_open (parser
))
32623 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
32625 if (t
== error_mark_node
32626 || !parens
.require_close (parser
))
32628 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32629 /*or_comma=*/false,
32630 /*consume_paren=*/true);
32634 check_no_duplicate_clause (list
, code
, str
, loc
);
32636 tree c
= build_omp_clause (loc
, code
);
32637 OMP_CLAUSE_OPERAND (c
, 0) = t
;
32638 OMP_CLAUSE_CHAIN (c
) = list
;
32644 gang [( gang-arg-list )]
32645 worker [( [num:] int-expr )]
32646 vector [( [length:] int-expr )]
32648 where gang-arg is one of:
32653 and size-expr may be:
32660 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
32661 const char *str
, tree list
)
32663 const char *id
= "num";
32664 cp_lexer
*lexer
= parser
->lexer
;
32665 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
32666 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
32668 if (kind
== OMP_CLAUSE_VECTOR
)
32671 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
32673 matching_parens parens
;
32674 parens
.consume_open (parser
);
32678 cp_token
*next
= cp_lexer_peek_token (lexer
);
32681 /* Gang static argument. */
32682 if (kind
== OMP_CLAUSE_GANG
32683 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
32685 cp_lexer_consume_token (lexer
);
32687 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32688 goto cleanup_error
;
32691 if (ops
[idx
] != NULL
)
32693 cp_parser_error (parser
, "too many %<static%> arguments");
32694 goto cleanup_error
;
32697 /* Check for the '*' argument. */
32698 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
32699 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32700 || cp_lexer_nth_token_is (parser
->lexer
, 2,
32703 cp_lexer_consume_token (lexer
);
32704 ops
[idx
] = integer_minus_one_node
;
32706 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32708 cp_lexer_consume_token (lexer
);
32714 /* Worker num: argument and vector length: arguments. */
32715 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
32716 && id_equal (next
->u
.value
, id
)
32717 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
32719 cp_lexer_consume_token (lexer
); /* id */
32720 cp_lexer_consume_token (lexer
); /* ':' */
32723 /* Now collect the actual argument. */
32724 if (ops
[idx
] != NULL_TREE
)
32726 cp_parser_error (parser
, "unexpected argument");
32727 goto cleanup_error
;
32730 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
32732 if (expr
== error_mark_node
)
32733 goto cleanup_error
;
32735 mark_exp_read (expr
);
32738 if (kind
== OMP_CLAUSE_GANG
32739 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32741 cp_lexer_consume_token (lexer
);
32748 if (!parens
.require_close (parser
))
32749 goto cleanup_error
;
32752 check_no_duplicate_clause (list
, kind
, str
, loc
);
32754 c
= build_omp_clause (loc
, kind
);
32757 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32759 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32760 OMP_CLAUSE_CHAIN (c
) = list
;
32765 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32770 tile ( size-expr-list ) */
32773 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32775 tree c
, expr
= error_mark_node
;
32776 tree tile
= NULL_TREE
;
32778 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32779 so, but the spec authors never considered such a case and have
32780 differing opinions on what it might mean, including 'not
32782 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32783 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32786 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32791 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32795 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32796 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32798 cp_lexer_consume_token (parser
->lexer
);
32799 expr
= integer_zero_node
;
32802 expr
= cp_parser_constant_expression (parser
);
32804 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32806 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32808 /* Consume the trailing ')'. */
32809 cp_lexer_consume_token (parser
->lexer
);
32811 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32812 tile
= nreverse (tile
);
32813 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32814 OMP_CLAUSE_CHAIN (c
) = list
;
32819 Parse wait clause or directive parameters. */
32822 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32824 vec
<tree
, va_gc
> *args
;
32827 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32829 /*allow_expansion_p=*/true,
32830 /*non_constant_p=*/NULL
);
32832 if (args
== NULL
|| args
->length () == 0)
32836 cp_parser_error (parser
, "expected integer expression list");
32837 release_tree_vector (args
);
32842 args_tree
= build_tree_list_vec (args
);
32844 release_tree_vector (args
);
32846 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32848 tree targ
= TREE_VALUE (t
);
32850 if (targ
!= error_mark_node
)
32852 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32853 error ("%<wait%> expression must be integral");
32856 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32858 targ
= mark_rvalue_use (targ
);
32859 OMP_CLAUSE_DECL (c
) = targ
;
32860 OMP_CLAUSE_CHAIN (c
) = list
;
32870 wait ( int-expr-list ) */
32873 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32875 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32877 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32880 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32886 collapse ( constant-expression ) */
32889 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32895 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32896 matching_parens parens
;
32897 if (!parens
.require_open (parser
))
32900 num
= cp_parser_constant_expression (parser
);
32902 if (!parens
.require_close (parser
))
32903 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32904 /*or_comma=*/false,
32905 /*consume_paren=*/true);
32907 if (num
== error_mark_node
)
32909 num
= fold_non_dependent_expr (num
);
32910 if (!tree_fits_shwi_p (num
)
32911 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32912 || (n
= tree_to_shwi (num
)) <= 0
32915 error_at (loc
, "collapse argument needs positive constant integer expression");
32919 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32920 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32921 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32922 OMP_CLAUSE_CHAIN (c
) = list
;
32923 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32929 default ( none | shared )
32932 default ( none | present ) */
32935 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32936 location_t location
, bool is_oacc
)
32938 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32941 matching_parens parens
;
32942 if (!parens
.require_open (parser
))
32944 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32946 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32947 const char *p
= IDENTIFIER_POINTER (id
);
32952 if (strcmp ("none", p
) != 0)
32954 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32958 if (strcmp ("present", p
) != 0 || !is_oacc
)
32960 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32964 if (strcmp ("shared", p
) != 0 || is_oacc
)
32966 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32973 cp_lexer_consume_token (parser
->lexer
);
32979 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32981 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32984 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32985 || !parens
.require_close (parser
))
32986 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32987 /*or_comma=*/false,
32988 /*consume_paren=*/true);
32990 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32993 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32994 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32995 OMP_CLAUSE_CHAIN (c
) = list
;
32996 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
33002 final ( expression ) */
33005 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
33009 matching_parens parens
;
33010 if (!parens
.require_open (parser
))
33013 t
= cp_parser_assignment_expression (parser
);
33015 if (t
== error_mark_node
33016 || !parens
.require_close (parser
))
33017 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33018 /*or_comma=*/false,
33019 /*consume_paren=*/true);
33021 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
33023 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
33024 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
33025 OMP_CLAUSE_CHAIN (c
) = list
;
33034 if ( directive-name-modifier : expression )
33036 directive-name-modifier:
33037 parallel | task | taskloop | target data | target | target update
33038 | target enter data | target exit data
33041 directive-name-modifier:
33042 ... | simd | cancel */
33045 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
33049 enum tree_code if_modifier
= ERROR_MARK
;
33051 matching_parens parens
;
33052 if (!parens
.require_open (parser
))
33055 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33057 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33058 const char *p
= IDENTIFIER_POINTER (id
);
33061 if (strcmp ("cancel", p
) == 0)
33062 if_modifier
= VOID_CST
;
33063 else if (strcmp ("parallel", p
) == 0)
33064 if_modifier
= OMP_PARALLEL
;
33065 else if (strcmp ("simd", p
) == 0)
33066 if_modifier
= OMP_SIMD
;
33067 else if (strcmp ("task", p
) == 0)
33068 if_modifier
= OMP_TASK
;
33069 else if (strcmp ("taskloop", p
) == 0)
33070 if_modifier
= OMP_TASKLOOP
;
33071 else if (strcmp ("target", p
) == 0)
33073 if_modifier
= OMP_TARGET
;
33074 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
33076 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
33077 p
= IDENTIFIER_POINTER (id
);
33078 if (strcmp ("data", p
) == 0)
33079 if_modifier
= OMP_TARGET_DATA
;
33080 else if (strcmp ("update", p
) == 0)
33081 if_modifier
= OMP_TARGET_UPDATE
;
33082 else if (strcmp ("enter", p
) == 0)
33083 if_modifier
= OMP_TARGET_ENTER_DATA
;
33084 else if (strcmp ("exit", p
) == 0)
33085 if_modifier
= OMP_TARGET_EXIT_DATA
;
33086 if (if_modifier
!= OMP_TARGET
)
33091 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
33092 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
33094 if_modifier
= ERROR_MARK
;
33096 if (if_modifier
== OMP_TARGET_ENTER_DATA
33097 || if_modifier
== OMP_TARGET_EXIT_DATA
)
33099 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
33101 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
33102 p
= IDENTIFIER_POINTER (id
);
33103 if (strcmp ("data", p
) == 0)
33109 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
33110 error_at (loc
, "expected %<data%>");
33111 if_modifier
= ERROR_MARK
;
33116 if (if_modifier
!= ERROR_MARK
)
33118 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
33121 cp_lexer_consume_token (parser
->lexer
);
33128 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
33129 error_at (loc
, "expected %<:%>");
33131 if_modifier
= ERROR_MARK
;
33136 t
= cp_parser_assignment_expression (parser
);
33138 if (t
== error_mark_node
33139 || !parens
.require_close (parser
))
33140 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33141 /*or_comma=*/false,
33142 /*consume_paren=*/true);
33144 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33145 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
33147 if (if_modifier
!= ERROR_MARK
33148 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33150 const char *p
= NULL
;
33151 switch (if_modifier
)
33153 case VOID_CST
: p
= "cancel"; break;
33154 case OMP_PARALLEL
: p
= "parallel"; break;
33155 case OMP_SIMD
: p
= "simd"; break;
33156 case OMP_TASK
: p
= "task"; break;
33157 case OMP_TASKLOOP
: p
= "taskloop"; break;
33158 case OMP_TARGET_DATA
: p
= "target data"; break;
33159 case OMP_TARGET
: p
= "target"; break;
33160 case OMP_TARGET_UPDATE
: p
= "target update"; break;
33161 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
33162 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
33163 default: gcc_unreachable ();
33165 error_at (location
, "too many %<if%> clauses with %qs modifier",
33169 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33172 error_at (location
, "too many %<if%> clauses");
33174 error_at (location
, "too many %<if%> clauses without modifier");
33177 else if (if_modifier
== ERROR_MARK
33178 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
33180 error_at (location
, "if any %<if%> clause has modifier, then all "
33181 "%<if%> clauses have to use modifier");
33186 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
33187 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
33188 OMP_CLAUSE_IF_EXPR (c
) = t
;
33189 OMP_CLAUSE_CHAIN (c
) = list
;
33198 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
33199 tree list
, location_t location
)
33203 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
33206 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
33207 OMP_CLAUSE_CHAIN (c
) = list
;
33215 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
33216 tree list
, location_t location
)
33220 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
33222 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
33223 OMP_CLAUSE_CHAIN (c
) = list
;
33228 num_threads ( expression ) */
33231 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
33232 location_t location
)
33236 matching_parens parens
;
33237 if (!parens
.require_open (parser
))
33240 t
= cp_parser_assignment_expression (parser
);
33242 if (t
== error_mark_node
33243 || !parens
.require_close (parser
))
33244 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33245 /*or_comma=*/false,
33246 /*consume_paren=*/true);
33248 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
33249 "num_threads", location
);
33251 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
33252 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
33253 OMP_CLAUSE_CHAIN (c
) = list
;
33259 num_tasks ( expression ) */
33262 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
33263 location_t location
)
33267 matching_parens parens
;
33268 if (!parens
.require_open (parser
))
33271 t
= cp_parser_assignment_expression (parser
);
33273 if (t
== error_mark_node
33274 || !parens
.require_close (parser
))
33275 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33276 /*or_comma=*/false,
33277 /*consume_paren=*/true);
33279 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
33280 "num_tasks", location
);
33282 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
33283 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
33284 OMP_CLAUSE_CHAIN (c
) = list
;
33290 grainsize ( expression ) */
33293 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
33294 location_t location
)
33298 matching_parens parens
;
33299 if (!parens
.require_open (parser
))
33302 t
= cp_parser_assignment_expression (parser
);
33304 if (t
== error_mark_node
33305 || !parens
.require_close (parser
))
33306 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33307 /*or_comma=*/false,
33308 /*consume_paren=*/true);
33310 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
33311 "grainsize", location
);
33313 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
33314 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
33315 OMP_CLAUSE_CHAIN (c
) = list
;
33321 priority ( expression ) */
33324 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
33325 location_t location
)
33329 matching_parens parens
;
33330 if (!parens
.require_open (parser
))
33333 t
= cp_parser_assignment_expression (parser
);
33335 if (t
== error_mark_node
33336 || !parens
.require_close (parser
))
33337 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33338 /*or_comma=*/false,
33339 /*consume_paren=*/true);
33341 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
33342 "priority", location
);
33344 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
33345 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
33346 OMP_CLAUSE_CHAIN (c
) = list
;
33352 hint ( expression ) */
33355 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
33359 matching_parens parens
;
33360 if (!parens
.require_open (parser
))
33363 t
= cp_parser_assignment_expression (parser
);
33365 if (t
== error_mark_node
33366 || !parens
.require_close (parser
))
33367 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33368 /*or_comma=*/false,
33369 /*consume_paren=*/true);
33371 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
33373 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
33374 OMP_CLAUSE_HINT_EXPR (c
) = t
;
33375 OMP_CLAUSE_CHAIN (c
) = list
;
33381 defaultmap ( tofrom : scalar )
33384 defaultmap ( implicit-behavior [ : variable-category ] ) */
33387 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
33388 location_t location
)
33392 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33393 enum omp_clause_defaultmap_kind category
33394 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
33396 matching_parens parens
;
33397 if (!parens
.require_open (parser
))
33400 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
33402 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33405 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
33406 "%<tofrom%>, %<firstprivate%>, %<none%> "
33412 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33413 p
= IDENTIFIER_POINTER (id
);
33419 if (strcmp ("alloc", p
) == 0)
33420 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
33422 goto invalid_behavior
;
33426 if (strcmp ("default", p
) == 0)
33427 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33429 goto invalid_behavior
;
33433 if (strcmp ("firstprivate", p
) == 0)
33434 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
33435 else if (strcmp ("from", p
) == 0)
33436 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
33438 goto invalid_behavior
;
33442 if (strcmp ("none", p
) == 0)
33443 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
33445 goto invalid_behavior
;
33449 if (strcmp ("tofrom", p
) == 0)
33450 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
33451 else if (strcmp ("to", p
) == 0)
33452 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
33454 goto invalid_behavior
;
33458 goto invalid_behavior
;
33460 cp_lexer_consume_token (parser
->lexer
);
33462 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33464 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33467 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33470 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
33474 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33475 p
= IDENTIFIER_POINTER (id
);
33480 if (strcmp ("aggregate", p
) == 0)
33481 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
33483 goto invalid_category
;
33487 if (strcmp ("pointer", p
) == 0)
33488 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
33490 goto invalid_category
;
33494 if (strcmp ("scalar", p
) == 0)
33495 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
33497 goto invalid_category
;
33501 goto invalid_category
;
33504 cp_lexer_consume_token (parser
->lexer
);
33506 if (!parens
.require_close (parser
))
33509 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33510 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
33511 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33512 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
33513 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
33514 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
33516 enum omp_clause_defaultmap_kind cat
= category
;
33517 location_t loc
= OMP_CLAUSE_LOCATION (c
);
33518 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
33519 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
33523 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
33526 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
33529 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
33532 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
33536 gcc_unreachable ();
33539 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
33542 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
33547 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
33548 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
33549 OMP_CLAUSE_CHAIN (c
) = list
;
33553 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33554 /*or_comma=*/false,
33555 /*consume_paren=*/true);
33563 ordered ( constant-expression ) */
33566 cp_parser_omp_clause_ordered (cp_parser
*parser
,
33567 tree list
, location_t location
)
33569 tree c
, num
= NULL_TREE
;
33572 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
33573 "ordered", location
);
33575 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33577 matching_parens parens
;
33578 parens
.consume_open (parser
);
33580 num
= cp_parser_constant_expression (parser
);
33582 if (!parens
.require_close (parser
))
33583 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33584 /*or_comma=*/false,
33585 /*consume_paren=*/true);
33587 if (num
== error_mark_node
)
33589 num
= fold_non_dependent_expr (num
);
33590 if (!tree_fits_shwi_p (num
)
33591 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33592 || (n
= tree_to_shwi (num
)) <= 0
33595 error_at (location
,
33596 "ordered argument needs positive constant integer "
33602 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
33603 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
33604 OMP_CLAUSE_CHAIN (c
) = list
;
33609 reduction ( reduction-operator : variable-list )
33611 reduction-operator:
33612 One of: + * - & ^ | && ||
33616 reduction-operator:
33617 One of: + * - & ^ | && || min max
33621 reduction-operator:
33622 One of: + * - & ^ | && ||
33626 reduction ( reduction-modifier, reduction-operator : variable-list )
33627 in_reduction ( reduction-operator : variable-list )
33628 task_reduction ( reduction-operator : variable-list ) */
33631 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
33632 bool is_omp
, tree list
)
33634 enum tree_code code
= ERROR_MARK
;
33635 tree nlist
, c
, id
= NULL_TREE
;
33637 bool inscan
= false;
33639 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33642 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
33644 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
33645 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33647 cp_lexer_consume_token (parser
->lexer
);
33648 cp_lexer_consume_token (parser
->lexer
);
33650 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33651 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33653 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33654 const char *p
= IDENTIFIER_POINTER (id
);
33655 if (strcmp (p
, "task") == 0)
33657 else if (strcmp (p
, "inscan") == 0)
33660 sorry ("%<inscan%> modifier on %<reduction%> clause "
33661 "not supported yet");
33663 if (task
|| inscan
)
33665 cp_lexer_consume_token (parser
->lexer
);
33666 cp_lexer_consume_token (parser
->lexer
);
33671 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33673 case CPP_PLUS
: code
= PLUS_EXPR
; break;
33674 case CPP_MULT
: code
= MULT_EXPR
; break;
33675 case CPP_MINUS
: code
= MINUS_EXPR
; break;
33676 case CPP_AND
: code
= BIT_AND_EXPR
; break;
33677 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
33678 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
33679 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
33680 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
33684 if (code
!= ERROR_MARK
)
33685 cp_lexer_consume_token (parser
->lexer
);
33688 bool saved_colon_corrects_to_scope_p
;
33689 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33690 parser
->colon_corrects_to_scope_p
= false;
33691 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
33692 /*check_dependency_p=*/true,
33693 /*template_p=*/NULL
,
33694 /*declarator_p=*/false,
33695 /*optional_p=*/false);
33696 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33697 if (identifier_p (id
))
33699 const char *p
= IDENTIFIER_POINTER (id
);
33701 if (strcmp (p
, "min") == 0)
33703 else if (strcmp (p
, "max") == 0)
33705 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
33707 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
33709 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
33711 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
33712 code
= BIT_AND_EXPR
;
33713 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
33714 code
= BIT_IOR_EXPR
;
33715 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
33716 code
= BIT_XOR_EXPR
;
33717 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
33718 code
= TRUTH_ANDIF_EXPR
;
33719 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
33720 code
= TRUTH_ORIF_EXPR
;
33721 id
= omp_reduction_id (code
, id
, NULL_TREE
);
33722 tree scope
= parser
->scope
;
33724 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
33725 parser
->scope
= NULL_TREE
;
33726 parser
->qualifying_scope
= NULL_TREE
;
33727 parser
->object_scope
= NULL_TREE
;
33731 error ("invalid reduction-identifier");
33733 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33734 /*or_comma=*/false,
33735 /*consume_paren=*/true);
33740 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33743 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
33745 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33747 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
33749 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
33751 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
33752 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
33759 schedule ( schedule-kind )
33760 schedule ( schedule-kind , expression )
33763 static | dynamic | guided | runtime | auto
33766 schedule ( schedule-modifier : schedule-kind )
33767 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33775 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
33778 int modifiers
= 0, nmodifiers
= 0;
33780 matching_parens parens
;
33781 if (!parens
.require_open (parser
))
33784 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
33786 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33788 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33789 const char *p
= IDENTIFIER_POINTER (id
);
33790 if (strcmp ("simd", p
) == 0)
33791 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
33792 else if (strcmp ("monotonic", p
) == 0)
33793 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
33794 else if (strcmp ("nonmonotonic", p
) == 0)
33795 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
33798 cp_lexer_consume_token (parser
->lexer
);
33799 if (nmodifiers
++ == 0
33800 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33801 cp_lexer_consume_token (parser
->lexer
);
33804 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33811 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33812 const char *p
= IDENTIFIER_POINTER (id
);
33817 if (strcmp ("dynamic", p
) != 0)
33819 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
33823 if (strcmp ("guided", p
) != 0)
33825 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
33829 if (strcmp ("runtime", p
) != 0)
33831 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
33838 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33839 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
33840 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
33841 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
33844 cp_lexer_consume_token (parser
->lexer
);
33846 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
33847 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33848 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33849 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33851 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33856 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33859 cp_lexer_consume_token (parser
->lexer
);
33861 token
= cp_lexer_peek_token (parser
->lexer
);
33862 t
= cp_parser_assignment_expression (parser
);
33864 if (t
== error_mark_node
)
33866 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
33867 error_at (token
->location
, "schedule %<runtime%> does not take "
33868 "a %<chunk_size%> parameter");
33869 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
33870 error_at (token
->location
, "schedule %<auto%> does not take "
33871 "a %<chunk_size%> parameter");
33873 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
33875 if (!parens
.require_close (parser
))
33878 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33881 OMP_CLAUSE_SCHEDULE_KIND (c
)
33882 = (enum omp_clause_schedule_kind
)
33883 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
33885 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
33886 OMP_CLAUSE_CHAIN (c
) = list
;
33890 cp_parser_error (parser
, "invalid schedule kind");
33892 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33893 /*or_comma=*/false,
33894 /*consume_paren=*/true);
33902 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
33903 tree list
, location_t location
)
33907 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
33909 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
33910 OMP_CLAUSE_CHAIN (c
) = list
;
33919 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
33920 tree list
, location_t location
)
33922 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33923 tree c
= build_omp_clause (location
, code
);
33924 OMP_CLAUSE_CHAIN (c
) = list
;
33935 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33936 enum omp_clause_code code
,
33937 tree list
, location_t location
)
33939 tree c
= build_omp_clause (location
, code
);
33940 OMP_CLAUSE_CHAIN (c
) = list
;
33948 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33949 tree list
, location_t location
)
33951 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33952 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33953 OMP_CLAUSE_CHAIN (c
) = list
;
33962 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33963 enum omp_clause_code code
,
33964 tree list
, location_t location
)
33966 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33967 tree c
= build_omp_clause (location
, code
);
33968 OMP_CLAUSE_CHAIN (c
) = list
;
33973 num_teams ( expression ) */
33976 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33977 location_t location
)
33981 matching_parens parens
;
33982 if (!parens
.require_open (parser
))
33985 t
= cp_parser_assignment_expression (parser
);
33987 if (t
== error_mark_node
33988 || !parens
.require_close (parser
))
33989 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33990 /*or_comma=*/false,
33991 /*consume_paren=*/true);
33993 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33994 "num_teams", location
);
33996 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33997 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33998 OMP_CLAUSE_CHAIN (c
) = list
;
34004 thread_limit ( expression ) */
34007 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
34008 location_t location
)
34012 matching_parens parens
;
34013 if (!parens
.require_open (parser
))
34016 t
= cp_parser_assignment_expression (parser
);
34018 if (t
== error_mark_node
34019 || !parens
.require_close (parser
))
34020 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34021 /*or_comma=*/false,
34022 /*consume_paren=*/true);
34024 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
34025 "thread_limit", location
);
34027 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
34028 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
34029 OMP_CLAUSE_CHAIN (c
) = list
;
34035 aligned ( variable-list )
34036 aligned ( variable-list : constant-expression ) */
34039 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
34041 tree nlist
, c
, alignment
= NULL_TREE
;
34044 matching_parens parens
;
34045 if (!parens
.require_open (parser
))
34048 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
34053 alignment
= cp_parser_constant_expression (parser
);
34055 if (!parens
.require_close (parser
))
34056 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34057 /*or_comma=*/false,
34058 /*consume_paren=*/true);
34060 if (alignment
== error_mark_node
)
34061 alignment
= NULL_TREE
;
34064 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34065 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
34071 lastprivate ( variable-list )
34074 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34077 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
34079 bool conditional
= false;
34081 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34084 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34085 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
34087 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34088 const char *p
= IDENTIFIER_POINTER (id
);
34090 if (strcmp ("conditional", p
) == 0)
34092 conditional
= true;
34093 cp_lexer_consume_token (parser
->lexer
);
34094 cp_lexer_consume_token (parser
->lexer
);
34098 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
34102 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34103 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
34108 linear ( variable-list )
34109 linear ( variable-list : expression )
34112 linear ( modifier ( variable-list ) )
34113 linear ( modifier ( variable-list ) : expression ) */
34116 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
34119 tree nlist
, c
, step
= integer_one_node
;
34121 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
34123 matching_parens parens
;
34124 if (!parens
.require_open (parser
))
34127 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34129 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34130 const char *p
= IDENTIFIER_POINTER (id
);
34132 if (strcmp ("ref", p
) == 0)
34133 kind
= OMP_CLAUSE_LINEAR_REF
;
34134 else if (strcmp ("val", p
) == 0)
34135 kind
= OMP_CLAUSE_LINEAR_VAL
;
34136 else if (strcmp ("uval", p
) == 0)
34137 kind
= OMP_CLAUSE_LINEAR_UVAL
;
34138 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
34139 cp_lexer_consume_token (parser
->lexer
);
34141 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
34144 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
34145 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
34149 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
34150 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
34152 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
34153 else if (!parens
.require_close (parser
))
34154 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34155 /*or_comma=*/false,
34156 /*consume_paren=*/true);
34163 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34164 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
34166 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34167 cp_parser_parse_tentatively (parser
);
34168 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
34169 /*check_dependency_p=*/true,
34170 /*template_p=*/NULL
,
34171 /*declarator_p=*/false,
34172 /*optional_p=*/false);
34173 if (step
!= error_mark_node
)
34174 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
34175 if (step
== error_mark_node
)
34178 cp_parser_abort_tentative_parse (parser
);
34180 else if (!cp_parser_parse_definitely (parser
))
34184 step
= cp_parser_assignment_expression (parser
);
34186 if (!parens
.require_close (parser
))
34187 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34188 /*or_comma=*/false,
34189 /*consume_paren=*/true);
34191 if (step
== error_mark_node
)
34195 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34197 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
34198 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
34205 safelen ( constant-expression ) */
34208 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
34209 location_t location
)
34213 matching_parens parens
;
34214 if (!parens
.require_open (parser
))
34217 t
= cp_parser_constant_expression (parser
);
34219 if (t
== error_mark_node
34220 || !parens
.require_close (parser
))
34221 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34222 /*or_comma=*/false,
34223 /*consume_paren=*/true);
34225 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
34227 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
34228 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
34229 OMP_CLAUSE_CHAIN (c
) = list
;
34235 simdlen ( constant-expression ) */
34238 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
34239 location_t location
)
34243 matching_parens parens
;
34244 if (!parens
.require_open (parser
))
34247 t
= cp_parser_constant_expression (parser
);
34249 if (t
== error_mark_node
34250 || !parens
.require_close (parser
))
34251 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34252 /*or_comma=*/false,
34253 /*consume_paren=*/true);
34255 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
34257 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
34258 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
34259 OMP_CLAUSE_CHAIN (c
) = list
;
34266 identifier [+/- integer]
34267 vec , identifier [+/- integer]
34271 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
34276 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34278 cp_parser_error (parser
, "expected identifier");
34282 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34284 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34285 tree t
, identifier
= cp_parser_identifier (parser
);
34286 tree addend
= NULL
;
34288 if (identifier
== error_mark_node
)
34289 t
= error_mark_node
;
34292 t
= cp_parser_lookup_name_simple
34293 (parser
, identifier
,
34294 cp_lexer_peek_token (parser
->lexer
)->location
);
34295 if (t
== error_mark_node
)
34296 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
34301 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
34303 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
34305 addend
= integer_zero_node
;
34306 goto add_to_vector
;
34308 cp_lexer_consume_token (parser
->lexer
);
34310 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
34312 cp_parser_error (parser
, "expected integer");
34316 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34317 if (TREE_CODE (addend
) != INTEGER_CST
)
34319 cp_parser_error (parser
, "expected integer");
34322 cp_lexer_consume_token (parser
->lexer
);
34325 if (t
!= error_mark_node
)
34327 vec
= tree_cons (addend
, t
, vec
);
34329 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
34332 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
34335 cp_lexer_consume_token (parser
->lexer
);
34338 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
34340 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
34341 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
34342 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
34343 OMP_CLAUSE_CHAIN (u
) = list
;
34350 iterators ( iterators-definition )
34352 iterators-definition:
34354 iterator-specifier , iterators-definition
34356 iterator-specifier:
34357 identifier = range-specification
34358 iterator-type identifier = range-specification
34360 range-specification:
34362 begin : end : step */
34365 cp_parser_omp_iterators (cp_parser
*parser
)
34367 tree ret
= NULL_TREE
, *last
= &ret
;
34368 cp_lexer_consume_token (parser
->lexer
);
34370 matching_parens parens
;
34371 if (!parens
.require_open (parser
))
34372 return error_mark_node
;
34374 bool saved_colon_corrects_to_scope_p
34375 = parser
->colon_corrects_to_scope_p
;
34376 bool saved_colon_doesnt_start_class_def_p
34377 = parser
->colon_doesnt_start_class_def_p
;
34382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34383 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
34384 iter_type
= integer_type_node
;
34387 const char *saved_message
34388 = parser
->type_definition_forbidden_message
;
34389 parser
->type_definition_forbidden_message
34390 = G_("types may not be defined in iterator type");
34392 iter_type
= cp_parser_type_id (parser
);
34394 parser
->type_definition_forbidden_message
= saved_message
;
34397 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34398 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34400 cp_parser_error (parser
, "expected identifier");
34404 tree id
= cp_parser_identifier (parser
);
34405 if (id
== error_mark_node
)
34408 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34411 parser
->colon_corrects_to_scope_p
= false;
34412 parser
->colon_doesnt_start_class_def_p
= true;
34413 tree begin
= cp_parser_assignment_expression (parser
);
34415 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34418 tree end
= cp_parser_assignment_expression (parser
);
34420 tree step
= integer_one_node
;
34421 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
34423 cp_lexer_consume_token (parser
->lexer
);
34424 step
= cp_parser_assignment_expression (parser
);
34427 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
34428 DECL_ARTIFICIAL (iter_var
) = 1;
34429 DECL_CONTEXT (iter_var
) = current_function_decl
;
34430 pushdecl (iter_var
);
34432 *last
= make_tree_vec (6);
34433 TREE_VEC_ELT (*last
, 0) = iter_var
;
34434 TREE_VEC_ELT (*last
, 1) = begin
;
34435 TREE_VEC_ELT (*last
, 2) = end
;
34436 TREE_VEC_ELT (*last
, 3) = step
;
34437 last
= &TREE_CHAIN (*last
);
34439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34441 cp_lexer_consume_token (parser
->lexer
);
34448 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34449 parser
->colon_doesnt_start_class_def_p
34450 = saved_colon_doesnt_start_class_def_p
;
34452 if (!parens
.require_close (parser
))
34453 cp_parser_skip_to_closing_parenthesis (parser
,
34454 /*recovering=*/true,
34455 /*or_comma=*/false,
34456 /*consume_paren=*/true);
34458 return ret
? ret
: error_mark_node
;
34462 depend ( depend-kind : variable-list )
34470 depend ( sink : vec )
34473 depend ( depend-modifier , depend-kind: variable-list )
34476 in | out | inout | mutexinoutset | depobj
34479 iterator ( iterators-definition ) */
34482 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
34484 tree nlist
, c
, iterators
= NULL_TREE
;
34485 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
34487 matching_parens parens
;
34488 if (!parens
.require_open (parser
))
34493 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34496 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34497 const char *p
= IDENTIFIER_POINTER (id
);
34499 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
34501 begin_scope (sk_omp
, NULL
);
34502 iterators
= cp_parser_omp_iterators (parser
);
34503 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
34506 if (strcmp ("in", p
) == 0)
34507 kind
= OMP_CLAUSE_DEPEND_IN
;
34508 else if (strcmp ("inout", p
) == 0)
34509 kind
= OMP_CLAUSE_DEPEND_INOUT
;
34510 else if (strcmp ("mutexinoutset", p
) == 0)
34511 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
34512 else if (strcmp ("out", p
) == 0)
34513 kind
= OMP_CLAUSE_DEPEND_OUT
;
34514 else if (strcmp ("depobj", p
) == 0)
34515 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
34516 else if (strcmp ("sink", p
) == 0)
34517 kind
= OMP_CLAUSE_DEPEND_SINK
;
34518 else if (strcmp ("source", p
) == 0)
34519 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
34526 cp_lexer_consume_token (parser
->lexer
);
34529 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
34531 poplevel (0, 1, 0);
34532 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
34533 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
34534 iterators
= NULL_TREE
;
34537 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
34539 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
34540 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34541 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
34542 OMP_CLAUSE_CHAIN (c
) = list
;
34543 if (!parens
.require_close (parser
))
34544 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34545 /*or_comma=*/false,
34546 /*consume_paren=*/true);
34550 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34553 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
34554 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
34557 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
34562 tree block
= poplevel (1, 1, 0);
34563 if (iterators
== error_mark_node
)
34564 iterators
= NULL_TREE
;
34566 TREE_VEC_ELT (iterators
, 5) = block
;
34569 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34571 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34573 OMP_CLAUSE_DECL (c
)
34574 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
34580 cp_parser_error (parser
, "invalid depend kind");
34583 poplevel (0, 1, 0);
34584 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34585 /*or_comma=*/false,
34586 /*consume_paren=*/true);
34591 map ( map-kind : variable-list )
34592 map ( variable-list )
34595 alloc | to | from | tofrom
34599 alloc | to | from | tofrom | release | delete
34601 map ( always [,] map-kind: variable-list ) */
34604 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
34607 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
34608 bool always
= false;
34610 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34615 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34616 const char *p
= IDENTIFIER_POINTER (id
);
34618 if (strcmp ("always", p
) == 0)
34621 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
34623 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
34624 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
34626 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
34630 cp_lexer_consume_token (parser
->lexer
);
34632 cp_lexer_consume_token (parser
->lexer
);
34637 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34638 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34640 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34641 const char *p
= IDENTIFIER_POINTER (id
);
34643 if (strcmp ("alloc", p
) == 0)
34644 kind
= GOMP_MAP_ALLOC
;
34645 else if (strcmp ("to", p
) == 0)
34646 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
34647 else if (strcmp ("from", p
) == 0)
34648 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
34649 else if (strcmp ("tofrom", p
) == 0)
34650 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
34651 else if (strcmp ("release", p
) == 0)
34652 kind
= GOMP_MAP_RELEASE
;
34655 cp_parser_error (parser
, "invalid map kind");
34656 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34657 /*or_comma=*/false,
34658 /*consume_paren=*/true);
34661 cp_lexer_consume_token (parser
->lexer
);
34662 cp_lexer_consume_token (parser
->lexer
);
34664 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
34665 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34667 kind
= GOMP_MAP_DELETE
;
34668 cp_lexer_consume_token (parser
->lexer
);
34669 cp_lexer_consume_token (parser
->lexer
);
34672 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
34675 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34676 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
34682 device ( expression ) */
34685 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
34686 location_t location
)
34690 matching_parens parens
;
34691 if (!parens
.require_open (parser
))
34694 t
= cp_parser_assignment_expression (parser
);
34696 if (t
== error_mark_node
34697 || !parens
.require_close (parser
))
34698 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34699 /*or_comma=*/false,
34700 /*consume_paren=*/true);
34702 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
34703 "device", location
);
34705 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
34706 OMP_CLAUSE_DEVICE_ID (c
) = t
;
34707 OMP_CLAUSE_CHAIN (c
) = list
;
34713 dist_schedule ( static )
34714 dist_schedule ( static , expression ) */
34717 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
34718 location_t location
)
34722 matching_parens parens
;
34723 if (!parens
.require_open (parser
))
34726 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
34728 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
34730 cp_lexer_consume_token (parser
->lexer
);
34732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34734 cp_lexer_consume_token (parser
->lexer
);
34736 t
= cp_parser_assignment_expression (parser
);
34738 if (t
== error_mark_node
)
34740 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
34742 if (!parens
.require_close (parser
))
34745 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34748 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
34750 OMP_CLAUSE_CHAIN (c
) = list
;
34754 cp_parser_error (parser
, "invalid dist_schedule kind");
34756 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34757 /*or_comma=*/false,
34758 /*consume_paren=*/true);
34763 proc_bind ( proc-bind-kind )
34766 master | close | spread */
34769 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
34770 location_t location
)
34773 enum omp_clause_proc_bind_kind kind
;
34775 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34778 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34780 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34781 const char *p
= IDENTIFIER_POINTER (id
);
34783 if (strcmp ("master", p
) == 0)
34784 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
34785 else if (strcmp ("close", p
) == 0)
34786 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
34787 else if (strcmp ("spread", p
) == 0)
34788 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
34795 cp_lexer_consume_token (parser
->lexer
);
34796 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34799 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
34800 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
34802 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
34803 OMP_CLAUSE_CHAIN (c
) = list
;
34807 cp_parser_error (parser
, "invalid depend kind");
34809 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34810 /*or_comma=*/false,
34811 /*consume_paren=*/true);
34816 async [( int-expr )] */
34819 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
34822 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34824 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
34826 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34828 matching_parens parens
;
34829 parens
.consume_open (parser
);
34831 t
= cp_parser_expression (parser
);
34832 if (t
== error_mark_node
34833 || !parens
.require_close (parser
))
34834 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34835 /*or_comma=*/false,
34836 /*consume_paren=*/true);
34839 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
34841 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
34842 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
34843 OMP_CLAUSE_CHAIN (c
) = list
;
34849 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34850 is a bitmask in MASK. Return the list of clauses found. */
34853 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34854 const char *where
, cp_token
*pragma_tok
,
34855 bool finish_p
= true)
34857 tree clauses
= NULL
;
34860 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34863 pragma_omp_clause c_kind
;
34864 omp_clause_code code
;
34865 const char *c_name
;
34866 tree prev
= clauses
;
34868 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34869 cp_lexer_consume_token (parser
->lexer
);
34871 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
34872 c_kind
= cp_parser_omp_clause_name (parser
);
34876 case PRAGMA_OACC_CLAUSE_ASYNC
:
34877 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
34880 case PRAGMA_OACC_CLAUSE_AUTO
:
34881 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
34885 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
34886 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
34887 c_name
= "collapse";
34889 case PRAGMA_OACC_CLAUSE_COPY
:
34890 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34893 case PRAGMA_OACC_CLAUSE_COPYIN
:
34894 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34897 case PRAGMA_OACC_CLAUSE_COPYOUT
:
34898 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34899 c_name
= "copyout";
34901 case PRAGMA_OACC_CLAUSE_CREATE
:
34902 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34905 case PRAGMA_OACC_CLAUSE_DELETE
:
34906 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34909 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34910 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
34911 c_name
= "default";
34913 case PRAGMA_OACC_CLAUSE_DEVICE
:
34914 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34917 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
34918 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
34919 c_name
= "deviceptr";
34921 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
34922 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34923 c_name
= "device_resident";
34925 case PRAGMA_OACC_CLAUSE_FINALIZE
:
34926 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
34928 c_name
= "finalize";
34930 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
34931 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34933 c_name
= "firstprivate";
34935 case PRAGMA_OACC_CLAUSE_GANG
:
34937 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
34940 case PRAGMA_OACC_CLAUSE_HOST
:
34941 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34944 case PRAGMA_OACC_CLAUSE_IF
:
34945 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
34948 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
34949 clauses
= cp_parser_oacc_simple_clause (parser
,
34950 OMP_CLAUSE_IF_PRESENT
,
34952 c_name
= "if_present";
34954 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
34955 clauses
= cp_parser_oacc_simple_clause (parser
,
34956 OMP_CLAUSE_INDEPENDENT
,
34958 c_name
= "independent";
34960 case PRAGMA_OACC_CLAUSE_LINK
:
34961 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34964 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
34965 code
= OMP_CLAUSE_NUM_GANGS
;
34966 c_name
= "num_gangs";
34967 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34970 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
34971 c_name
= "num_workers";
34972 code
= OMP_CLAUSE_NUM_WORKERS
;
34973 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34976 case PRAGMA_OACC_CLAUSE_PRESENT
:
34977 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34978 c_name
= "present";
34980 case PRAGMA_OACC_CLAUSE_PRIVATE
:
34981 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34983 c_name
= "private";
34985 case PRAGMA_OACC_CLAUSE_REDUCTION
:
34987 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
34989 c_name
= "reduction";
34991 case PRAGMA_OACC_CLAUSE_SEQ
:
34992 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
34996 case PRAGMA_OACC_CLAUSE_TILE
:
34997 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
35000 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
35001 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
35003 c_name
= "use_device";
35005 case PRAGMA_OACC_CLAUSE_VECTOR
:
35007 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
35010 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
35011 c_name
= "vector_length";
35012 code
= OMP_CLAUSE_VECTOR_LENGTH
;
35013 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
35016 case PRAGMA_OACC_CLAUSE_WAIT
:
35017 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
35020 case PRAGMA_OACC_CLAUSE_WORKER
:
35022 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
35026 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
35032 if (((mask
>> c_kind
) & 1) == 0)
35034 /* Remove the invalid clause(s) from the list to avoid
35035 confusing the rest of the compiler. */
35037 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
35042 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35045 return finish_omp_clauses (clauses
, C_ORT_ACC
);
35050 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35051 is a bitmask in MASK. Return the list of clauses found; the result
35052 of clause default goes in *pdefault. */
35055 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
35056 const char *where
, cp_token
*pragma_tok
,
35057 bool finish_p
= true)
35059 tree clauses
= NULL
;
35061 cp_token
*token
= NULL
;
35063 /* Don't create location wrapper nodes within OpenMP clauses. */
35064 auto_suppress_location_wrappers sentinel
;
35066 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35068 pragma_omp_clause c_kind
;
35069 const char *c_name
;
35070 tree prev
= clauses
;
35072 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35073 cp_lexer_consume_token (parser
->lexer
);
35075 token
= cp_lexer_peek_token (parser
->lexer
);
35076 c_kind
= cp_parser_omp_clause_name (parser
);
35080 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
35081 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
35083 c_name
= "collapse";
35085 case PRAGMA_OMP_CLAUSE_COPYIN
:
35086 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
35089 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
35090 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
35092 c_name
= "copyprivate";
35094 case PRAGMA_OMP_CLAUSE_DEFAULT
:
35095 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
35096 token
->location
, false);
35097 c_name
= "default";
35099 case PRAGMA_OMP_CLAUSE_FINAL
:
35100 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
35103 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
35104 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
35106 c_name
= "firstprivate";
35108 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
35109 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
35111 c_name
= "grainsize";
35113 case PRAGMA_OMP_CLAUSE_HINT
:
35114 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
35118 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
35119 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
35121 c_name
= "defaultmap";
35123 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
35124 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
35126 c_name
= "use_device_ptr";
35128 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
35129 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
35131 c_name
= "is_device_ptr";
35133 case PRAGMA_OMP_CLAUSE_IF
:
35134 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
35138 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
35140 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
35142 c_name
= "in_reduction";
35144 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
35145 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
35146 c_name
= "lastprivate";
35148 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
35149 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
35151 c_name
= "mergeable";
35153 case PRAGMA_OMP_CLAUSE_NOWAIT
:
35154 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
35157 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
35158 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
35160 c_name
= "num_tasks";
35162 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
35163 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
35165 c_name
= "num_threads";
35167 case PRAGMA_OMP_CLAUSE_ORDERED
:
35168 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
35170 c_name
= "ordered";
35172 case PRAGMA_OMP_CLAUSE_PRIORITY
:
35173 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
35175 c_name
= "priority";
35177 case PRAGMA_OMP_CLAUSE_PRIVATE
:
35178 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
35180 c_name
= "private";
35182 case PRAGMA_OMP_CLAUSE_REDUCTION
:
35184 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
35186 c_name
= "reduction";
35188 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
35189 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
35191 c_name
= "schedule";
35193 case PRAGMA_OMP_CLAUSE_SHARED
:
35194 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
35198 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
35200 = cp_parser_omp_clause_reduction (parser
,
35201 OMP_CLAUSE_TASK_REDUCTION
,
35203 c_name
= "task_reduction";
35205 case PRAGMA_OMP_CLAUSE_UNTIED
:
35206 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
35210 case PRAGMA_OMP_CLAUSE_INBRANCH
:
35211 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
35212 clauses
, token
->location
);
35213 c_name
= "inbranch";
35215 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
35216 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
35218 c_name
= "nontemporal";
35220 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
35221 clauses
= cp_parser_omp_clause_branch (parser
,
35222 OMP_CLAUSE_NOTINBRANCH
,
35223 clauses
, token
->location
);
35224 c_name
= "notinbranch";
35226 case PRAGMA_OMP_CLAUSE_PARALLEL
:
35227 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
35228 clauses
, token
->location
);
35229 c_name
= "parallel";
35233 error_at (token
->location
, "%qs must be the first clause of %qs",
35238 case PRAGMA_OMP_CLAUSE_FOR
:
35239 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
35240 clauses
, token
->location
);
35243 goto clause_not_first
;
35245 case PRAGMA_OMP_CLAUSE_SECTIONS
:
35246 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
35247 clauses
, token
->location
);
35248 c_name
= "sections";
35250 goto clause_not_first
;
35252 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
35253 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
35254 clauses
, token
->location
);
35255 c_name
= "taskgroup";
35257 goto clause_not_first
;
35259 case PRAGMA_OMP_CLAUSE_LINK
:
35260 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
35263 case PRAGMA_OMP_CLAUSE_TO
:
35264 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
35265 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
35268 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
35271 case PRAGMA_OMP_CLAUSE_FROM
:
35272 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
35275 case PRAGMA_OMP_CLAUSE_UNIFORM
:
35276 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
35278 c_name
= "uniform";
35280 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
35281 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
35283 c_name
= "num_teams";
35285 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
35286 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
35288 c_name
= "thread_limit";
35290 case PRAGMA_OMP_CLAUSE_ALIGNED
:
35291 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
35292 c_name
= "aligned";
35294 case PRAGMA_OMP_CLAUSE_LINEAR
:
35296 bool declare_simd
= false;
35297 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
35298 declare_simd
= true;
35299 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
35303 case PRAGMA_OMP_CLAUSE_DEPEND
:
35304 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
35308 case PRAGMA_OMP_CLAUSE_MAP
:
35309 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
35312 case PRAGMA_OMP_CLAUSE_DEVICE
:
35313 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
35317 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
35318 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
35320 c_name
= "dist_schedule";
35322 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
35323 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
35325 c_name
= "proc_bind";
35327 case PRAGMA_OMP_CLAUSE_SAFELEN
:
35328 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
35330 c_name
= "safelen";
35332 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
35333 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
35335 c_name
= "simdlen";
35337 case PRAGMA_OMP_CLAUSE_NOGROUP
:
35338 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
35340 c_name
= "nogroup";
35342 case PRAGMA_OMP_CLAUSE_THREADS
:
35344 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
35345 clauses
, token
->location
);
35346 c_name
= "threads";
35348 case PRAGMA_OMP_CLAUSE_SIMD
:
35350 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
35351 clauses
, token
->location
);
35355 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
35361 if (((mask
>> c_kind
) & 1) == 0)
35363 /* Remove the invalid clause(s) from the list to avoid
35364 confusing the rest of the compiler. */
35366 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
35370 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35373 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
35374 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
35376 return finish_omp_clauses (clauses
, C_ORT_OMP
);
35385 In practice, we're also interested in adding the statement to an
35386 outer node. So it is convenient if we work around the fact that
35387 cp_parser_statement calls add_stmt. */
35390 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
35392 unsigned save
= parser
->in_statement
;
35394 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35395 This preserves the "not within loop or switch" style error messages
35396 for nonsense cases like
35402 if (parser
->in_statement
)
35403 parser
->in_statement
= IN_OMP_BLOCK
;
35409 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
35411 parser
->in_statement
= save
;
35415 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
35417 tree stmt
= begin_omp_structured_block ();
35418 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
35420 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35422 cp_parser_end_omp_structured_block (parser
, save
);
35423 return finish_omp_structured_block (stmt
);
35427 # pragma omp atomic new-line
35431 x binop= expr | x++ | ++x | x-- | --x
35433 +, *, -, /, &, ^, |, <<, >>
35435 where x is an lvalue expression with scalar type.
35438 # pragma omp atomic new-line
35441 # pragma omp atomic read new-line
35444 # pragma omp atomic write new-line
35447 # pragma omp atomic update new-line
35450 # pragma omp atomic capture new-line
35453 # pragma omp atomic capture new-line
35461 expression-stmt | x = x binop expr
35463 v = expression-stmt
35465 { v = x; update-stmt; } | { update-stmt; v = x; }
35469 expression-stmt | x = x binop expr | x = expr binop x
35473 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35475 where x and v are lvalue expressions with scalar type. */
35478 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
35480 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
35481 tree rhs1
= NULL_TREE
, orig_lhs
;
35482 location_t loc
= pragma_tok
->location
;
35483 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
35484 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
35485 bool structured_block
= false;
35487 tree clauses
= NULL_TREE
;
35489 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35491 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35492 cp_lexer_consume_token (parser
->lexer
);
35496 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35498 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35499 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35500 const char *p
= IDENTIFIER_POINTER (id
);
35501 enum tree_code new_code
= ERROR_MARK
;
35502 enum omp_memory_order new_memory_order
35503 = OMP_MEMORY_ORDER_UNSPECIFIED
;
35505 if (!strcmp (p
, "read"))
35506 new_code
= OMP_ATOMIC_READ
;
35507 else if (!strcmp (p
, "write"))
35508 new_code
= NOP_EXPR
;
35509 else if (!strcmp (p
, "update"))
35510 new_code
= OMP_ATOMIC
;
35511 else if (!strcmp (p
, "capture"))
35512 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
35513 else if (!strcmp (p
, "seq_cst"))
35514 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35515 else if (!strcmp (p
, "acq_rel"))
35516 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35517 else if (!strcmp (p
, "release"))
35518 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35519 else if (!strcmp (p
, "acquire"))
35520 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35521 else if (!strcmp (p
, "relaxed"))
35522 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35523 else if (!strcmp (p
, "hint"))
35525 cp_lexer_consume_token (parser
->lexer
);
35526 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
35532 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
35533 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35534 "%<release%>, %<relaxed%> or %<hint%> clause");
35538 if (new_code
!= ERROR_MARK
)
35540 if (code
!= ERROR_MARK
)
35541 error_at (cloc
, "too many atomic clauses");
35545 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35547 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35548 error_at (cloc
, "too many memory order clauses");
35550 memory_order
= new_memory_order
;
35552 cp_lexer_consume_token (parser
->lexer
);
35558 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35560 if (code
== ERROR_MARK
)
35562 if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
35565 = (enum omp_requires
) (omp_requires_mask
35566 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
35567 switch ((enum omp_memory_order
)
35568 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
35570 case OMP_MEMORY_ORDER_UNSPECIFIED
:
35571 case OMP_MEMORY_ORDER_RELAXED
:
35572 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35574 case OMP_MEMORY_ORDER_SEQ_CST
:
35575 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35577 case OMP_MEMORY_ORDER_ACQ_REL
:
35580 case OMP_ATOMIC_READ
:
35581 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35583 case NOP_EXPR
: /* atomic write */
35585 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35588 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35593 gcc_unreachable ();
35599 case OMP_ATOMIC_READ
:
35600 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35601 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
35603 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
35604 "%<acq_rel%> or %<release%> clauses");
35605 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35608 case NOP_EXPR
: /* atomic write */
35609 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35610 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35612 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
35613 "%<acq_rel%> or %<acquire%> clauses");
35614 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35618 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35619 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35621 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
35622 "%<acq_rel%> or %<acquire%> clauses");
35623 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35632 case OMP_ATOMIC_READ
:
35633 case NOP_EXPR
: /* atomic write */
35634 v
= cp_parser_unary_expression (parser
);
35635 if (v
== error_mark_node
)
35637 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35639 if (code
== NOP_EXPR
)
35640 lhs
= cp_parser_expression (parser
);
35642 lhs
= cp_parser_unary_expression (parser
);
35643 if (lhs
== error_mark_node
)
35645 if (code
== NOP_EXPR
)
35647 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35655 case OMP_ATOMIC_CAPTURE_NEW
:
35656 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35658 cp_lexer_consume_token (parser
->lexer
);
35659 structured_block
= true;
35663 v
= cp_parser_unary_expression (parser
);
35664 if (v
== error_mark_node
)
35666 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35674 lhs
= cp_parser_unary_expression (parser
);
35676 switch (TREE_CODE (lhs
))
35681 case POSTINCREMENT_EXPR
:
35682 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35683 code
= OMP_ATOMIC_CAPTURE_OLD
;
35685 case PREINCREMENT_EXPR
:
35686 lhs
= TREE_OPERAND (lhs
, 0);
35687 opcode
= PLUS_EXPR
;
35688 rhs
= integer_one_node
;
35691 case POSTDECREMENT_EXPR
:
35692 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35693 code
= OMP_ATOMIC_CAPTURE_OLD
;
35695 case PREDECREMENT_EXPR
:
35696 lhs
= TREE_OPERAND (lhs
, 0);
35697 opcode
= MINUS_EXPR
;
35698 rhs
= integer_one_node
;
35701 case COMPOUND_EXPR
:
35702 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
35703 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
35704 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
35705 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
35706 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35707 (TREE_OPERAND (lhs
, 1), 0), 0)))
35709 /* Undo effects of boolean_increment for post {in,de}crement. */
35710 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
35713 if (TREE_CODE (lhs
) == MODIFY_EXPR
35714 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
35716 /* Undo effects of boolean_increment. */
35717 if (integer_onep (TREE_OPERAND (lhs
, 1)))
35719 /* This is pre or post increment. */
35720 rhs
= TREE_OPERAND (lhs
, 1);
35721 lhs
= TREE_OPERAND (lhs
, 0);
35723 if (code
== OMP_ATOMIC_CAPTURE_NEW
35724 && !structured_block
35725 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
35726 code
= OMP_ATOMIC_CAPTURE_OLD
;
35732 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35735 opcode
= MULT_EXPR
;
35738 opcode
= TRUNC_DIV_EXPR
;
35741 opcode
= PLUS_EXPR
;
35744 opcode
= MINUS_EXPR
;
35746 case CPP_LSHIFT_EQ
:
35747 opcode
= LSHIFT_EXPR
;
35749 case CPP_RSHIFT_EQ
:
35750 opcode
= RSHIFT_EXPR
;
35753 opcode
= BIT_AND_EXPR
;
35756 opcode
= BIT_IOR_EXPR
;
35759 opcode
= BIT_XOR_EXPR
;
35762 enum cp_parser_prec oprec
;
35764 cp_lexer_consume_token (parser
->lexer
);
35765 cp_parser_parse_tentatively (parser
);
35766 rhs1
= cp_parser_simple_cast_expression (parser
);
35767 if (rhs1
== error_mark_node
)
35769 cp_parser_abort_tentative_parse (parser
);
35770 cp_parser_simple_cast_expression (parser
);
35773 token
= cp_lexer_peek_token (parser
->lexer
);
35774 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
35776 cp_parser_abort_tentative_parse (parser
);
35777 cp_parser_parse_tentatively (parser
);
35778 rhs
= cp_parser_binary_expression (parser
, false, true,
35779 PREC_NOT_OPERATOR
, NULL
);
35780 if (rhs
== error_mark_node
)
35782 cp_parser_abort_tentative_parse (parser
);
35783 cp_parser_binary_expression (parser
, false, true,
35784 PREC_NOT_OPERATOR
, NULL
);
35787 switch (TREE_CODE (rhs
))
35790 case TRUNC_DIV_EXPR
:
35799 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
35801 if (cp_parser_parse_definitely (parser
))
35803 opcode
= TREE_CODE (rhs
);
35804 rhs1
= TREE_OPERAND (rhs
, 0);
35805 rhs
= TREE_OPERAND (rhs
, 1);
35815 cp_parser_abort_tentative_parse (parser
);
35816 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
35818 rhs
= cp_parser_expression (parser
);
35819 if (rhs
== error_mark_node
)
35825 cp_parser_error (parser
,
35826 "invalid form of %<#pragma omp atomic%>");
35829 if (!cp_parser_parse_definitely (parser
))
35831 switch (token
->type
)
35833 case CPP_SEMICOLON
:
35834 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35836 code
= OMP_ATOMIC_CAPTURE_OLD
;
35841 cp_lexer_consume_token (parser
->lexer
);
35844 else if (structured_block
)
35851 cp_parser_error (parser
,
35852 "invalid form of %<#pragma omp atomic%>");
35855 opcode
= MULT_EXPR
;
35858 opcode
= TRUNC_DIV_EXPR
;
35861 opcode
= PLUS_EXPR
;
35864 opcode
= MINUS_EXPR
;
35867 opcode
= LSHIFT_EXPR
;
35870 opcode
= RSHIFT_EXPR
;
35873 opcode
= BIT_AND_EXPR
;
35876 opcode
= BIT_IOR_EXPR
;
35879 opcode
= BIT_XOR_EXPR
;
35882 cp_parser_error (parser
,
35883 "invalid operator for %<#pragma omp atomic%>");
35886 oprec
= TOKEN_PRECEDENCE (token
);
35887 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
35888 if (commutative_tree_code (opcode
))
35889 oprec
= (enum cp_parser_prec
) (oprec
- 1);
35890 cp_lexer_consume_token (parser
->lexer
);
35891 rhs
= cp_parser_binary_expression (parser
, false, false,
35893 if (rhs
== error_mark_node
)
35898 cp_parser_error (parser
,
35899 "invalid operator for %<#pragma omp atomic%>");
35902 cp_lexer_consume_token (parser
->lexer
);
35904 rhs
= cp_parser_expression (parser
);
35905 if (rhs
== error_mark_node
)
35910 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35912 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
35914 v
= cp_parser_unary_expression (parser
);
35915 if (v
== error_mark_node
)
35917 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35919 lhs1
= cp_parser_unary_expression (parser
);
35920 if (lhs1
== error_mark_node
)
35923 if (structured_block
)
35925 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35926 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
35929 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
35930 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
35931 rhs1
, clauses
, memory_order
);
35932 if (!structured_block
)
35933 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35937 cp_parser_skip_to_end_of_block_or_statement (parser
);
35938 if (structured_block
)
35940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35941 cp_lexer_consume_token (parser
->lexer
);
35942 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
35944 cp_parser_skip_to_end_of_block_or_statement (parser
);
35945 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35946 cp_lexer_consume_token (parser
->lexer
);
35953 # pragma omp barrier new-line */
35956 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
35958 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35959 finish_omp_barrier ();
35963 # pragma omp critical [(name)] new-line
35967 # pragma omp critical [(name) [hint(expression)]] new-line
35968 structured-block */
35970 #define OMP_CRITICAL_CLAUSE_MASK \
35971 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35974 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35976 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
35978 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35980 matching_parens parens
;
35981 parens
.consume_open (parser
);
35983 name
= cp_parser_identifier (parser
);
35985 if (name
== error_mark_node
35986 || !parens
.require_close (parser
))
35987 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35988 /*or_comma=*/false,
35989 /*consume_paren=*/true);
35990 if (name
== error_mark_node
)
35993 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
35994 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
35995 cp_lexer_consume_token (parser
->lexer
);
35997 clauses
= cp_parser_omp_all_clauses (parser
,
35998 OMP_CRITICAL_CLAUSE_MASK
,
35999 "#pragma omp critical", pragma_tok
);
36002 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36004 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
36005 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
36009 # pragma omp depobj ( depobj ) depobj-clause new-line
36012 depend (dependence-type : locator)
36014 update (dependence-type)
36023 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
36025 location_t loc
= pragma_tok
->location
;
36026 matching_parens parens
;
36027 if (!parens
.require_open (parser
))
36029 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36033 tree depobj
= cp_parser_assignment_expression (parser
);
36035 if (!parens
.require_close (parser
))
36036 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36037 /*or_comma=*/false,
36038 /*consume_paren=*/true);
36040 tree clause
= NULL_TREE
;
36041 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
36042 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36043 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36045 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36046 const char *p
= IDENTIFIER_POINTER (id
);
36048 cp_lexer_consume_token (parser
->lexer
);
36049 if (!strcmp ("depend", p
))
36051 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
36053 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
36055 clause
= error_mark_node
;
36057 else if (!strcmp ("destroy", p
))
36058 kind
= OMP_CLAUSE_DEPEND_LAST
;
36059 else if (!strcmp ("update", p
))
36061 matching_parens c_parens
;
36062 if (c_parens
.require_open (parser
))
36065 = cp_lexer_peek_token (parser
->lexer
)->location
;
36066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36068 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36069 const char *p2
= IDENTIFIER_POINTER (id2
);
36071 cp_lexer_consume_token (parser
->lexer
);
36072 if (!strcmp ("in", p2
))
36073 kind
= OMP_CLAUSE_DEPEND_IN
;
36074 else if (!strcmp ("out", p2
))
36075 kind
= OMP_CLAUSE_DEPEND_OUT
;
36076 else if (!strcmp ("inout", p2
))
36077 kind
= OMP_CLAUSE_DEPEND_INOUT
;
36078 else if (!strcmp ("mutexinoutset", p2
))
36079 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
36081 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36083 clause
= error_mark_node
;
36084 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
36085 "%<mutexinoutset%>");
36087 if (!c_parens
.require_close (parser
))
36088 cp_parser_skip_to_closing_parenthesis (parser
,
36089 /*recovering=*/true,
36090 /*or_comma=*/false,
36091 /*consume_paren=*/true);
36094 clause
= error_mark_node
;
36097 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36099 clause
= error_mark_node
;
36100 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
36102 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36104 finish_omp_depobj (loc
, depobj
, kind
, clause
);
36109 # pragma omp flush flush-vars[opt] new-line
36115 # pragma omp flush memory-order-clause new-line */
36118 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
36120 enum memmodel mo
= MEMMODEL_LAST
;
36121 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36123 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36124 const char *p
= IDENTIFIER_POINTER (id
);
36125 if (!strcmp (p
, "acq_rel"))
36126 mo
= MEMMODEL_ACQ_REL
;
36127 else if (!strcmp (p
, "release"))
36128 mo
= MEMMODEL_RELEASE
;
36129 else if (!strcmp (p
, "acquire"))
36130 mo
= MEMMODEL_ACQUIRE
;
36132 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36133 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36134 cp_lexer_consume_token (parser
->lexer
);
36136 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36138 if (mo
!= MEMMODEL_LAST
)
36139 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36140 "%<flush%> list specified together with memory order "
36142 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36144 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36146 finish_omp_flush (mo
);
36149 /* Helper function, to parse omp for increment expression. */
36152 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
36154 tree cond
= cp_parser_binary_expression (parser
, false, true,
36155 PREC_NOT_OPERATOR
, NULL
);
36156 if (cond
== error_mark_node
36157 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36159 cp_parser_skip_to_end_of_statement (parser
);
36160 return error_mark_node
;
36163 switch (TREE_CODE (cond
))
36171 if (code
!= OACC_LOOP
)
36173 gcc_fallthrough ();
36175 return error_mark_node
;
36178 /* If decl is an iterator, preserve LHS and RHS of the relational
36179 expr until finish_omp_for. */
36181 && (type_dependent_expression_p (decl
)
36182 || CLASS_TYPE_P (TREE_TYPE (decl
))))
36185 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
36187 TREE_OPERAND (cond
, 0), ERROR_MARK
,
36188 TREE_OPERAND (cond
, 1), ERROR_MARK
,
36189 /*overload=*/NULL
, tf_warning_or_error
);
36192 /* Helper function, to parse omp for increment expression. */
36195 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
36197 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36203 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36205 op
= (token
->type
== CPP_PLUS_PLUS
36206 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
36207 cp_lexer_consume_token (parser
->lexer
);
36208 lhs
= cp_parser_simple_cast_expression (parser
);
36210 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36211 return error_mark_node
;
36212 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36215 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
36217 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36218 return error_mark_node
;
36220 token
= cp_lexer_peek_token (parser
->lexer
);
36221 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36223 op
= (token
->type
== CPP_PLUS_PLUS
36224 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
36225 cp_lexer_consume_token (parser
->lexer
);
36226 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36229 op
= cp_parser_assignment_operator_opt (parser
);
36230 if (op
== ERROR_MARK
)
36231 return error_mark_node
;
36233 if (op
!= NOP_EXPR
)
36235 rhs
= cp_parser_assignment_expression (parser
);
36236 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
36237 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36240 lhs
= cp_parser_binary_expression (parser
, false, false,
36241 PREC_ADDITIVE_EXPRESSION
, NULL
);
36242 token
= cp_lexer_peek_token (parser
->lexer
);
36243 decl_first
= (lhs
== decl
36244 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
36247 if (token
->type
!= CPP_PLUS
36248 && token
->type
!= CPP_MINUS
)
36249 return error_mark_node
;
36253 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
36254 cp_lexer_consume_token (parser
->lexer
);
36255 rhs
= cp_parser_binary_expression (parser
, false, false,
36256 PREC_ADDITIVE_EXPRESSION
, NULL
);
36257 token
= cp_lexer_peek_token (parser
->lexer
);
36258 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
36260 if (lhs
== NULL_TREE
)
36262 if (op
== PLUS_EXPR
)
36265 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
36266 tf_warning_or_error
);
36269 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
36270 ERROR_MARK
, NULL
, tf_warning_or_error
);
36273 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
36278 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
36279 || op
== MINUS_EXPR
)
36280 return error_mark_node
;
36281 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
36284 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
36286 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36289 /* Parse the initialization statement of an OpenMP for loop.
36291 Return true if the resulting construct should have an
36292 OMP_CLAUSE_PRIVATE added to it. */
36295 cp_parser_omp_for_loop_init (cp_parser
*parser
,
36296 tree
&this_pre_body
,
36297 vec
<tree
, va_gc
> *&for_block
,
36303 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36306 tree add_private_clause
= NULL_TREE
;
36308 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36312 integer-type var = lb
36313 random-access-iterator-type var = lb
36314 pointer-type var = lb
36316 cp_decl_specifier_seq type_specifiers
;
36318 /* First, try to parse as an initialized declaration. See
36319 cp_parser_condition, from whence the bulk of this is copied. */
36321 cp_parser_parse_tentatively (parser
);
36322 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
36323 /*is_declaration=*/true,
36324 /*is_trailing_return=*/false,
36326 if (cp_parser_parse_definitely (parser
))
36328 /* If parsing a type specifier seq succeeded, then this
36329 MUST be a initialized declaration. */
36330 tree asm_specification
, attributes
;
36331 cp_declarator
*declarator
;
36333 declarator
= cp_parser_declarator (parser
,
36334 CP_PARSER_DECLARATOR_NAMED
,
36335 CP_PARSER_FLAGS_NONE
,
36336 /*ctor_dtor_or_conv_p=*/NULL
,
36337 /*parenthesized_p=*/NULL
,
36338 /*member_p=*/false,
36339 /*friend_p=*/false,
36340 /*static_p=*/false);
36341 attributes
= cp_parser_attributes_opt (parser
);
36342 asm_specification
= cp_parser_asm_specification_opt (parser
);
36344 if (declarator
== cp_error_declarator
)
36345 cp_parser_skip_to_end_of_statement (parser
);
36349 tree pushed_scope
, auto_node
;
36351 decl
= start_decl (declarator
, &type_specifiers
,
36352 SD_INITIALIZED
, attributes
,
36353 /*prefix_attributes=*/NULL_TREE
,
36356 auto_node
= type_uses_auto (TREE_TYPE (decl
));
36357 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
36359 if (cp_lexer_next_token_is (parser
->lexer
,
36361 error ("parenthesized initialization is not allowed in "
36362 "OpenMP %<for%> loop");
36364 /* Trigger an error. */
36365 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36367 init
= error_mark_node
;
36368 cp_parser_skip_to_end_of_statement (parser
);
36370 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
36371 || type_dependent_expression_p (decl
)
36374 bool is_direct_init
, is_non_constant_init
;
36376 init
= cp_parser_initializer (parser
,
36378 &is_non_constant_init
);
36383 = do_auto_deduction (TREE_TYPE (decl
), init
,
36386 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
36387 && !type_dependent_expression_p (decl
))
36391 cp_finish_decl (decl
, init
, !is_non_constant_init
,
36393 LOOKUP_ONLYCONVERTING
);
36395 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
36397 vec_safe_push (for_block
, this_pre_body
);
36402 init
= pop_stmt_list (this_pre_body
);
36403 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
36405 tree_stmt_iterator i
= tsi_start (init
);
36406 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36407 while (!tsi_end_p (i
))
36409 tree t
= tsi_stmt (i
);
36410 if (TREE_CODE (t
) == DECL_EXPR
36411 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
36414 vec_safe_push (for_block
, t
);
36419 if (tsi_one_before_end_p (i
))
36421 tree t
= tsi_stmt (i
);
36423 free_stmt_list (init
);
36428 this_pre_body
= NULL_TREE
;
36433 cp_lexer_consume_token (parser
->lexer
);
36434 init
= cp_parser_assignment_expression (parser
);
36437 if (TYPE_REF_P (TREE_TYPE (decl
)))
36438 init
= error_mark_node
;
36440 cp_finish_decl (decl
, NULL_TREE
,
36441 /*init_const_expr_p=*/false,
36443 LOOKUP_ONLYCONVERTING
);
36447 pop_scope (pushed_scope
);
36453 /* If parsing a type specifier sequence failed, then
36454 this MUST be a simple expression. */
36455 cp_parser_parse_tentatively (parser
);
36456 decl
= cp_parser_primary_expression (parser
, false, false,
36458 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
36459 if (!cp_parser_error_occurred (parser
)
36461 && (TREE_CODE (decl
) == COMPONENT_REF
36462 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
36464 cp_parser_abort_tentative_parse (parser
);
36465 cp_parser_parse_tentatively (parser
);
36466 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36467 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36468 /*check_dependency_p=*/true,
36469 /*template_p=*/NULL
,
36470 /*declarator_p=*/false,
36471 /*optional_p=*/false);
36472 if (name
!= error_mark_node
36473 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
36475 decl
= cp_parser_lookup_name_simple (parser
, name
,
36477 if (TREE_CODE (decl
) == FIELD_DECL
)
36478 add_private_clause
= omp_privatize_field (decl
, false);
36480 cp_parser_abort_tentative_parse (parser
);
36481 cp_parser_parse_tentatively (parser
);
36482 decl
= cp_parser_primary_expression (parser
, false, false,
36485 if (!cp_parser_error_occurred (parser
)
36488 && CLASS_TYPE_P (TREE_TYPE (decl
)))
36492 cp_parser_parse_definitely (parser
);
36493 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36494 rhs
= cp_parser_assignment_expression (parser
);
36496 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
36499 tf_warning_or_error
));
36500 if (!add_private_clause
)
36501 add_private_clause
= decl
;
36506 cp_parser_abort_tentative_parse (parser
);
36507 init
= cp_parser_expression (parser
);
36510 if (TREE_CODE (init
) == MODIFY_EXPR
36511 || TREE_CODE (init
) == MODOP_EXPR
)
36512 real_decl
= TREE_OPERAND (init
, 0);
36516 return add_private_clause
;
36519 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36522 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
36523 tree
&decl
, tree
&orig_decl
, tree
&init
,
36524 tree
&orig_init
, tree
&cond
, tree
&incr
)
36526 tree begin
, end
, range_temp_decl
= NULL_TREE
;
36527 tree iter_type
, begin_expr
, end_expr
;
36529 if (processing_template_decl
)
36531 if (check_for_bare_parameter_packs (init
))
36532 init
= error_mark_node
;
36533 if (!type_dependent_expression_p (init
)
36534 /* do_auto_deduction doesn't mess with template init-lists. */
36535 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
36538 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
36540 tree v
= DECL_VALUE_EXPR (decl
);
36541 if (TREE_CODE (v
) == ARRAY_REF
36542 && VAR_P (TREE_OPERAND (v
, 0))
36543 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36544 d
= TREE_OPERAND (v
, 0);
36546 do_range_for_auto_deduction (d
, init
);
36548 cond
= global_namespace
;
36552 this_pre_body
= pop_stmt_list (this_pre_body
);
36556 init
= mark_lvalue_use (init
);
36558 if (decl
== error_mark_node
|| init
== error_mark_node
)
36559 /* If an error happened previously do nothing or else a lot of
36560 unhelpful errors would be issued. */
36561 begin_expr
= end_expr
= iter_type
= error_mark_node
;
36567 && array_of_runtime_bound_p (TREE_TYPE (init
)))
36568 /* Can't bind a reference to an array of runtime bound. */
36572 range_temp
= build_range_temp (init
);
36573 DECL_NAME (range_temp
) = NULL_TREE
;
36574 pushdecl (range_temp
);
36575 cp_finish_decl (range_temp
, init
,
36576 /*is_constant_init*/false, NULL_TREE
,
36577 LOOKUP_ONLYCONVERTING
);
36578 range_temp_decl
= range_temp
;
36579 range_temp
= convert_from_reference (range_temp
);
36581 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
36582 &begin_expr
, &end_expr
);
36585 tree end_iter_type
= iter_type
;
36586 if (cxx_dialect
>= cxx17
)
36587 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
36588 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
36589 TREE_USED (end
) = 1;
36590 DECL_ARTIFICIAL (end
) = 1;
36592 cp_finish_decl (end
, end_expr
,
36593 /*is_constant_init*/false, NULL_TREE
,
36594 LOOKUP_ONLYCONVERTING
);
36596 /* The new for initialization statement. */
36597 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
36598 TREE_USED (begin
) = 1;
36599 DECL_ARTIFICIAL (begin
) = 1;
36602 if (CLASS_TYPE_P (iter_type
))
36607 begin_expr
= NULL_TREE
;
36609 cp_finish_decl (begin
, begin_expr
,
36610 /*is_constant_init*/false, NULL_TREE
,
36611 LOOKUP_ONLYCONVERTING
);
36613 /* The new for condition. */
36614 if (CLASS_TYPE_P (iter_type
))
36615 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
36617 cond
= build_x_binary_op (input_location
, NE_EXPR
,
36620 NULL
, tf_warning_or_error
);
36622 /* The new increment expression. */
36623 if (CLASS_TYPE_P (iter_type
))
36624 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
36626 incr
= finish_unary_op_expr (input_location
,
36627 PREINCREMENT_EXPR
, begin
,
36628 tf_warning_or_error
);
36634 vec_safe_push (for_block
, this_pre_body
);
36635 this_pre_body
= NULL_TREE
;
36638 tree decomp_first_name
= NULL_TREE
;
36639 unsigned decomp_cnt
= 0;
36640 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
36642 tree v
= DECL_VALUE_EXPR (orig_decl
);
36643 if (TREE_CODE (v
) == ARRAY_REF
36644 && VAR_P (TREE_OPERAND (v
, 0))
36645 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36647 tree d
= orig_decl
;
36648 orig_decl
= TREE_OPERAND (v
, 0);
36649 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
36650 decomp_first_name
= d
;
36654 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
36657 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36659 if (!error_operand_p (t
))
36660 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
36664 tree v
= make_tree_vec (decomp_cnt
+ 3);
36665 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
36666 TREE_VEC_ELT (v
, 1) = end
;
36667 TREE_VEC_ELT (v
, 2) = orig_decl
;
36668 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
36670 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
36671 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
36673 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
36676 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36677 inside of the collapsed body. */
36680 cp_finish_omp_range_for (tree orig
, tree begin
)
36682 gcc_assert (TREE_CODE (orig
) == TREE_LIST
36683 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
36684 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
36685 tree decomp_first_name
= NULL_TREE
;
36686 unsigned int decomp_cnt
= 0;
36688 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36690 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
36691 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
36692 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
36695 /* The declaration is initialized with *__begin inside the loop body. */
36696 cp_finish_decl (decl
,
36697 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36698 tf_warning_or_error
),
36699 /*is_constant_init*/false, NULL_TREE
,
36700 LOOKUP_ONLYCONVERTING
);
36701 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36702 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
36705 /* Parse the restricted form of the for statement allowed by OpenMP. */
36708 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
36709 tree
*cclauses
, bool *if_p
)
36711 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
36713 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
36714 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
36715 location_t loc_first
;
36716 bool collapse_err
= false;
36717 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
36718 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
36719 auto_vec
<tree
, 4> orig_inits
;
36720 bool tiling
= false;
36722 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
36723 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
36724 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
36725 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
36728 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
36730 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
36731 && OMP_CLAUSE_ORDERED_EXPR (cl
))
36734 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
36737 if (ordered
&& ordered
< collapse
)
36739 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36740 "%<ordered%> clause parameter is less than %<collapse%>");
36741 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
36742 = build_int_cst (NULL_TREE
, collapse
);
36743 ordered
= collapse
;
36747 for (tree
*pc
= &clauses
; *pc
; )
36748 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
36750 error_at (OMP_CLAUSE_LOCATION (*pc
),
36751 "%<linear%> clause may not be specified together "
36752 "with %<ordered%> clause with a parameter");
36753 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36756 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36759 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
36760 count
= ordered
? ordered
: collapse
;
36762 declv
= make_tree_vec (count
);
36763 initv
= make_tree_vec (count
);
36764 condv
= make_tree_vec (count
);
36765 incrv
= make_tree_vec (count
);
36766 orig_declv
= NULL_TREE
;
36768 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
36770 for (i
= 0; i
< count
; i
++)
36772 int bracecount
= 0;
36773 tree add_private_clause
= NULL_TREE
;
36776 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36779 cp_parser_error (parser
, "for statement expected");
36782 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
36784 /* Don't create location wrapper nodes within an OpenMP "for"
36786 auto_suppress_location_wrappers sentinel
;
36788 matching_parens parens
;
36789 if (!parens
.require_open (parser
))
36792 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
36793 this_pre_body
= push_stmt_list ();
36795 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
36797 /* Save tokens so that we can put them back. */
36798 cp_lexer_save_tokens (parser
->lexer
);
36800 /* Look for ':' that is not nested in () or {}. */
36802 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
36803 /*recovering=*/false,
36808 /* Roll back the tokens we skipped. */
36809 cp_lexer_rollback_tokens (parser
->lexer
);
36813 bool saved_colon_corrects_to_scope_p
36814 = parser
->colon_corrects_to_scope_p
;
36816 /* A colon is used in range-based for. */
36817 parser
->colon_corrects_to_scope_p
= false;
36819 /* Parse the declaration. */
36820 cp_parser_simple_declaration (parser
,
36821 /*function_definition_allowed_p=*/
36823 parser
->colon_corrects_to_scope_p
36824 = saved_colon_corrects_to_scope_p
;
36826 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36828 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
36831 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
36832 orig_decl
, init
, orig_init
,
36839 pre_body
= push_stmt_list ();
36841 add_stmt (this_pre_body
);
36842 pre_body
= pop_stmt_list (pre_body
);
36845 pre_body
= this_pre_body
;
36849 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36850 "%<ordered%> clause with parameter on "
36851 "range-based %<for%> loop");
36853 goto parse_close_paren
;
36858 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
36859 init
, orig_init
, decl
, real_decl
);
36861 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36864 this_pre_body
= pop_stmt_list (this_pre_body
);
36868 pre_body
= push_stmt_list ();
36870 add_stmt (this_pre_body
);
36871 pre_body
= pop_stmt_list (pre_body
);
36874 pre_body
= this_pre_body
;
36879 if (cclauses
!= NULL
36880 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
36881 && real_decl
!= NULL_TREE
)
36884 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
36885 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
36886 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36888 error_at (loc
, "iteration variable %qD"
36889 " should not be firstprivate", real_decl
);
36890 *c
= OMP_CLAUSE_CHAIN (*c
);
36892 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
36893 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36895 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36897 *c
= OMP_CLAUSE_CHAIN (*c
);
36898 if (code
== OMP_SIMD
)
36900 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36901 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
36905 OMP_CLAUSE_CHAIN (l
) = clauses
;
36908 add_private_clause
= NULL_TREE
;
36912 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
36913 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36914 add_private_clause
= NULL_TREE
;
36915 c
= &OMP_CLAUSE_CHAIN (*c
);
36919 if (add_private_clause
)
36922 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
36924 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
36925 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
36926 && OMP_CLAUSE_DECL (c
) == decl
)
36928 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
36929 && OMP_CLAUSE_DECL (c
) == decl
)
36930 error_at (loc
, "iteration variable %qD "
36931 "should not be firstprivate",
36933 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
36934 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
36935 && OMP_CLAUSE_DECL (c
) == decl
)
36936 error_at (loc
, "iteration variable %qD should not be reduction",
36941 if (code
!= OMP_SIMD
)
36942 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
36943 else if (collapse
== 1)
36944 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
36946 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
36947 OMP_CLAUSE_DECL (c
) = add_private_clause
;
36948 c
= finish_omp_clauses (c
, C_ORT_OMP
);
36951 OMP_CLAUSE_CHAIN (c
) = clauses
;
36953 /* For linear, signal that we need to fill up
36954 the so far unknown linear step. */
36955 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
36956 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
36962 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36963 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
36964 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36967 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
36969 /* If decl is an iterator, preserve the operator on decl
36970 until finish_omp_for. */
36972 && ((processing_template_decl
36973 && (TREE_TYPE (real_decl
) == NULL_TREE
36974 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
36975 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
36976 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
36978 incr
= cp_parser_expression (parser
);
36979 if (!EXPR_HAS_LOCATION (incr
))
36980 protected_set_expr_location (incr
, input_location
);
36984 if (!parens
.require_close (parser
))
36985 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36986 /*or_comma=*/false,
36987 /*consume_paren=*/true);
36989 TREE_VEC_ELT (declv
, i
) = decl
;
36990 TREE_VEC_ELT (initv
, i
) = init
;
36991 TREE_VEC_ELT (condv
, i
) = cond
;
36992 TREE_VEC_ELT (incrv
, i
) = incr
;
36995 orig_inits
.safe_grow_cleared (i
+ 1);
36996 orig_inits
[i
] = orig_init
;
37001 orig_declv
= copy_node (declv
);
37002 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
37004 else if (orig_declv
)
37005 TREE_VEC_ELT (orig_declv
, i
) = decl
;
37007 if (i
== count
- 1)
37010 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37011 in between the collapsed for loops to be still considered perfectly
37012 nested. Hopefully the final version clarifies this.
37013 For now handle (multiple) {'s and empty statements. */
37014 cp_parser_parse_tentatively (parser
);
37017 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37019 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
37021 cp_lexer_consume_token (parser
->lexer
);
37024 else if (bracecount
37025 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
37026 cp_lexer_consume_token (parser
->lexer
);
37029 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37030 error_at (loc
, "not enough for loops to collapse");
37031 collapse_err
= true;
37032 cp_parser_abort_tentative_parse (parser
);
37040 cp_parser_parse_definitely (parser
);
37041 nbraces
+= bracecount
;
37048 /* Note that we saved the original contents of this flag when we entered
37049 the structured block, and so we don't need to re-save it here. */
37050 parser
->in_statement
= IN_OMP_FOR
;
37052 /* Note that the grammar doesn't call for a structured block here,
37053 though the loop as a whole is a structured block. */
37056 body
= begin_omp_structured_block ();
37057 for (i
= 0; i
< count
; i
++)
37058 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
37059 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
37060 TREE_VEC_ELT (declv
, i
));
37063 body
= push_stmt_list ();
37064 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37066 body
= finish_omp_structured_block (body
);
37068 body
= pop_stmt_list (body
);
37070 if (declv
== NULL_TREE
)
37073 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
37074 incrv
, body
, pre_body
, &orig_inits
, clauses
);
37078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
37080 cp_lexer_consume_token (parser
->lexer
);
37083 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
37084 cp_lexer_consume_token (parser
->lexer
);
37089 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37090 "collapsed loops not perfectly nested");
37092 collapse_err
= true;
37093 cp_parser_statement_seq_opt (parser
, NULL
);
37094 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
37099 while (!for_block
->is_empty ())
37101 tree t
= for_block
->pop ();
37102 if (TREE_CODE (t
) == STATEMENT_LIST
)
37103 add_stmt (pop_stmt_list (t
));
37107 release_tree_vector (for_block
);
37112 /* Helper function for OpenMP parsing, split clauses and call
37113 finish_omp_clauses on each of the set of clauses afterwards. */
37116 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
37117 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
37120 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
37121 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
37123 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
37127 #pragma omp simd simd-clause[optseq] new-line
37130 #define OMP_SIMD_CLAUSE_MASK \
37131 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37143 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37144 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37147 tree clauses
, sb
, ret
;
37149 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37151 strcat (p_name
, " simd");
37152 mask
|= OMP_SIMD_CLAUSE_MASK
;
37154 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37158 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
37159 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
37160 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
37161 OMP_CLAUSE_ORDERED
);
37162 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
37164 error_at (OMP_CLAUSE_LOCATION (c
),
37165 "%<ordered%> clause with parameter may not be specified "
37166 "on %qs construct", p_name
);
37167 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
37171 keep_next_level (true);
37172 sb
= begin_omp_structured_block ();
37173 save
= cp_parser_begin_omp_structured_block (parser
);
37175 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
37177 cp_parser_end_omp_structured_block (parser
, save
);
37178 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37184 #pragma omp for for-clause[optseq] new-line
37188 #pragma omp for simd for-simd-clause[optseq] new-line
37191 #define OMP_FOR_CLAUSE_MASK \
37192 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37203 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
37204 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37207 tree clauses
, sb
, ret
;
37209 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37211 strcat (p_name
, " for");
37212 mask
|= OMP_FOR_CLAUSE_MASK
;
37213 /* parallel for{, simd} disallows nowait clause, but for
37214 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37215 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
37216 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37217 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37218 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37219 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
37221 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37223 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37224 const char *p
= IDENTIFIER_POINTER (id
);
37226 if (strcmp (p
, "simd") == 0)
37228 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37229 if (cclauses
== NULL
)
37230 cclauses
= cclauses_buf
;
37232 cp_lexer_consume_token (parser
->lexer
);
37233 if (!flag_openmp
) /* flag_openmp_simd */
37234 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37236 sb
= begin_omp_structured_block ();
37237 save
= cp_parser_begin_omp_structured_block (parser
);
37238 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37240 cp_parser_end_omp_structured_block (parser
, save
);
37241 tree body
= finish_omp_structured_block (sb
);
37244 ret
= make_node (OMP_FOR
);
37245 TREE_TYPE (ret
) = void_type_node
;
37246 OMP_FOR_BODY (ret
) = body
;
37247 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37248 SET_EXPR_LOCATION (ret
, loc
);
37253 if (!flag_openmp
) /* flag_openmp_simd */
37255 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37259 /* Composite distribute parallel for disallows linear clause. */
37260 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37261 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
37263 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37267 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
37268 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37271 keep_next_level (true);
37272 sb
= begin_omp_structured_block ();
37273 save
= cp_parser_begin_omp_structured_block (parser
);
37275 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
37277 cp_parser_end_omp_structured_block (parser
, save
);
37278 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37283 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
37284 omp_clause_mask
, tree
*, bool *);
37287 # pragma omp master new-line
37288 structured-block */
37291 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
37292 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37295 tree clauses
, sb
, ret
;
37297 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37299 strcat (p_name
, " master");
37301 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37303 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37304 const char *p
= IDENTIFIER_POINTER (id
);
37306 if (strcmp (p
, "taskloop") == 0)
37308 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37309 if (cclauses
== NULL
)
37310 cclauses
= cclauses_buf
;
37312 cp_lexer_consume_token (parser
->lexer
);
37313 if (!flag_openmp
) /* flag_openmp_simd */
37314 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37316 sb
= begin_omp_structured_block ();
37317 save
= cp_parser_begin_omp_structured_block (parser
);
37318 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37320 cp_parser_end_omp_structured_block (parser
, save
);
37321 tree body
= finish_omp_structured_block (sb
);
37324 return c_finish_omp_master (loc
, body
);
37327 if (!flag_openmp
) /* flag_openmp_simd */
37329 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37335 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37337 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
37340 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37342 return c_finish_omp_master (loc
,
37343 cp_parser_omp_structured_block (parser
, if_p
));
37347 # pragma omp ordered new-line
37351 # pragma omp ordered ordered-clauses new-line
37352 structured-block */
37354 #define OMP_ORDERED_CLAUSE_MASK \
37355 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37358 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37359 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37362 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
37363 enum pragma_context context
, bool *if_p
)
37365 location_t loc
= pragma_tok
->location
;
37367 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37369 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37370 const char *p
= IDENTIFIER_POINTER (id
);
37372 if (strcmp (p
, "depend") == 0)
37374 if (!flag_openmp
) /* flag_openmp_simd */
37376 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37379 if (context
== pragma_stmt
)
37381 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
37382 "%<depend%> clause may only be used in compound "
37384 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37388 = cp_parser_omp_all_clauses (parser
,
37389 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
37390 "#pragma omp ordered", pragma_tok
);
37391 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
37397 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
37398 "#pragma omp ordered", pragma_tok
);
37400 if (!flag_openmp
/* flag_openmp_simd */
37401 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
37404 c_finish_omp_ordered (loc
, clauses
,
37405 cp_parser_omp_structured_block (parser
, if_p
));
37412 { section-sequence }
37415 section-directive[opt] structured-block
37416 section-sequence section-directive structured-block */
37419 cp_parser_omp_sections_scope (cp_parser
*parser
)
37421 tree stmt
, substmt
;
37422 bool error_suppress
= false;
37425 matching_braces braces
;
37426 if (!braces
.require_open (parser
))
37429 stmt
= push_stmt_list ();
37431 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
37432 != PRAGMA_OMP_SECTION
)
37434 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37435 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37436 add_stmt (substmt
);
37441 tok
= cp_lexer_peek_token (parser
->lexer
);
37442 if (tok
->type
== CPP_CLOSE_BRACE
)
37444 if (tok
->type
== CPP_EOF
)
37447 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
37449 cp_lexer_consume_token (parser
->lexer
);
37450 cp_parser_require_pragma_eol (parser
, tok
);
37451 error_suppress
= false;
37453 else if (!error_suppress
)
37455 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
37456 error_suppress
= true;
37459 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37460 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37461 add_stmt (substmt
);
37463 braces
.require_close (parser
);
37465 substmt
= pop_stmt_list (stmt
);
37467 stmt
= make_node (OMP_SECTIONS
);
37468 TREE_TYPE (stmt
) = void_type_node
;
37469 OMP_SECTIONS_BODY (stmt
) = substmt
;
37476 # pragma omp sections sections-clause[optseq] newline
37479 #define OMP_SECTIONS_CLAUSE_MASK \
37480 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37487 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
37488 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
37491 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37493 strcat (p_name
, " sections");
37494 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
37496 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37498 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37502 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
37503 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
37506 ret
= cp_parser_omp_sections_scope (parser
);
37508 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
37514 # pragma omp parallel parallel-clause[optseq] new-line
37516 # pragma omp parallel for parallel-for-clause[optseq] new-line
37518 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37522 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37523 structured-block */
37525 #define OMP_PARALLEL_CLAUSE_MASK \
37526 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37537 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37538 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37541 tree stmt
, clauses
, block
;
37543 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37545 strcat (p_name
, " parallel");
37546 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
37547 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37548 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
37549 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
37550 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
37552 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37554 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37555 if (cclauses
== NULL
)
37556 cclauses
= cclauses_buf
;
37558 cp_lexer_consume_token (parser
->lexer
);
37559 if (!flag_openmp
) /* flag_openmp_simd */
37560 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37562 block
= begin_omp_parallel ();
37563 save
= cp_parser_begin_omp_structured_block (parser
);
37564 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37566 cp_parser_end_omp_structured_block (parser
, save
);
37567 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37569 if (ret
== NULL_TREE
)
37571 OMP_PARALLEL_COMBINED (stmt
) = 1;
37574 /* When combined with distribute, parallel has to be followed by for.
37575 #pragma omp target parallel is allowed though. */
37577 && (mask
& (OMP_CLAUSE_MASK_1
37578 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37580 error_at (loc
, "expected %<for%> after %qs", p_name
);
37581 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37584 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37586 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37587 const char *p
= IDENTIFIER_POINTER (id
);
37588 if (strcmp (p
, "master") == 0)
37590 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37591 cclauses
= cclauses_buf
;
37593 cp_lexer_consume_token (parser
->lexer
);
37594 block
= begin_omp_parallel ();
37595 save
= cp_parser_begin_omp_structured_block (parser
);
37596 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
37598 cp_parser_end_omp_structured_block (parser
, save
);
37599 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37601 OMP_PARALLEL_COMBINED (stmt
) = 1;
37602 if (ret
== NULL_TREE
)
37606 else if (!flag_openmp
) /* flag_openmp_simd */
37608 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37611 else if (strcmp (p
, "sections") == 0)
37613 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37614 cclauses
= cclauses_buf
;
37616 cp_lexer_consume_token (parser
->lexer
);
37617 block
= begin_omp_parallel ();
37618 save
= cp_parser_begin_omp_structured_block (parser
);
37619 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
37620 cp_parser_end_omp_structured_block (parser
, save
);
37621 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37623 OMP_PARALLEL_COMBINED (stmt
) = 1;
37627 else if (!flag_openmp
) /* flag_openmp_simd */
37629 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37633 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37637 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
37638 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
37641 block
= begin_omp_parallel ();
37642 save
= cp_parser_begin_omp_structured_block (parser
);
37643 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37644 cp_parser_end_omp_structured_block (parser
, save
);
37645 stmt
= finish_omp_parallel (clauses
, block
);
37650 # pragma omp single single-clause[optseq] new-line
37651 structured-block */
37653 #define OMP_SINGLE_CLAUSE_MASK \
37654 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37660 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37662 tree stmt
= make_node (OMP_SINGLE
);
37663 TREE_TYPE (stmt
) = void_type_node
;
37664 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37666 OMP_SINGLE_CLAUSES (stmt
)
37667 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
37668 "#pragma omp single", pragma_tok
);
37669 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37671 return add_stmt (stmt
);
37675 # pragma omp task task-clause[optseq] new-line
37676 structured-block */
37678 #define OMP_TASK_CLAUSE_MASK \
37679 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37692 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37694 tree clauses
, block
;
37697 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
37698 "#pragma omp task", pragma_tok
);
37699 block
= begin_omp_task ();
37700 save
= cp_parser_begin_omp_structured_block (parser
);
37701 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37702 cp_parser_end_omp_structured_block (parser
, save
);
37703 return finish_omp_task (clauses
, block
);
37707 # pragma omp taskwait new-line
37710 # pragma omp taskwait taskwait-clause[opt] new-line */
37712 #define OMP_TASKWAIT_CLAUSE_MASK \
37713 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37716 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
37719 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
37720 "#pragma omp taskwait", pragma_tok
);
37724 tree stmt
= make_node (OMP_TASK
);
37725 TREE_TYPE (stmt
) = void_node
;
37726 OMP_TASK_CLAUSES (stmt
) = clauses
;
37727 OMP_TASK_BODY (stmt
) = NULL_TREE
;
37728 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37732 finish_omp_taskwait ();
37736 # pragma omp taskyield new-line */
37739 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
37741 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37742 finish_omp_taskyield ();
37746 # pragma omp taskgroup new-line
37750 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37752 #define OMP_TASKGROUP_CLAUSE_MASK \
37753 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37756 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37759 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
37760 "#pragma omp taskgroup", pragma_tok
);
37761 return c_finish_omp_taskgroup (input_location
,
37762 cp_parser_omp_structured_block (parser
,
37769 # pragma omp threadprivate (variable-list) */
37772 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
37776 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
37777 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37779 finish_omp_threadprivate (vars
);
37783 # pragma omp cancel cancel-clause[optseq] new-line */
37785 #define OMP_CANCEL_CLAUSE_MASK \
37786 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37793 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
37795 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
37796 "#pragma omp cancel", pragma_tok
);
37797 finish_omp_cancel (clauses
);
37801 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37803 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37804 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37810 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
37811 enum pragma_context context
)
37814 bool point_seen
= false;
37816 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37818 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37819 const char *p
= IDENTIFIER_POINTER (id
);
37821 if (strcmp (p
, "point") == 0)
37823 cp_lexer_consume_token (parser
->lexer
);
37829 cp_parser_error (parser
, "expected %<point%>");
37830 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37834 if (context
!= pragma_compound
)
37836 if (context
== pragma_stmt
)
37837 error_at (pragma_tok
->location
,
37838 "%<#pragma %s%> may only be used in compound statements",
37839 "omp cancellation point");
37841 cp_parser_error (parser
, "expected declaration specifiers");
37842 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37846 clauses
= cp_parser_omp_all_clauses (parser
,
37847 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
37848 "#pragma omp cancellation point",
37850 finish_omp_cancellation_point (clauses
);
37854 #pragma omp distribute distribute-clause[optseq] new-line
37857 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37858 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37865 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
37866 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37869 tree clauses
, sb
, ret
;
37871 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37873 strcat (p_name
, " distribute");
37874 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
37876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37878 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37879 const char *p
= IDENTIFIER_POINTER (id
);
37881 bool parallel
= false;
37883 if (strcmp (p
, "simd") == 0)
37886 parallel
= strcmp (p
, "parallel") == 0;
37887 if (parallel
|| simd
)
37889 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37890 if (cclauses
== NULL
)
37891 cclauses
= cclauses_buf
;
37892 cp_lexer_consume_token (parser
->lexer
);
37893 if (!flag_openmp
) /* flag_openmp_simd */
37896 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37899 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37902 sb
= begin_omp_structured_block ();
37903 save
= cp_parser_begin_omp_structured_block (parser
);
37905 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37908 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37910 cp_parser_end_omp_structured_block (parser
, save
);
37911 tree body
= finish_omp_structured_block (sb
);
37914 ret
= make_node (OMP_DISTRIBUTE
);
37915 TREE_TYPE (ret
) = void_type_node
;
37916 OMP_FOR_BODY (ret
) = body
;
37917 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37918 SET_EXPR_LOCATION (ret
, loc
);
37923 if (!flag_openmp
) /* flag_openmp_simd */
37925 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37929 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37933 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
37934 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37937 keep_next_level (true);
37938 sb
= begin_omp_structured_block ();
37939 save
= cp_parser_begin_omp_structured_block (parser
);
37941 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
37943 cp_parser_end_omp_structured_block (parser
, save
);
37944 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37950 # pragma omp teams teams-clause[optseq] new-line
37951 structured-block */
37953 #define OMP_TEAMS_CLAUSE_MASK \
37954 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37963 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
37964 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37967 tree clauses
, sb
, ret
;
37969 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37971 strcat (p_name
, " teams");
37972 mask
|= OMP_TEAMS_CLAUSE_MASK
;
37974 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37976 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37977 const char *p
= IDENTIFIER_POINTER (id
);
37978 if (strcmp (p
, "distribute") == 0)
37980 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37981 if (cclauses
== NULL
)
37982 cclauses
= cclauses_buf
;
37984 cp_lexer_consume_token (parser
->lexer
);
37985 if (!flag_openmp
) /* flag_openmp_simd */
37986 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37988 keep_next_level (true);
37989 sb
= begin_omp_structured_block ();
37990 save
= cp_parser_begin_omp_structured_block (parser
);
37991 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37993 cp_parser_end_omp_structured_block (parser
, save
);
37994 tree body
= finish_omp_structured_block (sb
);
37997 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37998 ret
= make_node (OMP_TEAMS
);
37999 TREE_TYPE (ret
) = void_type_node
;
38000 OMP_TEAMS_CLAUSES (ret
) = clauses
;
38001 OMP_TEAMS_BODY (ret
) = body
;
38002 OMP_TEAMS_COMBINED (ret
) = 1;
38003 SET_EXPR_LOCATION (ret
, loc
);
38004 return add_stmt (ret
);
38007 if (!flag_openmp
) /* flag_openmp_simd */
38009 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38013 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38017 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
38018 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38021 tree stmt
= make_node (OMP_TEAMS
);
38022 TREE_TYPE (stmt
) = void_type_node
;
38023 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
38024 keep_next_level (true);
38025 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38026 SET_EXPR_LOCATION (stmt
, loc
);
38028 return add_stmt (stmt
);
38032 # pragma omp target data target-data-clause[optseq] new-line
38033 structured-block */
38035 #define OMP_TARGET_DATA_CLAUSE_MASK \
38036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38042 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38045 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
38046 "#pragma omp target data", pragma_tok
);
38048 for (tree
*pc
= &clauses
; *pc
;)
38050 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38051 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38054 case GOMP_MAP_ALWAYS_TO
:
38055 case GOMP_MAP_FROM
:
38056 case GOMP_MAP_ALWAYS_FROM
:
38057 case GOMP_MAP_TOFROM
:
38058 case GOMP_MAP_ALWAYS_TOFROM
:
38059 case GOMP_MAP_ALLOC
:
38062 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38063 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38064 case GOMP_MAP_ALWAYS_POINTER
:
38068 error_at (OMP_CLAUSE_LOCATION (*pc
),
38069 "%<#pragma omp target data%> with map-type other "
38070 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38071 "on %<map%> clause");
38072 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38075 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
)
38077 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38083 error_at (pragma_tok
->location
,
38084 "%<#pragma omp target data%> must contain at least "
38085 "one %<map%> or %<use_device_ptr%> clause");
38089 tree stmt
= make_node (OMP_TARGET_DATA
);
38090 TREE_TYPE (stmt
) = void_type_node
;
38091 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
38093 keep_next_level (true);
38094 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38096 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38097 return add_stmt (stmt
);
38101 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38102 structured-block */
38104 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38105 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38112 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38113 enum pragma_context context
)
38115 bool data_seen
= false;
38116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38118 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38119 const char *p
= IDENTIFIER_POINTER (id
);
38121 if (strcmp (p
, "data") == 0)
38123 cp_lexer_consume_token (parser
->lexer
);
38129 cp_parser_error (parser
, "expected %<data%>");
38130 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38134 if (context
== pragma_stmt
)
38136 error_at (pragma_tok
->location
,
38137 "%<#pragma %s%> may only be used in compound statements",
38138 "omp target enter data");
38139 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38144 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
38145 "#pragma omp target enter data", pragma_tok
);
38147 for (tree
*pc
= &clauses
; *pc
;)
38149 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38150 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38153 case GOMP_MAP_ALWAYS_TO
:
38154 case GOMP_MAP_ALLOC
:
38157 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38158 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38159 case GOMP_MAP_ALWAYS_POINTER
:
38163 error_at (OMP_CLAUSE_LOCATION (*pc
),
38164 "%<#pragma omp target enter data%> with map-type other "
38165 "than %<to%> or %<alloc%> on %<map%> clause");
38166 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38169 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38175 error_at (pragma_tok
->location
,
38176 "%<#pragma omp target enter data%> must contain at least "
38177 "one %<map%> clause");
38181 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
38182 TREE_TYPE (stmt
) = void_type_node
;
38183 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
38184 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38185 return add_stmt (stmt
);
38189 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38190 structured-block */
38192 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38193 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38200 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38201 enum pragma_context context
)
38203 bool data_seen
= false;
38204 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38206 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38207 const char *p
= IDENTIFIER_POINTER (id
);
38209 if (strcmp (p
, "data") == 0)
38211 cp_lexer_consume_token (parser
->lexer
);
38217 cp_parser_error (parser
, "expected %<data%>");
38218 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38222 if (context
== pragma_stmt
)
38224 error_at (pragma_tok
->location
,
38225 "%<#pragma %s%> may only be used in compound statements",
38226 "omp target exit data");
38227 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38232 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
38233 "#pragma omp target exit data", pragma_tok
);
38235 for (tree
*pc
= &clauses
; *pc
;)
38237 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38238 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38240 case GOMP_MAP_FROM
:
38241 case GOMP_MAP_ALWAYS_FROM
:
38242 case GOMP_MAP_RELEASE
:
38243 case GOMP_MAP_DELETE
:
38246 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38247 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38248 case GOMP_MAP_ALWAYS_POINTER
:
38252 error_at (OMP_CLAUSE_LOCATION (*pc
),
38253 "%<#pragma omp target exit data%> with map-type other "
38254 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38256 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38259 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38265 error_at (pragma_tok
->location
,
38266 "%<#pragma omp target exit data%> must contain at least "
38267 "one %<map%> clause");
38271 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
38272 TREE_TYPE (stmt
) = void_type_node
;
38273 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
38274 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38275 return add_stmt (stmt
);
38279 # pragma omp target update target-update-clause[optseq] new-line */
38281 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38282 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38290 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
38291 enum pragma_context context
)
38293 if (context
== pragma_stmt
)
38295 error_at (pragma_tok
->location
,
38296 "%<#pragma %s%> may only be used in compound statements",
38297 "omp target update");
38298 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38303 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
38304 "#pragma omp target update", pragma_tok
);
38305 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
38306 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
38308 error_at (pragma_tok
->location
,
38309 "%<#pragma omp target update%> must contain at least one "
38310 "%<from%> or %<to%> clauses");
38314 tree stmt
= make_node (OMP_TARGET_UPDATE
);
38315 TREE_TYPE (stmt
) = void_type_node
;
38316 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
38317 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38323 # pragma omp target target-clause[optseq] new-line
38324 structured-block */
38326 #define OMP_TARGET_CLAUSE_MASK \
38327 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38338 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
38339 enum pragma_context context
, bool *if_p
)
38341 tree
*pc
= NULL
, stmt
;
38345 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
38347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38349 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38350 const char *p
= IDENTIFIER_POINTER (id
);
38351 enum tree_code ccode
= ERROR_MARK
;
38353 if (strcmp (p
, "teams") == 0)
38355 else if (strcmp (p
, "parallel") == 0)
38356 ccode
= OMP_PARALLEL
;
38357 else if (strcmp (p
, "simd") == 0)
38359 if (ccode
!= ERROR_MARK
)
38361 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
38362 char p_name
[sizeof ("#pragma omp target teams distribute "
38363 "parallel for simd")];
38365 cp_lexer_consume_token (parser
->lexer
);
38366 strcpy (p_name
, "#pragma omp target");
38367 if (!flag_openmp
) /* flag_openmp_simd */
38373 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38374 OMP_TARGET_CLAUSE_MASK
,
38378 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38379 OMP_TARGET_CLAUSE_MASK
,
38383 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38384 OMP_TARGET_CLAUSE_MASK
,
38388 gcc_unreachable ();
38390 return stmt
!= NULL_TREE
;
38392 keep_next_level (true);
38393 tree sb
= begin_omp_structured_block (), ret
;
38394 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
38398 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38399 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38403 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38404 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38408 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38409 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38413 gcc_unreachable ();
38415 cp_parser_end_omp_structured_block (parser
, save
);
38416 tree body
= finish_omp_structured_block (sb
);
38417 if (ret
== NULL_TREE
)
38419 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
38421 /* For combined target teams, ensure the num_teams and
38422 thread_limit clause expressions are evaluated on the host,
38423 before entering the target construct. */
38425 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38426 c
; c
= OMP_CLAUSE_CHAIN (c
))
38427 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
38428 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
38429 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
38431 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
38432 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
38433 if (expr
== error_mark_node
)
38435 tree tmp
= TARGET_EXPR_SLOT (expr
);
38437 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
38438 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
38439 OMP_CLAUSE_FIRSTPRIVATE
);
38440 OMP_CLAUSE_DECL (tc
) = tmp
;
38441 OMP_CLAUSE_CHAIN (tc
)
38442 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38443 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
38446 tree stmt
= make_node (OMP_TARGET
);
38447 TREE_TYPE (stmt
) = void_type_node
;
38448 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38449 OMP_TARGET_BODY (stmt
) = body
;
38450 OMP_TARGET_COMBINED (stmt
) = 1;
38451 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38453 pc
= &OMP_TARGET_CLAUSES (stmt
);
38454 goto check_clauses
;
38456 else if (!flag_openmp
) /* flag_openmp_simd */
38458 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38461 else if (strcmp (p
, "data") == 0)
38463 cp_lexer_consume_token (parser
->lexer
);
38464 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
38467 else if (strcmp (p
, "enter") == 0)
38469 cp_lexer_consume_token (parser
->lexer
);
38470 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
38473 else if (strcmp (p
, "exit") == 0)
38475 cp_lexer_consume_token (parser
->lexer
);
38476 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
38479 else if (strcmp (p
, "update") == 0)
38481 cp_lexer_consume_token (parser
->lexer
);
38482 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
38485 if (!flag_openmp
) /* flag_openmp_simd */
38487 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38491 stmt
= make_node (OMP_TARGET
);
38492 TREE_TYPE (stmt
) = void_type_node
;
38494 OMP_TARGET_CLAUSES (stmt
)
38495 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
38496 "#pragma omp target", pragma_tok
);
38497 pc
= &OMP_TARGET_CLAUSES (stmt
);
38498 keep_next_level (true);
38499 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38501 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38507 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38508 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38511 case GOMP_MAP_ALWAYS_TO
:
38512 case GOMP_MAP_FROM
:
38513 case GOMP_MAP_ALWAYS_FROM
:
38514 case GOMP_MAP_TOFROM
:
38515 case GOMP_MAP_ALWAYS_TOFROM
:
38516 case GOMP_MAP_ALLOC
:
38517 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38518 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38519 case GOMP_MAP_ALWAYS_POINTER
:
38522 error_at (OMP_CLAUSE_LOCATION (*pc
),
38523 "%<#pragma omp target%> with map-type other "
38524 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38525 "on %<map%> clause");
38526 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38529 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38535 # pragma acc cache (variable-list) new-line
38539 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
38541 tree stmt
, clauses
;
38543 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
38544 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38546 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
38548 stmt
= make_node (OACC_CACHE
);
38549 TREE_TYPE (stmt
) = void_type_node
;
38550 OACC_CACHE_CLAUSES (stmt
) = clauses
;
38551 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38558 # pragma acc data oacc-data-clause[optseq] new-line
38559 structured-block */
38561 #define OACC_DATA_CLAUSE_MASK \
38562 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38571 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38573 tree stmt
, clauses
, block
;
38576 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
38577 "#pragma acc data", pragma_tok
);
38579 block
= begin_omp_parallel ();
38580 save
= cp_parser_begin_omp_structured_block (parser
);
38581 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38582 cp_parser_end_omp_structured_block (parser
, save
);
38583 stmt
= finish_oacc_data (clauses
, block
);
38588 # pragma acc host_data <clauses> new-line
38589 structured-block */
38591 #define OACC_HOST_DATA_CLAUSE_MASK \
38592 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38595 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38597 tree stmt
, clauses
, block
;
38600 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
38601 "#pragma acc host_data", pragma_tok
);
38603 block
= begin_omp_parallel ();
38604 save
= cp_parser_begin_omp_structured_block (parser
);
38605 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38606 cp_parser_end_omp_structured_block (parser
, save
);
38607 stmt
= finish_oacc_host_data (clauses
, block
);
38612 # pragma acc declare oacc-data-clause[optseq] new-line
38615 #define OACC_DECLARE_CLAUSE_MASK \
38616 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38626 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
38628 tree clauses
, stmt
;
38629 bool error
= false;
38631 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
38632 "#pragma acc declare", pragma_tok
, true);
38635 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38637 error_at (pragma_tok
->location
,
38638 "no valid clauses specified in %<#pragma acc declare%>");
38642 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
38644 location_t loc
= OMP_CLAUSE_LOCATION (t
);
38645 tree decl
= OMP_CLAUSE_DECL (t
);
38646 if (!DECL_P (decl
))
38648 error_at (loc
, "array section in %<#pragma acc declare%>");
38652 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
38653 switch (OMP_CLAUSE_MAP_KIND (t
))
38655 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38656 case GOMP_MAP_ALLOC
:
38658 case GOMP_MAP_FORCE_DEVICEPTR
:
38659 case GOMP_MAP_DEVICE_RESIDENT
:
38662 case GOMP_MAP_LINK
:
38663 if (!global_bindings_p ()
38664 && (TREE_STATIC (decl
)
38665 || !DECL_EXTERNAL (decl
)))
38668 "%qD must be a global variable in "
38669 "%<#pragma acc declare link%>",
38677 if (global_bindings_p ())
38679 error_at (loc
, "invalid OpenACC clause at file scope");
38683 if (DECL_EXTERNAL (decl
))
38686 "invalid use of %<extern%> variable %qD "
38687 "in %<#pragma acc declare%>", decl
);
38691 else if (TREE_PUBLIC (decl
))
38694 "invalid use of %<global%> variable %qD "
38695 "in %<#pragma acc declare%>", decl
);
38702 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
38703 || lookup_attribute ("omp declare target link",
38704 DECL_ATTRIBUTES (decl
)))
38706 error_at (loc
, "variable %qD used more than once with "
38707 "%<#pragma acc declare%>", decl
);
38716 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
38717 id
= get_identifier ("omp declare target link");
38719 id
= get_identifier ("omp declare target");
38721 DECL_ATTRIBUTES (decl
)
38722 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
38723 if (global_bindings_p ())
38725 symtab_node
*node
= symtab_node::get (decl
);
38728 node
->offloadable
= 1;
38729 if (ENABLE_OFFLOADING
)
38731 g
->have_offload
= true;
38732 if (is_a
<varpool_node
*> (node
))
38733 vec_safe_push (offload_vars
, decl
);
38740 if (error
|| global_bindings_p ())
38743 stmt
= make_node (OACC_DECLARE
);
38744 TREE_TYPE (stmt
) = void_type_node
;
38745 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
38746 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38754 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38758 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38760 LOC is the location of the #pragma token.
38763 #define OACC_ENTER_DATA_CLAUSE_MASK \
38764 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38770 #define OACC_EXIT_DATA_CLAUSE_MASK \
38771 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38779 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38782 location_t loc
= pragma_tok
->location
;
38783 tree stmt
, clauses
;
38784 const char *p
= "";
38786 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38787 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38789 if (strcmp (p
, "data") != 0)
38791 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
38792 enter
? "enter" : "exit");
38793 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38797 cp_lexer_consume_token (parser
->lexer
);
38800 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
38801 "#pragma acc enter data", pragma_tok
);
38803 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
38804 "#pragma acc exit data", pragma_tok
);
38806 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38808 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
38809 enter
? "enter" : "exit");
38813 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
38814 TREE_TYPE (stmt
) = void_type_node
;
38815 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
38816 SET_EXPR_LOCATION (stmt
, loc
);
38822 # pragma acc loop oacc-loop-clause[optseq] new-line
38823 structured-block */
38825 #define OACC_LOOP_CLAUSE_MASK \
38826 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38838 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
38839 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
38841 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
38843 strcat (p_name
, " loop");
38844 mask
|= OACC_LOOP_CLAUSE_MASK
;
38846 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38850 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
38852 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
38854 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38857 tree block
= begin_omp_structured_block ();
38858 int save
= cp_parser_begin_omp_structured_block (parser
);
38859 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
38860 cp_parser_end_omp_structured_block (parser
, save
);
38861 add_stmt (finish_omp_structured_block (block
));
38867 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38872 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38876 #define OACC_KERNELS_CLAUSE_MASK \
38877 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38891 #define OACC_PARALLEL_CLAUSE_MASK \
38892 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38910 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
38911 char *p_name
, bool *if_p
)
38913 omp_clause_mask mask
;
38914 enum tree_code code
;
38915 switch (cp_parser_pragma_kind (pragma_tok
))
38917 case PRAGMA_OACC_KERNELS
:
38918 strcat (p_name
, " kernels");
38919 mask
= OACC_KERNELS_CLAUSE_MASK
;
38920 code
= OACC_KERNELS
;
38922 case PRAGMA_OACC_PARALLEL
:
38923 strcat (p_name
, " parallel");
38924 mask
= OACC_PARALLEL_CLAUSE_MASK
;
38925 code
= OACC_PARALLEL
;
38928 gcc_unreachable ();
38931 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38934 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38935 if (strcmp (p
, "loop") == 0)
38937 cp_lexer_consume_token (parser
->lexer
);
38938 tree block
= begin_omp_parallel ();
38940 tree stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
,
38942 protected_set_expr_location (stmt
, pragma_tok
->location
);
38943 return finish_omp_construct (code
, block
, clauses
);
38947 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
38949 tree block
= begin_omp_parallel ();
38950 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
38951 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38952 cp_parser_end_omp_structured_block (parser
, save
);
38953 return finish_omp_construct (code
, block
, clauses
);
38957 # pragma acc update oacc-update-clause[optseq] new-line
38960 #define OACC_UPDATE_CLAUSE_MASK \
38961 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38969 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
38971 tree stmt
, clauses
;
38973 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
38974 "#pragma acc update", pragma_tok
);
38976 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38978 error_at (pragma_tok
->location
,
38979 "%<#pragma acc update%> must contain at least one "
38980 "%<device%> or %<host%> or %<self%> clause");
38984 stmt
= make_node (OACC_UPDATE
);
38985 TREE_TYPE (stmt
) = void_type_node
;
38986 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
38987 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38993 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38995 LOC is the location of the #pragma token.
38998 #define OACC_WAIT_CLAUSE_MASK \
38999 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
39002 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
39004 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
39005 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39007 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
39008 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
39010 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
39011 "#pragma acc wait", pragma_tok
);
39013 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
39014 stmt
= finish_expr_stmt (stmt
);
39020 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39022 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39023 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39031 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
39032 enum pragma_context context
)
39034 bool first_p
= parser
->omp_declare_simd
== NULL
;
39035 cp_omp_declare_simd_data data
;
39038 data
.error_seen
= false;
39039 data
.fndecl_seen
= false;
39040 data
.tokens
= vNULL
;
39041 data
.clauses
= NULL_TREE
;
39042 /* It is safe to take the address of a local variable; it will only be
39043 used while this scope is live. */
39044 parser
->omp_declare_simd
= &data
;
39047 /* Store away all pragma tokens. */
39048 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39049 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39050 cp_lexer_consume_token (parser
->lexer
);
39051 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39052 parser
->omp_declare_simd
->error_seen
= true;
39053 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39054 struct cp_token_cache
*cp
39055 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
39056 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
39060 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
39061 cp_parser_pragma (parser
, context
, NULL
);
39064 case pragma_external
:
39065 cp_parser_declaration (parser
);
39067 case pragma_member
:
39068 cp_parser_member_declaration (parser
);
39070 case pragma_objc_icode
:
39071 cp_parser_block_declaration (parser
, /*statement_p=*/false);
39074 cp_parser_declaration_statement (parser
);
39077 if (parser
->omp_declare_simd
39078 && !parser
->omp_declare_simd
->error_seen
39079 && !parser
->omp_declare_simd
->fndecl_seen
)
39080 error_at (pragma_tok
->location
,
39081 "%<#pragma omp declare simd%> not immediately followed by "
39082 "function declaration or definition");
39083 data
.tokens
.release ();
39084 parser
->omp_declare_simd
= NULL
;
39088 /* Finalize #pragma omp declare simd clauses after direct declarator has
39089 been parsed, and put that into "omp declare simd" attribute. */
39092 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
39094 struct cp_token_cache
*ce
;
39095 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
39098 if (!data
->error_seen
&& data
->fndecl_seen
)
39100 error ("%<#pragma omp declare simd%> not immediately followed by "
39101 "a single function declaration or definition");
39102 data
->error_seen
= true;
39104 if (data
->error_seen
)
39107 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
39111 cp_parser_push_lexer_for_tokens (parser
, ce
);
39112 parser
->lexer
->in_pragma
= true;
39113 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
39114 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
39115 cp_lexer_consume_token (parser
->lexer
);
39116 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
39117 "#pragma omp declare simd", pragma_tok
);
39118 cp_parser_pop_lexer (parser
);
39120 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
39121 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
39122 TREE_CHAIN (c
) = attrs
;
39123 if (processing_template_decl
)
39124 ATTR_IS_DEPENDENT (c
) = 1;
39128 data
->fndecl_seen
= true;
39134 # pragma omp declare target new-line
39135 declarations and definitions
39136 # pragma omp end declare target new-line
39139 # pragma omp declare target ( extended-list ) new-line
39141 # pragma omp declare target declare-target-clauses[seq] new-line */
39143 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39144 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39148 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39150 tree clauses
= NULL_TREE
;
39151 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39153 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
39154 "#pragma omp declare target", pragma_tok
);
39155 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39157 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
39159 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
39160 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39164 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39165 scope_chain
->omp_declare_target_attribute
++;
39168 if (scope_chain
->omp_declare_target_attribute
)
39169 error_at (pragma_tok
->location
,
39170 "%<#pragma omp declare target%> with clauses in between "
39171 "%<#pragma omp declare target%> without clauses and "
39172 "%<#pragma omp end declare target%>");
39173 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
39175 tree t
= OMP_CLAUSE_DECL (c
), id
;
39176 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
39177 tree at2
= lookup_attribute ("omp declare target link",
39178 DECL_ATTRIBUTES (t
));
39179 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
39181 id
= get_identifier ("omp declare target link");
39182 std::swap (at1
, at2
);
39185 id
= get_identifier ("omp declare target");
39188 error_at (OMP_CLAUSE_LOCATION (c
),
39189 "%qD specified both in declare target %<link%> and %<to%>"
39195 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
39196 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
39199 symtab_node
*node
= symtab_node::get (t
);
39202 node
->offloadable
= 1;
39203 if (ENABLE_OFFLOADING
)
39205 g
->have_offload
= true;
39206 if (is_a
<varpool_node
*> (node
))
39207 vec_safe_push (offload_vars
, t
);
39215 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39217 const char *p
= "";
39218 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39220 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39221 p
= IDENTIFIER_POINTER (id
);
39223 if (strcmp (p
, "declare") == 0)
39225 cp_lexer_consume_token (parser
->lexer
);
39227 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39229 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39230 p
= IDENTIFIER_POINTER (id
);
39232 if (strcmp (p
, "target") == 0)
39233 cp_lexer_consume_token (parser
->lexer
);
39236 cp_parser_error (parser
, "expected %<target%>");
39237 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39243 cp_parser_error (parser
, "expected %<declare%>");
39244 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39247 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39248 if (!scope_chain
->omp_declare_target_attribute
)
39249 error_at (pragma_tok
->location
,
39250 "%<#pragma omp end declare target%> without corresponding "
39251 "%<#pragma omp declare target%>");
39253 scope_chain
->omp_declare_target_attribute
--;
39256 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39257 expression and optional initializer clause of
39258 #pragma omp declare reduction. We store the expression(s) as
39259 either 3, 6 or 7 special statements inside of the artificial function's
39260 body. The first two statements are DECL_EXPRs for the artificial
39261 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39262 expression that uses those variables.
39263 If there was any INITIALIZER clause, this is followed by further statements,
39264 the fourth and fifth statements are DECL_EXPRs for the artificial
39265 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39266 constructor variant (first token after open paren is not omp_priv),
39267 then the sixth statement is a statement with the function call expression
39268 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39269 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39270 to initialize the OMP_PRIV artificial variable and there is seventh
39271 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39274 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
39276 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
39277 gcc_assert (TYPE_REF_P (type
));
39278 type
= TREE_TYPE (type
);
39279 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
39280 DECL_ARTIFICIAL (omp_out
) = 1;
39281 pushdecl (omp_out
);
39282 add_decl_expr (omp_out
);
39283 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
39284 DECL_ARTIFICIAL (omp_in
) = 1;
39286 add_decl_expr (omp_in
);
39288 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
39290 keep_next_level (true);
39291 tree block
= begin_omp_structured_block ();
39292 combiner
= cp_parser_expression (parser
);
39293 finish_expr_stmt (combiner
);
39294 block
= finish_omp_structured_block (block
);
39297 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
39300 const char *p
= "";
39301 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39303 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39304 p
= IDENTIFIER_POINTER (id
);
39307 if (strcmp (p
, "initializer") == 0)
39309 cp_lexer_consume_token (parser
->lexer
);
39310 matching_parens parens
;
39311 if (!parens
.require_open (parser
))
39315 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39317 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39318 p
= IDENTIFIER_POINTER (id
);
39321 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
39322 DECL_ARTIFICIAL (omp_priv
) = 1;
39323 pushdecl (omp_priv
);
39324 add_decl_expr (omp_priv
);
39325 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
39326 DECL_ARTIFICIAL (omp_orig
) = 1;
39327 pushdecl (omp_orig
);
39328 add_decl_expr (omp_orig
);
39330 keep_next_level (true);
39331 block
= begin_omp_structured_block ();
39334 if (strcmp (p
, "omp_priv") == 0)
39336 bool is_direct_init
, is_non_constant_init
;
39338 cp_lexer_consume_token (parser
->lexer
);
39339 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
39341 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39342 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
39344 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
39345 == CPP_CLOSE_PAREN
))
39347 finish_omp_structured_block (block
);
39348 error ("invalid initializer clause");
39351 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
39352 &is_non_constant_init
);
39353 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
39354 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
39358 cp_parser_parse_tentatively (parser
);
39359 /* Don't create location wrapper nodes here. */
39360 auto_suppress_location_wrappers sentinel
;
39361 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
39362 /*check_dependency_p=*/true,
39363 /*template_p=*/NULL
,
39364 /*declarator_p=*/false,
39365 /*optional_p=*/false);
39366 vec
<tree
, va_gc
> *args
;
39367 if (fn_name
== error_mark_node
39368 || cp_parser_error_occurred (parser
)
39369 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39370 || ((args
= cp_parser_parenthesized_expression_list
39371 (parser
, non_attr
, /*cast_p=*/false,
39372 /*allow_expansion_p=*/true,
39373 /*non_constant_p=*/NULL
)),
39374 cp_parser_error_occurred (parser
)))
39376 finish_omp_structured_block (block
);
39377 cp_parser_abort_tentative_parse (parser
);
39378 cp_parser_error (parser
, "expected id-expression (arguments)");
39383 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
39384 if (arg
== omp_priv
39385 || (TREE_CODE (arg
) == ADDR_EXPR
39386 && TREE_OPERAND (arg
, 0) == omp_priv
))
39388 cp_parser_abort_tentative_parse (parser
);
39389 if (arg
== NULL_TREE
)
39390 error ("one of the initializer call arguments should be %<omp_priv%>"
39391 " or %<&omp_priv%>");
39392 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
39394 finish_expr_stmt (initializer
);
39397 block
= finish_omp_structured_block (block
);
39398 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
39402 add_decl_expr (omp_orig
);
39404 if (!parens
.require_close (parser
))
39408 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
39409 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
39416 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39417 initializer-clause[opt] new-line
39419 initializer-clause:
39420 initializer (omp_priv initializer)
39421 initializer (function-name (argument-list)) */
39424 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
39425 enum pragma_context
)
39427 auto_vec
<tree
> types
;
39428 enum tree_code reduc_code
= ERROR_MARK
;
39429 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
39431 cp_token
*first_token
;
39432 cp_token_cache
*cp
;
39436 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39437 p
= obstack_alloc (&declarator_obstack
, 0);
39439 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39442 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
39445 reduc_code
= PLUS_EXPR
;
39448 reduc_code
= MULT_EXPR
;
39451 reduc_code
= MINUS_EXPR
;
39454 reduc_code
= BIT_AND_EXPR
;
39457 reduc_code
= BIT_XOR_EXPR
;
39460 reduc_code
= BIT_IOR_EXPR
;
39463 reduc_code
= TRUTH_ANDIF_EXPR
;
39466 reduc_code
= TRUTH_ORIF_EXPR
;
39469 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
39472 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39473 "%<|%>, %<&&%>, %<||%> or identifier");
39477 if (reduc_code
!= ERROR_MARK
)
39478 cp_lexer_consume_token (parser
->lexer
);
39480 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
39481 if (reduc_id
== error_mark_node
)
39484 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39487 /* Types may not be defined in declare reduction type list. */
39488 const char *saved_message
;
39489 saved_message
= parser
->type_definition_forbidden_message
;
39490 parser
->type_definition_forbidden_message
39491 = G_("types may not be defined in declare reduction type list");
39492 bool saved_colon_corrects_to_scope_p
;
39493 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
39494 parser
->colon_corrects_to_scope_p
= false;
39495 bool saved_colon_doesnt_start_class_def_p
;
39496 saved_colon_doesnt_start_class_def_p
39497 = parser
->colon_doesnt_start_class_def_p
;
39498 parser
->colon_doesnt_start_class_def_p
= true;
39502 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39503 type
= cp_parser_type_id (parser
);
39504 if (type
== error_mark_node
)
39506 else if (ARITHMETIC_TYPE_P (type
)
39507 && (orig_reduc_id
== NULL_TREE
39508 || (TREE_CODE (type
) != COMPLEX_TYPE
39509 && (id_equal (orig_reduc_id
, "min")
39510 || id_equal (orig_reduc_id
, "max")))))
39511 error_at (loc
, "predeclared arithmetic type %qT in "
39512 "%<#pragma omp declare reduction%>", type
);
39513 else if (TREE_CODE (type
) == FUNCTION_TYPE
39514 || TREE_CODE (type
) == METHOD_TYPE
39515 || TREE_CODE (type
) == ARRAY_TYPE
)
39516 error_at (loc
, "function or array type %qT in "
39517 "%<#pragma omp declare reduction%>", type
);
39518 else if (TYPE_REF_P (type
))
39519 error_at (loc
, "reference type %qT in "
39520 "%<#pragma omp declare reduction%>", type
);
39521 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
39522 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
39523 "%<#pragma omp declare reduction%>", type
);
39525 types
.safe_push (type
);
39527 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39528 cp_lexer_consume_token (parser
->lexer
);
39533 /* Restore the saved message. */
39534 parser
->type_definition_forbidden_message
= saved_message
;
39535 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39536 parser
->colon_doesnt_start_class_def_p
39537 = saved_colon_doesnt_start_class_def_p
;
39539 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
39540 || types
.is_empty ())
39543 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39547 first_token
= cp_lexer_peek_token (parser
->lexer
);
39550 FOR_EACH_VEC_ELT (types
, i
, type
)
39553 = build_function_type_list (void_type_node
,
39554 cp_build_reference_type (type
, false),
39556 tree this_reduc_id
= reduc_id
;
39557 if (!dependent_type_p (type
))
39558 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
39559 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
39560 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
39561 DECL_ARTIFICIAL (fndecl
) = 1;
39562 DECL_EXTERNAL (fndecl
) = 1;
39563 DECL_DECLARED_INLINE_P (fndecl
) = 1;
39564 DECL_IGNORED_P (fndecl
) = 1;
39565 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
39566 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
39567 DECL_ATTRIBUTES (fndecl
)
39568 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
39569 DECL_ATTRIBUTES (fndecl
));
39570 if (processing_template_decl
)
39571 fndecl
= push_template_decl (fndecl
);
39572 bool block_scope
= false;
39573 tree block
= NULL_TREE
;
39574 if (current_function_decl
)
39576 block_scope
= true;
39577 DECL_CONTEXT (fndecl
) = global_namespace
;
39578 if (!processing_template_decl
)
39581 else if (current_class_type
)
39585 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39586 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39587 cp_lexer_consume_token (parser
->lexer
);
39588 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39590 cp
= cp_token_cache_new (first_token
,
39591 cp_lexer_peek_nth_token (parser
->lexer
,
39594 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
39595 finish_member_declaration (fndecl
);
39596 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
39597 DECL_PENDING_INLINE_P (fndecl
) = 1;
39598 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
39603 DECL_CONTEXT (fndecl
) = current_namespace
;
39607 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
39609 block
= begin_omp_structured_block ();
39612 cp_parser_push_lexer_for_tokens (parser
, cp
);
39613 parser
->lexer
->in_pragma
= true;
39615 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
39618 finish_function (/*inline_p=*/false);
39620 DECL_CONTEXT (fndecl
) = current_function_decl
;
39622 cp_parser_pop_lexer (parser
);
39626 cp_parser_pop_lexer (parser
);
39628 finish_function (/*inline_p=*/false);
39631 DECL_CONTEXT (fndecl
) = current_function_decl
;
39632 block
= finish_omp_structured_block (block
);
39633 if (TREE_CODE (block
) == BIND_EXPR
)
39634 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
39635 else if (TREE_CODE (block
) == STATEMENT_LIST
)
39636 DECL_SAVED_TREE (fndecl
) = block
;
39637 if (processing_template_decl
)
39638 add_decl_expr (fndecl
);
39640 cp_check_omp_declare_reduction (fndecl
);
39641 if (cp
== NULL
&& types
.length () > 1)
39642 cp
= cp_token_cache_new (first_token
,
39643 cp_lexer_peek_nth_token (parser
->lexer
, 2));
39644 if (errs
!= errorcount
)
39648 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39651 /* Free any declarators allocated. */
39652 obstack_free (&declarator_obstack
, p
);
39656 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39657 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39658 initializer-clause[opt] new-line
39659 #pragma omp declare target new-line */
39662 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
39663 enum pragma_context context
)
39665 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39667 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39668 const char *p
= IDENTIFIER_POINTER (id
);
39670 if (strcmp (p
, "simd") == 0)
39672 cp_lexer_consume_token (parser
->lexer
);
39673 cp_parser_omp_declare_simd (parser
, pragma_tok
,
39677 cp_ensure_no_omp_declare_simd (parser
);
39678 if (strcmp (p
, "reduction") == 0)
39680 cp_lexer_consume_token (parser
->lexer
);
39681 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
39685 if (!flag_openmp
) /* flag_openmp_simd */
39687 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39690 if (strcmp (p
, "target") == 0)
39692 cp_lexer_consume_token (parser
->lexer
);
39693 cp_parser_omp_declare_target (parser
, pragma_tok
);
39697 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
39699 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39704 #pragma omp requires clauses[optseq] new-line */
39707 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
39710 enum omp_requires new_req
= (enum omp_requires
) 0;
39712 location_t loc
= pragma_tok
->location
;
39713 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39715 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39716 cp_lexer_consume_token (parser
->lexer
);
39720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39722 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39723 const char *p
= IDENTIFIER_POINTER (id
);
39724 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39725 enum omp_requires this_req
= (enum omp_requires
) 0;
39727 if (!strcmp (p
, "unified_address"))
39728 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
39729 else if (!strcmp (p
, "unified_shared_memory"))
39730 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
39731 else if (!strcmp (p
, "dynamic_allocators"))
39732 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
39733 else if (!strcmp (p
, "reverse_offload"))
39734 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
39735 else if (!strcmp (p
, "atomic_default_mem_order"))
39737 cp_lexer_consume_token (parser
->lexer
);
39739 matching_parens parens
;
39740 if (parens
.require_open (parser
))
39742 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39744 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39745 p
= IDENTIFIER_POINTER (id
);
39747 if (!strcmp (p
, "seq_cst"))
39749 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
39750 else if (!strcmp (p
, "relaxed"))
39752 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
39753 else if (!strcmp (p
, "acq_rel"))
39755 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
39759 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39760 "expected %<seq_cst%>, %<relaxed%> or "
39762 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
39764 cp_lexer_consume_token (parser
->lexer
);
39767 cp_lexer_consume_token (parser
->lexer
);
39769 if (!parens
.require_close (parser
))
39770 cp_parser_skip_to_closing_parenthesis (parser
,
39771 /*recovering=*/true,
39772 /*or_comma=*/false,
39778 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39786 error_at (cloc
, "expected %<unified_address%>, "
39787 "%<unified_shared_memory%>, "
39788 "%<dynamic_allocators%>, "
39789 "%<reverse_offload%> "
39790 "or %<atomic_default_mem_order%> clause");
39791 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39795 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
39796 "supported yet", p
);
39798 cp_lexer_consume_token (parser
->lexer
);
39801 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39803 if ((this_req
& new_req
) != 0)
39804 error_at (cloc
, "too many %qs clauses", p
);
39805 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
39806 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
39807 error_at (cloc
, "%qs clause used lexically after first "
39808 "target construct or offloading API", p
);
39810 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39812 error_at (cloc
, "too many %qs clauses",
39813 "atomic_default_mem_order");
39814 this_req
= (enum omp_requires
) 0;
39816 else if ((omp_requires_mask
39817 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39819 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
39820 " clause in a single compilation unit");
39822 = (enum omp_requires
)
39824 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
39826 else if ((omp_requires_mask
39827 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
39828 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
39829 "lexically after first %<atomic%> construct "
39830 "without memory order clause");
39831 new_req
= (enum omp_requires
) (new_req
| this_req
);
39833 = (enum omp_requires
) (omp_requires_mask
| this_req
);
39839 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39842 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
39848 #pragma omp taskloop taskloop-clause[optseq] new-line
39851 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39854 #define OMP_TASKLOOP_CLAUSE_MASK \
39855 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39873 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
39874 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39877 tree clauses
, sb
, ret
;
39879 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39881 strcat (p_name
, " taskloop");
39882 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
39883 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39885 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
39886 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
39888 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39890 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39891 const char *p
= IDENTIFIER_POINTER (id
);
39893 if (strcmp (p
, "simd") == 0)
39895 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39896 if (cclauses
== NULL
)
39897 cclauses
= cclauses_buf
;
39899 cp_lexer_consume_token (parser
->lexer
);
39900 if (!flag_openmp
) /* flag_openmp_simd */
39901 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39903 sb
= begin_omp_structured_block ();
39904 save
= cp_parser_begin_omp_structured_block (parser
);
39905 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39907 cp_parser_end_omp_structured_block (parser
, save
);
39908 tree body
= finish_omp_structured_block (sb
);
39911 ret
= make_node (OMP_TASKLOOP
);
39912 TREE_TYPE (ret
) = void_type_node
;
39913 OMP_FOR_BODY (ret
) = body
;
39914 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39915 SET_EXPR_LOCATION (ret
, loc
);
39920 if (!flag_openmp
) /* flag_openmp_simd */
39922 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39926 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39930 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
39931 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39934 keep_next_level (true);
39935 sb
= begin_omp_structured_block ();
39936 save
= cp_parser_begin_omp_structured_block (parser
);
39938 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
39941 cp_parser_end_omp_structured_block (parser
, save
);
39942 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39949 # pragma acc routine oacc-routine-clause[optseq] new-line
39950 function-definition
39952 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39955 #define OACC_ROUTINE_CLAUSE_MASK \
39956 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39962 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39963 component, which must resolve to a declared namespace-scope
39964 function. The clauses are either processed directly (for a named
39965 function), or defered until the immediatley following declaration
39969 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
39970 enum pragma_context context
)
39972 gcc_checking_assert (context
== pragma_external
);
39973 /* The checking for "another pragma following this one" in the "no optional
39974 '( name )'" case makes sure that we dont re-enter. */
39975 gcc_checking_assert (parser
->oacc_routine
== NULL
);
39977 cp_oacc_routine_data data
;
39978 data
.error_seen
= false;
39979 data
.fndecl_seen
= false;
39980 data
.tokens
= vNULL
;
39981 data
.clauses
= NULL_TREE
;
39982 data
.loc
= pragma_tok
->location
;
39983 /* It is safe to take the address of a local variable; it will only be
39984 used while this scope is live. */
39985 parser
->oacc_routine
= &data
;
39987 /* Look for optional '( name )'. */
39988 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39990 matching_parens parens
;
39991 parens
.consume_open (parser
); /* '(' */
39993 /* We parse the name as an id-expression. If it resolves to
39994 anything other than a non-overloaded function at namespace
39995 scope, it's an error. */
39996 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39997 tree name
= cp_parser_id_expression (parser
,
39998 /*template_keyword_p=*/false,
39999 /*check_dependency_p=*/false,
40000 /*template_p=*/NULL
,
40001 /*declarator_p=*/false,
40002 /*optional_p=*/false);
40003 tree decl
= (identifier_p (name
)
40004 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
40006 if (name
!= error_mark_node
&& decl
== error_mark_node
)
40007 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
40009 if (decl
== error_mark_node
40010 || !parens
.require_close (parser
))
40012 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40013 parser
->oacc_routine
= NULL
;
40018 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
40019 "#pragma acc routine",
40020 cp_lexer_peek_token (parser
->lexer
));
40022 if (decl
&& is_overloaded_fn (decl
)
40023 && (TREE_CODE (decl
) != FUNCTION_DECL
40024 || DECL_FUNCTION_TEMPLATE_P (decl
)))
40026 error_at (name_loc
,
40027 "%<#pragma acc routine%> names a set of overloads");
40028 parser
->oacc_routine
= NULL
;
40032 /* Perhaps we should use the same rule as declarations in different
40034 if (!DECL_NAMESPACE_SCOPE_P (decl
))
40036 error_at (name_loc
,
40037 "%qD does not refer to a namespace scope function", decl
);
40038 parser
->oacc_routine
= NULL
;
40042 if (TREE_CODE (decl
) != FUNCTION_DECL
)
40044 error_at (name_loc
, "%qD does not refer to a function", decl
);
40045 parser
->oacc_routine
= NULL
;
40049 cp_finalize_oacc_routine (parser
, decl
, false);
40050 parser
->oacc_routine
= NULL
;
40052 else /* No optional '( name )'. */
40054 /* Store away all pragma tokens. */
40055 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
40056 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
40057 cp_lexer_consume_token (parser
->lexer
);
40058 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
40059 parser
->oacc_routine
->error_seen
= true;
40060 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40061 struct cp_token_cache
*cp
40062 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
40063 parser
->oacc_routine
->tokens
.safe_push (cp
);
40065 /* Emit a helpful diagnostic if there's another pragma following this
40067 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
40069 cp_ensure_no_oacc_routine (parser
);
40070 data
.tokens
.release ();
40071 /* ..., and then just keep going. */
40075 /* We only have to consider the pragma_external case here. */
40076 cp_parser_declaration (parser
);
40077 if (parser
->oacc_routine
40078 && !parser
->oacc_routine
->fndecl_seen
)
40079 cp_ensure_no_oacc_routine (parser
);
40081 parser
->oacc_routine
= NULL
;
40082 data
.tokens
.release ();
40086 /* Finalize #pragma acc routine clauses after direct declarator has
40090 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
40092 struct cp_token_cache
*ce
;
40093 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
40095 if (!data
->error_seen
&& data
->fndecl_seen
)
40097 error_at (data
->loc
,
40098 "%<#pragma acc routine%> not immediately followed by "
40099 "a single function declaration or definition");
40100 data
->error_seen
= true;
40102 if (data
->error_seen
)
40105 gcc_checking_assert (data
->tokens
.length () == 1);
40106 ce
= data
->tokens
[0];
40108 cp_parser_push_lexer_for_tokens (parser
, ce
);
40109 parser
->lexer
->in_pragma
= true;
40110 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
40112 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40113 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
40114 parser
->oacc_routine
->clauses
40115 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
40116 "#pragma acc routine", pragma_tok
);
40117 cp_parser_pop_lexer (parser
);
40118 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40124 /* Apply any saved OpenACC routine clauses to a just-parsed
40128 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
40130 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
40132 /* Keep going if we're in error reporting mode. */
40133 if (parser
->oacc_routine
->error_seen
40134 || fndecl
== error_mark_node
)
40137 if (parser
->oacc_routine
->fndecl_seen
)
40139 error_at (parser
->oacc_routine
->loc
,
40140 "%<#pragma acc routine%> not immediately followed by"
40141 " a single function declaration or definition");
40142 parser
->oacc_routine
= NULL
;
40145 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
40147 cp_ensure_no_oacc_routine (parser
);
40151 if (oacc_get_fn_attrib (fndecl
))
40153 error_at (parser
->oacc_routine
->loc
,
40154 "%<#pragma acc routine%> already applied to %qD", fndecl
);
40155 parser
->oacc_routine
= NULL
;
40159 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
40161 error_at (parser
->oacc_routine
->loc
,
40163 ? G_("%<#pragma acc routine%> must be applied before use")
40164 : G_("%<#pragma acc routine%> must be applied before "
40166 parser
->oacc_routine
= NULL
;
40170 /* Process the routine's dimension clauses. */
40171 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
40172 oacc_replace_fn_attrib (fndecl
, dims
);
40174 /* Add an "omp declare target" attribute. */
40175 DECL_ATTRIBUTES (fndecl
)
40176 = tree_cons (get_identifier ("omp declare target"),
40177 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
40179 /* Don't unset parser->oacc_routine here: we may still need it to
40180 diagnose wrong usage. But, remember that we've used this "#pragma acc
40182 parser
->oacc_routine
->fndecl_seen
= true;
40186 /* Main entry point to OpenMP statement pragmas. */
40189 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40192 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
40193 omp_clause_mask
mask (0);
40195 switch (cp_parser_pragma_kind (pragma_tok
))
40197 case PRAGMA_OACC_ATOMIC
:
40198 cp_parser_omp_atomic (parser
, pragma_tok
);
40200 case PRAGMA_OACC_CACHE
:
40201 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
40203 case PRAGMA_OACC_DATA
:
40204 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
40206 case PRAGMA_OACC_ENTER_DATA
:
40207 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
40209 case PRAGMA_OACC_EXIT_DATA
:
40210 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
40212 case PRAGMA_OACC_HOST_DATA
:
40213 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
40215 case PRAGMA_OACC_KERNELS
:
40216 case PRAGMA_OACC_PARALLEL
:
40217 strcpy (p_name
, "#pragma acc");
40218 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
40221 case PRAGMA_OACC_LOOP
:
40222 strcpy (p_name
, "#pragma acc");
40223 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40226 case PRAGMA_OACC_UPDATE
:
40227 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
40229 case PRAGMA_OACC_WAIT
:
40230 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
40232 case PRAGMA_OMP_ATOMIC
:
40233 cp_parser_omp_atomic (parser
, pragma_tok
);
40235 case PRAGMA_OMP_CRITICAL
:
40236 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
40238 case PRAGMA_OMP_DISTRIBUTE
:
40239 strcpy (p_name
, "#pragma omp");
40240 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
40243 case PRAGMA_OMP_FOR
:
40244 strcpy (p_name
, "#pragma omp");
40245 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
40248 case PRAGMA_OMP_MASTER
:
40249 strcpy (p_name
, "#pragma omp");
40250 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
40253 case PRAGMA_OMP_PARALLEL
:
40254 strcpy (p_name
, "#pragma omp");
40255 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
40258 case PRAGMA_OMP_SECTIONS
:
40259 strcpy (p_name
, "#pragma omp");
40260 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
40262 case PRAGMA_OMP_SIMD
:
40263 strcpy (p_name
, "#pragma omp");
40264 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
40267 case PRAGMA_OMP_SINGLE
:
40268 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
40270 case PRAGMA_OMP_TASK
:
40271 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
40273 case PRAGMA_OMP_TASKGROUP
:
40274 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
40276 case PRAGMA_OMP_TASKLOOP
:
40277 strcpy (p_name
, "#pragma omp");
40278 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40281 case PRAGMA_OMP_TEAMS
:
40282 strcpy (p_name
, "#pragma omp");
40283 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
40287 gcc_unreachable ();
40290 protected_set_expr_location (stmt
, pragma_tok
->location
);
40293 /* Transactional Memory parsing routines. */
40295 /* Parse a transaction attribute.
40301 We use this instead of cp_parser_attributes_opt for transactions to avoid
40302 the pedwarn in C++98 mode. */
40305 cp_parser_txn_attribute_opt (cp_parser
*parser
)
40308 tree attr_name
, attr
= NULL
;
40310 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
40311 return cp_parser_attributes_opt (parser
);
40313 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
40315 cp_lexer_consume_token (parser
->lexer
);
40316 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
40319 token
= cp_lexer_peek_token (parser
->lexer
);
40320 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
40322 token
= cp_lexer_consume_token (parser
->lexer
);
40324 attr_name
= (token
->type
== CPP_KEYWORD
40325 /* For keywords, use the canonical spelling,
40326 not the parsed identifier. */
40327 ? ridpointers
[(int) token
->keyword
]
40329 attr
= build_tree_list (attr_name
, NULL_TREE
);
40332 cp_parser_error (parser
, "expected identifier");
40334 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40336 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40340 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40342 transaction-statement:
40343 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40345 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40349 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
40351 unsigned char old_in
= parser
->in_transaction
;
40352 unsigned char this_in
= 1, new_in
;
40353 enum rid keyword
= token
->keyword
;
40354 tree stmt
, attrs
, noex
;
40356 cp_lexer_consume_token (parser
->lexer
);
40358 if (keyword
== RID_TRANSACTION_RELAXED
40359 || keyword
== RID_SYNCHRONIZED
)
40360 this_in
|= TM_STMT_ATTR_RELAXED
;
40363 attrs
= cp_parser_txn_attribute_opt (parser
);
40365 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40368 /* Parse a noexcept specification. */
40369 if (keyword
== RID_ATOMIC_NOEXCEPT
)
40370 noex
= boolean_true_node
;
40371 else if (keyword
== RID_ATOMIC_CANCEL
)
40373 /* cancel-and-throw is unimplemented. */
40374 sorry ("atomic_cancel");
40378 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
40380 /* Keep track if we're in the lexical scope of an outer transaction. */
40381 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
40383 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
40385 parser
->in_transaction
= new_in
;
40386 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
40387 parser
->in_transaction
= old_in
;
40389 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
40394 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40396 transaction-expression:
40397 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40398 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40402 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
40404 unsigned char old_in
= parser
->in_transaction
;
40405 unsigned char this_in
= 1;
40409 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40411 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40412 || keyword
== RID_TRANSACTION_RELAXED
);
40416 keyword
== RID_TRANSACTION_RELAXED
40417 ? G_("%<__transaction_relaxed%> without transactional memory "
40419 : G_("%<__transaction_atomic%> without transactional memory "
40420 "support enabled"));
40422 token
= cp_parser_require_keyword (parser
, keyword
,
40423 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40424 : RT_TRANSACTION_RELAXED
));
40425 gcc_assert (token
!= NULL
);
40427 if (keyword
== RID_TRANSACTION_RELAXED
)
40428 this_in
|= TM_STMT_ATTR_RELAXED
;
40430 /* Set this early. This might mean that we allow transaction_cancel in
40431 an expression that we find out later actually has to be a constexpr.
40432 However, we expect that cxx_constant_value will be able to deal with
40433 this; also, if the noexcept has no constexpr, then what we parse next
40434 really is a transaction's body. */
40435 parser
->in_transaction
= this_in
;
40437 /* Parse a noexcept specification. */
40438 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
40441 if (!noex
|| !noex_expr
40442 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
40444 matching_parens parens
;
40445 parens
.require_open (parser
);
40447 expr
= cp_parser_expression (parser
);
40448 expr
= finish_parenthesized_expr (expr
);
40450 parens
.require_close (parser
);
40454 /* The only expression that is available got parsed for the noexcept
40455 already. noexcept is true then. */
40457 noex
= boolean_true_node
;
40460 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
40461 parser
->in_transaction
= old_in
;
40463 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
40464 return error_mark_node
;
40466 return (flag_tm
? expr
: error_mark_node
);
40469 /* Parse a function-transaction-block.
40471 function-transaction-block:
40472 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40474 __transaction_atomic txn-attribute[opt] function-try-block
40475 __transaction_relaxed ctor-initializer[opt] function-body
40476 __transaction_relaxed function-try-block
40480 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
40482 unsigned char old_in
= parser
->in_transaction
;
40483 unsigned char new_in
= 1;
40484 tree compound_stmt
, stmt
, attrs
;
40487 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40488 || keyword
== RID_TRANSACTION_RELAXED
);
40489 token
= cp_parser_require_keyword (parser
, keyword
,
40490 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40491 : RT_TRANSACTION_RELAXED
));
40492 gcc_assert (token
!= NULL
);
40494 if (keyword
== RID_TRANSACTION_RELAXED
)
40495 new_in
|= TM_STMT_ATTR_RELAXED
;
40498 attrs
= cp_parser_txn_attribute_opt (parser
);
40500 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40503 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
40505 parser
->in_transaction
= new_in
;
40507 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
40508 cp_parser_function_try_block (parser
);
40510 cp_parser_ctor_initializer_opt_and_function_body
40511 (parser
, /*in_function_try_block=*/false);
40513 parser
->in_transaction
= old_in
;
40515 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
40518 /* Parse a __transaction_cancel statement.
40521 __transaction_cancel txn-attribute[opt] ;
40522 __transaction_cancel txn-attribute[opt] throw-expression ;
40524 ??? Cancel and throw is not yet implemented. */
40527 cp_parser_transaction_cancel (cp_parser
*parser
)
40530 bool is_outer
= false;
40533 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
40534 RT_TRANSACTION_CANCEL
);
40535 gcc_assert (token
!= NULL
);
40537 attrs
= cp_parser_txn_attribute_opt (parser
);
40539 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
40541 /* ??? Parse cancel-and-throw here. */
40543 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
40547 error_at (token
->location
, "%<__transaction_cancel%> without "
40548 "transactional memory support enabled");
40549 return error_mark_node
;
40551 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
40553 error_at (token
->location
, "%<__transaction_cancel%> within a "
40554 "%<__transaction_relaxed%>");
40555 return error_mark_node
;
40559 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
40560 && !is_tm_may_cancel_outer (current_function_decl
))
40562 error_at (token
->location
, "outer %<__transaction_cancel%> not "
40563 "within outer %<__transaction_atomic%>");
40564 error_at (token
->location
,
40565 " or a %<transaction_may_cancel_outer%> function");
40566 return error_mark_node
;
40569 else if (parser
->in_transaction
== 0)
40571 error_at (token
->location
, "%<__transaction_cancel%> not within "
40572 "%<__transaction_atomic%>");
40573 return error_mark_node
;
40576 stmt
= build_tm_abort_call (token
->location
, is_outer
);
40584 static GTY (()) cp_parser
*the_parser
;
40587 /* Special handling for the first token or line in the file. The first
40588 thing in the file might be #pragma GCC pch_preprocess, which loads a
40589 PCH file, which is a GC collection point. So we need to handle this
40590 first pragma without benefit of an existing lexer structure.
40592 Always returns one token to the caller in *FIRST_TOKEN. This is
40593 either the true first token of the file, or the first token after
40594 the initial pragma. */
40597 cp_parser_initial_pragma (cp_token
*first_token
)
40601 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40602 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
40605 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40606 if (first_token
->type
== CPP_STRING
)
40608 name
= first_token
->u
.value
;
40610 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40611 if (first_token
->type
!= CPP_PRAGMA_EOL
)
40612 error_at (first_token
->location
,
40613 "junk at end of %<#pragma GCC pch_preprocess%>");
40616 error_at (first_token
->location
, "expected string literal");
40618 /* Skip to the end of the pragma. */
40619 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
40620 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40622 /* Now actually load the PCH file. */
40624 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
40626 /* Read one more token to return to our caller. We have to do this
40627 after reading the PCH file in, since its pointers have to be
40629 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40632 /* Parse a pragma GCC ivdep. */
40635 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
40637 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40641 /* Parse a pragma GCC unroll. */
40643 static unsigned short
40644 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
40646 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
40647 tree expr
= cp_parser_constant_expression (parser
);
40648 unsigned short unroll
;
40649 expr
= maybe_constant_value (expr
);
40650 HOST_WIDE_INT lunroll
= 0;
40651 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
40652 || TREE_CODE (expr
) != INTEGER_CST
40653 || (lunroll
= tree_to_shwi (expr
)) < 0
40654 || lunroll
>= USHRT_MAX
)
40656 error_at (location
, "%<#pragma GCC unroll%> requires an"
40657 " assignment-expression that evaluates to a non-negative"
40658 " integral constant less than %u", USHRT_MAX
);
40663 unroll
= (unsigned short)lunroll
;
40667 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40671 /* Normal parsing of a pragma token. Here we can (and must) use the
40675 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
40677 cp_token
*pragma_tok
;
40682 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40683 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
40684 parser
->lexer
->in_pragma
= true;
40686 id
= cp_parser_pragma_kind (pragma_tok
);
40687 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
40688 cp_ensure_no_omp_declare_simd (parser
);
40691 case PRAGMA_GCC_PCH_PREPROCESS
:
40692 error_at (pragma_tok
->location
,
40693 "%<#pragma GCC pch_preprocess%> must be first");
40696 case PRAGMA_OMP_BARRIER
:
40699 case pragma_compound
:
40700 cp_parser_omp_barrier (parser
, pragma_tok
);
40703 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40704 "used in compound statements", "omp barrier");
40711 case PRAGMA_OMP_DEPOBJ
:
40714 case pragma_compound
:
40715 cp_parser_omp_depobj (parser
, pragma_tok
);
40718 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40719 "used in compound statements", "omp depobj");
40726 case PRAGMA_OMP_FLUSH
:
40729 case pragma_compound
:
40730 cp_parser_omp_flush (parser
, pragma_tok
);
40733 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40734 "used in compound statements", "omp flush");
40741 case PRAGMA_OMP_TASKWAIT
:
40744 case pragma_compound
:
40745 cp_parser_omp_taskwait (parser
, pragma_tok
);
40748 error_at (pragma_tok
->location
,
40749 "%<#pragma %s%> may only be used in compound statements",
40757 case PRAGMA_OMP_TASKYIELD
:
40760 case pragma_compound
:
40761 cp_parser_omp_taskyield (parser
, pragma_tok
);
40764 error_at (pragma_tok
->location
,
40765 "%<#pragma %s%> may only be used in compound statements",
40773 case PRAGMA_OMP_CANCEL
:
40776 case pragma_compound
:
40777 cp_parser_omp_cancel (parser
, pragma_tok
);
40780 error_at (pragma_tok
->location
,
40781 "%<#pragma %s%> may only be used in compound statements",
40789 case PRAGMA_OMP_CANCELLATION_POINT
:
40790 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
40793 case PRAGMA_OMP_THREADPRIVATE
:
40794 cp_parser_omp_threadprivate (parser
, pragma_tok
);
40797 case PRAGMA_OMP_DECLARE
:
40798 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
40800 case PRAGMA_OACC_DECLARE
:
40801 cp_parser_oacc_declare (parser
, pragma_tok
);
40804 case PRAGMA_OACC_ENTER_DATA
:
40805 if (context
== pragma_stmt
)
40807 error_at (pragma_tok
->location
,
40808 "%<#pragma %s%> may only be used in compound statements",
40812 else if (context
!= pragma_compound
)
40814 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40817 case PRAGMA_OACC_EXIT_DATA
:
40818 if (context
== pragma_stmt
)
40820 error_at (pragma_tok
->location
,
40821 "%<#pragma %s%> may only be used in compound statements",
40825 else if (context
!= pragma_compound
)
40827 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40830 case PRAGMA_OACC_ROUTINE
:
40831 if (context
!= pragma_external
)
40833 error_at (pragma_tok
->location
,
40834 "%<#pragma acc routine%> must be at file scope");
40837 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
40840 case PRAGMA_OACC_UPDATE
:
40841 if (context
== pragma_stmt
)
40843 error_at (pragma_tok
->location
,
40844 "%<#pragma %s%> may only be used in compound statements",
40848 else if (context
!= pragma_compound
)
40850 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40853 case PRAGMA_OACC_WAIT
:
40854 if (context
== pragma_stmt
)
40856 error_at (pragma_tok
->location
,
40857 "%<#pragma %s%> may only be used in compound statements",
40861 else if (context
!= pragma_compound
)
40863 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40866 case PRAGMA_OACC_ATOMIC
:
40867 case PRAGMA_OACC_CACHE
:
40868 case PRAGMA_OACC_DATA
:
40869 case PRAGMA_OACC_HOST_DATA
:
40870 case PRAGMA_OACC_KERNELS
:
40871 case PRAGMA_OACC_PARALLEL
:
40872 case PRAGMA_OACC_LOOP
:
40873 case PRAGMA_OMP_ATOMIC
:
40874 case PRAGMA_OMP_CRITICAL
:
40875 case PRAGMA_OMP_DISTRIBUTE
:
40876 case PRAGMA_OMP_FOR
:
40877 case PRAGMA_OMP_MASTER
:
40878 case PRAGMA_OMP_PARALLEL
:
40879 case PRAGMA_OMP_SECTIONS
:
40880 case PRAGMA_OMP_SIMD
:
40881 case PRAGMA_OMP_SINGLE
:
40882 case PRAGMA_OMP_TASK
:
40883 case PRAGMA_OMP_TASKGROUP
:
40884 case PRAGMA_OMP_TASKLOOP
:
40885 case PRAGMA_OMP_TEAMS
:
40886 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40888 stmt
= push_omp_privatization_clauses (false);
40889 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40890 pop_omp_privatization_clauses (stmt
);
40893 case PRAGMA_OMP_REQUIRES
:
40894 return cp_parser_omp_requires (parser
, pragma_tok
);
40896 case PRAGMA_OMP_ORDERED
:
40897 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40899 stmt
= push_omp_privatization_clauses (false);
40900 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
40901 pop_omp_privatization_clauses (stmt
);
40904 case PRAGMA_OMP_TARGET
:
40905 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40907 stmt
= push_omp_privatization_clauses (false);
40908 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
40909 pop_omp_privatization_clauses (stmt
);
40912 case PRAGMA_OMP_END_DECLARE_TARGET
:
40913 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
40916 case PRAGMA_OMP_SECTION
:
40917 error_at (pragma_tok
->location
,
40918 "%<#pragma omp section%> may only be used in "
40919 "%<#pragma omp sections%> construct");
40924 if (context
== pragma_external
)
40926 error_at (pragma_tok
->location
,
40927 "%<#pragma GCC ivdep%> must be inside a function");
40930 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
40931 unsigned short unroll
;
40932 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40933 if (tok
->type
== CPP_PRAGMA
40934 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
40936 tok
= cp_lexer_consume_token (parser
->lexer
);
40937 unroll
= cp_parser_pragma_unroll (parser
, tok
);
40938 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40942 if (tok
->type
!= CPP_KEYWORD
40943 || (tok
->keyword
!= RID_FOR
40944 && tok
->keyword
!= RID_WHILE
40945 && tok
->keyword
!= RID_DO
))
40947 cp_parser_error (parser
, "for, while or do statement expected");
40950 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40954 case PRAGMA_UNROLL
:
40956 if (context
== pragma_external
)
40958 error_at (pragma_tok
->location
,
40959 "%<#pragma GCC unroll%> must be inside a function");
40962 const unsigned short unroll
40963 = cp_parser_pragma_unroll (parser
, pragma_tok
);
40965 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40966 if (tok
->type
== CPP_PRAGMA
40967 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
40969 tok
= cp_lexer_consume_token (parser
->lexer
);
40970 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
40971 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40975 if (tok
->type
!= CPP_KEYWORD
40976 || (tok
->keyword
!= RID_FOR
40977 && tok
->keyword
!= RID_WHILE
40978 && tok
->keyword
!= RID_DO
))
40980 cp_parser_error (parser
, "for, while or do statement expected");
40983 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40988 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
40989 c_invoke_pragma_handler (id
);
40993 cp_parser_error (parser
, "expected declaration specifiers");
40997 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41001 /* The interface the pragma parsers have to the lexer. */
41004 pragma_lex (tree
*value
, location_t
*loc
)
41006 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
41007 enum cpp_ttype ret
= tok
->type
;
41009 *value
= tok
->u
.value
;
41011 *loc
= tok
->location
;
41013 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
41015 else if (ret
== CPP_STRING
)
41016 *value
= cp_parser_string_literal (the_parser
, false, false);
41019 if (ret
== CPP_KEYWORD
)
41021 cp_lexer_consume_token (the_parser
->lexer
);
41028 /* External interface. */
41030 /* Parse one entire translation unit. */
41033 c_parse_file (void)
41035 static bool already_called
= false;
41037 if (already_called
)
41038 fatal_error (input_location
,
41039 "inter-module optimizations not implemented for C++");
41040 already_called
= true;
41042 the_parser
= cp_parser_new ();
41043 push_deferring_access_checks (flag_access_control
41044 ? dk_no_deferred
: dk_no_check
);
41045 cp_parser_translation_unit (the_parser
);
41048 finish_translation_unit ();
41051 /* Create an identifier for a generic parameter type (a synthesized
41052 template parameter implied by `auto' or a concept identifier). */
41054 static GTY(()) int generic_parm_count
;
41056 make_generic_type_name ()
41059 sprintf (buf
, "auto:%d", ++generic_parm_count
);
41060 return get_identifier (buf
);
41063 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41064 (creating a new template parameter list if necessary). Returns the newly
41065 created template type parm. */
41068 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
41070 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
41072 /* Before committing to modifying any scope, if we're in an
41073 implicit template scope, and we're trying to synthesize a
41074 constrained parameter, try to find a previous parameter with
41075 the same name. This is the same-type rule for abbreviated
41076 function templates.
41078 NOTE: We can generate implicit parameters when tentatively
41079 parsing a nested name specifier, only to reject that parse
41080 later. However, matching the same template-id as part of a
41081 direct-declarator should generate an identical template
41082 parameter, so this rule will merge them. */
41083 if (parser
->implicit_template_scope
&& constr
)
41085 tree t
= parser
->implicit_template_parms
;
41088 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
41090 tree d
= TREE_VALUE (t
);
41091 if (TREE_CODE (d
) == PARM_DECL
)
41092 /* Return the TEMPLATE_PARM_INDEX. */
41093 d
= DECL_INITIAL (d
);
41096 t
= TREE_CHAIN (t
);
41100 /* We are either continuing a function template that already contains implicit
41101 template parameters, creating a new fully-implicit function template, or
41102 extending an existing explicit function template with implicit template
41105 cp_binding_level
*const entry_scope
= current_binding_level
;
41107 bool become_template
= false;
41108 cp_binding_level
*parent_scope
= 0;
41110 if (parser
->implicit_template_scope
)
41112 gcc_assert (parser
->implicit_template_parms
);
41114 current_binding_level
= parser
->implicit_template_scope
;
41118 /* Roll back to the existing template parameter scope (in the case of
41119 extending an explicit function template) or introduce a new template
41120 parameter scope ahead of the function parameter scope (or class scope
41121 in the case of out-of-line member definitions). The function scope is
41122 added back after template parameter synthesis below. */
41124 cp_binding_level
*scope
= entry_scope
;
41126 while (scope
->kind
== sk_function_parms
)
41128 parent_scope
= scope
;
41129 scope
= scope
->level_chain
;
41131 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
41133 /* If not defining a class, then any class scope is a scope level in
41134 an out-of-line member definition. In this case simply wind back
41135 beyond the first such scope to inject the template parameter list.
41136 Otherwise wind back to the class being defined. The latter can
41137 occur in class member friend declarations such as:
41143 friend void A::foo (auto);
41146 The template parameter list synthesized for the friend declaration
41147 must be injected in the scope of 'B'. This can also occur in
41148 erroneous cases such as:
41154 void B::foo (auto) {}
41157 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41158 but, nevertheless, the template parameter list synthesized for the
41159 declarator should be injected into the scope of 'A' as if the
41160 ill-formed template was specified explicitly. */
41162 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
41164 parent_scope
= scope
;
41165 scope
= scope
->level_chain
;
41169 current_binding_level
= scope
;
41171 if (scope
->kind
!= sk_template_parms
41172 || !function_being_declared_is_template_p (parser
))
41174 /* Introduce a new template parameter list for implicit template
41177 become_template
= true;
41179 parser
->implicit_template_scope
41180 = begin_scope (sk_template_parms
, NULL
);
41182 ++processing_template_decl
;
41184 parser
->fully_implicit_function_template_p
= true;
41185 ++parser
->num_template_parameter_lists
;
41189 /* Synthesize implicit template parameters at the end of the explicit
41190 template parameter list. */
41192 gcc_assert (current_template_parms
);
41194 parser
->implicit_template_scope
= scope
;
41196 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41197 parser
->implicit_template_parms
41198 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
41202 /* Synthesize a new template parameter and track the current template
41203 parameter chain with implicit_template_parms. */
41205 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
41206 tree synth_id
= make_generic_type_name ();
41207 tree synth_tmpl_parm
;
41208 bool non_type
= false;
41210 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
41212 = finish_template_type_parm (class_type_node
, synth_id
);
41213 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
41215 = finish_constrained_template_template_parm (proto
, synth_id
);
41218 synth_tmpl_parm
= copy_decl (proto
);
41219 DECL_NAME (synth_tmpl_parm
) = synth_id
;
41223 // Attach the constraint to the parm before processing.
41224 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
41225 TREE_TYPE (node
) = constr
;
41227 = process_template_parm (parser
->implicit_template_parms
,
41230 /*non_type=*/non_type
,
41231 /*param_pack=*/false);
41233 // Chain the new parameter to the list of implicit parameters.
41234 if (parser
->implicit_template_parms
)
41235 parser
->implicit_template_parms
41236 = TREE_CHAIN (parser
->implicit_template_parms
);
41238 parser
->implicit_template_parms
= new_parm
;
41240 tree new_decl
= get_local_decls ();
41242 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41243 new_decl
= DECL_INITIAL (new_decl
);
41245 /* If creating a fully implicit function template, start the new implicit
41246 template parameter list with this synthesized type, otherwise grow the
41247 current template parameter list. */
41249 if (become_template
)
41251 parent_scope
->level_chain
= current_binding_level
;
41253 tree new_parms
= make_tree_vec (1);
41254 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
41255 current_template_parms
= tree_cons (size_int (processing_template_decl
),
41256 new_parms
, current_template_parms
);
41260 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41261 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
41262 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
41263 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
41266 // If the new parameter was constrained, we need to add that to the
41267 // constraints in the template parameter list.
41268 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
41270 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
41271 reqs
= conjoin_constraints (reqs
, req
);
41272 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
41275 current_binding_level
= entry_scope
;
41280 /* Finish the declaration of a fully implicit function template. Such a
41281 template has no explicit template parameter list so has not been through the
41282 normal template head and tail processing. synthesize_implicit_template_parm
41283 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41284 provided if the declaration is a class member such that its template
41285 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41286 form is returned. Otherwise NULL_TREE is returned. */
41289 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
41291 gcc_assert (parser
->fully_implicit_function_template_p
);
41293 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
41294 && DECL_VIRTUAL_P (member_decl_opt
))
41296 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
41297 "implicit templates may not be %<virtual%>");
41298 DECL_VIRTUAL_P (member_decl_opt
) = false;
41301 if (member_decl_opt
)
41302 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
41303 end_template_decl ();
41305 parser
->fully_implicit_function_template_p
= false;
41306 parser
->implicit_template_parms
= 0;
41307 parser
->implicit_template_scope
= 0;
41308 --parser
->num_template_parameter_lists
;
41310 return member_decl_opt
;
41313 /* Like finish_fully_implicit_template, but to be used in error
41314 recovery, rearranging scopes so that we restore the state we had
41315 before synthesize_implicit_template_parm inserted the implement
41316 template parms scope. */
41319 abort_fully_implicit_template (cp_parser
*parser
)
41321 cp_binding_level
*return_to_scope
= current_binding_level
;
41323 if (parser
->implicit_template_scope
41324 && return_to_scope
!= parser
->implicit_template_scope
)
41326 cp_binding_level
*child
= return_to_scope
;
41327 for (cp_binding_level
*scope
= child
->level_chain
;
41328 scope
!= parser
->implicit_template_scope
;
41329 scope
= child
->level_chain
)
41331 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
41332 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
41333 current_binding_level
= parser
->implicit_template_scope
;
41336 return_to_scope
= return_to_scope
->level_chain
;
41338 finish_fully_implicit_template (parser
, NULL
);
41340 gcc_assert (current_binding_level
== return_to_scope
);
41343 /* Helper function for diagnostics that have complained about things
41344 being used with 'extern "C"' linkage.
41346 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41349 maybe_show_extern_c_location (void)
41351 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
41352 inform (the_parser
->innermost_linkage_specification_location
,
41353 "%<extern \"C\"%> linkage started here");
41356 #include "gt-cp-parser.h"