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_location
;
11068 /* There is no statement yet. */
11069 statement
= NULL_TREE
;
11071 saved_token_sentinel
saved_tokens (parser
->lexer
);
11072 attrs_location
= 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
);
11079 if (c_dialect_objc ())
11081 if (!cp_parser_parse_definitely (parser
))
11082 std_attrs
= NULL_TREE
;
11085 /* Peek at the next token. */
11086 token
= cp_lexer_peek_token (parser
->lexer
);
11087 /* Remember the location of the first token in the statement. */
11088 cp_token
*statement_token
= token
;
11089 statement_location
= token
->location
;
11090 add_debug_begin_stmt (statement_location
);
11091 /* If this is a keyword, then that will often determine what kind of
11092 statement we have. */
11093 if (token
->type
== CPP_KEYWORD
)
11095 enum rid keyword
= token
->keyword
;
11101 /* Looks like a labeled-statement with a case label.
11102 Parse the label, and then use tail recursion to parse
11104 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11105 in_compound
= false;
11110 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11111 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
11117 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11118 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
11125 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11126 statement
= cp_parser_jump_statement (parser
);
11129 /* Objective-C++ exception-handling constructs. */
11132 case RID_AT_FINALLY
:
11133 case RID_AT_SYNCHRONIZED
:
11135 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11136 statement
= cp_parser_objc_statement (parser
);
11140 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11141 statement
= cp_parser_try_block (parser
);
11144 case RID_NAMESPACE
:
11145 /* This must be a namespace alias definition. */
11146 if (std_attrs
!= NULL_TREE
)
11148 /* Attributes should be parsed as part of the the
11149 declaration, so let's un-parse them. */
11150 saved_tokens
.rollback();
11151 std_attrs
= NULL_TREE
;
11153 cp_parser_declaration_statement (parser
);
11156 case RID_TRANSACTION_ATOMIC
:
11157 case RID_TRANSACTION_RELAXED
:
11158 case RID_SYNCHRONIZED
:
11159 case RID_ATOMIC_NOEXCEPT
:
11160 case RID_ATOMIC_CANCEL
:
11161 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11162 statement
= cp_parser_transaction (parser
, token
);
11164 case RID_TRANSACTION_CANCEL
:
11165 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11166 statement
= cp_parser_transaction_cancel (parser
);
11170 /* It might be a keyword like `int' that can start a
11171 declaration-statement. */
11175 else if (token
->type
== CPP_NAME
)
11177 /* If the next token is a `:', then we are looking at a
11178 labeled-statement. */
11179 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11180 if (token
->type
== CPP_COLON
)
11182 /* Looks like a labeled-statement with an ordinary label.
11183 Parse the label, and then use tail recursion to parse
11186 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11187 in_compound
= false;
11191 /* Anything that starts with a `{' must be a compound-statement. */
11192 else if (token
->type
== CPP_OPEN_BRACE
)
11193 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11194 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11195 a statement all its own. */
11196 else if (token
->type
== CPP_PRAGMA
)
11198 /* Only certain OpenMP pragmas are attached to statements, and thus
11199 are considered statements themselves. All others are not. In
11200 the context of a compound, accept the pragma as a "statement" and
11201 return so that we can check for a close brace. Otherwise we
11202 require a real statement and must go back and read one. */
11204 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11205 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11209 else if (token
->type
== CPP_EOF
)
11211 cp_parser_error (parser
, "expected statement");
11215 /* Everything else must be a declaration-statement or an
11216 expression-statement. Try for the declaration-statement
11217 first, unless we are looking at a `;', in which case we know that
11218 we have an expression-statement. */
11221 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11223 if (std_attrs
!= NULL_TREE
)
11224 /* Attributes should be parsed as part of the declaration,
11225 so let's un-parse them. */
11226 saved_tokens
.rollback();
11228 cp_parser_parse_tentatively (parser
);
11229 /* Try to parse the declaration-statement. */
11230 cp_parser_declaration_statement (parser
);
11231 /* If that worked, we're done. */
11232 if (cp_parser_parse_definitely (parser
))
11234 /* It didn't work, restore the post-attribute position. */
11236 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11238 /* All preceding labels have been parsed at this point. */
11239 if (loc_after_labels
!= NULL
)
11240 *loc_after_labels
= statement_location
;
11242 std_attrs
= process_stmt_hotness_attribute (std_attrs
);
11244 /* Look for an expression-statement instead. */
11245 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11247 /* Handle [[fallthrough]];. */
11248 if (attribute_fallthrough_p (std_attrs
))
11250 /* The next token after the fallthrough attribute is ';'. */
11251 if (statement
== NULL_TREE
)
11253 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11254 statement
= build_call_expr_internal_loc (statement_location
,
11256 void_type_node
, 0);
11257 finish_expr_stmt (statement
);
11260 warning_at (statement_location
, OPT_Wattributes
,
11261 "%<fallthrough%> attribute not followed by %<;%>");
11262 std_attrs
= NULL_TREE
;
11266 /* Set the line number for the statement. */
11267 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11268 SET_EXPR_LOCATION (statement
, statement_location
);
11270 /* Allow "[[fallthrough]];", but warn otherwise. */
11271 if (std_attrs
!= NULL_TREE
)
11272 warning_at (attrs_location
,
11274 "attributes at the beginning of statement are ignored");
11277 /* Append ATTR to attribute list ATTRS. */
11280 attr_chainon (tree attrs
, tree attr
)
11282 if (attrs
== error_mark_node
)
11283 return error_mark_node
;
11284 if (attr
== error_mark_node
)
11285 return error_mark_node
;
11286 return chainon (attrs
, attr
);
11289 /* Parse the label for a labeled-statement, i.e.
11292 case constant-expression :
11296 case constant-expression ... constant-expression : statement
11298 When a label is parsed without errors, the label is added to the
11299 parse tree by the finish_* functions, so this function doesn't
11300 have to return the label. */
11303 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11306 tree label
= NULL_TREE
;
11307 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11309 /* The next token should be an identifier. */
11310 token
= cp_lexer_peek_token (parser
->lexer
);
11311 if (token
->type
!= CPP_NAME
11312 && token
->type
!= CPP_KEYWORD
)
11314 cp_parser_error (parser
, "expected labeled-statement");
11318 /* Remember whether this case or a user-defined label is allowed to fall
11320 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11322 parser
->colon_corrects_to_scope_p
= false;
11323 switch (token
->keyword
)
11327 tree expr
, expr_hi
;
11328 cp_token
*ellipsis
;
11330 /* Consume the `case' token. */
11331 cp_lexer_consume_token (parser
->lexer
);
11332 /* Parse the constant-expression. */
11333 expr
= cp_parser_constant_expression (parser
);
11334 if (check_for_bare_parameter_packs (expr
))
11335 expr
= error_mark_node
;
11337 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11338 if (ellipsis
->type
== CPP_ELLIPSIS
)
11340 /* Consume the `...' token. */
11341 cp_lexer_consume_token (parser
->lexer
);
11342 expr_hi
= cp_parser_constant_expression (parser
);
11343 if (check_for_bare_parameter_packs (expr_hi
))
11344 expr_hi
= error_mark_node
;
11346 /* We don't need to emit warnings here, as the common code
11347 will do this for us. */
11350 expr_hi
= NULL_TREE
;
11352 if (parser
->in_switch_statement_p
)
11354 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11355 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11357 label
= CASE_LABEL (l
);
11358 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11362 error_at (token
->location
,
11363 "case label %qE not within a switch statement",
11369 /* Consume the `default' token. */
11370 cp_lexer_consume_token (parser
->lexer
);
11372 if (parser
->in_switch_statement_p
)
11374 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11375 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11377 label
= CASE_LABEL (l
);
11378 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11382 error_at (token
->location
, "case label not within a switch statement");
11386 /* Anything else must be an ordinary label. */
11387 label
= finish_label_stmt (cp_parser_identifier (parser
));
11388 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11389 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11393 /* Require the `:' token. */
11394 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11396 /* An ordinary label may optionally be followed by attributes.
11397 However, this is only permitted if the attributes are then
11398 followed by a semicolon. This is because, for backward
11399 compatibility, when parsing
11400 lab: __attribute__ ((unused)) int i;
11401 we want the attribute to attach to "i", not "lab". */
11402 if (label
!= NULL_TREE
11403 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11406 cp_parser_parse_tentatively (parser
);
11407 attrs
= cp_parser_gnu_attributes_opt (parser
);
11408 if (attrs
== NULL_TREE
11409 /* And fallthrough always binds to the expression-statement. */
11410 || attribute_fallthrough_p (attrs
)
11411 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11412 cp_parser_abort_tentative_parse (parser
);
11413 else if (!cp_parser_parse_definitely (parser
))
11416 attributes
= attr_chainon (attributes
, attrs
);
11419 if (attributes
!= NULL_TREE
)
11420 cplus_decl_attributes (&label
, attributes
, 0);
11422 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11425 /* Parse an expression-statement.
11427 expression-statement:
11430 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11431 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11432 indicates whether this expression-statement is part of an
11433 expression statement. */
11436 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11438 tree statement
= NULL_TREE
;
11439 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11440 location_t loc
= token
->location
;
11442 /* There might be attribute fallthrough. */
11443 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11445 /* If the next token is a ';', then there is no expression
11447 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11449 statement
= cp_parser_expression (parser
);
11450 if (statement
== error_mark_node
11451 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11453 cp_parser_skip_to_end_of_block_or_statement (parser
);
11454 return error_mark_node
;
11458 /* Handle [[fallthrough]];. */
11459 if (attribute_fallthrough_p (attr
))
11461 /* The next token after the fallthrough attribute is ';'. */
11462 if (statement
== NULL_TREE
)
11463 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11464 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11465 void_type_node
, 0);
11467 warning_at (loc
, OPT_Wattributes
,
11468 "%<fallthrough%> attribute not followed by %<;%>");
11472 /* Allow "[[fallthrough]];", but warn otherwise. */
11473 if (attr
!= NULL_TREE
)
11474 warning_at (loc
, OPT_Wattributes
,
11475 "attributes at the beginning of statement are ignored");
11477 /* Give a helpful message for "A<T>::type t;" and the like. */
11478 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11479 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11481 if (TREE_CODE (statement
) == SCOPE_REF
)
11482 error_at (token
->location
, "need %<typename%> before %qE because "
11483 "%qT is a dependent scope",
11484 statement
, TREE_OPERAND (statement
, 0));
11485 else if (is_overloaded_fn (statement
)
11486 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11489 tree fn
= get_first_fn (statement
);
11490 error_at (token
->location
,
11491 "%<%T::%D%> names the constructor, not the type",
11492 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11496 /* Consume the final `;'. */
11497 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11499 if (in_statement_expr
11500 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11501 /* This is the final expression statement of a statement
11503 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11504 else if (statement
)
11505 statement
= finish_expr_stmt (statement
);
11510 /* Parse a compound-statement.
11512 compound-statement:
11513 { statement-seq [opt] }
11517 compound-statement:
11518 { label-declaration-seq [opt] statement-seq [opt] }
11520 label-declaration-seq:
11522 label-declaration-seq label-declaration
11524 Returns a tree representing the statement. */
11527 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11528 int bcs_flags
, bool function_body
)
11530 tree compound_stmt
;
11531 matching_braces braces
;
11533 /* Consume the `{'. */
11534 if (!braces
.require_open (parser
))
11535 return error_mark_node
;
11536 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11537 && !function_body
&& cxx_dialect
< cxx14
)
11538 pedwarn (input_location
, OPT_Wpedantic
,
11539 "compound-statement in %<constexpr%> function");
11540 /* Begin the compound-statement. */
11541 compound_stmt
= begin_compound_stmt (bcs_flags
);
11542 /* If the next keyword is `__label__' we have a label declaration. */
11543 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11544 cp_parser_label_declaration (parser
);
11545 /* Parse an (optional) statement-seq. */
11546 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11547 /* Finish the compound-statement. */
11548 finish_compound_stmt (compound_stmt
);
11549 /* Consume the `}'. */
11550 braces
.require_close (parser
);
11552 return compound_stmt
;
11555 /* Parse an (optional) statement-seq.
11559 statement-seq [opt] statement */
11562 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11564 /* Scan statements until there aren't any more. */
11567 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11569 /* If we are looking at a `}', then we have run out of
11570 statements; the same is true if we have reached the end
11571 of file, or have stumbled upon a stray '@end'. */
11572 if (token
->type
== CPP_CLOSE_BRACE
11573 || token
->type
== CPP_EOF
11574 || token
->type
== CPP_PRAGMA_EOL
11575 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11578 /* If we are in a compound statement and find 'else' then
11579 something went wrong. */
11580 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11582 if (parser
->in_statement
& IN_IF_STMT
)
11586 token
= cp_lexer_consume_token (parser
->lexer
);
11587 error_at (token
->location
, "%<else%> without a previous %<if%>");
11591 /* Parse the statement. */
11592 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11596 /* Return true if this is the C++20 version of range-based-for with
11600 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11604 /* Save tokens so that we can put them back. */
11605 cp_lexer_save_tokens (parser
->lexer
);
11607 /* There has to be an unnested ; followed by an unnested :. */
11608 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11609 /*recovering=*/false,
11611 /*consume_paren=*/false) != -1)
11614 /* We found the semicolon, eat it now. */
11615 cp_lexer_consume_token (parser
->lexer
);
11617 /* Now look for ':' that is not nested in () or {}. */
11618 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11619 /*recovering=*/false,
11621 /*consume_paren=*/false) == -1);
11624 /* Roll back the tokens we skipped. */
11625 cp_lexer_rollback_tokens (parser
->lexer
);
11630 /* Return true if we're looking at (init; cond), false otherwise. */
11633 cp_parser_init_statement_p (cp_parser
*parser
)
11635 /* Save tokens so that we can put them back. */
11636 cp_lexer_save_tokens (parser
->lexer
);
11638 /* Look for ';' that is not nested in () or {}. */
11639 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11640 /*recovering=*/false,
11642 /*consume_paren=*/false);
11644 /* Roll back the tokens we skipped. */
11645 cp_lexer_rollback_tokens (parser
->lexer
);
11650 /* Parse a selection-statement.
11652 selection-statement:
11653 if ( init-statement [opt] condition ) statement
11654 if ( init-statement [opt] condition ) statement else statement
11655 switch ( init-statement [opt] condition ) statement
11657 Returns the new IF_STMT or SWITCH_STMT.
11659 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11660 is a (possibly labeled) if statement which is not enclosed in
11661 braces and has an else clause. This is used to implement
11664 CHAIN is a vector of if-else-if conditions. This is used to implement
11665 -Wduplicated-cond. */
11668 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11673 token_indent_info guard_tinfo
;
11678 /* Peek at the next token. */
11679 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11680 guard_tinfo
= get_token_indent_info (token
);
11682 /* See what kind of keyword it is. */
11683 keyword
= token
->keyword
;
11693 if (keyword
== RID_IF
11694 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11698 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11699 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11700 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11701 "with -std=c++17 or -std=gnu++17");
11704 /* Look for the `('. */
11705 matching_parens parens
;
11706 if (!parens
.require_open (parser
))
11708 cp_parser_skip_to_end_of_statement (parser
);
11709 return error_mark_node
;
11712 /* Begin the selection-statement. */
11713 if (keyword
== RID_IF
)
11715 statement
= begin_if_stmt ();
11716 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11719 statement
= begin_switch_stmt ();
11721 /* Parse the optional init-statement. */
11722 if (cp_parser_init_statement_p (parser
))
11725 if (cxx_dialect
< cxx17
)
11726 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11727 "init-statement in selection statements only available "
11728 "with -std=c++17 or -std=gnu++17");
11729 cp_parser_init_statement (parser
, &decl
);
11732 /* Parse the condition. */
11733 condition
= cp_parser_condition (parser
);
11734 /* Look for the `)'. */
11735 if (!parens
.require_close (parser
))
11736 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11737 /*consume_paren=*/true);
11739 if (keyword
== RID_IF
)
11742 unsigned char in_statement
;
11744 /* Add the condition. */
11745 condition
= finish_if_stmt_cond (condition
, statement
);
11747 if (warn_duplicated_cond
)
11748 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11751 /* Parse the then-clause. */
11752 in_statement
= parser
->in_statement
;
11753 parser
->in_statement
|= IN_IF_STMT
;
11755 /* Outside a template, the non-selected branch of a constexpr
11756 if is a 'discarded statement', i.e. unevaluated. */
11757 bool was_discarded
= in_discarded_stmt
;
11758 bool discard_then
= (cx
&& !processing_template_decl
11759 && integer_zerop (condition
));
11762 in_discarded_stmt
= true;
11763 ++c_inhibit_evaluation_warnings
;
11766 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11769 parser
->in_statement
= in_statement
;
11771 finish_then_clause (statement
);
11775 THEN_CLAUSE (statement
) = NULL_TREE
;
11776 in_discarded_stmt
= was_discarded
;
11777 --c_inhibit_evaluation_warnings
;
11780 /* If the next token is `else', parse the else-clause. */
11781 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11784 bool discard_else
= (cx
&& !processing_template_decl
11785 && integer_nonzerop (condition
));
11788 in_discarded_stmt
= true;
11789 ++c_inhibit_evaluation_warnings
;
11793 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11794 /* Consume the `else' keyword. */
11795 cp_lexer_consume_token (parser
->lexer
);
11796 if (warn_duplicated_cond
)
11798 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11802 /* We've got "if (COND) else if (COND2)". Start
11803 the condition chain and add COND as the first
11805 chain
= new vec
<tree
> ();
11806 if (!CONSTANT_CLASS_P (condition
)
11807 && !TREE_SIDE_EFFECTS (condition
))
11809 /* Wrap it in a NOP_EXPR so that we can set the
11810 location of the condition. */
11811 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11813 SET_EXPR_LOCATION (e
, token
->location
);
11814 chain
->safe_push (e
);
11817 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11820 /* This is if-else without subsequent if. Zap the
11821 condition chain; we would have already warned at
11827 begin_else_clause (statement
);
11828 /* Parse the else-clause. */
11829 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11830 guard_tinfo
, chain
);
11832 finish_else_clause (statement
);
11834 /* If we are currently parsing a then-clause, then
11835 IF_P will not be NULL. We set it to true to
11836 indicate that this if statement has an else clause.
11837 This may trigger the Wparentheses warning below
11838 when we get back up to the parent if statement. */
11844 ELSE_CLAUSE (statement
) = NULL_TREE
;
11845 in_discarded_stmt
= was_discarded
;
11846 --c_inhibit_evaluation_warnings
;
11851 /* This if statement does not have an else clause. If
11852 NESTED_IF is true, then the then-clause has an if
11853 statement which does have an else clause. We warn
11854 about the potential ambiguity. */
11856 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11857 "suggest explicit braces to avoid ambiguous"
11859 if (warn_duplicated_cond
)
11861 /* We don't need the condition chain anymore. */
11867 /* Now we're all done with the if-statement. */
11868 finish_if_stmt (statement
);
11872 bool in_switch_statement_p
;
11873 unsigned char in_statement
;
11875 /* Add the condition. */
11876 finish_switch_cond (condition
, statement
);
11878 /* Parse the body of the switch-statement. */
11879 in_switch_statement_p
= parser
->in_switch_statement_p
;
11880 in_statement
= parser
->in_statement
;
11881 parser
->in_switch_statement_p
= true;
11882 parser
->in_statement
|= IN_SWITCH_STMT
;
11883 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11885 parser
->in_switch_statement_p
= in_switch_statement_p
;
11886 parser
->in_statement
= in_statement
;
11888 /* Now we're all done with the switch-statement. */
11889 finish_switch_stmt (statement
);
11897 cp_parser_error (parser
, "expected selection-statement");
11898 return error_mark_node
;
11902 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11903 If we have seen at least one decl-specifier, and the next token
11904 is not a parenthesis, then we must be looking at a declaration.
11905 (After "int (" we might be looking at a functional cast.) */
11908 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11909 bool any_specifiers_p
)
11911 if (any_specifiers_p
11912 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11913 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11914 && !cp_parser_error_occurred (parser
))
11915 cp_parser_commit_to_tentative_parse (parser
);
11918 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11919 The declarator shall not specify a function or an array. Returns
11920 TRUE if the declarator is valid, FALSE otherwise. */
11923 cp_parser_check_condition_declarator (cp_parser
* parser
,
11924 cp_declarator
*declarator
,
11927 if (declarator
== cp_error_declarator
11928 || function_declarator_p (declarator
)
11929 || declarator
->kind
== cdk_array
)
11931 if (declarator
== cp_error_declarator
)
11932 /* Already complained. */;
11933 else if (declarator
->kind
== cdk_array
)
11934 error_at (loc
, "condition declares an array");
11936 error_at (loc
, "condition declares a function");
11937 if (parser
->fully_implicit_function_template_p
)
11938 abort_fully_implicit_template (parser
);
11939 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11940 /*or_comma=*/false,
11941 /*consume_paren=*/false);
11948 /* Parse a condition.
11952 type-specifier-seq declarator = initializer-clause
11953 type-specifier-seq declarator braced-init-list
11958 type-specifier-seq declarator asm-specification [opt]
11959 attributes [opt] = assignment-expression
11961 Returns the expression that should be tested. */
11964 cp_parser_condition (cp_parser
* parser
)
11966 cp_decl_specifier_seq type_specifiers
;
11967 const char *saved_message
;
11968 int declares_class_or_enum
;
11970 /* Try the declaration first. */
11971 cp_parser_parse_tentatively (parser
);
11972 /* New types are not allowed in the type-specifier-seq for a
11974 saved_message
= parser
->type_definition_forbidden_message
;
11975 parser
->type_definition_forbidden_message
11976 = G_("types may not be defined in conditions");
11977 /* Parse the type-specifier-seq. */
11978 cp_parser_decl_specifier_seq (parser
,
11979 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11981 &declares_class_or_enum
);
11982 /* Restore the saved message. */
11983 parser
->type_definition_forbidden_message
= saved_message
;
11985 cp_parser_maybe_commit_to_declaration (parser
,
11986 type_specifiers
.any_specifiers_p
);
11988 /* If all is well, we might be looking at a declaration. */
11989 if (!cp_parser_error_occurred (parser
))
11992 tree asm_specification
;
11994 cp_declarator
*declarator
;
11995 tree initializer
= NULL_TREE
;
11996 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11998 /* Parse the declarator. */
11999 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12000 CP_PARSER_FLAGS_NONE
,
12001 /*ctor_dtor_or_conv_p=*/NULL
,
12002 /*parenthesized_p=*/NULL
,
12003 /*member_p=*/false,
12004 /*friend_p=*/false,
12005 /*static_p=*/false);
12006 /* Parse the attributes. */
12007 attributes
= cp_parser_attributes_opt (parser
);
12008 /* Parse the asm-specification. */
12009 asm_specification
= cp_parser_asm_specification_opt (parser
);
12010 /* If the next token is not an `=' or '{', then we might still be
12011 looking at an expression. For example:
12015 looks like a decl-specifier-seq and a declarator -- but then
12016 there is no `=', so this is an expression. */
12017 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
12018 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12019 cp_parser_simulate_error (parser
);
12021 /* If we did see an `=' or '{', then we are looking at a declaration
12023 if (cp_parser_parse_definitely (parser
))
12026 bool non_constant_p
= false;
12027 int flags
= LOOKUP_ONLYCONVERTING
;
12029 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
12030 return error_mark_node
;
12032 /* Create the declaration. */
12033 decl
= start_decl (declarator
, &type_specifiers
,
12034 /*initialized_p=*/true,
12035 attributes
, /*prefix_attributes=*/NULL_TREE
,
12038 /* Parse the initializer. */
12039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12041 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
12042 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
12045 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12047 /* Consume the `='. */
12048 cp_lexer_consume_token (parser
->lexer
);
12049 initializer
= cp_parser_initializer_clause (parser
,
12054 cp_parser_error (parser
, "expected initializer");
12055 initializer
= error_mark_node
;
12057 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
12058 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12060 /* Process the initializer. */
12061 cp_finish_decl (decl
,
12062 initializer
, !non_constant_p
,
12067 pop_scope (pushed_scope
);
12069 return convert_from_reference (decl
);
12072 /* If we didn't even get past the declarator successfully, we are
12073 definitely not looking at a declaration. */
12075 cp_parser_abort_tentative_parse (parser
);
12077 /* Otherwise, we are looking at an expression. */
12078 return cp_parser_expression (parser
);
12081 /* Parses a for-statement or range-for-statement until the closing ')',
12085 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
12087 tree init
, scope
, decl
;
12090 /* Begin the for-statement. */
12091 scope
= begin_for_scope (&init
);
12093 /* Parse the initialization. */
12094 is_range_for
= cp_parser_init_statement (parser
, &decl
);
12097 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
12100 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
12104 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
12105 unsigned short unroll
)
12107 /* Normal for loop */
12108 tree condition
= NULL_TREE
;
12109 tree expression
= NULL_TREE
;
12112 stmt
= begin_for_stmt (scope
, init
);
12113 /* The init-statement has already been parsed in
12114 cp_parser_init_statement, so no work is needed here. */
12115 finish_init_stmt (stmt
);
12117 /* If there's a condition, process it. */
12118 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12119 condition
= cp_parser_condition (parser
);
12122 cp_parser_error (parser
, "missing loop condition in loop with "
12123 "%<GCC ivdep%> pragma");
12124 condition
= error_mark_node
;
12128 cp_parser_error (parser
, "missing loop condition in loop with "
12129 "%<GCC unroll%> pragma");
12130 condition
= error_mark_node
;
12132 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
12133 /* Look for the `;'. */
12134 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12136 /* If there's an expression, process it. */
12137 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
12138 expression
= cp_parser_expression (parser
);
12139 finish_for_expr (expression
, stmt
);
12144 /* Tries to parse a range-based for-statement:
12147 decl-specifier-seq declarator : expression
12149 The decl-specifier-seq declarator and the `:' are already parsed by
12150 cp_parser_init_statement. If processing_template_decl it returns a
12151 newly created RANGE_FOR_STMT; if not, it is converted to a
12152 regular FOR_STMT. */
12155 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
12156 bool ivdep
, unsigned short unroll
, bool is_omp
)
12158 tree stmt
, range_expr
;
12159 auto_vec
<cxx_binding
*, 16> bindings
;
12160 auto_vec
<tree
, 16> names
;
12161 tree decomp_first_name
= NULL_TREE
;
12162 unsigned int decomp_cnt
= 0;
12164 /* Get the range declaration momentarily out of the way so that
12165 the range expression doesn't clash with it. */
12166 if (range_decl
!= error_mark_node
)
12168 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
12170 tree v
= DECL_VALUE_EXPR (range_decl
);
12171 /* For decomposition declaration get all of the corresponding
12172 declarations out of the way. */
12173 if (TREE_CODE (v
) == ARRAY_REF
12174 && VAR_P (TREE_OPERAND (v
, 0))
12175 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
12177 tree d
= range_decl
;
12178 range_decl
= TREE_OPERAND (v
, 0);
12179 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
12180 decomp_first_name
= d
;
12181 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
12183 tree name
= DECL_NAME (d
);
12184 names
.safe_push (name
);
12185 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12186 IDENTIFIER_BINDING (name
)
12187 = IDENTIFIER_BINDING (name
)->previous
;
12191 if (names
.is_empty ())
12193 tree name
= DECL_NAME (range_decl
);
12194 names
.safe_push (name
);
12195 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12196 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
12200 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12202 bool expr_non_constant_p
;
12203 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12206 range_expr
= cp_parser_expression (parser
);
12208 /* Put the range declaration(s) back into scope. */
12209 for (unsigned int i
= 0; i
< names
.length (); i
++)
12211 cxx_binding
*binding
= bindings
[i
];
12212 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12213 IDENTIFIER_BINDING (names
[i
]) = binding
;
12216 /* finish_omp_for has its own code for the following, so just
12217 return the range_expr instead. */
12221 /* If in template, STMT is converted to a normal for-statement
12222 at instantiation. If not, it is done just ahead. */
12223 if (processing_template_decl
)
12225 if (check_for_bare_parameter_packs (range_expr
))
12226 range_expr
= error_mark_node
;
12227 stmt
= begin_range_for_stmt (scope
, init
);
12229 RANGE_FOR_IVDEP (stmt
) = 1;
12231 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12232 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12233 if (!type_dependent_expression_p (range_expr
)
12234 /* do_auto_deduction doesn't mess with template init-lists. */
12235 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12236 do_range_for_auto_deduction (range_decl
, range_expr
);
12240 stmt
= begin_for_stmt (scope
, init
);
12241 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12242 decomp_first_name
, decomp_cnt
, ivdep
,
12248 /* Subroutine of cp_convert_range_for: given the initializer expression,
12249 builds up the range temporary. */
12252 build_range_temp (tree range_expr
)
12254 tree range_type
, range_temp
;
12256 /* Find out the type deduced by the declaration
12257 `auto &&__range = range_expr'. */
12258 range_type
= cp_build_reference_type (make_auto (), true);
12259 range_type
= do_auto_deduction (range_type
, range_expr
,
12260 type_uses_auto (range_type
));
12262 /* Create the __range variable. */
12263 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12265 TREE_USED (range_temp
) = 1;
12266 DECL_ARTIFICIAL (range_temp
) = 1;
12271 /* Used by cp_parser_range_for in template context: we aren't going to
12272 do a full conversion yet, but we still need to resolve auto in the
12273 type of the for-range-declaration if present. This is basically
12274 a shortcut version of cp_convert_range_for. */
12277 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12279 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12282 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12283 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12284 iter_type
= (cp_parser_perform_range_for_lookup
12285 (range_temp
, &begin_dummy
, &end_dummy
));
12288 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12290 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12292 tf_warning_or_error
);
12293 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12294 iter_decl
, auto_node
);
12299 /* Converts a range-based for-statement into a normal
12300 for-statement, as per the definition.
12302 for (RANGE_DECL : RANGE_EXPR)
12305 should be equivalent to:
12308 auto &&__range = RANGE_EXPR;
12309 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12313 RANGE_DECL = *__begin;
12318 If RANGE_EXPR is an array:
12319 BEGIN_EXPR = __range
12320 END_EXPR = __range + ARRAY_SIZE(__range)
12321 Else if RANGE_EXPR has a member 'begin' or 'end':
12322 BEGIN_EXPR = __range.begin()
12323 END_EXPR = __range.end()
12325 BEGIN_EXPR = begin(__range)
12326 END_EXPR = end(__range);
12328 If __range has a member 'begin' but not 'end', or vice versa, we must
12329 still use the second alternative (it will surely fail, however).
12330 When calling begin()/end() in the third alternative we must use
12331 argument dependent lookup, but always considering 'std' as an associated
12335 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12336 tree decomp_first_name
, unsigned int decomp_cnt
,
12337 bool ivdep
, unsigned short unroll
)
12340 tree iter_type
, begin_expr
, end_expr
;
12341 tree condition
, expression
;
12343 range_expr
= mark_lvalue_use (range_expr
);
12345 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12346 /* If an error happened previously do nothing or else a lot of
12347 unhelpful errors would be issued. */
12348 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12353 if (VAR_P (range_expr
)
12354 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12355 /* Can't bind a reference to an array of runtime bound. */
12356 range_temp
= range_expr
;
12359 range_temp
= build_range_temp (range_expr
);
12360 pushdecl (range_temp
);
12361 cp_finish_decl (range_temp
, range_expr
,
12362 /*is_constant_init*/false, NULL_TREE
,
12363 LOOKUP_ONLYCONVERTING
);
12364 range_temp
= convert_from_reference (range_temp
);
12366 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12367 &begin_expr
, &end_expr
);
12370 /* The new for initialization statement. */
12371 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12373 TREE_USED (begin
) = 1;
12374 DECL_ARTIFICIAL (begin
) = 1;
12376 cp_finish_decl (begin
, begin_expr
,
12377 /*is_constant_init*/false, NULL_TREE
,
12378 LOOKUP_ONLYCONVERTING
);
12380 if (cxx_dialect
>= cxx17
)
12381 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12382 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12383 TREE_USED (end
) = 1;
12384 DECL_ARTIFICIAL (end
) = 1;
12386 cp_finish_decl (end
, end_expr
,
12387 /*is_constant_init*/false, NULL_TREE
,
12388 LOOKUP_ONLYCONVERTING
);
12390 finish_init_stmt (statement
);
12392 /* The new for condition. */
12393 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12396 NULL
, tf_warning_or_error
);
12397 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12399 /* The new increment expression. */
12400 expression
= finish_unary_op_expr (input_location
,
12401 PREINCREMENT_EXPR
, begin
,
12402 tf_warning_or_error
);
12403 finish_for_expr (expression
, statement
);
12405 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12406 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12408 /* The declaration is initialized with *__begin inside the loop body. */
12409 cp_finish_decl (range_decl
,
12410 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12411 tf_warning_or_error
),
12412 /*is_constant_init*/false, NULL_TREE
,
12413 LOOKUP_ONLYCONVERTING
);
12414 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12415 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12420 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12421 We need to solve both at the same time because the method used
12422 depends on the existence of members begin or end.
12423 Returns the type deduced for the iterator expression. */
12426 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12428 if (error_operand_p (range
))
12430 *begin
= *end
= error_mark_node
;
12431 return error_mark_node
;
12434 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12436 error ("range-based %<for%> expression of type %qT "
12437 "has incomplete type", TREE_TYPE (range
));
12438 *begin
= *end
= error_mark_node
;
12439 return error_mark_node
;
12441 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12443 /* If RANGE is an array, we will use pointer arithmetic. */
12444 *begin
= decay_conversion (range
, tf_warning_or_error
);
12445 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12447 array_type_nelts_top (TREE_TYPE (range
)),
12449 return TREE_TYPE (*begin
);
12453 /* If it is not an array, we must do a bit of magic. */
12454 tree id_begin
, id_end
;
12455 tree member_begin
, member_end
;
12457 *begin
= *end
= error_mark_node
;
12459 id_begin
= get_identifier ("begin");
12460 id_end
= get_identifier ("end");
12461 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12462 /*protect=*/2, /*want_type=*/false,
12463 tf_warning_or_error
);
12464 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12465 /*protect=*/2, /*want_type=*/false,
12466 tf_warning_or_error
);
12468 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12470 /* Use the member functions. */
12471 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12472 *end
= cp_parser_range_for_member_function (range
, id_end
);
12476 /* Use global functions with ADL. */
12477 vec
<tree
, va_gc
> *vec
;
12478 vec
= make_tree_vector ();
12480 vec_safe_push (vec
, range
);
12482 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12483 tf_warning_or_error
);
12484 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12485 tf_warning_or_error
);
12486 member_end
= perform_koenig_lookup (id_end
, vec
,
12487 tf_warning_or_error
);
12488 *end
= finish_call_expr (member_end
, &vec
, false, true,
12489 tf_warning_or_error
);
12491 release_tree_vector (vec
);
12494 /* Last common checks. */
12495 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12497 /* If one of the expressions is an error do no more checks. */
12498 *begin
= *end
= error_mark_node
;
12499 return error_mark_node
;
12501 else if (type_dependent_expression_p (*begin
)
12502 || type_dependent_expression_p (*end
))
12503 /* Can happen, when, eg, in a template context, Koenig lookup
12504 can't resolve begin/end (c++/58503). */
12508 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12509 /* The unqualified type of the __begin and __end temporaries should
12510 be the same, as required by the multiple auto declaration. */
12511 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12513 if (cxx_dialect
>= cxx17
12514 && (build_x_binary_op (input_location
, NE_EXPR
,
12515 *begin
, ERROR_MARK
,
12518 != error_mark_node
))
12519 /* P0184R0 allows __begin and __end to have different types,
12520 but make sure they are comparable so we can give a better
12523 error ("inconsistent begin/end types in range-based %<for%> "
12524 "statement: %qT and %qT",
12525 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12532 /* Helper function for cp_parser_perform_range_for_lookup.
12533 Builds a tree for RANGE.IDENTIFIER(). */
12536 cp_parser_range_for_member_function (tree range
, tree identifier
)
12539 vec
<tree
, va_gc
> *vec
;
12541 member
= finish_class_member_access_expr (range
, identifier
,
12542 false, tf_warning_or_error
);
12543 if (member
== error_mark_node
)
12544 return error_mark_node
;
12546 vec
= make_tree_vector ();
12547 res
= finish_call_expr (member
, &vec
,
12548 /*disallow_virtual=*/false,
12549 /*koenig_p=*/false,
12550 tf_warning_or_error
);
12551 release_tree_vector (vec
);
12555 /* Parse an iteration-statement.
12557 iteration-statement:
12558 while ( condition ) statement
12559 do statement while ( expression ) ;
12560 for ( init-statement condition [opt] ; expression [opt] )
12563 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12566 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12567 unsigned short unroll
)
12572 unsigned char in_statement
;
12573 token_indent_info guard_tinfo
;
12575 /* Peek at the next token. */
12576 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12578 return error_mark_node
;
12580 guard_tinfo
= get_token_indent_info (token
);
12582 /* Remember whether or not we are already within an iteration
12584 in_statement
= parser
->in_statement
;
12586 /* See what kind of keyword it is. */
12587 keyword
= token
->keyword
;
12594 /* Begin the while-statement. */
12595 statement
= begin_while_stmt ();
12596 /* Look for the `('. */
12597 matching_parens parens
;
12598 parens
.require_open (parser
);
12599 /* Parse the condition. */
12600 condition
= cp_parser_condition (parser
);
12601 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12602 /* Look for the `)'. */
12603 parens
.require_close (parser
);
12604 /* Parse the dependent statement. */
12605 parser
->in_statement
= IN_ITERATION_STMT
;
12606 bool prev
= note_iteration_stmt_body_start ();
12607 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12608 note_iteration_stmt_body_end (prev
);
12609 parser
->in_statement
= in_statement
;
12610 /* We're done with the while-statement. */
12611 finish_while_stmt (statement
);
12619 /* Begin the do-statement. */
12620 statement
= begin_do_stmt ();
12621 /* Parse the body of the do-statement. */
12622 parser
->in_statement
= IN_ITERATION_STMT
;
12623 bool prev
= note_iteration_stmt_body_start ();
12624 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12625 note_iteration_stmt_body_end (prev
);
12626 parser
->in_statement
= in_statement
;
12627 finish_do_body (statement
);
12628 /* Look for the `while' keyword. */
12629 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12630 /* Look for the `('. */
12631 matching_parens parens
;
12632 parens
.require_open (parser
);
12633 /* Parse the expression. */
12634 expression
= cp_parser_expression (parser
);
12635 /* We're done with the do-statement. */
12636 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12637 /* Look for the `)'. */
12638 parens
.require_close (parser
);
12639 /* Look for the `;'. */
12640 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12646 /* Look for the `('. */
12647 matching_parens parens
;
12648 parens
.require_open (parser
);
12650 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12652 /* Look for the `)'. */
12653 parens
.require_close (parser
);
12655 /* Parse the body of the for-statement. */
12656 parser
->in_statement
= IN_ITERATION_STMT
;
12657 bool prev
= note_iteration_stmt_body_start ();
12658 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12659 note_iteration_stmt_body_end (prev
);
12660 parser
->in_statement
= in_statement
;
12662 /* We're done with the for-statement. */
12663 finish_for_stmt (statement
);
12668 cp_parser_error (parser
, "expected iteration-statement");
12669 statement
= error_mark_node
;
12676 /* Parse a init-statement or the declarator of a range-based-for.
12677 Returns true if a range-based-for declaration is seen.
12680 expression-statement
12681 simple-declaration */
12684 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12686 /* If the next token is a `;', then we have an empty
12687 expression-statement. Grammatically, this is also a
12688 simple-declaration, but an invalid one, because it does not
12689 declare anything. Therefore, if we did not handle this case
12690 specially, we would issue an error message about an invalid
12692 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12694 bool is_range_for
= false;
12695 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12697 /* Try to parse the init-statement. */
12698 if (cp_parser_range_based_for_with_init_p (parser
))
12701 cp_parser_parse_tentatively (parser
);
12702 /* Parse the declaration. */
12703 cp_parser_simple_declaration (parser
,
12704 /*function_definition_allowed_p=*/false,
12706 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12707 if (!cp_parser_parse_definitely (parser
))
12708 /* That didn't work, try to parse it as an expression-statement. */
12709 cp_parser_expression_statement (parser
, NULL_TREE
);
12711 if (cxx_dialect
< cxx2a
)
12713 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12714 "range-based %<for%> loops with initializer only "
12715 "available with -std=c++2a or -std=gnu++2a");
12716 *decl
= error_mark_node
;
12720 /* A colon is used in range-based for. */
12721 parser
->colon_corrects_to_scope_p
= false;
12723 /* We're going to speculatively look for a declaration, falling back
12724 to an expression, if necessary. */
12725 cp_parser_parse_tentatively (parser
);
12726 /* Parse the declaration. */
12727 cp_parser_simple_declaration (parser
,
12728 /*function_definition_allowed_p=*/false,
12730 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12731 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12733 /* It is a range-for, consume the ':'. */
12734 cp_lexer_consume_token (parser
->lexer
);
12735 is_range_for
= true;
12736 if (cxx_dialect
< cxx11
)
12737 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12738 "range-based %<for%> loops only available with "
12739 "-std=c++11 or -std=gnu++11");
12742 /* The ';' is not consumed yet because we told
12743 cp_parser_simple_declaration not to. */
12744 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12746 if (cp_parser_parse_definitely (parser
))
12747 return is_range_for
;
12748 /* If the tentative parse failed, then we shall need to look for an
12749 expression-statement. */
12751 /* If we are here, it is an expression-statement. */
12752 cp_parser_expression_statement (parser
, NULL_TREE
);
12756 /* Parse a jump-statement.
12761 return expression [opt] ;
12762 return braced-init-list ;
12768 goto * expression ;
12770 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12773 cp_parser_jump_statement (cp_parser
* parser
)
12775 tree statement
= error_mark_node
;
12778 unsigned char in_statement
;
12780 /* Peek at the next token. */
12781 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12783 return error_mark_node
;
12785 /* See what kind of keyword it is. */
12786 keyword
= token
->keyword
;
12790 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12791 switch (in_statement
)
12794 error_at (token
->location
, "break statement not within loop or switch");
12797 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12798 || in_statement
== IN_ITERATION_STMT
);
12799 statement
= finish_break_stmt ();
12800 if (in_statement
== IN_ITERATION_STMT
)
12801 break_maybe_infinite_loop ();
12804 error_at (token
->location
, "invalid exit from OpenMP structured block");
12807 error_at (token
->location
, "break statement used with OpenMP for loop");
12810 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12814 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12817 error_at (token
->location
, "continue statement not within a loop");
12819 /* Fall through. */
12820 case IN_ITERATION_STMT
:
12822 statement
= finish_continue_stmt ();
12825 error_at (token
->location
, "invalid exit from OpenMP structured block");
12828 gcc_unreachable ();
12830 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12836 bool expr_non_constant_p
;
12838 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12840 cp_lexer_set_source_position (parser
->lexer
);
12841 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12842 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12844 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12845 expr
= cp_parser_expression (parser
);
12847 /* If the next token is a `;', then there is no
12850 /* Build the return-statement. */
12851 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12852 /* Don't deduce from a discarded return statement. */;
12854 statement
= finish_return_stmt (expr
);
12855 /* Look for the final `;'. */
12856 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12861 if (parser
->in_function_body
12862 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12864 error ("%<goto%> in %<constexpr%> function");
12865 cp_function_chain
->invalid_constexpr
= true;
12868 /* Create the goto-statement. */
12869 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12871 /* Issue a warning about this use of a GNU extension. */
12872 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12873 /* Consume the '*' token. */
12874 cp_lexer_consume_token (parser
->lexer
);
12875 /* Parse the dependent expression. */
12876 finish_goto_stmt (cp_parser_expression (parser
));
12879 finish_goto_stmt (cp_parser_identifier (parser
));
12880 /* Look for the final `;'. */
12881 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12885 cp_parser_error (parser
, "expected jump-statement");
12892 /* Parse a declaration-statement.
12894 declaration-statement:
12895 block-declaration */
12898 cp_parser_declaration_statement (cp_parser
* parser
)
12902 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12903 p
= obstack_alloc (&declarator_obstack
, 0);
12905 /* Parse the block-declaration. */
12906 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12908 /* Free any declarators allocated. */
12909 obstack_free (&declarator_obstack
, p
);
12912 /* Some dependent statements (like `if (cond) statement'), are
12913 implicitly in their own scope. In other words, if the statement is
12914 a single statement (as opposed to a compound-statement), it is
12915 none-the-less treated as if it were enclosed in braces. Any
12916 declarations appearing in the dependent statement are out of scope
12917 after control passes that point. This function parses a statement,
12918 but ensures that is in its own scope, even if it is not a
12919 compound-statement.
12921 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12922 is a (possibly labeled) if statement which is not enclosed in
12923 braces and has an else clause. This is used to implement
12926 CHAIN is a vector of if-else-if conditions. This is used to implement
12929 Returns the new statement. */
12932 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12933 const token_indent_info
&guard_tinfo
,
12937 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12938 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12939 token_indent_info body_tinfo
12940 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12945 /* Mark if () ; with a special NOP_EXPR. */
12946 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12948 cp_lexer_consume_token (parser
->lexer
);
12949 statement
= add_stmt (build_empty_stmt (body_loc
));
12951 if (guard_tinfo
.keyword
== RID_IF
12952 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12953 warning_at (body_loc
, OPT_Wempty_body
,
12954 "suggest braces around empty body in an %<if%> statement");
12955 else if (guard_tinfo
.keyword
== RID_ELSE
)
12956 warning_at (body_loc
, OPT_Wempty_body
,
12957 "suggest braces around empty body in an %<else%> statement");
12959 /* if a compound is opened, we simply parse the statement directly. */
12960 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12961 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12962 /* If the token is not a `{', then we must take special action. */
12965 /* Create a compound-statement. */
12966 statement
= begin_compound_stmt (0);
12967 /* Parse the dependent-statement. */
12968 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12969 &body_loc_after_labels
);
12970 /* Finish the dummy compound-statement. */
12971 finish_compound_stmt (statement
);
12974 token_indent_info next_tinfo
12975 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12976 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12978 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12979 && next_tinfo
.type
!= CPP_SEMICOLON
)
12980 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12981 guard_tinfo
.location
, guard_tinfo
.keyword
);
12983 /* Return the statement. */
12987 /* For some dependent statements (like `while (cond) statement'), we
12988 have already created a scope. Therefore, even if the dependent
12989 statement is a compound-statement, we do not want to create another
12993 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12994 const token_indent_info
&guard_tinfo
)
12996 /* If the token is a `{', then we must take special action. */
12997 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12999 token_indent_info body_tinfo
13000 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13001 location_t loc_after_labels
= UNKNOWN_LOCATION
;
13003 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
13004 &loc_after_labels
);
13005 token_indent_info next_tinfo
13006 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13007 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13009 if (loc_after_labels
!= UNKNOWN_LOCATION
13010 && next_tinfo
.type
!= CPP_SEMICOLON
)
13011 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
13012 guard_tinfo
.location
,
13013 guard_tinfo
.keyword
);
13017 /* Avoid calling cp_parser_compound_statement, so that we
13018 don't create a new scope. Do everything else by hand. */
13019 matching_braces braces
;
13020 braces
.require_open (parser
);
13021 /* If the next keyword is `__label__' we have a label declaration. */
13022 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
13023 cp_parser_label_declaration (parser
);
13024 /* Parse an (optional) statement-seq. */
13025 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
13026 braces
.require_close (parser
);
13030 /* Declarations [gram.dcl.dcl] */
13032 /* Parse an optional declaration-sequence.
13036 declaration-seq declaration */
13039 cp_parser_declaration_seq_opt (cp_parser
* parser
)
13043 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13045 if (token
->type
== CPP_CLOSE_BRACE
13046 || token
->type
== CPP_EOF
)
13049 cp_parser_toplevel_declaration (parser
);
13053 /* Parse a declaration.
13057 function-definition
13058 template-declaration
13059 explicit-instantiation
13060 explicit-specialization
13061 linkage-specification
13062 namespace-definition
13070 __extension__ declaration */
13073 cp_parser_declaration (cp_parser
* parser
)
13077 int saved_pedantic
;
13079 tree attributes
= NULL_TREE
;
13081 /* Check for the `__extension__' keyword. */
13082 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13084 /* Parse the qualified declaration. */
13085 cp_parser_declaration (parser
);
13086 /* Restore the PEDANTIC flag. */
13087 pedantic
= saved_pedantic
;
13092 /* Try to figure out what kind of declaration is present. */
13093 token1
= *cp_lexer_peek_token (parser
->lexer
);
13095 if (token1
.type
!= CPP_EOF
)
13096 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
13099 token2
.type
= CPP_EOF
;
13100 token2
.keyword
= RID_MAX
;
13103 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13104 p
= obstack_alloc (&declarator_obstack
, 0);
13106 /* If the next token is `extern' and the following token is a string
13107 literal, then we have a linkage specification. */
13108 if (token1
.keyword
== RID_EXTERN
13109 && cp_parser_is_pure_string_literal (&token2
))
13110 cp_parser_linkage_specification (parser
);
13111 /* If the next token is `template', then we have either a template
13112 declaration, an explicit instantiation, or an explicit
13114 else if (token1
.keyword
== RID_TEMPLATE
)
13116 /* `template <>' indicates a template specialization. */
13117 if (token2
.type
== CPP_LESS
13118 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13119 cp_parser_explicit_specialization (parser
);
13120 /* `template <' indicates a template declaration. */
13121 else if (token2
.type
== CPP_LESS
)
13122 cp_parser_template_declaration (parser
, /*member_p=*/false);
13123 /* Anything else must be an explicit instantiation. */
13125 cp_parser_explicit_instantiation (parser
);
13127 /* If the next token is `export', then we have a template
13129 else if (token1
.keyword
== RID_EXPORT
)
13130 cp_parser_template_declaration (parser
, /*member_p=*/false);
13131 /* If the next token is `extern', 'static' or 'inline' and the one
13132 after that is `template', we have a GNU extended explicit
13133 instantiation directive. */
13134 else if (cp_parser_allow_gnu_extensions_p (parser
)
13135 && (token1
.keyword
== RID_EXTERN
13136 || token1
.keyword
== RID_STATIC
13137 || token1
.keyword
== RID_INLINE
)
13138 && token2
.keyword
== RID_TEMPLATE
)
13139 cp_parser_explicit_instantiation (parser
);
13140 /* If the next token is `namespace', check for a named or unnamed
13141 namespace definition. */
13142 else if (token1
.keyword
== RID_NAMESPACE
13143 && (/* A named namespace definition. */
13144 (token2
.type
== CPP_NAME
13145 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13147 || (token2
.type
== CPP_OPEN_SQUARE
13148 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13149 == CPP_OPEN_SQUARE
)
13150 /* An unnamed namespace definition. */
13151 || token2
.type
== CPP_OPEN_BRACE
13152 || token2
.keyword
== RID_ATTRIBUTE
))
13153 cp_parser_namespace_definition (parser
);
13154 /* An inline (associated) namespace definition. */
13155 else if (token1
.keyword
== RID_INLINE
13156 && token2
.keyword
== RID_NAMESPACE
)
13157 cp_parser_namespace_definition (parser
);
13158 /* Objective-C++ declaration/definition. */
13159 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
13160 cp_parser_objc_declaration (parser
, NULL_TREE
);
13161 else if (c_dialect_objc ()
13162 && token1
.keyword
== RID_ATTRIBUTE
13163 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
13164 cp_parser_objc_declaration (parser
, attributes
);
13165 /* At this point we may have a template declared by a concept
13167 else if (flag_concepts
13168 && cp_parser_template_declaration_after_export (parser
,
13169 /*member_p=*/false))
13172 /* Try to parse a block-declaration, or a function-definition. */
13173 cp_parser_block_declaration (parser
, /*statement_p=*/false);
13175 /* Free any declarators allocated. */
13176 obstack_free (&declarator_obstack
, p
);
13179 /* Parse a namespace-scope declaration. */
13182 cp_parser_toplevel_declaration (cp_parser
* parser
)
13184 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13186 if (token
->type
== CPP_PRAGMA
)
13187 /* A top-level declaration can consist solely of a #pragma. A
13188 nested declaration cannot, so this is done here and not in
13189 cp_parser_declaration. (A #pragma at block scope is
13190 handled in cp_parser_statement.) */
13191 cp_parser_pragma (parser
, pragma_external
, NULL
);
13192 else if (token
->type
== CPP_SEMICOLON
)
13194 /* A declaration consisting of a single semicolon is
13195 invalid. Allow it unless we're being pedantic. */
13196 cp_lexer_consume_token (parser
->lexer
);
13197 if (!in_system_header_at (input_location
))
13198 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
13201 /* Parse the declaration itself. */
13202 cp_parser_declaration (parser
);
13205 /* Parse a block-declaration.
13210 namespace-alias-definition
13217 __extension__ block-declaration
13222 static_assert-declaration
13224 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13225 part of a declaration-statement. */
13228 cp_parser_block_declaration (cp_parser
*parser
,
13232 int saved_pedantic
;
13234 /* Check for the `__extension__' keyword. */
13235 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13237 /* Parse the qualified declaration. */
13238 cp_parser_block_declaration (parser
, statement_p
);
13239 /* Restore the PEDANTIC flag. */
13240 pedantic
= saved_pedantic
;
13245 /* Peek at the next token to figure out which kind of declaration is
13247 token1
= cp_lexer_peek_token (parser
->lexer
);
13249 /* If the next keyword is `asm', we have an asm-definition. */
13250 if (token1
->keyword
== RID_ASM
)
13253 cp_parser_commit_to_tentative_parse (parser
);
13254 cp_parser_asm_definition (parser
);
13256 /* If the next keyword is `namespace', we have a
13257 namespace-alias-definition. */
13258 else if (token1
->keyword
== RID_NAMESPACE
)
13259 cp_parser_namespace_alias_definition (parser
);
13260 /* If the next keyword is `using', we have a
13261 using-declaration, a using-directive, or an alias-declaration. */
13262 else if (token1
->keyword
== RID_USING
)
13267 cp_parser_commit_to_tentative_parse (parser
);
13268 /* If the token after `using' is `namespace', then we have a
13269 using-directive. */
13270 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13271 if (token2
->keyword
== RID_NAMESPACE
)
13272 cp_parser_using_directive (parser
);
13273 /* If the second token after 'using' is '=', then we have an
13274 alias-declaration. */
13275 else if (cxx_dialect
>= cxx11
13276 && token2
->type
== CPP_NAME
13277 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13278 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13279 cp_parser_alias_declaration (parser
);
13280 /* Otherwise, it's a using-declaration. */
13282 cp_parser_using_declaration (parser
,
13283 /*access_declaration_p=*/false);
13285 /* If the next keyword is `__label__' we have a misplaced label
13287 else if (token1
->keyword
== RID_LABEL
)
13289 cp_lexer_consume_token (parser
->lexer
);
13290 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13291 cp_parser_skip_to_end_of_statement (parser
);
13292 /* If the next token is now a `;', consume it. */
13293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13294 cp_lexer_consume_token (parser
->lexer
);
13296 /* If the next token is `static_assert' we have a static assertion. */
13297 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13298 cp_parser_static_assert (parser
, /*member_p=*/false);
13299 /* Anything else must be a simple-declaration. */
13301 cp_parser_simple_declaration (parser
, !statement_p
,
13302 /*maybe_range_for_decl*/NULL
);
13305 /* Parse a simple-declaration.
13307 simple-declaration:
13308 decl-specifier-seq [opt] init-declarator-list [opt] ;
13309 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13310 brace-or-equal-initializer ;
13312 init-declarator-list:
13314 init-declarator-list , init-declarator
13316 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13317 function-definition as a simple-declaration.
13319 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13320 parsed declaration if it is an uninitialized single declarator not followed
13321 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13322 if present, will not be consumed. */
13325 cp_parser_simple_declaration (cp_parser
* parser
,
13326 bool function_definition_allowed_p
,
13327 tree
*maybe_range_for_decl
)
13329 cp_decl_specifier_seq decl_specifiers
;
13330 int declares_class_or_enum
;
13331 bool saw_declarator
;
13332 location_t comma_loc
= UNKNOWN_LOCATION
;
13333 location_t init_loc
= UNKNOWN_LOCATION
;
13335 if (maybe_range_for_decl
)
13336 *maybe_range_for_decl
= NULL_TREE
;
13338 /* Defer access checks until we know what is being declared; the
13339 checks for names appearing in the decl-specifier-seq should be
13340 done as if we were in the scope of the thing being declared. */
13341 push_deferring_access_checks (dk_deferred
);
13343 /* Parse the decl-specifier-seq. We have to keep track of whether
13344 or not the decl-specifier-seq declares a named class or
13345 enumeration type, since that is the only case in which the
13346 init-declarator-list is allowed to be empty.
13350 In a simple-declaration, the optional init-declarator-list can be
13351 omitted only when declaring a class or enumeration, that is when
13352 the decl-specifier-seq contains either a class-specifier, an
13353 elaborated-type-specifier, or an enum-specifier. */
13354 cp_parser_decl_specifier_seq (parser
,
13355 CP_PARSER_FLAGS_OPTIONAL
,
13357 &declares_class_or_enum
);
13358 /* We no longer need to defer access checks. */
13359 stop_deferring_access_checks ();
13361 /* In a block scope, a valid declaration must always have a
13362 decl-specifier-seq. By not trying to parse declarators, we can
13363 resolve the declaration/expression ambiguity more quickly. */
13364 if (!function_definition_allowed_p
13365 && !decl_specifiers
.any_specifiers_p
)
13367 cp_parser_error (parser
, "expected declaration");
13371 /* If the next two tokens are both identifiers, the code is
13372 erroneous. The usual cause of this situation is code like:
13376 where "T" should name a type -- but does not. */
13377 if (!decl_specifiers
.any_type_specifiers_p
13378 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13380 /* If parsing tentatively, we should commit; we really are
13381 looking at a declaration. */
13382 cp_parser_commit_to_tentative_parse (parser
);
13387 cp_parser_maybe_commit_to_declaration (parser
,
13388 decl_specifiers
.any_specifiers_p
);
13390 /* Look for C++17 decomposition declaration. */
13391 for (size_t n
= 1; ; n
++)
13392 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13393 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13395 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13396 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13397 && decl_specifiers
.any_specifiers_p
)
13400 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13401 maybe_range_for_decl
,
13404 /* The next token should be either a `,' or a `;'. */
13405 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13406 /* If it's a `;', we are done. */
13407 if (token
->type
== CPP_SEMICOLON
)
13409 else if (maybe_range_for_decl
)
13411 if (*maybe_range_for_decl
== NULL_TREE
)
13412 *maybe_range_for_decl
= error_mark_node
;
13415 /* Anything else is an error. */
13418 /* If we have already issued an error message we don't need
13419 to issue another one. */
13420 if ((decl
!= error_mark_node
13421 && DECL_INITIAL (decl
) != error_mark_node
)
13422 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13423 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13424 /* Skip tokens until we reach the end of the statement. */
13425 cp_parser_skip_to_end_of_statement (parser
);
13426 /* If the next token is now a `;', consume it. */
13427 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13428 cp_lexer_consume_token (parser
->lexer
);
13436 bool auto_specifier_p
;
13437 /* NULL_TREE if both variable and function declaration are allowed,
13438 error_mark_node if function declaration are not allowed and
13439 a FUNCTION_DECL that should be diagnosed if it is followed by
13440 variable declarations. */
13441 tree auto_function_declaration
;
13443 last_type
= NULL_TREE
;
13445 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13446 auto_function_declaration
= NULL_TREE
;
13448 /* Keep going until we hit the `;' at the end of the simple
13450 saw_declarator
= false;
13451 while (cp_lexer_next_token_is_not (parser
->lexer
,
13455 bool function_definition_p
;
13457 tree auto_result
= NULL_TREE
;
13459 if (saw_declarator
)
13461 /* If we are processing next declarator, comma is expected */
13462 token
= cp_lexer_peek_token (parser
->lexer
);
13463 gcc_assert (token
->type
== CPP_COMMA
);
13464 cp_lexer_consume_token (parser
->lexer
);
13465 if (maybe_range_for_decl
)
13467 *maybe_range_for_decl
= error_mark_node
;
13468 if (comma_loc
== UNKNOWN_LOCATION
)
13469 comma_loc
= token
->location
;
13473 saw_declarator
= true;
13475 /* Parse the init-declarator. */
13476 decl
= cp_parser_init_declarator (parser
,
13477 CP_PARSER_FLAGS_NONE
,
13480 function_definition_allowed_p
,
13481 /*member_p=*/false,
13482 declares_class_or_enum
,
13483 &function_definition_p
,
13484 maybe_range_for_decl
,
13487 /* If an error occurred while parsing tentatively, exit quickly.
13488 (That usually happens when in the body of a function; each
13489 statement is treated as a declaration-statement until proven
13491 if (cp_parser_error_occurred (parser
))
13494 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13496 /* If the init-declarator-list contains more than one
13497 init-declarator, they shall all form declarations of
13499 if (auto_function_declaration
== NULL_TREE
)
13500 auto_function_declaration
13501 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13502 else if (TREE_CODE (decl
) == FUNCTION_DECL
13503 || auto_function_declaration
!= error_mark_node
)
13505 error_at (decl_specifiers
.locations
[ds_type_spec
],
13506 "non-variable %qD in declaration with more than one "
13507 "declarator with placeholder type",
13508 TREE_CODE (decl
) == FUNCTION_DECL
13509 ? decl
: auto_function_declaration
);
13510 auto_function_declaration
= error_mark_node
;
13515 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13518 && last_type
!= error_mark_node
13519 && !same_type_p (auto_result
, last_type
))
13521 /* If the list of declarators contains more than one declarator,
13522 the type of each declared variable is determined as described
13523 above. If the type deduced for the template parameter U is not
13524 the same in each deduction, the program is ill-formed. */
13525 error_at (decl_specifiers
.locations
[ds_type_spec
],
13526 "inconsistent deduction for %qT: %qT and then %qT",
13527 decl_specifiers
.type
, last_type
, auto_result
);
13528 last_type
= error_mark_node
;
13531 last_type
= auto_result
;
13534 /* Handle function definitions specially. */
13535 if (function_definition_p
)
13537 /* If the next token is a `,', then we are probably
13538 processing something like:
13542 which is erroneous. */
13543 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13545 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13546 error_at (token
->location
,
13548 " declarations and function-definitions is forbidden");
13550 /* Otherwise, we're done with the list of declarators. */
13553 pop_deferring_access_checks ();
13557 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13558 *maybe_range_for_decl
= decl
;
13559 /* The next token should be either a `,' or a `;'. */
13560 token
= cp_lexer_peek_token (parser
->lexer
);
13561 /* If it's a `,', there are more declarators to come. */
13562 if (token
->type
== CPP_COMMA
)
13563 /* will be consumed next time around */;
13564 /* If it's a `;', we are done. */
13565 else if (token
->type
== CPP_SEMICOLON
)
13567 else if (maybe_range_for_decl
)
13569 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13570 permerror (decl_specifiers
.locations
[ds_type_spec
],
13571 "types may not be defined in a for-range-declaration");
13574 /* Anything else is an error. */
13577 /* If we have already issued an error message we don't need
13578 to issue another one. */
13579 if ((decl
!= error_mark_node
13580 && DECL_INITIAL (decl
) != error_mark_node
)
13581 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13582 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13583 /* Skip tokens until we reach the end of the statement. */
13584 cp_parser_skip_to_end_of_statement (parser
);
13585 /* If the next token is now a `;', consume it. */
13586 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13587 cp_lexer_consume_token (parser
->lexer
);
13590 /* After the first time around, a function-definition is not
13591 allowed -- even if it was OK at first. For example:
13596 function_definition_allowed_p
= false;
13599 /* Issue an error message if no declarators are present, and the
13600 decl-specifier-seq does not itself declare a class or
13601 enumeration: [dcl.dcl]/3. */
13602 if (!saw_declarator
)
13604 if (cp_parser_declares_only_class_p (parser
))
13606 if (!declares_class_or_enum
13607 && decl_specifiers
.type
13608 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13609 /* Ensure an error is issued anyway when finish_decltype_type,
13610 called via cp_parser_decl_specifier_seq, returns a class or
13611 an enumeration (c++/51786). */
13612 decl_specifiers
.type
= NULL_TREE
;
13613 shadow_tag (&decl_specifiers
);
13615 /* Perform any deferred access checks. */
13616 perform_deferred_access_checks (tf_warning_or_error
);
13619 /* Consume the `;'. */
13621 if (!maybe_range_for_decl
)
13622 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13623 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13625 if (init_loc
!= UNKNOWN_LOCATION
)
13626 error_at (init_loc
, "initializer in range-based %<for%> loop");
13627 if (comma_loc
!= UNKNOWN_LOCATION
)
13628 error_at (comma_loc
,
13629 "multiple declarations in range-based %<for%> loop");
13633 pop_deferring_access_checks ();
13636 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13637 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13641 cp_parser_decomposition_declaration (cp_parser
*parser
,
13642 cp_decl_specifier_seq
*decl_specifiers
,
13643 tree
*maybe_range_for_decl
,
13644 location_t
*init_loc
)
13646 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13647 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13648 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13650 /* Parse the identifier-list. */
13651 auto_vec
<cp_expr
, 10> v
;
13652 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13655 cp_expr e
= cp_parser_identifier (parser
);
13656 if (e
.get_value () == error_mark_node
)
13659 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13661 cp_lexer_consume_token (parser
->lexer
);
13664 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13665 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13667 end_loc
= UNKNOWN_LOCATION
;
13668 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13671 cp_lexer_consume_token (parser
->lexer
);
13674 cp_parser_skip_to_end_of_statement (parser
);
13675 return error_mark_node
;
13679 if (cxx_dialect
< cxx17
)
13680 pedwarn (loc
, 0, "structured bindings only available with "
13681 "-std=c++17 or -std=gnu++17");
13684 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13685 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13686 declarator
->id_loc
= loc
;
13687 if (ref_qual
!= REF_QUAL_NONE
)
13688 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13689 ref_qual
== REF_QUAL_RVALUE
,
13691 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13692 NULL_TREE
, decl_specifiers
->attributes
,
13694 tree orig_decl
= decl
;
13698 cp_decl_specifier_seq decl_specs
;
13699 clear_decl_specs (&decl_specs
);
13700 decl_specs
.type
= make_auto ();
13702 FOR_EACH_VEC_ELT (v
, i
, e
)
13705 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
13706 sfk_none
, e
.get_location ());
13709 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13710 declarator
->id_loc
= e
.get_location ();
13712 tree elt_pushed_scope
;
13713 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13714 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13715 if (decl2
== error_mark_node
)
13716 decl
= error_mark_node
;
13717 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13719 /* Ensure we've diagnosed redeclaration if we aren't creating
13721 gcc_assert (errorcount
);
13722 decl
= error_mark_node
;
13726 if (elt_pushed_scope
)
13727 pop_scope (elt_pushed_scope
);
13732 error_at (loc
, "empty structured binding declaration");
13733 decl
= error_mark_node
;
13736 if (maybe_range_for_decl
== NULL
13737 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13739 bool non_constant_p
= false, is_direct_init
= false;
13740 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13741 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13743 if (initializer
== NULL_TREE
13744 || (TREE_CODE (initializer
) == TREE_LIST
13745 && TREE_CHAIN (initializer
))
13747 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13748 && CONSTRUCTOR_NELTS (initializer
) != 1))
13750 error_at (loc
, "invalid initializer for structured binding "
13752 initializer
= error_mark_node
;
13755 if (decl
!= error_mark_node
)
13757 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13758 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13759 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13760 cp_finish_decomp (decl
, prev
, v
.length ());
13763 else if (decl
!= error_mark_node
)
13765 *maybe_range_for_decl
= prev
;
13766 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13767 the underlying DECL. */
13768 cp_finish_decomp (decl
, prev
, v
.length ());
13772 pop_scope (pushed_scope
);
13774 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13776 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13777 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13783 /* Parse a decl-specifier-seq.
13785 decl-specifier-seq:
13786 decl-specifier-seq [opt] decl-specifier
13787 decl-specifier attribute-specifier-seq [opt] (C++11)
13790 storage-class-specifier
13801 Concepts Extension:
13806 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13808 The parser flags FLAGS is used to control type-specifier parsing.
13810 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13813 1: one of the decl-specifiers is an elaborated-type-specifier
13814 (i.e., a type declaration)
13815 2: one of the decl-specifiers is an enum-specifier or a
13816 class-specifier (i.e., a type definition)
13821 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13822 cp_parser_flags flags
,
13823 cp_decl_specifier_seq
*decl_specs
,
13824 int* declares_class_or_enum
)
13826 bool constructor_possible_p
= !parser
->in_declarator_p
;
13827 bool found_decl_spec
= false;
13828 cp_token
*start_token
= NULL
;
13831 /* Clear DECL_SPECS. */
13832 clear_decl_specs (decl_specs
);
13834 /* Assume no class or enumeration type is declared. */
13835 *declares_class_or_enum
= 0;
13837 /* Keep reading specifiers until there are no more to read. */
13840 bool constructor_p
;
13844 /* Peek at the next token. */
13845 token
= cp_lexer_peek_token (parser
->lexer
);
13847 /* Save the first token of the decl spec list for error
13850 start_token
= token
;
13851 /* Handle attributes. */
13852 if (cp_next_tokens_can_be_attribute_p (parser
))
13854 /* Parse the attributes. */
13855 tree attrs
= cp_parser_attributes_opt (parser
);
13857 /* In a sequence of declaration specifiers, c++11 attributes
13858 appertain to the type that precede them. In that case
13861 The attribute-specifier-seq affects the type only for
13862 the declaration it appears in, not other declarations
13863 involving the same type.
13865 But for now let's force the user to position the
13866 attribute either at the beginning of the declaration or
13867 after the declarator-id, which would clearly mean that it
13868 applies to the declarator. */
13869 if (cxx11_attribute_p (attrs
))
13871 if (!found_decl_spec
)
13872 /* The c++11 attribute is at the beginning of the
13873 declaration. It appertains to the entity being
13877 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13879 /* This is an attribute following a
13880 class-specifier. */
13881 if (decl_specs
->type_definition_p
)
13882 warn_misplaced_attr_for_class_type (token
->location
,
13888 decl_specs
->std_attributes
13889 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13890 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13891 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13897 decl_specs
->attributes
13898 = attr_chainon (decl_specs
->attributes
, attrs
);
13899 if (decl_specs
->locations
[ds_attribute
] == 0)
13900 decl_specs
->locations
[ds_attribute
] = token
->location
;
13903 /* Assume we will find a decl-specifier keyword. */
13904 found_decl_spec
= true;
13905 /* If the next token is an appropriate keyword, we can simply
13906 add it to the list. */
13907 switch (token
->keyword
)
13913 if (!at_class_scope_p ())
13915 gcc_rich_location
richloc (token
->location
);
13916 richloc
.add_fixit_remove ();
13917 error_at (&richloc
, "%<friend%> used outside of class");
13918 cp_lexer_purge_token (parser
->lexer
);
13923 /* Consume the token. */
13924 cp_lexer_consume_token (parser
->lexer
);
13928 case RID_CONSTEXPR
:
13930 cp_lexer_consume_token (parser
->lexer
);
13935 cp_lexer_consume_token (parser
->lexer
);
13938 /* function-specifier:
13945 cp_parser_function_specifier_opt (parser
, decl_specs
);
13952 /* Consume the token. */
13953 cp_lexer_consume_token (parser
->lexer
);
13954 /* A constructor declarator cannot appear in a typedef. */
13955 constructor_possible_p
= false;
13956 /* The "typedef" keyword can only occur in a declaration; we
13957 may as well commit at this point. */
13958 cp_parser_commit_to_tentative_parse (parser
);
13960 if (decl_specs
->storage_class
!= sc_none
)
13961 decl_specs
->conflicting_specifiers_p
= true;
13964 /* storage-class-specifier:
13974 if (cxx_dialect
== cxx98
)
13976 /* Consume the token. */
13977 cp_lexer_consume_token (parser
->lexer
);
13979 /* Complain about `auto' as a storage specifier, if
13980 we're complaining about C++0x compatibility. */
13981 gcc_rich_location
richloc (token
->location
);
13982 richloc
.add_fixit_remove ();
13983 warning_at (&richloc
, OPT_Wc__11_compat
,
13984 "%<auto%> changes meaning in C++11; "
13985 "please remove it");
13987 /* Set the storage class anyway. */
13988 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13992 /* C++0x auto type-specifier. */
13993 found_decl_spec
= false;
14000 /* Consume the token. */
14001 cp_lexer_consume_token (parser
->lexer
);
14002 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
14006 /* Consume the token. */
14008 cp_lexer_consume_token (parser
->lexer
);
14012 /* We did not yet find a decl-specifier yet. */
14013 found_decl_spec
= false;
14017 if (found_decl_spec
14018 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
14019 && token
->keyword
!= RID_CONSTEXPR
)
14020 error ("decl-specifier invalid in condition");
14022 if (found_decl_spec
14023 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14024 && token
->keyword
!= RID_MUTABLE
14025 && token
->keyword
!= RID_CONSTEXPR
)
14026 error_at (token
->location
, "%qD invalid in lambda",
14027 ridpointers
[token
->keyword
]);
14030 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14032 /* Constructors are a special case. The `S' in `S()' is not a
14033 decl-specifier; it is the beginning of the declarator. */
14035 = (!found_decl_spec
14036 && constructor_possible_p
14037 && (cp_parser_constructor_declarator_p
14038 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
14040 /* If we don't have a DECL_SPEC yet, then we must be looking at
14041 a type-specifier. */
14042 if (!found_decl_spec
&& !constructor_p
)
14044 int decl_spec_declares_class_or_enum
;
14045 bool is_cv_qualifier
;
14049 = cp_parser_type_specifier (parser
, flags
,
14051 /*is_declaration=*/true,
14052 &decl_spec_declares_class_or_enum
,
14054 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
14056 /* If this type-specifier referenced a user-defined type
14057 (a typedef, class-name, etc.), then we can't allow any
14058 more such type-specifiers henceforth.
14062 The longest sequence of decl-specifiers that could
14063 possibly be a type name is taken as the
14064 decl-specifier-seq of a declaration. The sequence shall
14065 be self-consistent as described below.
14069 As a general rule, at most one type-specifier is allowed
14070 in the complete decl-specifier-seq of a declaration. The
14071 only exceptions are the following:
14073 -- const or volatile can be combined with any other
14076 -- signed or unsigned can be combined with char, long,
14084 void g (const int Pc);
14086 Here, Pc is *not* part of the decl-specifier seq; it's
14087 the declarator. Therefore, once we see a type-specifier
14088 (other than a cv-qualifier), we forbid any additional
14089 user-defined types. We *do* still allow things like `int
14090 int' to be considered a decl-specifier-seq, and issue the
14091 error message later. */
14092 if (type_spec
&& !is_cv_qualifier
)
14093 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
14094 /* A constructor declarator cannot follow a type-specifier. */
14097 constructor_possible_p
= false;
14098 found_decl_spec
= true;
14099 if (!is_cv_qualifier
)
14100 decl_specs
->any_type_specifiers_p
= true;
14102 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
14103 error_at (token
->location
, "type-specifier invalid in lambda");
14107 /* If we still do not have a DECL_SPEC, then there are no more
14108 decl-specifiers. */
14109 if (!found_decl_spec
)
14112 decl_specs
->any_specifiers_p
= true;
14113 /* After we see one decl-specifier, further decl-specifiers are
14114 always optional. */
14115 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
14118 /* Don't allow a friend specifier with a class definition. */
14119 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
14120 && (*declares_class_or_enum
& 2))
14121 error_at (decl_specs
->locations
[ds_friend
],
14122 "class definition may not be declared a friend");
14125 /* Parse an (optional) storage-class-specifier.
14127 storage-class-specifier:
14136 storage-class-specifier:
14139 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14142 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
14144 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
14147 if (cxx_dialect
!= cxx98
)
14149 /* Fall through for C++98. */
14150 gcc_fallthrough ();
14157 /* Consume the token. */
14158 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14165 /* Parse an (optional) function-specifier.
14167 function-specifier:
14173 explicit(constant-expression)
14175 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14176 Updates DECL_SPECS, if it is non-NULL. */
14179 cp_parser_function_specifier_opt (cp_parser
* parser
,
14180 cp_decl_specifier_seq
*decl_specs
)
14182 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14183 switch (token
->keyword
)
14186 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
14190 /* 14.5.2.3 [temp.mem]
14192 A member function template shall not be virtual. */
14193 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14194 && current_class_type
)
14195 error_at (token
->location
, "templates may not be %<virtual%>");
14197 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
14202 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14203 /* If we see '(', it's C++20 explicit(bool). */
14205 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14207 matching_parens parens
;
14208 parens
.consume_open (parser
);
14210 /* New types are not allowed in an explicit-specifier. */
14211 const char *saved_message
14212 = parser
->type_definition_forbidden_message
;
14213 parser
->type_definition_forbidden_message
14214 = G_("types may not be defined in explicit-specifier");
14216 if (cxx_dialect
< cxx2a
)
14217 pedwarn (token
->location
, 0,
14218 "%<explicit(bool)%> only available with -std=c++2a "
14219 "or -std=gnu++2a");
14221 /* Parse the constant-expression. */
14222 expr
= cp_parser_constant_expression (parser
);
14224 /* Restore the saved message. */
14225 parser
->type_definition_forbidden_message
= saved_message
;
14226 parens
.require_close (parser
);
14229 /* The explicit-specifier explicit without a constant-expression is
14230 equivalent to the explicit-specifier explicit(true). */
14231 expr
= boolean_true_node
;
14234 "the constant-expression, if supplied, shall be a contextually
14235 converted constant expression of type bool." */
14236 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14237 /* We could evaluate it -- mark the decl as appropriate. */
14238 if (expr
== boolean_true_node
)
14239 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14240 else if (expr
== boolean_false_node
)
14241 /* Don't mark the decl as explicit. */;
14242 else if (decl_specs
)
14243 /* The expression was value-dependent. Remember it so that we can
14244 substitute it later. */
14245 decl_specs
->explicit_specifier
= expr
;
14253 /* Consume the token. */
14254 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14257 /* Parse a linkage-specification.
14259 linkage-specification:
14260 extern string-literal { declaration-seq [opt] }
14261 extern string-literal declaration */
14264 cp_parser_linkage_specification (cp_parser
* parser
)
14268 /* Look for the `extern' keyword. */
14269 cp_token
*extern_token
14270 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14272 /* Look for the string-literal. */
14273 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14274 linkage
= cp_parser_string_literal (parser
, false, false);
14276 /* Transform the literal into an identifier. If the literal is a
14277 wide-character string, or contains embedded NULs, then we can't
14278 handle it as the user wants. */
14279 if (strlen (TREE_STRING_POINTER (linkage
))
14280 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14282 cp_parser_error (parser
, "invalid linkage-specification");
14283 /* Assume C++ linkage. */
14284 linkage
= lang_name_cplusplus
;
14287 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14289 /* We're now using the new linkage. */
14290 push_lang_context (linkage
);
14292 /* Preserve the location of the the innermost linkage specification,
14293 tracking the locations of nested specifications via a local. */
14294 location_t saved_location
14295 = parser
->innermost_linkage_specification_location
;
14296 /* Construct a location ranging from the start of the "extern" to
14297 the end of the string-literal, with the caret at the start, e.g.:
14301 parser
->innermost_linkage_specification_location
14302 = make_location (extern_token
->location
,
14303 extern_token
->location
,
14304 get_finish (string_token
->location
));
14306 /* If the next token is a `{', then we're using the first
14308 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14310 cp_ensure_no_omp_declare_simd (parser
);
14311 cp_ensure_no_oacc_routine (parser
);
14313 /* Consume the `{' token. */
14314 matching_braces braces
;
14315 braces
.consume_open (parser
);
14316 /* Parse the declarations. */
14317 cp_parser_declaration_seq_opt (parser
);
14318 /* Look for the closing `}'. */
14319 braces
.require_close (parser
);
14321 /* Otherwise, there's just one declaration. */
14324 bool saved_in_unbraced_linkage_specification_p
;
14326 saved_in_unbraced_linkage_specification_p
14327 = parser
->in_unbraced_linkage_specification_p
;
14328 parser
->in_unbraced_linkage_specification_p
= true;
14329 cp_parser_declaration (parser
);
14330 parser
->in_unbraced_linkage_specification_p
14331 = saved_in_unbraced_linkage_specification_p
;
14334 /* We're done with the linkage-specification. */
14335 pop_lang_context ();
14337 /* Restore location of parent linkage specification, if any. */
14338 parser
->innermost_linkage_specification_location
= saved_location
;
14341 /* Parse a static_assert-declaration.
14343 static_assert-declaration:
14344 static_assert ( constant-expression , string-literal ) ;
14345 static_assert ( constant-expression ) ; (C++17)
14347 If MEMBER_P, this static_assert is a class member. */
14350 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14353 location_t token_loc
;
14357 /* Peek at the `static_assert' token so we can keep track of exactly
14358 where the static assertion started. */
14359 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14361 /* Look for the `static_assert' keyword. */
14362 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14366 /* We know we are in a static assertion; commit to any tentative
14368 if (cp_parser_parsing_tentatively (parser
))
14369 cp_parser_commit_to_tentative_parse (parser
);
14371 /* Parse the `(' starting the static assertion condition. */
14372 matching_parens parens
;
14373 parens
.require_open (parser
);
14375 /* Parse the constant-expression. Allow a non-constant expression
14376 here in order to give better diagnostics in finish_static_assert. */
14378 cp_parser_constant_expression (parser
,
14379 /*allow_non_constant_p=*/true,
14380 /*non_constant_p=*/&dummy
);
14382 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14384 if (cxx_dialect
< cxx17
)
14385 pedwarn (input_location
, OPT_Wpedantic
,
14386 "static_assert without a message "
14387 "only available with -std=c++17 or -std=gnu++17");
14389 cp_lexer_consume_token (parser
->lexer
);
14390 message
= build_string (1, "");
14391 TREE_TYPE (message
) = char_array_type_node
;
14392 fix_string_type (message
);
14396 /* Parse the separating `,'. */
14397 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14399 /* Parse the string-literal message. */
14400 message
= cp_parser_string_literal (parser
,
14401 /*translate=*/false,
14404 /* A `)' completes the static assertion. */
14405 if (!parens
.require_close (parser
))
14406 cp_parser_skip_to_closing_parenthesis (parser
,
14407 /*recovering=*/true,
14408 /*or_comma=*/false,
14409 /*consume_paren=*/true);
14412 /* A semicolon terminates the declaration. */
14413 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14415 /* Get the location for the static assertion. Use that of the
14416 condition if available, otherwise, use that of the "static_assert"
14418 location_t assert_loc
= condition
.get_location ();
14419 if (assert_loc
== UNKNOWN_LOCATION
)
14420 assert_loc
= token_loc
;
14422 /* Complete the static assertion, which may mean either processing
14423 the static assert now or saving it for template instantiation. */
14424 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14427 /* Parse the expression in decltype ( expression ). */
14430 cp_parser_decltype_expr (cp_parser
*parser
,
14431 bool &id_expression_or_member_access_p
)
14433 cp_token
*id_expr_start_token
;
14436 /* Since we're going to preserve any side-effects from this parse, set up a
14437 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14438 in the expression. */
14439 tentative_firewall
firewall (parser
);
14441 /* First, try parsing an id-expression. */
14442 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14443 cp_parser_parse_tentatively (parser
);
14444 expr
= cp_parser_id_expression (parser
,
14445 /*template_keyword_p=*/false,
14446 /*check_dependency_p=*/true,
14447 /*template_p=*/NULL
,
14448 /*declarator_p=*/false,
14449 /*optional_p=*/false);
14451 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14453 bool non_integral_constant_expression_p
= false;
14454 tree id_expression
= expr
;
14456 const char *error_msg
;
14458 if (identifier_p (expr
))
14459 /* Lookup the name we got back from the id-expression. */
14460 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14461 id_expr_start_token
->location
);
14463 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14464 /* A template without args is not a complete id-expression. */
14465 expr
= error_mark_node
;
14468 && expr
!= error_mark_node
14469 && TREE_CODE (expr
) != TYPE_DECL
14470 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14471 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14472 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14474 /* Complete lookup of the id-expression. */
14475 expr
= (finish_id_expression
14476 (id_expression
, expr
, parser
->scope
, &idk
,
14477 /*integral_constant_expression_p=*/false,
14478 /*allow_non_integral_constant_expression_p=*/true,
14479 &non_integral_constant_expression_p
,
14480 /*template_p=*/false,
14482 /*address_p=*/false,
14483 /*template_arg_p=*/false,
14485 id_expr_start_token
->location
));
14487 if (expr
== error_mark_node
)
14488 /* We found an id-expression, but it was something that we
14489 should not have found. This is an error, not something
14490 we can recover from, so note that we found an
14491 id-expression and we'll recover as gracefully as
14493 id_expression_or_member_access_p
= true;
14497 && expr
!= error_mark_node
14498 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14499 /* We have an id-expression. */
14500 id_expression_or_member_access_p
= true;
14503 if (!id_expression_or_member_access_p
)
14505 /* Abort the id-expression parse. */
14506 cp_parser_abort_tentative_parse (parser
);
14508 /* Parsing tentatively, again. */
14509 cp_parser_parse_tentatively (parser
);
14511 /* Parse a class member access. */
14512 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14513 /*cast_p=*/false, /*decltype*/true,
14514 /*member_access_only_p=*/true, NULL
);
14517 && expr
!= error_mark_node
14518 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14519 /* We have an id-expression. */
14520 id_expression_or_member_access_p
= true;
14523 if (id_expression_or_member_access_p
)
14524 /* We have parsed the complete id-expression or member access. */
14525 cp_parser_parse_definitely (parser
);
14528 /* Abort our attempt to parse an id-expression or member access
14530 cp_parser_abort_tentative_parse (parser
);
14532 /* Commit to the tentative_firewall so we get syntax errors. */
14533 cp_parser_commit_to_tentative_parse (parser
);
14535 /* Parse a full expression. */
14536 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14537 /*decltype_p=*/true);
14543 /* Parse a `decltype' type. Returns the type.
14545 simple-type-specifier:
14546 decltype ( expression )
14548 decltype ( auto ) */
14551 cp_parser_decltype (cp_parser
*parser
)
14553 bool id_expression_or_member_access_p
= false;
14554 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14556 if (start_token
->type
== CPP_DECLTYPE
)
14558 /* Already parsed. */
14559 cp_lexer_consume_token (parser
->lexer
);
14560 return saved_checks_value (start_token
->u
.tree_check_value
);
14563 /* Look for the `decltype' token. */
14564 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14565 return error_mark_node
;
14567 /* Parse the opening `('. */
14568 matching_parens parens
;
14569 if (!parens
.require_open (parser
))
14570 return error_mark_node
;
14572 push_deferring_access_checks (dk_deferred
);
14574 tree expr
= NULL_TREE
;
14576 if (cxx_dialect
>= cxx14
14577 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14578 /* decltype (auto) */
14579 cp_lexer_consume_token (parser
->lexer
);
14582 /* decltype (expression) */
14584 /* Types cannot be defined in a `decltype' expression. Save away the
14585 old message and set the new one. */
14586 const char *saved_message
= parser
->type_definition_forbidden_message
;
14587 parser
->type_definition_forbidden_message
14588 = G_("types may not be defined in %<decltype%> expressions");
14590 /* The restrictions on constant-expressions do not apply inside
14591 decltype expressions. */
14592 bool saved_integral_constant_expression_p
14593 = parser
->integral_constant_expression_p
;
14594 bool saved_non_integral_constant_expression_p
14595 = parser
->non_integral_constant_expression_p
;
14596 parser
->integral_constant_expression_p
= false;
14598 /* Within a parenthesized expression, a `>' token is always
14599 the greater-than operator. */
14600 bool saved_greater_than_is_operator_p
14601 = parser
->greater_than_is_operator_p
;
14602 parser
->greater_than_is_operator_p
= true;
14604 /* Do not actually evaluate the expression. */
14605 ++cp_unevaluated_operand
;
14607 /* Do not warn about problems with the expression. */
14608 ++c_inhibit_evaluation_warnings
;
14610 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14611 STRIP_ANY_LOCATION_WRAPPER (expr
);
14613 /* Go back to evaluating expressions. */
14614 --cp_unevaluated_operand
;
14615 --c_inhibit_evaluation_warnings
;
14617 /* The `>' token might be the end of a template-id or
14618 template-parameter-list now. */
14619 parser
->greater_than_is_operator_p
14620 = saved_greater_than_is_operator_p
;
14622 /* Restore the old message and the integral constant expression
14624 parser
->type_definition_forbidden_message
= saved_message
;
14625 parser
->integral_constant_expression_p
14626 = saved_integral_constant_expression_p
;
14627 parser
->non_integral_constant_expression_p
14628 = saved_non_integral_constant_expression_p
;
14631 /* Parse to the closing `)'. */
14632 if (!parens
.require_close (parser
))
14634 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14635 /*consume_paren=*/true);
14636 pop_deferring_access_checks ();
14637 return error_mark_node
;
14643 expr
= make_decltype_auto ();
14644 AUTO_IS_DECLTYPE (expr
) = true;
14647 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14648 tf_warning_or_error
);
14650 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14652 start_token
->type
= CPP_DECLTYPE
;
14653 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14654 start_token
->u
.tree_check_value
->value
= expr
;
14655 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14656 start_token
->keyword
= RID_MAX
;
14657 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14659 pop_to_parent_deferring_access_checks ();
14664 /* Special member functions [gram.special] */
14666 /* Parse a conversion-function-id.
14668 conversion-function-id:
14669 operator conversion-type-id
14671 Returns an IDENTIFIER_NODE representing the operator. */
14674 cp_parser_conversion_function_id (cp_parser
* parser
)
14678 tree saved_qualifying_scope
;
14679 tree saved_object_scope
;
14680 tree pushed_scope
= NULL_TREE
;
14682 /* Look for the `operator' token. */
14683 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14684 return error_mark_node
;
14685 /* When we parse the conversion-type-id, the current scope will be
14686 reset. However, we need that information in able to look up the
14687 conversion function later, so we save it here. */
14688 saved_scope
= parser
->scope
;
14689 saved_qualifying_scope
= parser
->qualifying_scope
;
14690 saved_object_scope
= parser
->object_scope
;
14691 /* We must enter the scope of the class so that the names of
14692 entities declared within the class are available in the
14693 conversion-type-id. For example, consider:
14700 S::operator I() { ... }
14702 In order to see that `I' is a type-name in the definition, we
14703 must be in the scope of `S'. */
14705 pushed_scope
= push_scope (saved_scope
);
14706 /* Parse the conversion-type-id. */
14707 type
= cp_parser_conversion_type_id (parser
);
14708 /* Leave the scope of the class, if any. */
14710 pop_scope (pushed_scope
);
14711 /* Restore the saved scope. */
14712 parser
->scope
= saved_scope
;
14713 parser
->qualifying_scope
= saved_qualifying_scope
;
14714 parser
->object_scope
= saved_object_scope
;
14715 /* If the TYPE is invalid, indicate failure. */
14716 if (type
== error_mark_node
)
14717 return error_mark_node
;
14718 return make_conv_op_name (type
);
14721 /* Parse a conversion-type-id:
14723 conversion-type-id:
14724 type-specifier-seq conversion-declarator [opt]
14726 Returns the TYPE specified. */
14729 cp_parser_conversion_type_id (cp_parser
* parser
)
14732 cp_decl_specifier_seq type_specifiers
;
14733 cp_declarator
*declarator
;
14734 tree type_specified
;
14735 const char *saved_message
;
14737 /* Parse the attributes. */
14738 attributes
= cp_parser_attributes_opt (parser
);
14740 saved_message
= parser
->type_definition_forbidden_message
;
14741 parser
->type_definition_forbidden_message
14742 = G_("types may not be defined in a conversion-type-id");
14744 /* Parse the type-specifiers. */
14745 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
14746 /*is_declaration=*/false,
14747 /*is_trailing_return=*/false,
14750 parser
->type_definition_forbidden_message
= saved_message
;
14752 /* If that didn't work, stop. */
14753 if (type_specifiers
.type
== error_mark_node
)
14754 return error_mark_node
;
14755 /* Parse the conversion-declarator. */
14756 declarator
= cp_parser_conversion_declarator_opt (parser
);
14758 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14759 /*initialized=*/0, &attributes
);
14761 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14763 /* Don't give this error when parsing tentatively. This happens to
14764 work because we always parse this definitively once. */
14765 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14766 && type_uses_auto (type_specified
))
14768 if (cxx_dialect
< cxx14
)
14770 error ("invalid use of %<auto%> in conversion operator");
14771 return error_mark_node
;
14773 else if (template_parm_scope_p ())
14774 warning (0, "use of %<auto%> in member template "
14775 "conversion operator can never be deduced");
14778 return type_specified
;
14781 /* Parse an (optional) conversion-declarator.
14783 conversion-declarator:
14784 ptr-operator conversion-declarator [opt]
14788 static cp_declarator
*
14789 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14791 enum tree_code code
;
14792 tree class_type
, std_attributes
= NULL_TREE
;
14793 cp_cv_quals cv_quals
;
14795 /* We don't know if there's a ptr-operator next, or not. */
14796 cp_parser_parse_tentatively (parser
);
14797 /* Try the ptr-operator. */
14798 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14800 /* If it worked, look for more conversion-declarators. */
14801 if (cp_parser_parse_definitely (parser
))
14803 cp_declarator
*declarator
;
14805 /* Parse another optional declarator. */
14806 declarator
= cp_parser_conversion_declarator_opt (parser
);
14808 declarator
= cp_parser_make_indirect_declarator
14809 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14817 /* Parse an (optional) ctor-initializer.
14820 : mem-initializer-list */
14823 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14825 /* If the next token is not a `:', then there is no
14826 ctor-initializer. */
14827 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14829 /* Do default initialization of any bases and members. */
14830 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14831 finish_mem_initializers (NULL_TREE
);
14835 /* Consume the `:' token. */
14836 cp_lexer_consume_token (parser
->lexer
);
14837 /* And the mem-initializer-list. */
14838 cp_parser_mem_initializer_list (parser
);
14841 /* Parse a mem-initializer-list.
14843 mem-initializer-list:
14844 mem-initializer ... [opt]
14845 mem-initializer ... [opt] , mem-initializer-list */
14848 cp_parser_mem_initializer_list (cp_parser
* parser
)
14850 tree mem_initializer_list
= NULL_TREE
;
14851 tree target_ctor
= error_mark_node
;
14852 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14854 /* Let the semantic analysis code know that we are starting the
14855 mem-initializer-list. */
14856 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14857 error_at (token
->location
,
14858 "only constructors take member initializers");
14860 /* Loop through the list. */
14863 tree mem_initializer
;
14865 token
= cp_lexer_peek_token (parser
->lexer
);
14866 /* Parse the mem-initializer. */
14867 mem_initializer
= cp_parser_mem_initializer (parser
);
14868 /* If the next token is a `...', we're expanding member initializers. */
14869 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14871 || (mem_initializer
!= error_mark_node
14872 && check_for_bare_parameter_packs (TREE_PURPOSE
14873 (mem_initializer
))))
14875 /* Consume the `...'. */
14877 cp_lexer_consume_token (parser
->lexer
);
14879 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14880 can be expanded but members cannot. */
14881 if (mem_initializer
!= error_mark_node
14882 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14884 error_at (token
->location
,
14885 "cannot expand initializer for member %qD",
14886 TREE_PURPOSE (mem_initializer
));
14887 mem_initializer
= error_mark_node
;
14890 /* Construct the pack expansion type. */
14891 if (mem_initializer
!= error_mark_node
)
14892 mem_initializer
= make_pack_expansion (mem_initializer
);
14894 if (target_ctor
!= error_mark_node
14895 && mem_initializer
!= error_mark_node
)
14897 error ("mem-initializer for %qD follows constructor delegation",
14898 TREE_PURPOSE (mem_initializer
));
14899 mem_initializer
= error_mark_node
;
14901 /* Look for a target constructor. */
14902 if (mem_initializer
!= error_mark_node
14903 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14904 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14906 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14907 if (mem_initializer_list
)
14909 error ("constructor delegation follows mem-initializer for %qD",
14910 TREE_PURPOSE (mem_initializer_list
));
14911 mem_initializer
= error_mark_node
;
14913 target_ctor
= mem_initializer
;
14915 /* Add it to the list, unless it was erroneous. */
14916 if (mem_initializer
!= error_mark_node
)
14918 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14919 mem_initializer_list
= mem_initializer
;
14921 /* If the next token is not a `,', we're done. */
14922 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14924 /* Consume the `,' token. */
14925 cp_lexer_consume_token (parser
->lexer
);
14928 /* Perform semantic analysis. */
14929 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14930 finish_mem_initializers (mem_initializer_list
);
14933 /* Parse a mem-initializer.
14936 mem-initializer-id ( expression-list [opt] )
14937 mem-initializer-id braced-init-list
14942 ( expression-list [opt] )
14944 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14945 class) or FIELD_DECL (for a non-static data member) to initialize;
14946 the TREE_VALUE is the expression-list. An empty initialization
14947 list is represented by void_list_node. */
14950 cp_parser_mem_initializer (cp_parser
* parser
)
14952 tree mem_initializer_id
;
14953 tree expression_list
;
14955 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14957 /* Find out what is being initialized. */
14958 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14960 permerror (token
->location
,
14961 "anachronistic old-style base class initializer");
14962 mem_initializer_id
= NULL_TREE
;
14966 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14967 if (mem_initializer_id
== error_mark_node
)
14968 return mem_initializer_id
;
14970 member
= expand_member_init (mem_initializer_id
);
14971 if (member
&& !DECL_P (member
))
14972 in_base_initializer
= 1;
14974 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14976 bool expr_non_constant_p
;
14977 cp_lexer_set_source_position (parser
->lexer
);
14978 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14979 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14980 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14981 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14985 vec
<tree
, va_gc
> *vec
;
14986 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14988 /*allow_expansion_p=*/true,
14989 /*non_constant_p=*/NULL
,
14990 /*close_paren_loc=*/NULL
,
14991 /*wrap_locations_p=*/true);
14993 return error_mark_node
;
14994 expression_list
= build_tree_list_vec (vec
);
14995 release_tree_vector (vec
);
14998 if (expression_list
== error_mark_node
)
14999 return error_mark_node
;
15000 if (!expression_list
)
15001 expression_list
= void_type_node
;
15003 in_base_initializer
= 0;
15005 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
15008 /* Parse a mem-initializer-id.
15010 mem-initializer-id:
15011 :: [opt] nested-name-specifier [opt] class-name
15012 decltype-specifier (C++11)
15015 Returns a TYPE indicating the class to be initialized for the first
15016 production (and the second in C++11). Returns an IDENTIFIER_NODE
15017 indicating the data member to be initialized for the last production. */
15020 cp_parser_mem_initializer_id (cp_parser
* parser
)
15022 bool global_scope_p
;
15023 bool nested_name_specifier_p
;
15024 bool template_p
= false;
15027 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15029 /* `typename' is not allowed in this context ([temp.res]). */
15030 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
15032 error_at (token
->location
,
15033 "keyword %<typename%> not allowed in this context (a qualified "
15034 "member initializer is implicitly a type)");
15035 cp_lexer_consume_token (parser
->lexer
);
15037 /* Look for the optional `::' operator. */
15039 = (cp_parser_global_scope_opt (parser
,
15040 /*current_scope_valid_p=*/false)
15042 /* Look for the optional nested-name-specifier. The simplest way to
15047 The keyword `typename' is not permitted in a base-specifier or
15048 mem-initializer; in these contexts a qualified name that
15049 depends on a template-parameter is implicitly assumed to be a
15052 is to assume that we have seen the `typename' keyword at this
15054 nested_name_specifier_p
15055 = (cp_parser_nested_name_specifier_opt (parser
,
15056 /*typename_keyword_p=*/true,
15057 /*check_dependency_p=*/true,
15059 /*is_declaration=*/true)
15061 if (nested_name_specifier_p
)
15062 template_p
= cp_parser_optional_template_keyword (parser
);
15063 /* If there is a `::' operator or a nested-name-specifier, then we
15064 are definitely looking for a class-name. */
15065 if (global_scope_p
|| nested_name_specifier_p
)
15066 return cp_parser_class_name (parser
,
15067 /*typename_keyword_p=*/true,
15068 /*template_keyword_p=*/template_p
,
15070 /*check_dependency_p=*/true,
15071 /*class_head_p=*/false,
15072 /*is_declaration=*/true);
15073 /* Otherwise, we could also be looking for an ordinary identifier. */
15074 cp_parser_parse_tentatively (parser
);
15075 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
15076 /* Try a decltype-specifier. */
15077 id
= cp_parser_decltype (parser
);
15079 /* Otherwise, try a class-name. */
15080 id
= cp_parser_class_name (parser
,
15081 /*typename_keyword_p=*/true,
15082 /*template_keyword_p=*/false,
15084 /*check_dependency_p=*/true,
15085 /*class_head_p=*/false,
15086 /*is_declaration=*/true);
15087 /* If we found one, we're done. */
15088 if (cp_parser_parse_definitely (parser
))
15090 /* Otherwise, look for an ordinary identifier. */
15091 return cp_parser_identifier (parser
);
15094 /* Overloading [gram.over] */
15096 /* Parse an operator-function-id.
15098 operator-function-id:
15101 Returns an IDENTIFIER_NODE for the operator which is a
15102 human-readable spelling of the identifier, e.g., `operator +'. */
15105 cp_parser_operator_function_id (cp_parser
* parser
)
15107 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15108 /* Look for the `operator' keyword. */
15109 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15110 return error_mark_node
;
15111 /* And then the name of the operator itself. */
15112 return cp_parser_operator (parser
, start_loc
);
15115 /* Return an identifier node for a user-defined literal operator.
15116 The suffix identifier is chained to the operator name identifier. */
15119 cp_literal_operator_id (const char* name
)
15122 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
15123 + strlen (name
) + 10);
15124 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
15125 identifier
= get_identifier (buffer
);
15130 /* Parse an operator.
15133 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15134 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15135 || ++ -- , ->* -> () []
15142 Returns an IDENTIFIER_NODE for the operator which is a
15143 human-readable spelling of the identifier, e.g., `operator +'. */
15146 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
15148 tree id
= NULL_TREE
;
15152 /* Peek at the next token. */
15153 token
= cp_lexer_peek_token (parser
->lexer
);
15155 location_t end_loc
= token
->location
;
15157 /* Figure out which operator we have. */
15158 enum tree_code op
= ERROR_MARK
;
15159 bool assop
= false;
15160 bool consumed
= false;
15161 switch (token
->type
)
15165 /* The keyword should be either `new' or `delete'. */
15166 if (token
->keyword
== RID_NEW
)
15168 else if (token
->keyword
== RID_DELETE
)
15173 /* Consume the `new' or `delete' token. */
15174 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
15176 /* Peek at the next token. */
15177 token
= cp_lexer_peek_token (parser
->lexer
);
15178 /* If it's a `[' token then this is the array variant of the
15180 if (token
->type
== CPP_OPEN_SQUARE
)
15182 /* Consume the `[' token. */
15183 cp_lexer_consume_token (parser
->lexer
);
15184 /* Look for the `]' token. */
15185 if (cp_token
*close_token
15186 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15187 end_loc
= close_token
->location
;
15188 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
15207 op
= TRUNC_DIV_EXPR
;
15211 op
= TRUNC_MOD_EXPR
;
15231 op
= TRUTH_NOT_EXPR
;
15264 op
= TRUNC_DIV_EXPR
;
15269 op
= TRUNC_MOD_EXPR
;
15295 case CPP_LSHIFT_EQ
:
15300 case CPP_RSHIFT_EQ
:
15317 case CPP_GREATER_EQ
:
15322 op
= TRUTH_ANDIF_EXPR
;
15326 op
= TRUTH_ORIF_EXPR
;
15329 case CPP_PLUS_PLUS
:
15330 op
= POSTINCREMENT_EXPR
;
15333 case CPP_MINUS_MINUS
:
15334 op
= PREDECREMENT_EXPR
;
15338 op
= COMPOUND_EXPR
;
15341 case CPP_DEREF_STAR
:
15346 op
= COMPONENT_REF
;
15349 case CPP_OPEN_PAREN
:
15351 /* Consume the `('. */
15352 matching_parens parens
;
15353 parens
.consume_open (parser
);
15354 /* Look for the matching `)'. */
15355 token
= parens
.require_close (parser
);
15357 end_loc
= token
->location
;
15363 case CPP_OPEN_SQUARE
:
15364 /* Consume the `['. */
15365 cp_lexer_consume_token (parser
->lexer
);
15366 /* Look for the matching `]'. */
15367 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15369 end_loc
= token
->location
;
15374 case CPP_UTF8STRING
:
15375 case CPP_UTF8STRING_USERDEF
:
15382 case CPP_STRING_USERDEF
:
15383 case CPP_WSTRING_USERDEF
:
15384 case CPP_STRING16_USERDEF
:
15385 case CPP_STRING32_USERDEF
:
15391 if (cxx_dialect
== cxx98
)
15392 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15394 /* Consume the string. */
15395 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15396 /*wide_ok=*/true, /*lookup_udlit=*/false);
15397 if (str
== error_mark_node
)
15398 return error_mark_node
;
15399 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15401 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
15402 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
15403 end_loc
= str
.get_location ();
15408 /* Look for the suffix identifier. */
15409 token
= cp_lexer_peek_token (parser
->lexer
);
15410 if (token
->type
== CPP_NAME
)
15412 id
= cp_parser_identifier (parser
);
15413 end_loc
= token
->location
;
15415 else if (token
->type
== CPP_KEYWORD
)
15417 error ("unexpected keyword;"
15418 " remove space between quotes and suffix identifier");
15419 return error_mark_node
;
15423 error ("expected suffix identifier");
15424 return error_mark_node
;
15427 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15428 (TREE_TYPE (TREE_TYPE (string_tree
))));
15429 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15432 error ("expected empty string after %<operator%> keyword");
15433 return error_mark_node
;
15435 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15438 error ("invalid encoding prefix in literal operator");
15439 return error_mark_node
;
15441 if (id
!= error_mark_node
)
15443 const char *name
= IDENTIFIER_POINTER (id
);
15444 id
= cp_literal_operator_id (name
);
15446 /* Generate a location of the form:
15447 "" _suffix_identifier
15448 ^~~~~~~~~~~~~~~~~~~~~
15449 with caret == start at the start token, finish at the end of the
15450 suffix identifier. */
15451 location_t finish_loc
15452 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15453 location_t combined_loc
15454 = make_location (start_loc
, start_loc
, finish_loc
);
15455 return cp_expr (id
, combined_loc
);
15459 /* Anything else is an error. */
15463 /* If we have selected an identifier, we need to consume the
15465 if (op
!= ERROR_MARK
)
15467 id
= ovl_op_identifier (assop
, op
);
15469 cp_lexer_consume_token (parser
->lexer
);
15471 /* Otherwise, no valid operator name was present. */
15474 cp_parser_error (parser
, "expected operator");
15475 id
= error_mark_node
;
15478 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15479 return cp_expr (id
, start_loc
);
15482 /* Parse a template-declaration.
15484 template-declaration:
15485 export [opt] template < template-parameter-list > declaration
15487 If MEMBER_P is TRUE, this template-declaration occurs within a
15490 The grammar rule given by the standard isn't correct. What
15491 is really meant is:
15493 template-declaration:
15494 export [opt] template-parameter-list-seq
15495 decl-specifier-seq [opt] init-declarator [opt] ;
15496 export [opt] template-parameter-list-seq
15497 function-definition
15499 template-parameter-list-seq:
15500 template-parameter-list-seq [opt]
15501 template < template-parameter-list >
15503 Concept Extensions:
15505 template-parameter-list-seq:
15506 template < template-parameter-list > requires-clause [opt]
15509 requires logical-or-expression */
15512 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15514 /* Check for `export'. */
15515 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15517 /* Consume the `export' token. */
15518 cp_lexer_consume_token (parser
->lexer
);
15519 /* Warn that we do not support `export'. */
15520 warning (0, "keyword %<export%> not implemented, and will be ignored");
15523 cp_parser_template_declaration_after_export (parser
, member_p
);
15526 /* Parse a template-parameter-list.
15528 template-parameter-list:
15530 template-parameter-list , template-parameter
15532 Returns a TREE_LIST. Each node represents a template parameter.
15533 The nodes are connected via their TREE_CHAINs. */
15536 cp_parser_template_parameter_list (cp_parser
* parser
)
15538 tree parameter_list
= NULL_TREE
;
15540 /* Don't create wrapper nodes within a template-parameter-list,
15541 since we don't want to have different types based on the
15542 spelling location of constants and decls within them. */
15543 auto_suppress_location_wrappers sentinel
;
15545 begin_template_parm_list ();
15547 /* The loop below parses the template parms. We first need to know
15548 the total number of template parms to be able to compute proper
15549 canonical types of each dependent type. So after the loop, when
15550 we know the total number of template parms,
15551 end_template_parm_list computes the proper canonical types and
15552 fixes up the dependent types accordingly. */
15557 bool is_parameter_pack
;
15558 location_t parm_loc
;
15560 /* Parse the template-parameter. */
15561 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15562 parameter
= cp_parser_template_parameter (parser
,
15564 &is_parameter_pack
);
15565 /* Add it to the list. */
15566 if (parameter
!= error_mark_node
)
15567 parameter_list
= process_template_parm (parameter_list
,
15571 is_parameter_pack
);
15574 tree err_parm
= build_tree_list (parameter
, parameter
);
15575 parameter_list
= chainon (parameter_list
, err_parm
);
15578 /* If the next token is not a `,', we're done. */
15579 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15581 /* Otherwise, consume the `,' token. */
15582 cp_lexer_consume_token (parser
->lexer
);
15585 return end_template_parm_list (parameter_list
);
15588 /* Parse a introduction-list.
15591 introduced-parameter
15592 introduction-list , introduced-parameter
15594 introduced-parameter:
15595 ...[opt] identifier
15597 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15598 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15599 WILDCARD_DECL will also have DECL_NAME set and token location in
15600 DECL_SOURCE_LOCATION. */
15603 cp_parser_introduction_list (cp_parser
*parser
)
15605 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15609 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15611 cp_lexer_consume_token (parser
->lexer
);
15613 tree identifier
= cp_parser_identifier (parser
);
15614 if (identifier
== error_mark_node
)
15617 /* Build placeholder. */
15618 tree parm
= build_nt (WILDCARD_DECL
);
15619 DECL_SOURCE_LOCATION (parm
)
15620 = cp_lexer_peek_token (parser
->lexer
)->location
;
15621 DECL_NAME (parm
) = identifier
;
15622 WILDCARD_PACK_P (parm
) = is_pack
;
15623 vec_safe_push (introduction_vec
, parm
);
15625 /* If the next token is not a `,', we're done. */
15626 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15628 /* Otherwise, consume the `,' token. */
15629 cp_lexer_consume_token (parser
->lexer
);
15632 /* Convert the vec into a TREE_VEC. */
15633 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15636 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15637 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15639 release_tree_vector (introduction_vec
);
15640 return introduction_list
;
15643 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15644 is an abstract declarator. */
15646 static inline cp_declarator
*
15647 get_id_declarator (cp_declarator
*declarator
)
15649 cp_declarator
*d
= declarator
;
15650 while (d
&& d
->kind
!= cdk_id
)
15655 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15656 is an abstract declarator. */
15659 get_unqualified_id (cp_declarator
*declarator
)
15661 declarator
= get_id_declarator (declarator
);
15663 return declarator
->u
.id
.unqualified_name
;
15668 /* Returns true if DECL represents a constrained-parameter. */
15671 is_constrained_parameter (tree decl
)
15674 && TREE_CODE (decl
) == TYPE_DECL
15675 && CONSTRAINED_PARM_CONCEPT (decl
)
15676 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15679 /* Returns true if PARM declares a constrained-parameter. */
15682 is_constrained_parameter (cp_parameter_declarator
*parm
)
15684 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15687 /* Check that the type parameter is only a declarator-id, and that its
15688 type is not cv-qualified. */
15691 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15692 cp_parameter_declarator
*parm
)
15694 if (!parm
->declarator
)
15697 if (parm
->declarator
->kind
!= cdk_id
)
15699 cp_parser_error (parser
, "invalid constrained type parameter");
15703 /* Don't allow cv-qualified type parameters. */
15704 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15705 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15707 cp_parser_error (parser
, "cv-qualified type parameter");
15714 /* Finish parsing/processing a template type parameter and checking
15715 various restrictions. */
15718 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15720 cp_parameter_declarator
* parmdecl
)
15722 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15723 return finish_template_type_parm (class_type_node
, id
);
15725 return error_mark_node
;
15729 finish_constrained_template_template_parm (tree proto
, tree id
)
15731 /* FIXME: This should probably be copied, and we may need to adjust
15732 the template parameter depths. */
15733 tree saved_parms
= current_template_parms
;
15734 begin_template_parm_list ();
15735 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15736 end_template_parm_list ();
15738 tree parm
= finish_template_template_parm (class_type_node
, id
);
15739 current_template_parms
= saved_parms
;
15744 /* Finish parsing/processing a template template parameter by borrowing
15745 the template parameter list from the prototype parameter. */
15748 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15751 cp_parameter_declarator
*parmdecl
)
15753 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15754 return error_mark_node
;
15755 return finish_constrained_template_template_parm (proto
, id
);
15758 /* Create a new non-type template parameter from the given PARM
15762 constrained_non_type_template_parm (bool *is_non_type
,
15763 cp_parameter_declarator
*parm
)
15765 *is_non_type
= true;
15766 cp_declarator
*decl
= parm
->declarator
;
15767 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15768 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15769 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15772 /* Build a constrained template parameter based on the PARMDECL
15773 declarator. The type of PARMDECL is the constrained type, which
15774 refers to the prototype template parameter that ultimately
15775 specifies the type of the declared parameter. */
15778 finish_constrained_parameter (cp_parser
*parser
,
15779 cp_parameter_declarator
*parmdecl
,
15781 bool *is_parameter_pack
)
15783 tree decl
= parmdecl
->decl_specifiers
.type
;
15784 tree id
= get_unqualified_id (parmdecl
->declarator
);
15785 tree def
= parmdecl
->default_argument
;
15786 tree proto
= DECL_INITIAL (decl
);
15788 /* A template parameter constrained by a variadic concept shall also
15789 be declared as a template parameter pack. */
15790 bool is_variadic
= template_parameter_pack_p (proto
);
15791 if (is_variadic
&& !*is_parameter_pack
)
15792 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15794 /* Build the parameter. Return an error if the declarator was invalid. */
15796 if (TREE_CODE (proto
) == TYPE_DECL
)
15797 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15798 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15799 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15802 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15803 if (parm
== error_mark_node
)
15804 return error_mark_node
;
15806 /* Finish the parameter decl and create a node attaching the
15807 default argument and constraint. */
15808 parm
= build_tree_list (def
, parm
);
15809 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15814 /* Returns true if the parsed type actually represents the declaration
15815 of a type template-parameter. */
15818 declares_constrained_type_template_parameter (tree type
)
15820 return (is_constrained_parameter (type
)
15821 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15825 /* Returns true if the parsed type actually represents the declaration of
15826 a template template-parameter. */
15829 declares_constrained_template_template_parameter (tree type
)
15831 return (is_constrained_parameter (type
)
15832 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15835 /* Parse a default argument for a type template-parameter.
15836 Note that diagnostics are handled in cp_parser_template_parameter. */
15839 cp_parser_default_type_template_argument (cp_parser
*parser
)
15841 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15843 /* Consume the `=' token. */
15844 cp_lexer_consume_token (parser
->lexer
);
15846 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15848 /* Parse the default-argument. */
15849 push_deferring_access_checks (dk_no_deferred
);
15850 tree default_argument
= cp_parser_type_id (parser
,
15851 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15853 pop_deferring_access_checks ();
15855 if (flag_concepts
&& type_uses_auto (default_argument
))
15857 error_at (token
->location
,
15858 "invalid use of %<auto%> in default template argument");
15859 return error_mark_node
;
15862 return default_argument
;
15865 /* Parse a default argument for a template template-parameter. */
15868 cp_parser_default_template_template_argument (cp_parser
*parser
)
15870 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15874 /* Consume the `='. */
15875 cp_lexer_consume_token (parser
->lexer
);
15876 /* Parse the id-expression. */
15877 push_deferring_access_checks (dk_no_deferred
);
15878 /* save token before parsing the id-expression, for error
15880 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15881 tree default_argument
15882 = cp_parser_id_expression (parser
,
15883 /*template_keyword_p=*/false,
15884 /*check_dependency_p=*/true,
15885 /*template_p=*/&is_template
,
15886 /*declarator_p=*/false,
15887 /*optional_p=*/false);
15888 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15889 /* If the id-expression was a template-id that refers to
15890 a template-class, we already have the declaration here,
15891 so no further lookup is needed. */
15894 /* Look up the name. */
15896 = cp_parser_lookup_name (parser
, default_argument
,
15898 /*is_template=*/is_template
,
15899 /*is_namespace=*/false,
15900 /*check_dependency=*/true,
15901 /*ambiguous_decls=*/NULL
,
15903 /* See if the default argument is valid. */
15904 default_argument
= check_template_template_default_arg (default_argument
);
15905 pop_deferring_access_checks ();
15906 return default_argument
;
15909 /* Parse a template-parameter.
15911 template-parameter:
15913 parameter-declaration
15915 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15916 the parameter. The TREE_PURPOSE is the default value, if any.
15917 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15918 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15919 set to true iff this parameter is a parameter pack. */
15922 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15923 bool *is_parameter_pack
)
15926 cp_parameter_declarator
*parameter_declarator
;
15929 /* Assume it is a type parameter or a template parameter. */
15930 *is_non_type
= false;
15931 /* Assume it not a parameter pack. */
15932 *is_parameter_pack
= false;
15933 /* Peek at the next token. */
15934 token
= cp_lexer_peek_token (parser
->lexer
);
15935 /* If it is `template', we have a type-parameter. */
15936 if (token
->keyword
== RID_TEMPLATE
)
15937 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15938 /* If it is `class' or `typename' we do not know yet whether it is a
15939 type parameter or a non-type parameter. Consider:
15941 template <typename T, typename T::X X> ...
15945 template <class C, class D*> ...
15947 Here, the first parameter is a type parameter, and the second is
15948 a non-type parameter. We can tell by looking at the token after
15949 the identifier -- if it is a `,', `=', or `>' then we have a type
15951 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15953 /* Peek at the token after `class' or `typename'. */
15954 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15955 /* If it's an ellipsis, we have a template type parameter
15957 if (token
->type
== CPP_ELLIPSIS
)
15958 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15959 /* If it's an identifier, skip it. */
15960 if (token
->type
== CPP_NAME
)
15961 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15962 /* Now, see if the token looks like the end of a template
15964 if (token
->type
== CPP_COMMA
15965 || token
->type
== CPP_EQ
15966 || token
->type
== CPP_GREATER
)
15967 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15970 /* Otherwise, it is a non-type parameter or a constrained parameter.
15974 When parsing a default template-argument for a non-type
15975 template-parameter, the first non-nested `>' is taken as the end
15976 of the template parameter-list rather than a greater-than
15978 parameter_declarator
15979 = cp_parser_parameter_declaration (parser
,
15980 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15981 /*template_parm_p=*/true,
15982 /*parenthesized_p=*/NULL
);
15984 if (!parameter_declarator
)
15985 return error_mark_node
;
15987 /* If the parameter declaration is marked as a parameter pack, set
15988 *IS_PARAMETER_PACK to notify the caller. */
15989 if (parameter_declarator
->template_parameter_pack_p
)
15990 *is_parameter_pack
= true;
15992 if (parameter_declarator
->default_argument
)
15994 /* Can happen in some cases of erroneous input (c++/34892). */
15995 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15996 /* Consume the `...' for better error recovery. */
15997 cp_lexer_consume_token (parser
->lexer
);
16000 // The parameter may have been constrained.
16001 if (is_constrained_parameter (parameter_declarator
))
16002 return finish_constrained_parameter (parser
,
16003 parameter_declarator
,
16005 is_parameter_pack
);
16007 // Now we're sure that the parameter is a non-type parameter.
16008 *is_non_type
= true;
16010 parm
= grokdeclarator (parameter_declarator
->declarator
,
16011 ¶meter_declarator
->decl_specifiers
,
16012 TPARM
, /*initialized=*/0,
16013 /*attrlist=*/NULL
);
16014 if (parm
== error_mark_node
)
16015 return error_mark_node
;
16017 return build_tree_list (parameter_declarator
->default_argument
, parm
);
16020 /* Parse a type-parameter.
16023 class identifier [opt]
16024 class identifier [opt] = type-id
16025 typename identifier [opt]
16026 typename identifier [opt] = type-id
16027 template < template-parameter-list > class identifier [opt]
16028 template < template-parameter-list > class identifier [opt]
16031 GNU Extension (variadic templates):
16034 class ... identifier [opt]
16035 typename ... identifier [opt]
16037 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16038 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16039 the declaration of the parameter.
16041 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16044 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
16049 /* Look for a keyword to tell us what kind of parameter this is. */
16050 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
16052 return error_mark_node
;
16054 switch (token
->keyword
)
16060 tree default_argument
;
16062 /* If the next token is an ellipsis, we have a template
16064 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16066 /* Consume the `...' token. */
16067 cp_lexer_consume_token (parser
->lexer
);
16068 maybe_warn_variadic_templates ();
16070 *is_parameter_pack
= true;
16073 /* If the next token is an identifier, then it names the
16075 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16076 identifier
= cp_parser_identifier (parser
);
16078 identifier
= NULL_TREE
;
16080 /* Create the parameter. */
16081 parameter
= finish_template_type_parm (class_type_node
, identifier
);
16083 /* If the next token is an `=', we have a default argument. */
16084 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16087 = cp_parser_default_type_template_argument (parser
);
16089 /* Template parameter packs cannot have default
16091 if (*is_parameter_pack
)
16094 error_at (token
->location
,
16095 "template parameter pack %qD cannot have a "
16096 "default argument", identifier
);
16098 error_at (token
->location
,
16099 "template parameter packs cannot have "
16100 "default arguments");
16101 default_argument
= NULL_TREE
;
16103 else if (check_for_bare_parameter_packs (default_argument
))
16104 default_argument
= error_mark_node
;
16107 default_argument
= NULL_TREE
;
16109 /* Create the combined representation of the parameter and the
16110 default argument. */
16111 parameter
= build_tree_list (default_argument
, parameter
);
16118 tree default_argument
;
16120 /* Look for the `<'. */
16121 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16122 /* Parse the template-parameter-list. */
16123 cp_parser_template_parameter_list (parser
);
16124 /* Look for the `>'. */
16125 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16127 // If template requirements are present, parse them.
16130 tree reqs
= get_shorthand_constraints (current_template_parms
);
16131 if (tree r
= cp_parser_requires_clause_opt (parser
))
16132 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
16133 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
16136 /* Look for the `class' or 'typename' keywords. */
16137 cp_parser_type_parameter_key (parser
);
16138 /* If the next token is an ellipsis, we have a template
16140 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16142 /* Consume the `...' token. */
16143 cp_lexer_consume_token (parser
->lexer
);
16144 maybe_warn_variadic_templates ();
16146 *is_parameter_pack
= true;
16148 /* If the next token is an `=', then there is a
16149 default-argument. If the next token is a `>', we are at
16150 the end of the parameter-list. If the next token is a `,',
16151 then we are at the end of this parameter. */
16152 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16153 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
16154 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16156 identifier
= cp_parser_identifier (parser
);
16157 /* Treat invalid names as if the parameter were nameless. */
16158 if (identifier
== error_mark_node
)
16159 identifier
= NULL_TREE
;
16162 identifier
= NULL_TREE
;
16164 /* Create the template parameter. */
16165 parameter
= finish_template_template_parm (class_type_node
,
16168 /* If the next token is an `=', then there is a
16169 default-argument. */
16170 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16173 = cp_parser_default_template_template_argument (parser
);
16175 /* Template parameter packs cannot have default
16177 if (*is_parameter_pack
)
16180 error_at (token
->location
,
16181 "template parameter pack %qD cannot "
16182 "have a default argument",
16185 error_at (token
->location
, "template parameter packs cannot "
16186 "have default arguments");
16187 default_argument
= NULL_TREE
;
16191 default_argument
= NULL_TREE
;
16193 /* Create the combined representation of the parameter and the
16194 default argument. */
16195 parameter
= build_tree_list (default_argument
, parameter
);
16200 gcc_unreachable ();
16207 /* Parse a template-id.
16210 template-name < template-argument-list [opt] >
16212 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16213 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16214 returned. Otherwise, if the template-name names a function, or set
16215 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16216 names a class, returns a TYPE_DECL for the specialization.
16218 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16219 uninstantiated templates. */
16222 cp_parser_template_id (cp_parser
*parser
,
16223 bool template_keyword_p
,
16224 bool check_dependency_p
,
16225 enum tag_types tag_type
,
16226 bool is_declaration
)
16231 cp_token_position start_of_id
= 0;
16232 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
16233 bool is_identifier
;
16235 /* If the next token corresponds to a template-id, there is no need
16237 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16238 if (token
->type
== CPP_TEMPLATE_ID
)
16240 cp_lexer_consume_token (parser
->lexer
);
16241 return saved_checks_value (token
->u
.tree_check_value
);
16244 /* Avoid performing name lookup if there is no possibility of
16245 finding a template-id. */
16246 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
16247 || (token
->type
== CPP_NAME
16248 && !cp_parser_nth_token_starts_template_argument_list_p
16251 cp_parser_error (parser
, "expected template-id");
16252 return error_mark_node
;
16255 /* Remember where the template-id starts. */
16256 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
16257 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
16259 push_deferring_access_checks (dk_deferred
);
16261 /* Parse the template-name. */
16262 is_identifier
= false;
16263 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16264 check_dependency_p
,
16269 /* Push any access checks inside the firewall we're about to create. */
16270 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
16271 pop_deferring_access_checks ();
16272 if (templ
== error_mark_node
|| is_identifier
)
16275 /* Since we're going to preserve any side-effects from this parse, set up a
16276 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16277 in the template arguments. */
16278 tentative_firewall
firewall (parser
);
16279 reopen_deferring_access_checks (checks
);
16281 /* If we find the sequence `[:' after a template-name, it's probably
16282 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16283 parse correctly the argument list. */
16284 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16285 == CPP_OPEN_SQUARE
)
16286 && next_token
->flags
& DIGRAPH
16287 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16289 && !(next_token_2
->flags
& PREV_WHITE
))
16291 cp_parser_parse_tentatively (parser
);
16292 /* Change `:' into `::'. */
16293 next_token_2
->type
= CPP_SCOPE
;
16294 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16296 cp_lexer_consume_token (parser
->lexer
);
16298 /* Parse the arguments. */
16299 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16300 if (!cp_parser_parse_definitely (parser
))
16302 /* If we couldn't parse an argument list, then we revert our changes
16303 and return simply an error. Maybe this is not a template-id
16305 next_token_2
->type
= CPP_COLON
;
16306 cp_parser_error (parser
, "expected %<<%>");
16307 pop_deferring_access_checks ();
16308 return error_mark_node
;
16310 /* Otherwise, emit an error about the invalid digraph, but continue
16311 parsing because we got our argument list. */
16312 if (permerror (next_token
->location
,
16313 "%<<::%> cannot begin a template-argument list"))
16315 static bool hint
= false;
16316 inform (next_token
->location
,
16317 "%<<:%> is an alternate spelling for %<[%>."
16318 " Insert whitespace between %<<%> and %<::%>");
16319 if (!hint
&& !flag_permissive
)
16321 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16322 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16323 "accept your code)");
16330 /* Look for the `<' that starts the template-argument-list. */
16331 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16333 pop_deferring_access_checks ();
16334 return error_mark_node
;
16336 /* Parse the arguments. */
16337 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16339 if ((cxx_dialect
> cxx17
)
16340 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16341 && !template_keyword_p
16342 && (cp_parser_error_occurred (parser
)
16343 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16345 /* This didn't go well. */
16346 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16348 /* C++2A says that "function-name < a;" is now ill-formed. */
16349 if (cp_parser_error_occurred (parser
))
16351 error_at (token
->location
, "invalid template-argument-list");
16352 inform (token
->location
, "function name as the left hand "
16353 "operand of %<<%> is ill-formed in C++2a; wrap the "
16354 "function name in %<()%>");
16357 /* We expect "f<targs>" to be followed by "(args)". */
16358 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16359 "expected %<(%> after template-argument-list");
16361 /* Purge all subsequent tokens. */
16362 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16365 cp_parser_simulate_error (parser
);
16366 pop_deferring_access_checks ();
16367 return error_mark_node
;
16371 /* Set the location to be of the form:
16372 template-name < template-argument-list [opt] >
16373 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16374 with caret == start at the start of the template-name,
16375 ranging until the closing '>'. */
16376 location_t finish_loc
16377 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
16378 location_t combined_loc
16379 = make_location (token
->location
, token
->location
, finish_loc
);
16381 /* Check for concepts autos where they don't belong. We could
16382 identify types in some cases of idnetifier TEMPL, looking ahead
16383 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16384 types. We reject them in functions, but if what we have is an
16385 identifier, even with none_type we can't conclude it's NOT a
16386 type, we have to wait for template substitution. */
16387 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16388 template_id
= error_mark_node
;
16389 /* Build a representation of the specialization. */
16390 else if (identifier_p (templ
))
16391 template_id
= build_min_nt_loc (combined_loc
,
16394 else if (DECL_TYPE_TEMPLATE_P (templ
)
16395 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16397 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16398 template (rather than some instantiation thereof) only if
16399 is not nested within some other construct. For example, in
16400 "template <typename T> void f(T) { A<T>::", A<T> is just an
16401 instantiation of A. */
16402 bool entering_scope
16403 = (template_parm_scope_p ()
16404 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16406 = finish_template_type (templ
, arguments
, entering_scope
);
16408 /* A template-like identifier may be a partial concept id. */
16409 else if (flag_concepts
16410 && (template_id
= (cp_parser_maybe_partial_concept_id
16411 (parser
, templ
, arguments
))))
16412 return template_id
;
16413 else if (variable_template_p (templ
))
16415 template_id
= lookup_template_variable (templ
, arguments
);
16416 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16417 SET_EXPR_LOCATION (template_id
, combined_loc
);
16421 /* If it's not a class-template or a template-template, it should be
16422 a function-template. */
16423 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16424 || TREE_CODE (templ
) == OVERLOAD
16425 || TREE_CODE (templ
) == FUNCTION_DECL
16426 || BASELINK_P (templ
)));
16428 template_id
= lookup_template_function (templ
, arguments
);
16429 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16430 SET_EXPR_LOCATION (template_id
, combined_loc
);
16433 /* If parsing tentatively, replace the sequence of tokens that makes
16434 up the template-id with a CPP_TEMPLATE_ID token. That way,
16435 should we re-parse the token stream, we will not have to repeat
16436 the effort required to do the parse, nor will we issue duplicate
16437 error messages about problems during instantiation of the
16440 /* Don't do this if we had a parse error in a declarator; re-parsing
16441 might succeed if a name changes meaning (60361). */
16442 && !(cp_parser_error_occurred (parser
)
16443 && cp_parser_parsing_tentatively (parser
)
16444 && parser
->in_declarator_p
))
16446 /* Reset the contents of the START_OF_ID token. */
16447 token
->type
= CPP_TEMPLATE_ID
;
16448 token
->location
= combined_loc
;
16450 /* Retrieve any deferred checks. Do not pop this access checks yet
16451 so the memory will not be reclaimed during token replacing below. */
16452 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16453 token
->u
.tree_check_value
->value
= template_id
;
16454 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16455 token
->keyword
= RID_MAX
;
16457 /* Purge all subsequent tokens. */
16458 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16460 /* ??? Can we actually assume that, if template_id ==
16461 error_mark_node, we will have issued a diagnostic to the
16462 user, as opposed to simply marking the tentative parse as
16464 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16465 error_at (token
->location
, "parse error in template argument list");
16468 pop_to_parent_deferring_access_checks ();
16469 return template_id
;
16472 /* Parse a template-name.
16477 The standard should actually say:
16481 operator-function-id
16483 A defect report has been filed about this issue.
16485 A conversion-function-id cannot be a template name because they cannot
16486 be part of a template-id. In fact, looking at this code:
16488 a.operator K<int>()
16490 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16491 It is impossible to call a templated conversion-function-id with an
16492 explicit argument list, since the only allowed template parameter is
16493 the type to which it is converting.
16495 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16496 `template' keyword, in a construction like:
16500 In that case `f' is taken to be a template-name, even though there
16501 is no way of knowing for sure.
16503 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16504 name refers to a set of overloaded functions, at least one of which
16505 is a template, or an IDENTIFIER_NODE with the name of the template,
16506 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16507 names are looked up inside uninstantiated templates. */
16510 cp_parser_template_name (cp_parser
* parser
,
16511 bool template_keyword_p
,
16512 bool check_dependency_p
,
16513 bool is_declaration
,
16514 enum tag_types tag_type
,
16515 bool *is_identifier
)
16519 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16521 /* If the next token is `operator', then we have either an
16522 operator-function-id or a conversion-function-id. */
16523 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16525 /* We don't know whether we're looking at an
16526 operator-function-id or a conversion-function-id. */
16527 cp_parser_parse_tentatively (parser
);
16528 /* Try an operator-function-id. */
16529 identifier
= cp_parser_operator_function_id (parser
);
16530 /* If that didn't work, try a conversion-function-id. */
16531 if (!cp_parser_parse_definitely (parser
))
16533 cp_parser_error (parser
, "expected template-name");
16534 return error_mark_node
;
16537 /* Look for the identifier. */
16539 identifier
= cp_parser_identifier (parser
);
16541 /* If we didn't find an identifier, we don't have a template-id. */
16542 if (identifier
== error_mark_node
)
16543 return error_mark_node
;
16545 /* If the name immediately followed the `template' keyword, then it
16546 is a template-name. However, if the next token is not `<', then
16547 we do not treat it as a template-name, since it is not being used
16548 as part of a template-id. This enables us to handle constructs
16551 template <typename T> struct S { S(); };
16552 template <typename T> S<T>::S();
16554 correctly. We would treat `S' as a template -- if it were `S<T>'
16555 -- but we do not if there is no `<'. */
16557 if (processing_template_decl
16558 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16560 /* In a declaration, in a dependent context, we pretend that the
16561 "template" keyword was present in order to improve error
16562 recovery. For example, given:
16564 template <typename T> void f(T::X<int>);
16566 we want to treat "X<int>" as a template-id. */
16568 && !template_keyword_p
16569 && parser
->scope
&& TYPE_P (parser
->scope
)
16570 && check_dependency_p
16571 && dependent_scope_p (parser
->scope
)
16572 /* Do not do this for dtors (or ctors), since they never
16573 need the template keyword before their name. */
16574 && !constructor_name_p (identifier
, parser
->scope
))
16576 cp_token_position start
= 0;
16578 /* Explain what went wrong. */
16579 error_at (token
->location
, "non-template %qD used as template",
16581 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16582 parser
->scope
, identifier
);
16583 /* If parsing tentatively, find the location of the "<" token. */
16584 if (cp_parser_simulate_error (parser
))
16585 start
= cp_lexer_token_position (parser
->lexer
, true);
16586 /* Parse the template arguments so that we can issue error
16587 messages about them. */
16588 cp_lexer_consume_token (parser
->lexer
);
16589 cp_parser_enclosed_template_argument_list (parser
);
16590 /* Skip tokens until we find a good place from which to
16591 continue parsing. */
16592 cp_parser_skip_to_closing_parenthesis (parser
,
16593 /*recovering=*/true,
16595 /*consume_paren=*/false);
16596 /* If parsing tentatively, permanently remove the
16597 template argument list. That will prevent duplicate
16598 error messages from being issued about the missing
16599 "template" keyword. */
16601 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16603 *is_identifier
= true;
16604 parser
->context
->object_type
= NULL_TREE
;
16608 /* If the "template" keyword is present, then there is generally
16609 no point in doing name-lookup, so we just return IDENTIFIER.
16610 But, if the qualifying scope is non-dependent then we can
16611 (and must) do name-lookup normally. */
16612 if (template_keyword_p
)
16614 tree scope
= (parser
->scope
? parser
->scope
16615 : parser
->context
->object_type
);
16616 if (scope
&& TYPE_P (scope
)
16617 && (!CLASS_TYPE_P (scope
)
16618 || (check_dependency_p
&& dependent_type_p (scope
))))
16620 /* We're optimizing away the call to cp_parser_lookup_name, but
16621 we still need to do this. */
16622 parser
->context
->object_type
= NULL_TREE
;
16628 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16629 const bool scoped_p
= ((parser
->scope
? parser
->scope
16630 : parser
->context
->object_type
) != NULL_TREE
);
16632 /* Look up the name. */
16633 decl
= cp_parser_lookup_name (parser
, identifier
,
16635 /*is_template=*/true,
16636 /*is_namespace=*/false,
16637 check_dependency_p
,
16638 /*ambiguous_decls=*/NULL
,
16641 decl
= strip_using_decl (decl
);
16643 /* If DECL is a template, then the name was a template-name. */
16644 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16646 if (TREE_DEPRECATED (decl
)
16647 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16648 warn_deprecated_use (decl
, NULL_TREE
);
16652 /* The standard does not explicitly indicate whether a name that
16653 names a set of overloaded declarations, some of which are
16654 templates, is a template-name. However, such a name should
16655 be a template-name; otherwise, there is no way to form a
16656 template-id for the overloaded templates. */
16657 bool found
= false;
16659 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16660 !found
&& iter
; ++iter
)
16661 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16665 && (cxx_dialect
> cxx17
)
16667 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
)
16668 && tag_type
== none_type
)
16670 /* [temp.names] says "A name is also considered to refer to a template
16671 if it is an unqualified-id followed by a < and name lookup finds
16672 either one or more functions or finds nothing." */
16674 /* The "more functions" case. Just use the OVERLOAD as normally.
16675 We don't use is_overloaded_fn here to avoid considering
16677 if (TREE_CODE (decl
) == OVERLOAD
16678 /* Name lookup found one function. */
16679 || TREE_CODE (decl
) == FUNCTION_DECL
)
16681 /* Name lookup found nothing. */
16682 else if (decl
== error_mark_node
)
16688 /* The name does not name a template. */
16689 cp_parser_error (parser
, "expected template-name");
16690 return error_mark_node
;
16697 /* Parse a template-argument-list.
16699 template-argument-list:
16700 template-argument ... [opt]
16701 template-argument-list , template-argument ... [opt]
16703 Returns a TREE_VEC containing the arguments. */
16706 cp_parser_template_argument_list (cp_parser
* parser
)
16708 tree fixed_args
[10];
16709 unsigned n_args
= 0;
16710 unsigned alloced
= 10;
16711 tree
*arg_ary
= fixed_args
;
16713 bool saved_in_template_argument_list_p
;
16715 bool saved_non_ice_p
;
16717 /* Don't create location wrapper nodes within a template-argument-list. */
16718 auto_suppress_location_wrappers sentinel
;
16720 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16721 parser
->in_template_argument_list_p
= true;
16722 /* Even if the template-id appears in an integral
16723 constant-expression, the contents of the argument list do
16725 saved_ice_p
= parser
->integral_constant_expression_p
;
16726 parser
->integral_constant_expression_p
= false;
16727 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16728 parser
->non_integral_constant_expression_p
= false;
16730 /* Parse the arguments. */
16736 /* Consume the comma. */
16737 cp_lexer_consume_token (parser
->lexer
);
16739 /* Parse the template-argument. */
16740 argument
= cp_parser_template_argument (parser
);
16742 /* If the next token is an ellipsis, we're expanding a template
16744 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16746 if (argument
== error_mark_node
)
16748 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16749 error_at (token
->location
,
16750 "expected parameter pack before %<...%>");
16752 /* Consume the `...' token. */
16753 cp_lexer_consume_token (parser
->lexer
);
16755 /* Make the argument into a TYPE_PACK_EXPANSION or
16756 EXPR_PACK_EXPANSION. */
16757 argument
= make_pack_expansion (argument
);
16760 if (n_args
== alloced
)
16764 if (arg_ary
== fixed_args
)
16766 arg_ary
= XNEWVEC (tree
, alloced
);
16767 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16770 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16772 arg_ary
[n_args
++] = argument
;
16774 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16776 vec
= make_tree_vec (n_args
);
16779 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16781 if (arg_ary
!= fixed_args
)
16783 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16784 parser
->integral_constant_expression_p
= saved_ice_p
;
16785 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16787 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16791 /* Parse a template-argument.
16794 assignment-expression
16798 The representation is that of an assignment-expression, type-id, or
16799 id-expression -- except that the qualified id-expression is
16800 evaluated, so that the value returned is either a DECL or an
16803 Although the standard says "assignment-expression", it forbids
16804 throw-expressions or assignments in the template argument.
16805 Therefore, we use "conditional-expression" instead. */
16808 cp_parser_template_argument (cp_parser
* parser
)
16813 bool maybe_type_id
= false;
16814 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16815 location_t loc
= 0;
16818 /* There's really no way to know what we're looking at, so we just
16819 try each alternative in order.
16823 In a template-argument, an ambiguity between a type-id and an
16824 expression is resolved to a type-id, regardless of the form of
16825 the corresponding template-parameter.
16827 Therefore, we try a type-id first. */
16828 cp_parser_parse_tentatively (parser
);
16829 argument
= cp_parser_template_type_arg (parser
);
16830 /* If there was no error parsing the type-id but the next token is a
16831 '>>', our behavior depends on which dialect of C++ we're
16832 parsing. In C++98, we probably found a typo for '> >'. But there
16833 are type-id which are also valid expressions. For instance:
16835 struct X { int operator >> (int); };
16836 template <int V> struct Foo {};
16839 Here 'X()' is a valid type-id of a function type, but the user just
16840 wanted to write the expression "X() >> 5". Thus, we remember that we
16841 found a valid type-id, but we still try to parse the argument as an
16842 expression to see what happens.
16844 In C++0x, the '>>' will be considered two separate '>'
16846 if (!cp_parser_error_occurred (parser
)
16847 && cxx_dialect
== cxx98
16848 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16850 maybe_type_id
= true;
16851 cp_parser_abort_tentative_parse (parser
);
16855 /* If the next token isn't a `,' or a `>', then this argument wasn't
16856 really finished. This means that the argument is not a valid
16858 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16859 cp_parser_error (parser
, "expected template-argument");
16860 /* If that worked, we're done. */
16861 if (cp_parser_parse_definitely (parser
))
16864 /* We're still not sure what the argument will be. */
16865 cp_parser_parse_tentatively (parser
);
16866 /* Try a template. */
16867 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16868 argument
= cp_parser_id_expression (parser
,
16869 /*template_keyword_p=*/false,
16870 /*check_dependency_p=*/true,
16872 /*declarator_p=*/false,
16873 /*optional_p=*/false);
16874 /* If the next token isn't a `,' or a `>', then this argument wasn't
16875 really finished. */
16876 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16877 cp_parser_error (parser
, "expected template-argument");
16878 if (!cp_parser_error_occurred (parser
))
16880 /* Figure out what is being referred to. If the id-expression
16881 was for a class template specialization, then we will have a
16882 TYPE_DECL at this point. There is no need to do name lookup
16883 at this point in that case. */
16884 if (TREE_CODE (argument
) != TYPE_DECL
)
16885 argument
= cp_parser_lookup_name (parser
, argument
,
16887 /*is_template=*/template_p
,
16888 /*is_namespace=*/false,
16889 /*check_dependency=*/true,
16890 /*ambiguous_decls=*/NULL
,
16891 argument_start_token
->location
);
16892 /* Handle a constrained-type-specifier for a non-type template
16894 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16896 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16897 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16898 cp_parser_error (parser
, "expected template-name");
16900 if (cp_parser_parse_definitely (parser
))
16902 if (TREE_DEPRECATED (argument
))
16903 warn_deprecated_use (argument
, NULL_TREE
);
16906 /* It must be a non-type argument. In C++17 any constant-expression is
16908 if (cxx_dialect
> cxx14
)
16911 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16913 -- an integral constant-expression of integral or enumeration
16916 -- the name of a non-type template-parameter; or
16918 -- the name of an object or function with external linkage...
16920 -- the address of an object or function with external linkage...
16922 -- a pointer to member... */
16923 /* Look for a non-type template parameter. */
16924 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16926 cp_parser_parse_tentatively (parser
);
16927 argument
= cp_parser_primary_expression (parser
,
16928 /*address_p=*/false,
16930 /*template_arg_p=*/true,
16932 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16933 || !cp_parser_next_token_ends_template_argument_p (parser
))
16934 cp_parser_simulate_error (parser
);
16935 if (cp_parser_parse_definitely (parser
))
16939 /* If the next token is "&", the argument must be the address of an
16940 object or function with external linkage. */
16941 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16944 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16945 cp_lexer_consume_token (parser
->lexer
);
16947 /* See if we might have an id-expression. */
16948 token
= cp_lexer_peek_token (parser
->lexer
);
16949 if (token
->type
== CPP_NAME
16950 || token
->keyword
== RID_OPERATOR
16951 || token
->type
== CPP_SCOPE
16952 || token
->type
== CPP_TEMPLATE_ID
16953 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16955 cp_parser_parse_tentatively (parser
);
16956 argument
= cp_parser_primary_expression (parser
,
16959 /*template_arg_p=*/true,
16961 if (cp_parser_error_occurred (parser
)
16962 || !cp_parser_next_token_ends_template_argument_p (parser
))
16963 cp_parser_abort_tentative_parse (parser
);
16968 if (INDIRECT_REF_P (argument
))
16970 /* Strip the dereference temporarily. */
16971 gcc_assert (REFERENCE_REF_P (argument
));
16972 argument
= TREE_OPERAND (argument
, 0);
16975 /* If we're in a template, we represent a qualified-id referring
16976 to a static data member as a SCOPE_REF even if the scope isn't
16977 dependent so that we can check access control later. */
16979 if (TREE_CODE (probe
) == SCOPE_REF
)
16980 probe
= TREE_OPERAND (probe
, 1);
16983 /* A variable without external linkage might still be a
16984 valid constant-expression, so no error is issued here
16985 if the external-linkage check fails. */
16986 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16987 cp_parser_simulate_error (parser
);
16989 else if (is_overloaded_fn (argument
))
16990 /* All overloaded functions are allowed; if the external
16991 linkage test does not pass, an error will be issued
16995 && (TREE_CODE (argument
) == OFFSET_REF
16996 || TREE_CODE (argument
) == SCOPE_REF
))
16997 /* A pointer-to-member. */
16999 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
17002 cp_parser_simulate_error (parser
);
17004 if (cp_parser_parse_definitely (parser
))
17007 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
17008 tf_warning_or_error
);
17010 argument
= convert_from_reference (argument
);
17015 /* If the argument started with "&", there are no other valid
17016 alternatives at this point. */
17019 cp_parser_error (parser
, "invalid non-type template argument");
17020 return error_mark_node
;
17024 /* If the argument wasn't successfully parsed as a type-id followed
17025 by '>>', the argument can only be a constant expression now.
17026 Otherwise, we try parsing the constant-expression tentatively,
17027 because the argument could really be a type-id. */
17029 cp_parser_parse_tentatively (parser
);
17031 if (cxx_dialect
<= cxx14
)
17032 argument
= cp_parser_constant_expression (parser
);
17035 /* In C++20, we can encounter a braced-init-list. */
17036 if (cxx_dialect
>= cxx2a
17037 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17039 bool expr_non_constant_p
;
17040 return cp_parser_braced_list (parser
, &expr_non_constant_p
);
17043 /* With C++17 generalized non-type template arguments we need to handle
17044 lvalue constant expressions, too. */
17045 argument
= cp_parser_assignment_expression (parser
);
17046 require_potential_constant_expression (argument
);
17049 if (!maybe_type_id
)
17051 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17052 cp_parser_error (parser
, "expected template-argument");
17053 if (cp_parser_parse_definitely (parser
))
17055 /* We did our best to parse the argument as a non type-id, but that
17056 was the only alternative that matched (albeit with a '>' after
17057 it). We can assume it's just a typo from the user, and a
17058 diagnostic will then be issued. */
17059 return cp_parser_template_type_arg (parser
);
17062 /* Parse an explicit-instantiation.
17064 explicit-instantiation:
17065 template declaration
17067 Although the standard says `declaration', what it really means is:
17069 explicit-instantiation:
17070 template decl-specifier-seq [opt] declarator [opt] ;
17072 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17073 supposed to be allowed. A defect report has been filed about this
17078 explicit-instantiation:
17079 storage-class-specifier template
17080 decl-specifier-seq [opt] declarator [opt] ;
17081 function-specifier template
17082 decl-specifier-seq [opt] declarator [opt] ; */
17085 cp_parser_explicit_instantiation (cp_parser
* parser
)
17087 int declares_class_or_enum
;
17088 cp_decl_specifier_seq decl_specifiers
;
17089 tree extension_specifier
= NULL_TREE
;
17091 timevar_push (TV_TEMPLATE_INST
);
17093 /* Look for an (optional) storage-class-specifier or
17094 function-specifier. */
17095 if (cp_parser_allow_gnu_extensions_p (parser
))
17097 extension_specifier
17098 = cp_parser_storage_class_specifier_opt (parser
);
17099 if (!extension_specifier
)
17100 extension_specifier
17101 = cp_parser_function_specifier_opt (parser
,
17102 /*decl_specs=*/NULL
);
17105 /* Look for the `template' keyword. */
17106 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17107 /* Let the front end know that we are processing an explicit
17109 begin_explicit_instantiation ();
17110 /* [temp.explicit] says that we are supposed to ignore access
17111 control while processing explicit instantiation directives. */
17112 push_deferring_access_checks (dk_no_check
);
17113 /* Parse a decl-specifier-seq. */
17114 cp_parser_decl_specifier_seq (parser
,
17115 CP_PARSER_FLAGS_OPTIONAL
,
17117 &declares_class_or_enum
);
17118 /* If there was exactly one decl-specifier, and it declared a class,
17119 and there's no declarator, then we have an explicit type
17121 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
17125 type
= check_tag_decl (&decl_specifiers
,
17126 /*explicit_type_instantiation_p=*/true);
17127 /* Turn access control back on for names used during
17128 template instantiation. */
17129 pop_deferring_access_checks ();
17131 do_type_instantiation (type
, extension_specifier
,
17132 /*complain=*/tf_error
);
17136 cp_declarator
*declarator
;
17139 /* Parse the declarator. */
17141 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17142 CP_PARSER_FLAGS_NONE
,
17143 /*ctor_dtor_or_conv_p=*/NULL
,
17144 /*parenthesized_p=*/NULL
,
17145 /*member_p=*/false,
17146 /*friend_p=*/false,
17147 /*static_p=*/false);
17148 if (declares_class_or_enum
& 2)
17149 cp_parser_check_for_definition_in_return_type (declarator
,
17150 decl_specifiers
.type
,
17151 decl_specifiers
.locations
[ds_type_spec
]);
17152 if (declarator
!= cp_error_declarator
)
17154 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
17155 permerror (decl_specifiers
.locations
[ds_inline
],
17156 "explicit instantiation shall not use"
17157 " %<inline%> specifier");
17158 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
17159 permerror (decl_specifiers
.locations
[ds_constexpr
],
17160 "explicit instantiation shall not use"
17161 " %<constexpr%> specifier");
17163 decl
= grokdeclarator (declarator
, &decl_specifiers
,
17164 NORMAL
, 0, &decl_specifiers
.attributes
);
17165 /* Turn access control back on for names used during
17166 template instantiation. */
17167 pop_deferring_access_checks ();
17168 /* Do the explicit instantiation. */
17169 do_decl_instantiation (decl
, extension_specifier
);
17173 pop_deferring_access_checks ();
17174 /* Skip the body of the explicit instantiation. */
17175 cp_parser_skip_to_end_of_statement (parser
);
17178 /* We're done with the instantiation. */
17179 end_explicit_instantiation ();
17181 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17183 timevar_pop (TV_TEMPLATE_INST
);
17186 /* Parse an explicit-specialization.
17188 explicit-specialization:
17189 template < > declaration
17191 Although the standard says `declaration', what it really means is:
17193 explicit-specialization:
17194 template <> decl-specifier [opt] init-declarator [opt] ;
17195 template <> function-definition
17196 template <> explicit-specialization
17197 template <> template-declaration */
17200 cp_parser_explicit_specialization (cp_parser
* parser
)
17202 bool need_lang_pop
;
17203 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17205 /* Look for the `template' keyword. */
17206 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17207 /* Look for the `<'. */
17208 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
17209 /* Look for the `>'. */
17210 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
17211 /* We have processed another parameter list. */
17212 ++parser
->num_template_parameter_lists
;
17215 A template ... explicit specialization ... shall not have C
17217 if (current_lang_name
== lang_name_c
)
17219 error_at (token
->location
, "template specialization with C linkage");
17220 maybe_show_extern_c_location ();
17221 /* Give it C++ linkage to avoid confusing other parts of the
17223 push_lang_context (lang_name_cplusplus
);
17224 need_lang_pop
= true;
17227 need_lang_pop
= false;
17228 /* Let the front end know that we are beginning a specialization. */
17229 if (!begin_specialization ())
17231 end_specialization ();
17235 /* If the next keyword is `template', we need to figure out whether
17236 or not we're looking a template-declaration. */
17237 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17239 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17240 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
17241 cp_parser_template_declaration_after_export (parser
,
17242 /*member_p=*/false);
17244 cp_parser_explicit_specialization (parser
);
17247 /* Parse the dependent declaration. */
17248 cp_parser_single_declaration (parser
,
17250 /*member_p=*/false,
17251 /*explicit_specialization_p=*/true,
17252 /*friend_p=*/NULL
);
17253 /* We're done with the specialization. */
17254 end_specialization ();
17255 /* For the erroneous case of a template with C linkage, we pushed an
17256 implicit C++ linkage scope; exit that scope now. */
17258 pop_lang_context ();
17259 /* We're done with this parameter list. */
17260 --parser
->num_template_parameter_lists
;
17263 /* Parse a type-specifier.
17266 simple-type-specifier
17269 elaborated-type-specifier
17277 Returns a representation of the type-specifier. For a
17278 class-specifier, enum-specifier, or elaborated-type-specifier, a
17279 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17281 The parser flags FLAGS is used to control type-specifier parsing.
17283 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17284 in a decl-specifier-seq.
17286 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17287 class-specifier, enum-specifier, or elaborated-type-specifier, then
17288 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17289 if a type is declared; 2 if it is defined. Otherwise, it is set to
17292 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17293 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17294 is set to FALSE. */
17297 cp_parser_type_specifier (cp_parser
* parser
,
17298 cp_parser_flags flags
,
17299 cp_decl_specifier_seq
*decl_specs
,
17300 bool is_declaration
,
17301 int* declares_class_or_enum
,
17302 bool* is_cv_qualifier
)
17304 tree type_spec
= NULL_TREE
;
17307 cp_decl_spec ds
= ds_last
;
17309 /* Assume this type-specifier does not declare a new type. */
17310 if (declares_class_or_enum
)
17311 *declares_class_or_enum
= 0;
17312 /* And that it does not specify a cv-qualifier. */
17313 if (is_cv_qualifier
)
17314 *is_cv_qualifier
= false;
17315 /* Peek at the next token. */
17316 token
= cp_lexer_peek_token (parser
->lexer
);
17318 /* If we're looking at a keyword, we can use that to guide the
17319 production we choose. */
17320 keyword
= token
->keyword
;
17324 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17325 goto elaborated_type_specifier
;
17327 /* Look for the enum-specifier. */
17328 type_spec
= cp_parser_enum_specifier (parser
);
17329 /* If that worked, we're done. */
17332 if (declares_class_or_enum
)
17333 *declares_class_or_enum
= 2;
17335 cp_parser_set_decl_spec_type (decl_specs
,
17338 /*type_definition_p=*/true);
17342 goto elaborated_type_specifier
;
17344 /* Any of these indicate either a class-specifier, or an
17345 elaborated-type-specifier. */
17349 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17350 goto elaborated_type_specifier
;
17352 /* Parse tentatively so that we can back up if we don't find a
17353 class-specifier. */
17354 cp_parser_parse_tentatively (parser
);
17355 /* Look for the class-specifier. */
17356 type_spec
= cp_parser_class_specifier (parser
);
17357 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17358 /* If that worked, we're done. */
17359 if (cp_parser_parse_definitely (parser
))
17361 if (declares_class_or_enum
)
17362 *declares_class_or_enum
= 2;
17364 cp_parser_set_decl_spec_type (decl_specs
,
17367 /*type_definition_p=*/true);
17371 /* Fall through. */
17372 elaborated_type_specifier
:
17373 /* We're declaring (not defining) a class or enum. */
17374 if (declares_class_or_enum
)
17375 *declares_class_or_enum
= 1;
17377 /* Fall through. */
17379 /* Look for an elaborated-type-specifier. */
17381 = (cp_parser_elaborated_type_specifier
17383 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17386 cp_parser_set_decl_spec_type (decl_specs
,
17389 /*type_definition_p=*/false);
17394 if (is_cv_qualifier
)
17395 *is_cv_qualifier
= true;
17400 if (is_cv_qualifier
)
17401 *is_cv_qualifier
= true;
17406 if (is_cv_qualifier
)
17407 *is_cv_qualifier
= true;
17411 /* The `__complex__' keyword is a GNU extension. */
17419 /* Handle simple keywords. */
17424 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17425 decl_specs
->any_specifiers_p
= true;
17427 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17430 /* If we do not already have a type-specifier, assume we are looking
17431 at a simple-type-specifier. */
17432 type_spec
= cp_parser_simple_type_specifier (parser
,
17436 /* If we didn't find a type-specifier, and a type-specifier was not
17437 optional in this context, issue an error message. */
17438 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17440 cp_parser_error (parser
, "expected type specifier");
17441 return error_mark_node
;
17447 /* Parse a simple-type-specifier.
17449 simple-type-specifier:
17450 :: [opt] nested-name-specifier [opt] type-name
17451 :: [opt] nested-name-specifier template template-id
17466 simple-type-specifier:
17468 decltype ( expression )
17471 __underlying_type ( type-id )
17475 nested-name-specifier(opt) template-name
17479 simple-type-specifier:
17481 __typeof__ unary-expression
17482 __typeof__ ( type-id )
17483 __typeof__ ( type-id ) { initializer-list , [opt] }
17485 Concepts Extension:
17487 simple-type-specifier:
17488 constrained-type-specifier
17490 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17491 appropriately updated. */
17494 cp_parser_simple_type_specifier (cp_parser
* parser
,
17495 cp_decl_specifier_seq
*decl_specs
,
17496 cp_parser_flags flags
)
17498 tree type
= NULL_TREE
;
17502 /* Peek at the next token. */
17503 token
= cp_lexer_peek_token (parser
->lexer
);
17505 /* If we're looking at a keyword, things are easy. */
17506 switch (token
->keyword
)
17510 decl_specs
->explicit_char_p
= true;
17511 type
= char_type_node
;
17514 type
= char8_type_node
;
17517 type
= char16_type_node
;
17520 type
= char32_type_node
;
17523 type
= wchar_type_node
;
17526 type
= boolean_type_node
;
17529 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17530 type
= short_integer_type_node
;
17534 decl_specs
->explicit_int_p
= true;
17535 type
= integer_type_node
;
17541 idx
= token
->keyword
- RID_INT_N_0
;
17542 if (! int_n_enabled_p
[idx
])
17546 decl_specs
->explicit_intN_p
= true;
17547 decl_specs
->int_n_idx
= idx
;
17549 type
= int_n_trees
[idx
].signed_type
;
17553 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17554 type
= long_integer_type_node
;
17557 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17558 type
= integer_type_node
;
17561 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17562 type
= unsigned_type_node
;
17565 type
= float_type_node
;
17568 type
= double_type_node
;
17571 type
= void_type_node
;
17575 maybe_warn_cpp0x (CPP0X_AUTO
);
17576 if (parser
->auto_is_implicit_function_template_parm_p
)
17578 /* The 'auto' might be the placeholder return type for a function decl
17579 with trailing return type. */
17580 bool have_trailing_return_fn_decl
= false;
17582 cp_parser_parse_tentatively (parser
);
17583 cp_lexer_consume_token (parser
->lexer
);
17584 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17585 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17586 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17587 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17589 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17591 cp_lexer_consume_token (parser
->lexer
);
17592 cp_parser_skip_to_closing_parenthesis (parser
,
17593 /*recovering*/false,
17595 /*consume_paren*/true);
17599 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17601 have_trailing_return_fn_decl
= true;
17605 cp_lexer_consume_token (parser
->lexer
);
17607 cp_parser_abort_tentative_parse (parser
);
17609 if (have_trailing_return_fn_decl
)
17611 type
= make_auto ();
17615 if (cxx_dialect
>= cxx14
)
17617 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17618 type
= TREE_TYPE (type
);
17621 type
= error_mark_node
;
17623 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17625 if (cxx_dialect
< cxx14
)
17626 error_at (token
->location
,
17627 "use of %<auto%> in lambda parameter declaration "
17628 "only available with "
17629 "-std=c++14 or -std=gnu++14");
17631 else if (cxx_dialect
< cxx14
)
17632 error_at (token
->location
,
17633 "use of %<auto%> in parameter declaration "
17634 "only available with "
17635 "-std=c++14 or -std=gnu++14");
17636 else if (!flag_concepts
)
17637 pedwarn (token
->location
, 0,
17638 "use of %<auto%> in parameter declaration "
17639 "only available with -fconcepts");
17642 type
= make_auto ();
17646 /* Since DR 743, decltype can either be a simple-type-specifier by
17647 itself or begin a nested-name-specifier. Parsing it will replace
17648 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17649 handling below decide what to do. */
17650 cp_parser_decltype (parser
);
17651 cp_lexer_set_token_position (parser
->lexer
, token
);
17655 /* Consume the `typeof' token. */
17656 cp_lexer_consume_token (parser
->lexer
);
17657 /* Parse the operand to `typeof'. */
17658 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17659 /* If it is not already a TYPE, take its type. */
17660 if (!TYPE_P (type
))
17661 type
= finish_typeof (type
);
17664 cp_parser_set_decl_spec_type (decl_specs
, type
,
17666 /*type_definition_p=*/false);
17670 case RID_UNDERLYING_TYPE
:
17671 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17673 cp_parser_set_decl_spec_type (decl_specs
, type
,
17675 /*type_definition_p=*/false);
17680 case RID_DIRECT_BASES
:
17681 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17683 cp_parser_set_decl_spec_type (decl_specs
, type
,
17685 /*type_definition_p=*/false);
17691 /* If token is an already-parsed decltype not followed by ::,
17692 it's a simple-type-specifier. */
17693 if (token
->type
== CPP_DECLTYPE
17694 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17696 type
= saved_checks_value (token
->u
.tree_check_value
);
17699 cp_parser_set_decl_spec_type (decl_specs
, type
,
17701 /*type_definition_p=*/false);
17702 /* Remember that we are handling a decltype in order to
17703 implement the resolution of DR 1510 when the argument
17704 isn't instantiation dependent. */
17705 decl_specs
->decltype_p
= true;
17707 cp_lexer_consume_token (parser
->lexer
);
17711 /* If the type-specifier was for a built-in type, we're done. */
17714 /* Record the type. */
17716 && (token
->keyword
!= RID_SIGNED
17717 && token
->keyword
!= RID_UNSIGNED
17718 && token
->keyword
!= RID_SHORT
17719 && token
->keyword
!= RID_LONG
))
17720 cp_parser_set_decl_spec_type (decl_specs
,
17723 /*type_definition_p=*/false);
17725 decl_specs
->any_specifiers_p
= true;
17727 /* Consume the token. */
17728 cp_lexer_consume_token (parser
->lexer
);
17730 if (type
== error_mark_node
)
17731 return error_mark_node
;
17733 /* There is no valid C++ program where a non-template type is
17734 followed by a "<". That usually indicates that the user thought
17735 that the type was a template. */
17736 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17739 return TYPE_NAME (type
);
17742 /* The type-specifier must be a user-defined type. */
17743 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17747 const bool typename_p
= (cxx_dialect
>= cxx2a
17748 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
17750 /* Don't gobble tokens or issue error messages if this is an
17751 optional type-specifier. */
17752 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17753 cp_parser_parse_tentatively (parser
);
17755 token
= cp_lexer_peek_token (parser
->lexer
);
17757 /* Look for the optional `::' operator. */
17759 = (cp_parser_global_scope_opt (parser
,
17760 /*current_scope_valid_p=*/false)
17762 /* Look for the nested-name specifier. */
17764 = (cp_parser_nested_name_specifier_opt (parser
,
17765 /*typename_keyword_p=*/false,
17766 /*check_dependency_p=*/true,
17768 /*is_declaration=*/false)
17770 /* If we have seen a nested-name-specifier, and the next token
17771 is `template', then we are using the template-id production. */
17773 && cp_parser_optional_template_keyword (parser
))
17775 /* Look for the template-id. */
17776 type
= cp_parser_template_id (parser
,
17777 /*template_keyword_p=*/true,
17778 /*check_dependency_p=*/true,
17780 /*is_declaration=*/false);
17781 /* If the template-id did not name a type, we are out of
17783 if (TREE_CODE (type
) != TYPE_DECL
)
17785 /* ...unless we pretend we have seen 'typename'. */
17787 type
= cp_parser_make_typename_type (parser
, type
,
17791 cp_parser_error (parser
, "expected template-id for type");
17796 /* Otherwise, look for a type-name. */
17798 type
= cp_parser_type_name (parser
, (qualified_p
&& typename_p
));
17800 /* Keep track of all name-lookups performed in class scopes. */
17804 && TREE_CODE (type
) == TYPE_DECL
17805 && identifier_p (DECL_NAME (type
)))
17806 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17807 /* If it didn't work out, we don't have a TYPE. */
17808 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17809 && !cp_parser_parse_definitely (parser
))
17811 if (!type
&& cxx_dialect
>= cxx17
)
17813 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17814 cp_parser_parse_tentatively (parser
);
17816 cp_parser_global_scope_opt (parser
,
17817 /*current_scope_valid_p=*/false);
17818 cp_parser_nested_name_specifier_opt (parser
,
17819 /*typename_keyword_p=*/false,
17820 /*check_dependency_p=*/true,
17822 /*is_declaration=*/false);
17823 tree name
= cp_parser_identifier (parser
);
17824 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17825 && parser
->scope
!= error_mark_node
)
17827 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17829 /*is_template=*/false,
17830 /*is_namespace=*/false,
17831 /*check_dependency=*/true,
17832 /*ambiguous_decls=*/NULL
,
17834 if (tmpl
&& tmpl
!= error_mark_node
17835 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17836 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17837 type
= make_template_placeholder (tmpl
);
17840 type
= error_mark_node
;
17841 if (!cp_parser_simulate_error (parser
))
17842 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17843 NLE_TYPE
, token
->location
);
17847 type
= error_mark_node
;
17849 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17850 && !cp_parser_parse_definitely (parser
))
17853 if (type
&& decl_specs
)
17854 cp_parser_set_decl_spec_type (decl_specs
, type
,
17856 /*type_definition_p=*/false);
17859 /* If we didn't get a type-name, issue an error message. */
17860 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17862 cp_parser_error (parser
, "expected type-name");
17863 return error_mark_node
;
17866 if (type
&& type
!= error_mark_node
)
17868 /* See if TYPE is an Objective-C type, and if so, parse and
17869 accept any protocol references following it. Do this before
17870 the cp_parser_check_for_invalid_template_id() call, because
17871 Objective-C types can be followed by '<...>' which would
17872 enclose protocol names rather than template arguments, and so
17873 everything is fine. */
17874 if (c_dialect_objc () && !parser
->scope
17875 && (objc_is_id (type
) || objc_is_class_name (type
)))
17877 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17878 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17880 /* Clobber the "unqualified" type previously entered into
17881 DECL_SPECS with the new, improved protocol-qualified version. */
17883 decl_specs
->type
= qual_type
;
17888 /* There is no valid C++ program where a non-template type is
17889 followed by a "<". That usually indicates that the user
17890 thought that the type was a template. */
17891 cp_parser_check_for_invalid_template_id (parser
, type
,
17899 /* Parse a type-name.
17905 simple-template-id [in c++0x]
17922 Returns a TYPE_DECL for the type. */
17925 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17929 /* We can't know yet whether it is a class-name or not. */
17930 cp_parser_parse_tentatively (parser
);
17931 /* Try a class-name. */
17932 type_decl
= cp_parser_class_name (parser
,
17933 typename_keyword_p
,
17934 /*template_keyword_p=*/false,
17936 /*check_dependency_p=*/true,
17937 /*class_head_p=*/false,
17938 /*is_declaration=*/false);
17939 /* If it's not a class-name, keep looking. */
17940 if (!cp_parser_parse_definitely (parser
))
17942 if (cxx_dialect
< cxx11
)
17943 /* It must be a typedef-name or an enum-name. */
17944 return cp_parser_nonclass_name (parser
);
17946 cp_parser_parse_tentatively (parser
);
17947 /* It is either a simple-template-id representing an
17948 instantiation of an alias template... */
17949 type_decl
= cp_parser_template_id (parser
,
17950 /*template_keyword_p=*/false,
17951 /*check_dependency_p=*/true,
17953 /*is_declaration=*/false);
17954 /* Note that this must be an instantiation of an alias template
17955 because [temp.names]/6 says:
17957 A template-id that names an alias template specialization
17960 Whereas [temp.names]/7 says:
17962 A simple-template-id that names a class template
17963 specialization is a class-name.
17965 With concepts, this could also be a partial-concept-id that
17966 declares a non-type template parameter. */
17967 if (type_decl
!= NULL_TREE
17968 && TREE_CODE (type_decl
) == TYPE_DECL
17969 && TYPE_DECL_ALIAS_P (type_decl
))
17970 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17971 else if (is_constrained_parameter (type_decl
))
17972 /* Don't do anything. */ ;
17974 cp_parser_simulate_error (parser
);
17976 if (!cp_parser_parse_definitely (parser
))
17977 /* ... Or a typedef-name or an enum-name. */
17978 return cp_parser_nonclass_name (parser
);
17984 /* Check if DECL and ARGS can form a constrained-type-specifier.
17985 If ARGS is non-null, we try to form a concept check of the
17986 form DECL<?, ARGS> where ? is a wildcard that matches any
17987 kind of template argument. If ARGS is NULL, then we try to
17988 form a concept check of the form DECL<?>. */
17991 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17992 tree decl
, tree args
)
17994 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17996 /* If we a constrained-type-specifier cannot be deduced. */
17997 if (parser
->prevent_constrained_type_specifiers
)
18000 /* A constrained type specifier can only be found in an
18001 overload set or as a reference to a template declaration.
18003 FIXME: This might be masking a bug. It's possible that
18004 that the deduction below is causing template specializations
18005 to be formed with the wildcard as an argument. */
18006 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
18009 /* Try to build a call expression that evaluates the
18010 concept. This can fail if the overload set refers
18011 only to non-templates. */
18012 tree placeholder
= build_nt (WILDCARD_DECL
);
18013 tree check
= build_concept_check (decl
, placeholder
, args
);
18014 if (check
== error_mark_node
)
18017 /* Deduce the checked constraint and the prototype parameter.
18019 FIXME: In certain cases, failure to deduce should be a
18020 diagnosable error. */
18023 if (!deduce_constrained_parameter (check
, conc
, proto
))
18026 /* In template parameter scope, this results in a constrained
18027 parameter. Return a descriptor of that parm. */
18028 if (processing_template_parmlist
)
18029 return build_constrained_parameter (conc
, proto
, args
);
18031 /* In a parameter-declaration-clause, constrained-type
18032 specifiers result in invented template parameters. */
18033 if (parser
->auto_is_implicit_function_template_parm_p
)
18035 tree x
= build_constrained_parameter (conc
, proto
, args
);
18036 return synthesize_implicit_template_parm (parser
, x
);
18040 /* Otherwise, we're in a context where the constrained
18041 type name is deduced and the constraint applies
18042 after deduction. */
18043 return make_constrained_auto (conc
, args
);
18049 /* If DECL refers to a concept, return a TYPE_DECL representing
18050 the result of using the constrained type specifier in the
18051 current context. DECL refers to a concept if
18053 - it is an overload set containing a function concept taking a single
18056 - it is a variable concept taking a single type argument. */
18059 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
18062 && (TREE_CODE (decl
) == OVERLOAD
18063 || BASELINK_P (decl
)
18064 || variable_concept_p (decl
)))
18065 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
18070 /* Check if DECL and ARGS form a partial-concept-id. If so,
18071 assign ID to the resulting constrained placeholder.
18073 Returns true if the partial-concept-id designates a placeholder
18074 and false otherwise. Note that *id is set to NULL_TREE in
18078 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
18080 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
18083 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18095 Returns a TYPE_DECL for the type. */
18098 cp_parser_nonclass_name (cp_parser
* parser
)
18103 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18104 identifier
= cp_parser_identifier (parser
);
18105 if (identifier
== error_mark_node
)
18106 return error_mark_node
;
18108 /* Look up the type-name. */
18109 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
18111 type_decl
= strip_using_decl (type_decl
);
18113 /* If we found an overload set, then it may refer to a concept-name. */
18114 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
18117 if (TREE_CODE (type_decl
) != TYPE_DECL
18118 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
18120 /* See if this is an Objective-C type. */
18121 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18122 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
18124 type_decl
= TYPE_NAME (type
);
18127 /* Issue an error if we did not find a type-name. */
18128 if (TREE_CODE (type_decl
) != TYPE_DECL
18129 /* In Objective-C, we have the complication that class names are
18130 normally type names and start declarations (eg, the
18131 "NSObject" in "NSObject *object;"), but can be used in an
18132 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18133 is an expression. So, a classname followed by a dot is not a
18134 valid type-name. */
18135 || (objc_is_class_name (TREE_TYPE (type_decl
))
18136 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
18138 if (!cp_parser_simulate_error (parser
))
18139 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
18140 NLE_TYPE
, token
->location
);
18141 return error_mark_node
;
18143 /* Remember that the name was used in the definition of the
18144 current class so that we can check later to see if the
18145 meaning would have been different after the class was
18146 entirely defined. */
18147 else if (type_decl
!= error_mark_node
18149 maybe_note_name_used_in_class (identifier
, type_decl
);
18154 /* Parse an elaborated-type-specifier. Note that the grammar given
18155 here incorporates the resolution to DR68.
18157 elaborated-type-specifier:
18158 class-key :: [opt] nested-name-specifier [opt] identifier
18159 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18160 enum-key :: [opt] nested-name-specifier [opt] identifier
18161 typename :: [opt] nested-name-specifier identifier
18162 typename :: [opt] nested-name-specifier template [opt]
18167 elaborated-type-specifier:
18168 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18169 class-key attributes :: [opt] nested-name-specifier [opt]
18170 template [opt] template-id
18171 enum attributes :: [opt] nested-name-specifier [opt] identifier
18173 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18174 declared `friend'. If IS_DECLARATION is TRUE, then this
18175 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18176 something is being declared.
18178 Returns the TYPE specified. */
18181 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
18183 bool is_declaration
)
18185 enum tag_types tag_type
;
18187 tree type
= NULL_TREE
;
18188 tree attributes
= NULL_TREE
;
18190 cp_token
*token
= NULL
;
18192 /* See if we're looking at the `enum' keyword. */
18193 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
18195 /* Consume the `enum' token. */
18196 cp_lexer_consume_token (parser
->lexer
);
18197 /* Remember that it's an enumeration type. */
18198 tag_type
= enum_type
;
18199 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18200 enums) is used here. */
18201 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18202 if (cp_parser_is_keyword (token
, RID_CLASS
)
18203 || cp_parser_is_keyword (token
, RID_STRUCT
))
18205 gcc_rich_location
richloc (token
->location
);
18206 richloc
.add_range (input_location
);
18207 richloc
.add_fixit_remove ();
18208 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
18209 "a scoped enum must not use the %qD keyword",
18211 /* Consume the `struct' or `class' and parse it anyway. */
18212 cp_lexer_consume_token (parser
->lexer
);
18214 /* Parse the attributes. */
18215 attributes
= cp_parser_attributes_opt (parser
);
18217 /* Or, it might be `typename'. */
18218 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
18221 /* Consume the `typename' token. */
18222 cp_lexer_consume_token (parser
->lexer
);
18223 /* Remember that it's a `typename' type. */
18224 tag_type
= typename_type
;
18226 /* Otherwise it must be a class-key. */
18229 tag_type
= cp_parser_class_key (parser
);
18230 if (tag_type
== none_type
)
18231 return error_mark_node
;
18232 /* Parse the attributes. */
18233 attributes
= cp_parser_attributes_opt (parser
);
18236 /* Look for the `::' operator. */
18237 globalscope
= cp_parser_global_scope_opt (parser
,
18238 /*current_scope_valid_p=*/false);
18239 /* Look for the nested-name-specifier. */
18240 tree nested_name_specifier
;
18241 if (tag_type
== typename_type
&& !globalscope
)
18243 nested_name_specifier
18244 = cp_parser_nested_name_specifier (parser
,
18245 /*typename_keyword_p=*/true,
18246 /*check_dependency_p=*/true,
18249 if (!nested_name_specifier
)
18250 return error_mark_node
;
18253 /* Even though `typename' is not present, the proposed resolution
18254 to Core Issue 180 says that in `class A<T>::B', `B' should be
18255 considered a type-name, even if `A<T>' is dependent. */
18256 nested_name_specifier
18257 = cp_parser_nested_name_specifier_opt (parser
,
18258 /*typename_keyword_p=*/true,
18259 /*check_dependency_p=*/true,
18262 /* For everything but enumeration types, consider a template-id.
18263 For an enumeration type, consider only a plain identifier. */
18264 if (tag_type
!= enum_type
)
18266 bool template_p
= false;
18269 /* Allow the `template' keyword. */
18270 template_p
= cp_parser_optional_template_keyword (parser
);
18271 /* If we didn't see `template', we don't know if there's a
18272 template-id or not. */
18274 cp_parser_parse_tentatively (parser
);
18275 /* The `template' keyword must follow a nested-name-specifier. */
18276 else if (!nested_name_specifier
)
18278 cp_parser_error (parser
, "%<template%> must follow a nested-"
18280 return error_mark_node
;
18283 /* Parse the template-id. */
18284 token
= cp_lexer_peek_token (parser
->lexer
);
18285 decl
= cp_parser_template_id (parser
, template_p
,
18286 /*check_dependency_p=*/true,
18289 /* If we didn't find a template-id, look for an ordinary
18291 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18293 /* We can get here when cp_parser_template_id, called by
18294 cp_parser_class_name with tag_type == none_type, succeeds
18295 and caches a BASELINK. Then, when called again here,
18296 instead of failing and returning an error_mark_node
18297 returns it (see template/typename17.C in C++11).
18298 ??? Could we diagnose this earlier? */
18299 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18301 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18302 type
= error_mark_node
;
18304 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18305 in effect, then we must assume that, upon instantiation, the
18306 template will correspond to a class. */
18307 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18308 && tag_type
== typename_type
)
18309 type
= make_typename_type (parser
->scope
, decl
,
18311 /*complain=*/tf_error
);
18312 /* If the `typename' keyword is in effect and DECL is not a type
18313 decl, then type is non existent. */
18314 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18316 else if (TREE_CODE (decl
) == TYPE_DECL
)
18318 type
= check_elaborated_type_specifier (tag_type
, decl
,
18319 /*allow_template_p=*/true);
18321 /* If the next token is a semicolon, this must be a specialization,
18322 instantiation, or friend declaration. Check the scope while we
18323 still know whether or not we had a nested-name-specifier. */
18324 if (type
!= error_mark_node
18325 && !nested_name_specifier
&& !is_friend
18326 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18327 check_unqualified_spec_or_inst (type
, token
->location
);
18329 else if (decl
== error_mark_node
)
18330 type
= error_mark_node
;
18335 token
= cp_lexer_peek_token (parser
->lexer
);
18336 identifier
= cp_parser_identifier (parser
);
18338 if (identifier
== error_mark_node
)
18340 parser
->scope
= NULL_TREE
;
18341 return error_mark_node
;
18344 /* For a `typename', we needn't call xref_tag. */
18345 if (tag_type
== typename_type
18346 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18347 return cp_parser_make_typename_type (parser
, identifier
,
18350 /* Template parameter lists apply only if we are not within a
18351 function parameter list. */
18352 bool template_parm_lists_apply
18353 = parser
->num_template_parameter_lists
;
18354 if (template_parm_lists_apply
)
18355 for (cp_binding_level
*s
= current_binding_level
;
18356 s
&& s
->kind
!= sk_template_parms
;
18357 s
= s
->level_chain
)
18358 if (s
->kind
== sk_function_parms
)
18359 template_parm_lists_apply
= false;
18361 /* Look up a qualified name in the usual way. */
18365 tree ambiguous_decls
;
18367 decl
= cp_parser_lookup_name (parser
, identifier
,
18369 /*is_template=*/false,
18370 /*is_namespace=*/false,
18371 /*check_dependency=*/true,
18375 /* If the lookup was ambiguous, an error will already have been
18377 if (ambiguous_decls
)
18378 return error_mark_node
;
18380 /* If we are parsing friend declaration, DECL may be a
18381 TEMPLATE_DECL tree node here. However, we need to check
18382 whether this TEMPLATE_DECL results in valid code. Consider
18383 the following example:
18386 template <class T> class C {};
18389 template <class T> friend class N::C; // #1, valid code
18391 template <class T> class Y {
18392 friend class N::C; // #2, invalid code
18395 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18396 name lookup of `N::C'. We see that friend declaration must
18397 be template for the code to be valid. Note that
18398 processing_template_decl does not work here since it is
18399 always 1 for the above two cases. */
18401 decl
= (cp_parser_maybe_treat_template_as_class
18402 (decl
, /*tag_name_p=*/is_friend
18403 && template_parm_lists_apply
));
18405 if (TREE_CODE (decl
) != TYPE_DECL
)
18407 cp_parser_diagnose_invalid_type_name (parser
,
18410 return error_mark_node
;
18413 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18415 bool allow_template
= (template_parm_lists_apply
18416 || DECL_SELF_REFERENCE_P (decl
));
18417 type
= check_elaborated_type_specifier (tag_type
, decl
,
18420 if (type
== error_mark_node
)
18421 return error_mark_node
;
18424 /* Forward declarations of nested types, such as
18429 are invalid unless all components preceding the final '::'
18430 are complete. If all enclosing types are complete, these
18431 declarations become merely pointless.
18433 Invalid forward declarations of nested types are errors
18434 caught elsewhere in parsing. Those that are pointless arrive
18437 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18438 && !is_friend
&& !processing_explicit_instantiation
)
18439 warning (0, "declaration %qD does not declare anything", decl
);
18441 type
= TREE_TYPE (decl
);
18445 /* An elaborated-type-specifier sometimes introduces a new type and
18446 sometimes names an existing type. Normally, the rule is that it
18447 introduces a new type only if there is not an existing type of
18448 the same name already in scope. For example, given:
18451 void f() { struct S s; }
18453 the `struct S' in the body of `f' is the same `struct S' as in
18454 the global scope; the existing definition is used. However, if
18455 there were no global declaration, this would introduce a new
18456 local class named `S'.
18458 An exception to this rule applies to the following code:
18460 namespace N { struct S; }
18462 Here, the elaborated-type-specifier names a new type
18463 unconditionally; even if there is already an `S' in the
18464 containing scope this declaration names a new type.
18465 This exception only applies if the elaborated-type-specifier
18466 forms the complete declaration:
18470 A declaration consisting solely of `class-key identifier ;' is
18471 either a redeclaration of the name in the current scope or a
18472 forward declaration of the identifier as a class name. It
18473 introduces the name into the current scope.
18475 We are in this situation precisely when the next token is a `;'.
18477 An exception to the exception is that a `friend' declaration does
18478 *not* name a new type; i.e., given:
18480 struct S { friend struct T; };
18482 `T' is not a new type in the scope of `S'.
18484 Also, `new struct S' or `sizeof (struct S)' never results in the
18485 definition of a new type; a new type can only be declared in a
18486 declaration context. */
18492 /* Friends have special name lookup rules. */
18493 ts
= ts_within_enclosing_non_class
;
18494 else if (is_declaration
18495 && cp_lexer_next_token_is (parser
->lexer
,
18497 /* This is a `class-key identifier ;' */
18503 (template_parm_lists_apply
18504 && (cp_parser_next_token_starts_class_definition_p (parser
)
18505 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18506 /* An unqualified name was used to reference this type, so
18507 there were no qualifying templates. */
18508 if (template_parm_lists_apply
18509 && !cp_parser_check_template_parameters (parser
,
18510 /*num_templates=*/0,
18511 /*template_id*/false,
18513 /*declarator=*/NULL
))
18514 return error_mark_node
;
18515 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18519 if (type
== error_mark_node
)
18520 return error_mark_node
;
18522 /* Allow attributes on forward declarations of classes. */
18525 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18526 warning (OPT_Wattributes
,
18527 "attributes ignored on uninstantiated type");
18528 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18529 && ! processing_explicit_instantiation
)
18530 warning (OPT_Wattributes
,
18531 "attributes ignored on template instantiation");
18532 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18533 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18535 warning (OPT_Wattributes
,
18536 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18539 if (tag_type
!= enum_type
)
18541 /* Indicate whether this class was declared as a `class' or as a
18543 if (CLASS_TYPE_P (type
))
18544 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18545 cp_parser_check_class_key (tag_type
, type
);
18548 /* A "<" cannot follow an elaborated type specifier. If that
18549 happens, the user was probably trying to form a template-id. */
18550 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18556 /* Parse an enum-specifier.
18559 enum-head { enumerator-list [opt] }
18560 enum-head { enumerator-list , } [C++0x]
18563 enum-key identifier [opt] enum-base [opt]
18564 enum-key nested-name-specifier identifier enum-base [opt]
18569 enum struct [C++0x]
18572 : type-specifier-seq
18574 opaque-enum-specifier:
18575 enum-key identifier enum-base [opt] ;
18578 enum-key attributes[opt] identifier [opt] enum-base [opt]
18579 { enumerator-list [opt] }attributes[opt]
18580 enum-key attributes[opt] identifier [opt] enum-base [opt]
18581 { enumerator-list, }attributes[opt] [C++0x]
18583 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18584 if the token stream isn't an enum-specifier after all. */
18587 cp_parser_enum_specifier (cp_parser
* parser
)
18590 tree type
= NULL_TREE
;
18592 tree nested_name_specifier
= NULL_TREE
;
18594 bool scoped_enum_p
= false;
18595 bool has_underlying_type
= false;
18596 bool nested_being_defined
= false;
18597 bool new_value_list
= false;
18598 bool is_new_type
= false;
18599 bool is_unnamed
= false;
18600 tree underlying_type
= NULL_TREE
;
18601 cp_token
*type_start_token
= NULL
;
18602 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18604 parser
->colon_corrects_to_scope_p
= false;
18606 /* Parse tentatively so that we can back up if we don't find a
18608 cp_parser_parse_tentatively (parser
);
18610 /* Caller guarantees that the current token is 'enum', an identifier
18611 possibly follows, and the token after that is an opening brace.
18612 If we don't have an identifier, fabricate an anonymous name for
18613 the enumeration being defined. */
18614 cp_lexer_consume_token (parser
->lexer
);
18616 /* Parse the "class" or "struct", which indicates a scoped
18617 enumeration type in C++0x. */
18618 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18619 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18621 if (cxx_dialect
< cxx11
)
18622 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18624 /* Consume the `struct' or `class' token. */
18625 cp_lexer_consume_token (parser
->lexer
);
18627 scoped_enum_p
= true;
18630 attributes
= cp_parser_attributes_opt (parser
);
18632 /* Clear the qualification. */
18633 parser
->scope
= NULL_TREE
;
18634 parser
->qualifying_scope
= NULL_TREE
;
18635 parser
->object_scope
= NULL_TREE
;
18637 /* Figure out in what scope the declaration is being placed. */
18638 prev_scope
= current_scope ();
18640 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18642 push_deferring_access_checks (dk_no_check
);
18643 nested_name_specifier
18644 = cp_parser_nested_name_specifier_opt (parser
,
18645 /*typename_keyword_p=*/true,
18646 /*check_dependency_p=*/false,
18648 /*is_declaration=*/false);
18650 if (nested_name_specifier
)
18654 identifier
= cp_parser_identifier (parser
);
18655 name
= cp_parser_lookup_name (parser
, identifier
,
18657 /*is_template=*/false,
18658 /*is_namespace=*/false,
18659 /*check_dependency=*/true,
18660 /*ambiguous_decls=*/NULL
,
18662 if (name
&& name
!= error_mark_node
)
18664 type
= TREE_TYPE (name
);
18665 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18667 /* Are template enums allowed in ISO? */
18668 if (template_parm_scope_p ())
18669 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18670 "%qD is an enumeration template", name
);
18671 /* ignore a typename reference, for it will be solved by name
18676 else if (nested_name_specifier
== error_mark_node
)
18677 /* We already issued an error. */;
18680 error_at (type_start_token
->location
,
18681 "%qD does not name an enumeration in %qT",
18682 identifier
, nested_name_specifier
);
18683 nested_name_specifier
= error_mark_node
;
18688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18689 identifier
= cp_parser_identifier (parser
);
18692 identifier
= make_anon_name ();
18695 error_at (type_start_token
->location
,
18696 "unnamed scoped enum is not allowed");
18699 pop_deferring_access_checks ();
18701 /* Check for the `:' that denotes a specified underlying type in C++0x.
18702 Note that a ':' could also indicate a bitfield width, however. */
18703 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18705 cp_decl_specifier_seq type_specifiers
;
18707 /* Consume the `:'. */
18708 cp_lexer_consume_token (parser
->lexer
);
18710 /* Parse the type-specifier-seq. */
18711 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
18712 /*is_declaration=*/false,
18713 /*is_trailing_return=*/false,
18716 /* At this point this is surely not elaborated type specifier. */
18717 if (!cp_parser_parse_definitely (parser
))
18720 if (cxx_dialect
< cxx11
)
18721 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18723 has_underlying_type
= true;
18725 /* If that didn't work, stop. */
18726 if (type_specifiers
.type
!= error_mark_node
)
18728 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18729 /*initialized=*/0, NULL
);
18730 if (underlying_type
== error_mark_node
18731 || check_for_bare_parameter_packs (underlying_type
))
18732 underlying_type
= NULL_TREE
;
18736 /* Look for the `{' but don't consume it yet. */
18737 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18739 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18741 cp_parser_error (parser
, "expected %<{%>");
18742 if (has_underlying_type
)
18748 /* An opaque-enum-specifier must have a ';' here. */
18749 if ((scoped_enum_p
|| underlying_type
)
18750 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18752 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18753 if (has_underlying_type
)
18761 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18764 if (nested_name_specifier
)
18766 if (CLASS_TYPE_P (nested_name_specifier
))
18768 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18769 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18770 push_scope (nested_name_specifier
);
18772 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18774 push_nested_namespace (nested_name_specifier
);
18778 /* Issue an error message if type-definitions are forbidden here. */
18779 if (!cp_parser_check_type_definition (parser
))
18780 type
= error_mark_node
;
18782 /* Create the new type. We do this before consuming the opening
18783 brace so the enum will be recorded as being on the line of its
18784 tag (or the 'enum' keyword, if there is no tag). */
18785 type
= start_enum (identifier
, type
, underlying_type
,
18786 attributes
, scoped_enum_p
, &is_new_type
);
18788 /* If the next token is not '{' it is an opaque-enum-specifier or an
18789 elaborated-type-specifier. */
18790 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18792 timevar_push (TV_PARSE_ENUM
);
18793 if (nested_name_specifier
18794 && nested_name_specifier
!= error_mark_node
)
18796 /* The following catches invalid code such as:
18797 enum class S<int>::E { A, B, C }; */
18798 if (!processing_specialization
18799 && CLASS_TYPE_P (nested_name_specifier
)
18800 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18801 error_at (type_start_token
->location
, "cannot add an enumerator "
18802 "list to a template instantiation");
18804 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18806 error_at (type_start_token
->location
,
18807 "%<%T::%E%> has not been declared",
18808 TYPE_CONTEXT (nested_name_specifier
),
18809 nested_name_specifier
);
18810 type
= error_mark_node
;
18812 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18813 && !CLASS_TYPE_P (nested_name_specifier
))
18815 error_at (type_start_token
->location
, "nested name specifier "
18816 "%qT for enum declaration does not name a class "
18817 "or namespace", nested_name_specifier
);
18818 type
= error_mark_node
;
18820 /* If that scope does not contain the scope in which the
18821 class was originally declared, the program is invalid. */
18822 else if (prev_scope
&& !is_ancestor (prev_scope
,
18823 nested_name_specifier
))
18825 if (at_namespace_scope_p ())
18826 error_at (type_start_token
->location
,
18827 "declaration of %qD in namespace %qD which does not "
18829 type
, prev_scope
, nested_name_specifier
);
18831 error_at (type_start_token
->location
,
18832 "declaration of %qD in %qD which does not "
18834 type
, prev_scope
, nested_name_specifier
);
18835 type
= error_mark_node
;
18837 /* If that scope is the scope where the declaration is being placed
18838 the program is invalid. */
18839 else if (CLASS_TYPE_P (nested_name_specifier
)
18840 && CLASS_TYPE_P (prev_scope
)
18841 && same_type_p (nested_name_specifier
, prev_scope
))
18843 permerror (type_start_token
->location
,
18844 "extra qualification not allowed");
18845 nested_name_specifier
= NULL_TREE
;
18850 begin_scope (sk_scoped_enum
, type
);
18852 /* Consume the opening brace. */
18853 matching_braces braces
;
18854 braces
.consume_open (parser
);
18856 if (type
== error_mark_node
)
18857 ; /* Nothing to add */
18858 else if (OPAQUE_ENUM_P (type
)
18859 || (cxx_dialect
> cxx98
&& processing_specialization
))
18861 new_value_list
= true;
18862 SET_OPAQUE_ENUM_P (type
, false);
18863 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18867 error_at (type_start_token
->location
,
18868 "multiple definition of %q#T", type
);
18869 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18870 "previous definition here");
18871 type
= error_mark_node
;
18874 if (type
== error_mark_node
)
18875 cp_parser_skip_to_end_of_block_or_statement (parser
);
18876 /* If the next token is not '}', then there are some enumerators. */
18877 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18879 if (is_unnamed
&& !scoped_enum_p
)
18880 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18881 "ISO C++ forbids empty unnamed enum");
18884 cp_parser_enumerator_list (parser
, type
);
18886 /* Consume the final '}'. */
18887 braces
.require_close (parser
);
18891 timevar_pop (TV_PARSE_ENUM
);
18895 /* If a ';' follows, then it is an opaque-enum-specifier
18896 and additional restrictions apply. */
18897 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18900 error_at (type_start_token
->location
,
18901 "opaque-enum-specifier without name");
18902 else if (nested_name_specifier
)
18903 error_at (type_start_token
->location
,
18904 "opaque-enum-specifier must use a simple identifier");
18908 /* Look for trailing attributes to apply to this enumeration, and
18909 apply them if appropriate. */
18910 if (cp_parser_allow_gnu_extensions_p (parser
))
18912 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18913 cplus_decl_attributes (&type
,
18915 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18918 /* Finish up the enumeration. */
18919 if (type
!= error_mark_node
)
18921 if (new_value_list
)
18922 finish_enum_value_list (type
);
18924 finish_enum (type
);
18927 if (nested_name_specifier
)
18929 if (CLASS_TYPE_P (nested_name_specifier
))
18931 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18932 pop_scope (nested_name_specifier
);
18934 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18936 pop_nested_namespace (nested_name_specifier
);
18940 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18944 /* Parse an enumerator-list. The enumerators all have the indicated
18948 enumerator-definition
18949 enumerator-list , enumerator-definition */
18952 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18956 /* Parse an enumerator-definition. */
18957 cp_parser_enumerator_definition (parser
, type
);
18959 /* If the next token is not a ',', we've reached the end of
18961 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18963 /* Otherwise, consume the `,' and keep going. */
18964 cp_lexer_consume_token (parser
->lexer
);
18965 /* If the next token is a `}', there is a trailing comma. */
18966 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18968 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18969 pedwarn (input_location
, OPT_Wpedantic
,
18970 "comma at end of enumerator list");
18976 /* Parse an enumerator-definition. The enumerator has the indicated
18979 enumerator-definition:
18981 enumerator = constant-expression
18988 enumerator-definition:
18989 enumerator attributes [opt]
18990 enumerator attributes [opt] = constant-expression */
18993 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18999 /* Save the input location because we are interested in the location
19000 of the identifier and not the location of the explicit value. */
19001 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
19003 /* Look for the identifier. */
19004 identifier
= cp_parser_identifier (parser
);
19005 if (identifier
== error_mark_node
)
19008 /* Parse any specified attributes. */
19009 tree attrs
= cp_parser_attributes_opt (parser
);
19011 /* If the next token is an '=', then there is an explicit value. */
19012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19014 /* Consume the `=' token. */
19015 cp_lexer_consume_token (parser
->lexer
);
19016 /* Parse the value. */
19017 value
= cp_parser_constant_expression (parser
);
19022 /* If we are processing a template, make sure the initializer of the
19023 enumerator doesn't contain any bare template parameter pack. */
19024 if (check_for_bare_parameter_packs (value
))
19025 value
= error_mark_node
;
19027 /* Create the enumerator. */
19028 build_enumerator (identifier
, value
, type
, attrs
, loc
);
19031 /* Parse a namespace-name.
19034 original-namespace-name
19037 Returns the NAMESPACE_DECL for the namespace. */
19040 cp_parser_namespace_name (cp_parser
* parser
)
19043 tree namespace_decl
;
19045 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19047 /* Get the name of the namespace. */
19048 identifier
= cp_parser_identifier (parser
);
19049 if (identifier
== error_mark_node
)
19050 return error_mark_node
;
19052 /* Look up the identifier in the currently active scope. Look only
19053 for namespaces, due to:
19055 [basic.lookup.udir]
19057 When looking up a namespace-name in a using-directive or alias
19058 definition, only namespace names are considered.
19062 [basic.lookup.qual]
19064 During the lookup of a name preceding the :: scope resolution
19065 operator, object, function, and enumerator names are ignored.
19067 (Note that cp_parser_qualifying_entity only calls this
19068 function if the token after the name is the scope resolution
19070 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
19072 /*is_template=*/false,
19073 /*is_namespace=*/true,
19074 /*check_dependency=*/true,
19075 /*ambiguous_decls=*/NULL
,
19077 /* If it's not a namespace, issue an error. */
19078 if (namespace_decl
== error_mark_node
19079 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
19081 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
19083 auto_diagnostic_group d
;
19085 if (namespace_decl
== error_mark_node
19086 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
19087 hint
= suggest_alternative_in_explicit_scope (token
->location
,
19090 if (const char *suggestion
= hint
.suggestion ())
19092 gcc_rich_location
richloc (token
->location
);
19093 richloc
.add_fixit_replace (suggestion
);
19094 error_at (&richloc
,
19095 "%qD is not a namespace-name; did you mean %qs?",
19096 identifier
, suggestion
);
19099 error_at (token
->location
, "%qD is not a namespace-name",
19103 cp_parser_error (parser
, "expected namespace-name");
19104 namespace_decl
= error_mark_node
;
19107 return namespace_decl
;
19110 /* Parse a namespace-definition.
19112 namespace-definition:
19113 named-namespace-definition
19114 unnamed-namespace-definition
19116 named-namespace-definition:
19117 original-namespace-definition
19118 extension-namespace-definition
19120 original-namespace-definition:
19121 namespace identifier { namespace-body }
19123 extension-namespace-definition:
19124 namespace original-namespace-name { namespace-body }
19126 unnamed-namespace-definition:
19127 namespace { namespace-body } */
19130 cp_parser_namespace_definition (cp_parser
* parser
)
19133 int nested_definition_count
= 0;
19135 cp_ensure_no_omp_declare_simd (parser
);
19136 cp_ensure_no_oacc_routine (parser
);
19138 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
19139 const bool topmost_inline_p
= is_inline
;
19143 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
19144 cp_lexer_consume_token (parser
->lexer
);
19147 /* Look for the `namespace' keyword. */
19149 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19151 /* Parse any specified attributes before the identifier. */
19152 tree attribs
= cp_parser_attributes_opt (parser
);
19156 identifier
= NULL_TREE
;
19158 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
19160 if (nested_inline_p
&& nested_definition_count
!= 0)
19162 if (cxx_dialect
< cxx2a
)
19163 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
19164 OPT_Wpedantic
, "nested inline namespace definitions only "
19165 "available with -std=c++2a or -std=gnu++2a");
19166 cp_lexer_consume_token (parser
->lexer
);
19169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19171 identifier
= cp_parser_identifier (parser
);
19173 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19174 pedwarn (input_location
, OPT_Wpedantic
,
19175 "standard attributes on namespaces must precede "
19176 "the namespace name");
19178 /* Parse any attributes specified after the identifier. */
19179 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
19182 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
19184 /* Don't forget that the innermost namespace might have been
19185 marked as inline. Use |= because we cannot overwrite
19186 IS_INLINE in case the outermost namespace is inline, but
19187 there are no nested inlines. */
19188 is_inline
|= nested_inline_p
;
19192 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
19193 pedwarn (input_location
, OPT_Wpedantic
,
19194 "nested namespace definitions only available with "
19195 "-std=c++17 or -std=gnu++17");
19197 /* Nested namespace names can create new namespaces (unlike
19198 other qualified-ids). */
19199 if (int count
= (identifier
19200 ? push_namespace (identifier
, nested_inline_p
)
19202 nested_definition_count
+= count
;
19204 cp_parser_error (parser
, "nested namespace name required");
19205 cp_lexer_consume_token (parser
->lexer
);
19208 if (nested_definition_count
&& !identifier
)
19209 cp_parser_error (parser
, "namespace name required");
19211 if (nested_definition_count
&& attribs
)
19212 error_at (token
->location
,
19213 "a nested namespace definition cannot have attributes");
19214 if (nested_definition_count
&& topmost_inline_p
)
19215 error_at (token
->location
,
19216 "a nested namespace definition cannot be inline");
19218 /* Start the namespace. */
19219 nested_definition_count
+= push_namespace (identifier
, is_inline
);
19221 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
19223 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
19225 /* Look for the `{' to validate starting the namespace. */
19226 matching_braces braces
;
19227 if (braces
.require_open (parser
))
19229 /* Parse the body of the namespace. */
19230 cp_parser_namespace_body (parser
);
19232 /* Look for the final `}'. */
19233 braces
.require_close (parser
);
19236 if (has_visibility
)
19237 pop_visibility (1);
19239 /* Pop the nested namespace definitions. */
19240 while (nested_definition_count
--)
19244 /* Parse a namespace-body.
19247 declaration-seq [opt] */
19250 cp_parser_namespace_body (cp_parser
* parser
)
19252 cp_parser_declaration_seq_opt (parser
);
19255 /* Parse a namespace-alias-definition.
19257 namespace-alias-definition:
19258 namespace identifier = qualified-namespace-specifier ; */
19261 cp_parser_namespace_alias_definition (cp_parser
* parser
)
19264 tree namespace_specifier
;
19266 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19268 /* Look for the `namespace' keyword. */
19269 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19270 /* Look for the identifier. */
19271 identifier
= cp_parser_identifier (parser
);
19272 if (identifier
== error_mark_node
)
19274 /* Look for the `=' token. */
19275 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
19276 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19278 error_at (token
->location
, "%<namespace%> definition is not allowed here");
19279 /* Skip the definition. */
19280 cp_lexer_consume_token (parser
->lexer
);
19281 if (cp_parser_skip_to_closing_brace (parser
))
19282 cp_lexer_consume_token (parser
->lexer
);
19285 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19286 /* Look for the qualified-namespace-specifier. */
19287 namespace_specifier
19288 = cp_parser_qualified_namespace_specifier (parser
);
19289 /* Look for the `;' token. */
19290 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19292 /* Register the alias in the symbol table. */
19293 do_namespace_alias (identifier
, namespace_specifier
);
19296 /* Parse a qualified-namespace-specifier.
19298 qualified-namespace-specifier:
19299 :: [opt] nested-name-specifier [opt] namespace-name
19301 Returns a NAMESPACE_DECL corresponding to the specified
19305 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
19307 /* Look for the optional `::'. */
19308 cp_parser_global_scope_opt (parser
,
19309 /*current_scope_valid_p=*/false);
19311 /* Look for the optional nested-name-specifier. */
19312 cp_parser_nested_name_specifier_opt (parser
,
19313 /*typename_keyword_p=*/false,
19314 /*check_dependency_p=*/true,
19316 /*is_declaration=*/true);
19318 return cp_parser_namespace_name (parser
);
19321 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19322 access declaration.
19325 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19326 using :: unqualified-id ;
19328 access-declaration:
19334 cp_parser_using_declaration (cp_parser
* parser
,
19335 bool access_declaration_p
)
19338 bool typename_p
= false;
19339 bool global_scope_p
;
19343 int oldcount
= errorcount
;
19344 cp_token
*diag_token
= NULL
;
19346 if (access_declaration_p
)
19348 diag_token
= cp_lexer_peek_token (parser
->lexer
);
19349 cp_parser_parse_tentatively (parser
);
19353 /* Look for the `using' keyword. */
19354 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19357 /* Peek at the next token. */
19358 token
= cp_lexer_peek_token (parser
->lexer
);
19359 /* See if it's `typename'. */
19360 if (token
->keyword
== RID_TYPENAME
)
19362 /* Remember that we've seen it. */
19364 /* Consume the `typename' token. */
19365 cp_lexer_consume_token (parser
->lexer
);
19369 /* Look for the optional global scope qualification. */
19371 = (cp_parser_global_scope_opt (parser
,
19372 /*current_scope_valid_p=*/false)
19375 /* If we saw `typename', or didn't see `::', then there must be a
19376 nested-name-specifier present. */
19377 if (typename_p
|| !global_scope_p
)
19379 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19380 /*check_dependency_p=*/true,
19382 /*is_declaration=*/true);
19383 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19385 cp_parser_skip_to_end_of_block_or_statement (parser
);
19389 /* Otherwise, we could be in either of the two productions. In that
19390 case, treat the nested-name-specifier as optional. */
19392 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19393 /*typename_keyword_p=*/false,
19394 /*check_dependency_p=*/true,
19396 /*is_declaration=*/true);
19398 qscope
= global_namespace
;
19399 else if (UNSCOPED_ENUM_P (qscope
))
19400 qscope
= CP_TYPE_CONTEXT (qscope
);
19402 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19403 /* Something has already gone wrong; there's no need to parse
19404 further. Since an error has occurred, the return value of
19405 cp_parser_parse_definitely will be false, as required. */
19406 return cp_parser_parse_definitely (parser
);
19408 token
= cp_lexer_peek_token (parser
->lexer
);
19409 /* Parse the unqualified-id. */
19410 identifier
= cp_parser_unqualified_id (parser
,
19411 /*template_keyword_p=*/false,
19412 /*check_dependency_p=*/true,
19413 /*declarator_p=*/true,
19414 /*optional_p=*/false);
19416 if (access_declaration_p
)
19418 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19419 cp_parser_simulate_error (parser
);
19420 if (!cp_parser_parse_definitely (parser
))
19423 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19425 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19426 if (cxx_dialect
< cxx17
19427 && !in_system_header_at (ell
->location
))
19428 pedwarn (ell
->location
, 0,
19429 "pack expansion in using-declaration only available "
19430 "with -std=c++17 or -std=gnu++17");
19431 qscope
= make_pack_expansion (qscope
);
19434 /* The function we call to handle a using-declaration is different
19435 depending on what scope we are in. */
19436 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19438 else if (!identifier_p (identifier
)
19439 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19440 /* [namespace.udecl]
19442 A using declaration shall not name a template-id. */
19443 error_at (token
->location
,
19444 "a template-id may not appear in a using-declaration");
19447 if (at_class_scope_p ())
19449 /* Create the USING_DECL. */
19450 decl
= do_class_using_decl (qscope
, identifier
);
19452 if (decl
&& typename_p
)
19453 USING_DECL_TYPENAME_P (decl
) = 1;
19455 if (check_for_bare_parameter_packs (decl
))
19457 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19461 /* Add it to the list of members in this class. */
19462 finish_member_declaration (decl
);
19466 decl
= cp_parser_lookup_name_simple (parser
,
19469 if (decl
== error_mark_node
)
19470 cp_parser_name_lookup_error (parser
, identifier
,
19473 else if (check_for_bare_parameter_packs (decl
))
19475 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19478 else if (!at_namespace_scope_p ())
19479 finish_local_using_decl (decl
, qscope
, identifier
);
19481 finish_namespace_using_decl (decl
, qscope
, identifier
);
19485 if (!access_declaration_p
19486 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19488 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19489 if (cxx_dialect
< cxx17
)
19490 pedwarn (comma
->location
, 0,
19491 "comma-separated list in using-declaration only available "
19492 "with -std=c++17 or -std=gnu++17");
19496 /* Look for the final `;'. */
19497 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19499 if (access_declaration_p
&& errorcount
== oldcount
)
19500 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19501 "access declarations are deprecated "
19502 "in favour of using-declarations; "
19503 "suggestion: add the %<using%> keyword");
19508 /* Parse an alias-declaration.
19511 using identifier attribute-specifier-seq [opt] = type-id */
19514 cp_parser_alias_declaration (cp_parser
* parser
)
19516 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19517 location_t id_location
, type_location
;
19518 cp_declarator
*declarator
;
19519 cp_decl_specifier_seq decl_specs
;
19521 const char *saved_message
= NULL
;
19523 /* Look for the `using' keyword. */
19524 cp_token
*using_token
19525 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19526 if (using_token
== NULL
)
19527 return error_mark_node
;
19529 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19530 id
= cp_parser_identifier (parser
);
19531 if (id
== error_mark_node
)
19532 return error_mark_node
;
19534 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19535 attributes
= cp_parser_attributes_opt (parser
);
19536 if (attributes
== error_mark_node
)
19537 return error_mark_node
;
19539 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19541 if (cp_parser_error_occurred (parser
))
19542 return error_mark_node
;
19544 cp_parser_commit_to_tentative_parse (parser
);
19546 /* Now we are going to parse the type-id of the declaration. */
19551 "A type-specifier-seq shall not define a class or enumeration
19552 unless it appears in the type-id of an alias-declaration (7.1.3) that
19553 is not the declaration of a template-declaration."
19555 In other words, if we currently are in an alias template, the
19556 type-id should not define a type.
19558 So let's set parser->type_definition_forbidden_message in that
19559 case; cp_parser_check_type_definition (called by
19560 cp_parser_class_specifier) will then emit an error if a type is
19561 defined in the type-id. */
19562 if (parser
->num_template_parameter_lists
)
19564 saved_message
= parser
->type_definition_forbidden_message
;
19565 parser
->type_definition_forbidden_message
=
19566 G_("types may not be defined in alias template declarations");
19569 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
19572 /* Restore the error message if need be. */
19573 if (parser
->num_template_parameter_lists
)
19574 parser
->type_definition_forbidden_message
= saved_message
;
19576 if (type
== error_mark_node
19577 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19579 cp_parser_skip_to_end_of_block_or_statement (parser
);
19580 return error_mark_node
;
19583 /* A typedef-name can also be introduced by an alias-declaration. The
19584 identifier following the using keyword becomes a typedef-name. It has
19585 the same semantics as if it were introduced by the typedef
19586 specifier. In particular, it does not define a new type and it shall
19587 not appear in the type-id. */
19589 clear_decl_specs (&decl_specs
);
19590 decl_specs
.type
= type
;
19591 if (attributes
!= NULL_TREE
)
19593 decl_specs
.attributes
= attributes
;
19594 set_and_check_decl_spec_loc (&decl_specs
,
19598 set_and_check_decl_spec_loc (&decl_specs
,
19601 set_and_check_decl_spec_loc (&decl_specs
,
19604 decl_specs
.locations
[ds_type_spec
] = type_location
;
19606 if (parser
->num_template_parameter_lists
19607 && !cp_parser_check_template_parameters (parser
,
19608 /*num_templates=*/0,
19609 /*template_id*/false,
19611 /*declarator=*/NULL
))
19612 return error_mark_node
;
19614 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
19616 member_p
= at_class_scope_p ();
19618 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19619 NULL_TREE
, attributes
);
19621 decl
= start_decl (declarator
, &decl_specs
, 0,
19622 attributes
, NULL_TREE
, &pushed_scope
);
19623 if (decl
== error_mark_node
)
19626 // Attach constraints to the alias declaration.
19627 if (flag_concepts
&& current_template_parms
)
19629 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19630 tree constr
= build_constraints (reqs
, NULL_TREE
);
19631 set_constraints (decl
, constr
);
19634 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19637 pop_scope (pushed_scope
);
19639 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19640 added into the symbol table; otherwise, return the TYPE_DECL. */
19641 if (DECL_LANG_SPECIFIC (decl
)
19642 && DECL_TEMPLATE_INFO (decl
)
19643 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19645 decl
= DECL_TI_TEMPLATE (decl
);
19647 check_member_template (decl
);
19653 /* Parse a using-directive.
19656 using namespace :: [opt] nested-name-specifier [opt]
19657 namespace-name ; */
19660 cp_parser_using_directive (cp_parser
* parser
)
19662 tree namespace_decl
;
19665 /* Look for the `using' keyword. */
19666 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19667 /* And the `namespace' keyword. */
19668 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19669 /* Look for the optional `::' operator. */
19670 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19671 /* And the optional nested-name-specifier. */
19672 cp_parser_nested_name_specifier_opt (parser
,
19673 /*typename_keyword_p=*/false,
19674 /*check_dependency_p=*/true,
19676 /*is_declaration=*/true);
19677 /* Get the namespace being used. */
19678 namespace_decl
= cp_parser_namespace_name (parser
);
19679 /* And any specified attributes. */
19680 attribs
= cp_parser_attributes_opt (parser
);
19682 /* Update the symbol table. */
19683 if (namespace_bindings_p ())
19684 finish_namespace_using_directive (namespace_decl
, attribs
);
19686 finish_local_using_directive (namespace_decl
, attribs
);
19688 /* Look for the final `;'. */
19689 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19692 /* Parse an asm-definition.
19699 asm-qualifier-list:
19701 asm-qualifier-list asm-qualifier
19704 asm ( string-literal ) ;
19709 asm asm-qualifier-list [opt] ( string-literal ) ;
19710 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19711 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19712 : asm-operand-list [opt] ) ;
19713 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19714 : asm-operand-list [opt]
19715 : asm-clobber-list [opt] ) ;
19716 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19717 : asm-clobber-list [opt]
19718 : asm-goto-list ) ;
19720 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19721 contains goto, and is the only allowed form in that case. No duplicates are
19722 allowed in an asm-qualifier-list. */
19725 cp_parser_asm_definition (cp_parser
* parser
)
19728 tree outputs
= NULL_TREE
;
19729 tree inputs
= NULL_TREE
;
19730 tree clobbers
= NULL_TREE
;
19731 tree labels
= NULL_TREE
;
19733 bool extended_p
= false;
19734 bool invalid_inputs_p
= false;
19735 bool invalid_outputs_p
= false;
19736 required_token missing
= RT_NONE
;
19738 /* Look for the `asm' keyword. */
19739 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19741 if (parser
->in_function_body
19742 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19744 error ("%<asm%> in %<constexpr%> function");
19745 cp_function_chain
->invalid_constexpr
= true;
19748 /* Handle the asm-qualifier-list. */
19749 location_t volatile_loc
= UNKNOWN_LOCATION
;
19750 location_t inline_loc
= UNKNOWN_LOCATION
;
19751 location_t goto_loc
= UNKNOWN_LOCATION
;
19753 if (cp_parser_allow_gnu_extensions_p (parser
) && parser
->in_function_body
)
19756 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19757 location_t loc
= token
->location
;
19758 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
19763 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19764 inform (volatile_loc
, "first seen here");
19767 volatile_loc
= loc
;
19768 cp_lexer_consume_token (parser
->lexer
);
19774 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19775 inform (inline_loc
, "first seen here");
19779 cp_lexer_consume_token (parser
->lexer
);
19785 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19786 inform (goto_loc
, "first seen here");
19790 cp_lexer_consume_token (parser
->lexer
);
19795 error_at (loc
, "%qT is not an asm qualifier", token
->u
.value
);
19796 cp_lexer_consume_token (parser
->lexer
);
19805 bool volatile_p
= (volatile_loc
!= UNKNOWN_LOCATION
);
19806 bool inline_p
= (inline_loc
!= UNKNOWN_LOCATION
);
19807 bool goto_p
= (goto_loc
!= UNKNOWN_LOCATION
);
19809 /* Look for the opening `('. */
19810 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19812 /* Look for the string. */
19813 string
= cp_parser_string_literal (parser
, false, false);
19814 if (string
== error_mark_node
)
19816 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19817 /*consume_paren=*/true);
19821 /* If we're allowing GNU extensions, check for the extended assembly
19822 syntax. Unfortunately, the `:' tokens need not be separated by
19823 a space in C, and so, for compatibility, we tolerate that here
19824 too. Doing that means that we have to treat the `::' operator as
19826 if (cp_parser_allow_gnu_extensions_p (parser
)
19827 && parser
->in_function_body
19828 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19829 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19831 bool inputs_p
= false;
19832 bool clobbers_p
= false;
19833 bool labels_p
= false;
19835 /* The extended syntax was used. */
19838 /* Look for outputs. */
19839 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19841 /* Consume the `:'. */
19842 cp_lexer_consume_token (parser
->lexer
);
19843 /* Parse the output-operands. */
19844 if (cp_lexer_next_token_is_not (parser
->lexer
,
19846 && cp_lexer_next_token_is_not (parser
->lexer
,
19848 && cp_lexer_next_token_is_not (parser
->lexer
,
19852 outputs
= cp_parser_asm_operand_list (parser
);
19853 if (outputs
== error_mark_node
)
19854 invalid_outputs_p
= true;
19857 /* If the next token is `::', there are no outputs, and the
19858 next token is the beginning of the inputs. */
19859 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19860 /* The inputs are coming next. */
19863 /* Look for inputs. */
19865 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19867 /* Consume the `:' or `::'. */
19868 cp_lexer_consume_token (parser
->lexer
);
19869 /* Parse the output-operands. */
19870 if (cp_lexer_next_token_is_not (parser
->lexer
,
19872 && cp_lexer_next_token_is_not (parser
->lexer
,
19874 && cp_lexer_next_token_is_not (parser
->lexer
,
19877 inputs
= cp_parser_asm_operand_list (parser
);
19878 if (inputs
== error_mark_node
)
19879 invalid_inputs_p
= true;
19882 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19883 /* The clobbers are coming next. */
19886 /* Look for clobbers. */
19888 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19891 /* Consume the `:' or `::'. */
19892 cp_lexer_consume_token (parser
->lexer
);
19893 /* Parse the clobbers. */
19894 if (cp_lexer_next_token_is_not (parser
->lexer
,
19896 && cp_lexer_next_token_is_not (parser
->lexer
,
19898 clobbers
= cp_parser_asm_clobber_list (parser
);
19900 else if (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19901 /* The labels are coming next. */
19904 /* Look for labels. */
19906 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19909 /* Consume the `:' or `::'. */
19910 cp_lexer_consume_token (parser
->lexer
);
19911 /* Parse the labels. */
19912 labels
= cp_parser_asm_label_list (parser
);
19915 if (goto_p
&& !labels_p
)
19916 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19919 missing
= RT_COLON_SCOPE
;
19921 /* Look for the closing `)'. */
19922 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19923 missing
? missing
: RT_CLOSE_PAREN
))
19924 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19925 /*consume_paren=*/true);
19926 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19928 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19930 /* Create the ASM_EXPR. */
19931 if (parser
->in_function_body
)
19933 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19934 inputs
, clobbers
, labels
, inline_p
);
19935 /* If the extended syntax was not used, mark the ASM_EXPR. */
19938 tree temp
= asm_stmt
;
19939 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19940 temp
= TREE_OPERAND (temp
, 0);
19942 ASM_INPUT_P (temp
) = 1;
19946 symtab
->finalize_toplevel_asm (string
);
19950 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19951 type that comes from the decl-specifier-seq. */
19954 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19956 for (cp_declarator
*d
= declarator
; d
;)
19966 if (TYPE_PTRMEMFUNC_P (type
))
19967 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19968 type
= TREE_TYPE (type
);
19976 /* Declarators [gram.dcl.decl] */
19978 /* Parse an init-declarator.
19981 declarator initializer [opt]
19986 declarator asm-specification [opt] attributes [opt] initializer [opt]
19988 function-definition:
19989 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19991 decl-specifier-seq [opt] declarator function-try-block
19995 function-definition:
19996 __extension__ function-definition
20000 function-definition:
20001 decl-specifier-seq [opt] declarator function-transaction-block
20003 The parser flags FLAGS is used to control type-specifier parsing.
20005 The DECL_SPECIFIERS apply to this declarator. Returns a
20006 representation of the entity declared. If MEMBER_P is TRUE, then
20007 this declarator appears in a class scope. The new DECL created by
20008 this declarator is returned.
20010 The CHECKS are access checks that should be performed once we know
20011 what entity is being declared (and, therefore, what classes have
20014 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20015 for a function-definition here as well. If the declarator is a
20016 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20017 be TRUE upon return. By that point, the function-definition will
20018 have been completely parsed.
20020 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20023 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20024 parsed declaration if it is an uninitialized single declarator not followed
20025 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20026 if present, will not be consumed. If returned, this declarator will be
20027 created with SD_INITIALIZED but will not call cp_finish_decl.
20029 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20030 and there is an initializer, the pointed location_t is set to the
20031 location of the '=' or `(', or '{' in C++11 token introducing the
20035 cp_parser_init_declarator (cp_parser
* parser
,
20036 cp_parser_flags flags
,
20037 cp_decl_specifier_seq
*decl_specifiers
,
20038 vec
<deferred_access_check
, va_gc
> *checks
,
20039 bool function_definition_allowed_p
,
20041 int declares_class_or_enum
,
20042 bool* function_definition_p
,
20043 tree
* maybe_range_for_decl
,
20044 location_t
* init_loc
,
20047 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
20048 *attributes_start_token
= NULL
;
20049 cp_declarator
*declarator
;
20050 tree prefix_attributes
;
20051 tree attributes
= NULL
;
20052 tree asm_specification
;
20054 tree decl
= NULL_TREE
;
20056 int is_initialized
;
20057 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20058 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20060 enum cpp_ttype initialization_kind
;
20061 bool is_direct_init
= false;
20062 bool is_non_constant_init
;
20063 int ctor_dtor_or_conv_p
;
20064 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
20065 tree pushed_scope
= NULL_TREE
;
20066 bool range_for_decl_p
= false;
20067 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20068 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
20070 /* Gather the attributes that were provided with the
20071 decl-specifiers. */
20072 prefix_attributes
= decl_specifiers
->attributes
;
20074 /* Assume that this is not the declarator for a function
20076 if (function_definition_p
)
20077 *function_definition_p
= false;
20079 /* Default arguments are only permitted for function parameters. */
20080 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
20081 parser
->default_arg_ok_p
= false;
20083 /* Defer access checks while parsing the declarator; we cannot know
20084 what names are accessible until we know what is being
20086 resume_deferring_access_checks ();
20088 token
= cp_lexer_peek_token (parser
->lexer
);
20090 /* Parse the declarator. */
20092 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20093 flags
, &ctor_dtor_or_conv_p
,
20094 /*parenthesized_p=*/NULL
,
20095 member_p
, friend_p
, /*static_p=*/false);
20096 /* Gather up the deferred checks. */
20097 stop_deferring_access_checks ();
20099 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20101 /* If the DECLARATOR was erroneous, there's no need to go
20103 if (declarator
== cp_error_declarator
)
20104 return error_mark_node
;
20106 /* Check that the number of template-parameter-lists is OK. */
20107 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
20109 return error_mark_node
;
20111 if (declares_class_or_enum
& 2)
20112 cp_parser_check_for_definition_in_return_type (declarator
,
20113 decl_specifiers
->type
,
20114 decl_specifiers
->locations
[ds_type_spec
]);
20116 /* Figure out what scope the entity declared by the DECLARATOR is
20117 located in. `grokdeclarator' sometimes changes the scope, so
20118 we compute it now. */
20119 scope
= get_scope_of_declarator (declarator
);
20121 /* Perform any lookups in the declared type which were thought to be
20122 dependent, but are not in the scope of the declarator. */
20123 decl_specifiers
->type
20124 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
20126 /* If we're allowing GNU extensions, look for an
20127 asm-specification. */
20128 if (cp_parser_allow_gnu_extensions_p (parser
))
20130 /* Look for an asm-specification. */
20131 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
20132 asm_specification
= cp_parser_asm_specification_opt (parser
);
20135 asm_specification
= NULL_TREE
;
20137 /* Look for attributes. */
20138 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
20139 attributes
= cp_parser_attributes_opt (parser
);
20141 /* Peek at the next token. */
20142 token
= cp_lexer_peek_token (parser
->lexer
);
20144 bool bogus_implicit_tmpl
= false;
20146 if (function_declarator_p (declarator
))
20148 /* Handle C++17 deduction guides. */
20149 if (!decl_specifiers
->type
20150 && ctor_dtor_or_conv_p
<= 0
20151 && cxx_dialect
>= cxx17
)
20153 cp_declarator
*id
= get_id_declarator (declarator
);
20154 tree name
= id
->u
.id
.unqualified_name
;
20155 parser
->scope
= id
->u
.id
.qualifying_scope
;
20156 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
20158 && (DECL_CLASS_TEMPLATE_P (tmpl
)
20159 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
20161 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
20162 id
->u
.id
.sfk
= sfk_deduction_guide
;
20163 ctor_dtor_or_conv_p
= 1;
20167 /* Check to see if the token indicates the start of a
20168 function-definition. */
20169 if (cp_parser_token_starts_function_definition_p (token
))
20171 if (!function_definition_allowed_p
)
20173 /* If a function-definition should not appear here, issue an
20175 cp_parser_error (parser
,
20176 "a function-definition is not allowed here");
20177 return error_mark_node
;
20180 location_t func_brace_location
20181 = cp_lexer_peek_token (parser
->lexer
)->location
;
20183 /* Neither attributes nor an asm-specification are allowed
20184 on a function-definition. */
20185 if (asm_specification
)
20186 error_at (asm_spec_start_token
->location
,
20187 "an asm-specification is not allowed "
20188 "on a function-definition");
20190 error_at (attributes_start_token
->location
,
20191 "attributes are not allowed "
20192 "on a function-definition");
20193 /* This is a function-definition. */
20194 *function_definition_p
= true;
20196 /* Parse the function definition. */
20198 decl
= cp_parser_save_member_function_body (parser
,
20201 prefix_attributes
);
20204 (cp_parser_function_definition_from_specifiers_and_declarator
20205 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
20207 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
20209 /* This is where the prologue starts... */
20210 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
20211 = func_brace_location
;
20217 else if (parser
->fully_implicit_function_template_p
)
20219 /* A non-template declaration involving a function parameter list
20220 containing an implicit template parameter will be made into a
20221 template. If the resulting declaration is not going to be an
20222 actual function then finish the template scope here to prevent it.
20223 An error message will be issued once we have a decl to talk about.
20225 FIXME probably we should do type deduction rather than create an
20226 implicit template, but the standard currently doesn't allow it. */
20227 bogus_implicit_tmpl
= true;
20228 finish_fully_implicit_template (parser
, NULL_TREE
);
20233 Only in function declarations for constructors, destructors, type
20234 conversions, and deduction guides can the decl-specifier-seq be omitted.
20236 We explicitly postpone this check past the point where we handle
20237 function-definitions because we tolerate function-definitions
20238 that are missing their return types in some modes. */
20239 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
20241 cp_parser_error (parser
,
20242 "expected constructor, destructor, or type conversion");
20243 return error_mark_node
;
20246 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20247 if (token
->type
== CPP_EQ
20248 || token
->type
== CPP_OPEN_PAREN
20249 || token
->type
== CPP_OPEN_BRACE
)
20251 is_initialized
= SD_INITIALIZED
;
20252 initialization_kind
= token
->type
;
20253 if (maybe_range_for_decl
)
20254 *maybe_range_for_decl
= error_mark_node
;
20255 tmp_init_loc
= token
->location
;
20256 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
20257 *init_loc
= tmp_init_loc
;
20259 if (token
->type
== CPP_EQ
20260 && function_declarator_p (declarator
))
20262 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20263 if (t2
->keyword
== RID_DEFAULT
)
20264 is_initialized
= SD_DEFAULTED
;
20265 else if (t2
->keyword
== RID_DELETE
)
20266 is_initialized
= SD_DELETED
;
20271 /* If the init-declarator isn't initialized and isn't followed by a
20272 `,' or `;', it's not a valid init-declarator. */
20273 if (token
->type
!= CPP_COMMA
20274 && token
->type
!= CPP_SEMICOLON
)
20276 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
20277 range_for_decl_p
= true;
20280 if (!maybe_range_for_decl
)
20281 cp_parser_error (parser
, "expected initializer");
20282 return error_mark_node
;
20285 is_initialized
= SD_UNINITIALIZED
;
20286 initialization_kind
= CPP_EOF
;
20289 /* Because start_decl has side-effects, we should only call it if we
20290 know we're going ahead. By this point, we know that we cannot
20291 possibly be looking at any other construct. */
20292 cp_parser_commit_to_tentative_parse (parser
);
20294 /* Enter the newly declared entry in the symbol table. If we're
20295 processing a declaration in a class-specifier, we wait until
20296 after processing the initializer. */
20299 if (parser
->in_unbraced_linkage_specification_p
)
20300 decl_specifiers
->storage_class
= sc_extern
;
20301 decl
= start_decl (declarator
, decl_specifiers
,
20302 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
20303 attributes
, prefix_attributes
, &pushed_scope
);
20304 cp_finalize_omp_declare_simd (parser
, decl
);
20305 cp_finalize_oacc_routine (parser
, decl
, false);
20306 /* Adjust location of decl if declarator->id_loc is more appropriate:
20307 set, and decl wasn't merged with another decl, in which case its
20308 location would be different from input_location, and more accurate. */
20310 && declarator
->id_loc
!= UNKNOWN_LOCATION
20311 && DECL_SOURCE_LOCATION (decl
) == input_location
)
20312 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
20315 /* Enter the SCOPE. That way unqualified names appearing in the
20316 initializer will be looked up in SCOPE. */
20317 pushed_scope
= push_scope (scope
);
20319 /* Perform deferred access control checks, now that we know in which
20320 SCOPE the declared entity resides. */
20321 if (!member_p
&& decl
)
20323 tree saved_current_function_decl
= NULL_TREE
;
20325 /* If the entity being declared is a function, pretend that we
20326 are in its scope. If it is a `friend', it may have access to
20327 things that would not otherwise be accessible. */
20328 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20330 saved_current_function_decl
= current_function_decl
;
20331 current_function_decl
= decl
;
20334 /* Perform access checks for template parameters. */
20335 cp_parser_perform_template_parameter_access_checks (checks
);
20337 /* Perform the access control checks for the declarator and the
20338 decl-specifiers. */
20339 perform_deferred_access_checks (tf_warning_or_error
);
20341 /* Restore the saved value. */
20342 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20343 current_function_decl
= saved_current_function_decl
;
20346 /* Parse the initializer. */
20347 initializer
= NULL_TREE
;
20348 is_direct_init
= false;
20349 is_non_constant_init
= true;
20350 if (is_initialized
)
20352 if (function_declarator_p (declarator
))
20354 if (initialization_kind
== CPP_EQ
)
20355 initializer
= cp_parser_pure_specifier (parser
);
20358 /* If the declaration was erroneous, we don't really
20359 know what the user intended, so just silently
20360 consume the initializer. */
20361 if (decl
!= error_mark_node
)
20362 error_at (tmp_init_loc
, "initializer provided for function");
20363 cp_parser_skip_to_closing_parenthesis (parser
,
20364 /*recovering=*/true,
20365 /*or_comma=*/false,
20366 /*consume_paren=*/true);
20371 /* We want to record the extra mangling scope for in-class
20372 initializers of class members and initializers of static data
20373 member templates. The former involves deferring
20374 parsing of the initializer until end of class as with default
20375 arguments. So right here we only handle the latter. */
20376 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20377 start_lambda_scope (decl
);
20378 initializer
= cp_parser_initializer (parser
,
20380 &is_non_constant_init
);
20381 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20382 finish_lambda_scope ();
20383 if (initializer
== error_mark_node
)
20384 cp_parser_skip_to_end_of_statement (parser
);
20388 /* The old parser allows attributes to appear after a parenthesized
20389 initializer. Mark Mitchell proposed removing this functionality
20390 on the GCC mailing lists on 2002-08-13. This parser accepts the
20391 attributes -- but ignores them. Made a permerror in GCC 8. */
20392 if (cp_parser_allow_gnu_extensions_p (parser
)
20393 && initialization_kind
== CPP_OPEN_PAREN
20394 && cp_parser_attributes_opt (parser
)
20395 && permerror (input_location
,
20396 "attributes after parenthesized initializer ignored"))
20399 if (flag_permissive
&& !hint
)
20402 inform (input_location
,
20403 "this flexibility is deprecated and will be removed");
20407 /* And now complain about a non-function implicit template. */
20408 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20409 error_at (DECL_SOURCE_LOCATION (decl
),
20410 "non-function %qD declared as implicit template", decl
);
20412 /* For an in-class declaration, use `grokfield' to create the
20418 pop_scope (pushed_scope
);
20419 pushed_scope
= NULL_TREE
;
20421 decl
= grokfield (declarator
, decl_specifiers
,
20422 initializer
, !is_non_constant_init
,
20423 /*asmspec=*/NULL_TREE
,
20424 attr_chainon (attributes
, prefix_attributes
));
20425 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20426 cp_parser_save_default_args (parser
, decl
);
20427 cp_finalize_omp_declare_simd (parser
, decl
);
20428 cp_finalize_oacc_routine (parser
, decl
, false);
20431 /* Finish processing the declaration. But, skip member
20433 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20435 cp_finish_decl (decl
,
20436 initializer
, !is_non_constant_init
,
20438 /* If the initializer is in parentheses, then this is
20439 a direct-initialization, which means that an
20440 `explicit' constructor is OK. Otherwise, an
20441 `explicit' constructor cannot be used. */
20442 ((is_direct_init
|| !is_initialized
)
20443 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
20445 else if ((cxx_dialect
!= cxx98
) && friend_p
20446 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20447 /* Core issue #226 (C++0x only): A default template-argument
20448 shall not be specified in a friend class template
20450 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20451 /*is_partial=*/false, /*is_friend_decl=*/1);
20453 if (!friend_p
&& pushed_scope
)
20454 pop_scope (pushed_scope
);
20456 if (function_declarator_p (declarator
)
20457 && parser
->fully_implicit_function_template_p
)
20460 decl
= finish_fully_implicit_template (parser
, decl
);
20462 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
20465 if (auto_result
&& is_initialized
&& decl_specifiers
->type
20466 && type_uses_auto (decl_specifiers
->type
))
20467 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
20472 /* Parse a declarator.
20476 ptr-operator declarator
20478 abstract-declarator:
20479 ptr-operator abstract-declarator [opt]
20480 direct-abstract-declarator
20485 attributes [opt] direct-declarator
20486 attributes [opt] ptr-operator declarator
20488 abstract-declarator:
20489 attributes [opt] ptr-operator abstract-declarator [opt]
20490 attributes [opt] direct-abstract-declarator
20492 The parser flags FLAGS is used to control type-specifier parsing.
20494 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20495 detect constructors, destructors, deduction guides, or conversion operators.
20496 It is set to -1 if the declarator is a name, and +1 if it is a
20497 function. Otherwise it is set to zero. Usually you just want to
20498 test for >0, but internally the negative value is used.
20500 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20501 a decl-specifier-seq unless it declares a constructor, destructor,
20502 or conversion. It might seem that we could check this condition in
20503 semantic analysis, rather than parsing, but that makes it difficult
20504 to handle something like `f()'. We want to notice that there are
20505 no decl-specifiers, and therefore realize that this is an
20506 expression, not a declaration.)
20508 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20509 the declarator is a direct-declarator of the form "(...)".
20511 MEMBER_P is true iff this declarator is a member-declarator.
20513 FRIEND_P is true iff this declarator is a friend.
20515 STATIC_P is true iff the keyword static was seen. */
20517 static cp_declarator
*
20518 cp_parser_declarator (cp_parser
* parser
,
20519 cp_parser_declarator_kind dcl_kind
,
20520 cp_parser_flags flags
,
20521 int* ctor_dtor_or_conv_p
,
20522 bool* parenthesized_p
,
20523 bool member_p
, bool friend_p
, bool static_p
)
20525 cp_declarator
*declarator
;
20526 enum tree_code code
;
20527 cp_cv_quals cv_quals
;
20529 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
20531 /* Assume this is not a constructor, destructor, or type-conversion
20533 if (ctor_dtor_or_conv_p
)
20534 *ctor_dtor_or_conv_p
= 0;
20536 if (cp_parser_allow_gnu_extensions_p (parser
))
20537 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
20539 /* Check for the ptr-operator production. */
20540 cp_parser_parse_tentatively (parser
);
20541 /* Parse the ptr-operator. */
20542 code
= cp_parser_ptr_operator (parser
,
20547 /* If that worked, then we have a ptr-operator. */
20548 if (cp_parser_parse_definitely (parser
))
20550 /* If a ptr-operator was found, then this declarator was not
20552 if (parenthesized_p
)
20553 *parenthesized_p
= true;
20554 /* The dependent declarator is optional if we are parsing an
20555 abstract-declarator. */
20556 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20557 cp_parser_parse_tentatively (parser
);
20559 /* Parse the dependent declarator. */
20560 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20561 CP_PARSER_FLAGS_NONE
,
20562 /*ctor_dtor_or_conv_p=*/NULL
,
20563 /*parenthesized_p=*/NULL
,
20564 /*member_p=*/false,
20565 friend_p
, /*static_p=*/false);
20567 /* If we are parsing an abstract-declarator, we must handle the
20568 case where the dependent declarator is absent. */
20569 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20570 && !cp_parser_parse_definitely (parser
))
20573 declarator
= cp_parser_make_indirect_declarator
20574 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20576 /* Everything else is a direct-declarator. */
20579 if (parenthesized_p
)
20580 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20582 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20583 flags
, ctor_dtor_or_conv_p
,
20584 member_p
, friend_p
, static_p
);
20587 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20588 declarator
->attributes
= gnu_attributes
;
20592 /* Parse a direct-declarator or direct-abstract-declarator.
20596 direct-declarator ( parameter-declaration-clause )
20597 cv-qualifier-seq [opt]
20598 ref-qualifier [opt]
20599 exception-specification [opt]
20600 direct-declarator [ constant-expression [opt] ]
20603 direct-abstract-declarator:
20604 direct-abstract-declarator [opt]
20605 ( parameter-declaration-clause )
20606 cv-qualifier-seq [opt]
20607 ref-qualifier [opt]
20608 exception-specification [opt]
20609 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20610 ( abstract-declarator )
20612 Returns a representation of the declarator. DCL_KIND is
20613 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20614 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20615 we are parsing a direct-declarator. It is
20616 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20617 of ambiguity we prefer an abstract declarator, as per
20619 The parser flags FLAGS is used to control type-specifier parsing.
20620 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20621 as for cp_parser_declarator. */
20623 static cp_declarator
*
20624 cp_parser_direct_declarator (cp_parser
* parser
,
20625 cp_parser_declarator_kind dcl_kind
,
20626 cp_parser_flags flags
,
20627 int* ctor_dtor_or_conv_p
,
20628 bool member_p
, bool friend_p
, bool static_p
)
20631 cp_declarator
*declarator
= NULL
;
20632 tree scope
= NULL_TREE
;
20633 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20634 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20636 tree pushed_scope
= NULL_TREE
;
20637 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20641 /* Peek at the next token. */
20642 token
= cp_lexer_peek_token (parser
->lexer
);
20643 if (token
->type
== CPP_OPEN_PAREN
)
20645 /* This is either a parameter-declaration-clause, or a
20646 parenthesized declarator. When we know we are parsing a
20647 named declarator, it must be a parenthesized declarator
20648 if FIRST is true. For instance, `(int)' is a
20649 parameter-declaration-clause, with an omitted
20650 direct-abstract-declarator. But `((*))', is a
20651 parenthesized abstract declarator. Finally, when T is a
20652 template parameter `(T)' is a
20653 parameter-declaration-clause, and not a parenthesized
20656 We first try and parse a parameter-declaration-clause,
20657 and then try a nested declarator (if FIRST is true).
20659 It is not an error for it not to be a
20660 parameter-declaration-clause, even when FIRST is
20666 The first is the declaration of a function while the
20667 second is the definition of a variable, including its
20670 Having seen only the parenthesis, we cannot know which of
20671 these two alternatives should be selected. Even more
20672 complex are examples like:
20677 The former is a function-declaration; the latter is a
20678 variable initialization.
20680 Thus again, we try a parameter-declaration-clause, and if
20681 that fails, we back out and return. */
20683 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20686 bool is_declarator
= false;
20690 /* In a member-declarator, the only valid interpretation
20691 of a parenthesis is the start of a
20692 parameter-declaration-clause. (It is invalid to
20693 initialize a static data member with a parenthesized
20694 initializer; only the "=" form of initialization is
20697 cp_parser_parse_tentatively (parser
);
20699 /* Consume the `('. */
20700 matching_parens parens
;
20701 parens
.consume_open (parser
);
20704 /* If this is going to be an abstract declarator, we're
20705 in a declarator and we can't have default args. */
20706 parser
->default_arg_ok_p
= false;
20707 parser
->in_declarator_p
= true;
20710 begin_scope (sk_function_parms
, NULL_TREE
);
20712 /* Parse the parameter-declaration-clause. */
20714 = cp_parser_parameter_declaration_clause (parser
, flags
);
20716 /* Consume the `)'. */
20717 parens
.require_close (parser
);
20719 /* If all went well, parse the cv-qualifier-seq,
20720 ref-qualifier and the exception-specification. */
20721 if (member_p
|| cp_parser_parse_definitely (parser
))
20723 cp_cv_quals cv_quals
;
20724 cp_virt_specifiers virt_specifiers
;
20725 cp_ref_qualifier ref_qual
;
20726 tree exception_specification
;
20729 bool memfn
= (member_p
|| (pushed_scope
20730 && CLASS_TYPE_P (pushed_scope
)));
20731 unsigned char local_variables_forbidden_p
20732 = parser
->local_variables_forbidden_p
;
20733 /* 'this' is not allowed in static member functions. */
20734 if (static_p
|| friend_p
)
20735 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
20737 is_declarator
= true;
20739 if (ctor_dtor_or_conv_p
)
20740 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20743 /* Parse the cv-qualifier-seq. */
20744 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20745 /* Parse the ref-qualifier. */
20746 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20747 /* Parse the tx-qualifier. */
20748 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20749 /* And the exception-specification. */
20750 exception_specification
20751 = cp_parser_exception_specification_opt (parser
);
20753 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20755 /* In here, we handle cases where attribute is used after
20756 the function declaration. For example:
20757 void func (int x) __attribute__((vector(..))); */
20758 tree gnu_attrs
= NULL_TREE
;
20759 tree requires_clause
= NULL_TREE
;
20760 late_return
= (cp_parser_late_return_type_opt
20761 (parser
, declarator
, requires_clause
,
20762 memfn
? cv_quals
: -1));
20764 /* Parse the virt-specifier-seq. */
20765 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20767 /* Create the function-declarator. */
20768 declarator
= make_call_declarator (declarator
,
20774 exception_specification
,
20777 declarator
->std_attributes
= attrs
;
20778 declarator
->attributes
= gnu_attrs
;
20779 /* Any subsequent parameter lists are to do with
20780 return type, so are not those of the declared
20782 parser
->default_arg_ok_p
= false;
20784 /* Restore the state of local_variables_forbidden_p. */
20785 parser
->local_variables_forbidden_p
20786 = local_variables_forbidden_p
;
20789 /* Remove the function parms from scope. */
20790 pop_bindings_and_leave_scope ();
20793 /* Repeat the main loop. */
20797 /* If this is the first, we can try a parenthesized
20801 bool saved_in_type_id_in_expr_p
;
20803 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20804 parser
->in_declarator_p
= saved_in_declarator_p
;
20806 open_paren
= token
;
20807 /* Consume the `('. */
20808 matching_parens parens
;
20809 parens
.consume_open (parser
);
20810 /* Parse the nested declarator. */
20811 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20812 parser
->in_type_id_in_expr_p
= true;
20814 = cp_parser_declarator (parser
, dcl_kind
, flags
,
20815 ctor_dtor_or_conv_p
,
20816 /*parenthesized_p=*/NULL
,
20817 member_p
, friend_p
,
20818 /*static_p=*/false);
20819 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20821 /* Expect a `)'. */
20822 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20823 if (!parens
.require_close (parser
))
20824 declarator
= cp_error_declarator
;
20825 if (declarator
== cp_error_declarator
)
20828 goto handle_declarator
;
20830 /* Otherwise, we must be done. */
20834 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20835 && token
->type
== CPP_OPEN_SQUARE
20836 && !cp_next_tokens_can_be_attribute_p (parser
))
20838 /* Parse an array-declarator. */
20839 tree bounds
, attrs
;
20841 if (ctor_dtor_or_conv_p
)
20842 *ctor_dtor_or_conv_p
= 0;
20846 parser
->default_arg_ok_p
= false;
20847 parser
->in_declarator_p
= true;
20848 /* Consume the `['. */
20849 cp_lexer_consume_token (parser
->lexer
);
20850 /* Peek at the next token. */
20851 token
= cp_lexer_peek_token (parser
->lexer
);
20852 /* If the next token is `]', then there is no
20853 constant-expression. */
20854 if (token
->type
!= CPP_CLOSE_SQUARE
)
20856 bool non_constant_p
;
20858 = cp_parser_constant_expression (parser
,
20859 /*allow_non_constant=*/true,
20861 if (!non_constant_p
)
20863 else if (error_operand_p (bounds
))
20864 /* Already gave an error. */;
20865 else if (!parser
->in_function_body
20866 || current_binding_level
->kind
== sk_function_parms
)
20868 /* Normally, the array bound must be an integral constant
20869 expression. However, as an extension, we allow VLAs
20870 in function scopes as long as they aren't part of a
20871 parameter declaration. */
20872 cp_parser_error (parser
,
20873 "array bound is not an integer constant");
20874 bounds
= error_mark_node
;
20876 else if (processing_template_decl
20877 && !type_dependent_expression_p (bounds
))
20879 /* Remember this wasn't a constant-expression. */
20880 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20881 TREE_SIDE_EFFECTS (bounds
) = 1;
20885 bounds
= NULL_TREE
;
20886 /* Look for the closing `]'. */
20887 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20889 declarator
= cp_error_declarator
;
20893 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20894 declarator
= make_array_declarator (declarator
, bounds
);
20895 declarator
->std_attributes
= attrs
;
20897 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20900 tree qualifying_scope
;
20901 tree unqualified_name
;
20903 special_function_kind sfk
;
20905 bool pack_expansion_p
= false;
20906 cp_token
*declarator_id_start_token
;
20908 /* Parse a declarator-id */
20909 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20912 cp_parser_parse_tentatively (parser
);
20914 /* If we see an ellipsis, we should be looking at a
20916 if (token
->type
== CPP_ELLIPSIS
)
20918 /* Consume the `...' */
20919 cp_lexer_consume_token (parser
->lexer
);
20921 pack_expansion_p
= true;
20925 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20927 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20928 qualifying_scope
= parser
->scope
;
20933 if (!unqualified_name
&& pack_expansion_p
)
20935 /* Check whether an error occurred. */
20936 okay
= !cp_parser_error_occurred (parser
);
20938 /* We already consumed the ellipsis to mark a
20939 parameter pack, but we have no way to report it,
20940 so abort the tentative parse. We will be exiting
20941 immediately anyway. */
20942 cp_parser_abort_tentative_parse (parser
);
20945 okay
= cp_parser_parse_definitely (parser
);
20948 unqualified_name
= error_mark_node
;
20949 else if (unqualified_name
20950 && (qualifying_scope
20951 || (!identifier_p (unqualified_name
))))
20953 cp_parser_error (parser
, "expected unqualified-id");
20954 unqualified_name
= error_mark_node
;
20958 if (!unqualified_name
)
20960 if (unqualified_name
== error_mark_node
)
20962 declarator
= cp_error_declarator
;
20963 pack_expansion_p
= false;
20964 declarator
->parameter_pack_p
= false;
20968 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20970 if (qualifying_scope
&& at_namespace_scope_p ()
20971 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20973 /* In the declaration of a member of a template class
20974 outside of the class itself, the SCOPE will sometimes
20975 be a TYPENAME_TYPE. For example, given:
20977 template <typename T>
20978 int S<T>::R::i = 3;
20980 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20981 this context, we must resolve S<T>::R to an ordinary
20982 type, rather than a typename type.
20984 The reason we normally avoid resolving TYPENAME_TYPEs
20985 is that a specialization of `S' might render
20986 `S<T>::R' not a type. However, if `S' is
20987 specialized, then this `i' will not be used, so there
20988 is no harm in resolving the types here. */
20991 /* Resolve the TYPENAME_TYPE. */
20992 type
= resolve_typename_type (qualifying_scope
,
20993 /*only_current_p=*/false);
20994 /* If that failed, the declarator is invalid. */
20995 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20997 if (typedef_variant_p (type
))
20998 error_at (declarator_id_start_token
->location
,
20999 "cannot define member of dependent typedef "
21002 error_at (declarator_id_start_token
->location
,
21003 "%<%T::%E%> is not a type",
21004 TYPE_CONTEXT (qualifying_scope
),
21005 TYPE_IDENTIFIER (qualifying_scope
));
21007 qualifying_scope
= type
;
21012 if (unqualified_name
)
21016 if (qualifying_scope
21017 && CLASS_TYPE_P (qualifying_scope
))
21018 class_type
= qualifying_scope
;
21020 class_type
= current_class_type
;
21022 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
21024 tree name_type
= TREE_TYPE (unqualified_name
);
21026 if (!class_type
|| !same_type_p (name_type
, class_type
))
21028 /* We do not attempt to print the declarator
21029 here because we do not have enough
21030 information about its original syntactic
21032 cp_parser_error (parser
, "invalid declarator");
21033 declarator
= cp_error_declarator
;
21036 else if (qualifying_scope
21037 && CLASSTYPE_USE_TEMPLATE (name_type
))
21039 error_at (declarator_id_start_token
->location
,
21040 "invalid use of constructor as a template");
21041 inform (declarator_id_start_token
->location
,
21042 "use %<%T::%D%> instead of %<%T::%D%> to "
21043 "name the constructor in a qualified name",
21045 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
21046 class_type
, name_type
);
21047 declarator
= cp_error_declarator
;
21050 unqualified_name
= constructor_name (class_type
);
21055 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
21056 sfk
= sfk_destructor
;
21057 else if (identifier_p (unqualified_name
)
21058 && IDENTIFIER_CONV_OP_P (unqualified_name
))
21059 sfk
= sfk_conversion
;
21060 else if (/* There's no way to declare a constructor
21061 for an unnamed type, even if the type
21062 got a name for linkage purposes. */
21063 !TYPE_WAS_UNNAMED (class_type
)
21064 /* Handle correctly (c++/19200):
21078 friend void N::S();
21080 && (!friend_p
|| class_type
== qualifying_scope
)
21081 && constructor_name_p (unqualified_name
,
21083 sfk
= sfk_constructor
;
21084 else if (is_overloaded_fn (unqualified_name
)
21085 && DECL_CONSTRUCTOR_P (get_first_fn
21086 (unqualified_name
)))
21087 sfk
= sfk_constructor
;
21089 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
21090 *ctor_dtor_or_conv_p
= -1;
21093 declarator
= make_id_declarator (qualifying_scope
,
21095 sfk
, token
->location
);
21096 declarator
->std_attributes
= attrs
;
21097 declarator
->parameter_pack_p
= pack_expansion_p
;
21099 if (pack_expansion_p
)
21100 maybe_warn_variadic_templates ();
21102 /* We're looking for this case in [temp.res]:
21103 A qualified-id is assumed to name a type if [...]
21104 - it is a decl-specifier of the decl-specifier-seq of a
21105 parameter-declaration in a declarator of a function or
21106 function template declaration, ... */
21107 if (cxx_dialect
>= cxx2a
21108 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
)
21109 && declarator
->kind
== cdk_id
21110 && !at_class_scope_p ()
21111 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21113 /* ...whose declarator-id is qualified. If it isn't, never
21114 assume the parameters to refer to types. */
21115 if (qualifying_scope
== NULL_TREE
)
21116 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21119 /* Now we have something like
21120 template <typename T> int C::x(S::p);
21121 which can be a function template declaration or a
21122 variable template definition. If name lookup for
21123 the declarator-id C::x finds one or more function
21124 templates, assume S::p to name a type. Otherwise,
21127 = cp_parser_lookup_name_simple (parser
, unqualified_name
,
21129 if (!is_overloaded_fn (decl
))
21130 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21135 handle_declarator
:;
21136 scope
= get_scope_of_declarator (declarator
);
21139 /* Any names that appear after the declarator-id for a
21140 member are looked up in the containing scope. */
21141 if (at_function_scope_p ())
21143 /* But declarations with qualified-ids can't appear in a
21145 cp_parser_error (parser
, "qualified-id in declaration");
21146 declarator
= cp_error_declarator
;
21149 pushed_scope
= push_scope (scope
);
21151 parser
->in_declarator_p
= true;
21152 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
21153 || (declarator
&& declarator
->kind
== cdk_id
))
21154 /* Default args are only allowed on function
21156 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21158 parser
->default_arg_ok_p
= false;
21167 /* For an abstract declarator, we might wind up with nothing at this
21168 point. That's an error; the declarator is not optional. */
21170 cp_parser_error (parser
, "expected declarator");
21171 else if (open_paren
)
21173 /* Record overly parenthesized declarator so we can give a
21174 diagnostic about confusing decl/expr disambiguation. */
21175 if (declarator
->kind
== cdk_array
)
21177 /* If the open and close parens are on different lines, this
21178 is probably a formatting thing, so ignore. */
21179 expanded_location open
= expand_location (open_paren
->location
);
21180 expanded_location close
= expand_location (close_paren
->location
);
21181 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
21185 declarator
->parenthesized
= open_paren
->location
;
21188 /* If we entered a scope, we must exit it now. */
21190 pop_scope (pushed_scope
);
21192 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21193 parser
->in_declarator_p
= saved_in_declarator_p
;
21198 /* Parse a ptr-operator.
21201 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21202 * cv-qualifier-seq [opt]
21204 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21205 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21210 & cv-qualifier-seq [opt]
21212 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21213 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21214 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21215 filled in with the TYPE containing the member. *CV_QUALS is
21216 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21217 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21218 Note that the tree codes returned by this function have nothing
21219 to do with the types of trees that will be eventually be created
21220 to represent the pointer or reference type being parsed. They are
21221 just constants with suggestive names. */
21222 static enum tree_code
21223 cp_parser_ptr_operator (cp_parser
* parser
,
21225 cp_cv_quals
*cv_quals
,
21228 enum tree_code code
= ERROR_MARK
;
21230 tree attrs
= NULL_TREE
;
21232 /* Assume that it's not a pointer-to-member. */
21234 /* And that there are no cv-qualifiers. */
21235 *cv_quals
= TYPE_UNQUALIFIED
;
21237 /* Peek at the next token. */
21238 token
= cp_lexer_peek_token (parser
->lexer
);
21240 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21241 if (token
->type
== CPP_MULT
)
21242 code
= INDIRECT_REF
;
21243 else if (token
->type
== CPP_AND
)
21245 else if ((cxx_dialect
!= cxx98
) &&
21246 token
->type
== CPP_AND_AND
) /* C++0x only */
21247 code
= NON_LVALUE_EXPR
;
21249 if (code
!= ERROR_MARK
)
21251 /* Consume the `*', `&' or `&&'. */
21252 cp_lexer_consume_token (parser
->lexer
);
21254 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21255 `&', if we are allowing GNU extensions. (The only qualifier
21256 that can legally appear after `&' is `restrict', but that is
21257 enforced during semantic analysis. */
21258 if (code
== INDIRECT_REF
21259 || cp_parser_allow_gnu_extensions_p (parser
))
21260 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21262 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21263 if (attributes
!= NULL
)
21264 *attributes
= attrs
;
21268 /* Try the pointer-to-member case. */
21269 cp_parser_parse_tentatively (parser
);
21270 /* Look for the optional `::' operator. */
21271 cp_parser_global_scope_opt (parser
,
21272 /*current_scope_valid_p=*/false);
21273 /* Look for the nested-name specifier. */
21274 token
= cp_lexer_peek_token (parser
->lexer
);
21275 cp_parser_nested_name_specifier (parser
,
21276 /*typename_keyword_p=*/false,
21277 /*check_dependency_p=*/true,
21279 /*is_declaration=*/false);
21280 /* If we found it, and the next token is a `*', then we are
21281 indeed looking at a pointer-to-member operator. */
21282 if (!cp_parser_error_occurred (parser
)
21283 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
21285 /* Indicate that the `*' operator was used. */
21286 code
= INDIRECT_REF
;
21288 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
21289 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
21290 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
21291 error_at (token
->location
, "cannot form pointer to member of "
21292 "non-class %q#T", parser
->scope
);
21295 /* The type of which the member is a member is given by the
21297 *type
= parser
->scope
;
21298 /* The next name will not be qualified. */
21299 parser
->scope
= NULL_TREE
;
21300 parser
->qualifying_scope
= NULL_TREE
;
21301 parser
->object_scope
= NULL_TREE
;
21302 /* Look for optional c++11 attributes. */
21303 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21304 if (attributes
!= NULL
)
21305 *attributes
= attrs
;
21306 /* Look for the optional cv-qualifier-seq. */
21307 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21310 /* If that didn't work we don't have a ptr-operator. */
21311 if (!cp_parser_parse_definitely (parser
))
21312 cp_parser_error (parser
, "expected ptr-operator");
21318 /* Parse an (optional) cv-qualifier-seq.
21321 cv-qualifier cv-qualifier-seq [opt]
21332 Returns a bitmask representing the cv-qualifiers. */
21335 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
21337 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
21342 cp_cv_quals cv_qualifier
;
21344 /* Peek at the next token. */
21345 token
= cp_lexer_peek_token (parser
->lexer
);
21346 /* See if it's a cv-qualifier. */
21347 switch (token
->keyword
)
21350 cv_qualifier
= TYPE_QUAL_CONST
;
21354 cv_qualifier
= TYPE_QUAL_VOLATILE
;
21358 cv_qualifier
= TYPE_QUAL_RESTRICT
;
21362 cv_qualifier
= TYPE_UNQUALIFIED
;
21369 if (cv_quals
& cv_qualifier
)
21371 gcc_rich_location
richloc (token
->location
);
21372 richloc
.add_fixit_remove ();
21373 error_at (&richloc
, "duplicate cv-qualifier");
21374 cp_lexer_purge_token (parser
->lexer
);
21378 cp_lexer_consume_token (parser
->lexer
);
21379 cv_quals
|= cv_qualifier
;
21386 /* Parse an (optional) ref-qualifier
21392 Returns cp_ref_qualifier representing ref-qualifier. */
21394 static cp_ref_qualifier
21395 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
21397 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
21399 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21400 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
21405 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
21406 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21408 switch (token
->type
)
21411 curr_ref_qual
= REF_QUAL_LVALUE
;
21415 curr_ref_qual
= REF_QUAL_RVALUE
;
21419 curr_ref_qual
= REF_QUAL_NONE
;
21423 if (!curr_ref_qual
)
21427 error_at (token
->location
, "multiple ref-qualifiers");
21428 cp_lexer_purge_token (parser
->lexer
);
21432 ref_qual
= curr_ref_qual
;
21433 cp_lexer_consume_token (parser
->lexer
);
21440 /* Parse an optional tx-qualifier.
21444 transaction_safe_dynamic */
21447 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
21449 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21450 if (token
->type
== CPP_NAME
)
21452 tree name
= token
->u
.value
;
21453 const char *p
= IDENTIFIER_POINTER (name
);
21454 const int len
= strlen ("transaction_safe");
21455 if (!strncmp (p
, "transaction_safe", len
))
21459 || !strcmp (p
, "_dynamic"))
21461 cp_lexer_consume_token (parser
->lexer
);
21464 error ("%qE requires %<-fgnu-tm%>", name
);
21475 /* Parse an (optional) virt-specifier-seq.
21477 virt-specifier-seq:
21478 virt-specifier virt-specifier-seq [opt]
21484 Returns a bitmask representing the virt-specifiers. */
21486 static cp_virt_specifiers
21487 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
21489 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21494 cp_virt_specifiers virt_specifier
;
21496 /* Peek at the next token. */
21497 token
= cp_lexer_peek_token (parser
->lexer
);
21498 /* See if it's a virt-specifier-qualifier. */
21499 if (token
->type
!= CPP_NAME
)
21501 if (id_equal (token
->u
.value
, "override"))
21503 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21504 virt_specifier
= VIRT_SPEC_OVERRIDE
;
21506 else if (id_equal (token
->u
.value
, "final"))
21508 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21509 virt_specifier
= VIRT_SPEC_FINAL
;
21511 else if (id_equal (token
->u
.value
, "__final"))
21513 virt_specifier
= VIRT_SPEC_FINAL
;
21518 if (virt_specifiers
& virt_specifier
)
21520 gcc_rich_location
richloc (token
->location
);
21521 richloc
.add_fixit_remove ();
21522 error_at (&richloc
, "duplicate virt-specifier");
21523 cp_lexer_purge_token (parser
->lexer
);
21527 cp_lexer_consume_token (parser
->lexer
);
21528 virt_specifiers
|= virt_specifier
;
21531 return virt_specifiers
;
21534 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21535 is in scope even though it isn't real. */
21538 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
21542 if (current_class_ptr
)
21544 /* We don't clear this between NSDMIs. Is it already what we want? */
21545 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
21546 if (DECL_P (current_class_ptr
)
21547 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
21548 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
21549 && cp_type_quals (type
) == quals
)
21553 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
21554 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21555 current_class_ptr
= NULL_TREE
;
21557 = cp_build_fold_indirect_ref (this_parm
);
21558 current_class_ptr
= this_parm
;
21561 /* Return true iff our current scope is a non-static data member
21565 parsing_nsdmi (void)
21567 /* We recognize NSDMI context by the context-less 'this' pointer set up
21568 by the function above. */
21569 if (current_class_ptr
21570 && TREE_CODE (current_class_ptr
) == PARM_DECL
21571 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
21576 /* Parse a late-specified return type, if any. This is not a separate
21577 non-terminal, but part of a function declarator, which looks like
21579 -> trailing-type-specifier-seq abstract-declarator(opt)
21581 Returns the type indicated by the type-id.
21583 In addition to this, parse any queued up #pragma omp declare simd
21584 clauses, and #pragma acc routine clauses.
21586 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21590 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
21591 tree
& requires_clause
, cp_cv_quals quals
)
21594 tree type
= NULL_TREE
;
21595 bool declare_simd_p
= (parser
->omp_declare_simd
21597 && declarator
->kind
== cdk_id
);
21599 bool oacc_routine_p
= (parser
->oacc_routine
21601 && declarator
->kind
== cdk_id
);
21603 /* Peek at the next token. */
21604 token
= cp_lexer_peek_token (parser
->lexer
);
21605 /* A late-specified return type is indicated by an initial '->'. */
21606 if (token
->type
!= CPP_DEREF
21607 && token
->keyword
!= RID_REQUIRES
21608 && !(token
->type
== CPP_NAME
21609 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21610 && !(declare_simd_p
|| oacc_routine_p
))
21613 tree save_ccp
= current_class_ptr
;
21614 tree save_ccr
= current_class_ref
;
21617 /* DR 1207: 'this' is in scope in the trailing return type. */
21618 inject_this_parameter (current_class_type
, quals
);
21621 if (token
->type
== CPP_DEREF
)
21623 /* Consume the ->. */
21624 cp_lexer_consume_token (parser
->lexer
);
21626 type
= cp_parser_trailing_type_id (parser
);
21629 /* Function declarations may be followed by a trailing
21630 requires-clause. */
21631 requires_clause
= cp_parser_requires_clause_opt (parser
);
21633 if (declare_simd_p
)
21634 declarator
->attributes
21635 = cp_parser_late_parsing_omp_declare_simd (parser
,
21636 declarator
->attributes
);
21637 if (oacc_routine_p
)
21638 declarator
->attributes
21639 = cp_parser_late_parsing_oacc_routine (parser
,
21640 declarator
->attributes
);
21644 current_class_ptr
= save_ccp
;
21645 current_class_ref
= save_ccr
;
21651 /* Parse a declarator-id.
21655 :: [opt] nested-name-specifier [opt] type-name
21657 In the `id-expression' case, the value returned is as for
21658 cp_parser_id_expression if the id-expression was an unqualified-id.
21659 If the id-expression was a qualified-id, then a SCOPE_REF is
21660 returned. The first operand is the scope (either a NAMESPACE_DECL
21661 or TREE_TYPE), but the second is still just a representation of an
21665 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21668 /* The expression must be an id-expression. Assume that qualified
21669 names are the names of types so that:
21672 int S<T>::R::i = 3;
21674 will work; we must treat `S<T>::R' as the name of a type.
21675 Similarly, assume that qualified names are templates, where
21679 int S<T>::R<T>::i = 3;
21682 id
= cp_parser_id_expression (parser
,
21683 /*template_keyword_p=*/false,
21684 /*check_dependency_p=*/false,
21685 /*template_p=*/NULL
,
21686 /*declarator_p=*/true,
21688 if (id
&& BASELINK_P (id
))
21689 id
= BASELINK_FUNCTIONS (id
);
21693 /* Parse a type-id.
21696 type-specifier-seq abstract-declarator [opt]
21698 The parser flags FLAGS is used to control type-specifier parsing.
21700 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21702 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21703 i.e. we've just seen "->".
21705 Returns the TYPE specified. */
21708 cp_parser_type_id_1 (cp_parser
*parser
, cp_parser_flags flags
,
21709 bool is_template_arg
, bool is_trailing_return
,
21710 location_t
*type_location
)
21712 cp_decl_specifier_seq type_specifier_seq
;
21713 cp_declarator
*abstract_declarator
;
21715 /* Parse the type-specifier-seq. */
21716 cp_parser_type_specifier_seq (parser
, flags
,
21717 /*is_declaration=*/false,
21718 is_trailing_return
,
21719 &type_specifier_seq
);
21721 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
21723 if (is_template_arg
&& type_specifier_seq
.type
21724 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21725 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21726 /* A bare template name as a template argument is a template template
21727 argument, not a placeholder, so fail parsing it as a type argument. */
21729 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21730 cp_parser_simulate_error (parser
);
21731 return error_mark_node
;
21733 if (type_specifier_seq
.type
== error_mark_node
)
21734 return error_mark_node
;
21736 /* There might or might not be an abstract declarator. */
21737 cp_parser_parse_tentatively (parser
);
21738 /* Look for the declarator. */
21739 abstract_declarator
21740 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
,
21741 CP_PARSER_FLAGS_NONE
, NULL
,
21742 /*parenthesized_p=*/NULL
,
21743 /*member_p=*/false,
21744 /*friend_p=*/false,
21745 /*static_p=*/false);
21746 /* Check to see if there really was a declarator. */
21747 if (!cp_parser_parse_definitely (parser
))
21748 abstract_declarator
= NULL
;
21750 if (type_specifier_seq
.type
21751 /* The concepts TS allows 'auto' as a type-id. */
21752 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21753 /* None of the valid uses of 'auto' in C++14 involve the type-id
21754 nonterminal, but it is valid in a trailing-return-type. */
21755 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21756 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21758 /* A type-id with type 'auto' is only ok if the abstract declarator
21759 is a function declarator with a late-specified return type.
21761 A type-id with 'auto' is also valid in a trailing-return-type
21762 in a compound-requirement. */
21763 if (abstract_declarator
21764 && abstract_declarator
->kind
== cdk_function
21765 && abstract_declarator
->u
.function
.late_return_type
)
21767 else if (parser
->in_result_type_constraint_p
)
21771 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21772 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21774 error_at (loc
, "missing template arguments after %qT",
21776 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21780 error_at (loc
, "invalid use of %qT", auto_node
);
21781 return error_mark_node
;
21785 return groktypename (&type_specifier_seq
, abstract_declarator
,
21789 /* Wrapper for cp_parser_type_id_1. */
21792 cp_parser_type_id (cp_parser
*parser
, cp_parser_flags flags
,
21793 location_t
*type_location
)
21795 return cp_parser_type_id_1 (parser
, flags
, false, false, type_location
);
21798 /* Wrapper for cp_parser_type_id_1. */
21801 cp_parser_template_type_arg (cp_parser
*parser
)
21804 const char *saved_message
= parser
->type_definition_forbidden_message
;
21805 parser
->type_definition_forbidden_message
21806 = G_("types may not be defined in template arguments");
21807 r
= cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_NONE
, true, false, NULL
);
21808 parser
->type_definition_forbidden_message
= saved_message
;
21809 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21811 error ("invalid use of %<auto%> in template argument");
21812 r
= error_mark_node
;
21817 /* Wrapper for cp_parser_type_id_1. */
21820 cp_parser_trailing_type_id (cp_parser
*parser
)
21822 return cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
21823 false, true, NULL
);
21826 /* Parse a type-specifier-seq.
21828 type-specifier-seq:
21829 type-specifier type-specifier-seq [opt]
21833 type-specifier-seq:
21834 attributes type-specifier-seq [opt]
21836 The parser flags FLAGS is used to control type-specifier parsing.
21838 If IS_DECLARATION is true, we are at the start of a "condition" or
21839 exception-declaration, so we might be followed by a declarator-id.
21841 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21842 i.e. we've just seen "->".
21844 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21847 cp_parser_type_specifier_seq (cp_parser
* parser
,
21848 cp_parser_flags flags
,
21849 bool is_declaration
,
21850 bool is_trailing_return
,
21851 cp_decl_specifier_seq
*type_specifier_seq
)
21853 bool seen_type_specifier
= false;
21854 cp_token
*start_token
= NULL
;
21856 /* Clear the TYPE_SPECIFIER_SEQ. */
21857 clear_decl_specs (type_specifier_seq
);
21859 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
21860 /* In the context of a trailing return type, enum E { } is an
21861 elaborated-type-specifier followed by a function-body, not an
21863 if (is_trailing_return
)
21864 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21866 /* Parse the type-specifiers and attributes. */
21869 tree type_specifier
;
21870 bool is_cv_qualifier
;
21872 /* Check for attributes first. */
21873 if (cp_next_tokens_can_be_attribute_p (parser
))
21875 type_specifier_seq
->attributes
21876 = attr_chainon (type_specifier_seq
->attributes
,
21877 cp_parser_attributes_opt (parser
));
21881 /* record the token of the beginning of the type specifier seq,
21882 for error reporting purposes*/
21884 start_token
= cp_lexer_peek_token (parser
->lexer
);
21886 /* Look for the type-specifier. */
21887 type_specifier
= cp_parser_type_specifier (parser
,
21889 type_specifier_seq
,
21890 /*is_declaration=*/false,
21893 if (!type_specifier
)
21895 /* If the first type-specifier could not be found, this is not a
21896 type-specifier-seq at all. */
21897 if (!seen_type_specifier
)
21899 /* Set in_declarator_p to avoid skipping to the semicolon. */
21900 int in_decl
= parser
->in_declarator_p
;
21901 parser
->in_declarator_p
= true;
21903 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21904 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21905 cp_parser_error (parser
, "expected type-specifier");
21907 parser
->in_declarator_p
= in_decl
;
21909 type_specifier_seq
->type
= error_mark_node
;
21912 /* If subsequent type-specifiers could not be found, the
21913 type-specifier-seq is complete. */
21917 seen_type_specifier
= true;
21918 /* The standard says that a condition can be:
21920 type-specifier-seq declarator = assignment-expression
21927 we should treat the "S" as a declarator, not as a
21928 type-specifier. The standard doesn't say that explicitly for
21929 type-specifier-seq, but it does say that for
21930 decl-specifier-seq in an ordinary declaration. Perhaps it
21931 would be clearer just to allow a decl-specifier-seq here, and
21932 then add a semantic restriction that if any decl-specifiers
21933 that are not type-specifiers appear, the program is invalid. */
21934 if (is_declaration
&& !is_cv_qualifier
)
21935 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21939 /* Return whether the function currently being declared has an associated
21940 template parameter list. */
21943 function_being_declared_is_template_p (cp_parser
* parser
)
21945 if (!current_template_parms
|| processing_template_parmlist
)
21948 if (parser
->implicit_template_scope
)
21951 if (at_class_scope_p ()
21952 && TYPE_BEING_DEFINED (current_class_type
))
21953 return parser
->num_template_parameter_lists
!= 0;
21955 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21956 (current_class_type
));
21959 /* Parse a parameter-declaration-clause.
21961 parameter-declaration-clause:
21962 parameter-declaration-list [opt] ... [opt]
21963 parameter-declaration-list , ...
21965 The parser flags FLAGS is used to control type-specifier parsing.
21967 Returns a representation for the parameter declarations. A return
21968 value of NULL indicates a parameter-declaration-clause consisting
21969 only of an ellipsis. */
21972 cp_parser_parameter_declaration_clause (cp_parser
* parser
,
21973 cp_parser_flags flags
)
21979 temp_override
<bool> cleanup
21980 (parser
->auto_is_implicit_function_template_parm_p
);
21982 if (!processing_specialization
21983 && !processing_template_parmlist
21984 && !processing_explicit_instantiation
21985 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21986 actual function or a random abstract declarator. */
21987 && parser
->default_arg_ok_p
)
21988 if (!current_function_decl
21989 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21990 parser
->auto_is_implicit_function_template_parm_p
= true;
21992 /* Peek at the next token. */
21993 token
= cp_lexer_peek_token (parser
->lexer
);
21994 /* Check for trivial parameter-declaration-clauses. */
21995 if (token
->type
== CPP_ELLIPSIS
)
21997 /* Consume the `...' token. */
21998 cp_lexer_consume_token (parser
->lexer
);
22001 else if (token
->type
== CPP_CLOSE_PAREN
)
22002 /* There are no parameters. */
22003 return void_list_node
;
22004 /* Check for `(void)', too, which is a special case. */
22005 else if (token
->keyword
== RID_VOID
22006 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22007 == CPP_CLOSE_PAREN
))
22009 /* Consume the `void' token. */
22010 cp_lexer_consume_token (parser
->lexer
);
22011 /* There are no parameters. */
22012 return void_list_node
;
22015 /* Parse the parameter-declaration-list. */
22016 parameters
= cp_parser_parameter_declaration_list (parser
, flags
);
22017 /* If a parse error occurred while parsing the
22018 parameter-declaration-list, then the entire
22019 parameter-declaration-clause is erroneous. */
22020 if (parameters
== error_mark_node
)
22023 /* Peek at the next token. */
22024 token
= cp_lexer_peek_token (parser
->lexer
);
22025 /* If it's a `,', the clause should terminate with an ellipsis. */
22026 if (token
->type
== CPP_COMMA
)
22028 /* Consume the `,'. */
22029 cp_lexer_consume_token (parser
->lexer
);
22030 /* Expect an ellipsis. */
22032 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
22034 /* It might also be `...' if the optional trailing `,' was
22036 else if (token
->type
== CPP_ELLIPSIS
)
22038 /* Consume the `...' token. */
22039 cp_lexer_consume_token (parser
->lexer
);
22040 /* And remember that we saw it. */
22044 ellipsis_p
= false;
22046 /* Finish the parameter list. */
22048 parameters
= chainon (parameters
, void_list_node
);
22053 /* Parse a parameter-declaration-list.
22055 parameter-declaration-list:
22056 parameter-declaration
22057 parameter-declaration-list , parameter-declaration
22059 The parser flags FLAGS is used to control type-specifier parsing.
22061 Returns a representation of the parameter-declaration-list, as for
22062 cp_parser_parameter_declaration_clause. However, the
22063 `void_list_node' is never appended to the list. */
22066 cp_parser_parameter_declaration_list (cp_parser
* parser
, cp_parser_flags flags
)
22068 tree parameters
= NULL_TREE
;
22069 tree
*tail
= ¶meters
;
22070 bool saved_in_unbraced_linkage_specification_p
;
22073 /* The special considerations that apply to a function within an
22074 unbraced linkage specifications do not apply to the parameters
22075 to the function. */
22076 saved_in_unbraced_linkage_specification_p
22077 = parser
->in_unbraced_linkage_specification_p
;
22078 parser
->in_unbraced_linkage_specification_p
= false;
22080 /* Look for more parameters. */
22083 cp_parameter_declarator
*parameter
;
22084 tree decl
= error_mark_node
;
22085 bool parenthesized_p
= false;
22087 /* Parse the parameter. */
22089 = cp_parser_parameter_declaration (parser
, flags
,
22090 /*template_parm_p=*/false,
22093 /* We don't know yet if the enclosing context is deprecated, so wait
22094 and warn in grokparms if appropriate. */
22095 deprecated_state
= DEPRECATED_SUPPRESS
;
22099 decl
= grokdeclarator (parameter
->declarator
,
22100 ¶meter
->decl_specifiers
,
22102 parameter
->default_argument
!= NULL_TREE
,
22103 ¶meter
->decl_specifiers
.attributes
);
22104 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
22105 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
22108 deprecated_state
= DEPRECATED_NORMAL
;
22110 /* If a parse error occurred parsing the parameter declaration,
22111 then the entire parameter-declaration-list is erroneous. */
22112 if (decl
== error_mark_node
)
22114 parameters
= error_mark_node
;
22118 if (parameter
->decl_specifiers
.attributes
)
22119 cplus_decl_attributes (&decl
,
22120 parameter
->decl_specifiers
.attributes
,
22122 if (DECL_NAME (decl
))
22123 decl
= pushdecl (decl
);
22125 if (decl
!= error_mark_node
)
22127 retrofit_lang_decl (decl
);
22128 DECL_PARM_INDEX (decl
) = ++index
;
22129 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
22132 /* Add the new parameter to the list. */
22133 *tail
= build_tree_list (parameter
->default_argument
, decl
);
22134 tail
= &TREE_CHAIN (*tail
);
22136 /* Peek at the next token. */
22137 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
22138 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
22139 /* These are for Objective-C++ */
22140 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22141 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22142 /* The parameter-declaration-list is complete. */
22144 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22148 /* Peek at the next token. */
22149 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22150 /* If it's an ellipsis, then the list is complete. */
22151 if (token
->type
== CPP_ELLIPSIS
)
22153 /* Otherwise, there must be more parameters. Consume the
22155 cp_lexer_consume_token (parser
->lexer
);
22156 /* When parsing something like:
22158 int i(float f, double d)
22160 we can tell after seeing the declaration for "f" that we
22161 are not looking at an initialization of a variable "i",
22162 but rather at the declaration of a function "i".
22164 Due to the fact that the parsing of template arguments
22165 (as specified to a template-id) requires backtracking we
22166 cannot use this technique when inside a template argument
22168 if (!parser
->in_template_argument_list_p
22169 && !parser
->in_type_id_in_expr_p
22170 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22171 /* However, a parameter-declaration of the form
22172 "float(f)" (which is a valid declaration of a
22173 parameter "f") can also be interpreted as an
22174 expression (the conversion of "f" to "float"). */
22175 && !parenthesized_p
)
22176 cp_parser_commit_to_tentative_parse (parser
);
22180 cp_parser_error (parser
, "expected %<,%> or %<...%>");
22181 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
22182 cp_parser_skip_to_closing_parenthesis (parser
,
22183 /*recovering=*/true,
22184 /*or_comma=*/false,
22185 /*consume_paren=*/false);
22190 parser
->in_unbraced_linkage_specification_p
22191 = saved_in_unbraced_linkage_specification_p
;
22193 /* Reset implicit_template_scope if we are about to leave the function
22194 parameter list that introduced it. Note that for out-of-line member
22195 definitions, there will be one or more class scopes before we get to
22196 the template parameter scope. */
22198 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
22199 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
22201 while (maybe_its
->kind
== sk_class
)
22202 maybe_its
= maybe_its
->level_chain
;
22203 if (maybe_its
== its
)
22205 parser
->implicit_template_parms
= 0;
22206 parser
->implicit_template_scope
= 0;
22213 /* Parse a parameter declaration.
22215 parameter-declaration:
22216 decl-specifier-seq ... [opt] declarator
22217 decl-specifier-seq declarator = assignment-expression
22218 decl-specifier-seq ... [opt] abstract-declarator [opt]
22219 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22221 The parser flags FLAGS is used to control type-specifier parsing.
22223 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22224 declares a template parameter. (In that case, a non-nested `>'
22225 token encountered during the parsing of the assignment-expression
22226 is not interpreted as a greater-than operator.)
22228 Returns a representation of the parameter, or NULL if an error
22229 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22230 true iff the declarator is of the form "(p)". */
22232 static cp_parameter_declarator
*
22233 cp_parser_parameter_declaration (cp_parser
*parser
,
22234 cp_parser_flags flags
,
22235 bool template_parm_p
,
22236 bool *parenthesized_p
)
22238 int declares_class_or_enum
;
22239 cp_decl_specifier_seq decl_specifiers
;
22240 cp_declarator
*declarator
;
22241 tree default_argument
;
22242 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
22243 const char *saved_message
;
22244 bool template_parameter_pack_p
= false;
22246 /* In a template parameter, `>' is not an operator.
22250 When parsing a default template-argument for a non-type
22251 template-parameter, the first non-nested `>' is taken as the end
22252 of the template parameter-list rather than a greater-than
22255 /* Type definitions may not appear in parameter types. */
22256 saved_message
= parser
->type_definition_forbidden_message
;
22257 parser
->type_definition_forbidden_message
22258 = G_("types may not be defined in parameter types");
22260 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
22261 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22262 (current_template_parms
)) : 0);
22264 /* Parse the declaration-specifiers. */
22265 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
22266 cp_parser_decl_specifier_seq (parser
,
22269 &declares_class_or_enum
);
22271 /* Complain about missing 'typename' or other invalid type names. */
22272 if (!decl_specifiers
.any_type_specifiers_p
22273 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22274 decl_specifiers
.type
= error_mark_node
;
22276 /* If an error occurred, there's no reason to attempt to parse the
22277 rest of the declaration. */
22278 if (cp_parser_error_occurred (parser
))
22280 parser
->type_definition_forbidden_message
= saved_message
;
22284 /* Peek at the next token. */
22285 token
= cp_lexer_peek_token (parser
->lexer
);
22287 /* If the next token is a `)', `,', `=', `>', or `...', then there
22288 is no declarator. However, when variadic templates are enabled,
22289 there may be a declarator following `...'. */
22290 if (token
->type
== CPP_CLOSE_PAREN
22291 || token
->type
== CPP_COMMA
22292 || token
->type
== CPP_EQ
22293 || token
->type
== CPP_GREATER
)
22296 if (parenthesized_p
)
22297 *parenthesized_p
= false;
22299 /* Otherwise, there should be a declarator. */
22302 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22303 parser
->default_arg_ok_p
= false;
22305 /* After seeing a decl-specifier-seq, if the next token is not a
22306 "(", there is no possibility that the code is a valid
22307 expression. Therefore, if parsing tentatively, we commit at
22309 if (!parser
->in_template_argument_list_p
22310 /* In an expression context, having seen:
22314 we cannot be sure whether we are looking at a
22315 function-type (taking a "char" as a parameter) or a cast
22316 of some object of type "char" to "int". */
22317 && !parser
->in_type_id_in_expr_p
22318 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22319 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
22320 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
22321 cp_parser_commit_to_tentative_parse (parser
);
22322 /* Parse the declarator. */
22323 declarator_token_start
= token
;
22324 declarator
= cp_parser_declarator (parser
,
22325 CP_PARSER_DECLARATOR_EITHER
,
22326 CP_PARSER_FLAGS_NONE
,
22327 /*ctor_dtor_or_conv_p=*/NULL
,
22329 /*member_p=*/false,
22330 /*friend_p=*/false,
22331 /*static_p=*/false);
22332 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22333 /* After the declarator, allow more attributes. */
22334 decl_specifiers
.attributes
22335 = attr_chainon (decl_specifiers
.attributes
,
22336 cp_parser_attributes_opt (parser
));
22338 /* If the declarator is a template parameter pack, remember that and
22339 clear the flag in the declarator itself so we don't get errors
22340 from grokdeclarator. */
22341 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
22343 declarator
->parameter_pack_p
= false;
22344 template_parameter_pack_p
= true;
22348 /* If the next token is an ellipsis, and we have not seen a declarator
22349 name, and if either the type of the declarator contains parameter
22350 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22351 for, eg, abbreviated integral type names), then we actually have a
22352 parameter pack expansion expression. Otherwise, leave the ellipsis
22353 for a C-style variadic function. */
22354 token
= cp_lexer_peek_token (parser
->lexer
);
22356 /* If a function parameter pack was specified and an implicit template
22357 parameter was introduced during cp_parser_parameter_declaration,
22358 change any implicit parameters introduced into packs. */
22359 if (parser
->implicit_template_parms
22360 && ((token
->type
== CPP_ELLIPSIS
22361 && declarator_can_be_parameter_pack (declarator
))
22362 || (declarator
&& declarator
->parameter_pack_p
)))
22364 int latest_template_parm_idx
= TREE_VEC_LENGTH
22365 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
22367 if (latest_template_parm_idx
!= template_parm_idx
)
22368 decl_specifiers
.type
= convert_generic_types_to_packs
22369 (decl_specifiers
.type
,
22370 template_parm_idx
, latest_template_parm_idx
);
22373 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22375 tree type
= decl_specifiers
.type
;
22377 if (type
&& DECL_P (type
))
22378 type
= TREE_TYPE (type
);
22381 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
22382 && (template_parm_p
|| uses_parameter_packs (type
)))
22383 || (!type
&& template_parm_p
))
22384 && declarator_can_be_parameter_pack (declarator
))
22386 /* Consume the `...'. */
22387 cp_lexer_consume_token (parser
->lexer
);
22388 maybe_warn_variadic_templates ();
22390 /* Build a pack expansion type */
22391 if (template_parm_p
)
22392 template_parameter_pack_p
= true;
22393 else if (declarator
)
22394 declarator
->parameter_pack_p
= true;
22396 decl_specifiers
.type
= make_pack_expansion (type
);
22400 /* The restriction on defining new types applies only to the type
22401 of the parameter, not to the default argument. */
22402 parser
->type_definition_forbidden_message
= saved_message
;
22404 /* If the next token is `=', then process a default argument. */
22405 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22407 tree type
= decl_specifiers
.type
;
22408 token
= cp_lexer_peek_token (parser
->lexer
);
22409 /* If we are defining a class, then the tokens that make up the
22410 default argument must be saved and processed later. */
22411 if (!template_parm_p
&& at_class_scope_p ()
22412 && TYPE_BEING_DEFINED (current_class_type
)
22413 && !LAMBDA_TYPE_P (current_class_type
))
22414 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
22416 // A constrained-type-specifier may declare a type template-parameter.
22417 else if (declares_constrained_type_template_parameter (type
))
22419 = cp_parser_default_type_template_argument (parser
);
22421 // A constrained-type-specifier may declare a template-template-parameter.
22422 else if (declares_constrained_template_template_parameter (type
))
22424 = cp_parser_default_template_template_argument (parser
);
22426 /* Outside of a class definition, we can just parse the
22427 assignment-expression. */
22430 = cp_parser_default_argument (parser
, template_parm_p
);
22432 if (!parser
->default_arg_ok_p
)
22434 permerror (token
->location
,
22435 "default arguments are only "
22436 "permitted for function parameters");
22438 else if ((declarator
&& declarator
->parameter_pack_p
)
22439 || template_parameter_pack_p
22440 || (decl_specifiers
.type
22441 && PACK_EXPANSION_P (decl_specifiers
.type
)))
22443 /* Find the name of the parameter pack. */
22444 cp_declarator
*id_declarator
= declarator
;
22445 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
22446 id_declarator
= id_declarator
->declarator
;
22448 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
22449 error_at (declarator_token_start
->location
,
22451 ? G_("template parameter pack %qD "
22452 "cannot have a default argument")
22453 : G_("parameter pack %qD cannot have "
22454 "a default argument"),
22455 id_declarator
->u
.id
.unqualified_name
);
22457 error_at (declarator_token_start
->location
,
22459 ? G_("template parameter pack cannot have "
22460 "a default argument")
22461 : G_("parameter pack cannot have a "
22462 "default argument"));
22464 default_argument
= NULL_TREE
;
22468 default_argument
= NULL_TREE
;
22470 if (default_argument
)
22471 STRIP_ANY_LOCATION_WRAPPER (default_argument
);
22473 /* Generate a location for the parameter, ranging from the start of the
22474 initial token to the end of the final token (using input_location for
22475 the latter, set up by cp_lexer_set_source_position_from_token when
22478 If we have a identifier, then use it for the caret location, e.g.
22480 extern int callee (int one, int (*two)(int, int), float three);
22481 ~~~~~~^~~~~~~~~~~~~~
22483 otherwise, reuse the start location for the caret location e.g.:
22485 extern int callee (int one, int (*)(int, int), float three);
22489 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
22490 ? declarator
->id_loc
22491 : decl_spec_token_start
->location
);
22492 location_t param_loc
= make_location (caret_loc
,
22493 decl_spec_token_start
->location
,
22496 return make_parameter_declarator (&decl_specifiers
,
22500 template_parameter_pack_p
);
22503 /* Parse a default argument and return it.
22505 TEMPLATE_PARM_P is true if this is a default argument for a
22506 non-type template parameter. */
22508 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
22510 tree default_argument
= NULL_TREE
;
22511 bool saved_greater_than_is_operator_p
;
22512 unsigned char saved_local_variables_forbidden_p
;
22513 bool non_constant_p
, is_direct_init
;
22515 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22517 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
22518 parser
->greater_than_is_operator_p
= !template_parm_p
;
22519 /* Local variable names (and the `this' keyword) may not
22520 appear in a default argument. */
22521 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22522 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
22523 /* Parse the assignment-expression. */
22524 if (template_parm_p
)
22525 push_deferring_access_checks (dk_no_deferred
);
22526 tree saved_class_ptr
= NULL_TREE
;
22527 tree saved_class_ref
= NULL_TREE
;
22528 /* The "this" pointer is not valid in a default argument. */
22531 saved_class_ptr
= current_class_ptr
;
22532 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
22533 saved_class_ref
= current_class_ref
;
22534 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
22537 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
22538 /* Restore the "this" pointer. */
22541 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
22542 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
22544 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
22545 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22546 if (template_parm_p
)
22547 pop_deferring_access_checks ();
22548 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
22549 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22551 return default_argument
;
22554 /* Parse a function-body.
22557 compound_statement */
22560 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
22562 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
22563 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
22567 /* Parse a ctor-initializer-opt followed by a function-body. Return
22568 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22569 is true we are parsing a function-try-block. */
22572 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
22573 bool in_function_try_block
)
22576 const bool check_body_p
=
22577 DECL_CONSTRUCTOR_P (current_function_decl
)
22578 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
22581 /* Begin the function body. */
22582 body
= begin_function_body ();
22583 /* Parse the optional ctor-initializer. */
22584 cp_parser_ctor_initializer_opt (parser
);
22586 /* If we're parsing a constexpr constructor definition, we need
22587 to check that the constructor body is indeed empty. However,
22588 before we get to cp_parser_function_body lot of junk has been
22589 generated, so we can't just check that we have an empty block.
22590 Rather we take a snapshot of the outermost block, and check whether
22591 cp_parser_function_body changed its state. */
22594 list
= cur_stmt_list
;
22595 if (STATEMENT_LIST_TAIL (list
))
22596 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
22598 /* Parse the function-body. */
22599 cp_parser_function_body (parser
, in_function_try_block
);
22601 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
22602 /* Finish the function body. */
22603 finish_function_body (body
);
22606 /* Parse an initializer.
22609 = initializer-clause
22610 ( expression-list )
22612 Returns an expression representing the initializer. If no
22613 initializer is present, NULL_TREE is returned.
22615 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22616 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22617 set to TRUE if there is no initializer present. If there is an
22618 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22619 is set to true; otherwise it is set to false. */
22622 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
22623 bool* non_constant_p
, bool subexpression_p
)
22628 /* Peek at the next token. */
22629 token
= cp_lexer_peek_token (parser
->lexer
);
22631 /* Let our caller know whether or not this initializer was
22633 *is_direct_init
= (token
->type
!= CPP_EQ
);
22634 /* Assume that the initializer is constant. */
22635 *non_constant_p
= false;
22637 if (token
->type
== CPP_EQ
)
22639 /* Consume the `='. */
22640 cp_lexer_consume_token (parser
->lexer
);
22641 /* Parse the initializer-clause. */
22642 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22644 else if (token
->type
== CPP_OPEN_PAREN
)
22646 vec
<tree
, va_gc
> *vec
;
22647 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22649 /*allow_expansion_p=*/true,
22652 return error_mark_node
;
22653 init
= build_tree_list_vec (vec
);
22654 release_tree_vector (vec
);
22656 else if (token
->type
== CPP_OPEN_BRACE
)
22658 cp_lexer_set_source_position (parser
->lexer
);
22659 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22660 init
= cp_parser_braced_list (parser
, non_constant_p
);
22661 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22665 /* Anything else is an error. */
22666 cp_parser_error (parser
, "expected initializer");
22667 init
= error_mark_node
;
22670 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22671 init
= error_mark_node
;
22676 /* Parse an initializer-clause.
22678 initializer-clause:
22679 assignment-expression
22682 Returns an expression representing the initializer.
22684 If the `assignment-expression' production is used the value
22685 returned is simply a representation for the expression.
22687 Otherwise, calls cp_parser_braced_list. */
22690 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22692 cp_expr initializer
;
22694 /* Assume the expression is constant. */
22695 *non_constant_p
= false;
22697 /* If it is not a `{', then we are looking at an
22698 assignment-expression. */
22699 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22702 = cp_parser_constant_expression (parser
,
22703 /*allow_non_constant_p=*/true,
22707 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22709 return initializer
;
22712 /* Parse a brace-enclosed initializer list.
22715 { initializer-list , [opt] }
22716 { designated-initializer-list , [opt] }
22719 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22720 the elements of the initializer-list (or NULL, if the last
22721 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22722 NULL_TREE. There is no way to detect whether or not the optional
22723 trailing `,' was provided. NON_CONSTANT_P is as for
22724 cp_parser_initializer. */
22727 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22730 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22732 /* Consume the `{' token. */
22733 matching_braces braces
;
22734 braces
.require_open (parser
);
22735 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22736 initializer
= make_node (CONSTRUCTOR
);
22737 /* If it's not a `}', then there is a non-trivial initializer. */
22738 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22740 /* Parse the initializer list. */
22741 CONSTRUCTOR_ELTS (initializer
)
22742 = cp_parser_initializer_list (parser
, non_constant_p
);
22743 /* A trailing `,' token is allowed. */
22744 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22745 cp_lexer_consume_token (parser
->lexer
);
22748 *non_constant_p
= false;
22749 /* Now, there should be a trailing `}'. */
22750 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22751 braces
.require_close (parser
);
22752 TREE_TYPE (initializer
) = init_list_type_node
;
22754 cp_expr
result (initializer
);
22755 /* Build a location of the form:
22758 with caret==start at the open brace, finish at the close brace. */
22759 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22760 result
.set_location (combined_loc
);
22764 /* Consume tokens up to, and including, the next non-nested closing `]'.
22765 Returns true iff we found a closing `]'. */
22768 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22770 unsigned square_depth
= 0;
22774 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22776 switch (token
->type
)
22778 case CPP_PRAGMA_EOL
:
22779 if (!parser
->lexer
->in_pragma
)
22783 /* If we've run out of tokens, then there is no closing `]'. */
22786 case CPP_OPEN_SQUARE
:
22790 case CPP_CLOSE_SQUARE
:
22791 if (!square_depth
--)
22793 cp_lexer_consume_token (parser
->lexer
);
22802 /* Consume the token. */
22803 cp_lexer_consume_token (parser
->lexer
);
22807 /* Return true if we are looking at an array-designator, false otherwise. */
22810 cp_parser_array_designator_p (cp_parser
*parser
)
22812 /* Consume the `['. */
22813 cp_lexer_consume_token (parser
->lexer
);
22815 cp_lexer_save_tokens (parser
->lexer
);
22817 /* Skip tokens until the next token is a closing square bracket.
22818 If we find the closing `]', and the next token is a `=', then
22819 we are looking at an array designator. */
22820 bool array_designator_p
22821 = (cp_parser_skip_to_closing_square_bracket (parser
)
22822 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22824 /* Roll back the tokens we skipped. */
22825 cp_lexer_rollback_tokens (parser
->lexer
);
22827 return array_designator_p
;
22830 /* Parse an initializer-list.
22833 initializer-clause ... [opt]
22834 initializer-list , initializer-clause ... [opt]
22838 designated-initializer-list:
22839 designated-initializer-clause
22840 designated-initializer-list , designated-initializer-clause
22842 designated-initializer-clause:
22843 designator brace-or-equal-initializer
22851 designation initializer-clause ...[opt]
22852 initializer-list , designation initializer-clause ...[opt]
22857 [ constant-expression ] =
22859 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22860 for the initializer. If the INDEX of the elt is non-NULL, it is the
22861 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22862 as for cp_parser_initializer. */
22864 static vec
<constructor_elt
, va_gc
> *
22865 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22867 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22868 bool first_p
= true;
22869 tree first_designator
= NULL_TREE
;
22871 /* Assume all of the expressions are constant. */
22872 *non_constant_p
= false;
22874 /* Parse the rest of the list. */
22880 bool clause_non_constant_p
;
22881 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22883 /* Handle the C++2A syntax, '. id ='. */
22884 if ((cxx_dialect
>= cxx2a
22885 || cp_parser_allow_gnu_extensions_p (parser
))
22886 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22887 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22888 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22889 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22890 == CPP_OPEN_BRACE
)))
22892 if (cxx_dialect
< cxx2a
)
22893 pedwarn (loc
, OPT_Wpedantic
,
22894 "C++ designated initializers only available with "
22895 "-std=c++2a or -std=gnu++2a");
22896 /* Consume the `.'. */
22897 cp_lexer_consume_token (parser
->lexer
);
22898 /* Consume the identifier. */
22899 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22900 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22901 /* Consume the `='. */
22902 cp_lexer_consume_token (parser
->lexer
);
22904 /* Also, if the next token is an identifier and the following one is a
22905 colon, we are looking at the GNU designated-initializer
22907 else if (cp_parser_allow_gnu_extensions_p (parser
)
22908 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22909 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22912 /* Warn the user that they are using an extension. */
22913 pedwarn (loc
, OPT_Wpedantic
,
22914 "ISO C++ does not allow GNU designated initializers");
22915 /* Consume the identifier. */
22916 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22917 /* Consume the `:'. */
22918 cp_lexer_consume_token (parser
->lexer
);
22920 /* Also handle C99 array designators, '[ const ] ='. */
22921 else if (cp_parser_allow_gnu_extensions_p (parser
)
22922 && !c_dialect_objc ()
22923 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22925 /* In C++11, [ could start a lambda-introducer. */
22926 bool non_const
= false;
22928 cp_parser_parse_tentatively (parser
);
22930 if (!cp_parser_array_designator_p (parser
))
22932 cp_parser_simulate_error (parser
);
22933 designator
= NULL_TREE
;
22937 designator
= cp_parser_constant_expression (parser
, true,
22939 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22940 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22943 if (!cp_parser_parse_definitely (parser
))
22944 designator
= NULL_TREE
;
22946 && (!require_potential_rvalue_constant_expression
22948 designator
= NULL_TREE
;
22950 /* Warn the user that they are using an extension. */
22951 pedwarn (loc
, OPT_Wpedantic
,
22952 "ISO C++ does not allow C99 designated initializers");
22955 designator
= NULL_TREE
;
22959 first_designator
= designator
;
22962 else if (cxx_dialect
>= cxx2a
22963 && first_designator
!= error_mark_node
22964 && (!first_designator
!= !designator
))
22966 error_at (loc
, "either all initializer clauses should be designated "
22967 "or none of them should be");
22968 first_designator
= error_mark_node
;
22970 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22971 first_designator
= designator
;
22973 /* Parse the initializer. */
22974 initializer
= cp_parser_initializer_clause (parser
,
22975 &clause_non_constant_p
);
22976 /* If any clause is non-constant, so is the entire initializer. */
22977 if (clause_non_constant_p
)
22978 *non_constant_p
= true;
22980 /* If we have an ellipsis, this is an initializer pack
22982 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22984 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22986 /* Consume the `...'. */
22987 cp_lexer_consume_token (parser
->lexer
);
22989 if (designator
&& cxx_dialect
>= cxx2a
)
22991 "%<...%> not allowed in designated initializer list");
22993 /* Turn the initializer into an initializer expansion. */
22994 initializer
= make_pack_expansion (initializer
);
22997 /* Add it to the vector. */
22998 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
23000 /* If the next token is not a comma, we have reached the end of
23002 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23005 /* Peek at the next token. */
23006 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23007 /* If the next token is a `}', then we're still done. An
23008 initializer-clause can have a trailing `,' after the
23009 initializer-list and before the closing `}'. */
23010 if (token
->type
== CPP_CLOSE_BRACE
)
23013 /* Consume the `,' token. */
23014 cp_lexer_consume_token (parser
->lexer
);
23017 /* The same identifier shall not appear in multiple designators
23018 of a designated-initializer-list. */
23019 if (first_designator
)
23022 tree designator
, val
;
23023 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23024 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23026 if (IDENTIFIER_MARKED (designator
))
23028 error_at (cp_expr_loc_or_loc (val
, input_location
),
23029 "%<.%s%> designator used multiple times in "
23030 "the same initializer list",
23031 IDENTIFIER_POINTER (designator
));
23032 (*v
)[i
].index
= error_mark_node
;
23035 IDENTIFIER_MARKED (designator
) = 1;
23037 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23038 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23039 IDENTIFIER_MARKED (designator
) = 0;
23045 /* Classes [gram.class] */
23047 /* Parse a class-name.
23053 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23054 to indicate that names looked up in dependent types should be
23055 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23056 keyword has been used to indicate that the name that appears next
23057 is a template. TAG_TYPE indicates the explicit tag given before
23058 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23059 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23060 is the class being defined in a class-head. If ENUM_OK is TRUE,
23061 enum-names are also accepted.
23063 Returns the TYPE_DECL representing the class. */
23066 cp_parser_class_name (cp_parser
*parser
,
23067 bool typename_keyword_p
,
23068 bool template_keyword_p
,
23069 enum tag_types tag_type
,
23070 bool check_dependency_p
,
23072 bool is_declaration
,
23079 tree identifier
= NULL_TREE
;
23081 /* All class-names start with an identifier. */
23082 token
= cp_lexer_peek_token (parser
->lexer
);
23083 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
23085 cp_parser_error (parser
, "expected class-name");
23086 return error_mark_node
;
23089 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23090 to a template-id, so we save it here. */
23091 scope
= parser
->scope
;
23092 if (scope
== error_mark_node
)
23093 return error_mark_node
;
23095 /* Any name names a type if we're following the `typename' keyword
23096 in a qualified name where the enclosing scope is type-dependent. */
23097 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
23098 && dependent_type_p (scope
));
23099 /* Handle the common case (an identifier, but not a template-id)
23101 if (token
->type
== CPP_NAME
23102 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
23104 cp_token
*identifier_token
;
23107 /* Look for the identifier. */
23108 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
23109 ambiguous_p
= identifier_token
->error_reported
;
23110 identifier
= cp_parser_identifier (parser
);
23111 /* If the next token isn't an identifier, we are certainly not
23112 looking at a class-name. */
23113 if (identifier
== error_mark_node
)
23114 decl
= error_mark_node
;
23115 /* If we know this is a type-name, there's no need to look it
23117 else if (typename_p
)
23121 tree ambiguous_decls
;
23122 /* If we already know that this lookup is ambiguous, then
23123 we've already issued an error message; there's no reason
23127 cp_parser_simulate_error (parser
);
23128 return error_mark_node
;
23130 /* If the next token is a `::', then the name must be a type
23133 [basic.lookup.qual]
23135 During the lookup for a name preceding the :: scope
23136 resolution operator, object, function, and enumerator
23137 names are ignored. */
23138 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23139 tag_type
= scope_type
;
23140 /* Look up the name. */
23141 decl
= cp_parser_lookup_name (parser
, identifier
,
23143 /*is_template=*/false,
23144 /*is_namespace=*/false,
23145 check_dependency_p
,
23147 identifier_token
->location
);
23148 if (ambiguous_decls
)
23150 if (cp_parser_parsing_tentatively (parser
))
23151 cp_parser_simulate_error (parser
);
23152 return error_mark_node
;
23158 /* Try a template-id. */
23159 decl
= cp_parser_template_id (parser
, template_keyword_p
,
23160 check_dependency_p
,
23163 if (decl
== error_mark_node
)
23164 return error_mark_node
;
23167 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
23169 /* If this is a typename, create a TYPENAME_TYPE. */
23170 if (typename_p
&& decl
!= error_mark_node
)
23172 decl
= make_typename_type (scope
, decl
, typename_type
,
23173 /*complain=*/tf_error
);
23174 if (decl
!= error_mark_node
)
23175 decl
= TYPE_NAME (decl
);
23178 decl
= strip_using_decl (decl
);
23180 /* Check to see that it is really the name of a class. */
23181 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
23182 && identifier_p (TREE_OPERAND (decl
, 0))
23183 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23184 /* Situations like this:
23186 template <typename T> struct A {
23187 typename T::template X<int>::I i;
23190 are problematic. Is `T::template X<int>' a class-name? The
23191 standard does not seem to be definitive, but there is no other
23192 valid interpretation of the following `::'. Therefore, those
23193 names are considered class-names. */
23195 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
23196 if (decl
!= error_mark_node
)
23197 decl
= TYPE_NAME (decl
);
23199 else if (TREE_CODE (decl
) != TYPE_DECL
23200 || TREE_TYPE (decl
) == error_mark_node
23201 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
23202 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
23203 /* In Objective-C 2.0, a classname followed by '.' starts a
23204 dot-syntax expression, and it's not a type-name. */
23205 || (c_dialect_objc ()
23206 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
23207 && objc_is_class_name (decl
)))
23208 decl
= error_mark_node
;
23210 if (decl
== error_mark_node
)
23211 cp_parser_error (parser
, "expected class-name");
23212 else if (identifier
&& !parser
->scope
)
23213 maybe_note_name_used_in_class (identifier
, decl
);
23218 /* Parse a class-specifier.
23221 class-head { member-specification [opt] }
23223 Returns the TREE_TYPE representing the class. */
23226 cp_parser_class_specifier_1 (cp_parser
* parser
)
23229 tree attributes
= NULL_TREE
;
23230 bool nested_name_specifier_p
;
23231 unsigned saved_num_template_parameter_lists
;
23232 bool saved_in_function_body
;
23233 unsigned char in_statement
;
23234 bool in_switch_statement_p
;
23235 bool saved_in_unbraced_linkage_specification_p
;
23236 tree old_scope
= NULL_TREE
;
23237 tree scope
= NULL_TREE
;
23238 cp_token
*closing_brace
;
23240 push_deferring_access_checks (dk_no_deferred
);
23242 /* Parse the class-head. */
23243 type
= cp_parser_class_head (parser
,
23244 &nested_name_specifier_p
);
23245 /* If the class-head was a semantic disaster, skip the entire body
23249 cp_parser_skip_to_end_of_block_or_statement (parser
);
23250 pop_deferring_access_checks ();
23251 return error_mark_node
;
23254 /* Look for the `{'. */
23255 matching_braces braces
;
23256 if (!braces
.require_open (parser
))
23258 pop_deferring_access_checks ();
23259 return error_mark_node
;
23262 cp_ensure_no_omp_declare_simd (parser
);
23263 cp_ensure_no_oacc_routine (parser
);
23265 /* Issue an error message if type-definitions are forbidden here. */
23266 bool type_definition_ok_p
= cp_parser_check_type_definition (parser
);
23267 /* Remember that we are defining one more class. */
23268 ++parser
->num_classes_being_defined
;
23269 /* Inside the class, surrounding template-parameter-lists do not
23271 saved_num_template_parameter_lists
23272 = parser
->num_template_parameter_lists
;
23273 parser
->num_template_parameter_lists
= 0;
23274 /* We are not in a function body. */
23275 saved_in_function_body
= parser
->in_function_body
;
23276 parser
->in_function_body
= false;
23277 /* Or in a loop. */
23278 in_statement
= parser
->in_statement
;
23279 parser
->in_statement
= 0;
23280 /* Or in a switch. */
23281 in_switch_statement_p
= parser
->in_switch_statement_p
;
23282 parser
->in_switch_statement_p
= false;
23283 /* We are not immediately inside an extern "lang" block. */
23284 saved_in_unbraced_linkage_specification_p
23285 = parser
->in_unbraced_linkage_specification_p
;
23286 parser
->in_unbraced_linkage_specification_p
= false;
23288 // Associate constraints with the type.
23290 type
= associate_classtype_constraints (type
);
23292 /* Start the class. */
23293 if (nested_name_specifier_p
)
23295 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
23296 old_scope
= push_inner_scope (scope
);
23298 type
= begin_class_definition (type
);
23300 if (type
== error_mark_node
)
23301 /* If the type is erroneous, skip the entire body of the class. */
23302 cp_parser_skip_to_closing_brace (parser
);
23304 /* Parse the member-specification. */
23305 cp_parser_member_specification_opt (parser
);
23307 /* Look for the trailing `}'. */
23308 closing_brace
= braces
.require_close (parser
);
23309 /* Look for trailing attributes to apply to this class. */
23310 if (cp_parser_allow_gnu_extensions_p (parser
))
23311 attributes
= cp_parser_gnu_attributes_opt (parser
);
23312 if (type
!= error_mark_node
)
23313 type
= finish_struct (type
, attributes
);
23314 if (nested_name_specifier_p
)
23315 pop_inner_scope (old_scope
, scope
);
23317 /* We've finished a type definition. Check for the common syntax
23318 error of forgetting a semicolon after the definition. We need to
23319 be careful, as we can't just check for not-a-semicolon and be done
23320 with it; the user might have typed:
23322 class X { } c = ...;
23323 class X { } *p = ...;
23325 and so forth. Instead, enumerate all the possible tokens that
23326 might follow this production; if we don't see one of them, then
23327 complain and silently insert the semicolon. */
23329 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23330 bool want_semicolon
= true;
23332 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23333 /* Don't try to parse c++11 attributes here. As per the
23334 grammar, that should be a task for
23335 cp_parser_decl_specifier_seq. */
23336 want_semicolon
= false;
23338 switch (token
->type
)
23341 case CPP_SEMICOLON
:
23344 case CPP_OPEN_PAREN
:
23345 case CPP_CLOSE_PAREN
:
23347 want_semicolon
= false;
23350 /* While it's legal for type qualifiers and storage class
23351 specifiers to follow type definitions in the grammar, only
23352 compiler testsuites contain code like that. Assume that if
23353 we see such code, then what we're really seeing is a case
23357 const <type> var = ...;
23362 static <type> func (...) ...
23364 i.e. the qualifier or specifier applies to the next
23365 declaration. To do so, however, we need to look ahead one
23366 more token to see if *that* token is a type specifier.
23368 This code could be improved to handle:
23371 static const <type> var = ...; */
23373 if (keyword_is_decl_specifier (token
->keyword
))
23375 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23377 /* Handling user-defined types here would be nice, but very
23380 = (lookahead
->type
== CPP_KEYWORD
23381 && keyword_begins_type_specifier (lookahead
->keyword
));
23388 /* If we don't have a type, then something is very wrong and we
23389 shouldn't try to do anything clever. Likewise for not seeing the
23391 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
23393 /* Locate the closing brace. */
23394 cp_token_position prev
23395 = cp_lexer_previous_token_position (parser
->lexer
);
23396 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
23397 location_t loc
= prev_token
->location
;
23399 /* We want to suggest insertion of a ';' immediately *after* the
23400 closing brace, so, if we can, offset the location by 1 column. */
23401 location_t next_loc
= loc
;
23402 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
23403 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
23405 rich_location
richloc (line_table
, next_loc
);
23407 /* If we successfully offset the location, suggest the fix-it. */
23408 if (next_loc
!= loc
)
23409 richloc
.add_fixit_insert_before (next_loc
, ";");
23411 if (CLASSTYPE_DECLARED_CLASS (type
))
23412 error_at (&richloc
,
23413 "expected %<;%> after class definition");
23414 else if (TREE_CODE (type
) == RECORD_TYPE
)
23415 error_at (&richloc
,
23416 "expected %<;%> after struct definition");
23417 else if (TREE_CODE (type
) == UNION_TYPE
)
23418 error_at (&richloc
,
23419 "expected %<;%> after union definition");
23421 gcc_unreachable ();
23423 /* Unget one token and smash it to look as though we encountered
23424 a semicolon in the input stream. */
23425 cp_lexer_set_token_position (parser
->lexer
, prev
);
23426 token
= cp_lexer_peek_token (parser
->lexer
);
23427 token
->type
= CPP_SEMICOLON
;
23428 token
->keyword
= RID_MAX
;
23432 /* If this class is not itself within the scope of another class,
23433 then we need to parse the bodies of all of the queued function
23434 definitions. Note that the queued functions defined in a class
23435 are not always processed immediately following the
23436 class-specifier for that class. Consider:
23439 struct B { void f() { sizeof (A); } };
23442 If `f' were processed before the processing of `A' were
23443 completed, there would be no way to compute the size of `A'.
23444 Note that the nesting we are interested in here is lexical --
23445 not the semantic nesting given by TYPE_CONTEXT. In particular,
23448 struct A { struct B; };
23449 struct A::B { void f() { } };
23451 there is no need to delay the parsing of `A::B::f'. */
23452 if (--parser
->num_classes_being_defined
== 0)
23455 tree class_type
= NULL_TREE
;
23456 tree pushed_scope
= NULL_TREE
;
23458 cp_default_arg_entry
*e
;
23459 tree save_ccp
, save_ccr
;
23461 if (!type_definition_ok_p
|| any_erroneous_template_args_p (type
))
23463 /* Skip default arguments, NSDMIs, etc, in order to improve
23464 error recovery (c++/71169, c++/71832). */
23465 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23466 vec_safe_truncate (unparsed_nsdmis
, 0);
23467 vec_safe_truncate (unparsed_classes
, 0);
23468 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23471 /* In a first pass, parse default arguments to the functions.
23472 Then, in a second pass, parse the bodies of the functions.
23473 This two-phased approach handles cases like:
23481 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
23484 /* If there are default arguments that have not yet been processed,
23485 take care of them now. */
23486 if (class_type
!= e
->class_type
)
23489 pop_scope (pushed_scope
);
23490 class_type
= e
->class_type
;
23491 pushed_scope
= push_scope (class_type
);
23493 /* Make sure that any template parameters are in scope. */
23494 maybe_begin_member_template_processing (decl
);
23495 /* Parse the default argument expressions. */
23496 cp_parser_late_parsing_default_args (parser
, decl
);
23497 /* Remove any template parameters from the symbol table. */
23498 maybe_end_member_template_processing ();
23500 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23501 /* Now parse any NSDMIs. */
23502 save_ccp
= current_class_ptr
;
23503 save_ccr
= current_class_ref
;
23504 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
23506 if (class_type
!= DECL_CONTEXT (decl
))
23509 pop_scope (pushed_scope
);
23510 class_type
= DECL_CONTEXT (decl
);
23511 pushed_scope
= push_scope (class_type
);
23513 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
23514 cp_parser_late_parsing_nsdmi (parser
, decl
);
23516 vec_safe_truncate (unparsed_nsdmis
, 0);
23517 current_class_ptr
= save_ccp
;
23518 current_class_ref
= save_ccr
;
23520 pop_scope (pushed_scope
);
23522 /* Now do some post-NSDMI bookkeeping. */
23523 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
23524 after_nsdmi_defaulted_late_checks (class_type
);
23525 vec_safe_truncate (unparsed_classes
, 0);
23526 after_nsdmi_defaulted_late_checks (type
);
23528 /* Now parse the body of the functions. */
23531 /* OpenMP UDRs need to be parsed before all other functions. */
23532 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23533 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
23534 cp_parser_late_parsing_for_member (parser
, decl
);
23535 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23536 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
23537 cp_parser_late_parsing_for_member (parser
, decl
);
23540 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23541 cp_parser_late_parsing_for_member (parser
, decl
);
23542 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23545 vec_safe_push (unparsed_classes
, type
);
23547 /* Put back any saved access checks. */
23548 pop_deferring_access_checks ();
23550 /* Restore saved state. */
23551 parser
->in_switch_statement_p
= in_switch_statement_p
;
23552 parser
->in_statement
= in_statement
;
23553 parser
->in_function_body
= saved_in_function_body
;
23554 parser
->num_template_parameter_lists
23555 = saved_num_template_parameter_lists
;
23556 parser
->in_unbraced_linkage_specification_p
23557 = saved_in_unbraced_linkage_specification_p
;
23563 cp_parser_class_specifier (cp_parser
* parser
)
23566 timevar_push (TV_PARSE_STRUCT
);
23567 ret
= cp_parser_class_specifier_1 (parser
);
23568 timevar_pop (TV_PARSE_STRUCT
);
23572 /* Parse a class-head.
23575 class-key identifier [opt] base-clause [opt]
23576 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23577 class-key nested-name-specifier [opt] template-id
23580 class-virt-specifier:
23584 class-key attributes identifier [opt] base-clause [opt]
23585 class-key attributes nested-name-specifier identifier base-clause [opt]
23586 class-key attributes nested-name-specifier [opt] template-id
23589 Upon return BASES is initialized to the list of base classes (or
23590 NULL, if there are none) in the same form returned by
23591 cp_parser_base_clause.
23593 Returns the TYPE of the indicated class. Sets
23594 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23595 involving a nested-name-specifier was used, and FALSE otherwise.
23597 Returns error_mark_node if this is not a class-head.
23599 Returns NULL_TREE if the class-head is syntactically valid, but
23600 semantically invalid in a way that means we should skip the entire
23601 body of the class. */
23604 cp_parser_class_head (cp_parser
* parser
,
23605 bool* nested_name_specifier_p
)
23607 tree nested_name_specifier
;
23608 enum tag_types class_key
;
23609 tree id
= NULL_TREE
;
23610 tree type
= NULL_TREE
;
23613 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
23614 bool template_id_p
= false;
23615 bool qualified_p
= false;
23616 bool invalid_nested_name_p
= false;
23617 bool invalid_explicit_specialization_p
= false;
23618 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23619 tree pushed_scope
= NULL_TREE
;
23620 unsigned num_templates
;
23621 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
23622 /* Assume no nested-name-specifier will be present. */
23623 *nested_name_specifier_p
= false;
23624 /* Assume no template parameter lists will be used in defining the
23627 parser
->colon_corrects_to_scope_p
= false;
23629 /* Look for the class-key. */
23630 class_key
= cp_parser_class_key (parser
);
23631 if (class_key
== none_type
)
23632 return error_mark_node
;
23634 location_t class_head_start_location
= input_location
;
23636 /* Parse the attributes. */
23637 attributes
= cp_parser_attributes_opt (parser
);
23639 /* If the next token is `::', that is invalid -- but sometimes
23640 people do try to write:
23644 Handle this gracefully by accepting the extra qualifier, and then
23645 issuing an error about it later if this really is a
23646 class-head. If it turns out just to be an elaborated type
23647 specifier, remain silent. */
23648 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23649 qualified_p
= true;
23651 push_deferring_access_checks (dk_no_check
);
23653 /* Determine the name of the class. Begin by looking for an
23654 optional nested-name-specifier. */
23655 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23656 nested_name_specifier
23657 = cp_parser_nested_name_specifier_opt (parser
,
23658 /*typename_keyword_p=*/false,
23659 /*check_dependency_p=*/false,
23661 /*is_declaration=*/false);
23662 /* If there was a nested-name-specifier, then there *must* be an
23665 cp_token
*bad_template_keyword
= NULL
;
23667 if (nested_name_specifier
)
23669 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23670 /* Although the grammar says `identifier', it really means
23671 `class-name' or `template-name'. You are only allowed to
23672 define a class that has already been declared with this
23675 The proposed resolution for Core Issue 180 says that wherever
23676 you see `class T::X' you should treat `X' as a type-name.
23678 It is OK to define an inaccessible class; for example:
23680 class A { class B; };
23683 We do not know if we will see a class-name, or a
23684 template-name. We look for a class-name first, in case the
23685 class-name is a template-id; if we looked for the
23686 template-name first we would stop after the template-name. */
23687 cp_parser_parse_tentatively (parser
);
23688 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23689 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23690 type
= cp_parser_class_name (parser
,
23691 /*typename_keyword_p=*/false,
23692 /*template_keyword_p=*/false,
23694 /*check_dependency_p=*/false,
23695 /*class_head_p=*/true,
23696 /*is_declaration=*/false);
23697 /* If that didn't work, ignore the nested-name-specifier. */
23698 if (!cp_parser_parse_definitely (parser
))
23700 invalid_nested_name_p
= true;
23701 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23702 id
= cp_parser_identifier (parser
);
23703 if (id
== error_mark_node
)
23706 /* If we could not find a corresponding TYPE, treat this
23707 declaration like an unqualified declaration. */
23708 if (type
== error_mark_node
)
23709 nested_name_specifier
= NULL_TREE
;
23710 /* Otherwise, count the number of templates used in TYPE and its
23711 containing scopes. */
23713 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23715 /* Otherwise, the identifier is optional. */
23718 /* We don't know whether what comes next is a template-id,
23719 an identifier, or nothing at all. */
23720 cp_parser_parse_tentatively (parser
);
23721 /* Check for a template-id. */
23722 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23723 id
= cp_parser_template_id (parser
,
23724 /*template_keyword_p=*/false,
23725 /*check_dependency_p=*/true,
23727 /*is_declaration=*/true);
23728 /* If that didn't work, it could still be an identifier. */
23729 if (!cp_parser_parse_definitely (parser
))
23731 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23733 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23734 id
= cp_parser_identifier (parser
);
23741 template_id_p
= true;
23746 pop_deferring_access_checks ();
23750 cp_parser_check_for_invalid_template_id (parser
, id
,
23752 type_start_token
->location
);
23754 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23756 /* If it's not a `:' or a `{' then we can't really be looking at a
23757 class-head, since a class-head only appears as part of a
23758 class-specifier. We have to detect this situation before calling
23759 xref_tag, since that has irreversible side-effects. */
23760 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23762 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23763 type
= error_mark_node
;
23767 /* At this point, we're going ahead with the class-specifier, even
23768 if some other problem occurs. */
23769 cp_parser_commit_to_tentative_parse (parser
);
23770 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23772 cp_parser_error (parser
,
23773 "cannot specify %<override%> for a class");
23774 type
= error_mark_node
;
23777 /* Issue the error about the overly-qualified name now. */
23780 cp_parser_error (parser
,
23781 "global qualification of class name is invalid");
23782 type
= error_mark_node
;
23785 else if (invalid_nested_name_p
)
23787 cp_parser_error (parser
,
23788 "qualified name does not name a class");
23789 type
= error_mark_node
;
23792 else if (nested_name_specifier
)
23796 if (bad_template_keyword
)
23797 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23798 keyword template shall not appear at the top level. */
23799 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23800 "keyword %<template%> not allowed in class-head-name");
23802 /* Reject typedef-names in class heads. */
23803 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23805 error_at (type_start_token
->location
,
23806 "invalid class name in declaration of %qD",
23812 /* Figure out in what scope the declaration is being placed. */
23813 scope
= current_scope ();
23814 /* If that scope does not contain the scope in which the
23815 class was originally declared, the program is invalid. */
23816 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23818 if (at_namespace_scope_p ())
23819 error_at (type_start_token
->location
,
23820 "declaration of %qD in namespace %qD which does not "
23822 type
, scope
, nested_name_specifier
);
23824 error_at (type_start_token
->location
,
23825 "declaration of %qD in %qD which does not enclose %qD",
23826 type
, scope
, nested_name_specifier
);
23832 A declarator-id shall not be qualified except for the
23833 definition of a ... nested class outside of its class
23834 ... [or] the definition or explicit instantiation of a
23835 class member of a namespace outside of its namespace. */
23836 if (scope
== nested_name_specifier
)
23838 permerror (nested_name_specifier_token_start
->location
,
23839 "extra qualification not allowed");
23840 nested_name_specifier
= NULL_TREE
;
23844 /* An explicit-specialization must be preceded by "template <>". If
23845 it is not, try to recover gracefully. */
23846 if (at_namespace_scope_p ()
23847 && parser
->num_template_parameter_lists
== 0
23848 && !processing_template_parmlist
23851 /* Build a location of this form:
23852 struct typename <ARGS>
23853 ^~~~~~~~~~~~~~~~~~~~~~
23854 with caret==start at the start token, and
23855 finishing at the end of the type. */
23856 location_t reported_loc
23857 = make_location (class_head_start_location
,
23858 class_head_start_location
,
23859 get_finish (type_start_token
->location
));
23860 rich_location
richloc (line_table
, reported_loc
);
23861 richloc
.add_fixit_insert_before (class_head_start_location
,
23863 error_at (&richloc
,
23864 "an explicit specialization must be preceded by"
23865 " %<template <>%>");
23866 invalid_explicit_specialization_p
= true;
23867 /* Take the same action that would have been taken by
23868 cp_parser_explicit_specialization. */
23869 ++parser
->num_template_parameter_lists
;
23870 begin_specialization ();
23872 /* There must be no "return" statements between this point and the
23873 end of this function; set "type "to the correct return value and
23874 use "goto done;" to return. */
23875 /* Make sure that the right number of template parameters were
23877 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23879 type_start_token
->location
,
23880 /*declarator=*/NULL
))
23882 /* If something went wrong, there is no point in even trying to
23883 process the class-definition. */
23888 /* Look up the type. */
23891 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23892 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23893 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23895 error_at (type_start_token
->location
,
23896 "function template %qD redeclared as a class template", id
);
23897 type
= error_mark_node
;
23901 type
= TREE_TYPE (id
);
23902 type
= maybe_process_partial_specialization (type
);
23904 /* Check the scope while we still know whether or not we had a
23905 nested-name-specifier. */
23906 if (type
!= error_mark_node
)
23907 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23909 if (nested_name_specifier
)
23910 pushed_scope
= push_scope (nested_name_specifier
);
23912 else if (nested_name_specifier
)
23918 template <typename T> struct S { struct T };
23919 template <typename T> struct S<T>::T { };
23921 we will get a TYPENAME_TYPE when processing the definition of
23922 `S::T'. We need to resolve it to the actual type before we
23923 try to define it. */
23924 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23926 class_type
= resolve_typename_type (TREE_TYPE (type
),
23927 /*only_current_p=*/false);
23928 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23929 type
= TYPE_NAME (class_type
);
23932 cp_parser_error (parser
, "could not resolve typename type");
23933 type
= error_mark_node
;
23937 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23938 == error_mark_node
)
23944 class_type
= current_class_type
;
23945 /* Enter the scope indicated by the nested-name-specifier. */
23946 pushed_scope
= push_scope (nested_name_specifier
);
23947 /* Get the canonical version of this type. */
23948 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23949 /* Call push_template_decl if it seems like we should be defining a
23950 template either from the template headers or the type we're
23951 defining, so that we diagnose both extra and missing headers. */
23952 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23953 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23954 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23956 type
= push_template_decl (type
);
23957 if (type
== error_mark_node
)
23964 type
= TREE_TYPE (type
);
23965 *nested_name_specifier_p
= true;
23967 else /* The name is not a nested name. */
23969 /* If the class was unnamed, create a dummy name. */
23971 id
= make_anon_name ();
23972 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23973 ? ts_within_enclosing_non_class
23975 type
= xref_tag (class_key
, id
, tag_scope
,
23976 parser
->num_template_parameter_lists
);
23979 /* Indicate whether this class was declared as a `class' or as a
23981 if (TREE_CODE (type
) == RECORD_TYPE
)
23982 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23983 cp_parser_check_class_key (class_key
, type
);
23985 /* If this type was already complete, and we see another definition,
23986 that's an error. */
23987 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23989 error_at (type_start_token
->location
, "redefinition of %q#T",
23991 inform (location_of (type
), "previous definition of %q#T",
23996 else if (type
== error_mark_node
)
24001 /* Apply attributes now, before any use of the class as a template
24002 argument in its base list. */
24003 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
24004 fixup_attribute_variants (type
);
24007 /* We will have entered the scope containing the class; the names of
24008 base classes should be looked up in that context. For example:
24010 struct A { struct B {}; struct C; };
24011 struct A::C : B {};
24015 /* Get the list of base-classes, if there is one. */
24016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
24018 /* PR59482: enter the class scope so that base-specifiers are looked
24022 bases
= cp_parser_base_clause (parser
);
24023 /* PR59482: get out of the previously pushed class scope so that the
24024 subsequent pops pop the right thing. */
24031 /* If we're really defining a class, process the base classes.
24032 If they're invalid, fail. */
24033 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24034 xref_basetypes (type
, bases
);
24037 /* Leave the scope given by the nested-name-specifier. We will
24038 enter the class scope itself while processing the members. */
24040 pop_scope (pushed_scope
);
24042 if (invalid_explicit_specialization_p
)
24044 end_specialization ();
24045 --parser
->num_template_parameter_lists
;
24049 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
24050 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
24051 CLASSTYPE_FINAL (type
) = 1;
24053 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24057 /* Parse a class-key.
24064 Returns the kind of class-key specified, or none_type to indicate
24067 static enum tag_types
24068 cp_parser_class_key (cp_parser
* parser
)
24071 enum tag_types tag_type
;
24073 /* Look for the class-key. */
24074 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
24078 /* Check to see if the TOKEN is a class-key. */
24079 tag_type
= cp_parser_token_is_class_key (token
);
24081 cp_parser_error (parser
, "expected class-key");
24085 /* Parse a type-parameter-key.
24087 type-parameter-key:
24093 cp_parser_type_parameter_key (cp_parser
* parser
)
24095 /* Look for the type-parameter-key. */
24096 enum tag_types tag_type
= none_type
;
24097 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24098 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
24100 cp_lexer_consume_token (parser
->lexer
);
24101 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
24102 /* typename is not allowed in a template template parameter
24103 by the standard until C++17. */
24104 pedwarn (token
->location
, OPT_Wpedantic
,
24105 "ISO C++ forbids typename key in template template parameter;"
24106 " use -std=c++17 or -std=gnu++17");
24109 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
24114 /* Parse an (optional) member-specification.
24116 member-specification:
24117 member-declaration member-specification [opt]
24118 access-specifier : member-specification [opt] */
24121 cp_parser_member_specification_opt (cp_parser
* parser
)
24128 /* Peek at the next token. */
24129 token
= cp_lexer_peek_token (parser
->lexer
);
24130 /* If it's a `}', or EOF then we've seen all the members. */
24131 if (token
->type
== CPP_CLOSE_BRACE
24132 || token
->type
== CPP_EOF
24133 || token
->type
== CPP_PRAGMA_EOL
)
24136 /* See if this token is a keyword. */
24137 keyword
= token
->keyword
;
24141 case RID_PROTECTED
:
24143 /* Consume the access-specifier. */
24144 cp_lexer_consume_token (parser
->lexer
);
24145 /* Remember which access-specifier is active. */
24146 current_access_specifier
= token
->u
.value
;
24147 /* Look for the `:'. */
24148 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24152 /* Accept #pragmas at class scope. */
24153 if (token
->type
== CPP_PRAGMA
)
24155 cp_parser_pragma (parser
, pragma_member
, NULL
);
24159 /* Otherwise, the next construction must be a
24160 member-declaration. */
24161 cp_parser_member_declaration (parser
);
24166 /* Parse a member-declaration.
24168 member-declaration:
24169 decl-specifier-seq [opt] member-declarator-list [opt] ;
24170 function-definition ; [opt]
24171 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24173 template-declaration
24176 member-declarator-list:
24178 member-declarator-list , member-declarator
24181 declarator pure-specifier [opt]
24182 declarator constant-initializer [opt]
24183 identifier [opt] : constant-expression
24187 member-declaration:
24188 __extension__ member-declaration
24191 declarator attributes [opt] pure-specifier [opt]
24192 declarator attributes [opt] constant-initializer [opt]
24193 identifier [opt] attributes [opt] : constant-expression
24197 member-declaration:
24198 static_assert-declaration */
24201 cp_parser_member_declaration (cp_parser
* parser
)
24203 cp_decl_specifier_seq decl_specifiers
;
24204 tree prefix_attributes
;
24206 int declares_class_or_enum
;
24208 cp_token
*token
= NULL
;
24209 cp_token
*decl_spec_token_start
= NULL
;
24210 cp_token
*initializer_token_start
= NULL
;
24211 int saved_pedantic
;
24212 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
24214 /* Check for the `__extension__' keyword. */
24215 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
24218 cp_parser_member_declaration (parser
);
24219 /* Restore the old value of the PEDANTIC flag. */
24220 pedantic
= saved_pedantic
;
24225 /* Check for a template-declaration. */
24226 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24228 /* An explicit specialization here is an error condition, and we
24229 expect the specialization handler to detect and report this. */
24230 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
24231 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
24232 cp_parser_explicit_specialization (parser
);
24234 cp_parser_template_declaration (parser
, /*member_p=*/true);
24238 /* Check for a template introduction. */
24239 else if (cp_parser_template_declaration_after_export (parser
, true))
24242 /* Check for a using-declaration. */
24243 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24245 if (cxx_dialect
< cxx11
)
24247 /* Parse the using-declaration. */
24248 cp_parser_using_declaration (parser
,
24249 /*access_declaration_p=*/false);
24255 bool alias_decl_expected
;
24256 cp_parser_parse_tentatively (parser
);
24257 decl
= cp_parser_alias_declaration (parser
);
24258 /* Note that if we actually see the '=' token after the
24259 identifier, cp_parser_alias_declaration commits the
24260 tentative parse. In that case, we really expect an
24261 alias-declaration. Otherwise, we expect a using
24263 alias_decl_expected
=
24264 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
24265 cp_parser_parse_definitely (parser
);
24267 if (alias_decl_expected
)
24268 finish_member_declaration (decl
);
24270 cp_parser_using_declaration (parser
,
24271 /*access_declaration_p=*/false);
24276 /* Check for @defs. */
24277 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
24280 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
24281 ivar
= ivar_chains
;
24285 ivar
= TREE_CHAIN (member
);
24286 TREE_CHAIN (member
) = NULL_TREE
;
24287 finish_member_declaration (member
);
24292 /* If the next token is `static_assert' we have a static assertion. */
24293 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
24295 cp_parser_static_assert (parser
, /*member_p=*/true);
24299 parser
->colon_corrects_to_scope_p
= false;
24301 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
24304 /* Parse the decl-specifier-seq. */
24305 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
24306 cp_parser_decl_specifier_seq (parser
,
24307 (CP_PARSER_FLAGS_OPTIONAL
24308 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
24310 &declares_class_or_enum
);
24311 /* Check for an invalid type-name. */
24312 if (!decl_specifiers
.any_type_specifiers_p
24313 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24315 /* If there is no declarator, then the decl-specifier-seq should
24317 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24319 /* If there was no decl-specifier-seq, and the next token is a
24320 `;', then we have something like:
24326 Each member-declaration shall declare at least one member
24327 name of the class. */
24328 if (!decl_specifiers
.any_specifiers_p
)
24330 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24331 if (!in_system_header_at (token
->location
))
24333 gcc_rich_location
richloc (token
->location
);
24334 richloc
.add_fixit_remove ();
24335 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
24342 /* See if this declaration is a friend. */
24343 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24344 /* If there were decl-specifiers, check to see if there was
24345 a class-declaration. */
24346 type
= check_tag_decl (&decl_specifiers
,
24347 /*explicit_type_instantiation_p=*/false);
24348 /* Nested classes have already been added to the class, but
24349 a `friend' needs to be explicitly registered. */
24352 /* If the `friend' keyword was present, the friend must
24353 be introduced with a class-key. */
24354 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
24355 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
24356 "in C++03 a class-key must be used "
24357 "when declaring a friend");
24360 template <typename T> struct A {
24361 friend struct A<T>::B;
24364 A<T>::B will be represented by a TYPENAME_TYPE, and
24365 therefore not recognized by check_tag_decl. */
24368 type
= decl_specifiers
.type
;
24369 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
24370 type
= TREE_TYPE (type
);
24372 if (!type
|| !TYPE_P (type
))
24373 error_at (decl_spec_token_start
->location
,
24374 "friend declaration does not name a class or "
24377 make_friend_class (current_class_type
, type
,
24378 /*complain=*/true);
24380 /* If there is no TYPE, an error message will already have
24382 else if (!type
|| type
== error_mark_node
)
24384 /* An anonymous aggregate has to be handled specially; such
24385 a declaration really declares a data member (with a
24386 particular type), as opposed to a nested class. */
24387 else if (ANON_AGGR_TYPE_P (type
))
24390 if (decl_specifiers
.storage_class
!= sc_none
)
24391 error_at (decl_spec_token_start
->location
,
24392 "a storage class on an anonymous aggregate "
24393 "in class scope is not allowed");
24395 /* Remove constructors and such from TYPE, now that we
24396 know it is an anonymous aggregate. */
24397 fixup_anonymous_aggr (type
);
24398 /* And make the corresponding data member. */
24399 decl
= build_decl (decl_spec_token_start
->location
,
24400 FIELD_DECL
, NULL_TREE
, type
);
24401 /* Add it to the class. */
24402 finish_member_declaration (decl
);
24405 cp_parser_check_access_in_redeclaration
24407 decl_spec_token_start
->location
);
24412 bool assume_semicolon
= false;
24414 /* Clear attributes from the decl_specifiers but keep them
24415 around as prefix attributes that apply them to the entity
24417 prefix_attributes
= decl_specifiers
.attributes
;
24418 decl_specifiers
.attributes
= NULL_TREE
;
24420 /* See if these declarations will be friends. */
24421 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24423 /* Keep going until we hit the `;' at the end of the
24425 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24427 tree attributes
= NULL_TREE
;
24428 tree first_attribute
;
24430 bool named_bitfld
= false;
24432 /* Peek at the next token. */
24433 token
= cp_lexer_peek_token (parser
->lexer
);
24435 /* The following code wants to know early if it is a bit-field
24436 or some other declaration. Attributes can appear before
24437 the `:' token. Skip over them without consuming any tokens
24438 to peek if they are followed by `:'. */
24439 if (cp_next_tokens_can_be_attribute_p (parser
)
24440 || (token
->type
== CPP_NAME
24441 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
24442 && (named_bitfld
= true)))
24445 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
24446 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24449 /* Check for a bitfield declaration. */
24450 if (token
->type
== CPP_COLON
24451 || (token
->type
== CPP_NAME
24452 && token
== cp_lexer_peek_token (parser
->lexer
)
24453 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
24454 && (named_bitfld
= true)))
24458 tree late_attributes
= NULL_TREE
;
24459 location_t id_location
24460 = cp_lexer_peek_token (parser
->lexer
)->location
;
24463 identifier
= cp_parser_identifier (parser
);
24465 identifier
= NULL_TREE
;
24467 /* Look for attributes that apply to the bitfield. */
24468 attributes
= cp_parser_attributes_opt (parser
);
24470 /* Consume the `:' token. */
24471 cp_lexer_consume_token (parser
->lexer
);
24473 /* Get the width of the bitfield. */
24474 width
= cp_parser_constant_expression (parser
, false, NULL
,
24475 cxx_dialect
>= cxx11
);
24477 /* In C++2A and as extension for C++11 and above we allow
24478 default member initializers for bit-fields. */
24479 initializer
= NULL_TREE
;
24480 if (cxx_dialect
>= cxx11
24481 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
24482 || cp_lexer_next_token_is (parser
->lexer
,
24486 = cp_lexer_peek_token (parser
->lexer
)->location
;
24487 if (cxx_dialect
< cxx2a
24488 && !in_system_header_at (loc
)
24489 && identifier
!= NULL_TREE
)
24491 "default member initializers for bit-fields "
24492 "only available with -std=c++2a or "
24495 initializer
= cp_parser_save_nsdmi (parser
);
24496 if (identifier
== NULL_TREE
)
24498 error_at (loc
, "default member initializer for "
24499 "unnamed bit-field");
24500 initializer
= NULL_TREE
;
24505 /* Look for attributes that apply to the bitfield after
24506 the `:' token and width. This is where GCC used to
24507 parse attributes in the past, pedwarn if there is
24508 a std attribute. */
24509 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24510 pedwarn (input_location
, OPT_Wpedantic
,
24511 "ISO C++ allows bit-field attributes only "
24512 "before the %<:%> token");
24514 late_attributes
= cp_parser_attributes_opt (parser
);
24517 attributes
= attr_chainon (attributes
, late_attributes
);
24519 /* Remember which attributes are prefix attributes and
24521 first_attribute
= attributes
;
24522 /* Combine the attributes. */
24523 attributes
= attr_chainon (prefix_attributes
, attributes
);
24525 /* Create the bitfield declaration. */
24526 decl
= grokbitfield (identifier
24527 ? make_id_declarator (NULL_TREE
,
24533 width
, initializer
,
24538 cp_declarator
*declarator
;
24539 tree asm_specification
;
24540 int ctor_dtor_or_conv_p
;
24541 bool static_p
= (decl_specifiers
.storage_class
== sc_static
);
24543 /* Parse the declarator. */
24545 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24546 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
24547 &ctor_dtor_or_conv_p
,
24548 /*parenthesized_p=*/NULL
,
24550 friend_p
, static_p
);
24552 /* If something went wrong parsing the declarator, make sure
24553 that we at least consume some tokens. */
24554 if (declarator
== cp_error_declarator
)
24556 /* Skip to the end of the statement. */
24557 cp_parser_skip_to_end_of_statement (parser
);
24558 /* If the next token is not a semicolon, that is
24559 probably because we just skipped over the body of
24560 a function. So, we consume a semicolon if
24561 present, but do not issue an error message if it
24563 if (cp_lexer_next_token_is (parser
->lexer
,
24565 cp_lexer_consume_token (parser
->lexer
);
24569 if (declares_class_or_enum
& 2)
24570 cp_parser_check_for_definition_in_return_type
24571 (declarator
, decl_specifiers
.type
,
24572 decl_specifiers
.locations
[ds_type_spec
]);
24574 /* Look for an asm-specification. */
24575 asm_specification
= cp_parser_asm_specification_opt (parser
);
24576 /* Look for attributes that apply to the declaration. */
24577 attributes
= cp_parser_attributes_opt (parser
);
24578 /* Remember which attributes are prefix attributes and
24580 first_attribute
= attributes
;
24581 /* Combine the attributes. */
24582 attributes
= attr_chainon (prefix_attributes
, attributes
);
24584 /* If it's an `=', then we have a constant-initializer or a
24585 pure-specifier. It is not correct to parse the
24586 initializer before registering the member declaration
24587 since the member declaration should be in scope while
24588 its initializer is processed. However, the rest of the
24589 front end does not yet provide an interface that allows
24590 us to handle this correctly. */
24591 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24595 A pure-specifier shall be used only in the declaration of
24596 a virtual function.
24598 A member-declarator can contain a constant-initializer
24599 only if it declares a static member of integral or
24602 Therefore, if the DECLARATOR is for a function, we look
24603 for a pure-specifier; otherwise, we look for a
24604 constant-initializer. When we call `grokfield', it will
24605 perform more stringent semantics checks. */
24606 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
24607 if (function_declarator_p (declarator
)
24608 || (decl_specifiers
.type
24609 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
24610 && declarator
->kind
== cdk_id
24611 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
24612 == FUNCTION_TYPE
)))
24613 initializer
= cp_parser_pure_specifier (parser
);
24614 else if (decl_specifiers
.storage_class
!= sc_static
)
24615 initializer
= cp_parser_save_nsdmi (parser
);
24616 else if (cxx_dialect
>= cxx11
)
24619 /* Don't require a constant rvalue in C++11, since we
24620 might want a reference constant. We'll enforce
24621 constancy later. */
24622 cp_lexer_consume_token (parser
->lexer
);
24623 /* Parse the initializer. */
24624 initializer
= cp_parser_initializer_clause (parser
,
24628 /* Parse the initializer. */
24629 initializer
= cp_parser_constant_initializer (parser
);
24631 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
24632 && !function_declarator_p (declarator
))
24635 if (decl_specifiers
.storage_class
!= sc_static
)
24636 initializer
= cp_parser_save_nsdmi (parser
);
24638 initializer
= cp_parser_initializer (parser
, &x
, &x
);
24640 /* Otherwise, there is no initializer. */
24642 initializer
= NULL_TREE
;
24644 /* See if we are probably looking at a function
24645 definition. We are certainly not looking at a
24646 member-declarator. Calling `grokfield' has
24647 side-effects, so we must not do it unless we are sure
24648 that we are looking at a member-declarator. */
24649 if (cp_parser_token_starts_function_definition_p
24650 (cp_lexer_peek_token (parser
->lexer
)))
24652 /* The grammar does not allow a pure-specifier to be
24653 used when a member function is defined. (It is
24654 possible that this fact is an oversight in the
24655 standard, since a pure function may be defined
24656 outside of the class-specifier. */
24657 if (initializer
&& initializer_token_start
)
24658 error_at (initializer_token_start
->location
,
24659 "pure-specifier on function-definition");
24660 decl
= cp_parser_save_member_function_body (parser
,
24664 if (parser
->fully_implicit_function_template_p
)
24665 decl
= finish_fully_implicit_template (parser
, decl
);
24666 /* If the member was not a friend, declare it here. */
24668 finish_member_declaration (decl
);
24669 /* Peek at the next token. */
24670 token
= cp_lexer_peek_token (parser
->lexer
);
24671 /* If the next token is a semicolon, consume it. */
24672 if (token
->type
== CPP_SEMICOLON
)
24674 location_t semicolon_loc
24675 = cp_lexer_consume_token (parser
->lexer
)->location
;
24676 gcc_rich_location
richloc (semicolon_loc
);
24677 richloc
.add_fixit_remove ();
24678 warning_at (&richloc
, OPT_Wextra_semi
,
24679 "extra %<;%> after in-class "
24680 "function definition");
24685 if (declarator
->kind
== cdk_function
)
24686 declarator
->id_loc
= token
->location
;
24687 /* Create the declaration. */
24688 decl
= grokfield (declarator
, &decl_specifiers
,
24689 initializer
, /*init_const_expr_p=*/true,
24690 asm_specification
, attributes
);
24691 if (parser
->fully_implicit_function_template_p
)
24694 finish_fully_implicit_template (parser
, 0);
24696 decl
= finish_fully_implicit_template (parser
, decl
);
24700 cp_finalize_omp_declare_simd (parser
, decl
);
24701 cp_finalize_oacc_routine (parser
, decl
, false);
24703 /* Reset PREFIX_ATTRIBUTES. */
24704 if (attributes
!= error_mark_node
)
24706 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24707 attributes
= TREE_CHAIN (attributes
);
24709 TREE_CHAIN (attributes
) = NULL_TREE
;
24712 /* If there is any qualification still in effect, clear it
24713 now; we will be starting fresh with the next declarator. */
24714 parser
->scope
= NULL_TREE
;
24715 parser
->qualifying_scope
= NULL_TREE
;
24716 parser
->object_scope
= NULL_TREE
;
24717 /* If it's a `,', then there are more declarators. */
24718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24720 cp_lexer_consume_token (parser
->lexer
);
24721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24723 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24724 gcc_rich_location
richloc (token
->location
);
24725 richloc
.add_fixit_remove ();
24726 error_at (&richloc
, "stray %<,%> at end of "
24727 "member declaration");
24730 /* If the next token isn't a `;', then we have a parse error. */
24731 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24734 /* The next token might be a ways away from where the
24735 actual semicolon is missing. Find the previous token
24736 and use that for our error position. */
24737 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24738 gcc_rich_location
richloc (token
->location
);
24739 richloc
.add_fixit_insert_after (";");
24740 error_at (&richloc
, "expected %<;%> at end of "
24741 "member declaration");
24743 /* Assume that the user meant to provide a semicolon. If
24744 we were to cp_parser_skip_to_end_of_statement, we might
24745 skip to a semicolon inside a member function definition
24746 and issue nonsensical error messages. */
24747 assume_semicolon
= true;
24752 /* Add DECL to the list of members. */
24754 /* Explicitly include, eg, NSDMIs, for better error
24755 recovery (c++/58650). */
24756 || !DECL_DECLARES_FUNCTION_P (decl
))
24757 finish_member_declaration (decl
);
24759 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24760 cp_parser_save_default_args (parser
, decl
);
24761 else if (TREE_CODE (decl
) == FIELD_DECL
24762 && DECL_INITIAL (decl
))
24763 /* Add DECL to the queue of NSDMI to be parsed later. */
24764 vec_safe_push (unparsed_nsdmis
, decl
);
24767 if (assume_semicolon
)
24772 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24774 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24777 /* Parse a pure-specifier.
24782 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24783 Otherwise, ERROR_MARK_NODE is returned. */
24786 cp_parser_pure_specifier (cp_parser
* parser
)
24790 /* Look for the `=' token. */
24791 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24792 return error_mark_node
;
24793 /* Look for the `0' token. */
24794 token
= cp_lexer_peek_token (parser
->lexer
);
24796 if (token
->type
== CPP_EOF
24797 || token
->type
== CPP_PRAGMA_EOL
)
24798 return error_mark_node
;
24800 cp_lexer_consume_token (parser
->lexer
);
24802 /* Accept = default or = delete in c++0x mode. */
24803 if (token
->keyword
== RID_DEFAULT
24804 || token
->keyword
== RID_DELETE
)
24806 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24807 return token
->u
.value
;
24810 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24811 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24813 cp_parser_error (parser
,
24814 "invalid pure specifier (only %<= 0%> is allowed)");
24815 cp_parser_skip_to_end_of_statement (parser
);
24816 return error_mark_node
;
24818 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24820 error_at (token
->location
, "templates may not be %<virtual%>");
24821 return error_mark_node
;
24824 return integer_zero_node
;
24827 /* Parse a constant-initializer.
24829 constant-initializer:
24830 = constant-expression
24832 Returns a representation of the constant-expression. */
24835 cp_parser_constant_initializer (cp_parser
* parser
)
24837 /* Look for the `=' token. */
24838 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24839 return error_mark_node
;
24841 /* It is invalid to write:
24843 struct S { static const int i = { 7 }; };
24846 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24848 cp_parser_error (parser
,
24849 "a brace-enclosed initializer is not allowed here");
24850 /* Consume the opening brace. */
24851 matching_braces braces
;
24852 braces
.consume_open (parser
);
24853 /* Skip the initializer. */
24854 cp_parser_skip_to_closing_brace (parser
);
24855 /* Look for the trailing `}'. */
24856 braces
.require_close (parser
);
24858 return error_mark_node
;
24861 return cp_parser_constant_expression (parser
);
24864 /* Derived classes [gram.class.derived] */
24866 /* Parse a base-clause.
24869 : base-specifier-list
24871 base-specifier-list:
24872 base-specifier ... [opt]
24873 base-specifier-list , base-specifier ... [opt]
24875 Returns a TREE_LIST representing the base-classes, in the order in
24876 which they were declared. The representation of each node is as
24877 described by cp_parser_base_specifier.
24879 In the case that no bases are specified, this function will return
24880 NULL_TREE, not ERROR_MARK_NODE. */
24883 cp_parser_base_clause (cp_parser
* parser
)
24885 tree bases
= NULL_TREE
;
24887 /* Look for the `:' that begins the list. */
24888 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24890 /* Scan the base-specifier-list. */
24895 bool pack_expansion_p
= false;
24897 /* Look for the base-specifier. */
24898 base
= cp_parser_base_specifier (parser
);
24899 /* Look for the (optional) ellipsis. */
24900 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24902 /* Consume the `...'. */
24903 cp_lexer_consume_token (parser
->lexer
);
24905 pack_expansion_p
= true;
24908 /* Add BASE to the front of the list. */
24909 if (base
&& base
!= error_mark_node
)
24911 if (pack_expansion_p
)
24912 /* Make this a pack expansion type. */
24913 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24915 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24917 TREE_CHAIN (base
) = bases
;
24921 /* Peek at the next token. */
24922 token
= cp_lexer_peek_token (parser
->lexer
);
24923 /* If it's not a comma, then the list is complete. */
24924 if (token
->type
!= CPP_COMMA
)
24926 /* Consume the `,'. */
24927 cp_lexer_consume_token (parser
->lexer
);
24930 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24931 base class had a qualified name. However, the next name that
24932 appears is certainly not qualified. */
24933 parser
->scope
= NULL_TREE
;
24934 parser
->qualifying_scope
= NULL_TREE
;
24935 parser
->object_scope
= NULL_TREE
;
24937 return nreverse (bases
);
24940 /* Parse a base-specifier.
24943 :: [opt] nested-name-specifier [opt] class-name
24944 virtual access-specifier [opt] :: [opt] nested-name-specifier
24946 access-specifier virtual [opt] :: [opt] nested-name-specifier
24949 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24950 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24951 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24952 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24955 cp_parser_base_specifier (cp_parser
* parser
)
24959 bool virtual_p
= false;
24960 bool duplicate_virtual_error_issued_p
= false;
24961 bool duplicate_access_error_issued_p
= false;
24962 bool class_scope_p
, template_p
;
24963 tree access
= access_default_node
;
24966 /* Process the optional `virtual' and `access-specifier'. */
24969 /* Peek at the next token. */
24970 token
= cp_lexer_peek_token (parser
->lexer
);
24971 /* Process `virtual'. */
24972 switch (token
->keyword
)
24975 /* If `virtual' appears more than once, issue an error. */
24976 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24978 cp_parser_error (parser
,
24979 "%<virtual%> specified more than once in base-specifier");
24980 duplicate_virtual_error_issued_p
= true;
24985 /* Consume the `virtual' token. */
24986 cp_lexer_consume_token (parser
->lexer
);
24991 case RID_PROTECTED
:
24993 /* If more than one access specifier appears, issue an
24995 if (access
!= access_default_node
24996 && !duplicate_access_error_issued_p
)
24998 cp_parser_error (parser
,
24999 "more than one access specifier in base-specifier");
25000 duplicate_access_error_issued_p
= true;
25003 access
= ridpointers
[(int) token
->keyword
];
25005 /* Consume the access-specifier. */
25006 cp_lexer_consume_token (parser
->lexer
);
25015 /* It is not uncommon to see programs mechanically, erroneously, use
25016 the 'typename' keyword to denote (dependent) qualified types
25017 as base classes. */
25018 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25020 token
= cp_lexer_peek_token (parser
->lexer
);
25021 if (!processing_template_decl
)
25022 error_at (token
->location
,
25023 "keyword %<typename%> not allowed outside of templates");
25025 error_at (token
->location
,
25026 "keyword %<typename%> not allowed in this context "
25027 "(the base class is implicitly a type)");
25028 cp_lexer_consume_token (parser
->lexer
);
25031 /* Look for the optional `::' operator. */
25032 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
25033 /* Look for the nested-name-specifier. The simplest way to
25038 The keyword `typename' is not permitted in a base-specifier or
25039 mem-initializer; in these contexts a qualified name that
25040 depends on a template-parameter is implicitly assumed to be a
25043 is to pretend that we have seen the `typename' keyword at this
25045 cp_parser_nested_name_specifier_opt (parser
,
25046 /*typename_keyword_p=*/true,
25047 /*check_dependency_p=*/true,
25049 /*is_declaration=*/true);
25050 /* If the base class is given by a qualified name, assume that names
25051 we see are type names or templates, as appropriate. */
25052 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
25053 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
25056 && cp_lexer_next_token_is_decltype (parser
->lexer
))
25057 /* DR 950 allows decltype as a base-specifier. */
25058 type
= cp_parser_decltype (parser
);
25061 /* Otherwise, look for the class-name. */
25062 type
= cp_parser_class_name (parser
,
25066 /*check_dependency_p=*/true,
25067 /*class_head_p=*/false,
25068 /*is_declaration=*/true);
25069 type
= TREE_TYPE (type
);
25072 if (type
== error_mark_node
)
25073 return error_mark_node
;
25075 return finish_base_specifier (type
, access
, virtual_p
);
25078 /* Exception handling [gram.exception] */
25080 /* Parse an (optional) noexcept-specification.
25082 noexcept-specification:
25083 noexcept ( constant-expression ) [opt]
25085 If no noexcept-specification is present, returns NULL_TREE.
25086 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25087 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25088 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25089 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25090 in which case a boolean condition is returned instead. */
25093 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
25094 bool require_constexpr
,
25095 bool* consumed_expr
,
25099 const char *saved_message
;
25101 /* Peek at the next token. */
25102 token
= cp_lexer_peek_token (parser
->lexer
);
25104 /* Is it a noexcept-specification? */
25105 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
25108 cp_lexer_consume_token (parser
->lexer
);
25110 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
25112 matching_parens parens
;
25113 parens
.consume_open (parser
);
25115 tree save_ccp
= current_class_ptr
;
25116 tree save_ccr
= current_class_ref
;
25118 if (current_class_type
)
25119 inject_this_parameter (current_class_type
, TYPE_UNQUALIFIED
);
25121 if (require_constexpr
)
25123 /* Types may not be defined in an exception-specification. */
25124 saved_message
= parser
->type_definition_forbidden_message
;
25125 parser
->type_definition_forbidden_message
25126 = G_("types may not be defined in an exception-specification");
25128 expr
= cp_parser_constant_expression (parser
);
25130 /* Restore the saved message. */
25131 parser
->type_definition_forbidden_message
= saved_message
;
25135 expr
= cp_parser_expression (parser
);
25136 *consumed_expr
= true;
25139 parens
.require_close (parser
);
25141 current_class_ptr
= save_ccp
;
25142 current_class_ref
= save_ccr
;
25146 expr
= boolean_true_node
;
25147 if (!require_constexpr
)
25148 *consumed_expr
= false;
25151 /* We cannot build a noexcept-spec right away because this will check
25152 that expr is a constexpr. */
25154 return build_noexcept_spec (expr
, tf_warning_or_error
);
25162 /* Parse an (optional) exception-specification.
25164 exception-specification:
25165 throw ( type-id-list [opt] )
25167 Returns a TREE_LIST representing the exception-specification. The
25168 TREE_VALUE of each node is a type. */
25171 cp_parser_exception_specification_opt (cp_parser
* parser
)
25175 const char *saved_message
;
25177 /* Peek at the next token. */
25178 token
= cp_lexer_peek_token (parser
->lexer
);
25180 /* Is it a noexcept-specification? */
25181 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
25183 if (type_id_list
!= NULL_TREE
)
25184 return type_id_list
;
25186 /* If it's not `throw', then there's no exception-specification. */
25187 if (!cp_parser_is_keyword (token
, RID_THROW
))
25190 location_t loc
= token
->location
;
25192 /* Consume the `throw'. */
25193 cp_lexer_consume_token (parser
->lexer
);
25195 /* Look for the `('. */
25196 matching_parens parens
;
25197 parens
.require_open (parser
);
25199 /* Peek at the next token. */
25200 token
= cp_lexer_peek_token (parser
->lexer
);
25201 /* If it's not a `)', then there is a type-id-list. */
25202 if (token
->type
!= CPP_CLOSE_PAREN
)
25204 /* Types may not be defined in an exception-specification. */
25205 saved_message
= parser
->type_definition_forbidden_message
;
25206 parser
->type_definition_forbidden_message
25207 = G_("types may not be defined in an exception-specification");
25208 /* Parse the type-id-list. */
25209 type_id_list
= cp_parser_type_id_list (parser
);
25210 /* Restore the saved message. */
25211 parser
->type_definition_forbidden_message
= saved_message
;
25213 if (cxx_dialect
>= cxx17
)
25215 error_at (loc
, "ISO C++17 does not allow dynamic exception "
25217 type_id_list
= NULL_TREE
;
25219 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
25220 warning_at (loc
, OPT_Wdeprecated
,
25221 "dynamic exception specifications are deprecated in "
25224 /* In C++17, throw() is equivalent to noexcept (true). throw()
25225 is deprecated in C++11 and above as well, but is still widely used,
25226 so don't warn about it yet. */
25227 else if (cxx_dialect
>= cxx17
)
25228 type_id_list
= noexcept_true_spec
;
25230 type_id_list
= empty_except_spec
;
25232 /* Look for the `)'. */
25233 parens
.require_close (parser
);
25235 return type_id_list
;
25238 /* Parse an (optional) type-id-list.
25242 type-id-list , type-id ... [opt]
25244 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25245 in the order that the types were presented. */
25248 cp_parser_type_id_list (cp_parser
* parser
)
25250 tree types
= NULL_TREE
;
25257 token
= cp_lexer_peek_token (parser
->lexer
);
25259 /* Get the next type-id. */
25260 type
= cp_parser_type_id (parser
);
25261 /* Check for invalid 'auto'. */
25262 if (flag_concepts
&& type_uses_auto (type
))
25264 error_at (token
->location
,
25265 "invalid use of %<auto%> in exception-specification");
25266 type
= error_mark_node
;
25268 /* Parse the optional ellipsis. */
25269 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25271 /* Consume the `...'. */
25272 cp_lexer_consume_token (parser
->lexer
);
25274 /* Turn the type into a pack expansion expression. */
25275 type
= make_pack_expansion (type
);
25277 /* Add it to the list. */
25278 types
= add_exception_specifier (types
, type
, /*complain=*/1);
25279 /* Peek at the next token. */
25280 token
= cp_lexer_peek_token (parser
->lexer
);
25281 /* If it is not a `,', we are done. */
25282 if (token
->type
!= CPP_COMMA
)
25284 /* Consume the `,'. */
25285 cp_lexer_consume_token (parser
->lexer
);
25288 return nreverse (types
);
25291 /* Parse a try-block.
25294 try compound-statement handler-seq */
25297 cp_parser_try_block (cp_parser
* parser
)
25301 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
25302 if (parser
->in_function_body
25303 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
25304 error ("%<try%> in %<constexpr%> function");
25306 try_block
= begin_try_block ();
25307 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
25308 finish_try_block (try_block
);
25309 cp_parser_handler_seq (parser
);
25310 finish_handler_sequence (try_block
);
25315 /* Parse a function-try-block.
25317 function-try-block:
25318 try ctor-initializer [opt] function-body handler-seq */
25321 cp_parser_function_try_block (cp_parser
* parser
)
25323 tree compound_stmt
;
25326 /* Look for the `try' keyword. */
25327 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
25329 /* Let the rest of the front end know where we are. */
25330 try_block
= begin_function_try_block (&compound_stmt
);
25331 /* Parse the function-body. */
25332 cp_parser_ctor_initializer_opt_and_function_body
25333 (parser
, /*in_function_try_block=*/true);
25334 /* We're done with the `try' part. */
25335 finish_function_try_block (try_block
);
25336 /* Parse the handlers. */
25337 cp_parser_handler_seq (parser
);
25338 /* We're done with the handlers. */
25339 finish_function_handler_sequence (try_block
, compound_stmt
);
25342 /* Parse a handler-seq.
25345 handler handler-seq [opt] */
25348 cp_parser_handler_seq (cp_parser
* parser
)
25354 /* Parse the handler. */
25355 cp_parser_handler (parser
);
25356 /* Peek at the next token. */
25357 token
= cp_lexer_peek_token (parser
->lexer
);
25358 /* If it's not `catch' then there are no more handlers. */
25359 if (!cp_parser_is_keyword (token
, RID_CATCH
))
25364 /* Parse a handler.
25367 catch ( exception-declaration ) compound-statement */
25370 cp_parser_handler (cp_parser
* parser
)
25375 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
25376 handler
= begin_handler ();
25377 matching_parens parens
;
25378 parens
.require_open (parser
);
25379 declaration
= cp_parser_exception_declaration (parser
);
25380 finish_handler_parms (declaration
, handler
);
25381 parens
.require_close (parser
);
25382 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
25383 finish_handler (handler
);
25386 /* Parse an exception-declaration.
25388 exception-declaration:
25389 type-specifier-seq declarator
25390 type-specifier-seq abstract-declarator
25394 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25395 ellipsis variant is used. */
25398 cp_parser_exception_declaration (cp_parser
* parser
)
25400 cp_decl_specifier_seq type_specifiers
;
25401 cp_declarator
*declarator
;
25402 const char *saved_message
;
25404 /* If it's an ellipsis, it's easy to handle. */
25405 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25407 /* Consume the `...' token. */
25408 cp_lexer_consume_token (parser
->lexer
);
25412 /* Types may not be defined in exception-declarations. */
25413 saved_message
= parser
->type_definition_forbidden_message
;
25414 parser
->type_definition_forbidden_message
25415 = G_("types may not be defined in exception-declarations");
25417 /* Parse the type-specifier-seq. */
25418 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
25419 /*is_declaration=*/true,
25420 /*is_trailing_return=*/false,
25422 /* If it's a `)', then there is no declarator. */
25423 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25426 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
25427 CP_PARSER_FLAGS_NONE
,
25428 /*ctor_dtor_or_conv_p=*/NULL
,
25429 /*parenthesized_p=*/NULL
,
25430 /*member_p=*/false,
25431 /*friend_p=*/false,
25432 /*static_p=*/false);
25434 /* Restore the saved message. */
25435 parser
->type_definition_forbidden_message
= saved_message
;
25437 if (!type_specifiers
.any_specifiers_p
)
25438 return error_mark_node
;
25440 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
25443 /* Parse a throw-expression.
25446 throw assignment-expression [opt]
25448 Returns a THROW_EXPR representing the throw-expression. */
25451 cp_parser_throw_expression (cp_parser
* parser
)
25456 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
25457 token
= cp_lexer_peek_token (parser
->lexer
);
25458 /* Figure out whether or not there is an assignment-expression
25459 following the "throw" keyword. */
25460 if (token
->type
== CPP_COMMA
25461 || token
->type
== CPP_SEMICOLON
25462 || token
->type
== CPP_CLOSE_PAREN
25463 || token
->type
== CPP_CLOSE_SQUARE
25464 || token
->type
== CPP_CLOSE_BRACE
25465 || token
->type
== CPP_COLON
)
25466 expression
= NULL_TREE
;
25468 expression
= cp_parser_assignment_expression (parser
);
25470 return build_throw (expression
);
25473 /* GNU Extensions */
25475 /* Parse an (optional) asm-specification.
25478 asm ( string-literal )
25480 If the asm-specification is present, returns a STRING_CST
25481 corresponding to the string-literal. Otherwise, returns
25485 cp_parser_asm_specification_opt (cp_parser
* parser
)
25488 tree asm_specification
;
25490 /* Peek at the next token. */
25491 token
= cp_lexer_peek_token (parser
->lexer
);
25492 /* If the next token isn't the `asm' keyword, then there's no
25493 asm-specification. */
25494 if (!cp_parser_is_keyword (token
, RID_ASM
))
25497 /* Consume the `asm' token. */
25498 cp_lexer_consume_token (parser
->lexer
);
25499 /* Look for the `('. */
25500 matching_parens parens
;
25501 parens
.require_open (parser
);
25503 /* Look for the string-literal. */
25504 asm_specification
= cp_parser_string_literal (parser
, false, false);
25506 /* Look for the `)'. */
25507 parens
.require_close (parser
);
25509 return asm_specification
;
25512 /* Parse an asm-operand-list.
25516 asm-operand-list , asm-operand
25519 string-literal ( expression )
25520 [ string-literal ] string-literal ( expression )
25522 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25523 each node is the expression. The TREE_PURPOSE is itself a
25524 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25525 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25526 is a STRING_CST for the string literal before the parenthesis. Returns
25527 ERROR_MARK_NODE if any of the operands are invalid. */
25530 cp_parser_asm_operand_list (cp_parser
* parser
)
25532 tree asm_operands
= NULL_TREE
;
25533 bool invalid_operands
= false;
25537 tree string_literal
;
25541 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25543 /* Consume the `[' token. */
25544 cp_lexer_consume_token (parser
->lexer
);
25545 /* Read the operand name. */
25546 name
= cp_parser_identifier (parser
);
25547 if (name
!= error_mark_node
)
25548 name
= build_string (IDENTIFIER_LENGTH (name
),
25549 IDENTIFIER_POINTER (name
));
25550 /* Look for the closing `]'. */
25551 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25555 /* Look for the string-literal. */
25556 string_literal
= cp_parser_string_literal (parser
, false, false);
25558 /* Look for the `('. */
25559 matching_parens parens
;
25560 parens
.require_open (parser
);
25561 /* Parse the expression. */
25562 expression
= cp_parser_expression (parser
);
25563 /* Look for the `)'. */
25564 parens
.require_close (parser
);
25566 if (name
== error_mark_node
25567 || string_literal
== error_mark_node
25568 || expression
== error_mark_node
)
25569 invalid_operands
= true;
25571 /* Add this operand to the list. */
25572 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
25575 /* If the next token is not a `,', there are no more
25577 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25579 /* Consume the `,'. */
25580 cp_lexer_consume_token (parser
->lexer
);
25583 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
25586 /* Parse an asm-clobber-list.
25590 asm-clobber-list , string-literal
25592 Returns a TREE_LIST, indicating the clobbers in the order that they
25593 appeared. The TREE_VALUE of each node is a STRING_CST. */
25596 cp_parser_asm_clobber_list (cp_parser
* parser
)
25598 tree clobbers
= NULL_TREE
;
25602 tree string_literal
;
25604 /* Look for the string literal. */
25605 string_literal
= cp_parser_string_literal (parser
, false, false);
25606 /* Add it to the list. */
25607 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
25608 /* If the next token is not a `,', then the list is
25610 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25612 /* Consume the `,' token. */
25613 cp_lexer_consume_token (parser
->lexer
);
25619 /* Parse an asm-label-list.
25623 asm-label-list , identifier
25625 Returns a TREE_LIST, indicating the labels in the order that they
25626 appeared. The TREE_VALUE of each node is a label. */
25629 cp_parser_asm_label_list (cp_parser
* parser
)
25631 tree labels
= NULL_TREE
;
25635 tree identifier
, label
, name
;
25637 /* Look for the identifier. */
25638 identifier
= cp_parser_identifier (parser
);
25639 if (!error_operand_p (identifier
))
25641 label
= lookup_label (identifier
);
25642 if (TREE_CODE (label
) == LABEL_DECL
)
25644 TREE_USED (label
) = 1;
25645 check_goto (label
);
25646 name
= build_string (IDENTIFIER_LENGTH (identifier
),
25647 IDENTIFIER_POINTER (identifier
));
25648 labels
= tree_cons (name
, label
, labels
);
25651 /* If the next token is not a `,', then the list is
25653 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25655 /* Consume the `,' token. */
25656 cp_lexer_consume_token (parser
->lexer
);
25659 return nreverse (labels
);
25662 /* Return TRUE iff the next tokens in the stream are possibly the
25663 beginning of a GNU extension attribute. */
25666 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25668 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25671 /* Return TRUE iff the next tokens in the stream are possibly the
25672 beginning of a standard C++-11 attribute specifier. */
25675 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25677 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25680 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25681 beginning of a standard C++-11 attribute specifier. */
25684 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25686 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25688 return (cxx_dialect
>= cxx11
25689 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25690 || (token
->type
== CPP_OPEN_SQUARE
25691 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25692 && token
->type
== CPP_OPEN_SQUARE
)));
25695 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25696 beginning of a GNU extension attribute. */
25699 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25701 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25703 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25706 /* Return true iff the next tokens can be the beginning of either a
25707 GNU attribute list, or a standard C++11 attribute sequence. */
25710 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25712 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25713 || cp_next_tokens_can_be_std_attribute_p (parser
));
25716 /* Return true iff the next Nth tokens can be the beginning of either
25717 a GNU attribute list, or a standard C++11 attribute sequence. */
25720 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25722 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25723 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25726 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25727 of GNU attributes, or return NULL. */
25730 cp_parser_attributes_opt (cp_parser
*parser
)
25732 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25733 return cp_parser_gnu_attributes_opt (parser
);
25734 return cp_parser_std_attribute_spec_seq (parser
);
25737 /* Parse an (optional) series of attributes.
25740 attributes attribute
25743 __attribute__ (( attribute-list [opt] ))
25745 The return value is as for cp_parser_gnu_attribute_list. */
25748 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25750 tree attributes
= NULL_TREE
;
25752 temp_override
<bool> cleanup
25753 (parser
->auto_is_implicit_function_template_parm_p
, false);
25758 tree attribute_list
;
25761 /* Peek at the next token. */
25762 token
= cp_lexer_peek_token (parser
->lexer
);
25763 /* If it's not `__attribute__', then we're done. */
25764 if (token
->keyword
!= RID_ATTRIBUTE
)
25767 /* Consume the `__attribute__' keyword. */
25768 cp_lexer_consume_token (parser
->lexer
);
25769 /* Look for the two `(' tokens. */
25770 matching_parens outer_parens
;
25771 outer_parens
.require_open (parser
);
25772 matching_parens inner_parens
;
25773 inner_parens
.require_open (parser
);
25775 /* Peek at the next token. */
25776 token
= cp_lexer_peek_token (parser
->lexer
);
25777 if (token
->type
!= CPP_CLOSE_PAREN
)
25778 /* Parse the attribute-list. */
25779 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25781 /* If the next token is a `)', then there is no attribute
25783 attribute_list
= NULL
;
25785 /* Look for the two `)' tokens. */
25786 if (!inner_parens
.require_close (parser
))
25788 if (!outer_parens
.require_close (parser
))
25791 cp_parser_skip_to_end_of_statement (parser
);
25793 /* Add these new attributes to the list. */
25794 attributes
= attr_chainon (attributes
, attribute_list
);
25800 /* Parse a GNU attribute-list.
25804 attribute-list , attribute
25808 identifier ( identifier )
25809 identifier ( identifier , expression-list )
25810 identifier ( expression-list )
25812 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25813 to an attribute. The TREE_PURPOSE of each node is the identifier
25814 indicating which attribute is in use. The TREE_VALUE represents
25815 the arguments, if any. */
25818 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
25820 tree attribute_list
= NULL_TREE
;
25821 bool save_translate_strings_p
= parser
->translate_strings_p
;
25823 /* Don't create wrapper nodes within attributes: the
25824 handlers don't know how to handle them. */
25825 auto_suppress_location_wrappers sentinel
;
25827 parser
->translate_strings_p
= false;
25834 /* Look for the identifier. We also allow keywords here; for
25835 example `__attribute__ ((const))' is legal. */
25836 token
= cp_lexer_peek_token (parser
->lexer
);
25837 if (token
->type
== CPP_NAME
25838 || token
->type
== CPP_KEYWORD
)
25840 tree arguments
= NULL_TREE
;
25842 /* Consume the token, but save it since we need it for the
25843 SIMD enabled function parsing. */
25844 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25846 /* Save away the identifier that indicates which attribute
25848 identifier
= (token
->type
== CPP_KEYWORD
)
25849 /* For keywords, use the canonical spelling, not the
25850 parsed identifier. */
25851 ? ridpointers
[(int) token
->keyword
]
25852 : id_token
->u
.value
;
25854 identifier
= canonicalize_attr_name (identifier
);
25855 attribute
= build_tree_list (identifier
, NULL_TREE
);
25857 /* Peek at the next token. */
25858 token
= cp_lexer_peek_token (parser
->lexer
);
25859 /* If it's an `(', then parse the attribute arguments. */
25860 if (token
->type
== CPP_OPEN_PAREN
)
25862 vec
<tree
, va_gc
> *vec
;
25863 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25864 ? id_attr
: normal_attr
);
25865 vec
= cp_parser_parenthesized_expression_list
25866 (parser
, attr_flag
, /*cast_p=*/false,
25867 /*allow_expansion_p=*/false,
25868 /*non_constant_p=*/NULL
);
25870 arguments
= error_mark_node
;
25873 arguments
= build_tree_list_vec (vec
);
25874 release_tree_vector (vec
);
25876 /* Save the arguments away. */
25877 TREE_VALUE (attribute
) = arguments
;
25880 if (arguments
!= error_mark_node
)
25882 /* Add this attribute to the list. */
25883 TREE_CHAIN (attribute
) = attribute_list
;
25884 attribute_list
= attribute
;
25887 token
= cp_lexer_peek_token (parser
->lexer
);
25889 /* Unless EXACTLY_ONE is set look for more attributes.
25890 If the next token isn't a `,', we're done. */
25891 if (exactly_one
|| token
->type
!= CPP_COMMA
)
25894 /* Consume the comma and keep going. */
25895 cp_lexer_consume_token (parser
->lexer
);
25897 parser
->translate_strings_p
= save_translate_strings_p
;
25899 /* We built up the list in reverse order. */
25900 return nreverse (attribute_list
);
25903 /* Parse a standard C++11 attribute.
25905 The returned representation is a TREE_LIST which TREE_PURPOSE is
25906 the scoped name of the attribute, and the TREE_VALUE is its
25909 Note that the scoped name of the attribute is itself a TREE_LIST
25910 which TREE_PURPOSE is the namespace of the attribute, and
25911 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25912 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25913 and which TREE_PURPOSE is directly the attribute name.
25915 Clients of the attribute code should use get_attribute_namespace
25916 and get_attribute_name to get the actual namespace and name of
25917 attributes, regardless of their being GNU or C++11 attributes.
25920 attribute-token attribute-argument-clause [opt]
25924 attribute-scoped-token
25926 attribute-scoped-token:
25927 attribute-namespace :: identifier
25929 attribute-namespace:
25932 attribute-argument-clause:
25933 ( balanced-token-seq )
25935 balanced-token-seq:
25936 balanced-token [opt]
25937 balanced-token-seq balanced-token
25940 ( balanced-token-seq )
25941 [ balanced-token-seq ]
25942 { balanced-token-seq }. */
25945 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25947 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25950 temp_override
<bool> cleanup
25951 (parser
->auto_is_implicit_function_template_parm_p
, false);
25953 /* First, parse name of the attribute, a.k.a attribute-token. */
25955 token
= cp_lexer_peek_token (parser
->lexer
);
25956 if (token
->type
== CPP_NAME
)
25957 attr_id
= token
->u
.value
;
25958 else if (token
->type
== CPP_KEYWORD
)
25959 attr_id
= ridpointers
[(int) token
->keyword
];
25960 else if (token
->flags
& NAMED_OP
)
25961 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25963 if (attr_id
== NULL_TREE
)
25966 cp_lexer_consume_token (parser
->lexer
);
25968 token
= cp_lexer_peek_token (parser
->lexer
);
25969 if (token
->type
== CPP_SCOPE
)
25971 /* We are seeing a scoped attribute token. */
25973 cp_lexer_consume_token (parser
->lexer
);
25975 error_at (token
->location
, "attribute using prefix used together "
25976 "with scoped attribute token");
25979 token
= cp_lexer_consume_token (parser
->lexer
);
25980 if (token
->type
== CPP_NAME
)
25981 attr_id
= token
->u
.value
;
25982 else if (token
->type
== CPP_KEYWORD
)
25983 attr_id
= ridpointers
[(int) token
->keyword
];
25984 else if (token
->flags
& NAMED_OP
)
25985 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25988 error_at (token
->location
,
25989 "expected an identifier for the attribute name");
25990 return error_mark_node
;
25993 attr_ns
= canonicalize_attr_name (attr_ns
);
25994 attr_id
= canonicalize_attr_name (attr_id
);
25995 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25997 token
= cp_lexer_peek_token (parser
->lexer
);
26001 attr_ns
= canonicalize_attr_name (attr_ns
);
26002 attr_id
= canonicalize_attr_name (attr_id
);
26003 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
26008 attr_id
= canonicalize_attr_name (attr_id
);
26009 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
26011 /* C++11 noreturn attribute is equivalent to GNU's. */
26012 if (is_attribute_p ("noreturn", attr_id
))
26013 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26014 /* C++14 deprecated attribute is equivalent to GNU's. */
26015 else if (is_attribute_p ("deprecated", attr_id
))
26016 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26017 /* C++17 fallthrough attribute is equivalent to GNU's. */
26018 else if (is_attribute_p ("fallthrough", attr_id
))
26019 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26020 /* Transactional Memory TS optimize_for_synchronized attribute is
26021 equivalent to GNU transaction_callable. */
26022 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
26023 TREE_PURPOSE (attribute
)
26024 = get_identifier ("transaction_callable");
26025 /* Transactional Memory attributes are GNU attributes. */
26026 else if (tm_attr_to_mask (attr_id
))
26027 TREE_PURPOSE (attribute
) = attr_id
;
26030 /* Now parse the optional argument clause of the attribute. */
26032 if (token
->type
!= CPP_OPEN_PAREN
)
26036 vec
<tree
, va_gc
> *vec
;
26037 int attr_flag
= normal_attr
;
26039 if (attr_ns
== gnu_identifier
26040 && attribute_takes_identifier_p (attr_id
))
26041 /* A GNU attribute that takes an identifier in parameter. */
26042 attr_flag
= id_attr
;
26044 vec
= cp_parser_parenthesized_expression_list
26045 (parser
, attr_flag
, /*cast_p=*/false,
26046 /*allow_expansion_p=*/true,
26047 /*non_constant_p=*/NULL
);
26049 arguments
= error_mark_node
;
26052 arguments
= build_tree_list_vec (vec
);
26053 release_tree_vector (vec
);
26056 if (arguments
== error_mark_node
)
26057 attribute
= error_mark_node
;
26059 TREE_VALUE (attribute
) = arguments
;
26065 /* Check that the attribute ATTRIBUTE appears at most once in the
26066 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26067 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26068 isn't implemented yet in GCC. */
26071 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
26075 tree name
= get_attribute_name (attribute
);
26076 if (is_attribute_p ("noreturn", name
)
26077 && lookup_attribute ("noreturn", attributes
))
26078 error ("attribute %<noreturn%> can appear at most once "
26079 "in an attribute-list");
26080 else if (is_attribute_p ("deprecated", name
)
26081 && lookup_attribute ("deprecated", attributes
))
26082 error ("attribute %<deprecated%> can appear at most once "
26083 "in an attribute-list");
26087 /* Parse a list of standard C++-11 attributes.
26091 attribute-list , attribute[opt]
26093 attribute-list , attribute ...
26097 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
26099 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
26100 cp_token
*token
= NULL
;
26104 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
26105 if (attribute
== error_mark_node
)
26107 if (attribute
!= NULL_TREE
)
26109 cp_parser_check_std_attribute (attributes
, attribute
);
26110 TREE_CHAIN (attribute
) = attributes
;
26111 attributes
= attribute
;
26113 token
= cp_lexer_peek_token (parser
->lexer
);
26114 if (token
->type
== CPP_ELLIPSIS
)
26116 cp_lexer_consume_token (parser
->lexer
);
26117 if (attribute
== NULL_TREE
)
26118 error_at (token
->location
,
26119 "expected attribute before %<...%>");
26122 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
26123 if (pack
== error_mark_node
)
26124 return error_mark_node
;
26125 TREE_VALUE (attribute
) = pack
;
26127 token
= cp_lexer_peek_token (parser
->lexer
);
26129 if (token
->type
!= CPP_COMMA
)
26131 cp_lexer_consume_token (parser
->lexer
);
26133 attributes
= nreverse (attributes
);
26137 /* Parse a standard C++-11 attribute specifier.
26139 attribute-specifier:
26140 [ [ attribute-using-prefix [opt] attribute-list ] ]
26141 alignment-specifier
26143 attribute-using-prefix:
26144 using attribute-namespace :
26146 alignment-specifier:
26147 alignas ( type-id ... [opt] )
26148 alignas ( alignment-expression ... [opt] ). */
26151 cp_parser_std_attribute_spec (cp_parser
*parser
)
26153 tree attributes
= NULL_TREE
;
26154 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26156 if (token
->type
== CPP_OPEN_SQUARE
26157 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
26159 tree attr_ns
= NULL_TREE
;
26161 cp_lexer_consume_token (parser
->lexer
);
26162 cp_lexer_consume_token (parser
->lexer
);
26164 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26166 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26167 if (token
->type
== CPP_NAME
)
26168 attr_ns
= token
->u
.value
;
26169 else if (token
->type
== CPP_KEYWORD
)
26170 attr_ns
= ridpointers
[(int) token
->keyword
];
26171 else if (token
->flags
& NAMED_OP
)
26172 attr_ns
= get_identifier (cpp_type2name (token
->type
,
26175 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
26177 if (cxx_dialect
< cxx17
26178 && !in_system_header_at (input_location
))
26179 pedwarn (input_location
, 0,
26180 "attribute using prefix only available "
26181 "with -std=c++17 or -std=gnu++17");
26183 cp_lexer_consume_token (parser
->lexer
);
26184 cp_lexer_consume_token (parser
->lexer
);
26185 cp_lexer_consume_token (parser
->lexer
);
26188 attr_ns
= NULL_TREE
;
26191 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
26193 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
26194 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
26195 cp_parser_skip_to_end_of_statement (parser
);
26197 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26198 when we are sure that we have actually parsed them. */
26199 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26205 /* Look for an alignment-specifier. */
26207 token
= cp_lexer_peek_token (parser
->lexer
);
26209 if (token
->type
!= CPP_KEYWORD
26210 || token
->keyword
!= RID_ALIGNAS
)
26213 cp_lexer_consume_token (parser
->lexer
);
26214 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26216 matching_parens parens
;
26217 if (!parens
.require_open (parser
))
26218 return error_mark_node
;
26220 cp_parser_parse_tentatively (parser
);
26221 alignas_expr
= cp_parser_type_id (parser
);
26223 if (!cp_parser_parse_definitely (parser
))
26225 alignas_expr
= cp_parser_assignment_expression (parser
);
26226 if (alignas_expr
== error_mark_node
)
26227 cp_parser_skip_to_end_of_statement (parser
);
26228 if (alignas_expr
== NULL_TREE
26229 || alignas_expr
== error_mark_node
)
26230 return alignas_expr
;
26233 alignas_expr
= cxx_alignas_expr (alignas_expr
);
26234 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
26236 /* Handle alignas (pack...). */
26237 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26239 cp_lexer_consume_token (parser
->lexer
);
26240 alignas_expr
= make_pack_expansion (alignas_expr
);
26243 /* Something went wrong, so don't build the attribute. */
26244 if (alignas_expr
== error_mark_node
)
26245 return error_mark_node
;
26247 if (!parens
.require_close (parser
))
26248 return error_mark_node
;
26250 /* Build the C++-11 representation of an 'aligned'
26253 = build_tree_list (build_tree_list (gnu_identifier
,
26254 aligned_identifier
), alignas_expr
);
26260 /* Parse a standard C++-11 attribute-specifier-seq.
26262 attribute-specifier-seq:
26263 attribute-specifier-seq [opt] attribute-specifier
26267 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
26269 tree attr_specs
= NULL_TREE
;
26270 tree attr_last
= NULL_TREE
;
26272 /* Don't create wrapper nodes within attributes: the
26273 handlers don't know how to handle them. */
26274 auto_suppress_location_wrappers sentinel
;
26278 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
26279 if (attr_spec
== NULL_TREE
)
26281 if (attr_spec
== error_mark_node
)
26282 return error_mark_node
;
26285 TREE_CHAIN (attr_last
) = attr_spec
;
26287 attr_specs
= attr_last
= attr_spec
;
26288 attr_last
= tree_last (attr_last
);
26294 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26295 return index of the first token after balanced-token, or N on failure. */
26298 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
26301 int nparens
= 0, nbraces
= 0, nsquares
= 0;
26303 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
26305 case CPP_PRAGMA_EOL
:
26306 if (!parser
->lexer
->in_pragma
)
26310 /* Ran out of tokens. */
26312 case CPP_OPEN_PAREN
:
26315 case CPP_OPEN_BRACE
:
26318 case CPP_OPEN_SQUARE
:
26321 case CPP_CLOSE_PAREN
:
26324 case CPP_CLOSE_BRACE
:
26327 case CPP_CLOSE_SQUARE
:
26333 while (nparens
|| nbraces
|| nsquares
);
26337 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26338 return index of the first token after the GNU attribute tokens, or N on
26342 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
26346 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
26347 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
26348 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
26351 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
26354 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
26361 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26362 next token), return index of the first token after the standard C++11
26363 attribute tokens, or N on failure. */
26366 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
26370 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
26371 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
26373 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26376 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
26380 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
26381 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
26383 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26394 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26395 as the next token), return index of the first token after the attribute
26396 tokens, or N on failure. */
26399 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
26401 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
26402 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
26403 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
26406 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26407 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26408 current value of the PEDANTIC flag, regardless of whether or not
26409 the `__extension__' keyword is present. The caller is responsible
26410 for restoring the value of the PEDANTIC flag. */
26413 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
26415 /* Save the old value of the PEDANTIC flag. */
26416 *saved_pedantic
= pedantic
;
26418 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
26420 /* Consume the `__extension__' token. */
26421 cp_lexer_consume_token (parser
->lexer
);
26422 /* We're not being pedantic while the `__extension__' keyword is
26432 /* Parse a label declaration.
26435 __label__ label-declarator-seq ;
26437 label-declarator-seq:
26438 identifier , label-declarator-seq
26442 cp_parser_label_declaration (cp_parser
* parser
)
26444 /* Look for the `__label__' keyword. */
26445 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
26451 /* Look for an identifier. */
26452 identifier
= cp_parser_identifier (parser
);
26453 /* If we failed, stop. */
26454 if (identifier
== error_mark_node
)
26456 /* Declare it as a label. */
26457 finish_label_decl (identifier
);
26458 /* If the next token is a `;', stop. */
26459 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26461 /* Look for the `,' separating the label declarations. */
26462 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
26465 /* Look for the final `;'. */
26466 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
26469 // -------------------------------------------------------------------------- //
26472 // Parse a requires clause.
26474 // requires-clause:
26475 // 'requires' logical-or-expression
26477 // The required logical-or-expression must be a constant expression. Note
26478 // that we don't check that the expression is constepxr here. We defer until
26479 // we analyze constraints and then, we only check atomic constraints.
26481 cp_parser_requires_clause (cp_parser
*parser
)
26483 // Parse the requires clause so that it is not automatically folded.
26484 ++processing_template_decl
;
26485 tree expr
= cp_parser_binary_expression (parser
, false, false,
26486 PREC_NOT_OPERATOR
, NULL
);
26487 if (check_for_bare_parameter_packs (expr
))
26488 expr
= error_mark_node
;
26489 --processing_template_decl
;
26493 // Optionally parse a requires clause:
26495 cp_parser_requires_clause_opt (cp_parser
*parser
)
26497 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
26498 if (tok
->keyword
!= RID_REQUIRES
)
26500 if (!flag_concepts
&& tok
->type
== CPP_NAME
26501 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
26503 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26504 "%<requires%> only available with -fconcepts");
26505 /* Parse and discard the requires-clause. */
26506 cp_lexer_consume_token (parser
->lexer
);
26507 cp_parser_requires_clause (parser
);
26511 cp_lexer_consume_token (parser
->lexer
);
26512 return cp_parser_requires_clause (parser
);
26516 /*---------------------------------------------------------------------------
26517 Requires expressions
26518 ---------------------------------------------------------------------------*/
26520 /* Parse a requires expression
26522 requirement-expression:
26523 'requires' requirement-parameter-list [opt] requirement-body */
26525 cp_parser_requires_expression (cp_parser
*parser
)
26527 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
26528 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26530 /* A requires-expression shall appear only within a concept
26531 definition or a requires-clause.
26533 TODO: Implement this diagnostic correctly. */
26534 if (!processing_template_decl
)
26536 error_at (loc
, "a requires expression cannot appear outside a template");
26537 cp_parser_skip_to_end_of_statement (parser
);
26538 return error_mark_node
;
26543 /* Local parameters are delared as variables within the scope
26544 of the expression. They are not visible past the end of
26545 the expression. Expressions within the requires-expression
26546 are unevaluated. */
26547 struct scope_sentinel
26551 ++cp_unevaluated_operand
;
26552 begin_scope (sk_block
, NULL_TREE
);
26557 pop_bindings_and_leave_scope ();
26558 --cp_unevaluated_operand
;
26562 /* Parse the optional parameter list. */
26563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26565 parms
= cp_parser_requirement_parameter_list (parser
);
26566 if (parms
== error_mark_node
)
26567 return error_mark_node
;
26572 /* Parse the requirement body. */
26573 reqs
= cp_parser_requirement_body (parser
);
26574 if (reqs
== error_mark_node
)
26575 return error_mark_node
;
26578 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26580 grokparms (parms
, &parms
);
26581 return finish_requires_expr (parms
, reqs
);
26584 /* Parse a parameterized requirement.
26586 requirement-parameter-list:
26587 '(' parameter-declaration-clause ')' */
26589 cp_parser_requirement_parameter_list (cp_parser
*parser
)
26591 matching_parens parens
;
26592 if (!parens
.require_open (parser
))
26593 return error_mark_node
;
26596 = cp_parser_parameter_declaration_clause (parser
, CP_PARSER_FLAGS_NONE
);
26598 if (!parens
.require_close (parser
))
26599 return error_mark_node
;
26604 /* Parse the body of a requirement.
26607 '{' requirement-list '}' */
26609 cp_parser_requirement_body (cp_parser
*parser
)
26611 matching_braces braces
;
26612 if (!braces
.require_open (parser
))
26613 return error_mark_node
;
26615 tree reqs
= cp_parser_requirement_list (parser
);
26617 if (!braces
.require_close (parser
))
26618 return error_mark_node
;
26623 /* Parse a list of requirements.
26627 requirement-list ';' requirement[opt] */
26629 cp_parser_requirement_list (cp_parser
*parser
)
26631 tree result
= NULL_TREE
;
26634 tree req
= cp_parser_requirement (parser
);
26635 if (req
== error_mark_node
)
26636 return error_mark_node
;
26638 result
= tree_cons (NULL_TREE
, req
, result
);
26640 /* If we see a semi-colon, consume it. */
26641 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26642 cp_lexer_consume_token (parser
->lexer
);
26644 /* Stop processing at the end of the list. */
26645 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26649 /* Reverse the order of requirements so they are analyzed in
26650 declaration order. */
26651 return nreverse (result
);
26654 /* Parse a syntactic requirement or type requirement.
26658 compound-requirement
26660 nested-requirement */
26662 cp_parser_requirement (cp_parser
*parser
)
26664 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26665 return cp_parser_compound_requirement (parser
);
26666 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26667 return cp_parser_type_requirement (parser
);
26668 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
26669 return cp_parser_nested_requirement (parser
);
26671 return cp_parser_simple_requirement (parser
);
26674 /* Parse a simple requirement.
26676 simple-requirement:
26679 cp_parser_simple_requirement (cp_parser
*parser
)
26681 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26682 if (!expr
|| expr
== error_mark_node
)
26683 return error_mark_node
;
26685 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26686 return error_mark_node
;
26688 return finish_simple_requirement (expr
);
26691 /* Parse a type requirement
26694 nested-name-specifier [opt] required-type-name ';'
26696 required-type-name:
26698 'template' [opt] simple-template-id */
26700 cp_parser_type_requirement (cp_parser
*parser
)
26702 cp_lexer_consume_token (parser
->lexer
);
26704 // Save the scope before parsing name specifiers.
26705 tree saved_scope
= parser
->scope
;
26706 tree saved_object_scope
= parser
->object_scope
;
26707 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26708 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26709 cp_parser_nested_name_specifier_opt (parser
,
26710 /*typename_keyword_p=*/true,
26711 /*check_dependency_p=*/false,
26713 /*is_declaration=*/false);
26716 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26718 cp_lexer_consume_token (parser
->lexer
);
26719 type
= cp_parser_template_id (parser
,
26720 /*template_keyword_p=*/true,
26721 /*check_dependency=*/false,
26722 /*tag_type=*/none_type
,
26723 /*is_declaration=*/false);
26724 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26725 /*complain=*/tf_error
);
26728 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26730 if (TREE_CODE (type
) == TYPE_DECL
)
26731 type
= TREE_TYPE (type
);
26733 parser
->scope
= saved_scope
;
26734 parser
->object_scope
= saved_object_scope
;
26735 parser
->qualifying_scope
= saved_qualifying_scope
;
26737 if (type
== error_mark_node
)
26738 cp_parser_skip_to_end_of_statement (parser
);
26740 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26741 return error_mark_node
;
26742 if (type
== error_mark_node
)
26743 return error_mark_node
;
26745 return finish_type_requirement (type
);
26748 /* Parse a compound requirement
26750 compound-requirement:
26751 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26753 cp_parser_compound_requirement (cp_parser
*parser
)
26755 /* Parse an expression enclosed in '{ }'s. */
26756 matching_braces braces
;
26757 if (!braces
.require_open (parser
))
26758 return error_mark_node
;
26760 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26761 if (!expr
|| expr
== error_mark_node
)
26762 return error_mark_node
;
26764 if (!braces
.require_close (parser
))
26765 return error_mark_node
;
26767 /* Parse the optional noexcept. */
26768 bool noexcept_p
= false;
26769 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26771 cp_lexer_consume_token (parser
->lexer
);
26775 /* Parse the optional trailing return type. */
26776 tree type
= NULL_TREE
;
26777 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26779 cp_lexer_consume_token (parser
->lexer
);
26780 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26781 parser
->in_result_type_constraint_p
= true;
26782 type
= cp_parser_trailing_type_id (parser
);
26783 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26784 if (type
== error_mark_node
)
26785 return error_mark_node
;
26788 return finish_compound_requirement (expr
, type
, noexcept_p
);
26791 /* Parse a nested requirement. This is the same as a requires clause.
26793 nested-requirement:
26796 cp_parser_nested_requirement (cp_parser
*parser
)
26798 cp_lexer_consume_token (parser
->lexer
);
26799 tree req
= cp_parser_requires_clause (parser
);
26800 if (req
== error_mark_node
)
26801 return error_mark_node
;
26802 return finish_nested_requirement (req
);
26805 /* Support Functions */
26807 /* Return the appropriate prefer_type argument for lookup_name_real based on
26808 tag_type and template_mem_access. */
26811 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26813 /* DR 141: When looking in the current enclosing context for a template-name
26814 after -> or ., only consider class templates. */
26815 if (template_mem_access
)
26819 case none_type
: return 0; // No preference.
26820 case scope_type
: return 1; // Type or namespace.
26821 default: return 2; // Type only.
26825 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26826 NAME should have one of the representations used for an
26827 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26828 is returned. If PARSER->SCOPE is a dependent type, then a
26829 SCOPE_REF is returned.
26831 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26832 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26833 was formed. Abstractly, such entities should not be passed to this
26834 function, because they do not need to be looked up, but it is
26835 simpler to check for this special case here, rather than at the
26838 In cases not explicitly covered above, this function returns a
26839 DECL, OVERLOAD, or baselink representing the result of the lookup.
26840 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26843 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26844 (e.g., "struct") that was used. In that case bindings that do not
26845 refer to types are ignored.
26847 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26850 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26853 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26856 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26857 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26858 NULL_TREE otherwise. */
26861 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26862 enum tag_types tag_type
,
26865 bool check_dependency
,
26866 tree
*ambiguous_decls
,
26867 location_t name_location
)
26870 tree object_type
= parser
->context
->object_type
;
26872 /* Assume that the lookup will be unambiguous. */
26873 if (ambiguous_decls
)
26874 *ambiguous_decls
= NULL_TREE
;
26876 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26877 no longer valid. Note that if we are parsing tentatively, and
26878 the parse fails, OBJECT_TYPE will be automatically restored. */
26879 parser
->context
->object_type
= NULL_TREE
;
26881 if (name
== error_mark_node
)
26882 return error_mark_node
;
26884 /* A template-id has already been resolved; there is no lookup to
26886 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26888 if (BASELINK_P (name
))
26890 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26891 == TEMPLATE_ID_EXPR
);
26895 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26896 it should already have been checked to make sure that the name
26897 used matches the type being destroyed. */
26898 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26902 /* Figure out to which type this destructor applies. */
26904 type
= parser
->scope
;
26905 else if (object_type
)
26906 type
= object_type
;
26908 type
= current_class_type
;
26909 /* If that's not a class type, there is no destructor. */
26910 if (!type
|| !CLASS_TYPE_P (type
))
26911 return error_mark_node
;
26913 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26914 lazily_declare_fn (sfk_destructor
, type
);
26916 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26919 return error_mark_node
;
26922 /* By this point, the NAME should be an ordinary identifier. If
26923 the id-expression was a qualified name, the qualifying scope is
26924 stored in PARSER->SCOPE at this point. */
26925 gcc_assert (identifier_p (name
));
26927 /* Perform the lookup. */
26932 if (parser
->scope
== error_mark_node
)
26933 return error_mark_node
;
26935 /* If the SCOPE is dependent, the lookup must be deferred until
26936 the template is instantiated -- unless we are explicitly
26937 looking up names in uninstantiated templates. Even then, we
26938 cannot look up the name if the scope is not a class type; it
26939 might, for example, be a template type parameter. */
26940 dependent_p
= (TYPE_P (parser
->scope
)
26941 && dependent_scope_p (parser
->scope
));
26942 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26944 /* Defer lookup. */
26945 decl
= error_mark_node
;
26948 tree pushed_scope
= NULL_TREE
;
26950 /* If PARSER->SCOPE is a dependent type, then it must be a
26951 class type, and we must not be checking dependencies;
26952 otherwise, we would have processed this lookup above. So
26953 that PARSER->SCOPE is not considered a dependent base by
26954 lookup_member, we must enter the scope here. */
26956 pushed_scope
= push_scope (parser
->scope
);
26958 /* If the PARSER->SCOPE is a template specialization, it
26959 may be instantiated during name lookup. In that case,
26960 errors may be issued. Even if we rollback the current
26961 tentative parse, those errors are valid. */
26962 decl
= lookup_qualified_name (parser
->scope
, name
,
26963 prefer_type_arg (tag_type
),
26964 /*complain=*/true);
26966 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26967 lookup result and the nested-name-specifier nominates a class C:
26968 * if the name specified after the nested-name-specifier, when
26969 looked up in C, is the injected-class-name of C (Clause 9), or
26970 * if the name specified after the nested-name-specifier is the
26971 same as the identifier or the simple-template-id's template-
26972 name in the last component of the nested-name-specifier,
26973 the name is instead considered to name the constructor of
26974 class C. [ Note: for example, the constructor is not an
26975 acceptable lookup result in an elaborated-type-specifier so
26976 the constructor would not be used in place of the
26977 injected-class-name. --end note ] Such a constructor name
26978 shall be used only in the declarator-id of a declaration that
26979 names a constructor or in a using-declaration. */
26980 if (tag_type
== none_type
26981 && DECL_SELF_REFERENCE_P (decl
)
26982 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26983 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26984 prefer_type_arg (tag_type
),
26985 /*complain=*/true);
26987 /* If we have a single function from a using decl, pull it out. */
26988 if (TREE_CODE (decl
) == OVERLOAD
26989 && !really_overloaded_fn (decl
))
26990 decl
= OVL_FUNCTION (decl
);
26993 pop_scope (pushed_scope
);
26996 /* If the scope is a dependent type and either we deferred lookup or
26997 we did lookup but didn't find the name, rememeber the name. */
26998 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26999 && dependent_type_p (parser
->scope
))
27005 /* The resolution to Core Issue 180 says that `struct
27006 A::B' should be considered a type-name, even if `A'
27008 type
= make_typename_type (parser
->scope
, name
, tag_type
,
27009 /*complain=*/tf_error
);
27010 if (type
!= error_mark_node
)
27011 decl
= TYPE_NAME (type
);
27013 else if (is_template
27014 && (cp_parser_next_token_ends_template_argument_p (parser
)
27015 || cp_lexer_next_token_is (parser
->lexer
,
27017 decl
= make_unbound_class_template (parser
->scope
,
27019 /*complain=*/tf_error
);
27021 decl
= build_qualified_name (/*type=*/NULL_TREE
,
27022 parser
->scope
, name
,
27025 parser
->qualifying_scope
= parser
->scope
;
27026 parser
->object_scope
= NULL_TREE
;
27028 else if (object_type
)
27030 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27031 OBJECT_TYPE is not a class. */
27032 if (CLASS_TYPE_P (object_type
))
27033 /* If the OBJECT_TYPE is a template specialization, it may
27034 be instantiated during name lookup. In that case, errors
27035 may be issued. Even if we rollback the current tentative
27036 parse, those errors are valid. */
27037 decl
= lookup_member (object_type
,
27040 prefer_type_arg (tag_type
),
27041 tf_warning_or_error
);
27046 /* Look it up in the enclosing context. DR 141: When looking for a
27047 template-name after -> or ., only consider class templates. */
27048 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
27050 /*block_p=*/true, is_namespace
, 0);
27051 if (object_type
== unknown_type_node
)
27052 /* The object is type-dependent, so we can't look anything up; we used
27053 this to get the DR 141 behavior. */
27054 object_type
= NULL_TREE
;
27055 parser
->object_scope
= object_type
;
27056 parser
->qualifying_scope
= NULL_TREE
;
27060 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
27062 /*block_p=*/true, is_namespace
, 0);
27063 parser
->qualifying_scope
= NULL_TREE
;
27064 parser
->object_scope
= NULL_TREE
;
27067 /* If the lookup failed, let our caller know. */
27068 if (!decl
|| decl
== error_mark_node
)
27069 return error_mark_node
;
27071 /* Pull out the template from an injected-class-name (or multiple). */
27073 decl
= maybe_get_template_decl_from_type_decl (decl
);
27075 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27076 if (TREE_CODE (decl
) == TREE_LIST
)
27078 if (ambiguous_decls
)
27079 *ambiguous_decls
= decl
;
27080 /* The error message we have to print is too complicated for
27081 cp_parser_error, so we incorporate its actions directly. */
27082 if (!cp_parser_simulate_error (parser
))
27084 error_at (name_location
, "reference to %qD is ambiguous",
27086 print_candidates (decl
);
27088 return error_mark_node
;
27091 gcc_assert (DECL_P (decl
)
27092 || TREE_CODE (decl
) == OVERLOAD
27093 || TREE_CODE (decl
) == SCOPE_REF
27094 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
27095 || BASELINK_P (decl
));
27097 /* If we have resolved the name of a member declaration, check to
27098 see if the declaration is accessible. When the name resolves to
27099 set of overloaded functions, accessibility is checked when
27100 overload resolution is done.
27102 During an explicit instantiation, access is not checked at all,
27103 as per [temp.explicit]. */
27105 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
27107 maybe_record_typedef_use (decl
);
27109 return cp_expr (decl
, name_location
);
27112 /* Like cp_parser_lookup_name, but for use in the typical case where
27113 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27114 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27117 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
27119 return cp_parser_lookup_name (parser
, name
,
27121 /*is_template=*/false,
27122 /*is_namespace=*/false,
27123 /*check_dependency=*/true,
27124 /*ambiguous_decls=*/NULL
,
27128 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27129 the current context, return the TYPE_DECL. If TAG_NAME_P is
27130 true, the DECL indicates the class being defined in a class-head,
27131 or declared in an elaborated-type-specifier.
27133 Otherwise, return DECL. */
27136 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
27138 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27139 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27142 template <typename T> struct B;
27145 template <typename T> struct A::B {};
27147 Similarly, in an elaborated-type-specifier:
27149 namespace N { struct X{}; }
27152 template <typename T> friend struct N::X;
27155 However, if the DECL refers to a class type, and we are in
27156 the scope of the class, then the name lookup automatically
27157 finds the TYPE_DECL created by build_self_reference rather
27158 than a TEMPLATE_DECL. For example, in:
27160 template <class T> struct S {
27164 there is no need to handle such case. */
27166 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
27167 return DECL_TEMPLATE_RESULT (decl
);
27172 /* If too many, or too few, template-parameter lists apply to the
27173 declarator, issue an error message. Returns TRUE if all went well,
27174 and FALSE otherwise. */
27177 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
27178 cp_declarator
*declarator
,
27179 location_t declarator_location
)
27181 switch (declarator
->kind
)
27185 unsigned num_templates
= 0;
27186 tree scope
= declarator
->u
.id
.qualifying_scope
;
27187 bool template_id_p
= false;
27190 num_templates
= num_template_headers_for_class (scope
);
27191 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
27192 == TEMPLATE_ID_EXPR
)
27194 /* If the DECLARATOR has the form `X<y>' then it uses one
27195 additional level of template parameters. */
27197 template_id_p
= true;
27200 return cp_parser_check_template_parameters
27201 (parser
, num_templates
, template_id_p
, declarator_location
,
27208 case cdk_reference
:
27210 return (cp_parser_check_declarator_template_parameters
27211 (parser
, declarator
->declarator
, declarator_location
));
27218 gcc_unreachable ();
27223 /* NUM_TEMPLATES were used in the current declaration. If that is
27224 invalid, return FALSE and issue an error messages. Otherwise,
27225 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27226 declarator and we can print more accurate diagnostics. */
27229 cp_parser_check_template_parameters (cp_parser
* parser
,
27230 unsigned num_templates
,
27231 bool template_id_p
,
27232 location_t location
,
27233 cp_declarator
*declarator
)
27235 /* If there are the same number of template classes and parameter
27236 lists, that's OK. */
27237 if (parser
->num_template_parameter_lists
== num_templates
)
27239 /* If there are more, but only one more, and the name ends in an identifier,
27240 then we are declaring a primary template. That's OK too. */
27242 && parser
->num_template_parameter_lists
== num_templates
+ 1)
27244 /* If there are more template classes than parameter lists, we have
27247 template <class T> void S<T>::R<T>::f (); */
27248 if (parser
->num_template_parameter_lists
< num_templates
)
27250 if (declarator
&& !current_function_decl
)
27251 error_at (location
, "specializing member %<%T::%E%> "
27252 "requires %<template<>%> syntax",
27253 declarator
->u
.id
.qualifying_scope
,
27254 declarator
->u
.id
.unqualified_name
);
27255 else if (declarator
)
27256 error_at (location
, "invalid declaration of %<%T::%E%>",
27257 declarator
->u
.id
.qualifying_scope
,
27258 declarator
->u
.id
.unqualified_name
);
27260 error_at (location
, "too few template-parameter-lists");
27263 /* Otherwise, there are too many template parameter lists. We have
27266 template <class T> template <class U> void S::f(); */
27267 error_at (location
, "too many template-parameter-lists");
27271 /* Parse an optional `::' token indicating that the following name is
27272 from the global namespace. If so, PARSER->SCOPE is set to the
27273 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27274 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27275 Returns the new value of PARSER->SCOPE, if the `::' token is
27276 present, and NULL_TREE otherwise. */
27279 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
27283 /* Peek at the next token. */
27284 token
= cp_lexer_peek_token (parser
->lexer
);
27285 /* If we're looking at a `::' token then we're starting from the
27286 global namespace, not our current location. */
27287 if (token
->type
== CPP_SCOPE
)
27289 /* Consume the `::' token. */
27290 cp_lexer_consume_token (parser
->lexer
);
27291 /* Set the SCOPE so that we know where to start the lookup. */
27292 parser
->scope
= global_namespace
;
27293 parser
->qualifying_scope
= global_namespace
;
27294 parser
->object_scope
= NULL_TREE
;
27296 return parser
->scope
;
27298 else if (!current_scope_valid_p
)
27300 parser
->scope
= NULL_TREE
;
27301 parser
->qualifying_scope
= NULL_TREE
;
27302 parser
->object_scope
= NULL_TREE
;
27308 /* Returns TRUE if the upcoming token sequence is the start of a
27309 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27310 declarator is preceded by the `friend' specifier. */
27313 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
27315 bool constructor_p
;
27316 bool outside_class_specifier_p
;
27317 tree nested_name_specifier
;
27318 cp_token
*next_token
;
27320 /* The common case is that this is not a constructor declarator, so
27321 try to avoid doing lots of work if at all possible. It's not
27322 valid declare a constructor at function scope. */
27323 if (parser
->in_function_body
)
27325 /* And only certain tokens can begin a constructor declarator. */
27326 next_token
= cp_lexer_peek_token (parser
->lexer
);
27327 if (next_token
->type
!= CPP_NAME
27328 && next_token
->type
!= CPP_SCOPE
27329 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
27330 && next_token
->type
!= CPP_TEMPLATE_ID
)
27333 /* Parse tentatively; we are going to roll back all of the tokens
27335 cp_parser_parse_tentatively (parser
);
27336 /* Assume that we are looking at a constructor declarator. */
27337 constructor_p
= true;
27339 /* Look for the optional `::' operator. */
27340 cp_parser_global_scope_opt (parser
,
27341 /*current_scope_valid_p=*/false);
27342 /* Look for the nested-name-specifier. */
27343 nested_name_specifier
27344 = (cp_parser_nested_name_specifier_opt (parser
,
27345 /*typename_keyword_p=*/false,
27346 /*check_dependency_p=*/false,
27348 /*is_declaration=*/false));
27350 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27351 if (nested_name_specifier
27352 && TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
27354 tree s
= resolve_typename_type (nested_name_specifier
,
27355 /*only_current_p=*/false);
27356 if (TREE_CODE (s
) != TYPENAME_TYPE
)
27357 nested_name_specifier
= s
;
27360 outside_class_specifier_p
= (!at_class_scope_p ()
27361 || !TYPE_BEING_DEFINED (current_class_type
)
27364 /* Outside of a class-specifier, there must be a
27365 nested-name-specifier. Except in C++17 mode, where we
27366 might be declaring a guiding declaration. */
27367 if (!nested_name_specifier
&& outside_class_specifier_p
27368 && cxx_dialect
< cxx17
)
27369 constructor_p
= false;
27370 else if (nested_name_specifier
== error_mark_node
)
27371 constructor_p
= false;
27373 /* If we have a class scope, this is easy; DR 147 says that S::S always
27374 names the constructor, and no other qualified name could. */
27375 if (constructor_p
&& nested_name_specifier
27376 && CLASS_TYPE_P (nested_name_specifier
))
27378 tree id
= cp_parser_unqualified_id (parser
,
27379 /*template_keyword_p=*/false,
27380 /*check_dependency_p=*/false,
27381 /*declarator_p=*/true,
27382 /*optional_p=*/false);
27383 if (is_overloaded_fn (id
))
27384 id
= DECL_NAME (get_first_fn (id
));
27385 if (!constructor_name_p (id
, nested_name_specifier
))
27386 constructor_p
= false;
27388 /* If we still think that this might be a constructor-declarator,
27389 look for a class-name. */
27390 else if (constructor_p
)
27394 template <typename T> struct S {
27398 we must recognize that the nested `S' names a class. */
27399 if (cxx_dialect
>= cxx17
)
27400 cp_parser_parse_tentatively (parser
);
27403 type_decl
= cp_parser_class_name (parser
,
27404 /*typename_keyword_p=*/false,
27405 /*template_keyword_p=*/false,
27407 /*check_dependency_p=*/false,
27408 /*class_head_p=*/false,
27409 /*is_declaration=*/false);
27411 if (cxx_dialect
>= cxx17
27412 && !cp_parser_parse_definitely (parser
))
27414 type_decl
= NULL_TREE
;
27415 tree tmpl
= cp_parser_template_name (parser
,
27416 /*template_keyword*/false,
27417 /*check_dependency_p*/false,
27418 /*is_declaration*/false,
27420 /*is_identifier*/NULL
);
27421 if (DECL_CLASS_TEMPLATE_P (tmpl
)
27422 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
27423 /* It's a deduction guide, return true. */;
27425 cp_parser_simulate_error (parser
);
27428 /* If there was no class-name, then this is not a constructor.
27429 Otherwise, if we are in a class-specifier and we aren't
27430 handling a friend declaration, check that its type matches
27431 current_class_type (c++/38313). Note: error_mark_node
27432 is left alone for error recovery purposes. */
27433 constructor_p
= (!cp_parser_error_occurred (parser
)
27434 && (outside_class_specifier_p
27435 || type_decl
== NULL_TREE
27436 || type_decl
== error_mark_node
27437 || same_type_p (current_class_type
,
27438 TREE_TYPE (type_decl
))));
27440 /* If we're still considering a constructor, we have to see a `(',
27441 to begin the parameter-declaration-clause, followed by either a
27442 `)', an `...', or a decl-specifier. We need to check for a
27443 type-specifier to avoid being fooled into thinking that:
27447 is a constructor. (It is actually a function named `f' that
27448 takes one parameter (of type `int') and returns a value of type
27451 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27452 constructor_p
= false;
27455 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
27456 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
27457 /* A parameter declaration begins with a decl-specifier,
27458 which is either the "attribute" keyword, a storage class
27459 specifier, or (usually) a type-specifier. */
27460 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
27463 tree pushed_scope
= NULL_TREE
;
27464 unsigned saved_num_template_parameter_lists
;
27466 /* Names appearing in the type-specifier should be looked up
27467 in the scope of the class. */
27468 if (current_class_type
)
27470 else if (type_decl
)
27472 type
= TREE_TYPE (type_decl
);
27473 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27475 type
= resolve_typename_type (type
,
27476 /*only_current_p=*/false);
27477 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27479 cp_parser_abort_tentative_parse (parser
);
27483 pushed_scope
= push_scope (type
);
27486 /* Inside the constructor parameter list, surrounding
27487 template-parameter-lists do not apply. */
27488 saved_num_template_parameter_lists
27489 = parser
->num_template_parameter_lists
;
27490 parser
->num_template_parameter_lists
= 0;
27492 /* Look for the type-specifier. */
27493 cp_parser_type_specifier (parser
,
27494 CP_PARSER_FLAGS_NONE
,
27495 /*decl_specs=*/NULL
,
27496 /*is_declarator=*/true,
27497 /*declares_class_or_enum=*/NULL
,
27498 /*is_cv_qualifier=*/NULL
);
27500 parser
->num_template_parameter_lists
27501 = saved_num_template_parameter_lists
;
27503 /* Leave the scope of the class. */
27505 pop_scope (pushed_scope
);
27507 constructor_p
= !cp_parser_error_occurred (parser
);
27511 /* We did not really want to consume any tokens. */
27512 cp_parser_abort_tentative_parse (parser
);
27514 return constructor_p
;
27517 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27518 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27519 they must be performed once we are in the scope of the function.
27521 Returns the function defined. */
27524 cp_parser_function_definition_from_specifiers_and_declarator
27525 (cp_parser
* parser
,
27526 cp_decl_specifier_seq
*decl_specifiers
,
27528 const cp_declarator
*declarator
)
27533 /* Begin the function-definition. */
27534 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
27536 /* The things we're about to see are not directly qualified by any
27537 template headers we've seen thus far. */
27538 reset_specialization ();
27540 /* If there were names looked up in the decl-specifier-seq that we
27541 did not check, check them now. We must wait until we are in the
27542 scope of the function to perform the checks, since the function
27543 might be a friend. */
27544 perform_deferred_access_checks (tf_warning_or_error
);
27548 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
27549 parser
->omp_declare_simd
= NULL
;
27550 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
27551 parser
->oacc_routine
= NULL
;
27556 /* Skip the entire function. */
27557 cp_parser_skip_to_end_of_block_or_statement (parser
);
27558 fn
= error_mark_node
;
27560 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
27562 /* Seen already, skip it. An error message has already been output. */
27563 cp_parser_skip_to_end_of_block_or_statement (parser
);
27564 fn
= current_function_decl
;
27565 current_function_decl
= NULL_TREE
;
27566 /* If this is a function from a class, pop the nested class. */
27567 if (current_class_name
)
27568 pop_nested_class ();
27573 if (DECL_DECLARED_INLINE_P (current_function_decl
))
27574 tv
= TV_PARSE_INLINE
;
27576 tv
= TV_PARSE_FUNC
;
27578 fn
= cp_parser_function_definition_after_declarator (parser
,
27579 /*inline_p=*/false);
27586 /* Parse the part of a function-definition that follows the
27587 declarator. INLINE_P is TRUE iff this function is an inline
27588 function defined within a class-specifier.
27590 Returns the function defined. */
27593 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
27597 bool saved_in_unbraced_linkage_specification_p
;
27598 bool saved_in_function_body
;
27599 unsigned saved_num_template_parameter_lists
;
27601 bool fully_implicit_function_template_p
27602 = parser
->fully_implicit_function_template_p
;
27603 parser
->fully_implicit_function_template_p
= false;
27604 tree implicit_template_parms
27605 = parser
->implicit_template_parms
;
27606 parser
->implicit_template_parms
= 0;
27607 cp_binding_level
* implicit_template_scope
27608 = parser
->implicit_template_scope
;
27609 parser
->implicit_template_scope
= 0;
27611 saved_in_function_body
= parser
->in_function_body
;
27612 parser
->in_function_body
= true;
27613 /* If the next token is `return', then the code may be trying to
27614 make use of the "named return value" extension that G++ used to
27616 token
= cp_lexer_peek_token (parser
->lexer
);
27617 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
27619 /* Consume the `return' keyword. */
27620 cp_lexer_consume_token (parser
->lexer
);
27621 /* Look for the identifier that indicates what value is to be
27623 cp_parser_identifier (parser
);
27624 /* Issue an error message. */
27625 error_at (token
->location
,
27626 "named return values are no longer supported");
27627 /* Skip tokens until we reach the start of the function body. */
27630 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27631 if (token
->type
== CPP_OPEN_BRACE
27632 || token
->type
== CPP_EOF
27633 || token
->type
== CPP_PRAGMA_EOL
)
27635 cp_lexer_consume_token (parser
->lexer
);
27638 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27639 anything declared inside `f'. */
27640 saved_in_unbraced_linkage_specification_p
27641 = parser
->in_unbraced_linkage_specification_p
;
27642 parser
->in_unbraced_linkage_specification_p
= false;
27643 /* Inside the function, surrounding template-parameter-lists do not
27645 saved_num_template_parameter_lists
27646 = parser
->num_template_parameter_lists
;
27647 parser
->num_template_parameter_lists
= 0;
27649 /* If the next token is `try', `__transaction_atomic', or
27650 `__transaction_relaxed`, then we are looking at either function-try-block
27651 or function-transaction-block. Note that all of these include the
27653 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
27654 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
27655 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27656 RID_TRANSACTION_RELAXED
))
27657 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
27658 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27659 cp_parser_function_try_block (parser
);
27661 cp_parser_ctor_initializer_opt_and_function_body
27662 (parser
, /*in_function_try_block=*/false);
27664 /* Finish the function. */
27665 fn
= finish_function (inline_p
);
27666 /* Generate code for it, if necessary. */
27667 expand_or_defer_fn (fn
);
27668 /* Restore the saved values. */
27669 parser
->in_unbraced_linkage_specification_p
27670 = saved_in_unbraced_linkage_specification_p
;
27671 parser
->num_template_parameter_lists
27672 = saved_num_template_parameter_lists
;
27673 parser
->in_function_body
= saved_in_function_body
;
27675 parser
->fully_implicit_function_template_p
27676 = fully_implicit_function_template_p
;
27677 parser
->implicit_template_parms
27678 = implicit_template_parms
;
27679 parser
->implicit_template_scope
27680 = implicit_template_scope
;
27682 if (parser
->fully_implicit_function_template_p
)
27683 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27688 /* Parse a template-declaration body (following argument list). */
27691 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27692 tree parameter_list
,
27695 tree decl
= NULL_TREE
;
27696 bool friend_p
= false;
27698 /* We just processed one more parameter list. */
27699 ++parser
->num_template_parameter_lists
;
27701 /* Get the deferred access checks from the parameter list. These
27702 will be checked once we know what is being declared, as for a
27703 member template the checks must be performed in the scope of the
27704 class containing the member. */
27705 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27707 /* Tentatively parse for a new template parameter list, which can either be
27708 the template keyword or a template introduction. */
27709 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27711 else if (cxx_dialect
>= cxx11
27712 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27713 decl
= cp_parser_alias_declaration (parser
);
27716 /* There are no access checks when parsing a template, as we do not
27717 know if a specialization will be a friend. */
27718 push_deferring_access_checks (dk_no_check
);
27719 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27720 decl
= cp_parser_single_declaration (parser
,
27723 /*explicit_specialization_p=*/false,
27725 pop_deferring_access_checks ();
27727 /* If this is a member template declaration, let the front
27729 if (member_p
&& !friend_p
&& decl
)
27731 if (TREE_CODE (decl
) == TYPE_DECL
)
27732 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27734 decl
= finish_member_template_decl (decl
);
27736 else if (friend_p
&& decl
27737 && DECL_DECLARES_TYPE_P (decl
))
27738 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27739 /*complain=*/true);
27741 /* We are done with the current parameter list. */
27742 --parser
->num_template_parameter_lists
;
27744 pop_deferring_access_checks ();
27747 finish_template_decl (parameter_list
);
27749 /* Check the template arguments for a literal operator template. */
27751 && DECL_DECLARES_FUNCTION_P (decl
)
27752 && UDLIT_OPER_P (DECL_NAME (decl
)))
27755 if (parameter_list
== NULL_TREE
)
27759 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27760 if (num_parms
== 1)
27762 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27763 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27764 if (CLASS_TYPE_P (TREE_TYPE (parm
)))
27765 /* OK, C++20 string literal operator template. We don't need
27766 to warn in lower dialects here because we will have already
27767 warned about the template parameter. */;
27768 else if (TREE_TYPE (parm
) != char_type_node
27769 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27772 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27774 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27775 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27776 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27777 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27778 if (parm
== error_mark_node
27779 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27780 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27783 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27784 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
27785 "ISO C++ did not adopt string literal operator templa"
27786 "tes taking an argument pack of characters");
27793 if (cxx_dialect
> cxx17
)
27794 error ("literal operator template %qD has invalid parameter list;"
27795 " Expected non-type template parameter pack <char...> "
27796 " or single non-type parameter of class type",
27799 error ("literal operator template %qD has invalid parameter list."
27800 " Expected non-type template parameter pack <char...>",
27805 /* Register member declarations. */
27806 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27807 finish_member_declaration (decl
);
27808 /* If DECL is a function template, we must return to parse it later.
27809 (Even though there is no definition, there might be default
27810 arguments that need handling.) */
27811 if (member_p
&& decl
27812 && DECL_DECLARES_FUNCTION_P (decl
))
27813 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27816 /* Parse a template introduction header for a template-declaration. Returns
27817 false if tentative parse fails. */
27820 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27822 cp_parser_parse_tentatively (parser
);
27824 tree saved_scope
= parser
->scope
;
27825 tree saved_object_scope
= parser
->object_scope
;
27826 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27828 /* Look for the optional `::' operator. */
27829 cp_parser_global_scope_opt (parser
,
27830 /*current_scope_valid_p=*/false);
27831 /* Look for the nested-name-specifier. */
27832 cp_parser_nested_name_specifier_opt (parser
,
27833 /*typename_keyword_p=*/false,
27834 /*check_dependency_p=*/true,
27836 /*is_declaration=*/false);
27838 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27839 tree concept_name
= cp_parser_identifier (parser
);
27841 /* Look up the concept for which we will be matching
27842 template parameters. */
27843 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27845 parser
->scope
= saved_scope
;
27846 parser
->object_scope
= saved_object_scope
;
27847 parser
->qualifying_scope
= saved_qualifying_scope
;
27849 if (concept_name
== error_mark_node
)
27850 cp_parser_simulate_error (parser
);
27852 /* Look for opening brace for introduction. */
27853 matching_braces braces
;
27854 braces
.require_open (parser
);
27856 if (!cp_parser_parse_definitely (parser
))
27859 push_deferring_access_checks (dk_deferred
);
27861 /* Build vector of placeholder parameters and grab
27862 matching identifiers. */
27863 tree introduction_list
= cp_parser_introduction_list (parser
);
27865 /* Look for closing brace for introduction. */
27866 if (!braces
.require_close (parser
))
27869 /* The introduction-list shall not be empty. */
27870 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27873 /* In cp_parser_introduction_list we have already issued an error. */
27877 if (tmpl_decl
== error_mark_node
)
27879 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27884 /* Build and associate the constraint. */
27885 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27886 if (parms
&& parms
!= error_mark_node
)
27888 cp_parser_template_declaration_after_parameters (parser
, parms
,
27893 error_at (token
->location
, "no matching concept for template-introduction");
27897 /* Parse a normal template-declaration following the template keyword. */
27900 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27902 tree parameter_list
;
27903 bool need_lang_pop
;
27904 location_t location
= input_location
;
27906 /* Look for the `<' token. */
27907 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27909 if (at_class_scope_p () && current_function_decl
)
27911 /* 14.5.2.2 [temp.mem]
27913 A local class shall not have member templates. */
27914 error_at (location
,
27915 "invalid declaration of member template in local class");
27916 cp_parser_skip_to_end_of_block_or_statement (parser
);
27921 A template ... shall not have C linkage. */
27922 if (current_lang_name
== lang_name_c
)
27924 error_at (location
, "template with C linkage");
27925 maybe_show_extern_c_location ();
27926 /* Give it C++ linkage to avoid confusing other parts of the
27928 push_lang_context (lang_name_cplusplus
);
27929 need_lang_pop
= true;
27932 need_lang_pop
= false;
27934 /* We cannot perform access checks on the template parameter
27935 declarations until we know what is being declared, just as we
27936 cannot check the decl-specifier list. */
27937 push_deferring_access_checks (dk_deferred
);
27939 /* If the next token is `>', then we have an invalid
27940 specialization. Rather than complain about an invalid template
27941 parameter, issue an error message here. */
27942 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27944 cp_parser_error (parser
, "invalid explicit specialization");
27945 begin_specialization ();
27946 parameter_list
= NULL_TREE
;
27950 /* Parse the template parameters. */
27951 parameter_list
= cp_parser_template_parameter_list (parser
);
27954 /* Look for the `>'. */
27955 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27957 /* Manage template requirements */
27960 tree reqs
= get_shorthand_constraints (current_template_parms
);
27961 if (tree r
= cp_parser_requires_clause_opt (parser
))
27962 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27963 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27966 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27969 /* For the erroneous case of a template with C linkage, we pushed an
27970 implicit C++ linkage scope; exit that scope now. */
27972 pop_lang_context ();
27975 /* Parse a template-declaration, assuming that the `export' (and
27976 `extern') keywords, if present, has already been scanned. MEMBER_P
27977 is as for cp_parser_template_declaration. */
27980 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27982 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27984 cp_lexer_consume_token (parser
->lexer
);
27985 cp_parser_explicit_template_declaration (parser
, member_p
);
27988 else if (flag_concepts
)
27989 return cp_parser_template_introduction (parser
, member_p
);
27994 /* Perform the deferred access checks from a template-parameter-list.
27995 CHECKS is a TREE_LIST of access checks, as returned by
27996 get_deferred_access_checks. */
27999 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
28001 ++processing_template_parmlist
;
28002 perform_access_checks (checks
, tf_warning_or_error
);
28003 --processing_template_parmlist
;
28006 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28007 `function-definition' sequence that follows a template header.
28008 If MEMBER_P is true, this declaration appears in a class scope.
28010 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28011 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28014 cp_parser_single_declaration (cp_parser
* parser
,
28015 vec
<deferred_access_check
, va_gc
> *checks
,
28017 bool explicit_specialization_p
,
28020 int declares_class_or_enum
;
28021 tree decl
= NULL_TREE
;
28022 cp_decl_specifier_seq decl_specifiers
;
28023 bool function_definition_p
= false;
28024 cp_token
*decl_spec_token_start
;
28026 /* This function is only used when processing a template
28028 gcc_assert (innermost_scope_kind () == sk_template_parms
28029 || innermost_scope_kind () == sk_template_spec
);
28031 /* Defer access checks until we know what is being declared. */
28032 push_deferring_access_checks (dk_deferred
);
28034 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28036 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
28037 cp_parser_decl_specifier_seq (parser
,
28038 (CP_PARSER_FLAGS_OPTIONAL
28039 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
28041 &declares_class_or_enum
);
28043 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
28045 /* There are no template typedefs. */
28046 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
28048 error_at (decl_spec_token_start
->location
,
28049 "template declaration of %<typedef%>");
28050 decl
= error_mark_node
;
28053 /* Gather up the access checks that occurred the
28054 decl-specifier-seq. */
28055 stop_deferring_access_checks ();
28057 /* Check for the declaration of a template class. */
28058 if (declares_class_or_enum
)
28060 if (cp_parser_declares_only_class_p (parser
)
28061 || (declares_class_or_enum
& 2))
28063 // If this is a declaration, but not a definition, associate
28064 // any constraints with the type declaration. Constraints
28065 // are associated with definitions in cp_parser_class_specifier.
28066 if (declares_class_or_enum
== 1)
28067 associate_classtype_constraints (decl_specifiers
.type
);
28069 decl
= shadow_tag (&decl_specifiers
);
28074 friend template <typename T> struct A<T>::B;
28077 A<T>::B will be represented by a TYPENAME_TYPE, and
28078 therefore not recognized by shadow_tag. */
28079 if (friend_p
&& *friend_p
28081 && decl_specifiers
.type
28082 && TYPE_P (decl_specifiers
.type
))
28083 decl
= decl_specifiers
.type
;
28085 if (decl
&& decl
!= error_mark_node
)
28086 decl
= TYPE_NAME (decl
);
28088 decl
= error_mark_node
;
28090 /* Perform access checks for template parameters. */
28091 cp_parser_perform_template_parameter_access_checks (checks
);
28093 /* Give a helpful diagnostic for
28094 template <class T> struct A { } a;
28095 if we aren't already recovering from an error. */
28096 if (!cp_parser_declares_only_class_p (parser
)
28099 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
28100 "a class template declaration must not declare "
28102 cp_parser_skip_to_end_of_block_or_statement (parser
);
28108 /* Complain about missing 'typename' or other invalid type names. */
28109 if (!decl_specifiers
.any_type_specifiers_p
28110 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
28112 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28113 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28114 the rest of this declaration. */
28115 decl
= error_mark_node
;
28119 /* If it's not a template class, try for a template function. If
28120 the next token is a `;', then this declaration does not declare
28121 anything. But, if there were errors in the decl-specifiers, then
28122 the error might well have come from an attempted class-specifier.
28123 In that case, there's no need to warn about a missing declarator. */
28125 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
28126 || decl_specifiers
.type
!= error_mark_node
))
28128 decl
= cp_parser_init_declarator (parser
,
28129 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
28132 /*function_definition_allowed_p=*/true,
28134 declares_class_or_enum
,
28135 &function_definition_p
,
28138 /* 7.1.1-1 [dcl.stc]
28140 A storage-class-specifier shall not be specified in an explicit
28141 specialization... */
28143 && explicit_specialization_p
28144 && decl_specifiers
.storage_class
!= sc_none
)
28146 error_at (decl_spec_token_start
->location
,
28147 "explicit template specialization cannot have a storage class");
28148 decl
= error_mark_node
;
28151 if (decl
&& VAR_P (decl
))
28152 check_template_variable (decl
);
28155 /* Look for a trailing `;' after the declaration. */
28156 if (!function_definition_p
28157 && (decl
== error_mark_node
28158 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
28159 cp_parser_skip_to_end_of_block_or_statement (parser
);
28162 pop_deferring_access_checks ();
28164 /* Clear any current qualification; whatever comes next is the start
28165 of something new. */
28166 parser
->scope
= NULL_TREE
;
28167 parser
->qualifying_scope
= NULL_TREE
;
28168 parser
->object_scope
= NULL_TREE
;
28173 /* Parse a cast-expression that is not the operand of a unary "&". */
28176 cp_parser_simple_cast_expression (cp_parser
*parser
)
28178 return cp_parser_cast_expression (parser
, /*address_p=*/false,
28179 /*cast_p=*/false, /*decltype*/false, NULL
);
28182 /* Parse a functional cast to TYPE. Returns an expression
28183 representing the cast. */
28186 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
28188 vec
<tree
, va_gc
> *vec
;
28189 tree expression_list
;
28193 location_t start_loc
= input_location
;
28196 type
= error_mark_node
;
28198 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28200 cp_lexer_set_source_position (parser
->lexer
);
28201 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28202 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
28203 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
28204 if (TREE_CODE (type
) == TYPE_DECL
)
28205 type
= TREE_TYPE (type
);
28207 cast
= finish_compound_literal (type
, expression_list
,
28208 tf_warning_or_error
, fcl_functional
);
28209 /* Create a location of the form:
28212 with caret == start at the start of the type name,
28213 finishing at the closing brace. */
28214 location_t finish_loc
28215 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28216 location_t combined_loc
= make_location (start_loc
, start_loc
,
28218 cast
.set_location (combined_loc
);
28223 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
28225 /*allow_expansion_p=*/true,
28226 /*non_constant_p=*/NULL
);
28228 expression_list
= error_mark_node
;
28231 expression_list
= build_tree_list_vec (vec
);
28232 release_tree_vector (vec
);
28235 cast
= build_functional_cast (type
, expression_list
,
28236 tf_warning_or_error
);
28237 /* [expr.const]/1: In an integral constant expression "only type
28238 conversions to integral or enumeration type can be used". */
28239 if (TREE_CODE (type
) == TYPE_DECL
)
28240 type
= TREE_TYPE (type
);
28241 if (cast
!= error_mark_node
28242 && !cast_valid_in_integral_constant_expression_p (type
)
28243 && cp_parser_non_integral_constant_expression (parser
,
28245 return error_mark_node
;
28247 /* Create a location of the form:
28250 with caret == start at the start of the type name,
28251 finishing at the closing paren. */
28252 location_t finish_loc
28253 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28254 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
28255 cast
.set_location (combined_loc
);
28259 /* Save the tokens that make up the body of a member function defined
28260 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28261 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28262 specifiers applied to the declaration. Returns the FUNCTION_DECL
28263 for the member function. */
28266 cp_parser_save_member_function_body (cp_parser
* parser
,
28267 cp_decl_specifier_seq
*decl_specifiers
,
28268 cp_declarator
*declarator
,
28274 bool function_try_block
= false;
28276 /* Create the FUNCTION_DECL. */
28277 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
28278 cp_finalize_omp_declare_simd (parser
, fn
);
28279 cp_finalize_oacc_routine (parser
, fn
, true);
28280 /* If something went badly wrong, bail out now. */
28281 if (fn
== error_mark_node
)
28283 /* If there's a function-body, skip it. */
28284 if (cp_parser_token_starts_function_definition_p
28285 (cp_lexer_peek_token (parser
->lexer
)))
28286 cp_parser_skip_to_end_of_block_or_statement (parser
);
28287 return error_mark_node
;
28290 /* Remember it, if there default args to post process. */
28291 cp_parser_save_default_args (parser
, fn
);
28293 /* Save away the tokens that make up the body of the
28295 first
= parser
->lexer
->next_token
;
28297 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
28298 cp_lexer_consume_token (parser
->lexer
);
28299 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
28300 RID_TRANSACTION_ATOMIC
))
28302 cp_lexer_consume_token (parser
->lexer
);
28303 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
28305 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
28306 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
28307 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
28308 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
28309 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
28311 cp_lexer_consume_token (parser
->lexer
);
28312 cp_lexer_consume_token (parser
->lexer
);
28313 cp_lexer_consume_token (parser
->lexer
);
28314 cp_lexer_consume_token (parser
->lexer
);
28315 cp_lexer_consume_token (parser
->lexer
);
28318 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
28319 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
28321 cp_lexer_consume_token (parser
->lexer
);
28322 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28327 /* Handle function try blocks. */
28328 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
28330 cp_lexer_consume_token (parser
->lexer
);
28331 function_try_block
= true;
28333 /* We can have braced-init-list mem-initializers before the fn body. */
28334 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
28336 cp_lexer_consume_token (parser
->lexer
);
28337 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
28339 /* cache_group will stop after an un-nested { } pair, too. */
28340 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28343 /* variadic mem-inits have ... after the ')'. */
28344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28345 cp_lexer_consume_token (parser
->lexer
);
28348 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28349 /* Handle function try blocks. */
28350 if (function_try_block
)
28351 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
28352 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28353 last
= parser
->lexer
->next_token
;
28355 /* Save away the inline definition; we will process it when the
28356 class is complete. */
28357 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
28358 DECL_PENDING_INLINE_P (fn
) = 1;
28360 /* We need to know that this was defined in the class, so that
28361 friend templates are handled correctly. */
28362 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
28364 /* Add FN to the queue of functions to be parsed later. */
28365 vec_safe_push (unparsed_funs_with_definitions
, fn
);
28370 /* Save the tokens that make up the in-class initializer for a non-static
28371 data member. Returns a DEFAULT_ARG. */
28374 cp_parser_save_nsdmi (cp_parser
* parser
)
28376 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
28379 /* Parse a template-argument-list, as well as the trailing ">" (but
28380 not the opening "<"). See cp_parser_template_argument_list for the
28384 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
28388 tree saved_qualifying_scope
;
28389 tree saved_object_scope
;
28390 bool saved_greater_than_is_operator_p
;
28394 When parsing a template-id, the first non-nested `>' is taken as
28395 the end of the template-argument-list rather than a greater-than
28397 saved_greater_than_is_operator_p
28398 = parser
->greater_than_is_operator_p
;
28399 parser
->greater_than_is_operator_p
= false;
28400 /* Parsing the argument list may modify SCOPE, so we save it
28402 saved_scope
= parser
->scope
;
28403 saved_qualifying_scope
= parser
->qualifying_scope
;
28404 saved_object_scope
= parser
->object_scope
;
28405 /* We need to evaluate the template arguments, even though this
28406 template-id may be nested within a "sizeof". */
28408 /* Parse the template-argument-list itself. */
28409 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
28410 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28411 arguments
= NULL_TREE
;
28413 arguments
= cp_parser_template_argument_list (parser
);
28414 /* Look for the `>' that ends the template-argument-list. If we find
28415 a '>>' instead, it's probably just a typo. */
28416 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28418 if (cxx_dialect
!= cxx98
)
28420 /* In C++0x, a `>>' in a template argument list or cast
28421 expression is considered to be two separate `>'
28422 tokens. So, change the current token to a `>', but don't
28423 consume it: it will be consumed later when the outer
28424 template argument list (or cast expression) is parsed.
28425 Note that this replacement of `>' for `>>' is necessary
28426 even if we are parsing tentatively: in the tentative
28427 case, after calling
28428 cp_parser_enclosed_template_argument_list we will always
28429 throw away all of the template arguments and the first
28430 closing `>', either because the template argument list
28431 was erroneous or because we are replacing those tokens
28432 with a CPP_TEMPLATE_ID token. The second `>' (which will
28433 not have been thrown away) is needed either to close an
28434 outer template argument list or to complete a new-style
28436 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28437 token
->type
= CPP_GREATER
;
28439 else if (!saved_greater_than_is_operator_p
)
28441 /* If we're in a nested template argument list, the '>>' has
28442 to be a typo for '> >'. We emit the error message, but we
28443 continue parsing and we push a '>' as next token, so that
28444 the argument list will be parsed correctly. Note that the
28445 global source location is still on the token before the
28446 '>>', so we need to say explicitly where we want it. */
28447 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28448 gcc_rich_location
richloc (token
->location
);
28449 richloc
.add_fixit_replace ("> >");
28450 error_at (&richloc
, "%<>>%> should be %<> >%> "
28451 "within a nested template argument list");
28453 token
->type
= CPP_GREATER
;
28457 /* If this is not a nested template argument list, the '>>'
28458 is a typo for '>'. Emit an error message and continue.
28459 Same deal about the token location, but here we can get it
28460 right by consuming the '>>' before issuing the diagnostic. */
28461 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
28462 error_at (token
->location
,
28463 "spurious %<>>%>, use %<>%> to terminate "
28464 "a template argument list");
28468 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28469 /* The `>' token might be a greater-than operator again now. */
28470 parser
->greater_than_is_operator_p
28471 = saved_greater_than_is_operator_p
;
28472 /* Restore the SAVED_SCOPE. */
28473 parser
->scope
= saved_scope
;
28474 parser
->qualifying_scope
= saved_qualifying_scope
;
28475 parser
->object_scope
= saved_object_scope
;
28480 /* MEMBER_FUNCTION is a member function, or a friend. If default
28481 arguments, or the body of the function have not yet been parsed,
28485 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
28487 timevar_push (TV_PARSE_INMETH
);
28488 /* If this member is a template, get the underlying
28490 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
28491 member_function
= DECL_TEMPLATE_RESULT (member_function
);
28493 /* There should not be any class definitions in progress at this
28494 point; the bodies of members are only parsed outside of all class
28496 gcc_assert (parser
->num_classes_being_defined
== 0);
28497 /* While we're parsing the member functions we might encounter more
28498 classes. We want to handle them right away, but we don't want
28499 them getting mixed up with functions that are currently in the
28501 push_unparsed_function_queues (parser
);
28503 /* Make sure that any template parameters are in scope. */
28504 maybe_begin_member_template_processing (member_function
);
28506 /* If the body of the function has not yet been parsed, parse it
28508 if (DECL_PENDING_INLINE_P (member_function
))
28510 tree function_scope
;
28511 cp_token_cache
*tokens
;
28513 /* The function is no longer pending; we are processing it. */
28514 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
28515 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
28516 DECL_PENDING_INLINE_P (member_function
) = 0;
28518 /* If this is a local class, enter the scope of the containing
28520 function_scope
= current_function_decl
;
28521 if (function_scope
)
28522 push_function_context ();
28524 /* Push the body of the function onto the lexer stack. */
28525 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28527 /* Let the front end know that we going to be defining this
28529 start_preparsed_function (member_function
, NULL_TREE
,
28530 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
28532 /* Don't do access checking if it is a templated function. */
28533 if (processing_template_decl
)
28534 push_deferring_access_checks (dk_no_check
);
28536 /* #pragma omp declare reduction needs special parsing. */
28537 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
28539 parser
->lexer
->in_pragma
= true;
28540 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
28541 finish_function (/*inline_p=*/true);
28542 cp_check_omp_declare_reduction (member_function
);
28545 /* Now, parse the body of the function. */
28546 cp_parser_function_definition_after_declarator (parser
,
28547 /*inline_p=*/true);
28549 if (processing_template_decl
)
28550 pop_deferring_access_checks ();
28552 /* Leave the scope of the containing function. */
28553 if (function_scope
)
28554 pop_function_context ();
28555 cp_parser_pop_lexer (parser
);
28558 /* Remove any template parameters from the symbol table. */
28559 maybe_end_member_template_processing ();
28561 /* Restore the queue. */
28562 pop_unparsed_function_queues (parser
);
28563 timevar_pop (TV_PARSE_INMETH
);
28566 /* If DECL contains any default args, remember it on the unparsed
28567 functions queue. */
28570 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
28574 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
28576 probe
= TREE_CHAIN (probe
))
28577 if (TREE_PURPOSE (probe
))
28579 cp_default_arg_entry entry
= {current_class_type
, decl
};
28580 vec_safe_push (unparsed_funs_with_default_args
, entry
);
28585 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28586 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28587 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28588 from the parameter-type-list. */
28591 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
28592 tree default_arg
, tree parmtype
)
28594 cp_token_cache
*tokens
;
28598 if (default_arg
== error_mark_node
)
28599 return error_mark_node
;
28601 /* Push the saved tokens for the default argument onto the parser's
28603 tokens
= DEFARG_TOKENS (default_arg
);
28604 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28606 start_lambda_scope (decl
);
28608 /* Parse the default argument. */
28609 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
28610 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
28611 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28613 finish_lambda_scope ();
28615 if (parsed_arg
== error_mark_node
)
28616 cp_parser_skip_to_end_of_statement (parser
);
28618 if (!processing_template_decl
)
28620 /* In a non-template class, check conversions now. In a template,
28621 we'll wait and instantiate these as needed. */
28622 if (TREE_CODE (decl
) == PARM_DECL
)
28623 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
28624 tf_warning_or_error
);
28625 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
28626 parsed_arg
= error_mark_node
;
28628 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
28631 /* If the token stream has not been completely used up, then
28632 there was extra junk after the end of the default
28634 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
28636 if (TREE_CODE (decl
) == PARM_DECL
)
28637 cp_parser_error (parser
, "expected %<,%>");
28639 cp_parser_error (parser
, "expected %<;%>");
28642 /* Revert to the main lexer. */
28643 cp_parser_pop_lexer (parser
);
28648 /* FIELD is a non-static data member with an initializer which we saved for
28649 later; parse it now. */
28652 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
28656 maybe_begin_member_template_processing (field
);
28658 push_unparsed_function_queues (parser
);
28659 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
28660 DECL_INITIAL (field
),
28662 pop_unparsed_function_queues (parser
);
28664 maybe_end_member_template_processing ();
28666 DECL_INITIAL (field
) = def
;
28669 /* FN is a FUNCTION_DECL which may contains a parameter with an
28670 unparsed DEFAULT_ARG. Parse the default args now. This function
28671 assumes that the current scope is the scope in which the default
28672 argument should be processed. */
28675 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
28677 unsigned char saved_local_variables_forbidden_p
;
28678 tree parm
, parmdecl
;
28680 /* While we're parsing the default args, we might (due to the
28681 statement expression extension) encounter more classes. We want
28682 to handle them right away, but we don't want them getting mixed
28683 up with default args that are currently in the queue. */
28684 push_unparsed_function_queues (parser
);
28686 /* Local variable names (and the `this' keyword) may not appear
28687 in a default argument. */
28688 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28689 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
28691 push_defarg_context (fn
);
28693 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28694 parmdecl
= DECL_ARGUMENTS (fn
);
28695 parm
&& parm
!= void_list_node
;
28696 parm
= TREE_CHAIN (parm
),
28697 parmdecl
= DECL_CHAIN (parmdecl
))
28699 tree default_arg
= TREE_PURPOSE (parm
);
28701 vec
<tree
, va_gc
> *insts
;
28708 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28709 /* This can happen for a friend declaration for a function
28710 already declared with default arguments. */
28714 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28716 TREE_VALUE (parm
));
28717 TREE_PURPOSE (parm
) = parsed_arg
;
28719 /* Update any instantiations we've already created. */
28720 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28721 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28722 TREE_PURPOSE (copy
) = parsed_arg
;
28725 pop_defarg_context ();
28727 /* Make sure no default arg is missing. */
28728 check_default_args (fn
);
28730 /* Restore the state of local_variables_forbidden_p. */
28731 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28733 /* Restore the queue. */
28734 pop_unparsed_function_queues (parser
);
28737 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28739 sizeof ... ( identifier )
28741 where the 'sizeof' token has already been consumed. */
28744 cp_parser_sizeof_pack (cp_parser
*parser
)
28746 /* Consume the `...'. */
28747 cp_lexer_consume_token (parser
->lexer
);
28748 maybe_warn_variadic_templates ();
28750 matching_parens parens
;
28751 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28753 parens
.consume_open (parser
);
28755 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28756 "%<sizeof...%> argument must be surrounded by parentheses");
28758 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28759 tree name
= cp_parser_identifier (parser
);
28760 if (name
== error_mark_node
)
28761 return error_mark_node
;
28762 /* The name is not qualified. */
28763 parser
->scope
= NULL_TREE
;
28764 parser
->qualifying_scope
= NULL_TREE
;
28765 parser
->object_scope
= NULL_TREE
;
28766 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28767 if (expr
== error_mark_node
)
28768 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28770 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28771 expr
= TREE_TYPE (expr
);
28772 else if (TREE_CODE (expr
) == CONST_DECL
)
28773 expr
= DECL_INITIAL (expr
);
28774 expr
= make_pack_expansion (expr
);
28775 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28778 parens
.require_close (parser
);
28783 /* Parse the operand of `sizeof' (or a similar operator). Returns
28784 either a TYPE or an expression, depending on the form of the
28785 input. The KEYWORD indicates which kind of expression we have
28789 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28791 tree expr
= NULL_TREE
;
28792 const char *saved_message
;
28794 bool saved_integral_constant_expression_p
;
28795 bool saved_non_integral_constant_expression_p
;
28797 /* If it's a `...', then we are computing the length of a parameter
28799 if (keyword
== RID_SIZEOF
28800 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28801 return cp_parser_sizeof_pack (parser
);
28803 /* Types cannot be defined in a `sizeof' expression. Save away the
28805 saved_message
= parser
->type_definition_forbidden_message
;
28806 /* And create the new one. */
28807 tmp
= concat ("types may not be defined in %<",
28808 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28809 "%> expressions", NULL
);
28810 parser
->type_definition_forbidden_message
= tmp
;
28812 /* The restrictions on constant-expressions do not apply inside
28813 sizeof expressions. */
28814 saved_integral_constant_expression_p
28815 = parser
->integral_constant_expression_p
;
28816 saved_non_integral_constant_expression_p
28817 = parser
->non_integral_constant_expression_p
;
28818 parser
->integral_constant_expression_p
= false;
28820 /* Do not actually evaluate the expression. */
28821 ++cp_unevaluated_operand
;
28822 ++c_inhibit_evaluation_warnings
;
28823 /* If it's a `(', then we might be looking at the type-id
28825 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28827 tree type
= NULL_TREE
;
28829 /* We can't be sure yet whether we're looking at a type-id or an
28831 cp_parser_parse_tentatively (parser
);
28833 matching_parens parens
;
28834 parens
.consume_open (parser
);
28836 /* Note: as a GNU Extension, compound literals are considered
28837 postfix-expressions as they are in C99, so they are valid
28838 arguments to sizeof. See comment in cp_parser_cast_expression
28840 if (cp_parser_compound_literal_p (parser
))
28841 cp_parser_simulate_error (parser
);
28844 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28845 parser
->in_type_id_in_expr_p
= true;
28846 /* Look for the type-id. */
28847 type
= cp_parser_type_id (parser
);
28848 /* Look for the closing `)'. */
28849 parens
.require_close (parser
);
28850 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28853 /* If all went well, then we're done. */
28854 if (cp_parser_parse_definitely (parser
))
28858 /* If the type-id production did not work out, then we must be
28859 looking at the unary-expression production. */
28861 expr
= cp_parser_unary_expression (parser
);
28863 /* Go back to evaluating expressions. */
28864 --cp_unevaluated_operand
;
28865 --c_inhibit_evaluation_warnings
;
28867 /* Free the message we created. */
28869 /* And restore the old one. */
28870 parser
->type_definition_forbidden_message
= saved_message
;
28871 parser
->integral_constant_expression_p
28872 = saved_integral_constant_expression_p
;
28873 parser
->non_integral_constant_expression_p
28874 = saved_non_integral_constant_expression_p
;
28879 /* If the current declaration has no declarator, return true. */
28882 cp_parser_declares_only_class_p (cp_parser
*parser
)
28884 /* If the next token is a `;' or a `,' then there is no
28886 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28887 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28890 /* Update the DECL_SPECS to reflect the storage class indicated by
28894 cp_parser_set_storage_class (cp_parser
*parser
,
28895 cp_decl_specifier_seq
*decl_specs
,
28899 cp_storage_class storage_class
;
28901 if (parser
->in_unbraced_linkage_specification_p
)
28903 error_at (token
->location
, "invalid use of %qD in linkage specification",
28904 ridpointers
[keyword
]);
28907 else if (decl_specs
->storage_class
!= sc_none
)
28909 decl_specs
->conflicting_specifiers_p
= true;
28913 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28914 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28915 && decl_specs
->gnu_thread_keyword_p
)
28917 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28918 "%<__thread%> before %qD", ridpointers
[keyword
]);
28924 storage_class
= sc_auto
;
28927 storage_class
= sc_register
;
28930 storage_class
= sc_static
;
28933 storage_class
= sc_extern
;
28936 storage_class
= sc_mutable
;
28939 gcc_unreachable ();
28941 decl_specs
->storage_class
= storage_class
;
28942 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28944 /* A storage class specifier cannot be applied alongside a typedef
28945 specifier. If there is a typedef specifier present then set
28946 conflicting_specifiers_p which will trigger an error later
28947 on in grokdeclarator. */
28948 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28949 decl_specs
->conflicting_specifiers_p
= true;
28952 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28953 is true, the type is a class or enum definition. */
28956 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28959 bool type_definition_p
)
28961 decl_specs
->any_specifiers_p
= true;
28963 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
28964 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
28965 this is what happened. In system headers, we ignore these
28966 declarations so that G++ can work with system headers that are not
28968 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28969 && !type_definition_p
28970 && (type_spec
== boolean_type_node
28971 || type_spec
== char8_type_node
28972 || type_spec
== char16_type_node
28973 || type_spec
== char32_type_node
28974 || type_spec
== wchar_type_node
)
28975 && (decl_specs
->type
28976 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28977 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28978 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28979 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28981 decl_specs
->redefined_builtin_type
= type_spec
;
28982 set_and_check_decl_spec_loc (decl_specs
,
28983 ds_redefined_builtin_type_spec
,
28985 if (!decl_specs
->type
)
28987 decl_specs
->type
= type_spec
;
28988 decl_specs
->type_definition_p
= false;
28989 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28992 else if (decl_specs
->type
)
28993 decl_specs
->multiple_types_p
= true;
28996 decl_specs
->type
= type_spec
;
28997 decl_specs
->type_definition_p
= type_definition_p
;
28998 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28999 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
29003 /* True iff TOKEN is the GNU keyword __thread. */
29006 token_is__thread (cp_token
*token
)
29008 gcc_assert (token
->keyword
== RID_THREAD
);
29009 return id_equal (token
->u
.value
, "__thread");
29012 /* Set the location for a declarator specifier and check if it is
29015 DECL_SPECS is the sequence of declarator specifiers onto which to
29018 DS is the single declarator specifier to set which location is to
29019 be set onto the existing sequence of declarators.
29021 LOCATION is the location for the declarator specifier to
29025 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
29026 cp_decl_spec ds
, cp_token
*token
)
29028 gcc_assert (ds
< ds_last
);
29030 if (decl_specs
== NULL
)
29033 location_t location
= token
->location
;
29035 if (decl_specs
->locations
[ds
] == 0)
29037 decl_specs
->locations
[ds
] = location
;
29038 if (ds
== ds_thread
)
29039 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
29045 if (decl_specs
->locations
[ds_long_long
] != 0)
29046 error_at (location
,
29047 "%<long long long%> is too long for GCC");
29050 decl_specs
->locations
[ds_long_long
] = location
;
29051 pedwarn_cxx98 (location
,
29053 "ISO C++ 1998 does not support %<long long%>");
29056 else if (ds
== ds_thread
)
29058 bool gnu
= token_is__thread (token
);
29059 gcc_rich_location
richloc (location
);
29060 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
29062 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
29063 error_at (&richloc
,
29064 "both %<__thread%> and %<thread_local%> specified");
29068 richloc
.add_fixit_remove ();
29069 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
29074 static const char *const decl_spec_names
[] = {
29091 gcc_rich_location
richloc (location
);
29092 richloc
.add_fixit_remove ();
29093 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
29098 /* Return true iff the declarator specifier DS is present in the
29099 sequence of declarator specifiers DECL_SPECS. */
29102 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
29105 gcc_assert (ds
< ds_last
);
29107 if (decl_specs
== NULL
)
29110 return decl_specs
->locations
[ds
] != 0;
29113 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29114 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29117 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
29119 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
29122 /* Issue an error message indicating that TOKEN_DESC was expected.
29123 If KEYWORD is true, it indicated this function is called by
29124 cp_parser_require_keword and the required token can only be
29125 a indicated keyword.
29127 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29128 within any error as the location of an "opening" token matching
29129 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29130 RT_CLOSE_PAREN). */
29133 cp_parser_required_error (cp_parser
*parser
,
29134 required_token token_desc
,
29136 location_t matching_location
)
29138 if (cp_parser_simulate_error (parser
))
29141 const char *gmsgid
= NULL
;
29142 switch (token_desc
)
29145 gmsgid
= G_("expected %<new%>");
29148 gmsgid
= G_("expected %<delete%>");
29151 gmsgid
= G_("expected %<return%>");
29154 gmsgid
= G_("expected %<while%>");
29157 gmsgid
= G_("expected %<extern%>");
29159 case RT_STATIC_ASSERT
:
29160 gmsgid
= G_("expected %<static_assert%>");
29163 gmsgid
= G_("expected %<decltype%>");
29166 gmsgid
= G_("expected %<operator%>");
29169 gmsgid
= G_("expected %<class%>");
29172 gmsgid
= G_("expected %<template%>");
29175 gmsgid
= G_("expected %<namespace%>");
29178 gmsgid
= G_("expected %<using%>");
29181 gmsgid
= G_("expected %<asm%>");
29184 gmsgid
= G_("expected %<try%>");
29187 gmsgid
= G_("expected %<catch%>");
29190 gmsgid
= G_("expected %<throw%>");
29193 gmsgid
= G_("expected %<__label__%>");
29196 gmsgid
= G_("expected %<@try%>");
29198 case RT_AT_SYNCHRONIZED
:
29199 gmsgid
= G_("expected %<@synchronized%>");
29202 gmsgid
= G_("expected %<@throw%>");
29204 case RT_TRANSACTION_ATOMIC
:
29205 gmsgid
= G_("expected %<__transaction_atomic%>");
29207 case RT_TRANSACTION_RELAXED
:
29208 gmsgid
= G_("expected %<__transaction_relaxed%>");
29214 if (!gmsgid
&& !keyword
)
29216 switch (token_desc
)
29219 gmsgid
= G_("expected %<;%>");
29221 case RT_OPEN_PAREN
:
29222 gmsgid
= G_("expected %<(%>");
29224 case RT_CLOSE_BRACE
:
29225 gmsgid
= G_("expected %<}%>");
29227 case RT_OPEN_BRACE
:
29228 gmsgid
= G_("expected %<{%>");
29230 case RT_CLOSE_SQUARE
:
29231 gmsgid
= G_("expected %<]%>");
29233 case RT_OPEN_SQUARE
:
29234 gmsgid
= G_("expected %<[%>");
29237 gmsgid
= G_("expected %<,%>");
29240 gmsgid
= G_("expected %<::%>");
29243 gmsgid
= G_("expected %<<%>");
29246 gmsgid
= G_("expected %<>%>");
29249 gmsgid
= G_("expected %<=%>");
29252 gmsgid
= G_("expected %<...%>");
29255 gmsgid
= G_("expected %<*%>");
29258 gmsgid
= G_("expected %<~%>");
29261 gmsgid
= G_("expected %<:%>");
29263 case RT_COLON_SCOPE
:
29264 gmsgid
= G_("expected %<:%> or %<::%>");
29266 case RT_CLOSE_PAREN
:
29267 gmsgid
= G_("expected %<)%>");
29269 case RT_COMMA_CLOSE_PAREN
:
29270 gmsgid
= G_("expected %<,%> or %<)%>");
29272 case RT_PRAGMA_EOL
:
29273 gmsgid
= G_("expected end of line");
29276 gmsgid
= G_("expected identifier");
29279 gmsgid
= G_("expected selection-statement");
29282 gmsgid
= G_("expected iteration-statement");
29285 gmsgid
= G_("expected jump-statement");
29288 gmsgid
= G_("expected class-key");
29290 case RT_CLASS_TYPENAME_TEMPLATE
:
29291 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
29294 gcc_unreachable ();
29299 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
29303 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29304 issue an error message indicating that TOKEN_DESC was expected.
29306 Returns the token consumed, if the token had the appropriate type.
29307 Otherwise, returns NULL.
29309 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29310 within any error as the location of an "opening" token matching
29311 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29312 RT_CLOSE_PAREN). */
29315 cp_parser_require (cp_parser
* parser
,
29316 enum cpp_ttype type
,
29317 required_token token_desc
,
29318 location_t matching_location
)
29320 if (cp_lexer_next_token_is (parser
->lexer
, type
))
29321 return cp_lexer_consume_token (parser
->lexer
);
29324 /* Output the MESSAGE -- unless we're parsing tentatively. */
29325 if (!cp_parser_simulate_error (parser
))
29326 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
29327 matching_location
);
29332 /* An error message is produced if the next token is not '>'.
29333 All further tokens are skipped until the desired token is
29334 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29337 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
29339 /* Current level of '< ... >'. */
29340 unsigned level
= 0;
29341 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29342 unsigned nesting_depth
= 0;
29344 /* Are we ready, yet? If not, issue error message. */
29345 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
29348 /* Skip tokens until the desired token is found. */
29351 /* Peek at the next token. */
29352 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29355 if (!nesting_depth
)
29360 if (cxx_dialect
== cxx98
)
29361 /* C++0x views the `>>' operator as two `>' tokens, but
29364 else if (!nesting_depth
&& level
-- == 0)
29366 /* We've hit a `>>' where the first `>' closes the
29367 template argument list, and the second `>' is
29368 spurious. Just consume the `>>' and stop; we've
29369 already produced at least one error. */
29370 cp_lexer_consume_token (parser
->lexer
);
29373 /* Fall through for C++0x, so we handle the second `>' in
29375 gcc_fallthrough ();
29378 if (!nesting_depth
&& level
-- == 0)
29380 /* We've reached the token we want, consume it and stop. */
29381 cp_lexer_consume_token (parser
->lexer
);
29386 case CPP_OPEN_PAREN
:
29387 case CPP_OPEN_SQUARE
:
29391 case CPP_CLOSE_PAREN
:
29392 case CPP_CLOSE_SQUARE
:
29393 if (nesting_depth
-- == 0)
29398 case CPP_PRAGMA_EOL
:
29399 case CPP_SEMICOLON
:
29400 case CPP_OPEN_BRACE
:
29401 case CPP_CLOSE_BRACE
:
29402 /* The '>' was probably forgotten, don't look further. */
29409 /* Consume this token. */
29410 cp_lexer_consume_token (parser
->lexer
);
29414 /* If the next token is the indicated keyword, consume it. Otherwise,
29415 issue an error message indicating that TOKEN_DESC was expected.
29417 Returns the token consumed, if the token had the appropriate type.
29418 Otherwise, returns NULL. */
29421 cp_parser_require_keyword (cp_parser
* parser
,
29423 required_token token_desc
)
29425 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
29427 if (token
&& token
->keyword
!= keyword
)
29429 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
29437 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29438 function-definition. */
29441 cp_parser_token_starts_function_definition_p (cp_token
* token
)
29443 return (/* An ordinary function-body begins with an `{'. */
29444 token
->type
== CPP_OPEN_BRACE
29445 /* A ctor-initializer begins with a `:'. */
29446 || token
->type
== CPP_COLON
29447 /* A function-try-block begins with `try'. */
29448 || token
->keyword
== RID_TRY
29449 /* A function-transaction-block begins with `__transaction_atomic'
29450 or `__transaction_relaxed'. */
29451 || token
->keyword
== RID_TRANSACTION_ATOMIC
29452 || token
->keyword
== RID_TRANSACTION_RELAXED
29453 /* The named return value extension begins with `return'. */
29454 || token
->keyword
== RID_RETURN
);
29457 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29461 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
29465 token
= cp_lexer_peek_token (parser
->lexer
);
29466 return (token
->type
== CPP_OPEN_BRACE
29467 || (token
->type
== CPP_COLON
29468 && !parser
->colon_doesnt_start_class_def_p
));
29471 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29472 C++0x) ending a template-argument. */
29475 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
29479 token
= cp_lexer_peek_token (parser
->lexer
);
29480 return (token
->type
== CPP_COMMA
29481 || token
->type
== CPP_GREATER
29482 || token
->type
== CPP_ELLIPSIS
29483 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
29486 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29487 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29490 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
29495 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
29496 if (token
->type
== CPP_LESS
)
29498 /* Check for the sequence `<::' in the original code. It would be lexed as
29499 `[:', where `[' is a digraph, and there is no whitespace before
29501 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
29504 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
29505 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
29511 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29512 or none_type otherwise. */
29514 static enum tag_types
29515 cp_parser_token_is_class_key (cp_token
* token
)
29517 switch (token
->keyword
)
29522 return record_type
;
29531 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29532 or none_type otherwise or if the token is null. */
29534 static enum tag_types
29535 cp_parser_token_is_type_parameter_key (cp_token
* token
)
29540 switch (token
->keyword
)
29545 return typename_type
;
29552 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29555 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
29557 if (type
== error_mark_node
)
29559 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
29561 if (permerror (input_location
, "%qs tag used in naming %q#T",
29562 class_key
== union_type
? "union"
29563 : class_key
== record_type
? "struct" : "class",
29565 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
29566 "%q#T was previously declared here", type
);
29570 /* Issue an error message if DECL is redeclared with different
29571 access than its original declaration [class.access.spec/3].
29572 This applies to nested classes, nested class templates and
29573 enumerations [class.mem/1]. */
29576 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
29579 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
29580 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
29583 if ((TREE_PRIVATE (decl
)
29584 != (current_access_specifier
== access_private_node
))
29585 || (TREE_PROTECTED (decl
)
29586 != (current_access_specifier
== access_protected_node
)))
29587 error_at (location
, "%qD redeclared with different access", decl
);
29590 /* Look for the `template' keyword, as a syntactic disambiguator.
29591 Return TRUE iff it is present, in which case it will be
29595 cp_parser_optional_template_keyword (cp_parser
*parser
)
29597 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29599 /* In C++98 the `template' keyword can only be used within templates;
29600 outside templates the parser can always figure out what is a
29601 template and what is not. In C++11, per the resolution of DR 468,
29602 `template' is allowed in cases where it is not strictly necessary. */
29603 if (!processing_template_decl
29604 && pedantic
&& cxx_dialect
== cxx98
)
29606 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29607 pedwarn (token
->location
, OPT_Wpedantic
,
29608 "in C++98 %<template%> (as a disambiguator) is only "
29609 "allowed within templates");
29610 /* If this part of the token stream is rescanned, the same
29611 error message would be generated. So, we purge the token
29612 from the stream. */
29613 cp_lexer_purge_token (parser
->lexer
);
29618 /* Consume the `template' keyword. */
29619 cp_lexer_consume_token (parser
->lexer
);
29626 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29627 set PARSER->SCOPE, and perform other related actions. */
29630 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
29632 struct tree_check
*check_value
;
29634 /* Get the stored value. */
29635 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
29636 /* Set the scope from the stored value. */
29637 parser
->scope
= saved_checks_value (check_value
);
29638 parser
->qualifying_scope
= check_value
->qualifying_scope
;
29639 parser
->object_scope
= NULL_TREE
;
29642 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29643 encounter the end of a block before what we were looking for. */
29646 cp_parser_cache_group (cp_parser
*parser
,
29647 enum cpp_ttype end
,
29652 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29654 /* Abort a parenthesized expression if we encounter a semicolon. */
29655 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
29656 && token
->type
== CPP_SEMICOLON
)
29658 /* If we've reached the end of the file, stop. */
29659 if (token
->type
== CPP_EOF
29660 || (end
!= CPP_PRAGMA_EOL
29661 && token
->type
== CPP_PRAGMA_EOL
))
29663 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
29664 /* We've hit the end of an enclosing block, so there's been some
29665 kind of syntax error. */
29668 /* Consume the token. */
29669 cp_lexer_consume_token (parser
->lexer
);
29670 /* See if it starts a new group. */
29671 if (token
->type
== CPP_OPEN_BRACE
)
29673 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29674 /* In theory this should probably check end == '}', but
29675 cp_parser_save_member_function_body needs it to exit
29676 after either '}' or ')' when called with ')'. */
29680 else if (token
->type
== CPP_OPEN_PAREN
)
29682 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29683 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29686 else if (token
->type
== CPP_PRAGMA
)
29687 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29688 else if (token
->type
== end
)
29693 /* Like above, for caching a default argument or NSDMI. Both of these are
29694 terminated by a non-nested comma, but it can be unclear whether or not a
29695 comma is nested in a template argument list unless we do more parsing.
29696 In order to handle this ambiguity, when we encounter a ',' after a '<'
29697 we try to parse what follows as a parameter-declaration-list (in the
29698 case of a default argument) or a member-declarator (in the case of an
29699 NSDMI). If that succeeds, then we stop caching. */
29702 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29704 unsigned depth
= 0;
29705 int maybe_template_id
= 0;
29706 cp_token
*first_token
;
29708 tree default_argument
;
29710 /* Add tokens until we have processed the entire default
29711 argument. We add the range [first_token, token). */
29712 first_token
= cp_lexer_peek_token (parser
->lexer
);
29713 if (first_token
->type
== CPP_OPEN_BRACE
)
29715 /* For list-initialization, this is straightforward. */
29716 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29717 token
= cp_lexer_peek_token (parser
->lexer
);
29723 /* Peek at the next token. */
29724 token
= cp_lexer_peek_token (parser
->lexer
);
29725 /* What we do depends on what token we have. */
29726 switch (token
->type
)
29728 /* In valid code, a default argument must be
29729 immediately followed by a `,' `)', or `...'. */
29731 if (depth
== 0 && maybe_template_id
)
29733 /* If we've seen a '<', we might be in a
29734 template-argument-list. Until Core issue 325 is
29735 resolved, we don't know how this situation ought
29736 to be handled, so try to DTRT. We check whether
29737 what comes after the comma is a valid parameter
29738 declaration list. If it is, then the comma ends
29739 the default argument; otherwise the default
29740 argument continues. */
29741 bool error
= false;
29744 /* Set ITALP so cp_parser_parameter_declaration_list
29745 doesn't decide to commit to this parse. */
29746 bool saved_italp
= parser
->in_template_argument_list_p
;
29747 parser
->in_template_argument_list_p
= true;
29749 cp_parser_parse_tentatively (parser
);
29753 /* Parse declarators until we reach a non-comma or
29754 somthing that cannot be an initializer.
29755 Just checking whether we're looking at a single
29756 declarator is insufficient. Consider:
29757 int var = tuple<T,U>::x;
29758 The template parameter 'U' looks exactly like a
29762 int ctor_dtor_or_conv_p
;
29763 cp_lexer_consume_token (parser
->lexer
);
29764 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29765 CP_PARSER_FLAGS_NONE
,
29766 &ctor_dtor_or_conv_p
,
29767 /*parenthesized_p=*/NULL
,
29769 /*friend_p=*/false,
29770 /*static_p=*/false);
29771 peek
= cp_lexer_peek_token (parser
->lexer
);
29772 if (cp_parser_error_occurred (parser
))
29775 while (peek
->type
== CPP_COMMA
);
29776 /* If we met an '=' or ';' then the original comma
29777 was the end of the NSDMI. Otherwise assume
29778 we're still in the NSDMI. */
29779 error
= (peek
->type
!= CPP_EQ
29780 && peek
->type
!= CPP_SEMICOLON
);
29784 cp_lexer_consume_token (parser
->lexer
);
29785 begin_scope (sk_function_parms
, NULL_TREE
);
29786 tree t
= cp_parser_parameter_declaration_list
29787 (parser
, CP_PARSER_FLAGS_NONE
);
29788 if (t
== error_mark_node
)
29790 pop_bindings_and_leave_scope ();
29792 if (!cp_parser_error_occurred (parser
) && !error
)
29794 cp_parser_abort_tentative_parse (parser
);
29796 parser
->in_template_argument_list_p
= saved_italp
;
29800 case CPP_CLOSE_PAREN
:
29802 /* If we run into a non-nested `;', `}', or `]',
29803 then the code is invalid -- but the default
29804 argument is certainly over. */
29805 case CPP_SEMICOLON
:
29806 case CPP_CLOSE_BRACE
:
29807 case CPP_CLOSE_SQUARE
:
29809 /* Handle correctly int n = sizeof ... ( p ); */
29810 && token
->type
!= CPP_ELLIPSIS
)
29812 /* Update DEPTH, if necessary. */
29813 else if (token
->type
== CPP_CLOSE_PAREN
29814 || token
->type
== CPP_CLOSE_BRACE
29815 || token
->type
== CPP_CLOSE_SQUARE
)
29819 case CPP_OPEN_PAREN
:
29820 case CPP_OPEN_SQUARE
:
29821 case CPP_OPEN_BRACE
:
29827 /* This might be the comparison operator, or it might
29828 start a template argument list. */
29829 ++maybe_template_id
;
29833 if (cxx_dialect
== cxx98
)
29835 /* Fall through for C++0x, which treats the `>>'
29836 operator like two `>' tokens in certain
29838 gcc_fallthrough ();
29843 /* This might be an operator, or it might close a
29844 template argument list. But if a previous '<'
29845 started a template argument list, this will have
29846 closed it, so we can't be in one anymore. */
29847 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29848 if (maybe_template_id
< 0)
29849 maybe_template_id
= 0;
29853 /* If we run out of tokens, issue an error message. */
29855 case CPP_PRAGMA_EOL
:
29856 error_at (token
->location
, "file ends in default argument");
29857 return error_mark_node
;
29861 /* In these cases, we should look for template-ids.
29862 For example, if the default argument is
29863 `X<int, double>()', we need to do name lookup to
29864 figure out whether or not `X' is a template; if
29865 so, the `,' does not end the default argument.
29867 That is not yet done. */
29874 /* If we've reached the end, stop. */
29878 /* Add the token to the token block. */
29879 token
= cp_lexer_consume_token (parser
->lexer
);
29882 /* Create a DEFAULT_ARG to represent the unparsed default
29884 default_argument
= make_node (DEFAULT_ARG
);
29885 DEFARG_TOKENS (default_argument
)
29886 = cp_token_cache_new (first_token
, token
);
29887 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29889 return default_argument
;
29892 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29895 defarg_location (tree default_argument
)
29897 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29898 location_t start
= tokens
->first
->location
;
29899 location_t end
= tokens
->last
->location
;
29900 return make_location (start
, start
, end
);
29903 /* Begin parsing tentatively. We always save tokens while parsing
29904 tentatively so that if the tentative parsing fails we can restore the
29908 cp_parser_parse_tentatively (cp_parser
* parser
)
29910 /* Enter a new parsing context. */
29911 parser
->context
= cp_parser_context_new (parser
->context
);
29912 /* Begin saving tokens. */
29913 cp_lexer_save_tokens (parser
->lexer
);
29914 /* In order to avoid repetitive access control error messages,
29915 access checks are queued up until we are no longer parsing
29917 push_deferring_access_checks (dk_deferred
);
29920 /* Commit to the currently active tentative parse. */
29923 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29925 cp_parser_context
*context
;
29928 /* Mark all of the levels as committed. */
29929 lexer
= parser
->lexer
;
29930 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29932 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29934 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29935 while (!cp_lexer_saving_tokens (lexer
))
29936 lexer
= lexer
->next
;
29937 cp_lexer_commit_tokens (lexer
);
29941 /* Commit to the topmost currently active tentative parse.
29943 Note that this function shouldn't be called when there are
29944 irreversible side-effects while in a tentative state. For
29945 example, we shouldn't create a permanent entry in the symbol
29946 table, or issue an error message that might not apply if the
29947 tentative parse is aborted. */
29950 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29952 cp_parser_context
*context
= parser
->context
;
29953 cp_lexer
*lexer
= parser
->lexer
;
29957 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29959 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29961 while (!cp_lexer_saving_tokens (lexer
))
29962 lexer
= lexer
->next
;
29963 cp_lexer_commit_tokens (lexer
);
29967 /* Abort the currently active tentative parse. All consumed tokens
29968 will be rolled back, and no diagnostics will be issued. */
29971 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29973 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29974 || errorcount
> 0);
29975 cp_parser_simulate_error (parser
);
29976 /* Now, pretend that we want to see if the construct was
29977 successfully parsed. */
29978 cp_parser_parse_definitely (parser
);
29981 /* Stop parsing tentatively. If a parse error has occurred, restore the
29982 token stream. Otherwise, commit to the tokens we have consumed.
29983 Returns true if no error occurred; false otherwise. */
29986 cp_parser_parse_definitely (cp_parser
* parser
)
29988 bool error_occurred
;
29989 cp_parser_context
*context
;
29991 /* Remember whether or not an error occurred, since we are about to
29992 destroy that information. */
29993 error_occurred
= cp_parser_error_occurred (parser
);
29994 /* Remove the topmost context from the stack. */
29995 context
= parser
->context
;
29996 parser
->context
= context
->next
;
29997 /* If no parse errors occurred, commit to the tentative parse. */
29998 if (!error_occurred
)
30000 /* Commit to the tokens read tentatively, unless that was
30002 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
30003 cp_lexer_commit_tokens (parser
->lexer
);
30005 pop_to_parent_deferring_access_checks ();
30007 /* Otherwise, if errors occurred, roll back our state so that things
30008 are just as they were before we began the tentative parse. */
30011 cp_lexer_rollback_tokens (parser
->lexer
);
30012 pop_deferring_access_checks ();
30014 /* Add the context to the front of the free list. */
30015 context
->next
= cp_parser_context_free_list
;
30016 cp_parser_context_free_list
= context
;
30018 return !error_occurred
;
30021 /* Returns true if we are parsing tentatively and are not committed to
30022 this tentative parse. */
30025 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
30027 return (cp_parser_parsing_tentatively (parser
)
30028 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
30031 /* Returns nonzero iff an error has occurred during the most recent
30032 tentative parse. */
30035 cp_parser_error_occurred (cp_parser
* parser
)
30037 return (cp_parser_parsing_tentatively (parser
)
30038 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
30041 /* Returns nonzero if GNU extensions are allowed. */
30044 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
30046 return parser
->allow_gnu_extensions_p
;
30049 /* Objective-C++ Productions */
30052 /* Parse an Objective-C expression, which feeds into a primary-expression
30056 objc-message-expression
30057 objc-string-literal
30058 objc-encode-expression
30059 objc-protocol-expression
30060 objc-selector-expression
30062 Returns a tree representation of the expression. */
30065 cp_parser_objc_expression (cp_parser
* parser
)
30067 /* Try to figure out what kind of declaration is present. */
30068 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30072 case CPP_OPEN_SQUARE
:
30073 return cp_parser_objc_message_expression (parser
);
30075 case CPP_OBJC_STRING
:
30076 kwd
= cp_lexer_consume_token (parser
->lexer
);
30077 return objc_build_string_object (kwd
->u
.value
);
30080 switch (kwd
->keyword
)
30082 case RID_AT_ENCODE
:
30083 return cp_parser_objc_encode_expression (parser
);
30085 case RID_AT_PROTOCOL
:
30086 return cp_parser_objc_protocol_expression (parser
);
30088 case RID_AT_SELECTOR
:
30089 return cp_parser_objc_selector_expression (parser
);
30096 error_at (kwd
->location
,
30097 "misplaced %<@%D%> Objective-C++ construct",
30099 cp_parser_skip_to_end_of_block_or_statement (parser
);
30102 return error_mark_node
;
30105 /* Parse an Objective-C message expression.
30107 objc-message-expression:
30108 [ objc-message-receiver objc-message-args ]
30110 Returns a representation of an Objective-C message. */
30113 cp_parser_objc_message_expression (cp_parser
* parser
)
30115 tree receiver
, messageargs
;
30117 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30118 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
30119 receiver
= cp_parser_objc_message_receiver (parser
);
30120 messageargs
= cp_parser_objc_message_args (parser
);
30121 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30122 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
30124 tree result
= objc_build_message_expr (receiver
, messageargs
);
30126 /* Construct a location e.g.
30129 ranging from the '[' to the ']', with the caret at the start. */
30130 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
30131 protected_set_expr_location (result
, combined_loc
);
30136 /* Parse an objc-message-receiver.
30138 objc-message-receiver:
30140 simple-type-specifier
30142 Returns a representation of the type or expression. */
30145 cp_parser_objc_message_receiver (cp_parser
* parser
)
30149 /* An Objective-C message receiver may be either (1) a type
30150 or (2) an expression. */
30151 cp_parser_parse_tentatively (parser
);
30152 rcv
= cp_parser_expression (parser
);
30154 /* If that worked out, fine. */
30155 if (cp_parser_parse_definitely (parser
))
30158 cp_parser_parse_tentatively (parser
);
30159 rcv
= cp_parser_simple_type_specifier (parser
,
30160 /*decl_specs=*/NULL
,
30161 CP_PARSER_FLAGS_NONE
);
30163 if (cp_parser_parse_definitely (parser
))
30164 return objc_get_class_reference (rcv
);
30166 cp_parser_error (parser
, "objective-c++ message receiver expected");
30167 return error_mark_node
;
30170 /* Parse the arguments and selectors comprising an Objective-C message.
30175 objc-selector-args , objc-comma-args
30177 objc-selector-args:
30178 objc-selector [opt] : assignment-expression
30179 objc-selector-args objc-selector [opt] : assignment-expression
30182 assignment-expression
30183 objc-comma-args , assignment-expression
30185 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30186 selector arguments and TREE_VALUE containing a list of comma
30190 cp_parser_objc_message_args (cp_parser
* parser
)
30192 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
30193 bool maybe_unary_selector_p
= true;
30194 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30196 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30198 tree selector
= NULL_TREE
, arg
;
30200 if (token
->type
!= CPP_COLON
)
30201 selector
= cp_parser_objc_selector (parser
);
30203 /* Detect if we have a unary selector. */
30204 if (maybe_unary_selector_p
30205 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30206 return build_tree_list (selector
, NULL_TREE
);
30208 maybe_unary_selector_p
= false;
30209 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30210 arg
= cp_parser_assignment_expression (parser
);
30213 = chainon (sel_args
,
30214 build_tree_list (selector
, arg
));
30216 token
= cp_lexer_peek_token (parser
->lexer
);
30219 /* Handle non-selector arguments, if any. */
30220 while (token
->type
== CPP_COMMA
)
30224 cp_lexer_consume_token (parser
->lexer
);
30225 arg
= cp_parser_assignment_expression (parser
);
30228 = chainon (addl_args
,
30229 build_tree_list (NULL_TREE
, arg
));
30231 token
= cp_lexer_peek_token (parser
->lexer
);
30234 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
30236 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
30237 return build_tree_list (error_mark_node
, error_mark_node
);
30240 return build_tree_list (sel_args
, addl_args
);
30243 /* Parse an Objective-C encode expression.
30245 objc-encode-expression:
30246 @encode objc-typename
30248 Returns an encoded representation of the type argument. */
30251 cp_parser_objc_encode_expression (cp_parser
* parser
)
30255 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30257 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
30258 matching_parens parens
;
30259 parens
.require_open (parser
);
30260 token
= cp_lexer_peek_token (parser
->lexer
);
30261 type
= complete_type (cp_parser_type_id (parser
));
30262 parens
.require_close (parser
);
30266 error_at (token
->location
,
30267 "%<@encode%> must specify a type as an argument");
30268 return error_mark_node
;
30271 /* This happens if we find @encode(T) (where T is a template
30272 typename or something dependent on a template typename) when
30273 parsing a template. In that case, we can't compile it
30274 immediately, but we rather create an AT_ENCODE_EXPR which will
30275 need to be instantiated when the template is used.
30277 if (dependent_type_p (type
))
30279 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
30280 TREE_READONLY (value
) = 1;
30285 /* Build a location of the form:
30288 with caret==start at the @ token, finishing at the close paren. */
30289 location_t combined_loc
30290 = make_location (start_loc
, start_loc
,
30291 cp_lexer_previous_token (parser
->lexer
)->location
);
30293 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
30296 /* Parse an Objective-C @defs expression. */
30299 cp_parser_objc_defs_expression (cp_parser
*parser
)
30303 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
30304 matching_parens parens
;
30305 parens
.require_open (parser
);
30306 name
= cp_parser_identifier (parser
);
30307 parens
.require_close (parser
);
30309 return objc_get_class_ivars (name
);
30312 /* Parse an Objective-C protocol expression.
30314 objc-protocol-expression:
30315 @protocol ( identifier )
30317 Returns a representation of the protocol expression. */
30320 cp_parser_objc_protocol_expression (cp_parser
* parser
)
30323 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30325 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30326 matching_parens parens
;
30327 parens
.require_open (parser
);
30328 proto
= cp_parser_identifier (parser
);
30329 parens
.require_close (parser
);
30331 /* Build a location of the form:
30334 with caret==start at the @ token, finishing at the close paren. */
30335 location_t combined_loc
30336 = make_location (start_loc
, start_loc
,
30337 cp_lexer_previous_token (parser
->lexer
)->location
);
30338 tree result
= objc_build_protocol_expr (proto
);
30339 protected_set_expr_location (result
, combined_loc
);
30343 /* Parse an Objective-C selector expression.
30345 objc-selector-expression:
30346 @selector ( objc-method-signature )
30348 objc-method-signature:
30354 objc-selector-seq objc-selector :
30356 Returns a representation of the method selector. */
30359 cp_parser_objc_selector_expression (cp_parser
* parser
)
30361 tree sel_seq
= NULL_TREE
;
30362 bool maybe_unary_selector_p
= true;
30364 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30366 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
30367 matching_parens parens
;
30368 parens
.require_open (parser
);
30369 token
= cp_lexer_peek_token (parser
->lexer
);
30371 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
30372 || token
->type
== CPP_SCOPE
)
30374 tree selector
= NULL_TREE
;
30376 if (token
->type
!= CPP_COLON
30377 || token
->type
== CPP_SCOPE
)
30378 selector
= cp_parser_objc_selector (parser
);
30380 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
30381 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
30383 /* Detect if we have a unary selector. */
30384 if (maybe_unary_selector_p
)
30386 sel_seq
= selector
;
30387 goto finish_selector
;
30391 cp_parser_error (parser
, "expected %<:%>");
30394 maybe_unary_selector_p
= false;
30395 token
= cp_lexer_consume_token (parser
->lexer
);
30397 if (token
->type
== CPP_SCOPE
)
30400 = chainon (sel_seq
,
30401 build_tree_list (selector
, NULL_TREE
));
30403 = chainon (sel_seq
,
30404 build_tree_list (NULL_TREE
, NULL_TREE
));
30408 = chainon (sel_seq
,
30409 build_tree_list (selector
, NULL_TREE
));
30411 token
= cp_lexer_peek_token (parser
->lexer
);
30415 parens
.require_close (parser
);
30418 /* Build a location of the form:
30421 with caret==start at the @ token, finishing at the close paren. */
30422 location_t combined_loc
30423 = make_location (loc
, loc
,
30424 cp_lexer_previous_token (parser
->lexer
)->location
);
30425 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
30426 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30427 protected_set_expr_location (result
, combined_loc
);
30431 /* Parse a list of identifiers.
30433 objc-identifier-list:
30435 objc-identifier-list , identifier
30437 Returns a TREE_LIST of identifier nodes. */
30440 cp_parser_objc_identifier_list (cp_parser
* parser
)
30446 identifier
= cp_parser_identifier (parser
);
30447 if (identifier
== error_mark_node
)
30448 return error_mark_node
;
30450 list
= build_tree_list (NULL_TREE
, identifier
);
30451 sep
= cp_lexer_peek_token (parser
->lexer
);
30453 while (sep
->type
== CPP_COMMA
)
30455 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30456 identifier
= cp_parser_identifier (parser
);
30457 if (identifier
== error_mark_node
)
30460 list
= chainon (list
, build_tree_list (NULL_TREE
,
30462 sep
= cp_lexer_peek_token (parser
->lexer
);
30468 /* Parse an Objective-C alias declaration.
30470 objc-alias-declaration:
30471 @compatibility_alias identifier identifier ;
30473 This function registers the alias mapping with the Objective-C front end.
30474 It returns nothing. */
30477 cp_parser_objc_alias_declaration (cp_parser
* parser
)
30481 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
30482 alias
= cp_parser_identifier (parser
);
30483 orig
= cp_parser_identifier (parser
);
30484 objc_declare_alias (alias
, orig
);
30485 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30488 /* Parse an Objective-C class forward-declaration.
30490 objc-class-declaration:
30491 @class objc-identifier-list ;
30493 The function registers the forward declarations with the Objective-C
30494 front end. It returns nothing. */
30497 cp_parser_objc_class_declaration (cp_parser
* parser
)
30499 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
30504 id
= cp_parser_identifier (parser
);
30505 if (id
== error_mark_node
)
30508 objc_declare_class (id
);
30510 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30511 cp_lexer_consume_token (parser
->lexer
);
30515 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30518 /* Parse a list of Objective-C protocol references.
30520 objc-protocol-refs-opt:
30521 objc-protocol-refs [opt]
30523 objc-protocol-refs:
30524 < objc-identifier-list >
30526 Returns a TREE_LIST of identifiers, if any. */
30529 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
30531 tree protorefs
= NULL_TREE
;
30533 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
30535 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
30536 protorefs
= cp_parser_objc_identifier_list (parser
);
30537 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
30543 /* Parse a Objective-C visibility specification. */
30546 cp_parser_objc_visibility_spec (cp_parser
* parser
)
30548 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
30550 switch (vis
->keyword
)
30552 case RID_AT_PRIVATE
:
30553 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
30555 case RID_AT_PROTECTED
:
30556 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
30558 case RID_AT_PUBLIC
:
30559 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
30561 case RID_AT_PACKAGE
:
30562 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
30568 /* Eat '@private'/'@protected'/'@public'. */
30569 cp_lexer_consume_token (parser
->lexer
);
30572 /* Parse an Objective-C method type. Return 'true' if it is a class
30573 (+) method, and 'false' if it is an instance (-) method. */
30576 cp_parser_objc_method_type (cp_parser
* parser
)
30578 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
30584 /* Parse an Objective-C protocol qualifier. */
30587 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
30589 tree quals
= NULL_TREE
, node
;
30590 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30592 node
= token
->u
.value
;
30594 while (node
&& identifier_p (node
)
30595 && (node
== ridpointers
[(int) RID_IN
]
30596 || node
== ridpointers
[(int) RID_OUT
]
30597 || node
== ridpointers
[(int) RID_INOUT
]
30598 || node
== ridpointers
[(int) RID_BYCOPY
]
30599 || node
== ridpointers
[(int) RID_BYREF
]
30600 || node
== ridpointers
[(int) RID_ONEWAY
]))
30602 quals
= tree_cons (NULL_TREE
, node
, quals
);
30603 cp_lexer_consume_token (parser
->lexer
);
30604 token
= cp_lexer_peek_token (parser
->lexer
);
30605 node
= token
->u
.value
;
30611 /* Parse an Objective-C typename. */
30614 cp_parser_objc_typename (cp_parser
* parser
)
30616 tree type_name
= NULL_TREE
;
30618 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30620 tree proto_quals
, cp_type
= NULL_TREE
;
30622 matching_parens parens
;
30623 parens
.consume_open (parser
); /* Eat '('. */
30624 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
30626 /* An ObjC type name may consist of just protocol qualifiers, in which
30627 case the type shall default to 'id'. */
30628 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30630 cp_type
= cp_parser_type_id (parser
);
30632 /* If the type could not be parsed, an error has already
30633 been produced. For error recovery, behave as if it had
30634 not been specified, which will use the default type
30636 if (cp_type
== error_mark_node
)
30638 cp_type
= NULL_TREE
;
30639 /* We need to skip to the closing parenthesis as
30640 cp_parser_type_id() does not seem to do it for
30642 cp_parser_skip_to_closing_parenthesis (parser
,
30643 /*recovering=*/true,
30644 /*or_comma=*/false,
30645 /*consume_paren=*/false);
30649 parens
.require_close (parser
);
30650 type_name
= build_tree_list (proto_quals
, cp_type
);
30656 /* Check to see if TYPE refers to an Objective-C selector name. */
30659 cp_parser_objc_selector_p (enum cpp_ttype type
)
30661 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
30662 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
30663 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
30664 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
30665 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
30668 /* Parse an Objective-C selector. */
30671 cp_parser_objc_selector (cp_parser
* parser
)
30673 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30675 if (!cp_parser_objc_selector_p (token
->type
))
30677 error_at (token
->location
, "invalid Objective-C++ selector name");
30678 return error_mark_node
;
30681 /* C++ operator names are allowed to appear in ObjC selectors. */
30682 switch (token
->type
)
30684 case CPP_AND_AND
: return get_identifier ("and");
30685 case CPP_AND_EQ
: return get_identifier ("and_eq");
30686 case CPP_AND
: return get_identifier ("bitand");
30687 case CPP_OR
: return get_identifier ("bitor");
30688 case CPP_COMPL
: return get_identifier ("compl");
30689 case CPP_NOT
: return get_identifier ("not");
30690 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30691 case CPP_OR_OR
: return get_identifier ("or");
30692 case CPP_OR_EQ
: return get_identifier ("or_eq");
30693 case CPP_XOR
: return get_identifier ("xor");
30694 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30695 default: return token
->u
.value
;
30699 /* Parse an Objective-C params list. */
30702 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30704 tree params
= NULL_TREE
;
30705 bool maybe_unary_selector_p
= true;
30706 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30708 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30710 tree selector
= NULL_TREE
, type_name
, identifier
;
30711 tree parm_attr
= NULL_TREE
;
30713 if (token
->keyword
== RID_ATTRIBUTE
)
30716 if (token
->type
!= CPP_COLON
)
30717 selector
= cp_parser_objc_selector (parser
);
30719 /* Detect if we have a unary selector. */
30720 if (maybe_unary_selector_p
30721 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30723 params
= selector
; /* Might be followed by attributes. */
30727 maybe_unary_selector_p
= false;
30728 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30730 /* Something went quite wrong. There should be a colon
30731 here, but there is not. Stop parsing parameters. */
30734 type_name
= cp_parser_objc_typename (parser
);
30735 /* New ObjC allows attributes on parameters too. */
30736 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30737 parm_attr
= cp_parser_attributes_opt (parser
);
30738 identifier
= cp_parser_identifier (parser
);
30742 objc_build_keyword_decl (selector
,
30747 token
= cp_lexer_peek_token (parser
->lexer
);
30750 if (params
== NULL_TREE
)
30752 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30753 return error_mark_node
;
30756 /* We allow tail attributes for the method. */
30757 if (token
->keyword
== RID_ATTRIBUTE
)
30759 *attributes
= cp_parser_attributes_opt (parser
);
30760 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30761 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30763 cp_parser_error (parser
,
30764 "method attributes must be specified at the end");
30765 return error_mark_node
;
30768 if (params
== NULL_TREE
)
30770 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30771 return error_mark_node
;
30776 /* Parse the non-keyword Objective-C params. */
30779 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30782 tree params
= make_node (TREE_LIST
);
30783 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30784 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30786 while (token
->type
== CPP_COMMA
)
30788 cp_parameter_declarator
*parmdecl
;
30791 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30792 token
= cp_lexer_peek_token (parser
->lexer
);
30794 if (token
->type
== CPP_ELLIPSIS
)
30796 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30798 token
= cp_lexer_peek_token (parser
->lexer
);
30802 /* TODO: parse attributes for tail parameters. */
30803 parmdecl
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
30805 parm
= grokdeclarator (parmdecl
->declarator
,
30806 &parmdecl
->decl_specifiers
,
30807 PARM
, /*initialized=*/0,
30808 /*attrlist=*/NULL
);
30810 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30811 token
= cp_lexer_peek_token (parser
->lexer
);
30814 /* We allow tail attributes for the method. */
30815 if (token
->keyword
== RID_ATTRIBUTE
)
30817 if (*attributes
== NULL_TREE
)
30819 *attributes
= cp_parser_attributes_opt (parser
);
30820 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30821 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30825 /* We have an error, but parse the attributes, so that we can
30827 *attributes
= cp_parser_attributes_opt (parser
);
30829 cp_parser_error (parser
,
30830 "method attributes must be specified at the end");
30831 return error_mark_node
;
30837 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30840 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30842 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30844 /* If the next token is `extern' and the following token is a string
30845 literal, then we have a linkage specification. */
30846 if (token
->keyword
== RID_EXTERN
30847 && cp_parser_is_pure_string_literal
30848 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30849 cp_parser_linkage_specification (parser
);
30850 /* Handle #pragma, if any. */
30851 else if (token
->type
== CPP_PRAGMA
)
30852 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30853 /* Allow stray semicolons. */
30854 else if (token
->type
== CPP_SEMICOLON
)
30855 cp_lexer_consume_token (parser
->lexer
);
30856 /* Mark methods as optional or required, when building protocols. */
30857 else if (token
->keyword
== RID_AT_OPTIONAL
)
30859 cp_lexer_consume_token (parser
->lexer
);
30860 objc_set_method_opt (true);
30862 else if (token
->keyword
== RID_AT_REQUIRED
)
30864 cp_lexer_consume_token (parser
->lexer
);
30865 objc_set_method_opt (false);
30867 else if (token
->keyword
== RID_NAMESPACE
)
30868 cp_parser_namespace_definition (parser
);
30869 /* Other stray characters must generate errors. */
30870 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30872 cp_lexer_consume_token (parser
->lexer
);
30873 error ("stray %qs between Objective-C++ methods",
30874 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30876 /* Finally, try to parse a block-declaration, or a function-definition. */
30878 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30881 /* Parse a method signature. */
30884 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30886 tree rettype
, kwdparms
, optparms
;
30887 bool ellipsis
= false;
30888 bool is_class_method
;
30890 is_class_method
= cp_parser_objc_method_type (parser
);
30891 rettype
= cp_parser_objc_typename (parser
);
30892 *attributes
= NULL_TREE
;
30893 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30894 if (kwdparms
== error_mark_node
)
30895 return error_mark_node
;
30896 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30897 if (optparms
== error_mark_node
)
30898 return error_mark_node
;
30900 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30904 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30907 cp_lexer_save_tokens (parser
->lexer
);
30908 tattr
= cp_parser_attributes_opt (parser
);
30909 gcc_assert (tattr
) ;
30911 /* If the attributes are followed by a method introducer, this is not allowed.
30912 Dump the attributes and flag the situation. */
30913 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30914 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30917 /* Otherwise, the attributes introduce some interstitial code, possibly so
30918 rewind to allow that check. */
30919 cp_lexer_rollback_tokens (parser
->lexer
);
30923 /* Parse an Objective-C method prototype list. */
30926 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30928 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30930 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30932 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30934 tree attributes
, sig
;
30935 bool is_class_method
;
30936 if (token
->type
== CPP_PLUS
)
30937 is_class_method
= true;
30939 is_class_method
= false;
30940 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30941 if (sig
== error_mark_node
)
30943 cp_parser_skip_to_end_of_block_or_statement (parser
);
30944 token
= cp_lexer_peek_token (parser
->lexer
);
30947 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30948 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30950 else if (token
->keyword
== RID_AT_PROPERTY
)
30951 cp_parser_objc_at_property_declaration (parser
);
30952 else if (token
->keyword
== RID_ATTRIBUTE
30953 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30954 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30956 "prefix attributes are ignored for methods");
30958 /* Allow for interspersed non-ObjC++ code. */
30959 cp_parser_objc_interstitial_code (parser
);
30961 token
= cp_lexer_peek_token (parser
->lexer
);
30964 if (token
->type
!= CPP_EOF
)
30965 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30967 cp_parser_error (parser
, "expected %<@end%>");
30969 objc_finish_interface ();
30972 /* Parse an Objective-C method definition list. */
30975 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30977 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30979 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30983 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30986 tree sig
, attribute
;
30987 bool is_class_method
;
30988 if (token
->type
== CPP_PLUS
)
30989 is_class_method
= true;
30991 is_class_method
= false;
30992 push_deferring_access_checks (dk_deferred
);
30993 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30994 if (sig
== error_mark_node
)
30996 cp_parser_skip_to_end_of_block_or_statement (parser
);
30997 token
= cp_lexer_peek_token (parser
->lexer
);
31000 objc_start_method_definition (is_class_method
, sig
, attribute
,
31003 /* For historical reasons, we accept an optional semicolon. */
31004 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31005 cp_lexer_consume_token (parser
->lexer
);
31007 ptk
= cp_lexer_peek_token (parser
->lexer
);
31008 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
31009 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
31011 perform_deferred_access_checks (tf_warning_or_error
);
31012 stop_deferring_access_checks ();
31013 meth
= cp_parser_function_definition_after_declarator (parser
,
31015 pop_deferring_access_checks ();
31016 objc_finish_method_definition (meth
);
31019 /* The following case will be removed once @synthesize is
31020 completely implemented. */
31021 else if (token
->keyword
== RID_AT_PROPERTY
)
31022 cp_parser_objc_at_property_declaration (parser
);
31023 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
31024 cp_parser_objc_at_synthesize_declaration (parser
);
31025 else if (token
->keyword
== RID_AT_DYNAMIC
)
31026 cp_parser_objc_at_dynamic_declaration (parser
);
31027 else if (token
->keyword
== RID_ATTRIBUTE
31028 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
31029 warning_at (token
->location
, OPT_Wattributes
,
31030 "prefix attributes are ignored for methods");
31032 /* Allow for interspersed non-ObjC++ code. */
31033 cp_parser_objc_interstitial_code (parser
);
31035 token
= cp_lexer_peek_token (parser
->lexer
);
31038 if (token
->type
!= CPP_EOF
)
31039 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31041 cp_parser_error (parser
, "expected %<@end%>");
31043 objc_finish_implementation ();
31046 /* Parse Objective-C ivars. */
31049 cp_parser_objc_class_ivars (cp_parser
* parser
)
31051 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31053 if (token
->type
!= CPP_OPEN_BRACE
)
31054 return; /* No ivars specified. */
31056 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
31057 token
= cp_lexer_peek_token (parser
->lexer
);
31059 while (token
->type
!= CPP_CLOSE_BRACE
31060 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31062 cp_decl_specifier_seq declspecs
;
31063 int decl_class_or_enum_p
;
31064 tree prefix_attributes
;
31066 cp_parser_objc_visibility_spec (parser
);
31068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
31071 cp_parser_decl_specifier_seq (parser
,
31072 CP_PARSER_FLAGS_OPTIONAL
,
31074 &decl_class_or_enum_p
);
31076 /* auto, register, static, extern, mutable. */
31077 if (declspecs
.storage_class
!= sc_none
)
31079 cp_parser_error (parser
, "invalid type for instance variable");
31080 declspecs
.storage_class
= sc_none
;
31083 /* thread_local. */
31084 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31086 cp_parser_error (parser
, "invalid type for instance variable");
31087 declspecs
.locations
[ds_thread
] = 0;
31091 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31093 cp_parser_error (parser
, "invalid type for instance variable");
31094 declspecs
.locations
[ds_typedef
] = 0;
31097 prefix_attributes
= declspecs
.attributes
;
31098 declspecs
.attributes
= NULL_TREE
;
31100 /* Keep going until we hit the `;' at the end of the
31102 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31104 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
31105 cp_declarator
*declarator
= NULL
;
31106 int ctor_dtor_or_conv_p
;
31108 /* Check for a (possibly unnamed) bitfield declaration. */
31109 token
= cp_lexer_peek_token (parser
->lexer
);
31110 if (token
->type
== CPP_COLON
)
31113 if (token
->type
== CPP_NAME
31114 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
31117 /* Get the name of the bitfield. */
31118 declarator
= make_id_declarator (NULL_TREE
,
31119 cp_parser_identifier (parser
),
31120 sfk_none
, token
->location
);
31123 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
31124 /* Get the width of the bitfield. */
31126 = cp_parser_constant_expression (parser
);
31130 /* Parse the declarator. */
31132 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31133 CP_PARSER_FLAGS_NONE
,
31134 &ctor_dtor_or_conv_p
,
31135 /*parenthesized_p=*/NULL
,
31136 /*member_p=*/false,
31137 /*friend_p=*/false,
31138 /*static_p=*/false);
31141 /* Look for attributes that apply to the ivar. */
31142 attributes
= cp_parser_attributes_opt (parser
);
31143 /* Remember which attributes are prefix attributes and
31145 first_attribute
= attributes
;
31146 /* Combine the attributes. */
31147 attributes
= attr_chainon (prefix_attributes
, attributes
);
31150 /* Create the bitfield declaration. */
31151 decl
= grokbitfield (declarator
, &declspecs
,
31152 width
, NULL_TREE
, attributes
);
31154 decl
= grokfield (declarator
, &declspecs
,
31155 NULL_TREE
, /*init_const_expr_p=*/false,
31156 NULL_TREE
, attributes
);
31158 /* Add the instance variable. */
31159 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
31160 objc_add_instance_variable (decl
);
31162 /* Reset PREFIX_ATTRIBUTES. */
31163 if (attributes
!= error_mark_node
)
31165 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31166 attributes
= TREE_CHAIN (attributes
);
31168 TREE_CHAIN (attributes
) = NULL_TREE
;
31171 token
= cp_lexer_peek_token (parser
->lexer
);
31173 if (token
->type
== CPP_COMMA
)
31175 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31181 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31182 token
= cp_lexer_peek_token (parser
->lexer
);
31185 if (token
->keyword
== RID_AT_END
)
31186 cp_parser_error (parser
, "expected %<}%>");
31188 /* Do not consume the RID_AT_END, so it will be read again as terminating
31189 the @interface of @implementation. */
31190 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31191 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
31193 /* For historical reasons, we accept an optional semicolon. */
31194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31195 cp_lexer_consume_token (parser
->lexer
);
31198 /* Parse an Objective-C protocol declaration. */
31201 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
31203 tree proto
, protorefs
;
31206 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
31207 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31209 tok
= cp_lexer_peek_token (parser
->lexer
);
31210 error_at (tok
->location
, "identifier expected after %<@protocol%>");
31211 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31215 /* See if we have a forward declaration or a definition. */
31216 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
31218 /* Try a forward declaration first. */
31219 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
31225 id
= cp_parser_identifier (parser
);
31226 if (id
== error_mark_node
)
31229 objc_declare_protocol (id
, attributes
);
31231 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31232 cp_lexer_consume_token (parser
->lexer
);
31236 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31239 /* Ok, we got a full-fledged definition (or at least should). */
31242 proto
= cp_parser_identifier (parser
);
31243 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
31244 objc_start_protocol (proto
, protorefs
, attributes
);
31245 cp_parser_objc_method_prototype_list (parser
);
31249 /* Parse an Objective-C superclass or category. */
31252 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
31255 tree
*categ
, bool *is_class_extension
)
31257 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
31259 *super
= *categ
= NULL_TREE
;
31260 *is_class_extension
= false;
31261 if (next
->type
== CPP_COLON
)
31263 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
31264 *super
= cp_parser_identifier (parser
);
31266 else if (next
->type
== CPP_OPEN_PAREN
)
31268 matching_parens parens
;
31269 parens
.consume_open (parser
); /* Eat '('. */
31271 /* If there is no category name, and this is an @interface, we
31272 have a class extension. */
31273 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31275 *categ
= NULL_TREE
;
31276 *is_class_extension
= true;
31279 *categ
= cp_parser_identifier (parser
);
31281 parens
.require_close (parser
);
31285 /* Parse an Objective-C class interface. */
31288 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
31290 tree name
, super
, categ
, protos
;
31291 bool is_class_extension
;
31293 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
31294 name
= cp_parser_identifier (parser
);
31295 if (name
== error_mark_node
)
31297 /* It's hard to recover because even if valid @interface stuff
31298 is to follow, we can't compile it (or validate it) if we
31299 don't even know which class it refers to. Let's assume this
31300 was a stray '@interface' token in the stream and skip it.
31304 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
31305 &is_class_extension
);
31306 protos
= cp_parser_objc_protocol_refs_opt (parser
);
31308 /* We have either a class or a category on our hands. */
31309 if (categ
|| is_class_extension
)
31310 objc_start_category_interface (name
, categ
, protos
, attributes
);
31313 objc_start_class_interface (name
, super
, protos
, attributes
);
31314 /* Handle instance variable declarations, if any. */
31315 cp_parser_objc_class_ivars (parser
);
31316 objc_continue_interface ();
31319 cp_parser_objc_method_prototype_list (parser
);
31322 /* Parse an Objective-C class implementation. */
31325 cp_parser_objc_class_implementation (cp_parser
* parser
)
31327 tree name
, super
, categ
;
31328 bool is_class_extension
;
31330 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
31331 name
= cp_parser_identifier (parser
);
31332 if (name
== error_mark_node
)
31334 /* It's hard to recover because even if valid @implementation
31335 stuff is to follow, we can't compile it (or validate it) if
31336 we don't even know which class it refers to. Let's assume
31337 this was a stray '@implementation' token in the stream and
31342 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
31343 &is_class_extension
);
31345 /* We have either a class or a category on our hands. */
31347 objc_start_category_implementation (name
, categ
);
31350 objc_start_class_implementation (name
, super
);
31351 /* Handle instance variable declarations, if any. */
31352 cp_parser_objc_class_ivars (parser
);
31353 objc_continue_implementation ();
31356 cp_parser_objc_method_definition_list (parser
);
31359 /* Consume the @end token and finish off the implementation. */
31362 cp_parser_objc_end_implementation (cp_parser
* parser
)
31364 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31365 objc_finish_implementation ();
31368 /* Parse an Objective-C declaration. */
31371 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
31373 /* Try to figure out what kind of declaration is present. */
31374 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31377 switch (kwd
->keyword
)
31382 error_at (kwd
->location
, "attributes may not be specified before"
31383 " the %<@%D%> Objective-C++ keyword",
31387 case RID_AT_IMPLEMENTATION
:
31388 warning_at (kwd
->location
, OPT_Wattributes
,
31389 "prefix attributes are ignored before %<@%D%>",
31396 switch (kwd
->keyword
)
31399 cp_parser_objc_alias_declaration (parser
);
31402 cp_parser_objc_class_declaration (parser
);
31404 case RID_AT_PROTOCOL
:
31405 cp_parser_objc_protocol_declaration (parser
, attributes
);
31407 case RID_AT_INTERFACE
:
31408 cp_parser_objc_class_interface (parser
, attributes
);
31410 case RID_AT_IMPLEMENTATION
:
31411 cp_parser_objc_class_implementation (parser
);
31414 cp_parser_objc_end_implementation (parser
);
31417 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31419 cp_parser_skip_to_end_of_block_or_statement (parser
);
31423 /* Parse an Objective-C try-catch-finally statement.
31425 objc-try-catch-finally-stmt:
31426 @try compound-statement objc-catch-clause-seq [opt]
31427 objc-finally-clause [opt]
31429 objc-catch-clause-seq:
31430 objc-catch-clause objc-catch-clause-seq [opt]
31433 @catch ( objc-exception-declaration ) compound-statement
31435 objc-finally-clause:
31436 @finally compound-statement
31438 objc-exception-declaration:
31439 parameter-declaration
31442 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31446 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31447 for C. Keep them in sync. */
31450 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
31452 location_t location
;
31455 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
31456 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31457 objc_maybe_warn_exceptions (location
);
31458 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31459 node, lest it get absorbed into the surrounding block. */
31460 stmt
= push_stmt_list ();
31461 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31462 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
31464 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
31466 cp_parameter_declarator
*parm
;
31467 tree parameter_declaration
= error_mark_node
;
31468 bool seen_open_paren
= false;
31469 matching_parens parens
;
31471 cp_lexer_consume_token (parser
->lexer
);
31472 if (parens
.require_open (parser
))
31473 seen_open_paren
= true;
31474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
31476 /* We have "@catch (...)" (where the '...' are literally
31477 what is in the code). Skip the '...'.
31478 parameter_declaration is set to NULL_TREE, and
31479 objc_being_catch_clauses() knows that that means
31481 cp_lexer_consume_token (parser
->lexer
);
31482 parameter_declaration
= NULL_TREE
;
31486 /* We have "@catch (NSException *exception)" or something
31487 like that. Parse the parameter declaration. */
31488 parm
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
31491 parameter_declaration
= error_mark_node
;
31493 parameter_declaration
= grokdeclarator (parm
->declarator
,
31494 &parm
->decl_specifiers
,
31495 PARM
, /*initialized=*/0,
31496 /*attrlist=*/NULL
);
31498 if (seen_open_paren
)
31499 parens
.require_close (parser
);
31502 /* If there was no open parenthesis, we are recovering from
31503 an error, and we are trying to figure out what mistake
31504 the user has made. */
31506 /* If there is an immediate closing parenthesis, the user
31507 probably forgot the opening one (ie, they typed "@catch
31508 NSException *e)". Parse the closing parenthesis and keep
31510 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31511 cp_lexer_consume_token (parser
->lexer
);
31513 /* If these is no immediate closing parenthesis, the user
31514 probably doesn't know that parenthesis are required at
31515 all (ie, they typed "@catch NSException *e"). So, just
31516 forget about the closing parenthesis and keep going. */
31518 objc_begin_catch_clause (parameter_declaration
);
31519 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31520 objc_finish_catch_clause ();
31522 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
31524 cp_lexer_consume_token (parser
->lexer
);
31525 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31526 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31527 node, lest it get absorbed into the surrounding block. */
31528 stmt
= push_stmt_list ();
31529 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31530 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
31533 return objc_finish_try_stmt ();
31536 /* Parse an Objective-C synchronized statement.
31538 objc-synchronized-stmt:
31539 @synchronized ( expression ) compound-statement
31541 Returns NULL_TREE. */
31544 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
31546 location_t location
;
31549 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
31551 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31552 objc_maybe_warn_exceptions (location
);
31553 matching_parens parens
;
31554 parens
.require_open (parser
);
31555 lock
= cp_parser_expression (parser
);
31556 parens
.require_close (parser
);
31558 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31559 node, lest it get absorbed into the surrounding block. */
31560 stmt
= push_stmt_list ();
31561 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31563 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
31566 /* Parse an Objective-C throw statement.
31569 @throw assignment-expression [opt] ;
31571 Returns a constructed '@throw' statement. */
31574 cp_parser_objc_throw_statement (cp_parser
*parser
)
31576 tree expr
= NULL_TREE
;
31577 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31579 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
31581 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31582 expr
= cp_parser_expression (parser
);
31584 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31586 return objc_build_throw_stmt (loc
, expr
);
31589 /* Parse an Objective-C statement. */
31592 cp_parser_objc_statement (cp_parser
* parser
)
31594 /* Try to figure out what kind of declaration is present. */
31595 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31597 switch (kwd
->keyword
)
31600 return cp_parser_objc_try_catch_finally_statement (parser
);
31601 case RID_AT_SYNCHRONIZED
:
31602 return cp_parser_objc_synchronized_statement (parser
);
31604 return cp_parser_objc_throw_statement (parser
);
31606 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31608 cp_parser_skip_to_end_of_block_or_statement (parser
);
31611 return error_mark_node
;
31614 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31615 look ahead to see if an objc keyword follows the attributes. This
31616 is to detect the use of prefix attributes on ObjC @interface and
31620 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
31622 cp_lexer_save_tokens (parser
->lexer
);
31623 *attrib
= cp_parser_attributes_opt (parser
);
31624 gcc_assert (*attrib
);
31625 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
31627 cp_lexer_commit_tokens (parser
->lexer
);
31630 cp_lexer_rollback_tokens (parser
->lexer
);
31634 /* This routine is a minimal replacement for
31635 c_parser_struct_declaration () used when parsing the list of
31636 types/names or ObjC++ properties. For example, when parsing the
31639 @property (readonly) int a, b, c;
31641 this function is responsible for parsing "int a, int b, int c" and
31642 returning the declarations as CHAIN of DECLs.
31644 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31645 similar parsing. */
31647 cp_parser_objc_struct_declaration (cp_parser
*parser
)
31649 tree decls
= NULL_TREE
;
31650 cp_decl_specifier_seq declspecs
;
31651 int decl_class_or_enum_p
;
31652 tree prefix_attributes
;
31654 cp_parser_decl_specifier_seq (parser
,
31655 CP_PARSER_FLAGS_NONE
,
31657 &decl_class_or_enum_p
);
31659 if (declspecs
.type
== error_mark_node
)
31660 return error_mark_node
;
31662 /* auto, register, static, extern, mutable. */
31663 if (declspecs
.storage_class
!= sc_none
)
31665 cp_parser_error (parser
, "invalid type for property");
31666 declspecs
.storage_class
= sc_none
;
31669 /* thread_local. */
31670 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31672 cp_parser_error (parser
, "invalid type for property");
31673 declspecs
.locations
[ds_thread
] = 0;
31677 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31679 cp_parser_error (parser
, "invalid type for property");
31680 declspecs
.locations
[ds_typedef
] = 0;
31683 prefix_attributes
= declspecs
.attributes
;
31684 declspecs
.attributes
= NULL_TREE
;
31686 /* Keep going until we hit the `;' at the end of the declaration. */
31687 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31689 tree attributes
, first_attribute
, decl
;
31690 cp_declarator
*declarator
;
31693 /* Parse the declarator. */
31694 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31695 CP_PARSER_FLAGS_NONE
,
31696 NULL
, NULL
, false, false, false);
31698 /* Look for attributes that apply to the ivar. */
31699 attributes
= cp_parser_attributes_opt (parser
);
31700 /* Remember which attributes are prefix attributes and
31702 first_attribute
= attributes
;
31703 /* Combine the attributes. */
31704 attributes
= attr_chainon (prefix_attributes
, attributes
);
31706 decl
= grokfield (declarator
, &declspecs
,
31707 NULL_TREE
, /*init_const_expr_p=*/false,
31708 NULL_TREE
, attributes
);
31710 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31711 return error_mark_node
;
31713 /* Reset PREFIX_ATTRIBUTES. */
31714 if (attributes
!= error_mark_node
)
31716 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31717 attributes
= TREE_CHAIN (attributes
);
31719 TREE_CHAIN (attributes
) = NULL_TREE
;
31722 DECL_CHAIN (decl
) = decls
;
31725 token
= cp_lexer_peek_token (parser
->lexer
);
31726 if (token
->type
== CPP_COMMA
)
31728 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31737 /* Parse an Objective-C @property declaration. The syntax is:
31739 objc-property-declaration:
31740 '@property' objc-property-attributes[opt] struct-declaration ;
31742 objc-property-attributes:
31743 '(' objc-property-attribute-list ')'
31745 objc-property-attribute-list:
31746 objc-property-attribute
31747 objc-property-attribute-list, objc-property-attribute
31749 objc-property-attribute
31750 'getter' = identifier
31751 'setter' = identifier
31760 @property NSString *name;
31761 @property (readonly) id object;
31762 @property (retain, nonatomic, getter=getTheName) id name;
31763 @property int a, b, c;
31765 PS: This function is identical to
31766 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31768 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31770 /* The following variables hold the attributes of the properties as
31771 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31772 seen. When we see an attribute, we set them to 'true' (if they
31773 are boolean properties) or to the identifier (if they have an
31774 argument, ie, for getter and setter). Note that here we only
31775 parse the list of attributes, check the syntax and accumulate the
31776 attributes that we find. objc_add_property_declaration() will
31777 then process the information. */
31778 bool property_assign
= false;
31779 bool property_copy
= false;
31780 tree property_getter_ident
= NULL_TREE
;
31781 bool property_nonatomic
= false;
31782 bool property_readonly
= false;
31783 bool property_readwrite
= false;
31784 bool property_retain
= false;
31785 tree property_setter_ident
= NULL_TREE
;
31787 /* 'properties' is the list of properties that we read. Usually a
31788 single one, but maybe more (eg, in "@property int a, b, c;" there
31793 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31795 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31797 /* Parse the optional attribute list... */
31798 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31801 matching_parens parens
;
31802 parens
.consume_open (parser
);
31806 bool syntax_error
= false;
31807 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31810 if (token
->type
!= CPP_NAME
)
31812 cp_parser_error (parser
, "expected identifier");
31815 keyword
= C_RID_CODE (token
->u
.value
);
31816 cp_lexer_consume_token (parser
->lexer
);
31819 case RID_ASSIGN
: property_assign
= true; break;
31820 case RID_COPY
: property_copy
= true; break;
31821 case RID_NONATOMIC
: property_nonatomic
= true; break;
31822 case RID_READONLY
: property_readonly
= true; break;
31823 case RID_READWRITE
: property_readwrite
= true; break;
31824 case RID_RETAIN
: property_retain
= true; break;
31828 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31830 if (keyword
== RID_GETTER
)
31831 cp_parser_error (parser
,
31832 "missing %<=%> (after %<getter%> attribute)");
31834 cp_parser_error (parser
,
31835 "missing %<=%> (after %<setter%> attribute)");
31836 syntax_error
= true;
31839 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31840 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31842 cp_parser_error (parser
, "expected identifier");
31843 syntax_error
= true;
31846 if (keyword
== RID_SETTER
)
31848 if (property_setter_ident
!= NULL_TREE
)
31850 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31851 cp_lexer_consume_token (parser
->lexer
);
31854 property_setter_ident
= cp_parser_objc_selector (parser
);
31855 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31856 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31858 cp_lexer_consume_token (parser
->lexer
);
31862 if (property_getter_ident
!= NULL_TREE
)
31864 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31865 cp_lexer_consume_token (parser
->lexer
);
31868 property_getter_ident
= cp_parser_objc_selector (parser
);
31872 cp_parser_error (parser
, "unknown property attribute");
31873 syntax_error
= true;
31880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31881 cp_lexer_consume_token (parser
->lexer
);
31886 /* FIXME: "@property (setter, assign);" will generate a spurious
31887 "error: expected ‘)’ before ‘,’ token". This is because
31888 cp_parser_require, unlike the C counterpart, will produce an
31889 error even if we are in error recovery. */
31890 if (!parens
.require_close (parser
))
31892 cp_parser_skip_to_closing_parenthesis (parser
,
31893 /*recovering=*/true,
31894 /*or_comma=*/false,
31895 /*consume_paren=*/true);
31899 /* ... and the property declaration(s). */
31900 properties
= cp_parser_objc_struct_declaration (parser
);
31902 if (properties
== error_mark_node
)
31904 cp_parser_skip_to_end_of_statement (parser
);
31905 /* If the next token is now a `;', consume it. */
31906 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31907 cp_lexer_consume_token (parser
->lexer
);
31911 if (properties
== NULL_TREE
)
31912 cp_parser_error (parser
, "expected identifier");
31915 /* Comma-separated properties are chained together in
31916 reverse order; add them one by one. */
31917 properties
= nreverse (properties
);
31919 for (; properties
; properties
= TREE_CHAIN (properties
))
31920 objc_add_property_declaration (loc
, copy_node (properties
),
31921 property_readonly
, property_readwrite
,
31922 property_assign
, property_retain
,
31923 property_copy
, property_nonatomic
,
31924 property_getter_ident
, property_setter_ident
);
31927 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31930 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31932 objc-synthesize-declaration:
31933 @synthesize objc-synthesize-identifier-list ;
31935 objc-synthesize-identifier-list:
31936 objc-synthesize-identifier
31937 objc-synthesize-identifier-list, objc-synthesize-identifier
31939 objc-synthesize-identifier
31941 identifier = identifier
31944 @synthesize MyProperty;
31945 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31947 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31948 for C. Keep them in sync.
31951 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31953 tree list
= NULL_TREE
;
31955 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31957 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31960 tree property
, ivar
;
31961 property
= cp_parser_identifier (parser
);
31962 if (property
== error_mark_node
)
31964 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31969 cp_lexer_consume_token (parser
->lexer
);
31970 ivar
= cp_parser_identifier (parser
);
31971 if (ivar
== error_mark_node
)
31973 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31979 list
= chainon (list
, build_tree_list (ivar
, property
));
31980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31981 cp_lexer_consume_token (parser
->lexer
);
31985 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31986 objc_add_synthesize_declaration (loc
, list
);
31989 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31991 objc-dynamic-declaration:
31992 @dynamic identifier-list ;
31995 @dynamic MyProperty;
31996 @dynamic MyProperty, AnotherProperty;
31998 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31999 for C. Keep them in sync.
32002 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
32004 tree list
= NULL_TREE
;
32006 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32008 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
32012 property
= cp_parser_identifier (parser
);
32013 if (property
== error_mark_node
)
32015 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32018 list
= chainon (list
, build_tree_list (NULL
, property
));
32019 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32020 cp_lexer_consume_token (parser
->lexer
);
32024 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32025 objc_add_dynamic_declaration (loc
, list
);
32029 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32031 /* Returns name of the next clause.
32032 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32033 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32034 returned and the token is consumed. */
32036 static pragma_omp_clause
32037 cp_parser_omp_clause_name (cp_parser
*parser
)
32039 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
32041 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32042 result
= PRAGMA_OACC_CLAUSE_AUTO
;
32043 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
32044 result
= PRAGMA_OMP_CLAUSE_IF
;
32045 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
32046 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
32047 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
32048 result
= PRAGMA_OACC_CLAUSE_DELETE
;
32049 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
32050 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
32051 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
32052 result
= PRAGMA_OMP_CLAUSE_FOR
;
32053 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32055 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32056 const char *p
= IDENTIFIER_POINTER (id
);
32061 if (!strcmp ("aligned", p
))
32062 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
32063 else if (!strcmp ("async", p
))
32064 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
32067 if (!strcmp ("collapse", p
))
32068 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
32069 else if (!strcmp ("copy", p
))
32070 result
= PRAGMA_OACC_CLAUSE_COPY
;
32071 else if (!strcmp ("copyin", p
))
32072 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
32073 else if (!strcmp ("copyout", p
))
32074 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
32075 else if (!strcmp ("copyprivate", p
))
32076 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
32077 else if (!strcmp ("create", p
))
32078 result
= PRAGMA_OACC_CLAUSE_CREATE
;
32081 if (!strcmp ("defaultmap", p
))
32082 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
32083 else if (!strcmp ("depend", p
))
32084 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
32085 else if (!strcmp ("device", p
))
32086 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
32087 else if (!strcmp ("deviceptr", p
))
32088 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
32089 else if (!strcmp ("device_resident", p
))
32090 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
32091 else if (!strcmp ("dist_schedule", p
))
32092 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
32095 if (!strcmp ("final", p
))
32096 result
= PRAGMA_OMP_CLAUSE_FINAL
;
32097 else if (!strcmp ("finalize", p
))
32098 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
32099 else if (!strcmp ("firstprivate", p
))
32100 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
32101 else if (!strcmp ("from", p
))
32102 result
= PRAGMA_OMP_CLAUSE_FROM
;
32105 if (!strcmp ("gang", p
))
32106 result
= PRAGMA_OACC_CLAUSE_GANG
;
32107 else if (!strcmp ("grainsize", p
))
32108 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
32111 if (!strcmp ("hint", p
))
32112 result
= PRAGMA_OMP_CLAUSE_HINT
;
32113 else if (!strcmp ("host", p
))
32114 result
= PRAGMA_OACC_CLAUSE_HOST
;
32117 if (!strcmp ("if_present", p
))
32118 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
32119 else if (!strcmp ("in_reduction", p
))
32120 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
32121 else if (!strcmp ("inbranch", p
))
32122 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
32123 else if (!strcmp ("independent", p
))
32124 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
32125 else if (!strcmp ("is_device_ptr", p
))
32126 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
32129 if (!strcmp ("lastprivate", p
))
32130 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
32131 else if (!strcmp ("linear", p
))
32132 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
32133 else if (!strcmp ("link", p
))
32134 result
= PRAGMA_OMP_CLAUSE_LINK
;
32137 if (!strcmp ("map", p
))
32138 result
= PRAGMA_OMP_CLAUSE_MAP
;
32139 else if (!strcmp ("mergeable", p
))
32140 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
32143 if (!strcmp ("nogroup", p
))
32144 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
32145 else if (!strcmp ("nontemporal", p
))
32146 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
32147 else if (!strcmp ("notinbranch", p
))
32148 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
32149 else if (!strcmp ("nowait", p
))
32150 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
32151 else if (!strcmp ("num_gangs", p
))
32152 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
32153 else if (!strcmp ("num_tasks", p
))
32154 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
32155 else if (!strcmp ("num_teams", p
))
32156 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
32157 else if (!strcmp ("num_threads", p
))
32158 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
32159 else if (!strcmp ("num_workers", p
))
32160 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
32163 if (!strcmp ("ordered", p
))
32164 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
32167 if (!strcmp ("parallel", p
))
32168 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
32169 else if (!strcmp ("present", p
))
32170 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
32171 else if (!strcmp ("present_or_copy", p
)
32172 || !strcmp ("pcopy", p
))
32173 result
= PRAGMA_OACC_CLAUSE_COPY
;
32174 else if (!strcmp ("present_or_copyin", p
)
32175 || !strcmp ("pcopyin", p
))
32176 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
32177 else if (!strcmp ("present_or_copyout", p
)
32178 || !strcmp ("pcopyout", p
))
32179 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
32180 else if (!strcmp ("present_or_create", p
)
32181 || !strcmp ("pcreate", p
))
32182 result
= PRAGMA_OACC_CLAUSE_CREATE
;
32183 else if (!strcmp ("priority", p
))
32184 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
32185 else if (!strcmp ("proc_bind", p
))
32186 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
32189 if (!strcmp ("reduction", p
))
32190 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
32193 if (!strcmp ("safelen", p
))
32194 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
32195 else if (!strcmp ("schedule", p
))
32196 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
32197 else if (!strcmp ("sections", p
))
32198 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
32199 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
32200 result
= PRAGMA_OACC_CLAUSE_HOST
;
32201 else if (!strcmp ("seq", p
))
32202 result
= PRAGMA_OACC_CLAUSE_SEQ
;
32203 else if (!strcmp ("shared", p
))
32204 result
= PRAGMA_OMP_CLAUSE_SHARED
;
32205 else if (!strcmp ("simd", p
))
32206 result
= PRAGMA_OMP_CLAUSE_SIMD
;
32207 else if (!strcmp ("simdlen", p
))
32208 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
32211 if (!strcmp ("task_reduction", p
))
32212 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
32213 else if (!strcmp ("taskgroup", p
))
32214 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
32215 else if (!strcmp ("thread_limit", p
))
32216 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
32217 else if (!strcmp ("threads", p
))
32218 result
= PRAGMA_OMP_CLAUSE_THREADS
;
32219 else if (!strcmp ("tile", p
))
32220 result
= PRAGMA_OACC_CLAUSE_TILE
;
32221 else if (!strcmp ("to", p
))
32222 result
= PRAGMA_OMP_CLAUSE_TO
;
32225 if (!strcmp ("uniform", p
))
32226 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
32227 else if (!strcmp ("untied", p
))
32228 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
32229 else if (!strcmp ("use_device", p
))
32230 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
32231 else if (!strcmp ("use_device_ptr", p
))
32232 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
32235 if (!strcmp ("vector", p
))
32236 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
32237 else if (!strcmp ("vector_length", p
))
32238 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
32241 if (!strcmp ("wait", p
))
32242 result
= PRAGMA_OACC_CLAUSE_WAIT
;
32243 else if (!strcmp ("worker", p
))
32244 result
= PRAGMA_OACC_CLAUSE_WORKER
;
32249 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
32250 cp_lexer_consume_token (parser
->lexer
);
32255 /* Validate that a clause of the given type does not already exist. */
32258 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
32259 const char *name
, location_t location
)
32263 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32264 if (OMP_CLAUSE_CODE (c
) == code
)
32266 error_at (location
, "too many %qs clauses", name
);
32274 variable-list , identifier
32276 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32277 colon). An opening parenthesis will have been consumed by the caller.
32279 If KIND is nonzero, create the appropriate node and install the decl
32280 in OMP_CLAUSE_DECL and add the node to the head of the list.
32282 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32283 return the list created.
32285 COLON can be NULL if only closing parenthesis should end the list,
32286 or pointer to bool which will receive false if the list is terminated
32287 by closing parenthesis or true if the list is terminated by colon. */
32290 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
32291 tree list
, bool *colon
)
32294 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32297 parser
->colon_corrects_to_scope_p
= false;
32304 if (kind
== OMP_CLAUSE_DEPEND
)
32305 cp_parser_parse_tentatively (parser
);
32306 token
= cp_lexer_peek_token (parser
->lexer
);
32308 && current_class_ptr
32309 && cp_parser_is_keyword (token
, RID_THIS
))
32311 decl
= finish_this_expr ();
32312 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
32313 || CONVERT_EXPR_P (decl
))
32314 decl
= TREE_OPERAND (decl
, 0);
32315 cp_lexer_consume_token (parser
->lexer
);
32319 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
32320 /*check_dependency_p=*/true,
32321 /*template_p=*/NULL
,
32322 /*declarator_p=*/false,
32323 /*optional_p=*/false);
32324 if (name
== error_mark_node
)
32326 if (kind
== OMP_CLAUSE_DEPEND
32327 && cp_parser_simulate_error (parser
))
32328 goto depend_lvalue
;
32332 if (identifier_p (name
))
32333 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
32336 if (decl
== error_mark_node
)
32338 if (kind
== OMP_CLAUSE_DEPEND
32339 && cp_parser_simulate_error (parser
))
32340 goto depend_lvalue
;
32341 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
32345 if (decl
== error_mark_node
)
32347 else if (kind
!= 0)
32351 case OMP_CLAUSE__CACHE_
:
32352 /* The OpenACC cache directive explicitly only allows "array
32353 elements or subarrays". */
32354 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
32356 error_at (token
->location
, "expected %<[%>");
32357 decl
= error_mark_node
;
32361 case OMP_CLAUSE_MAP
:
32362 case OMP_CLAUSE_FROM
:
32363 case OMP_CLAUSE_TO
:
32364 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
32367 = cp_lexer_peek_token (parser
->lexer
)->location
;
32368 cp_id_kind idk
= CP_ID_KIND_NONE
;
32369 cp_lexer_consume_token (parser
->lexer
);
32370 decl
= convert_from_reference (decl
);
32372 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
32377 case OMP_CLAUSE_DEPEND
:
32378 case OMP_CLAUSE_REDUCTION
:
32379 case OMP_CLAUSE_IN_REDUCTION
:
32380 case OMP_CLAUSE_TASK_REDUCTION
:
32381 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
32383 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
32385 parser
->colon_corrects_to_scope_p
= false;
32386 cp_lexer_consume_token (parser
->lexer
);
32387 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32388 low_bound
= cp_parser_expression (parser
);
32390 parser
->colon_corrects_to_scope_p
32391 = saved_colon_corrects_to_scope_p
;
32392 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
32393 length
= integer_one_node
;
32396 /* Look for `:'. */
32397 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32399 if (kind
== OMP_CLAUSE_DEPEND
32400 && cp_parser_simulate_error (parser
))
32401 goto depend_lvalue
;
32404 if (kind
== OMP_CLAUSE_DEPEND
)
32405 cp_parser_commit_to_tentative_parse (parser
);
32406 if (!cp_lexer_next_token_is (parser
->lexer
,
32408 length
= cp_parser_expression (parser
);
32410 /* Look for the closing `]'. */
32411 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
32414 if (kind
== OMP_CLAUSE_DEPEND
32415 && cp_parser_simulate_error (parser
))
32416 goto depend_lvalue
;
32420 decl
= tree_cons (low_bound
, length
, decl
);
32427 if (kind
== OMP_CLAUSE_DEPEND
)
32429 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
32430 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
32431 && cp_parser_simulate_error (parser
))
32434 cp_parser_abort_tentative_parse (parser
);
32435 decl
= cp_parser_assignment_expression (parser
, NULL
,
32439 cp_parser_parse_definitely (parser
);
32442 tree u
= build_omp_clause (token
->location
, kind
);
32443 OMP_CLAUSE_DECL (u
) = decl
;
32444 OMP_CLAUSE_CHAIN (u
) = list
;
32448 list
= tree_cons (decl
, NULL_TREE
, list
);
32451 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
32453 cp_lexer_consume_token (parser
->lexer
);
32457 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32459 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32462 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32466 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32470 /* Try to resync to an unnested comma. Copied from
32471 cp_parser_parenthesized_expression_list. */
32474 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32475 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
32476 /*recovering=*/true,
32478 /*consume_paren=*/true);
32486 /* Similarly, but expect leading and trailing parenthesis. This is a very
32487 common case for omp clauses. */
32490 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
32492 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32493 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
32498 copy ( variable-list )
32499 copyin ( variable-list )
32500 copyout ( variable-list )
32501 create ( variable-list )
32502 delete ( variable-list )
32503 present ( variable-list ) */
32506 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
32509 enum gomp_map_kind kind
;
32512 case PRAGMA_OACC_CLAUSE_COPY
:
32513 kind
= GOMP_MAP_TOFROM
;
32515 case PRAGMA_OACC_CLAUSE_COPYIN
:
32516 kind
= GOMP_MAP_TO
;
32518 case PRAGMA_OACC_CLAUSE_COPYOUT
:
32519 kind
= GOMP_MAP_FROM
;
32521 case PRAGMA_OACC_CLAUSE_CREATE
:
32522 kind
= GOMP_MAP_ALLOC
;
32524 case PRAGMA_OACC_CLAUSE_DELETE
:
32525 kind
= GOMP_MAP_RELEASE
;
32527 case PRAGMA_OACC_CLAUSE_DEVICE
:
32528 kind
= GOMP_MAP_FORCE_TO
;
32530 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
32531 kind
= GOMP_MAP_DEVICE_RESIDENT
;
32533 case PRAGMA_OACC_CLAUSE_HOST
:
32534 kind
= GOMP_MAP_FORCE_FROM
;
32536 case PRAGMA_OACC_CLAUSE_LINK
:
32537 kind
= GOMP_MAP_LINK
;
32539 case PRAGMA_OACC_CLAUSE_PRESENT
:
32540 kind
= GOMP_MAP_FORCE_PRESENT
;
32543 gcc_unreachable ();
32546 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
32548 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32549 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32555 deviceptr ( variable-list ) */
32558 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
32560 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32563 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32564 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32565 variable-list must only allow for pointer variables. */
32566 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
32567 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
32569 tree v
= TREE_PURPOSE (t
);
32570 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
32571 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
32572 OMP_CLAUSE_DECL (u
) = v
;
32573 OMP_CLAUSE_CHAIN (u
) = list
;
32588 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
32589 enum omp_clause_code code
,
32590 tree list
, location_t location
)
32592 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32593 tree c
= build_omp_clause (location
, code
);
32594 OMP_CLAUSE_CHAIN (c
) = list
;
32599 num_gangs ( expression )
32600 num_workers ( expression )
32601 vector_length ( expression ) */
32604 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
32605 const char *str
, tree list
)
32607 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32609 matching_parens parens
;
32610 if (!parens
.require_open (parser
))
32613 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
32615 if (t
== error_mark_node
32616 || !parens
.require_close (parser
))
32618 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32619 /*or_comma=*/false,
32620 /*consume_paren=*/true);
32624 check_no_duplicate_clause (list
, code
, str
, loc
);
32626 tree c
= build_omp_clause (loc
, code
);
32627 OMP_CLAUSE_OPERAND (c
, 0) = t
;
32628 OMP_CLAUSE_CHAIN (c
) = list
;
32634 gang [( gang-arg-list )]
32635 worker [( [num:] int-expr )]
32636 vector [( [length:] int-expr )]
32638 where gang-arg is one of:
32643 and size-expr may be:
32650 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
32651 const char *str
, tree list
)
32653 const char *id
= "num";
32654 cp_lexer
*lexer
= parser
->lexer
;
32655 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
32656 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
32658 if (kind
== OMP_CLAUSE_VECTOR
)
32661 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
32663 matching_parens parens
;
32664 parens
.consume_open (parser
);
32668 cp_token
*next
= cp_lexer_peek_token (lexer
);
32671 /* Gang static argument. */
32672 if (kind
== OMP_CLAUSE_GANG
32673 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
32675 cp_lexer_consume_token (lexer
);
32677 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32678 goto cleanup_error
;
32681 if (ops
[idx
] != NULL
)
32683 cp_parser_error (parser
, "too many %<static%> arguments");
32684 goto cleanup_error
;
32687 /* Check for the '*' argument. */
32688 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
32689 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32690 || cp_lexer_nth_token_is (parser
->lexer
, 2,
32693 cp_lexer_consume_token (lexer
);
32694 ops
[idx
] = integer_minus_one_node
;
32696 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32698 cp_lexer_consume_token (lexer
);
32704 /* Worker num: argument and vector length: arguments. */
32705 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
32706 && id_equal (next
->u
.value
, id
)
32707 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
32709 cp_lexer_consume_token (lexer
); /* id */
32710 cp_lexer_consume_token (lexer
); /* ':' */
32713 /* Now collect the actual argument. */
32714 if (ops
[idx
] != NULL_TREE
)
32716 cp_parser_error (parser
, "unexpected argument");
32717 goto cleanup_error
;
32720 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
32722 if (expr
== error_mark_node
)
32723 goto cleanup_error
;
32725 mark_exp_read (expr
);
32728 if (kind
== OMP_CLAUSE_GANG
32729 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32731 cp_lexer_consume_token (lexer
);
32738 if (!parens
.require_close (parser
))
32739 goto cleanup_error
;
32742 check_no_duplicate_clause (list
, kind
, str
, loc
);
32744 c
= build_omp_clause (loc
, kind
);
32747 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32749 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32750 OMP_CLAUSE_CHAIN (c
) = list
;
32755 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32760 tile ( size-expr-list ) */
32763 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32765 tree c
, expr
= error_mark_node
;
32766 tree tile
= NULL_TREE
;
32768 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32769 so, but the spec authors never considered such a case and have
32770 differing opinions on what it might mean, including 'not
32772 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32773 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32776 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32781 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32784 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32785 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32786 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32788 cp_lexer_consume_token (parser
->lexer
);
32789 expr
= integer_zero_node
;
32792 expr
= cp_parser_constant_expression (parser
);
32794 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32796 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32798 /* Consume the trailing ')'. */
32799 cp_lexer_consume_token (parser
->lexer
);
32801 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32802 tile
= nreverse (tile
);
32803 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32804 OMP_CLAUSE_CHAIN (c
) = list
;
32809 Parse wait clause or directive parameters. */
32812 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32814 vec
<tree
, va_gc
> *args
;
32817 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32819 /*allow_expansion_p=*/true,
32820 /*non_constant_p=*/NULL
);
32822 if (args
== NULL
|| args
->length () == 0)
32826 cp_parser_error (parser
, "expected integer expression list");
32827 release_tree_vector (args
);
32832 args_tree
= build_tree_list_vec (args
);
32834 release_tree_vector (args
);
32836 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32838 tree targ
= TREE_VALUE (t
);
32840 if (targ
!= error_mark_node
)
32842 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32843 error ("%<wait%> expression must be integral");
32846 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32848 targ
= mark_rvalue_use (targ
);
32849 OMP_CLAUSE_DECL (c
) = targ
;
32850 OMP_CLAUSE_CHAIN (c
) = list
;
32860 wait ( int-expr-list ) */
32863 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32865 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32867 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32870 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32876 collapse ( constant-expression ) */
32879 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32885 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32886 matching_parens parens
;
32887 if (!parens
.require_open (parser
))
32890 num
= cp_parser_constant_expression (parser
);
32892 if (!parens
.require_close (parser
))
32893 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32894 /*or_comma=*/false,
32895 /*consume_paren=*/true);
32897 if (num
== error_mark_node
)
32899 num
= fold_non_dependent_expr (num
);
32900 if (!tree_fits_shwi_p (num
)
32901 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32902 || (n
= tree_to_shwi (num
)) <= 0
32905 error_at (loc
, "collapse argument needs positive constant integer expression");
32909 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32910 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32911 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32912 OMP_CLAUSE_CHAIN (c
) = list
;
32913 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32919 default ( none | shared )
32922 default ( none | present ) */
32925 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32926 location_t location
, bool is_oacc
)
32928 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32931 matching_parens parens
;
32932 if (!parens
.require_open (parser
))
32934 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32936 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32937 const char *p
= IDENTIFIER_POINTER (id
);
32942 if (strcmp ("none", p
) != 0)
32944 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32948 if (strcmp ("present", p
) != 0 || !is_oacc
)
32950 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32954 if (strcmp ("shared", p
) != 0 || is_oacc
)
32956 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32963 cp_lexer_consume_token (parser
->lexer
);
32969 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32971 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32974 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32975 || !parens
.require_close (parser
))
32976 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32977 /*or_comma=*/false,
32978 /*consume_paren=*/true);
32980 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32983 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32984 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32985 OMP_CLAUSE_CHAIN (c
) = list
;
32986 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32992 final ( expression ) */
32995 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32999 matching_parens parens
;
33000 if (!parens
.require_open (parser
))
33003 t
= cp_parser_assignment_expression (parser
);
33005 if (t
== error_mark_node
33006 || !parens
.require_close (parser
))
33007 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33008 /*or_comma=*/false,
33009 /*consume_paren=*/true);
33011 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
33013 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
33014 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
33015 OMP_CLAUSE_CHAIN (c
) = list
;
33024 if ( directive-name-modifier : expression )
33026 directive-name-modifier:
33027 parallel | task | taskloop | target data | target | target update
33028 | target enter data | target exit data
33031 directive-name-modifier:
33032 ... | simd | cancel */
33035 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
33039 enum tree_code if_modifier
= ERROR_MARK
;
33041 matching_parens parens
;
33042 if (!parens
.require_open (parser
))
33045 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33047 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33048 const char *p
= IDENTIFIER_POINTER (id
);
33051 if (strcmp ("cancel", p
) == 0)
33052 if_modifier
= VOID_CST
;
33053 else if (strcmp ("parallel", p
) == 0)
33054 if_modifier
= OMP_PARALLEL
;
33055 else if (strcmp ("simd", p
) == 0)
33056 if_modifier
= OMP_SIMD
;
33057 else if (strcmp ("task", p
) == 0)
33058 if_modifier
= OMP_TASK
;
33059 else if (strcmp ("taskloop", p
) == 0)
33060 if_modifier
= OMP_TASKLOOP
;
33061 else if (strcmp ("target", p
) == 0)
33063 if_modifier
= OMP_TARGET
;
33064 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
33066 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
33067 p
= IDENTIFIER_POINTER (id
);
33068 if (strcmp ("data", p
) == 0)
33069 if_modifier
= OMP_TARGET_DATA
;
33070 else if (strcmp ("update", p
) == 0)
33071 if_modifier
= OMP_TARGET_UPDATE
;
33072 else if (strcmp ("enter", p
) == 0)
33073 if_modifier
= OMP_TARGET_ENTER_DATA
;
33074 else if (strcmp ("exit", p
) == 0)
33075 if_modifier
= OMP_TARGET_EXIT_DATA
;
33076 if (if_modifier
!= OMP_TARGET
)
33081 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
33082 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
33084 if_modifier
= ERROR_MARK
;
33086 if (if_modifier
== OMP_TARGET_ENTER_DATA
33087 || if_modifier
== OMP_TARGET_EXIT_DATA
)
33089 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
33091 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
33092 p
= IDENTIFIER_POINTER (id
);
33093 if (strcmp ("data", p
) == 0)
33099 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
33100 error_at (loc
, "expected %<data%>");
33101 if_modifier
= ERROR_MARK
;
33106 if (if_modifier
!= ERROR_MARK
)
33108 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
33111 cp_lexer_consume_token (parser
->lexer
);
33118 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
33119 error_at (loc
, "expected %<:%>");
33121 if_modifier
= ERROR_MARK
;
33126 t
= cp_parser_assignment_expression (parser
);
33128 if (t
== error_mark_node
33129 || !parens
.require_close (parser
))
33130 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33131 /*or_comma=*/false,
33132 /*consume_paren=*/true);
33134 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33135 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
33137 if (if_modifier
!= ERROR_MARK
33138 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33140 const char *p
= NULL
;
33141 switch (if_modifier
)
33143 case VOID_CST
: p
= "cancel"; break;
33144 case OMP_PARALLEL
: p
= "parallel"; break;
33145 case OMP_SIMD
: p
= "simd"; break;
33146 case OMP_TASK
: p
= "task"; break;
33147 case OMP_TASKLOOP
: p
= "taskloop"; break;
33148 case OMP_TARGET_DATA
: p
= "target data"; break;
33149 case OMP_TARGET
: p
= "target"; break;
33150 case OMP_TARGET_UPDATE
: p
= "target update"; break;
33151 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
33152 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
33153 default: gcc_unreachable ();
33155 error_at (location
, "too many %<if%> clauses with %qs modifier",
33159 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33162 error_at (location
, "too many %<if%> clauses");
33164 error_at (location
, "too many %<if%> clauses without modifier");
33167 else if (if_modifier
== ERROR_MARK
33168 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
33170 error_at (location
, "if any %<if%> clause has modifier, then all "
33171 "%<if%> clauses have to use modifier");
33176 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
33177 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
33178 OMP_CLAUSE_IF_EXPR (c
) = t
;
33179 OMP_CLAUSE_CHAIN (c
) = list
;
33188 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
33189 tree list
, location_t location
)
33193 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
33196 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
33197 OMP_CLAUSE_CHAIN (c
) = list
;
33205 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
33206 tree list
, location_t location
)
33210 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
33212 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
33213 OMP_CLAUSE_CHAIN (c
) = list
;
33218 num_threads ( expression ) */
33221 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
33222 location_t location
)
33226 matching_parens parens
;
33227 if (!parens
.require_open (parser
))
33230 t
= cp_parser_assignment_expression (parser
);
33232 if (t
== error_mark_node
33233 || !parens
.require_close (parser
))
33234 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33235 /*or_comma=*/false,
33236 /*consume_paren=*/true);
33238 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
33239 "num_threads", location
);
33241 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
33242 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
33243 OMP_CLAUSE_CHAIN (c
) = list
;
33249 num_tasks ( expression ) */
33252 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
33253 location_t location
)
33257 matching_parens parens
;
33258 if (!parens
.require_open (parser
))
33261 t
= cp_parser_assignment_expression (parser
);
33263 if (t
== error_mark_node
33264 || !parens
.require_close (parser
))
33265 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33266 /*or_comma=*/false,
33267 /*consume_paren=*/true);
33269 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
33270 "num_tasks", location
);
33272 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
33273 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
33274 OMP_CLAUSE_CHAIN (c
) = list
;
33280 grainsize ( expression ) */
33283 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
33284 location_t location
)
33288 matching_parens parens
;
33289 if (!parens
.require_open (parser
))
33292 t
= cp_parser_assignment_expression (parser
);
33294 if (t
== error_mark_node
33295 || !parens
.require_close (parser
))
33296 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33297 /*or_comma=*/false,
33298 /*consume_paren=*/true);
33300 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
33301 "grainsize", location
);
33303 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
33304 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
33305 OMP_CLAUSE_CHAIN (c
) = list
;
33311 priority ( expression ) */
33314 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
33315 location_t location
)
33319 matching_parens parens
;
33320 if (!parens
.require_open (parser
))
33323 t
= cp_parser_assignment_expression (parser
);
33325 if (t
== error_mark_node
33326 || !parens
.require_close (parser
))
33327 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33328 /*or_comma=*/false,
33329 /*consume_paren=*/true);
33331 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
33332 "priority", location
);
33334 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
33335 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
33336 OMP_CLAUSE_CHAIN (c
) = list
;
33342 hint ( expression ) */
33345 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
33349 matching_parens parens
;
33350 if (!parens
.require_open (parser
))
33353 t
= cp_parser_assignment_expression (parser
);
33355 if (t
== error_mark_node
33356 || !parens
.require_close (parser
))
33357 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33358 /*or_comma=*/false,
33359 /*consume_paren=*/true);
33361 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
33363 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
33364 OMP_CLAUSE_HINT_EXPR (c
) = t
;
33365 OMP_CLAUSE_CHAIN (c
) = list
;
33371 defaultmap ( tofrom : scalar )
33374 defaultmap ( implicit-behavior [ : variable-category ] ) */
33377 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
33378 location_t location
)
33382 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33383 enum omp_clause_defaultmap_kind category
33384 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
33386 matching_parens parens
;
33387 if (!parens
.require_open (parser
))
33390 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
33392 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33395 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
33396 "%<tofrom%>, %<firstprivate%>, %<none%> "
33402 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33403 p
= IDENTIFIER_POINTER (id
);
33409 if (strcmp ("alloc", p
) == 0)
33410 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
33412 goto invalid_behavior
;
33416 if (strcmp ("default", p
) == 0)
33417 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33419 goto invalid_behavior
;
33423 if (strcmp ("firstprivate", p
) == 0)
33424 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
33425 else if (strcmp ("from", p
) == 0)
33426 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
33428 goto invalid_behavior
;
33432 if (strcmp ("none", p
) == 0)
33433 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
33435 goto invalid_behavior
;
33439 if (strcmp ("tofrom", p
) == 0)
33440 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
33441 else if (strcmp ("to", p
) == 0)
33442 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
33444 goto invalid_behavior
;
33448 goto invalid_behavior
;
33450 cp_lexer_consume_token (parser
->lexer
);
33452 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33454 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33457 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33460 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
33464 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33465 p
= IDENTIFIER_POINTER (id
);
33470 if (strcmp ("aggregate", p
) == 0)
33471 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
33473 goto invalid_category
;
33477 if (strcmp ("pointer", p
) == 0)
33478 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
33480 goto invalid_category
;
33484 if (strcmp ("scalar", p
) == 0)
33485 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
33487 goto invalid_category
;
33491 goto invalid_category
;
33494 cp_lexer_consume_token (parser
->lexer
);
33496 if (!parens
.require_close (parser
))
33499 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33500 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
33501 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33502 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
33503 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
33504 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
33506 enum omp_clause_defaultmap_kind cat
= category
;
33507 location_t loc
= OMP_CLAUSE_LOCATION (c
);
33508 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
33509 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
33513 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
33516 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
33519 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
33522 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
33526 gcc_unreachable ();
33529 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
33532 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
33537 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
33538 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
33539 OMP_CLAUSE_CHAIN (c
) = list
;
33543 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33544 /*or_comma=*/false,
33545 /*consume_paren=*/true);
33553 ordered ( constant-expression ) */
33556 cp_parser_omp_clause_ordered (cp_parser
*parser
,
33557 tree list
, location_t location
)
33559 tree c
, num
= NULL_TREE
;
33562 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
33563 "ordered", location
);
33565 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33567 matching_parens parens
;
33568 parens
.consume_open (parser
);
33570 num
= cp_parser_constant_expression (parser
);
33572 if (!parens
.require_close (parser
))
33573 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33574 /*or_comma=*/false,
33575 /*consume_paren=*/true);
33577 if (num
== error_mark_node
)
33579 num
= fold_non_dependent_expr (num
);
33580 if (!tree_fits_shwi_p (num
)
33581 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33582 || (n
= tree_to_shwi (num
)) <= 0
33585 error_at (location
,
33586 "ordered argument needs positive constant integer "
33592 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
33593 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
33594 OMP_CLAUSE_CHAIN (c
) = list
;
33599 reduction ( reduction-operator : variable-list )
33601 reduction-operator:
33602 One of: + * - & ^ | && ||
33606 reduction-operator:
33607 One of: + * - & ^ | && || min max
33611 reduction-operator:
33612 One of: + * - & ^ | && ||
33616 reduction ( reduction-modifier, reduction-operator : variable-list )
33617 in_reduction ( reduction-operator : variable-list )
33618 task_reduction ( reduction-operator : variable-list ) */
33621 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
33622 bool is_omp
, tree list
)
33624 enum tree_code code
= ERROR_MARK
;
33625 tree nlist
, c
, id
= NULL_TREE
;
33627 bool inscan
= false;
33629 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33632 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
33634 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
33635 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33637 cp_lexer_consume_token (parser
->lexer
);
33638 cp_lexer_consume_token (parser
->lexer
);
33640 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33641 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33643 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33644 const char *p
= IDENTIFIER_POINTER (id
);
33645 if (strcmp (p
, "task") == 0)
33647 else if (strcmp (p
, "inscan") == 0)
33650 sorry ("%<inscan%> modifier on %<reduction%> clause "
33651 "not supported yet");
33653 if (task
|| inscan
)
33655 cp_lexer_consume_token (parser
->lexer
);
33656 cp_lexer_consume_token (parser
->lexer
);
33661 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33663 case CPP_PLUS
: code
= PLUS_EXPR
; break;
33664 case CPP_MULT
: code
= MULT_EXPR
; break;
33665 case CPP_MINUS
: code
= MINUS_EXPR
; break;
33666 case CPP_AND
: code
= BIT_AND_EXPR
; break;
33667 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
33668 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
33669 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
33670 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
33674 if (code
!= ERROR_MARK
)
33675 cp_lexer_consume_token (parser
->lexer
);
33678 bool saved_colon_corrects_to_scope_p
;
33679 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33680 parser
->colon_corrects_to_scope_p
= false;
33681 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
33682 /*check_dependency_p=*/true,
33683 /*template_p=*/NULL
,
33684 /*declarator_p=*/false,
33685 /*optional_p=*/false);
33686 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33687 if (identifier_p (id
))
33689 const char *p
= IDENTIFIER_POINTER (id
);
33691 if (strcmp (p
, "min") == 0)
33693 else if (strcmp (p
, "max") == 0)
33695 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
33697 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
33699 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
33701 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
33702 code
= BIT_AND_EXPR
;
33703 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
33704 code
= BIT_IOR_EXPR
;
33705 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
33706 code
= BIT_XOR_EXPR
;
33707 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
33708 code
= TRUTH_ANDIF_EXPR
;
33709 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
33710 code
= TRUTH_ORIF_EXPR
;
33711 id
= omp_reduction_id (code
, id
, NULL_TREE
);
33712 tree scope
= parser
->scope
;
33714 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
33715 parser
->scope
= NULL_TREE
;
33716 parser
->qualifying_scope
= NULL_TREE
;
33717 parser
->object_scope
= NULL_TREE
;
33721 error ("invalid reduction-identifier");
33723 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33724 /*or_comma=*/false,
33725 /*consume_paren=*/true);
33730 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33733 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
33735 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33737 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
33739 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
33741 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
33742 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
33749 schedule ( schedule-kind )
33750 schedule ( schedule-kind , expression )
33753 static | dynamic | guided | runtime | auto
33756 schedule ( schedule-modifier : schedule-kind )
33757 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33765 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
33768 int modifiers
= 0, nmodifiers
= 0;
33770 matching_parens parens
;
33771 if (!parens
.require_open (parser
))
33774 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
33776 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33778 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33779 const char *p
= IDENTIFIER_POINTER (id
);
33780 if (strcmp ("simd", p
) == 0)
33781 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
33782 else if (strcmp ("monotonic", p
) == 0)
33783 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
33784 else if (strcmp ("nonmonotonic", p
) == 0)
33785 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
33788 cp_lexer_consume_token (parser
->lexer
);
33789 if (nmodifiers
++ == 0
33790 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33791 cp_lexer_consume_token (parser
->lexer
);
33794 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33799 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33801 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33802 const char *p
= IDENTIFIER_POINTER (id
);
33807 if (strcmp ("dynamic", p
) != 0)
33809 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
33813 if (strcmp ("guided", p
) != 0)
33815 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
33819 if (strcmp ("runtime", p
) != 0)
33821 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
33828 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33829 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
33830 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
33831 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
33834 cp_lexer_consume_token (parser
->lexer
);
33836 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
33837 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33838 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33839 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33841 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33846 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33849 cp_lexer_consume_token (parser
->lexer
);
33851 token
= cp_lexer_peek_token (parser
->lexer
);
33852 t
= cp_parser_assignment_expression (parser
);
33854 if (t
== error_mark_node
)
33856 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
33857 error_at (token
->location
, "schedule %<runtime%> does not take "
33858 "a %<chunk_size%> parameter");
33859 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
33860 error_at (token
->location
, "schedule %<auto%> does not take "
33861 "a %<chunk_size%> parameter");
33863 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
33865 if (!parens
.require_close (parser
))
33868 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33871 OMP_CLAUSE_SCHEDULE_KIND (c
)
33872 = (enum omp_clause_schedule_kind
)
33873 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
33875 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
33876 OMP_CLAUSE_CHAIN (c
) = list
;
33880 cp_parser_error (parser
, "invalid schedule kind");
33882 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33883 /*or_comma=*/false,
33884 /*consume_paren=*/true);
33892 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
33893 tree list
, location_t location
)
33897 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
33899 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
33900 OMP_CLAUSE_CHAIN (c
) = list
;
33909 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
33910 tree list
, location_t location
)
33912 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33913 tree c
= build_omp_clause (location
, code
);
33914 OMP_CLAUSE_CHAIN (c
) = list
;
33925 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33926 enum omp_clause_code code
,
33927 tree list
, location_t location
)
33929 tree c
= build_omp_clause (location
, code
);
33930 OMP_CLAUSE_CHAIN (c
) = list
;
33938 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33939 tree list
, location_t location
)
33941 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33942 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33943 OMP_CLAUSE_CHAIN (c
) = list
;
33952 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33953 enum omp_clause_code code
,
33954 tree list
, location_t location
)
33956 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33957 tree c
= build_omp_clause (location
, code
);
33958 OMP_CLAUSE_CHAIN (c
) = list
;
33963 num_teams ( expression ) */
33966 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33967 location_t location
)
33971 matching_parens parens
;
33972 if (!parens
.require_open (parser
))
33975 t
= cp_parser_assignment_expression (parser
);
33977 if (t
== error_mark_node
33978 || !parens
.require_close (parser
))
33979 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33980 /*or_comma=*/false,
33981 /*consume_paren=*/true);
33983 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33984 "num_teams", location
);
33986 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33987 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33988 OMP_CLAUSE_CHAIN (c
) = list
;
33994 thread_limit ( expression ) */
33997 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33998 location_t location
)
34002 matching_parens parens
;
34003 if (!parens
.require_open (parser
))
34006 t
= cp_parser_assignment_expression (parser
);
34008 if (t
== error_mark_node
34009 || !parens
.require_close (parser
))
34010 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34011 /*or_comma=*/false,
34012 /*consume_paren=*/true);
34014 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
34015 "thread_limit", location
);
34017 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
34018 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
34019 OMP_CLAUSE_CHAIN (c
) = list
;
34025 aligned ( variable-list )
34026 aligned ( variable-list : constant-expression ) */
34029 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
34031 tree nlist
, c
, alignment
= NULL_TREE
;
34034 matching_parens parens
;
34035 if (!parens
.require_open (parser
))
34038 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
34043 alignment
= cp_parser_constant_expression (parser
);
34045 if (!parens
.require_close (parser
))
34046 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34047 /*or_comma=*/false,
34048 /*consume_paren=*/true);
34050 if (alignment
== error_mark_node
)
34051 alignment
= NULL_TREE
;
34054 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34055 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
34061 lastprivate ( variable-list )
34064 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34067 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
34069 bool conditional
= false;
34071 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34074 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34075 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
34077 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34078 const char *p
= IDENTIFIER_POINTER (id
);
34080 if (strcmp ("conditional", p
) == 0)
34082 conditional
= true;
34083 cp_lexer_consume_token (parser
->lexer
);
34084 cp_lexer_consume_token (parser
->lexer
);
34088 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
34092 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34093 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
34098 linear ( variable-list )
34099 linear ( variable-list : expression )
34102 linear ( modifier ( variable-list ) )
34103 linear ( modifier ( variable-list ) : expression ) */
34106 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
34109 tree nlist
, c
, step
= integer_one_node
;
34111 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
34113 matching_parens parens
;
34114 if (!parens
.require_open (parser
))
34117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34119 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34120 const char *p
= IDENTIFIER_POINTER (id
);
34122 if (strcmp ("ref", p
) == 0)
34123 kind
= OMP_CLAUSE_LINEAR_REF
;
34124 else if (strcmp ("val", p
) == 0)
34125 kind
= OMP_CLAUSE_LINEAR_VAL
;
34126 else if (strcmp ("uval", p
) == 0)
34127 kind
= OMP_CLAUSE_LINEAR_UVAL
;
34128 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
34129 cp_lexer_consume_token (parser
->lexer
);
34131 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
34134 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
34135 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
34139 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
34140 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
34142 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
34143 else if (!parens
.require_close (parser
))
34144 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34145 /*or_comma=*/false,
34146 /*consume_paren=*/true);
34153 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34154 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
34156 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34157 cp_parser_parse_tentatively (parser
);
34158 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
34159 /*check_dependency_p=*/true,
34160 /*template_p=*/NULL
,
34161 /*declarator_p=*/false,
34162 /*optional_p=*/false);
34163 if (step
!= error_mark_node
)
34164 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
34165 if (step
== error_mark_node
)
34168 cp_parser_abort_tentative_parse (parser
);
34170 else if (!cp_parser_parse_definitely (parser
))
34174 step
= cp_parser_assignment_expression (parser
);
34176 if (!parens
.require_close (parser
))
34177 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34178 /*or_comma=*/false,
34179 /*consume_paren=*/true);
34181 if (step
== error_mark_node
)
34185 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34187 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
34188 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
34195 safelen ( constant-expression ) */
34198 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
34199 location_t location
)
34203 matching_parens parens
;
34204 if (!parens
.require_open (parser
))
34207 t
= cp_parser_constant_expression (parser
);
34209 if (t
== error_mark_node
34210 || !parens
.require_close (parser
))
34211 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34212 /*or_comma=*/false,
34213 /*consume_paren=*/true);
34215 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
34217 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
34218 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
34219 OMP_CLAUSE_CHAIN (c
) = list
;
34225 simdlen ( constant-expression ) */
34228 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
34229 location_t location
)
34233 matching_parens parens
;
34234 if (!parens
.require_open (parser
))
34237 t
= cp_parser_constant_expression (parser
);
34239 if (t
== error_mark_node
34240 || !parens
.require_close (parser
))
34241 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34242 /*or_comma=*/false,
34243 /*consume_paren=*/true);
34245 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
34247 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
34248 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
34249 OMP_CLAUSE_CHAIN (c
) = list
;
34256 identifier [+/- integer]
34257 vec , identifier [+/- integer]
34261 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
34266 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34268 cp_parser_error (parser
, "expected identifier");
34272 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34274 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34275 tree t
, identifier
= cp_parser_identifier (parser
);
34276 tree addend
= NULL
;
34278 if (identifier
== error_mark_node
)
34279 t
= error_mark_node
;
34282 t
= cp_parser_lookup_name_simple
34283 (parser
, identifier
,
34284 cp_lexer_peek_token (parser
->lexer
)->location
);
34285 if (t
== error_mark_node
)
34286 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
34291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
34293 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
34295 addend
= integer_zero_node
;
34296 goto add_to_vector
;
34298 cp_lexer_consume_token (parser
->lexer
);
34300 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
34302 cp_parser_error (parser
, "expected integer");
34306 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34307 if (TREE_CODE (addend
) != INTEGER_CST
)
34309 cp_parser_error (parser
, "expected integer");
34312 cp_lexer_consume_token (parser
->lexer
);
34315 if (t
!= error_mark_node
)
34317 vec
= tree_cons (addend
, t
, vec
);
34319 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
34322 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
34325 cp_lexer_consume_token (parser
->lexer
);
34328 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
34330 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
34331 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
34332 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
34333 OMP_CLAUSE_CHAIN (u
) = list
;
34340 iterators ( iterators-definition )
34342 iterators-definition:
34344 iterator-specifier , iterators-definition
34346 iterator-specifier:
34347 identifier = range-specification
34348 iterator-type identifier = range-specification
34350 range-specification:
34352 begin : end : step */
34355 cp_parser_omp_iterators (cp_parser
*parser
)
34357 tree ret
= NULL_TREE
, *last
= &ret
;
34358 cp_lexer_consume_token (parser
->lexer
);
34360 matching_parens parens
;
34361 if (!parens
.require_open (parser
))
34362 return error_mark_node
;
34364 bool saved_colon_corrects_to_scope_p
34365 = parser
->colon_corrects_to_scope_p
;
34366 bool saved_colon_doesnt_start_class_def_p
34367 = parser
->colon_doesnt_start_class_def_p
;
34372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34373 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
34374 iter_type
= integer_type_node
;
34377 const char *saved_message
34378 = parser
->type_definition_forbidden_message
;
34379 parser
->type_definition_forbidden_message
34380 = G_("types may not be defined in iterator type");
34382 iter_type
= cp_parser_type_id (parser
);
34384 parser
->type_definition_forbidden_message
= saved_message
;
34387 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34388 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34390 cp_parser_error (parser
, "expected identifier");
34394 tree id
= cp_parser_identifier (parser
);
34395 if (id
== error_mark_node
)
34398 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34401 parser
->colon_corrects_to_scope_p
= false;
34402 parser
->colon_doesnt_start_class_def_p
= true;
34403 tree begin
= cp_parser_assignment_expression (parser
);
34405 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34408 tree end
= cp_parser_assignment_expression (parser
);
34410 tree step
= integer_one_node
;
34411 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
34413 cp_lexer_consume_token (parser
->lexer
);
34414 step
= cp_parser_assignment_expression (parser
);
34417 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
34418 DECL_ARTIFICIAL (iter_var
) = 1;
34419 DECL_CONTEXT (iter_var
) = current_function_decl
;
34420 pushdecl (iter_var
);
34422 *last
= make_tree_vec (6);
34423 TREE_VEC_ELT (*last
, 0) = iter_var
;
34424 TREE_VEC_ELT (*last
, 1) = begin
;
34425 TREE_VEC_ELT (*last
, 2) = end
;
34426 TREE_VEC_ELT (*last
, 3) = step
;
34427 last
= &TREE_CHAIN (*last
);
34429 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34431 cp_lexer_consume_token (parser
->lexer
);
34438 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34439 parser
->colon_doesnt_start_class_def_p
34440 = saved_colon_doesnt_start_class_def_p
;
34442 if (!parens
.require_close (parser
))
34443 cp_parser_skip_to_closing_parenthesis (parser
,
34444 /*recovering=*/true,
34445 /*or_comma=*/false,
34446 /*consume_paren=*/true);
34448 return ret
? ret
: error_mark_node
;
34452 depend ( depend-kind : variable-list )
34460 depend ( sink : vec )
34463 depend ( depend-modifier , depend-kind: variable-list )
34466 in | out | inout | mutexinoutset | depobj
34469 iterator ( iterators-definition ) */
34472 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
34474 tree nlist
, c
, iterators
= NULL_TREE
;
34475 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
34477 matching_parens parens
;
34478 if (!parens
.require_open (parser
))
34483 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34486 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34487 const char *p
= IDENTIFIER_POINTER (id
);
34489 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
34491 begin_scope (sk_omp
, NULL
);
34492 iterators
= cp_parser_omp_iterators (parser
);
34493 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
34496 if (strcmp ("in", p
) == 0)
34497 kind
= OMP_CLAUSE_DEPEND_IN
;
34498 else if (strcmp ("inout", p
) == 0)
34499 kind
= OMP_CLAUSE_DEPEND_INOUT
;
34500 else if (strcmp ("mutexinoutset", p
) == 0)
34501 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
34502 else if (strcmp ("out", p
) == 0)
34503 kind
= OMP_CLAUSE_DEPEND_OUT
;
34504 else if (strcmp ("depobj", p
) == 0)
34505 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
34506 else if (strcmp ("sink", p
) == 0)
34507 kind
= OMP_CLAUSE_DEPEND_SINK
;
34508 else if (strcmp ("source", p
) == 0)
34509 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
34516 cp_lexer_consume_token (parser
->lexer
);
34519 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
34521 poplevel (0, 1, 0);
34522 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
34523 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
34524 iterators
= NULL_TREE
;
34527 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
34529 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
34530 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34531 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
34532 OMP_CLAUSE_CHAIN (c
) = list
;
34533 if (!parens
.require_close (parser
))
34534 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34535 /*or_comma=*/false,
34536 /*consume_paren=*/true);
34540 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34543 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
34544 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
34547 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
34552 tree block
= poplevel (1, 1, 0);
34553 if (iterators
== error_mark_node
)
34554 iterators
= NULL_TREE
;
34556 TREE_VEC_ELT (iterators
, 5) = block
;
34559 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34561 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34563 OMP_CLAUSE_DECL (c
)
34564 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
34570 cp_parser_error (parser
, "invalid depend kind");
34573 poplevel (0, 1, 0);
34574 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34575 /*or_comma=*/false,
34576 /*consume_paren=*/true);
34581 map ( map-kind : variable-list )
34582 map ( variable-list )
34585 alloc | to | from | tofrom
34589 alloc | to | from | tofrom | release | delete
34591 map ( always [,] map-kind: variable-list ) */
34594 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
34597 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
34598 bool always
= false;
34600 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34603 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34605 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34606 const char *p
= IDENTIFIER_POINTER (id
);
34608 if (strcmp ("always", p
) == 0)
34611 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
34613 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
34614 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
34616 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
34620 cp_lexer_consume_token (parser
->lexer
);
34622 cp_lexer_consume_token (parser
->lexer
);
34627 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34628 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34630 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34631 const char *p
= IDENTIFIER_POINTER (id
);
34633 if (strcmp ("alloc", p
) == 0)
34634 kind
= GOMP_MAP_ALLOC
;
34635 else if (strcmp ("to", p
) == 0)
34636 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
34637 else if (strcmp ("from", p
) == 0)
34638 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
34639 else if (strcmp ("tofrom", p
) == 0)
34640 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
34641 else if (strcmp ("release", p
) == 0)
34642 kind
= GOMP_MAP_RELEASE
;
34645 cp_parser_error (parser
, "invalid map kind");
34646 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34647 /*or_comma=*/false,
34648 /*consume_paren=*/true);
34651 cp_lexer_consume_token (parser
->lexer
);
34652 cp_lexer_consume_token (parser
->lexer
);
34654 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
34655 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34657 kind
= GOMP_MAP_DELETE
;
34658 cp_lexer_consume_token (parser
->lexer
);
34659 cp_lexer_consume_token (parser
->lexer
);
34662 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
34665 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34666 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
34672 device ( expression ) */
34675 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
34676 location_t location
)
34680 matching_parens parens
;
34681 if (!parens
.require_open (parser
))
34684 t
= cp_parser_assignment_expression (parser
);
34686 if (t
== error_mark_node
34687 || !parens
.require_close (parser
))
34688 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34689 /*or_comma=*/false,
34690 /*consume_paren=*/true);
34692 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
34693 "device", location
);
34695 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
34696 OMP_CLAUSE_DEVICE_ID (c
) = t
;
34697 OMP_CLAUSE_CHAIN (c
) = list
;
34703 dist_schedule ( static )
34704 dist_schedule ( static , expression ) */
34707 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
34708 location_t location
)
34712 matching_parens parens
;
34713 if (!parens
.require_open (parser
))
34716 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
34718 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
34720 cp_lexer_consume_token (parser
->lexer
);
34722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34724 cp_lexer_consume_token (parser
->lexer
);
34726 t
= cp_parser_assignment_expression (parser
);
34728 if (t
== error_mark_node
)
34730 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
34732 if (!parens
.require_close (parser
))
34735 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34738 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
34740 OMP_CLAUSE_CHAIN (c
) = list
;
34744 cp_parser_error (parser
, "invalid dist_schedule kind");
34746 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34747 /*or_comma=*/false,
34748 /*consume_paren=*/true);
34753 proc_bind ( proc-bind-kind )
34756 master | close | spread */
34759 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
34760 location_t location
)
34763 enum omp_clause_proc_bind_kind kind
;
34765 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34768 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34770 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34771 const char *p
= IDENTIFIER_POINTER (id
);
34773 if (strcmp ("master", p
) == 0)
34774 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
34775 else if (strcmp ("close", p
) == 0)
34776 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
34777 else if (strcmp ("spread", p
) == 0)
34778 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
34785 cp_lexer_consume_token (parser
->lexer
);
34786 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34789 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
34790 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
34792 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
34793 OMP_CLAUSE_CHAIN (c
) = list
;
34797 cp_parser_error (parser
, "invalid depend kind");
34799 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34800 /*or_comma=*/false,
34801 /*consume_paren=*/true);
34806 async [( int-expr )] */
34809 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
34812 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34814 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
34816 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34818 matching_parens parens
;
34819 parens
.consume_open (parser
);
34821 t
= cp_parser_expression (parser
);
34822 if (t
== error_mark_node
34823 || !parens
.require_close (parser
))
34824 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34825 /*or_comma=*/false,
34826 /*consume_paren=*/true);
34829 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
34831 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
34832 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
34833 OMP_CLAUSE_CHAIN (c
) = list
;
34839 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34840 is a bitmask in MASK. Return the list of clauses found. */
34843 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34844 const char *where
, cp_token
*pragma_tok
,
34845 bool finish_p
= true)
34847 tree clauses
= NULL
;
34850 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34853 pragma_omp_clause c_kind
;
34854 omp_clause_code code
;
34855 const char *c_name
;
34856 tree prev
= clauses
;
34858 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34859 cp_lexer_consume_token (parser
->lexer
);
34861 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
34862 c_kind
= cp_parser_omp_clause_name (parser
);
34866 case PRAGMA_OACC_CLAUSE_ASYNC
:
34867 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
34870 case PRAGMA_OACC_CLAUSE_AUTO
:
34871 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
34875 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
34876 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
34877 c_name
= "collapse";
34879 case PRAGMA_OACC_CLAUSE_COPY
:
34880 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34883 case PRAGMA_OACC_CLAUSE_COPYIN
:
34884 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34887 case PRAGMA_OACC_CLAUSE_COPYOUT
:
34888 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34889 c_name
= "copyout";
34891 case PRAGMA_OACC_CLAUSE_CREATE
:
34892 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34895 case PRAGMA_OACC_CLAUSE_DELETE
:
34896 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34899 case PRAGMA_OMP_CLAUSE_DEFAULT
:
34900 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
34901 c_name
= "default";
34903 case PRAGMA_OACC_CLAUSE_DEVICE
:
34904 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34907 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
34908 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
34909 c_name
= "deviceptr";
34911 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
34912 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34913 c_name
= "device_resident";
34915 case PRAGMA_OACC_CLAUSE_FINALIZE
:
34916 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
34918 c_name
= "finalize";
34920 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
34921 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34923 c_name
= "firstprivate";
34925 case PRAGMA_OACC_CLAUSE_GANG
:
34927 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
34930 case PRAGMA_OACC_CLAUSE_HOST
:
34931 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34934 case PRAGMA_OACC_CLAUSE_IF
:
34935 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
34938 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
34939 clauses
= cp_parser_oacc_simple_clause (parser
,
34940 OMP_CLAUSE_IF_PRESENT
,
34942 c_name
= "if_present";
34944 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
34945 clauses
= cp_parser_oacc_simple_clause (parser
,
34946 OMP_CLAUSE_INDEPENDENT
,
34948 c_name
= "independent";
34950 case PRAGMA_OACC_CLAUSE_LINK
:
34951 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34954 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
34955 code
= OMP_CLAUSE_NUM_GANGS
;
34956 c_name
= "num_gangs";
34957 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34960 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
34961 c_name
= "num_workers";
34962 code
= OMP_CLAUSE_NUM_WORKERS
;
34963 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
34966 case PRAGMA_OACC_CLAUSE_PRESENT
:
34967 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34968 c_name
= "present";
34970 case PRAGMA_OACC_CLAUSE_PRIVATE
:
34971 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34973 c_name
= "private";
34975 case PRAGMA_OACC_CLAUSE_REDUCTION
:
34977 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
34979 c_name
= "reduction";
34981 case PRAGMA_OACC_CLAUSE_SEQ
:
34982 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
34986 case PRAGMA_OACC_CLAUSE_TILE
:
34987 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
34990 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
34991 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34993 c_name
= "use_device";
34995 case PRAGMA_OACC_CLAUSE_VECTOR
:
34997 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
35000 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
35001 c_name
= "vector_length";
35002 code
= OMP_CLAUSE_VECTOR_LENGTH
;
35003 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
35006 case PRAGMA_OACC_CLAUSE_WAIT
:
35007 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
35010 case PRAGMA_OACC_CLAUSE_WORKER
:
35012 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
35016 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
35022 if (((mask
>> c_kind
) & 1) == 0)
35024 /* Remove the invalid clause(s) from the list to avoid
35025 confusing the rest of the compiler. */
35027 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
35032 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35035 return finish_omp_clauses (clauses
, C_ORT_ACC
);
35040 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35041 is a bitmask in MASK. Return the list of clauses found; the result
35042 of clause default goes in *pdefault. */
35045 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
35046 const char *where
, cp_token
*pragma_tok
,
35047 bool finish_p
= true)
35049 tree clauses
= NULL
;
35051 cp_token
*token
= NULL
;
35053 /* Don't create location wrapper nodes within OpenMP clauses. */
35054 auto_suppress_location_wrappers sentinel
;
35056 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35058 pragma_omp_clause c_kind
;
35059 const char *c_name
;
35060 tree prev
= clauses
;
35062 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35063 cp_lexer_consume_token (parser
->lexer
);
35065 token
= cp_lexer_peek_token (parser
->lexer
);
35066 c_kind
= cp_parser_omp_clause_name (parser
);
35070 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
35071 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
35073 c_name
= "collapse";
35075 case PRAGMA_OMP_CLAUSE_COPYIN
:
35076 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
35079 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
35080 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
35082 c_name
= "copyprivate";
35084 case PRAGMA_OMP_CLAUSE_DEFAULT
:
35085 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
35086 token
->location
, false);
35087 c_name
= "default";
35089 case PRAGMA_OMP_CLAUSE_FINAL
:
35090 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
35093 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
35094 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
35096 c_name
= "firstprivate";
35098 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
35099 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
35101 c_name
= "grainsize";
35103 case PRAGMA_OMP_CLAUSE_HINT
:
35104 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
35108 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
35109 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
35111 c_name
= "defaultmap";
35113 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
35114 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
35116 c_name
= "use_device_ptr";
35118 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
35119 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
35121 c_name
= "is_device_ptr";
35123 case PRAGMA_OMP_CLAUSE_IF
:
35124 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
35128 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
35130 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
35132 c_name
= "in_reduction";
35134 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
35135 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
35136 c_name
= "lastprivate";
35138 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
35139 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
35141 c_name
= "mergeable";
35143 case PRAGMA_OMP_CLAUSE_NOWAIT
:
35144 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
35147 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
35148 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
35150 c_name
= "num_tasks";
35152 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
35153 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
35155 c_name
= "num_threads";
35157 case PRAGMA_OMP_CLAUSE_ORDERED
:
35158 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
35160 c_name
= "ordered";
35162 case PRAGMA_OMP_CLAUSE_PRIORITY
:
35163 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
35165 c_name
= "priority";
35167 case PRAGMA_OMP_CLAUSE_PRIVATE
:
35168 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
35170 c_name
= "private";
35172 case PRAGMA_OMP_CLAUSE_REDUCTION
:
35174 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
35176 c_name
= "reduction";
35178 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
35179 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
35181 c_name
= "schedule";
35183 case PRAGMA_OMP_CLAUSE_SHARED
:
35184 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
35188 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
35190 = cp_parser_omp_clause_reduction (parser
,
35191 OMP_CLAUSE_TASK_REDUCTION
,
35193 c_name
= "task_reduction";
35195 case PRAGMA_OMP_CLAUSE_UNTIED
:
35196 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
35200 case PRAGMA_OMP_CLAUSE_INBRANCH
:
35201 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
35202 clauses
, token
->location
);
35203 c_name
= "inbranch";
35205 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
35206 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
35208 c_name
= "nontemporal";
35210 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
35211 clauses
= cp_parser_omp_clause_branch (parser
,
35212 OMP_CLAUSE_NOTINBRANCH
,
35213 clauses
, token
->location
);
35214 c_name
= "notinbranch";
35216 case PRAGMA_OMP_CLAUSE_PARALLEL
:
35217 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
35218 clauses
, token
->location
);
35219 c_name
= "parallel";
35223 error_at (token
->location
, "%qs must be the first clause of %qs",
35228 case PRAGMA_OMP_CLAUSE_FOR
:
35229 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
35230 clauses
, token
->location
);
35233 goto clause_not_first
;
35235 case PRAGMA_OMP_CLAUSE_SECTIONS
:
35236 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
35237 clauses
, token
->location
);
35238 c_name
= "sections";
35240 goto clause_not_first
;
35242 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
35243 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
35244 clauses
, token
->location
);
35245 c_name
= "taskgroup";
35247 goto clause_not_first
;
35249 case PRAGMA_OMP_CLAUSE_LINK
:
35250 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
35253 case PRAGMA_OMP_CLAUSE_TO
:
35254 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
35255 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
35258 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
35261 case PRAGMA_OMP_CLAUSE_FROM
:
35262 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
35265 case PRAGMA_OMP_CLAUSE_UNIFORM
:
35266 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
35268 c_name
= "uniform";
35270 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
35271 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
35273 c_name
= "num_teams";
35275 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
35276 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
35278 c_name
= "thread_limit";
35280 case PRAGMA_OMP_CLAUSE_ALIGNED
:
35281 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
35282 c_name
= "aligned";
35284 case PRAGMA_OMP_CLAUSE_LINEAR
:
35286 bool declare_simd
= false;
35287 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
35288 declare_simd
= true;
35289 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
35293 case PRAGMA_OMP_CLAUSE_DEPEND
:
35294 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
35298 case PRAGMA_OMP_CLAUSE_MAP
:
35299 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
35302 case PRAGMA_OMP_CLAUSE_DEVICE
:
35303 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
35307 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
35308 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
35310 c_name
= "dist_schedule";
35312 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
35313 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
35315 c_name
= "proc_bind";
35317 case PRAGMA_OMP_CLAUSE_SAFELEN
:
35318 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
35320 c_name
= "safelen";
35322 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
35323 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
35325 c_name
= "simdlen";
35327 case PRAGMA_OMP_CLAUSE_NOGROUP
:
35328 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
35330 c_name
= "nogroup";
35332 case PRAGMA_OMP_CLAUSE_THREADS
:
35334 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
35335 clauses
, token
->location
);
35336 c_name
= "threads";
35338 case PRAGMA_OMP_CLAUSE_SIMD
:
35340 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
35341 clauses
, token
->location
);
35345 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
35351 if (((mask
>> c_kind
) & 1) == 0)
35353 /* Remove the invalid clause(s) from the list to avoid
35354 confusing the rest of the compiler. */
35356 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
35360 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35363 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
35364 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
35366 return finish_omp_clauses (clauses
, C_ORT_OMP
);
35375 In practice, we're also interested in adding the statement to an
35376 outer node. So it is convenient if we work around the fact that
35377 cp_parser_statement calls add_stmt. */
35380 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
35382 unsigned save
= parser
->in_statement
;
35384 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35385 This preserves the "not within loop or switch" style error messages
35386 for nonsense cases like
35392 if (parser
->in_statement
)
35393 parser
->in_statement
= IN_OMP_BLOCK
;
35399 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
35401 parser
->in_statement
= save
;
35405 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
35407 tree stmt
= begin_omp_structured_block ();
35408 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
35410 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35412 cp_parser_end_omp_structured_block (parser
, save
);
35413 return finish_omp_structured_block (stmt
);
35417 # pragma omp atomic new-line
35421 x binop= expr | x++ | ++x | x-- | --x
35423 +, *, -, /, &, ^, |, <<, >>
35425 where x is an lvalue expression with scalar type.
35428 # pragma omp atomic new-line
35431 # pragma omp atomic read new-line
35434 # pragma omp atomic write new-line
35437 # pragma omp atomic update new-line
35440 # pragma omp atomic capture new-line
35443 # pragma omp atomic capture new-line
35451 expression-stmt | x = x binop expr
35453 v = expression-stmt
35455 { v = x; update-stmt; } | { update-stmt; v = x; }
35459 expression-stmt | x = x binop expr | x = expr binop x
35463 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35465 where x and v are lvalue expressions with scalar type. */
35468 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
35470 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
35471 tree rhs1
= NULL_TREE
, orig_lhs
;
35472 location_t loc
= pragma_tok
->location
;
35473 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
35474 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
35475 bool structured_block
= false;
35477 tree clauses
= NULL_TREE
;
35479 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35481 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35482 cp_lexer_consume_token (parser
->lexer
);
35486 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35488 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35489 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35490 const char *p
= IDENTIFIER_POINTER (id
);
35491 enum tree_code new_code
= ERROR_MARK
;
35492 enum omp_memory_order new_memory_order
35493 = OMP_MEMORY_ORDER_UNSPECIFIED
;
35495 if (!strcmp (p
, "read"))
35496 new_code
= OMP_ATOMIC_READ
;
35497 else if (!strcmp (p
, "write"))
35498 new_code
= NOP_EXPR
;
35499 else if (!strcmp (p
, "update"))
35500 new_code
= OMP_ATOMIC
;
35501 else if (!strcmp (p
, "capture"))
35502 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
35503 else if (!strcmp (p
, "seq_cst"))
35504 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35505 else if (!strcmp (p
, "acq_rel"))
35506 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35507 else if (!strcmp (p
, "release"))
35508 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35509 else if (!strcmp (p
, "acquire"))
35510 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35511 else if (!strcmp (p
, "relaxed"))
35512 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35513 else if (!strcmp (p
, "hint"))
35515 cp_lexer_consume_token (parser
->lexer
);
35516 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
35522 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
35523 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35524 "%<release%>, %<relaxed%> or %<hint%> clause");
35528 if (new_code
!= ERROR_MARK
)
35530 if (code
!= ERROR_MARK
)
35531 error_at (cloc
, "too many atomic clauses");
35535 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35537 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35538 error_at (cloc
, "too many memory order clauses");
35540 memory_order
= new_memory_order
;
35542 cp_lexer_consume_token (parser
->lexer
);
35548 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35550 if (code
== ERROR_MARK
)
35552 if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
35555 = (enum omp_requires
) (omp_requires_mask
35556 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
35557 switch ((enum omp_memory_order
)
35558 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
35560 case OMP_MEMORY_ORDER_UNSPECIFIED
:
35561 case OMP_MEMORY_ORDER_RELAXED
:
35562 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35564 case OMP_MEMORY_ORDER_SEQ_CST
:
35565 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35567 case OMP_MEMORY_ORDER_ACQ_REL
:
35570 case OMP_ATOMIC_READ
:
35571 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35573 case NOP_EXPR
: /* atomic write */
35575 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35578 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35583 gcc_unreachable ();
35589 case OMP_ATOMIC_READ
:
35590 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35591 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
35593 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
35594 "%<acq_rel%> or %<release%> clauses");
35595 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35598 case NOP_EXPR
: /* atomic write */
35599 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35600 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35602 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
35603 "%<acq_rel%> or %<acquire%> clauses");
35604 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35608 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35609 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35611 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
35612 "%<acq_rel%> or %<acquire%> clauses");
35613 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35622 case OMP_ATOMIC_READ
:
35623 case NOP_EXPR
: /* atomic write */
35624 v
= cp_parser_unary_expression (parser
);
35625 if (v
== error_mark_node
)
35627 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35629 if (code
== NOP_EXPR
)
35630 lhs
= cp_parser_expression (parser
);
35632 lhs
= cp_parser_unary_expression (parser
);
35633 if (lhs
== error_mark_node
)
35635 if (code
== NOP_EXPR
)
35637 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35645 case OMP_ATOMIC_CAPTURE_NEW
:
35646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35648 cp_lexer_consume_token (parser
->lexer
);
35649 structured_block
= true;
35653 v
= cp_parser_unary_expression (parser
);
35654 if (v
== error_mark_node
)
35656 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35664 lhs
= cp_parser_unary_expression (parser
);
35666 switch (TREE_CODE (lhs
))
35671 case POSTINCREMENT_EXPR
:
35672 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35673 code
= OMP_ATOMIC_CAPTURE_OLD
;
35675 case PREINCREMENT_EXPR
:
35676 lhs
= TREE_OPERAND (lhs
, 0);
35677 opcode
= PLUS_EXPR
;
35678 rhs
= integer_one_node
;
35681 case POSTDECREMENT_EXPR
:
35682 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35683 code
= OMP_ATOMIC_CAPTURE_OLD
;
35685 case PREDECREMENT_EXPR
:
35686 lhs
= TREE_OPERAND (lhs
, 0);
35687 opcode
= MINUS_EXPR
;
35688 rhs
= integer_one_node
;
35691 case COMPOUND_EXPR
:
35692 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
35693 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
35694 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
35695 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
35696 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35697 (TREE_OPERAND (lhs
, 1), 0), 0)))
35699 /* Undo effects of boolean_increment for post {in,de}crement. */
35700 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
35703 if (TREE_CODE (lhs
) == MODIFY_EXPR
35704 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
35706 /* Undo effects of boolean_increment. */
35707 if (integer_onep (TREE_OPERAND (lhs
, 1)))
35709 /* This is pre or post increment. */
35710 rhs
= TREE_OPERAND (lhs
, 1);
35711 lhs
= TREE_OPERAND (lhs
, 0);
35713 if (code
== OMP_ATOMIC_CAPTURE_NEW
35714 && !structured_block
35715 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
35716 code
= OMP_ATOMIC_CAPTURE_OLD
;
35722 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35725 opcode
= MULT_EXPR
;
35728 opcode
= TRUNC_DIV_EXPR
;
35731 opcode
= PLUS_EXPR
;
35734 opcode
= MINUS_EXPR
;
35736 case CPP_LSHIFT_EQ
:
35737 opcode
= LSHIFT_EXPR
;
35739 case CPP_RSHIFT_EQ
:
35740 opcode
= RSHIFT_EXPR
;
35743 opcode
= BIT_AND_EXPR
;
35746 opcode
= BIT_IOR_EXPR
;
35749 opcode
= BIT_XOR_EXPR
;
35752 enum cp_parser_prec oprec
;
35754 cp_lexer_consume_token (parser
->lexer
);
35755 cp_parser_parse_tentatively (parser
);
35756 rhs1
= cp_parser_simple_cast_expression (parser
);
35757 if (rhs1
== error_mark_node
)
35759 cp_parser_abort_tentative_parse (parser
);
35760 cp_parser_simple_cast_expression (parser
);
35763 token
= cp_lexer_peek_token (parser
->lexer
);
35764 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
35766 cp_parser_abort_tentative_parse (parser
);
35767 cp_parser_parse_tentatively (parser
);
35768 rhs
= cp_parser_binary_expression (parser
, false, true,
35769 PREC_NOT_OPERATOR
, NULL
);
35770 if (rhs
== error_mark_node
)
35772 cp_parser_abort_tentative_parse (parser
);
35773 cp_parser_binary_expression (parser
, false, true,
35774 PREC_NOT_OPERATOR
, NULL
);
35777 switch (TREE_CODE (rhs
))
35780 case TRUNC_DIV_EXPR
:
35789 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
35791 if (cp_parser_parse_definitely (parser
))
35793 opcode
= TREE_CODE (rhs
);
35794 rhs1
= TREE_OPERAND (rhs
, 0);
35795 rhs
= TREE_OPERAND (rhs
, 1);
35805 cp_parser_abort_tentative_parse (parser
);
35806 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
35808 rhs
= cp_parser_expression (parser
);
35809 if (rhs
== error_mark_node
)
35815 cp_parser_error (parser
,
35816 "invalid form of %<#pragma omp atomic%>");
35819 if (!cp_parser_parse_definitely (parser
))
35821 switch (token
->type
)
35823 case CPP_SEMICOLON
:
35824 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35826 code
= OMP_ATOMIC_CAPTURE_OLD
;
35831 cp_lexer_consume_token (parser
->lexer
);
35834 else if (structured_block
)
35841 cp_parser_error (parser
,
35842 "invalid form of %<#pragma omp atomic%>");
35845 opcode
= MULT_EXPR
;
35848 opcode
= TRUNC_DIV_EXPR
;
35851 opcode
= PLUS_EXPR
;
35854 opcode
= MINUS_EXPR
;
35857 opcode
= LSHIFT_EXPR
;
35860 opcode
= RSHIFT_EXPR
;
35863 opcode
= BIT_AND_EXPR
;
35866 opcode
= BIT_IOR_EXPR
;
35869 opcode
= BIT_XOR_EXPR
;
35872 cp_parser_error (parser
,
35873 "invalid operator for %<#pragma omp atomic%>");
35876 oprec
= TOKEN_PRECEDENCE (token
);
35877 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
35878 if (commutative_tree_code (opcode
))
35879 oprec
= (enum cp_parser_prec
) (oprec
- 1);
35880 cp_lexer_consume_token (parser
->lexer
);
35881 rhs
= cp_parser_binary_expression (parser
, false, false,
35883 if (rhs
== error_mark_node
)
35888 cp_parser_error (parser
,
35889 "invalid operator for %<#pragma omp atomic%>");
35892 cp_lexer_consume_token (parser
->lexer
);
35894 rhs
= cp_parser_expression (parser
);
35895 if (rhs
== error_mark_node
)
35900 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35902 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
35904 v
= cp_parser_unary_expression (parser
);
35905 if (v
== error_mark_node
)
35907 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35909 lhs1
= cp_parser_unary_expression (parser
);
35910 if (lhs1
== error_mark_node
)
35913 if (structured_block
)
35915 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35916 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
35919 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
35920 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
35921 rhs1
, clauses
, memory_order
);
35922 if (!structured_block
)
35923 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35927 cp_parser_skip_to_end_of_block_or_statement (parser
);
35928 if (structured_block
)
35930 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35931 cp_lexer_consume_token (parser
->lexer
);
35932 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
35934 cp_parser_skip_to_end_of_block_or_statement (parser
);
35935 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35936 cp_lexer_consume_token (parser
->lexer
);
35943 # pragma omp barrier new-line */
35946 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
35948 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35949 finish_omp_barrier ();
35953 # pragma omp critical [(name)] new-line
35957 # pragma omp critical [(name) [hint(expression)]] new-line
35958 structured-block */
35960 #define OMP_CRITICAL_CLAUSE_MASK \
35961 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35964 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35966 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
35968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35970 matching_parens parens
;
35971 parens
.consume_open (parser
);
35973 name
= cp_parser_identifier (parser
);
35975 if (name
== error_mark_node
35976 || !parens
.require_close (parser
))
35977 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35978 /*or_comma=*/false,
35979 /*consume_paren=*/true);
35980 if (name
== error_mark_node
)
35983 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
35984 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
35985 cp_lexer_consume_token (parser
->lexer
);
35987 clauses
= cp_parser_omp_all_clauses (parser
,
35988 OMP_CRITICAL_CLAUSE_MASK
,
35989 "#pragma omp critical", pragma_tok
);
35992 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35994 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
35995 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
35999 # pragma omp depobj ( depobj ) depobj-clause new-line
36002 depend (dependence-type : locator)
36004 update (dependence-type)
36013 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
36015 location_t loc
= pragma_tok
->location
;
36016 matching_parens parens
;
36017 if (!parens
.require_open (parser
))
36019 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36023 tree depobj
= cp_parser_assignment_expression (parser
);
36025 if (!parens
.require_close (parser
))
36026 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36027 /*or_comma=*/false,
36028 /*consume_paren=*/true);
36030 tree clause
= NULL_TREE
;
36031 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
36032 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36033 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36035 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36036 const char *p
= IDENTIFIER_POINTER (id
);
36038 cp_lexer_consume_token (parser
->lexer
);
36039 if (!strcmp ("depend", p
))
36041 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
36043 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
36045 clause
= error_mark_node
;
36047 else if (!strcmp ("destroy", p
))
36048 kind
= OMP_CLAUSE_DEPEND_LAST
;
36049 else if (!strcmp ("update", p
))
36051 matching_parens c_parens
;
36052 if (c_parens
.require_open (parser
))
36055 = cp_lexer_peek_token (parser
->lexer
)->location
;
36056 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36058 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36059 const char *p2
= IDENTIFIER_POINTER (id2
);
36061 cp_lexer_consume_token (parser
->lexer
);
36062 if (!strcmp ("in", p2
))
36063 kind
= OMP_CLAUSE_DEPEND_IN
;
36064 else if (!strcmp ("out", p2
))
36065 kind
= OMP_CLAUSE_DEPEND_OUT
;
36066 else if (!strcmp ("inout", p2
))
36067 kind
= OMP_CLAUSE_DEPEND_INOUT
;
36068 else if (!strcmp ("mutexinoutset", p2
))
36069 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
36071 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36073 clause
= error_mark_node
;
36074 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
36075 "%<mutexinoutset%>");
36077 if (!c_parens
.require_close (parser
))
36078 cp_parser_skip_to_closing_parenthesis (parser
,
36079 /*recovering=*/true,
36080 /*or_comma=*/false,
36081 /*consume_paren=*/true);
36084 clause
= error_mark_node
;
36087 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36089 clause
= error_mark_node
;
36090 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
36092 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36094 finish_omp_depobj (loc
, depobj
, kind
, clause
);
36099 # pragma omp flush flush-vars[opt] new-line
36105 # pragma omp flush memory-order-clause new-line */
36108 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
36110 enum memmodel mo
= MEMMODEL_LAST
;
36111 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36113 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36114 const char *p
= IDENTIFIER_POINTER (id
);
36115 if (!strcmp (p
, "acq_rel"))
36116 mo
= MEMMODEL_ACQ_REL
;
36117 else if (!strcmp (p
, "release"))
36118 mo
= MEMMODEL_RELEASE
;
36119 else if (!strcmp (p
, "acquire"))
36120 mo
= MEMMODEL_ACQUIRE
;
36122 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36123 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36124 cp_lexer_consume_token (parser
->lexer
);
36126 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36128 if (mo
!= MEMMODEL_LAST
)
36129 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36130 "%<flush%> list specified together with memory order "
36132 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36134 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36136 finish_omp_flush (mo
);
36139 /* Helper function, to parse omp for increment expression. */
36142 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
36144 tree cond
= cp_parser_binary_expression (parser
, false, true,
36145 PREC_NOT_OPERATOR
, NULL
);
36146 if (cond
== error_mark_node
36147 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36149 cp_parser_skip_to_end_of_statement (parser
);
36150 return error_mark_node
;
36153 switch (TREE_CODE (cond
))
36161 if (code
!= OACC_LOOP
)
36163 gcc_fallthrough ();
36165 return error_mark_node
;
36168 /* If decl is an iterator, preserve LHS and RHS of the relational
36169 expr until finish_omp_for. */
36171 && (type_dependent_expression_p (decl
)
36172 || CLASS_TYPE_P (TREE_TYPE (decl
))))
36175 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
36177 TREE_OPERAND (cond
, 0), ERROR_MARK
,
36178 TREE_OPERAND (cond
, 1), ERROR_MARK
,
36179 /*overload=*/NULL
, tf_warning_or_error
);
36182 /* Helper function, to parse omp for increment expression. */
36185 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
36187 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36193 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36195 op
= (token
->type
== CPP_PLUS_PLUS
36196 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
36197 cp_lexer_consume_token (parser
->lexer
);
36198 lhs
= cp_parser_simple_cast_expression (parser
);
36200 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36201 return error_mark_node
;
36202 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36205 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
36207 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36208 return error_mark_node
;
36210 token
= cp_lexer_peek_token (parser
->lexer
);
36211 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36213 op
= (token
->type
== CPP_PLUS_PLUS
36214 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
36215 cp_lexer_consume_token (parser
->lexer
);
36216 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36219 op
= cp_parser_assignment_operator_opt (parser
);
36220 if (op
== ERROR_MARK
)
36221 return error_mark_node
;
36223 if (op
!= NOP_EXPR
)
36225 rhs
= cp_parser_assignment_expression (parser
);
36226 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
36227 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36230 lhs
= cp_parser_binary_expression (parser
, false, false,
36231 PREC_ADDITIVE_EXPRESSION
, NULL
);
36232 token
= cp_lexer_peek_token (parser
->lexer
);
36233 decl_first
= (lhs
== decl
36234 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
36237 if (token
->type
!= CPP_PLUS
36238 && token
->type
!= CPP_MINUS
)
36239 return error_mark_node
;
36243 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
36244 cp_lexer_consume_token (parser
->lexer
);
36245 rhs
= cp_parser_binary_expression (parser
, false, false,
36246 PREC_ADDITIVE_EXPRESSION
, NULL
);
36247 token
= cp_lexer_peek_token (parser
->lexer
);
36248 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
36250 if (lhs
== NULL_TREE
)
36252 if (op
== PLUS_EXPR
)
36255 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
36256 tf_warning_or_error
);
36259 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
36260 ERROR_MARK
, NULL
, tf_warning_or_error
);
36263 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
36268 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
36269 || op
== MINUS_EXPR
)
36270 return error_mark_node
;
36271 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
36274 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
36276 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36279 /* Parse the initialization statement of an OpenMP for loop.
36281 Return true if the resulting construct should have an
36282 OMP_CLAUSE_PRIVATE added to it. */
36285 cp_parser_omp_for_loop_init (cp_parser
*parser
,
36286 tree
&this_pre_body
,
36287 vec
<tree
, va_gc
> *&for_block
,
36293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36296 tree add_private_clause
= NULL_TREE
;
36298 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36302 integer-type var = lb
36303 random-access-iterator-type var = lb
36304 pointer-type var = lb
36306 cp_decl_specifier_seq type_specifiers
;
36308 /* First, try to parse as an initialized declaration. See
36309 cp_parser_condition, from whence the bulk of this is copied. */
36311 cp_parser_parse_tentatively (parser
);
36312 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
36313 /*is_declaration=*/true,
36314 /*is_trailing_return=*/false,
36316 if (cp_parser_parse_definitely (parser
))
36318 /* If parsing a type specifier seq succeeded, then this
36319 MUST be a initialized declaration. */
36320 tree asm_specification
, attributes
;
36321 cp_declarator
*declarator
;
36323 declarator
= cp_parser_declarator (parser
,
36324 CP_PARSER_DECLARATOR_NAMED
,
36325 CP_PARSER_FLAGS_NONE
,
36326 /*ctor_dtor_or_conv_p=*/NULL
,
36327 /*parenthesized_p=*/NULL
,
36328 /*member_p=*/false,
36329 /*friend_p=*/false,
36330 /*static_p=*/false);
36331 attributes
= cp_parser_attributes_opt (parser
);
36332 asm_specification
= cp_parser_asm_specification_opt (parser
);
36334 if (declarator
== cp_error_declarator
)
36335 cp_parser_skip_to_end_of_statement (parser
);
36339 tree pushed_scope
, auto_node
;
36341 decl
= start_decl (declarator
, &type_specifiers
,
36342 SD_INITIALIZED
, attributes
,
36343 /*prefix_attributes=*/NULL_TREE
,
36346 auto_node
= type_uses_auto (TREE_TYPE (decl
));
36347 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
36349 if (cp_lexer_next_token_is (parser
->lexer
,
36351 error ("parenthesized initialization is not allowed in "
36352 "OpenMP %<for%> loop");
36354 /* Trigger an error. */
36355 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36357 init
= error_mark_node
;
36358 cp_parser_skip_to_end_of_statement (parser
);
36360 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
36361 || type_dependent_expression_p (decl
)
36364 bool is_direct_init
, is_non_constant_init
;
36366 init
= cp_parser_initializer (parser
,
36368 &is_non_constant_init
);
36373 = do_auto_deduction (TREE_TYPE (decl
), init
,
36376 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
36377 && !type_dependent_expression_p (decl
))
36381 cp_finish_decl (decl
, init
, !is_non_constant_init
,
36383 LOOKUP_ONLYCONVERTING
);
36385 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
36387 vec_safe_push (for_block
, this_pre_body
);
36392 init
= pop_stmt_list (this_pre_body
);
36393 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
36395 tree_stmt_iterator i
= tsi_start (init
);
36396 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36397 while (!tsi_end_p (i
))
36399 tree t
= tsi_stmt (i
);
36400 if (TREE_CODE (t
) == DECL_EXPR
36401 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
36404 vec_safe_push (for_block
, t
);
36409 if (tsi_one_before_end_p (i
))
36411 tree t
= tsi_stmt (i
);
36413 free_stmt_list (init
);
36418 this_pre_body
= NULL_TREE
;
36423 cp_lexer_consume_token (parser
->lexer
);
36424 init
= cp_parser_assignment_expression (parser
);
36427 if (TYPE_REF_P (TREE_TYPE (decl
)))
36428 init
= error_mark_node
;
36430 cp_finish_decl (decl
, NULL_TREE
,
36431 /*init_const_expr_p=*/false,
36433 LOOKUP_ONLYCONVERTING
);
36437 pop_scope (pushed_scope
);
36443 /* If parsing a type specifier sequence failed, then
36444 this MUST be a simple expression. */
36445 cp_parser_parse_tentatively (parser
);
36446 decl
= cp_parser_primary_expression (parser
, false, false,
36448 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
36449 if (!cp_parser_error_occurred (parser
)
36451 && (TREE_CODE (decl
) == COMPONENT_REF
36452 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
36454 cp_parser_abort_tentative_parse (parser
);
36455 cp_parser_parse_tentatively (parser
);
36456 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36457 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36458 /*check_dependency_p=*/true,
36459 /*template_p=*/NULL
,
36460 /*declarator_p=*/false,
36461 /*optional_p=*/false);
36462 if (name
!= error_mark_node
36463 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
36465 decl
= cp_parser_lookup_name_simple (parser
, name
,
36467 if (TREE_CODE (decl
) == FIELD_DECL
)
36468 add_private_clause
= omp_privatize_field (decl
, false);
36470 cp_parser_abort_tentative_parse (parser
);
36471 cp_parser_parse_tentatively (parser
);
36472 decl
= cp_parser_primary_expression (parser
, false, false,
36475 if (!cp_parser_error_occurred (parser
)
36478 && CLASS_TYPE_P (TREE_TYPE (decl
)))
36482 cp_parser_parse_definitely (parser
);
36483 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36484 rhs
= cp_parser_assignment_expression (parser
);
36486 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
36489 tf_warning_or_error
));
36490 if (!add_private_clause
)
36491 add_private_clause
= decl
;
36496 cp_parser_abort_tentative_parse (parser
);
36497 init
= cp_parser_expression (parser
);
36500 if (TREE_CODE (init
) == MODIFY_EXPR
36501 || TREE_CODE (init
) == MODOP_EXPR
)
36502 real_decl
= TREE_OPERAND (init
, 0);
36506 return add_private_clause
;
36509 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36512 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
36513 tree
&decl
, tree
&orig_decl
, tree
&init
,
36514 tree
&orig_init
, tree
&cond
, tree
&incr
)
36516 tree begin
, end
, range_temp_decl
= NULL_TREE
;
36517 tree iter_type
, begin_expr
, end_expr
;
36519 if (processing_template_decl
)
36521 if (check_for_bare_parameter_packs (init
))
36522 init
= error_mark_node
;
36523 if (!type_dependent_expression_p (init
)
36524 /* do_auto_deduction doesn't mess with template init-lists. */
36525 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
36528 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
36530 tree v
= DECL_VALUE_EXPR (decl
);
36531 if (TREE_CODE (v
) == ARRAY_REF
36532 && VAR_P (TREE_OPERAND (v
, 0))
36533 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36534 d
= TREE_OPERAND (v
, 0);
36536 do_range_for_auto_deduction (d
, init
);
36538 cond
= global_namespace
;
36542 this_pre_body
= pop_stmt_list (this_pre_body
);
36546 init
= mark_lvalue_use (init
);
36548 if (decl
== error_mark_node
|| init
== error_mark_node
)
36549 /* If an error happened previously do nothing or else a lot of
36550 unhelpful errors would be issued. */
36551 begin_expr
= end_expr
= iter_type
= error_mark_node
;
36557 && array_of_runtime_bound_p (TREE_TYPE (init
)))
36558 /* Can't bind a reference to an array of runtime bound. */
36562 range_temp
= build_range_temp (init
);
36563 DECL_NAME (range_temp
) = NULL_TREE
;
36564 pushdecl (range_temp
);
36565 cp_finish_decl (range_temp
, init
,
36566 /*is_constant_init*/false, NULL_TREE
,
36567 LOOKUP_ONLYCONVERTING
);
36568 range_temp_decl
= range_temp
;
36569 range_temp
= convert_from_reference (range_temp
);
36571 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
36572 &begin_expr
, &end_expr
);
36575 tree end_iter_type
= iter_type
;
36576 if (cxx_dialect
>= cxx17
)
36577 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
36578 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
36579 TREE_USED (end
) = 1;
36580 DECL_ARTIFICIAL (end
) = 1;
36582 cp_finish_decl (end
, end_expr
,
36583 /*is_constant_init*/false, NULL_TREE
,
36584 LOOKUP_ONLYCONVERTING
);
36586 /* The new for initialization statement. */
36587 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
36588 TREE_USED (begin
) = 1;
36589 DECL_ARTIFICIAL (begin
) = 1;
36592 if (CLASS_TYPE_P (iter_type
))
36597 begin_expr
= NULL_TREE
;
36599 cp_finish_decl (begin
, begin_expr
,
36600 /*is_constant_init*/false, NULL_TREE
,
36601 LOOKUP_ONLYCONVERTING
);
36603 /* The new for condition. */
36604 if (CLASS_TYPE_P (iter_type
))
36605 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
36607 cond
= build_x_binary_op (input_location
, NE_EXPR
,
36610 NULL
, tf_warning_or_error
);
36612 /* The new increment expression. */
36613 if (CLASS_TYPE_P (iter_type
))
36614 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
36616 incr
= finish_unary_op_expr (input_location
,
36617 PREINCREMENT_EXPR
, begin
,
36618 tf_warning_or_error
);
36624 vec_safe_push (for_block
, this_pre_body
);
36625 this_pre_body
= NULL_TREE
;
36628 tree decomp_first_name
= NULL_TREE
;
36629 unsigned decomp_cnt
= 0;
36630 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
36632 tree v
= DECL_VALUE_EXPR (orig_decl
);
36633 if (TREE_CODE (v
) == ARRAY_REF
36634 && VAR_P (TREE_OPERAND (v
, 0))
36635 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36637 tree d
= orig_decl
;
36638 orig_decl
= TREE_OPERAND (v
, 0);
36639 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
36640 decomp_first_name
= d
;
36644 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
36647 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36649 if (!error_operand_p (t
))
36650 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
36654 tree v
= make_tree_vec (decomp_cnt
+ 3);
36655 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
36656 TREE_VEC_ELT (v
, 1) = end
;
36657 TREE_VEC_ELT (v
, 2) = orig_decl
;
36658 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
36660 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
36661 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
36663 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
36666 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36667 inside of the collapsed body. */
36670 cp_finish_omp_range_for (tree orig
, tree begin
)
36672 gcc_assert (TREE_CODE (orig
) == TREE_LIST
36673 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
36674 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
36675 tree decomp_first_name
= NULL_TREE
;
36676 unsigned int decomp_cnt
= 0;
36678 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36680 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
36681 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
36682 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
36685 /* The declaration is initialized with *__begin inside the loop body. */
36686 cp_finish_decl (decl
,
36687 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36688 tf_warning_or_error
),
36689 /*is_constant_init*/false, NULL_TREE
,
36690 LOOKUP_ONLYCONVERTING
);
36691 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36692 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
36695 /* Parse the restricted form of the for statement allowed by OpenMP. */
36698 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
36699 tree
*cclauses
, bool *if_p
)
36701 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
36703 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
36704 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
36705 location_t loc_first
;
36706 bool collapse_err
= false;
36707 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
36708 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
36709 auto_vec
<tree
, 4> orig_inits
;
36710 bool tiling
= false;
36712 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
36713 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
36714 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
36715 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
36718 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
36720 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
36721 && OMP_CLAUSE_ORDERED_EXPR (cl
))
36724 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
36727 if (ordered
&& ordered
< collapse
)
36729 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36730 "%<ordered%> clause parameter is less than %<collapse%>");
36731 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
36732 = build_int_cst (NULL_TREE
, collapse
);
36733 ordered
= collapse
;
36737 for (tree
*pc
= &clauses
; *pc
; )
36738 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
36740 error_at (OMP_CLAUSE_LOCATION (*pc
),
36741 "%<linear%> clause may not be specified together "
36742 "with %<ordered%> clause with a parameter");
36743 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36746 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36749 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
36750 count
= ordered
? ordered
: collapse
;
36752 declv
= make_tree_vec (count
);
36753 initv
= make_tree_vec (count
);
36754 condv
= make_tree_vec (count
);
36755 incrv
= make_tree_vec (count
);
36756 orig_declv
= NULL_TREE
;
36758 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
36760 for (i
= 0; i
< count
; i
++)
36762 int bracecount
= 0;
36763 tree add_private_clause
= NULL_TREE
;
36766 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36769 cp_parser_error (parser
, "for statement expected");
36772 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
36774 /* Don't create location wrapper nodes within an OpenMP "for"
36776 auto_suppress_location_wrappers sentinel
;
36778 matching_parens parens
;
36779 if (!parens
.require_open (parser
))
36782 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
36783 this_pre_body
= push_stmt_list ();
36785 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
36787 /* Save tokens so that we can put them back. */
36788 cp_lexer_save_tokens (parser
->lexer
);
36790 /* Look for ':' that is not nested in () or {}. */
36792 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
36793 /*recovering=*/false,
36798 /* Roll back the tokens we skipped. */
36799 cp_lexer_rollback_tokens (parser
->lexer
);
36803 bool saved_colon_corrects_to_scope_p
36804 = parser
->colon_corrects_to_scope_p
;
36806 /* A colon is used in range-based for. */
36807 parser
->colon_corrects_to_scope_p
= false;
36809 /* Parse the declaration. */
36810 cp_parser_simple_declaration (parser
,
36811 /*function_definition_allowed_p=*/
36813 parser
->colon_corrects_to_scope_p
36814 = saved_colon_corrects_to_scope_p
;
36816 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36818 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
36821 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
36822 orig_decl
, init
, orig_init
,
36829 pre_body
= push_stmt_list ();
36831 add_stmt (this_pre_body
);
36832 pre_body
= pop_stmt_list (pre_body
);
36835 pre_body
= this_pre_body
;
36839 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36840 "%<ordered%> clause with parameter on "
36841 "range-based %<for%> loop");
36843 goto parse_close_paren
;
36848 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
36849 init
, orig_init
, decl
, real_decl
);
36851 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36854 this_pre_body
= pop_stmt_list (this_pre_body
);
36858 pre_body
= push_stmt_list ();
36860 add_stmt (this_pre_body
);
36861 pre_body
= pop_stmt_list (pre_body
);
36864 pre_body
= this_pre_body
;
36869 if (cclauses
!= NULL
36870 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
36871 && real_decl
!= NULL_TREE
)
36874 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
36875 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
36876 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36878 error_at (loc
, "iteration variable %qD"
36879 " should not be firstprivate", real_decl
);
36880 *c
= OMP_CLAUSE_CHAIN (*c
);
36882 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
36883 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36885 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36887 *c
= OMP_CLAUSE_CHAIN (*c
);
36888 if (code
== OMP_SIMD
)
36890 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
36891 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
36895 OMP_CLAUSE_CHAIN (l
) = clauses
;
36898 add_private_clause
= NULL_TREE
;
36902 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
36903 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36904 add_private_clause
= NULL_TREE
;
36905 c
= &OMP_CLAUSE_CHAIN (*c
);
36909 if (add_private_clause
)
36912 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
36914 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
36915 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
36916 && OMP_CLAUSE_DECL (c
) == decl
)
36918 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
36919 && OMP_CLAUSE_DECL (c
) == decl
)
36920 error_at (loc
, "iteration variable %qD "
36921 "should not be firstprivate",
36923 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
36924 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
36925 && OMP_CLAUSE_DECL (c
) == decl
)
36926 error_at (loc
, "iteration variable %qD should not be reduction",
36931 if (code
!= OMP_SIMD
)
36932 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
36933 else if (collapse
== 1)
36934 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
36936 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
36937 OMP_CLAUSE_DECL (c
) = add_private_clause
;
36938 c
= finish_omp_clauses (c
, C_ORT_OMP
);
36941 OMP_CLAUSE_CHAIN (c
) = clauses
;
36943 /* For linear, signal that we need to fill up
36944 the so far unknown linear step. */
36945 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
36946 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
36952 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36953 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
36954 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36957 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
36959 /* If decl is an iterator, preserve the operator on decl
36960 until finish_omp_for. */
36962 && ((processing_template_decl
36963 && (TREE_TYPE (real_decl
) == NULL_TREE
36964 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
36965 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
36966 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
36968 incr
= cp_parser_expression (parser
);
36969 if (!EXPR_HAS_LOCATION (incr
))
36970 protected_set_expr_location (incr
, input_location
);
36974 if (!parens
.require_close (parser
))
36975 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36976 /*or_comma=*/false,
36977 /*consume_paren=*/true);
36979 TREE_VEC_ELT (declv
, i
) = decl
;
36980 TREE_VEC_ELT (initv
, i
) = init
;
36981 TREE_VEC_ELT (condv
, i
) = cond
;
36982 TREE_VEC_ELT (incrv
, i
) = incr
;
36985 orig_inits
.safe_grow_cleared (i
+ 1);
36986 orig_inits
[i
] = orig_init
;
36991 orig_declv
= copy_node (declv
);
36992 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
36994 else if (orig_declv
)
36995 TREE_VEC_ELT (orig_declv
, i
) = decl
;
36997 if (i
== count
- 1)
37000 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37001 in between the collapsed for loops to be still considered perfectly
37002 nested. Hopefully the final version clarifies this.
37003 For now handle (multiple) {'s and empty statements. */
37004 cp_parser_parse_tentatively (parser
);
37007 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37009 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
37011 cp_lexer_consume_token (parser
->lexer
);
37014 else if (bracecount
37015 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
37016 cp_lexer_consume_token (parser
->lexer
);
37019 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37020 error_at (loc
, "not enough for loops to collapse");
37021 collapse_err
= true;
37022 cp_parser_abort_tentative_parse (parser
);
37030 cp_parser_parse_definitely (parser
);
37031 nbraces
+= bracecount
;
37038 /* Note that we saved the original contents of this flag when we entered
37039 the structured block, and so we don't need to re-save it here. */
37040 parser
->in_statement
= IN_OMP_FOR
;
37042 /* Note that the grammar doesn't call for a structured block here,
37043 though the loop as a whole is a structured block. */
37046 body
= begin_omp_structured_block ();
37047 for (i
= 0; i
< count
; i
++)
37048 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
37049 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
37050 TREE_VEC_ELT (declv
, i
));
37053 body
= push_stmt_list ();
37054 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37056 body
= finish_omp_structured_block (body
);
37058 body
= pop_stmt_list (body
);
37060 if (declv
== NULL_TREE
)
37063 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
37064 incrv
, body
, pre_body
, &orig_inits
, clauses
);
37068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
37070 cp_lexer_consume_token (parser
->lexer
);
37073 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
37074 cp_lexer_consume_token (parser
->lexer
);
37079 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37080 "collapsed loops not perfectly nested");
37082 collapse_err
= true;
37083 cp_parser_statement_seq_opt (parser
, NULL
);
37084 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
37089 while (!for_block
->is_empty ())
37091 tree t
= for_block
->pop ();
37092 if (TREE_CODE (t
) == STATEMENT_LIST
)
37093 add_stmt (pop_stmt_list (t
));
37097 release_tree_vector (for_block
);
37102 /* Helper function for OpenMP parsing, split clauses and call
37103 finish_omp_clauses on each of the set of clauses afterwards. */
37106 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
37107 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
37110 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
37111 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
37113 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
37117 #pragma omp simd simd-clause[optseq] new-line
37120 #define OMP_SIMD_CLAUSE_MASK \
37121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37133 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37134 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37137 tree clauses
, sb
, ret
;
37139 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37141 strcat (p_name
, " simd");
37142 mask
|= OMP_SIMD_CLAUSE_MASK
;
37144 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37148 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
37149 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
37150 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
37151 OMP_CLAUSE_ORDERED
);
37152 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
37154 error_at (OMP_CLAUSE_LOCATION (c
),
37155 "%<ordered%> clause with parameter may not be specified "
37156 "on %qs construct", p_name
);
37157 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
37161 keep_next_level (true);
37162 sb
= begin_omp_structured_block ();
37163 save
= cp_parser_begin_omp_structured_block (parser
);
37165 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
37167 cp_parser_end_omp_structured_block (parser
, save
);
37168 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37174 #pragma omp for for-clause[optseq] new-line
37178 #pragma omp for simd for-simd-clause[optseq] new-line
37181 #define OMP_FOR_CLAUSE_MASK \
37182 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37193 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
37194 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37197 tree clauses
, sb
, ret
;
37199 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37201 strcat (p_name
, " for");
37202 mask
|= OMP_FOR_CLAUSE_MASK
;
37203 /* parallel for{, simd} disallows nowait clause, but for
37204 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37205 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
37206 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37207 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37208 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37209 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
37211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37213 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37214 const char *p
= IDENTIFIER_POINTER (id
);
37216 if (strcmp (p
, "simd") == 0)
37218 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37219 if (cclauses
== NULL
)
37220 cclauses
= cclauses_buf
;
37222 cp_lexer_consume_token (parser
->lexer
);
37223 if (!flag_openmp
) /* flag_openmp_simd */
37224 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37226 sb
= begin_omp_structured_block ();
37227 save
= cp_parser_begin_omp_structured_block (parser
);
37228 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37230 cp_parser_end_omp_structured_block (parser
, save
);
37231 tree body
= finish_omp_structured_block (sb
);
37234 ret
= make_node (OMP_FOR
);
37235 TREE_TYPE (ret
) = void_type_node
;
37236 OMP_FOR_BODY (ret
) = body
;
37237 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37238 SET_EXPR_LOCATION (ret
, loc
);
37243 if (!flag_openmp
) /* flag_openmp_simd */
37245 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37249 /* Composite distribute parallel for disallows linear clause. */
37250 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37251 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
37253 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37257 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
37258 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37261 keep_next_level (true);
37262 sb
= begin_omp_structured_block ();
37263 save
= cp_parser_begin_omp_structured_block (parser
);
37265 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
37267 cp_parser_end_omp_structured_block (parser
, save
);
37268 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37273 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
37274 omp_clause_mask
, tree
*, bool *);
37277 # pragma omp master new-line
37278 structured-block */
37281 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
37282 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37285 tree clauses
, sb
, ret
;
37287 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37289 strcat (p_name
, " master");
37291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37293 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37294 const char *p
= IDENTIFIER_POINTER (id
);
37296 if (strcmp (p
, "taskloop") == 0)
37298 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37299 if (cclauses
== NULL
)
37300 cclauses
= cclauses_buf
;
37302 cp_lexer_consume_token (parser
->lexer
);
37303 if (!flag_openmp
) /* flag_openmp_simd */
37304 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37306 sb
= begin_omp_structured_block ();
37307 save
= cp_parser_begin_omp_structured_block (parser
);
37308 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37310 cp_parser_end_omp_structured_block (parser
, save
);
37311 tree body
= finish_omp_structured_block (sb
);
37314 return c_finish_omp_master (loc
, body
);
37317 if (!flag_openmp
) /* flag_openmp_simd */
37319 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37325 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37327 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
37330 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37332 return c_finish_omp_master (loc
,
37333 cp_parser_omp_structured_block (parser
, if_p
));
37337 # pragma omp ordered new-line
37341 # pragma omp ordered ordered-clauses new-line
37342 structured-block */
37344 #define OMP_ORDERED_CLAUSE_MASK \
37345 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37348 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37349 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37352 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
37353 enum pragma_context context
, bool *if_p
)
37355 location_t loc
= pragma_tok
->location
;
37357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37359 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37360 const char *p
= IDENTIFIER_POINTER (id
);
37362 if (strcmp (p
, "depend") == 0)
37364 if (!flag_openmp
) /* flag_openmp_simd */
37366 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37369 if (context
== pragma_stmt
)
37371 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
37372 "%<depend%> clause may only be used in compound "
37374 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37378 = cp_parser_omp_all_clauses (parser
,
37379 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
37380 "#pragma omp ordered", pragma_tok
);
37381 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
37387 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
37388 "#pragma omp ordered", pragma_tok
);
37390 if (!flag_openmp
/* flag_openmp_simd */
37391 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
37394 c_finish_omp_ordered (loc
, clauses
,
37395 cp_parser_omp_structured_block (parser
, if_p
));
37402 { section-sequence }
37405 section-directive[opt] structured-block
37406 section-sequence section-directive structured-block */
37409 cp_parser_omp_sections_scope (cp_parser
*parser
)
37411 tree stmt
, substmt
;
37412 bool error_suppress
= false;
37415 matching_braces braces
;
37416 if (!braces
.require_open (parser
))
37419 stmt
= push_stmt_list ();
37421 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
37422 != PRAGMA_OMP_SECTION
)
37424 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37425 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37426 add_stmt (substmt
);
37431 tok
= cp_lexer_peek_token (parser
->lexer
);
37432 if (tok
->type
== CPP_CLOSE_BRACE
)
37434 if (tok
->type
== CPP_EOF
)
37437 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
37439 cp_lexer_consume_token (parser
->lexer
);
37440 cp_parser_require_pragma_eol (parser
, tok
);
37441 error_suppress
= false;
37443 else if (!error_suppress
)
37445 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
37446 error_suppress
= true;
37449 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37450 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37451 add_stmt (substmt
);
37453 braces
.require_close (parser
);
37455 substmt
= pop_stmt_list (stmt
);
37457 stmt
= make_node (OMP_SECTIONS
);
37458 TREE_TYPE (stmt
) = void_type_node
;
37459 OMP_SECTIONS_BODY (stmt
) = substmt
;
37466 # pragma omp sections sections-clause[optseq] newline
37469 #define OMP_SECTIONS_CLAUSE_MASK \
37470 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37477 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
37478 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
37481 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37483 strcat (p_name
, " sections");
37484 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
37486 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37488 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37492 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
37493 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
37496 ret
= cp_parser_omp_sections_scope (parser
);
37498 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
37504 # pragma omp parallel parallel-clause[optseq] new-line
37506 # pragma omp parallel for parallel-for-clause[optseq] new-line
37508 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37512 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37513 structured-block */
37515 #define OMP_PARALLEL_CLAUSE_MASK \
37516 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37527 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37528 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37531 tree stmt
, clauses
, block
;
37533 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37535 strcat (p_name
, " parallel");
37536 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
37537 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37538 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
37539 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
37540 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
37542 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37544 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37545 if (cclauses
== NULL
)
37546 cclauses
= cclauses_buf
;
37548 cp_lexer_consume_token (parser
->lexer
);
37549 if (!flag_openmp
) /* flag_openmp_simd */
37550 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37552 block
= begin_omp_parallel ();
37553 save
= cp_parser_begin_omp_structured_block (parser
);
37554 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37556 cp_parser_end_omp_structured_block (parser
, save
);
37557 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37559 if (ret
== NULL_TREE
)
37561 OMP_PARALLEL_COMBINED (stmt
) = 1;
37564 /* When combined with distribute, parallel has to be followed by for.
37565 #pragma omp target parallel is allowed though. */
37567 && (mask
& (OMP_CLAUSE_MASK_1
37568 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37570 error_at (loc
, "expected %<for%> after %qs", p_name
);
37571 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37574 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37576 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37577 const char *p
= IDENTIFIER_POINTER (id
);
37578 if (strcmp (p
, "master") == 0)
37580 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37581 cclauses
= cclauses_buf
;
37583 cp_lexer_consume_token (parser
->lexer
);
37584 block
= begin_omp_parallel ();
37585 save
= cp_parser_begin_omp_structured_block (parser
);
37586 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
37588 cp_parser_end_omp_structured_block (parser
, save
);
37589 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37591 OMP_PARALLEL_COMBINED (stmt
) = 1;
37592 if (ret
== NULL_TREE
)
37596 else if (!flag_openmp
) /* flag_openmp_simd */
37598 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37601 else if (strcmp (p
, "sections") == 0)
37603 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37604 cclauses
= cclauses_buf
;
37606 cp_lexer_consume_token (parser
->lexer
);
37607 block
= begin_omp_parallel ();
37608 save
= cp_parser_begin_omp_structured_block (parser
);
37609 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
37610 cp_parser_end_omp_structured_block (parser
, save
);
37611 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37613 OMP_PARALLEL_COMBINED (stmt
) = 1;
37617 else if (!flag_openmp
) /* flag_openmp_simd */
37619 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37623 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37627 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
37628 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
37631 block
= begin_omp_parallel ();
37632 save
= cp_parser_begin_omp_structured_block (parser
);
37633 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37634 cp_parser_end_omp_structured_block (parser
, save
);
37635 stmt
= finish_omp_parallel (clauses
, block
);
37640 # pragma omp single single-clause[optseq] new-line
37641 structured-block */
37643 #define OMP_SINGLE_CLAUSE_MASK \
37644 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37650 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37652 tree stmt
= make_node (OMP_SINGLE
);
37653 TREE_TYPE (stmt
) = void_type_node
;
37654 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37656 OMP_SINGLE_CLAUSES (stmt
)
37657 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
37658 "#pragma omp single", pragma_tok
);
37659 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37661 return add_stmt (stmt
);
37665 # pragma omp task task-clause[optseq] new-line
37666 structured-block */
37668 #define OMP_TASK_CLAUSE_MASK \
37669 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37682 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37684 tree clauses
, block
;
37687 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
37688 "#pragma omp task", pragma_tok
);
37689 block
= begin_omp_task ();
37690 save
= cp_parser_begin_omp_structured_block (parser
);
37691 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37692 cp_parser_end_omp_structured_block (parser
, save
);
37693 return finish_omp_task (clauses
, block
);
37697 # pragma omp taskwait new-line
37700 # pragma omp taskwait taskwait-clause[opt] new-line */
37702 #define OMP_TASKWAIT_CLAUSE_MASK \
37703 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37706 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
37709 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
37710 "#pragma omp taskwait", pragma_tok
);
37714 tree stmt
= make_node (OMP_TASK
);
37715 TREE_TYPE (stmt
) = void_node
;
37716 OMP_TASK_CLAUSES (stmt
) = clauses
;
37717 OMP_TASK_BODY (stmt
) = NULL_TREE
;
37718 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37722 finish_omp_taskwait ();
37726 # pragma omp taskyield new-line */
37729 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
37731 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37732 finish_omp_taskyield ();
37736 # pragma omp taskgroup new-line
37740 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37742 #define OMP_TASKGROUP_CLAUSE_MASK \
37743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37746 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37749 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
37750 "#pragma omp taskgroup", pragma_tok
);
37751 return c_finish_omp_taskgroup (input_location
,
37752 cp_parser_omp_structured_block (parser
,
37759 # pragma omp threadprivate (variable-list) */
37762 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
37766 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
37767 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37769 finish_omp_threadprivate (vars
);
37773 # pragma omp cancel cancel-clause[optseq] new-line */
37775 #define OMP_CANCEL_CLAUSE_MASK \
37776 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37783 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
37785 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
37786 "#pragma omp cancel", pragma_tok
);
37787 finish_omp_cancel (clauses
);
37791 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37793 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37794 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37800 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
37801 enum pragma_context context
)
37804 bool point_seen
= false;
37806 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37808 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37809 const char *p
= IDENTIFIER_POINTER (id
);
37811 if (strcmp (p
, "point") == 0)
37813 cp_lexer_consume_token (parser
->lexer
);
37819 cp_parser_error (parser
, "expected %<point%>");
37820 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37824 if (context
!= pragma_compound
)
37826 if (context
== pragma_stmt
)
37827 error_at (pragma_tok
->location
,
37828 "%<#pragma %s%> may only be used in compound statements",
37829 "omp cancellation point");
37831 cp_parser_error (parser
, "expected declaration specifiers");
37832 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37836 clauses
= cp_parser_omp_all_clauses (parser
,
37837 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
37838 "#pragma omp cancellation point",
37840 finish_omp_cancellation_point (clauses
);
37844 #pragma omp distribute distribute-clause[optseq] new-line
37847 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37848 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37855 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
37856 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37859 tree clauses
, sb
, ret
;
37861 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37863 strcat (p_name
, " distribute");
37864 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
37866 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37868 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37869 const char *p
= IDENTIFIER_POINTER (id
);
37871 bool parallel
= false;
37873 if (strcmp (p
, "simd") == 0)
37876 parallel
= strcmp (p
, "parallel") == 0;
37877 if (parallel
|| simd
)
37879 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37880 if (cclauses
== NULL
)
37881 cclauses
= cclauses_buf
;
37882 cp_lexer_consume_token (parser
->lexer
);
37883 if (!flag_openmp
) /* flag_openmp_simd */
37886 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37889 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37892 sb
= begin_omp_structured_block ();
37893 save
= cp_parser_begin_omp_structured_block (parser
);
37895 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37898 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
37900 cp_parser_end_omp_structured_block (parser
, save
);
37901 tree body
= finish_omp_structured_block (sb
);
37904 ret
= make_node (OMP_DISTRIBUTE
);
37905 TREE_TYPE (ret
) = void_type_node
;
37906 OMP_FOR_BODY (ret
) = body
;
37907 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37908 SET_EXPR_LOCATION (ret
, loc
);
37913 if (!flag_openmp
) /* flag_openmp_simd */
37915 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37919 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37923 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
37924 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
37927 keep_next_level (true);
37928 sb
= begin_omp_structured_block ();
37929 save
= cp_parser_begin_omp_structured_block (parser
);
37931 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
37933 cp_parser_end_omp_structured_block (parser
, save
);
37934 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37940 # pragma omp teams teams-clause[optseq] new-line
37941 structured-block */
37943 #define OMP_TEAMS_CLAUSE_MASK \
37944 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37953 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
37954 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37957 tree clauses
, sb
, ret
;
37959 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37961 strcat (p_name
, " teams");
37962 mask
|= OMP_TEAMS_CLAUSE_MASK
;
37964 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37966 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37967 const char *p
= IDENTIFIER_POINTER (id
);
37968 if (strcmp (p
, "distribute") == 0)
37970 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37971 if (cclauses
== NULL
)
37972 cclauses
= cclauses_buf
;
37974 cp_lexer_consume_token (parser
->lexer
);
37975 if (!flag_openmp
) /* flag_openmp_simd */
37976 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37978 keep_next_level (true);
37979 sb
= begin_omp_structured_block ();
37980 save
= cp_parser_begin_omp_structured_block (parser
);
37981 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
37983 cp_parser_end_omp_structured_block (parser
, save
);
37984 tree body
= finish_omp_structured_block (sb
);
37987 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
37988 ret
= make_node (OMP_TEAMS
);
37989 TREE_TYPE (ret
) = void_type_node
;
37990 OMP_TEAMS_CLAUSES (ret
) = clauses
;
37991 OMP_TEAMS_BODY (ret
) = body
;
37992 OMP_TEAMS_COMBINED (ret
) = 1;
37993 SET_EXPR_LOCATION (ret
, loc
);
37994 return add_stmt (ret
);
37997 if (!flag_openmp
) /* flag_openmp_simd */
37999 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38003 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38007 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
38008 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38011 tree stmt
= make_node (OMP_TEAMS
);
38012 TREE_TYPE (stmt
) = void_type_node
;
38013 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
38014 keep_next_level (true);
38015 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38016 SET_EXPR_LOCATION (stmt
, loc
);
38018 return add_stmt (stmt
);
38022 # pragma omp target data target-data-clause[optseq] new-line
38023 structured-block */
38025 #define OMP_TARGET_DATA_CLAUSE_MASK \
38026 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38032 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38035 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
38036 "#pragma omp target data", pragma_tok
);
38038 for (tree
*pc
= &clauses
; *pc
;)
38040 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38041 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38044 case GOMP_MAP_ALWAYS_TO
:
38045 case GOMP_MAP_FROM
:
38046 case GOMP_MAP_ALWAYS_FROM
:
38047 case GOMP_MAP_TOFROM
:
38048 case GOMP_MAP_ALWAYS_TOFROM
:
38049 case GOMP_MAP_ALLOC
:
38052 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38053 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38054 case GOMP_MAP_ALWAYS_POINTER
:
38058 error_at (OMP_CLAUSE_LOCATION (*pc
),
38059 "%<#pragma omp target data%> with map-type other "
38060 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38061 "on %<map%> clause");
38062 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38065 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
)
38067 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38073 error_at (pragma_tok
->location
,
38074 "%<#pragma omp target data%> must contain at least "
38075 "one %<map%> or %<use_device_ptr%> clause");
38079 tree stmt
= make_node (OMP_TARGET_DATA
);
38080 TREE_TYPE (stmt
) = void_type_node
;
38081 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
38083 keep_next_level (true);
38084 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38086 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38087 return add_stmt (stmt
);
38091 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38092 structured-block */
38094 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38095 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38102 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38103 enum pragma_context context
)
38105 bool data_seen
= false;
38106 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38108 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38109 const char *p
= IDENTIFIER_POINTER (id
);
38111 if (strcmp (p
, "data") == 0)
38113 cp_lexer_consume_token (parser
->lexer
);
38119 cp_parser_error (parser
, "expected %<data%>");
38120 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38124 if (context
== pragma_stmt
)
38126 error_at (pragma_tok
->location
,
38127 "%<#pragma %s%> may only be used in compound statements",
38128 "omp target enter data");
38129 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38134 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
38135 "#pragma omp target enter data", pragma_tok
);
38137 for (tree
*pc
= &clauses
; *pc
;)
38139 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38140 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38143 case GOMP_MAP_ALWAYS_TO
:
38144 case GOMP_MAP_ALLOC
:
38147 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38148 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38149 case GOMP_MAP_ALWAYS_POINTER
:
38153 error_at (OMP_CLAUSE_LOCATION (*pc
),
38154 "%<#pragma omp target enter data%> with map-type other "
38155 "than %<to%> or %<alloc%> on %<map%> clause");
38156 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38159 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38165 error_at (pragma_tok
->location
,
38166 "%<#pragma omp target enter data%> must contain at least "
38167 "one %<map%> clause");
38171 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
38172 TREE_TYPE (stmt
) = void_type_node
;
38173 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
38174 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38175 return add_stmt (stmt
);
38179 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38180 structured-block */
38182 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38183 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38190 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38191 enum pragma_context context
)
38193 bool data_seen
= false;
38194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38196 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38197 const char *p
= IDENTIFIER_POINTER (id
);
38199 if (strcmp (p
, "data") == 0)
38201 cp_lexer_consume_token (parser
->lexer
);
38207 cp_parser_error (parser
, "expected %<data%>");
38208 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38212 if (context
== pragma_stmt
)
38214 error_at (pragma_tok
->location
,
38215 "%<#pragma %s%> may only be used in compound statements",
38216 "omp target exit data");
38217 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38222 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
38223 "#pragma omp target exit data", pragma_tok
);
38225 for (tree
*pc
= &clauses
; *pc
;)
38227 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38228 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38230 case GOMP_MAP_FROM
:
38231 case GOMP_MAP_ALWAYS_FROM
:
38232 case GOMP_MAP_RELEASE
:
38233 case GOMP_MAP_DELETE
:
38236 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38237 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38238 case GOMP_MAP_ALWAYS_POINTER
:
38242 error_at (OMP_CLAUSE_LOCATION (*pc
),
38243 "%<#pragma omp target exit data%> with map-type other "
38244 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38246 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38249 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38255 error_at (pragma_tok
->location
,
38256 "%<#pragma omp target exit data%> must contain at least "
38257 "one %<map%> clause");
38261 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
38262 TREE_TYPE (stmt
) = void_type_node
;
38263 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
38264 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38265 return add_stmt (stmt
);
38269 # pragma omp target update target-update-clause[optseq] new-line */
38271 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38272 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38280 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
38281 enum pragma_context context
)
38283 if (context
== pragma_stmt
)
38285 error_at (pragma_tok
->location
,
38286 "%<#pragma %s%> may only be used in compound statements",
38287 "omp target update");
38288 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38293 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
38294 "#pragma omp target update", pragma_tok
);
38295 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
38296 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
38298 error_at (pragma_tok
->location
,
38299 "%<#pragma omp target update%> must contain at least one "
38300 "%<from%> or %<to%> clauses");
38304 tree stmt
= make_node (OMP_TARGET_UPDATE
);
38305 TREE_TYPE (stmt
) = void_type_node
;
38306 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
38307 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38313 # pragma omp target target-clause[optseq] new-line
38314 structured-block */
38316 #define OMP_TARGET_CLAUSE_MASK \
38317 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38328 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
38329 enum pragma_context context
, bool *if_p
)
38331 tree
*pc
= NULL
, stmt
;
38335 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
38337 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38339 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38340 const char *p
= IDENTIFIER_POINTER (id
);
38341 enum tree_code ccode
= ERROR_MARK
;
38343 if (strcmp (p
, "teams") == 0)
38345 else if (strcmp (p
, "parallel") == 0)
38346 ccode
= OMP_PARALLEL
;
38347 else if (strcmp (p
, "simd") == 0)
38349 if (ccode
!= ERROR_MARK
)
38351 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
38352 char p_name
[sizeof ("#pragma omp target teams distribute "
38353 "parallel for simd")];
38355 cp_lexer_consume_token (parser
->lexer
);
38356 strcpy (p_name
, "#pragma omp target");
38357 if (!flag_openmp
) /* flag_openmp_simd */
38363 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38364 OMP_TARGET_CLAUSE_MASK
,
38368 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38369 OMP_TARGET_CLAUSE_MASK
,
38373 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38374 OMP_TARGET_CLAUSE_MASK
,
38378 gcc_unreachable ();
38380 return stmt
!= NULL_TREE
;
38382 keep_next_level (true);
38383 tree sb
= begin_omp_structured_block (), ret
;
38384 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
38388 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38389 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38393 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38394 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38398 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38399 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38403 gcc_unreachable ();
38405 cp_parser_end_omp_structured_block (parser
, save
);
38406 tree body
= finish_omp_structured_block (sb
);
38407 if (ret
== NULL_TREE
)
38409 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
38411 /* For combined target teams, ensure the num_teams and
38412 thread_limit clause expressions are evaluated on the host,
38413 before entering the target construct. */
38415 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38416 c
; c
= OMP_CLAUSE_CHAIN (c
))
38417 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
38418 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
38419 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
38421 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
38422 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
38423 if (expr
== error_mark_node
)
38425 tree tmp
= TARGET_EXPR_SLOT (expr
);
38427 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
38428 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
38429 OMP_CLAUSE_FIRSTPRIVATE
);
38430 OMP_CLAUSE_DECL (tc
) = tmp
;
38431 OMP_CLAUSE_CHAIN (tc
)
38432 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38433 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
38436 tree stmt
= make_node (OMP_TARGET
);
38437 TREE_TYPE (stmt
) = void_type_node
;
38438 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38439 OMP_TARGET_BODY (stmt
) = body
;
38440 OMP_TARGET_COMBINED (stmt
) = 1;
38441 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38443 pc
= &OMP_TARGET_CLAUSES (stmt
);
38444 goto check_clauses
;
38446 else if (!flag_openmp
) /* flag_openmp_simd */
38448 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38451 else if (strcmp (p
, "data") == 0)
38453 cp_lexer_consume_token (parser
->lexer
);
38454 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
38457 else if (strcmp (p
, "enter") == 0)
38459 cp_lexer_consume_token (parser
->lexer
);
38460 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
38463 else if (strcmp (p
, "exit") == 0)
38465 cp_lexer_consume_token (parser
->lexer
);
38466 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
38469 else if (strcmp (p
, "update") == 0)
38471 cp_lexer_consume_token (parser
->lexer
);
38472 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
38475 if (!flag_openmp
) /* flag_openmp_simd */
38477 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38481 stmt
= make_node (OMP_TARGET
);
38482 TREE_TYPE (stmt
) = void_type_node
;
38484 OMP_TARGET_CLAUSES (stmt
)
38485 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
38486 "#pragma omp target", pragma_tok
);
38487 pc
= &OMP_TARGET_CLAUSES (stmt
);
38488 keep_next_level (true);
38489 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38491 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38497 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38498 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38501 case GOMP_MAP_ALWAYS_TO
:
38502 case GOMP_MAP_FROM
:
38503 case GOMP_MAP_ALWAYS_FROM
:
38504 case GOMP_MAP_TOFROM
:
38505 case GOMP_MAP_ALWAYS_TOFROM
:
38506 case GOMP_MAP_ALLOC
:
38507 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38508 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38509 case GOMP_MAP_ALWAYS_POINTER
:
38512 error_at (OMP_CLAUSE_LOCATION (*pc
),
38513 "%<#pragma omp target%> with map-type other "
38514 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38515 "on %<map%> clause");
38516 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38519 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38525 # pragma acc cache (variable-list) new-line
38529 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
38531 tree stmt
, clauses
;
38533 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
38534 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38536 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
38538 stmt
= make_node (OACC_CACHE
);
38539 TREE_TYPE (stmt
) = void_type_node
;
38540 OACC_CACHE_CLAUSES (stmt
) = clauses
;
38541 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38548 # pragma acc data oacc-data-clause[optseq] new-line
38549 structured-block */
38551 #define OACC_DATA_CLAUSE_MASK \
38552 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38561 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38563 tree stmt
, clauses
, block
;
38566 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
38567 "#pragma acc data", pragma_tok
);
38569 block
= begin_omp_parallel ();
38570 save
= cp_parser_begin_omp_structured_block (parser
);
38571 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38572 cp_parser_end_omp_structured_block (parser
, save
);
38573 stmt
= finish_oacc_data (clauses
, block
);
38578 # pragma acc host_data <clauses> new-line
38579 structured-block */
38581 #define OACC_HOST_DATA_CLAUSE_MASK \
38582 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38585 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38587 tree stmt
, clauses
, block
;
38590 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
38591 "#pragma acc host_data", pragma_tok
);
38593 block
= begin_omp_parallel ();
38594 save
= cp_parser_begin_omp_structured_block (parser
);
38595 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38596 cp_parser_end_omp_structured_block (parser
, save
);
38597 stmt
= finish_oacc_host_data (clauses
, block
);
38602 # pragma acc declare oacc-data-clause[optseq] new-line
38605 #define OACC_DECLARE_CLAUSE_MASK \
38606 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38616 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
38618 tree clauses
, stmt
;
38619 bool error
= false;
38621 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
38622 "#pragma acc declare", pragma_tok
, true);
38625 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38627 error_at (pragma_tok
->location
,
38628 "no valid clauses specified in %<#pragma acc declare%>");
38632 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
38634 location_t loc
= OMP_CLAUSE_LOCATION (t
);
38635 tree decl
= OMP_CLAUSE_DECL (t
);
38636 if (!DECL_P (decl
))
38638 error_at (loc
, "array section in %<#pragma acc declare%>");
38642 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
38643 switch (OMP_CLAUSE_MAP_KIND (t
))
38645 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38646 case GOMP_MAP_ALLOC
:
38648 case GOMP_MAP_FORCE_DEVICEPTR
:
38649 case GOMP_MAP_DEVICE_RESIDENT
:
38652 case GOMP_MAP_LINK
:
38653 if (!global_bindings_p ()
38654 && (TREE_STATIC (decl
)
38655 || !DECL_EXTERNAL (decl
)))
38658 "%qD must be a global variable in "
38659 "%<#pragma acc declare link%>",
38667 if (global_bindings_p ())
38669 error_at (loc
, "invalid OpenACC clause at file scope");
38673 if (DECL_EXTERNAL (decl
))
38676 "invalid use of %<extern%> variable %qD "
38677 "in %<#pragma acc declare%>", decl
);
38681 else if (TREE_PUBLIC (decl
))
38684 "invalid use of %<global%> variable %qD "
38685 "in %<#pragma acc declare%>", decl
);
38692 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
38693 || lookup_attribute ("omp declare target link",
38694 DECL_ATTRIBUTES (decl
)))
38696 error_at (loc
, "variable %qD used more than once with "
38697 "%<#pragma acc declare%>", decl
);
38706 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
38707 id
= get_identifier ("omp declare target link");
38709 id
= get_identifier ("omp declare target");
38711 DECL_ATTRIBUTES (decl
)
38712 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
38713 if (global_bindings_p ())
38715 symtab_node
*node
= symtab_node::get (decl
);
38718 node
->offloadable
= 1;
38719 if (ENABLE_OFFLOADING
)
38721 g
->have_offload
= true;
38722 if (is_a
<varpool_node
*> (node
))
38723 vec_safe_push (offload_vars
, decl
);
38730 if (error
|| global_bindings_p ())
38733 stmt
= make_node (OACC_DECLARE
);
38734 TREE_TYPE (stmt
) = void_type_node
;
38735 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
38736 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38744 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38748 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38750 LOC is the location of the #pragma token.
38753 #define OACC_ENTER_DATA_CLAUSE_MASK \
38754 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38760 #define OACC_EXIT_DATA_CLAUSE_MASK \
38761 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38769 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38772 location_t loc
= pragma_tok
->location
;
38773 tree stmt
, clauses
;
38774 const char *p
= "";
38776 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38777 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38779 if (strcmp (p
, "data") != 0)
38781 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
38782 enter
? "enter" : "exit");
38783 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38787 cp_lexer_consume_token (parser
->lexer
);
38790 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
38791 "#pragma acc enter data", pragma_tok
);
38793 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
38794 "#pragma acc exit data", pragma_tok
);
38796 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38798 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
38799 enter
? "enter" : "exit");
38803 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
38804 TREE_TYPE (stmt
) = void_type_node
;
38805 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
38806 SET_EXPR_LOCATION (stmt
, loc
);
38812 # pragma acc loop oacc-loop-clause[optseq] new-line
38813 structured-block */
38815 #define OACC_LOOP_CLAUSE_MASK \
38816 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38828 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
38829 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
38831 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
38833 strcat (p_name
, " loop");
38834 mask
|= OACC_LOOP_CLAUSE_MASK
;
38836 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38840 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
38842 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
38844 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38847 tree block
= begin_omp_structured_block ();
38848 int save
= cp_parser_begin_omp_structured_block (parser
);
38849 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
38850 cp_parser_end_omp_structured_block (parser
, save
);
38851 add_stmt (finish_omp_structured_block (block
));
38857 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38862 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38866 #define OACC_KERNELS_CLAUSE_MASK \
38867 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38881 #define OACC_PARALLEL_CLAUSE_MASK \
38882 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38900 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
38901 char *p_name
, bool *if_p
)
38903 omp_clause_mask mask
;
38904 enum tree_code code
;
38905 switch (cp_parser_pragma_kind (pragma_tok
))
38907 case PRAGMA_OACC_KERNELS
:
38908 strcat (p_name
, " kernels");
38909 mask
= OACC_KERNELS_CLAUSE_MASK
;
38910 code
= OACC_KERNELS
;
38912 case PRAGMA_OACC_PARALLEL
:
38913 strcat (p_name
, " parallel");
38914 mask
= OACC_PARALLEL_CLAUSE_MASK
;
38915 code
= OACC_PARALLEL
;
38918 gcc_unreachable ();
38921 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38924 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38925 if (strcmp (p
, "loop") == 0)
38927 cp_lexer_consume_token (parser
->lexer
);
38928 tree block
= begin_omp_parallel ();
38930 tree stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
,
38932 protected_set_expr_location (stmt
, pragma_tok
->location
);
38933 return finish_omp_construct (code
, block
, clauses
);
38937 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
38939 tree block
= begin_omp_parallel ();
38940 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
38941 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38942 cp_parser_end_omp_structured_block (parser
, save
);
38943 return finish_omp_construct (code
, block
, clauses
);
38947 # pragma acc update oacc-update-clause[optseq] new-line
38950 #define OACC_UPDATE_CLAUSE_MASK \
38951 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38959 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
38961 tree stmt
, clauses
;
38963 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
38964 "#pragma acc update", pragma_tok
);
38966 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38968 error_at (pragma_tok
->location
,
38969 "%<#pragma acc update%> must contain at least one "
38970 "%<device%> or %<host%> or %<self%> clause");
38974 stmt
= make_node (OACC_UPDATE
);
38975 TREE_TYPE (stmt
) = void_type_node
;
38976 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
38977 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38983 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38985 LOC is the location of the #pragma token.
38988 #define OACC_WAIT_CLAUSE_MASK \
38989 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38992 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
38994 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
38995 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38997 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38998 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
39000 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
39001 "#pragma acc wait", pragma_tok
);
39003 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
39004 stmt
= finish_expr_stmt (stmt
);
39010 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39012 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39013 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39021 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
39022 enum pragma_context context
)
39024 bool first_p
= parser
->omp_declare_simd
== NULL
;
39025 cp_omp_declare_simd_data data
;
39028 data
.error_seen
= false;
39029 data
.fndecl_seen
= false;
39030 data
.tokens
= vNULL
;
39031 data
.clauses
= NULL_TREE
;
39032 /* It is safe to take the address of a local variable; it will only be
39033 used while this scope is live. */
39034 parser
->omp_declare_simd
= &data
;
39037 /* Store away all pragma tokens. */
39038 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39039 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39040 cp_lexer_consume_token (parser
->lexer
);
39041 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39042 parser
->omp_declare_simd
->error_seen
= true;
39043 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39044 struct cp_token_cache
*cp
39045 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
39046 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
39050 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
39051 cp_parser_pragma (parser
, context
, NULL
);
39054 case pragma_external
:
39055 cp_parser_declaration (parser
);
39057 case pragma_member
:
39058 cp_parser_member_declaration (parser
);
39060 case pragma_objc_icode
:
39061 cp_parser_block_declaration (parser
, /*statement_p=*/false);
39064 cp_parser_declaration_statement (parser
);
39067 if (parser
->omp_declare_simd
39068 && !parser
->omp_declare_simd
->error_seen
39069 && !parser
->omp_declare_simd
->fndecl_seen
)
39070 error_at (pragma_tok
->location
,
39071 "%<#pragma omp declare simd%> not immediately followed by "
39072 "function declaration or definition");
39073 data
.tokens
.release ();
39074 parser
->omp_declare_simd
= NULL
;
39078 /* Finalize #pragma omp declare simd clauses after direct declarator has
39079 been parsed, and put that into "omp declare simd" attribute. */
39082 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
39084 struct cp_token_cache
*ce
;
39085 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
39088 if (!data
->error_seen
&& data
->fndecl_seen
)
39090 error ("%<#pragma omp declare simd%> not immediately followed by "
39091 "a single function declaration or definition");
39092 data
->error_seen
= true;
39094 if (data
->error_seen
)
39097 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
39101 cp_parser_push_lexer_for_tokens (parser
, ce
);
39102 parser
->lexer
->in_pragma
= true;
39103 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
39104 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
39105 cp_lexer_consume_token (parser
->lexer
);
39106 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
39107 "#pragma omp declare simd", pragma_tok
);
39108 cp_parser_pop_lexer (parser
);
39110 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
39111 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
39112 TREE_CHAIN (c
) = attrs
;
39113 if (processing_template_decl
)
39114 ATTR_IS_DEPENDENT (c
) = 1;
39118 data
->fndecl_seen
= true;
39124 # pragma omp declare target new-line
39125 declarations and definitions
39126 # pragma omp end declare target new-line
39129 # pragma omp declare target ( extended-list ) new-line
39131 # pragma omp declare target declare-target-clauses[seq] new-line */
39133 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39134 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39138 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39140 tree clauses
= NULL_TREE
;
39141 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39143 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
39144 "#pragma omp declare target", pragma_tok
);
39145 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39147 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
39149 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
39150 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39154 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39155 scope_chain
->omp_declare_target_attribute
++;
39158 if (scope_chain
->omp_declare_target_attribute
)
39159 error_at (pragma_tok
->location
,
39160 "%<#pragma omp declare target%> with clauses in between "
39161 "%<#pragma omp declare target%> without clauses and "
39162 "%<#pragma omp end declare target%>");
39163 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
39165 tree t
= OMP_CLAUSE_DECL (c
), id
;
39166 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
39167 tree at2
= lookup_attribute ("omp declare target link",
39168 DECL_ATTRIBUTES (t
));
39169 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
39171 id
= get_identifier ("omp declare target link");
39172 std::swap (at1
, at2
);
39175 id
= get_identifier ("omp declare target");
39178 error_at (OMP_CLAUSE_LOCATION (c
),
39179 "%qD specified both in declare target %<link%> and %<to%>"
39185 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
39186 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
39189 symtab_node
*node
= symtab_node::get (t
);
39192 node
->offloadable
= 1;
39193 if (ENABLE_OFFLOADING
)
39195 g
->have_offload
= true;
39196 if (is_a
<varpool_node
*> (node
))
39197 vec_safe_push (offload_vars
, t
);
39205 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39207 const char *p
= "";
39208 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39210 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39211 p
= IDENTIFIER_POINTER (id
);
39213 if (strcmp (p
, "declare") == 0)
39215 cp_lexer_consume_token (parser
->lexer
);
39217 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39219 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39220 p
= IDENTIFIER_POINTER (id
);
39222 if (strcmp (p
, "target") == 0)
39223 cp_lexer_consume_token (parser
->lexer
);
39226 cp_parser_error (parser
, "expected %<target%>");
39227 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39233 cp_parser_error (parser
, "expected %<declare%>");
39234 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39237 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39238 if (!scope_chain
->omp_declare_target_attribute
)
39239 error_at (pragma_tok
->location
,
39240 "%<#pragma omp end declare target%> without corresponding "
39241 "%<#pragma omp declare target%>");
39243 scope_chain
->omp_declare_target_attribute
--;
39246 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39247 expression and optional initializer clause of
39248 #pragma omp declare reduction. We store the expression(s) as
39249 either 3, 6 or 7 special statements inside of the artificial function's
39250 body. The first two statements are DECL_EXPRs for the artificial
39251 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39252 expression that uses those variables.
39253 If there was any INITIALIZER clause, this is followed by further statements,
39254 the fourth and fifth statements are DECL_EXPRs for the artificial
39255 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39256 constructor variant (first token after open paren is not omp_priv),
39257 then the sixth statement is a statement with the function call expression
39258 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39259 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39260 to initialize the OMP_PRIV artificial variable and there is seventh
39261 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39264 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
39266 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
39267 gcc_assert (TYPE_REF_P (type
));
39268 type
= TREE_TYPE (type
);
39269 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
39270 DECL_ARTIFICIAL (omp_out
) = 1;
39271 pushdecl (omp_out
);
39272 add_decl_expr (omp_out
);
39273 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
39274 DECL_ARTIFICIAL (omp_in
) = 1;
39276 add_decl_expr (omp_in
);
39278 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
39280 keep_next_level (true);
39281 tree block
= begin_omp_structured_block ();
39282 combiner
= cp_parser_expression (parser
);
39283 finish_expr_stmt (combiner
);
39284 block
= finish_omp_structured_block (block
);
39287 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
39290 const char *p
= "";
39291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39293 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39294 p
= IDENTIFIER_POINTER (id
);
39297 if (strcmp (p
, "initializer") == 0)
39299 cp_lexer_consume_token (parser
->lexer
);
39300 matching_parens parens
;
39301 if (!parens
.require_open (parser
))
39305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39307 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39308 p
= IDENTIFIER_POINTER (id
);
39311 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
39312 DECL_ARTIFICIAL (omp_priv
) = 1;
39313 pushdecl (omp_priv
);
39314 add_decl_expr (omp_priv
);
39315 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
39316 DECL_ARTIFICIAL (omp_orig
) = 1;
39317 pushdecl (omp_orig
);
39318 add_decl_expr (omp_orig
);
39320 keep_next_level (true);
39321 block
= begin_omp_structured_block ();
39324 if (strcmp (p
, "omp_priv") == 0)
39326 bool is_direct_init
, is_non_constant_init
;
39328 cp_lexer_consume_token (parser
->lexer
);
39329 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39330 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
39331 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39332 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
39334 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
39335 == CPP_CLOSE_PAREN
))
39337 finish_omp_structured_block (block
);
39338 error ("invalid initializer clause");
39341 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
39342 &is_non_constant_init
);
39343 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
39344 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
39348 cp_parser_parse_tentatively (parser
);
39349 /* Don't create location wrapper nodes here. */
39350 auto_suppress_location_wrappers sentinel
;
39351 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
39352 /*check_dependency_p=*/true,
39353 /*template_p=*/NULL
,
39354 /*declarator_p=*/false,
39355 /*optional_p=*/false);
39356 vec
<tree
, va_gc
> *args
;
39357 if (fn_name
== error_mark_node
39358 || cp_parser_error_occurred (parser
)
39359 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39360 || ((args
= cp_parser_parenthesized_expression_list
39361 (parser
, non_attr
, /*cast_p=*/false,
39362 /*allow_expansion_p=*/true,
39363 /*non_constant_p=*/NULL
)),
39364 cp_parser_error_occurred (parser
)))
39366 finish_omp_structured_block (block
);
39367 cp_parser_abort_tentative_parse (parser
);
39368 cp_parser_error (parser
, "expected id-expression (arguments)");
39373 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
39374 if (arg
== omp_priv
39375 || (TREE_CODE (arg
) == ADDR_EXPR
39376 && TREE_OPERAND (arg
, 0) == omp_priv
))
39378 cp_parser_abort_tentative_parse (parser
);
39379 if (arg
== NULL_TREE
)
39380 error ("one of the initializer call arguments should be %<omp_priv%>"
39381 " or %<&omp_priv%>");
39382 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
39384 finish_expr_stmt (initializer
);
39387 block
= finish_omp_structured_block (block
);
39388 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
39392 add_decl_expr (omp_orig
);
39394 if (!parens
.require_close (parser
))
39398 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
39399 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
39406 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39407 initializer-clause[opt] new-line
39409 initializer-clause:
39410 initializer (omp_priv initializer)
39411 initializer (function-name (argument-list)) */
39414 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
39415 enum pragma_context
)
39417 auto_vec
<tree
> types
;
39418 enum tree_code reduc_code
= ERROR_MARK
;
39419 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
39421 cp_token
*first_token
;
39422 cp_token_cache
*cp
;
39426 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39427 p
= obstack_alloc (&declarator_obstack
, 0);
39429 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39432 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
39435 reduc_code
= PLUS_EXPR
;
39438 reduc_code
= MULT_EXPR
;
39441 reduc_code
= MINUS_EXPR
;
39444 reduc_code
= BIT_AND_EXPR
;
39447 reduc_code
= BIT_XOR_EXPR
;
39450 reduc_code
= BIT_IOR_EXPR
;
39453 reduc_code
= TRUTH_ANDIF_EXPR
;
39456 reduc_code
= TRUTH_ORIF_EXPR
;
39459 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
39462 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39463 "%<|%>, %<&&%>, %<||%> or identifier");
39467 if (reduc_code
!= ERROR_MARK
)
39468 cp_lexer_consume_token (parser
->lexer
);
39470 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
39471 if (reduc_id
== error_mark_node
)
39474 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39477 /* Types may not be defined in declare reduction type list. */
39478 const char *saved_message
;
39479 saved_message
= parser
->type_definition_forbidden_message
;
39480 parser
->type_definition_forbidden_message
39481 = G_("types may not be defined in declare reduction type list");
39482 bool saved_colon_corrects_to_scope_p
;
39483 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
39484 parser
->colon_corrects_to_scope_p
= false;
39485 bool saved_colon_doesnt_start_class_def_p
;
39486 saved_colon_doesnt_start_class_def_p
39487 = parser
->colon_doesnt_start_class_def_p
;
39488 parser
->colon_doesnt_start_class_def_p
= true;
39492 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39493 type
= cp_parser_type_id (parser
);
39494 if (type
== error_mark_node
)
39496 else if (ARITHMETIC_TYPE_P (type
)
39497 && (orig_reduc_id
== NULL_TREE
39498 || (TREE_CODE (type
) != COMPLEX_TYPE
39499 && (id_equal (orig_reduc_id
, "min")
39500 || id_equal (orig_reduc_id
, "max")))))
39501 error_at (loc
, "predeclared arithmetic type %qT in "
39502 "%<#pragma omp declare reduction%>", type
);
39503 else if (TREE_CODE (type
) == FUNCTION_TYPE
39504 || TREE_CODE (type
) == METHOD_TYPE
39505 || TREE_CODE (type
) == ARRAY_TYPE
)
39506 error_at (loc
, "function or array type %qT in "
39507 "%<#pragma omp declare reduction%>", type
);
39508 else if (TYPE_REF_P (type
))
39509 error_at (loc
, "reference type %qT in "
39510 "%<#pragma omp declare reduction%>", type
);
39511 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
39512 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
39513 "%<#pragma omp declare reduction%>", type
);
39515 types
.safe_push (type
);
39517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39518 cp_lexer_consume_token (parser
->lexer
);
39523 /* Restore the saved message. */
39524 parser
->type_definition_forbidden_message
= saved_message
;
39525 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39526 parser
->colon_doesnt_start_class_def_p
39527 = saved_colon_doesnt_start_class_def_p
;
39529 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
39530 || types
.is_empty ())
39533 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39537 first_token
= cp_lexer_peek_token (parser
->lexer
);
39540 FOR_EACH_VEC_ELT (types
, i
, type
)
39543 = build_function_type_list (void_type_node
,
39544 cp_build_reference_type (type
, false),
39546 tree this_reduc_id
= reduc_id
;
39547 if (!dependent_type_p (type
))
39548 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
39549 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
39550 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
39551 DECL_ARTIFICIAL (fndecl
) = 1;
39552 DECL_EXTERNAL (fndecl
) = 1;
39553 DECL_DECLARED_INLINE_P (fndecl
) = 1;
39554 DECL_IGNORED_P (fndecl
) = 1;
39555 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
39556 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
39557 DECL_ATTRIBUTES (fndecl
)
39558 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
39559 DECL_ATTRIBUTES (fndecl
));
39560 if (processing_template_decl
)
39561 fndecl
= push_template_decl (fndecl
);
39562 bool block_scope
= false;
39563 tree block
= NULL_TREE
;
39564 if (current_function_decl
)
39566 block_scope
= true;
39567 DECL_CONTEXT (fndecl
) = global_namespace
;
39568 if (!processing_template_decl
)
39571 else if (current_class_type
)
39575 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39576 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39577 cp_lexer_consume_token (parser
->lexer
);
39578 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39580 cp
= cp_token_cache_new (first_token
,
39581 cp_lexer_peek_nth_token (parser
->lexer
,
39584 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
39585 finish_member_declaration (fndecl
);
39586 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
39587 DECL_PENDING_INLINE_P (fndecl
) = 1;
39588 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
39593 DECL_CONTEXT (fndecl
) = current_namespace
;
39597 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
39599 block
= begin_omp_structured_block ();
39602 cp_parser_push_lexer_for_tokens (parser
, cp
);
39603 parser
->lexer
->in_pragma
= true;
39605 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
39608 finish_function (/*inline_p=*/false);
39610 DECL_CONTEXT (fndecl
) = current_function_decl
;
39612 cp_parser_pop_lexer (parser
);
39616 cp_parser_pop_lexer (parser
);
39618 finish_function (/*inline_p=*/false);
39621 DECL_CONTEXT (fndecl
) = current_function_decl
;
39622 block
= finish_omp_structured_block (block
);
39623 if (TREE_CODE (block
) == BIND_EXPR
)
39624 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
39625 else if (TREE_CODE (block
) == STATEMENT_LIST
)
39626 DECL_SAVED_TREE (fndecl
) = block
;
39627 if (processing_template_decl
)
39628 add_decl_expr (fndecl
);
39630 cp_check_omp_declare_reduction (fndecl
);
39631 if (cp
== NULL
&& types
.length () > 1)
39632 cp
= cp_token_cache_new (first_token
,
39633 cp_lexer_peek_nth_token (parser
->lexer
, 2));
39634 if (errs
!= errorcount
)
39638 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39641 /* Free any declarators allocated. */
39642 obstack_free (&declarator_obstack
, p
);
39646 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39647 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39648 initializer-clause[opt] new-line
39649 #pragma omp declare target new-line */
39652 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
39653 enum pragma_context context
)
39655 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39657 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39658 const char *p
= IDENTIFIER_POINTER (id
);
39660 if (strcmp (p
, "simd") == 0)
39662 cp_lexer_consume_token (parser
->lexer
);
39663 cp_parser_omp_declare_simd (parser
, pragma_tok
,
39667 cp_ensure_no_omp_declare_simd (parser
);
39668 if (strcmp (p
, "reduction") == 0)
39670 cp_lexer_consume_token (parser
->lexer
);
39671 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
39675 if (!flag_openmp
) /* flag_openmp_simd */
39677 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39680 if (strcmp (p
, "target") == 0)
39682 cp_lexer_consume_token (parser
->lexer
);
39683 cp_parser_omp_declare_target (parser
, pragma_tok
);
39687 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
39689 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39694 #pragma omp requires clauses[optseq] new-line */
39697 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
39700 enum omp_requires new_req
= (enum omp_requires
) 0;
39702 location_t loc
= pragma_tok
->location
;
39703 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39705 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39706 cp_lexer_consume_token (parser
->lexer
);
39710 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39712 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39713 const char *p
= IDENTIFIER_POINTER (id
);
39714 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39715 enum omp_requires this_req
= (enum omp_requires
) 0;
39717 if (!strcmp (p
, "unified_address"))
39718 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
39719 else if (!strcmp (p
, "unified_shared_memory"))
39720 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
39721 else if (!strcmp (p
, "dynamic_allocators"))
39722 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
39723 else if (!strcmp (p
, "reverse_offload"))
39724 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
39725 else if (!strcmp (p
, "atomic_default_mem_order"))
39727 cp_lexer_consume_token (parser
->lexer
);
39729 matching_parens parens
;
39730 if (parens
.require_open (parser
))
39732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39734 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39735 p
= IDENTIFIER_POINTER (id
);
39737 if (!strcmp (p
, "seq_cst"))
39739 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
39740 else if (!strcmp (p
, "relaxed"))
39742 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
39743 else if (!strcmp (p
, "acq_rel"))
39745 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
39749 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39750 "expected %<seq_cst%>, %<relaxed%> or "
39752 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
39754 cp_lexer_consume_token (parser
->lexer
);
39757 cp_lexer_consume_token (parser
->lexer
);
39759 if (!parens
.require_close (parser
))
39760 cp_parser_skip_to_closing_parenthesis (parser
,
39761 /*recovering=*/true,
39762 /*or_comma=*/false,
39768 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39776 error_at (cloc
, "expected %<unified_address%>, "
39777 "%<unified_shared_memory%>, "
39778 "%<dynamic_allocators%>, "
39779 "%<reverse_offload%> "
39780 "or %<atomic_default_mem_order%> clause");
39781 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39785 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
39786 "supported yet", p
);
39788 cp_lexer_consume_token (parser
->lexer
);
39791 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39793 if ((this_req
& new_req
) != 0)
39794 error_at (cloc
, "too many %qs clauses", p
);
39795 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
39796 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
39797 error_at (cloc
, "%qs clause used lexically after first "
39798 "target construct or offloading API", p
);
39800 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39802 error_at (cloc
, "too many %qs clauses",
39803 "atomic_default_mem_order");
39804 this_req
= (enum omp_requires
) 0;
39806 else if ((omp_requires_mask
39807 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39809 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
39810 " clause in a single compilation unit");
39812 = (enum omp_requires
)
39814 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
39816 else if ((omp_requires_mask
39817 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
39818 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
39819 "lexically after first %<atomic%> construct "
39820 "without memory order clause");
39821 new_req
= (enum omp_requires
) (new_req
| this_req
);
39823 = (enum omp_requires
) (omp_requires_mask
| this_req
);
39829 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39832 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
39838 #pragma omp taskloop taskloop-clause[optseq] new-line
39841 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39844 #define OMP_TASKLOOP_CLAUSE_MASK \
39845 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39863 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
39864 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39867 tree clauses
, sb
, ret
;
39869 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39871 strcat (p_name
, " taskloop");
39872 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
39873 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39875 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
39876 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
39878 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39880 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39881 const char *p
= IDENTIFIER_POINTER (id
);
39883 if (strcmp (p
, "simd") == 0)
39885 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39886 if (cclauses
== NULL
)
39887 cclauses
= cclauses_buf
;
39889 cp_lexer_consume_token (parser
->lexer
);
39890 if (!flag_openmp
) /* flag_openmp_simd */
39891 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39893 sb
= begin_omp_structured_block ();
39894 save
= cp_parser_begin_omp_structured_block (parser
);
39895 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39897 cp_parser_end_omp_structured_block (parser
, save
);
39898 tree body
= finish_omp_structured_block (sb
);
39901 ret
= make_node (OMP_TASKLOOP
);
39902 TREE_TYPE (ret
) = void_type_node
;
39903 OMP_FOR_BODY (ret
) = body
;
39904 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39905 SET_EXPR_LOCATION (ret
, loc
);
39910 if (!flag_openmp
) /* flag_openmp_simd */
39912 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39916 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39920 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
39921 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
39924 keep_next_level (true);
39925 sb
= begin_omp_structured_block ();
39926 save
= cp_parser_begin_omp_structured_block (parser
);
39928 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
39931 cp_parser_end_omp_structured_block (parser
, save
);
39932 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39939 # pragma acc routine oacc-routine-clause[optseq] new-line
39940 function-definition
39942 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39945 #define OACC_ROUTINE_CLAUSE_MASK \
39946 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39952 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39953 component, which must resolve to a declared namespace-scope
39954 function. The clauses are either processed directly (for a named
39955 function), or defered until the immediatley following declaration
39959 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
39960 enum pragma_context context
)
39962 gcc_checking_assert (context
== pragma_external
);
39963 /* The checking for "another pragma following this one" in the "no optional
39964 '( name )'" case makes sure that we dont re-enter. */
39965 gcc_checking_assert (parser
->oacc_routine
== NULL
);
39967 cp_oacc_routine_data data
;
39968 data
.error_seen
= false;
39969 data
.fndecl_seen
= false;
39970 data
.tokens
= vNULL
;
39971 data
.clauses
= NULL_TREE
;
39972 data
.loc
= pragma_tok
->location
;
39973 /* It is safe to take the address of a local variable; it will only be
39974 used while this scope is live. */
39975 parser
->oacc_routine
= &data
;
39977 /* Look for optional '( name )'. */
39978 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39980 matching_parens parens
;
39981 parens
.consume_open (parser
); /* '(' */
39983 /* We parse the name as an id-expression. If it resolves to
39984 anything other than a non-overloaded function at namespace
39985 scope, it's an error. */
39986 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39987 tree name
= cp_parser_id_expression (parser
,
39988 /*template_keyword_p=*/false,
39989 /*check_dependency_p=*/false,
39990 /*template_p=*/NULL
,
39991 /*declarator_p=*/false,
39992 /*optional_p=*/false);
39993 tree decl
= (identifier_p (name
)
39994 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
39996 if (name
!= error_mark_node
&& decl
== error_mark_node
)
39997 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
39999 if (decl
== error_mark_node
40000 || !parens
.require_close (parser
))
40002 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40003 parser
->oacc_routine
= NULL
;
40008 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
40009 "#pragma acc routine",
40010 cp_lexer_peek_token (parser
->lexer
));
40012 if (decl
&& is_overloaded_fn (decl
)
40013 && (TREE_CODE (decl
) != FUNCTION_DECL
40014 || DECL_FUNCTION_TEMPLATE_P (decl
)))
40016 error_at (name_loc
,
40017 "%<#pragma acc routine%> names a set of overloads");
40018 parser
->oacc_routine
= NULL
;
40022 /* Perhaps we should use the same rule as declarations in different
40024 if (!DECL_NAMESPACE_SCOPE_P (decl
))
40026 error_at (name_loc
,
40027 "%qD does not refer to a namespace scope function", decl
);
40028 parser
->oacc_routine
= NULL
;
40032 if (TREE_CODE (decl
) != FUNCTION_DECL
)
40034 error_at (name_loc
, "%qD does not refer to a function", decl
);
40035 parser
->oacc_routine
= NULL
;
40039 cp_finalize_oacc_routine (parser
, decl
, false);
40040 parser
->oacc_routine
= NULL
;
40042 else /* No optional '( name )'. */
40044 /* Store away all pragma tokens. */
40045 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
40046 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
40047 cp_lexer_consume_token (parser
->lexer
);
40048 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
40049 parser
->oacc_routine
->error_seen
= true;
40050 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40051 struct cp_token_cache
*cp
40052 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
40053 parser
->oacc_routine
->tokens
.safe_push (cp
);
40055 /* Emit a helpful diagnostic if there's another pragma following this
40057 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
40059 cp_ensure_no_oacc_routine (parser
);
40060 data
.tokens
.release ();
40061 /* ..., and then just keep going. */
40065 /* We only have to consider the pragma_external case here. */
40066 cp_parser_declaration (parser
);
40067 if (parser
->oacc_routine
40068 && !parser
->oacc_routine
->fndecl_seen
)
40069 cp_ensure_no_oacc_routine (parser
);
40071 parser
->oacc_routine
= NULL
;
40072 data
.tokens
.release ();
40076 /* Finalize #pragma acc routine clauses after direct declarator has
40080 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
40082 struct cp_token_cache
*ce
;
40083 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
40085 if (!data
->error_seen
&& data
->fndecl_seen
)
40087 error_at (data
->loc
,
40088 "%<#pragma acc routine%> not immediately followed by "
40089 "a single function declaration or definition");
40090 data
->error_seen
= true;
40092 if (data
->error_seen
)
40095 gcc_checking_assert (data
->tokens
.length () == 1);
40096 ce
= data
->tokens
[0];
40098 cp_parser_push_lexer_for_tokens (parser
, ce
);
40099 parser
->lexer
->in_pragma
= true;
40100 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
40102 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40103 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
40104 parser
->oacc_routine
->clauses
40105 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
40106 "#pragma acc routine", pragma_tok
);
40107 cp_parser_pop_lexer (parser
);
40108 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40114 /* Apply any saved OpenACC routine clauses to a just-parsed
40118 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
40120 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
40122 /* Keep going if we're in error reporting mode. */
40123 if (parser
->oacc_routine
->error_seen
40124 || fndecl
== error_mark_node
)
40127 if (parser
->oacc_routine
->fndecl_seen
)
40129 error_at (parser
->oacc_routine
->loc
,
40130 "%<#pragma acc routine%> not immediately followed by"
40131 " a single function declaration or definition");
40132 parser
->oacc_routine
= NULL
;
40135 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
40137 cp_ensure_no_oacc_routine (parser
);
40141 if (oacc_get_fn_attrib (fndecl
))
40143 error_at (parser
->oacc_routine
->loc
,
40144 "%<#pragma acc routine%> already applied to %qD", fndecl
);
40145 parser
->oacc_routine
= NULL
;
40149 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
40151 error_at (parser
->oacc_routine
->loc
,
40153 ? G_("%<#pragma acc routine%> must be applied before use")
40154 : G_("%<#pragma acc routine%> must be applied before "
40156 parser
->oacc_routine
= NULL
;
40160 /* Process the routine's dimension clauses. */
40161 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
40162 oacc_replace_fn_attrib (fndecl
, dims
);
40164 /* Add an "omp declare target" attribute. */
40165 DECL_ATTRIBUTES (fndecl
)
40166 = tree_cons (get_identifier ("omp declare target"),
40167 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
40169 /* Don't unset parser->oacc_routine here: we may still need it to
40170 diagnose wrong usage. But, remember that we've used this "#pragma acc
40172 parser
->oacc_routine
->fndecl_seen
= true;
40176 /* Main entry point to OpenMP statement pragmas. */
40179 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40182 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
40183 omp_clause_mask
mask (0);
40185 switch (cp_parser_pragma_kind (pragma_tok
))
40187 case PRAGMA_OACC_ATOMIC
:
40188 cp_parser_omp_atomic (parser
, pragma_tok
);
40190 case PRAGMA_OACC_CACHE
:
40191 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
40193 case PRAGMA_OACC_DATA
:
40194 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
40196 case PRAGMA_OACC_ENTER_DATA
:
40197 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
40199 case PRAGMA_OACC_EXIT_DATA
:
40200 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
40202 case PRAGMA_OACC_HOST_DATA
:
40203 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
40205 case PRAGMA_OACC_KERNELS
:
40206 case PRAGMA_OACC_PARALLEL
:
40207 strcpy (p_name
, "#pragma acc");
40208 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
40211 case PRAGMA_OACC_LOOP
:
40212 strcpy (p_name
, "#pragma acc");
40213 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40216 case PRAGMA_OACC_UPDATE
:
40217 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
40219 case PRAGMA_OACC_WAIT
:
40220 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
40222 case PRAGMA_OMP_ATOMIC
:
40223 cp_parser_omp_atomic (parser
, pragma_tok
);
40225 case PRAGMA_OMP_CRITICAL
:
40226 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
40228 case PRAGMA_OMP_DISTRIBUTE
:
40229 strcpy (p_name
, "#pragma omp");
40230 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
40233 case PRAGMA_OMP_FOR
:
40234 strcpy (p_name
, "#pragma omp");
40235 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
40238 case PRAGMA_OMP_MASTER
:
40239 strcpy (p_name
, "#pragma omp");
40240 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
40243 case PRAGMA_OMP_PARALLEL
:
40244 strcpy (p_name
, "#pragma omp");
40245 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
40248 case PRAGMA_OMP_SECTIONS
:
40249 strcpy (p_name
, "#pragma omp");
40250 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
40252 case PRAGMA_OMP_SIMD
:
40253 strcpy (p_name
, "#pragma omp");
40254 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
40257 case PRAGMA_OMP_SINGLE
:
40258 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
40260 case PRAGMA_OMP_TASK
:
40261 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
40263 case PRAGMA_OMP_TASKGROUP
:
40264 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
40266 case PRAGMA_OMP_TASKLOOP
:
40267 strcpy (p_name
, "#pragma omp");
40268 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40271 case PRAGMA_OMP_TEAMS
:
40272 strcpy (p_name
, "#pragma omp");
40273 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
40277 gcc_unreachable ();
40280 protected_set_expr_location (stmt
, pragma_tok
->location
);
40283 /* Transactional Memory parsing routines. */
40285 /* Parse a transaction attribute.
40291 We use this instead of cp_parser_attributes_opt for transactions to avoid
40292 the pedwarn in C++98 mode. */
40295 cp_parser_txn_attribute_opt (cp_parser
*parser
)
40298 tree attr_name
, attr
= NULL
;
40300 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
40301 return cp_parser_attributes_opt (parser
);
40303 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
40305 cp_lexer_consume_token (parser
->lexer
);
40306 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
40309 token
= cp_lexer_peek_token (parser
->lexer
);
40310 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
40312 token
= cp_lexer_consume_token (parser
->lexer
);
40314 attr_name
= (token
->type
== CPP_KEYWORD
40315 /* For keywords, use the canonical spelling,
40316 not the parsed identifier. */
40317 ? ridpointers
[(int) token
->keyword
]
40319 attr
= build_tree_list (attr_name
, NULL_TREE
);
40322 cp_parser_error (parser
, "expected identifier");
40324 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40326 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40330 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40332 transaction-statement:
40333 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40335 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40339 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
40341 unsigned char old_in
= parser
->in_transaction
;
40342 unsigned char this_in
= 1, new_in
;
40343 enum rid keyword
= token
->keyword
;
40344 tree stmt
, attrs
, noex
;
40346 cp_lexer_consume_token (parser
->lexer
);
40348 if (keyword
== RID_TRANSACTION_RELAXED
40349 || keyword
== RID_SYNCHRONIZED
)
40350 this_in
|= TM_STMT_ATTR_RELAXED
;
40353 attrs
= cp_parser_txn_attribute_opt (parser
);
40355 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40358 /* Parse a noexcept specification. */
40359 if (keyword
== RID_ATOMIC_NOEXCEPT
)
40360 noex
= boolean_true_node
;
40361 else if (keyword
== RID_ATOMIC_CANCEL
)
40363 /* cancel-and-throw is unimplemented. */
40364 sorry ("atomic_cancel");
40368 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
40370 /* Keep track if we're in the lexical scope of an outer transaction. */
40371 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
40373 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
40375 parser
->in_transaction
= new_in
;
40376 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
40377 parser
->in_transaction
= old_in
;
40379 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
40384 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40386 transaction-expression:
40387 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40388 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40392 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
40394 unsigned char old_in
= parser
->in_transaction
;
40395 unsigned char this_in
= 1;
40399 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40401 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40402 || keyword
== RID_TRANSACTION_RELAXED
);
40406 keyword
== RID_TRANSACTION_RELAXED
40407 ? G_("%<__transaction_relaxed%> without transactional memory "
40409 : G_("%<__transaction_atomic%> without transactional memory "
40410 "support enabled"));
40412 token
= cp_parser_require_keyword (parser
, keyword
,
40413 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40414 : RT_TRANSACTION_RELAXED
));
40415 gcc_assert (token
!= NULL
);
40417 if (keyword
== RID_TRANSACTION_RELAXED
)
40418 this_in
|= TM_STMT_ATTR_RELAXED
;
40420 /* Set this early. This might mean that we allow transaction_cancel in
40421 an expression that we find out later actually has to be a constexpr.
40422 However, we expect that cxx_constant_value will be able to deal with
40423 this; also, if the noexcept has no constexpr, then what we parse next
40424 really is a transaction's body. */
40425 parser
->in_transaction
= this_in
;
40427 /* Parse a noexcept specification. */
40428 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
40431 if (!noex
|| !noex_expr
40432 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
40434 matching_parens parens
;
40435 parens
.require_open (parser
);
40437 expr
= cp_parser_expression (parser
);
40438 expr
= finish_parenthesized_expr (expr
);
40440 parens
.require_close (parser
);
40444 /* The only expression that is available got parsed for the noexcept
40445 already. noexcept is true then. */
40447 noex
= boolean_true_node
;
40450 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
40451 parser
->in_transaction
= old_in
;
40453 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
40454 return error_mark_node
;
40456 return (flag_tm
? expr
: error_mark_node
);
40459 /* Parse a function-transaction-block.
40461 function-transaction-block:
40462 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40464 __transaction_atomic txn-attribute[opt] function-try-block
40465 __transaction_relaxed ctor-initializer[opt] function-body
40466 __transaction_relaxed function-try-block
40470 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
40472 unsigned char old_in
= parser
->in_transaction
;
40473 unsigned char new_in
= 1;
40474 tree compound_stmt
, stmt
, attrs
;
40477 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40478 || keyword
== RID_TRANSACTION_RELAXED
);
40479 token
= cp_parser_require_keyword (parser
, keyword
,
40480 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40481 : RT_TRANSACTION_RELAXED
));
40482 gcc_assert (token
!= NULL
);
40484 if (keyword
== RID_TRANSACTION_RELAXED
)
40485 new_in
|= TM_STMT_ATTR_RELAXED
;
40488 attrs
= cp_parser_txn_attribute_opt (parser
);
40490 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40493 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
40495 parser
->in_transaction
= new_in
;
40497 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
40498 cp_parser_function_try_block (parser
);
40500 cp_parser_ctor_initializer_opt_and_function_body
40501 (parser
, /*in_function_try_block=*/false);
40503 parser
->in_transaction
= old_in
;
40505 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
40508 /* Parse a __transaction_cancel statement.
40511 __transaction_cancel txn-attribute[opt] ;
40512 __transaction_cancel txn-attribute[opt] throw-expression ;
40514 ??? Cancel and throw is not yet implemented. */
40517 cp_parser_transaction_cancel (cp_parser
*parser
)
40520 bool is_outer
= false;
40523 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
40524 RT_TRANSACTION_CANCEL
);
40525 gcc_assert (token
!= NULL
);
40527 attrs
= cp_parser_txn_attribute_opt (parser
);
40529 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
40531 /* ??? Parse cancel-and-throw here. */
40533 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
40537 error_at (token
->location
, "%<__transaction_cancel%> without "
40538 "transactional memory support enabled");
40539 return error_mark_node
;
40541 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
40543 error_at (token
->location
, "%<__transaction_cancel%> within a "
40544 "%<__transaction_relaxed%>");
40545 return error_mark_node
;
40549 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
40550 && !is_tm_may_cancel_outer (current_function_decl
))
40552 error_at (token
->location
, "outer %<__transaction_cancel%> not "
40553 "within outer %<__transaction_atomic%>");
40554 error_at (token
->location
,
40555 " or a %<transaction_may_cancel_outer%> function");
40556 return error_mark_node
;
40559 else if (parser
->in_transaction
== 0)
40561 error_at (token
->location
, "%<__transaction_cancel%> not within "
40562 "%<__transaction_atomic%>");
40563 return error_mark_node
;
40566 stmt
= build_tm_abort_call (token
->location
, is_outer
);
40574 static GTY (()) cp_parser
*the_parser
;
40577 /* Special handling for the first token or line in the file. The first
40578 thing in the file might be #pragma GCC pch_preprocess, which loads a
40579 PCH file, which is a GC collection point. So we need to handle this
40580 first pragma without benefit of an existing lexer structure.
40582 Always returns one token to the caller in *FIRST_TOKEN. This is
40583 either the true first token of the file, or the first token after
40584 the initial pragma. */
40587 cp_parser_initial_pragma (cp_token
*first_token
)
40591 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40592 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
40595 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40596 if (first_token
->type
== CPP_STRING
)
40598 name
= first_token
->u
.value
;
40600 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40601 if (first_token
->type
!= CPP_PRAGMA_EOL
)
40602 error_at (first_token
->location
,
40603 "junk at end of %<#pragma GCC pch_preprocess%>");
40606 error_at (first_token
->location
, "expected string literal");
40608 /* Skip to the end of the pragma. */
40609 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
40610 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40612 /* Now actually load the PCH file. */
40614 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
40616 /* Read one more token to return to our caller. We have to do this
40617 after reading the PCH file in, since its pointers have to be
40619 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40622 /* Parse a pragma GCC ivdep. */
40625 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
40627 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40631 /* Parse a pragma GCC unroll. */
40633 static unsigned short
40634 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
40636 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
40637 tree expr
= cp_parser_constant_expression (parser
);
40638 unsigned short unroll
;
40639 expr
= maybe_constant_value (expr
);
40640 HOST_WIDE_INT lunroll
= 0;
40641 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
40642 || TREE_CODE (expr
) != INTEGER_CST
40643 || (lunroll
= tree_to_shwi (expr
)) < 0
40644 || lunroll
>= USHRT_MAX
)
40646 error_at (location
, "%<#pragma GCC unroll%> requires an"
40647 " assignment-expression that evaluates to a non-negative"
40648 " integral constant less than %u", USHRT_MAX
);
40653 unroll
= (unsigned short)lunroll
;
40657 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40661 /* Normal parsing of a pragma token. Here we can (and must) use the
40665 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
40667 cp_token
*pragma_tok
;
40672 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40673 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
40674 parser
->lexer
->in_pragma
= true;
40676 id
= cp_parser_pragma_kind (pragma_tok
);
40677 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
40678 cp_ensure_no_omp_declare_simd (parser
);
40681 case PRAGMA_GCC_PCH_PREPROCESS
:
40682 error_at (pragma_tok
->location
,
40683 "%<#pragma GCC pch_preprocess%> must be first");
40686 case PRAGMA_OMP_BARRIER
:
40689 case pragma_compound
:
40690 cp_parser_omp_barrier (parser
, pragma_tok
);
40693 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40694 "used in compound statements", "omp barrier");
40701 case PRAGMA_OMP_DEPOBJ
:
40704 case pragma_compound
:
40705 cp_parser_omp_depobj (parser
, pragma_tok
);
40708 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40709 "used in compound statements", "omp depobj");
40716 case PRAGMA_OMP_FLUSH
:
40719 case pragma_compound
:
40720 cp_parser_omp_flush (parser
, pragma_tok
);
40723 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40724 "used in compound statements", "omp flush");
40731 case PRAGMA_OMP_TASKWAIT
:
40734 case pragma_compound
:
40735 cp_parser_omp_taskwait (parser
, pragma_tok
);
40738 error_at (pragma_tok
->location
,
40739 "%<#pragma %s%> may only be used in compound statements",
40747 case PRAGMA_OMP_TASKYIELD
:
40750 case pragma_compound
:
40751 cp_parser_omp_taskyield (parser
, pragma_tok
);
40754 error_at (pragma_tok
->location
,
40755 "%<#pragma %s%> may only be used in compound statements",
40763 case PRAGMA_OMP_CANCEL
:
40766 case pragma_compound
:
40767 cp_parser_omp_cancel (parser
, pragma_tok
);
40770 error_at (pragma_tok
->location
,
40771 "%<#pragma %s%> may only be used in compound statements",
40779 case PRAGMA_OMP_CANCELLATION_POINT
:
40780 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
40783 case PRAGMA_OMP_THREADPRIVATE
:
40784 cp_parser_omp_threadprivate (parser
, pragma_tok
);
40787 case PRAGMA_OMP_DECLARE
:
40788 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
40790 case PRAGMA_OACC_DECLARE
:
40791 cp_parser_oacc_declare (parser
, pragma_tok
);
40794 case PRAGMA_OACC_ENTER_DATA
:
40795 if (context
== pragma_stmt
)
40797 error_at (pragma_tok
->location
,
40798 "%<#pragma %s%> may only be used in compound statements",
40802 else if (context
!= pragma_compound
)
40804 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40807 case PRAGMA_OACC_EXIT_DATA
:
40808 if (context
== pragma_stmt
)
40810 error_at (pragma_tok
->location
,
40811 "%<#pragma %s%> may only be used in compound statements",
40815 else if (context
!= pragma_compound
)
40817 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40820 case PRAGMA_OACC_ROUTINE
:
40821 if (context
!= pragma_external
)
40823 error_at (pragma_tok
->location
,
40824 "%<#pragma acc routine%> must be at file scope");
40827 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
40830 case PRAGMA_OACC_UPDATE
:
40831 if (context
== pragma_stmt
)
40833 error_at (pragma_tok
->location
,
40834 "%<#pragma %s%> may only be used in compound statements",
40838 else if (context
!= pragma_compound
)
40840 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40843 case PRAGMA_OACC_WAIT
:
40844 if (context
== pragma_stmt
)
40846 error_at (pragma_tok
->location
,
40847 "%<#pragma %s%> may only be used in compound statements",
40851 else if (context
!= pragma_compound
)
40853 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40856 case PRAGMA_OACC_ATOMIC
:
40857 case PRAGMA_OACC_CACHE
:
40858 case PRAGMA_OACC_DATA
:
40859 case PRAGMA_OACC_HOST_DATA
:
40860 case PRAGMA_OACC_KERNELS
:
40861 case PRAGMA_OACC_PARALLEL
:
40862 case PRAGMA_OACC_LOOP
:
40863 case PRAGMA_OMP_ATOMIC
:
40864 case PRAGMA_OMP_CRITICAL
:
40865 case PRAGMA_OMP_DISTRIBUTE
:
40866 case PRAGMA_OMP_FOR
:
40867 case PRAGMA_OMP_MASTER
:
40868 case PRAGMA_OMP_PARALLEL
:
40869 case PRAGMA_OMP_SECTIONS
:
40870 case PRAGMA_OMP_SIMD
:
40871 case PRAGMA_OMP_SINGLE
:
40872 case PRAGMA_OMP_TASK
:
40873 case PRAGMA_OMP_TASKGROUP
:
40874 case PRAGMA_OMP_TASKLOOP
:
40875 case PRAGMA_OMP_TEAMS
:
40876 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40878 stmt
= push_omp_privatization_clauses (false);
40879 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40880 pop_omp_privatization_clauses (stmt
);
40883 case PRAGMA_OMP_REQUIRES
:
40884 return cp_parser_omp_requires (parser
, pragma_tok
);
40886 case PRAGMA_OMP_ORDERED
:
40887 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40889 stmt
= push_omp_privatization_clauses (false);
40890 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
40891 pop_omp_privatization_clauses (stmt
);
40894 case PRAGMA_OMP_TARGET
:
40895 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
40897 stmt
= push_omp_privatization_clauses (false);
40898 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
40899 pop_omp_privatization_clauses (stmt
);
40902 case PRAGMA_OMP_END_DECLARE_TARGET
:
40903 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
40906 case PRAGMA_OMP_SECTION
:
40907 error_at (pragma_tok
->location
,
40908 "%<#pragma omp section%> may only be used in "
40909 "%<#pragma omp sections%> construct");
40914 if (context
== pragma_external
)
40916 error_at (pragma_tok
->location
,
40917 "%<#pragma GCC ivdep%> must be inside a function");
40920 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
40921 unsigned short unroll
;
40922 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40923 if (tok
->type
== CPP_PRAGMA
40924 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
40926 tok
= cp_lexer_consume_token (parser
->lexer
);
40927 unroll
= cp_parser_pragma_unroll (parser
, tok
);
40928 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40932 if (tok
->type
!= CPP_KEYWORD
40933 || (tok
->keyword
!= RID_FOR
40934 && tok
->keyword
!= RID_WHILE
40935 && tok
->keyword
!= RID_DO
))
40937 cp_parser_error (parser
, "for, while or do statement expected");
40940 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40944 case PRAGMA_UNROLL
:
40946 if (context
== pragma_external
)
40948 error_at (pragma_tok
->location
,
40949 "%<#pragma GCC unroll%> must be inside a function");
40952 const unsigned short unroll
40953 = cp_parser_pragma_unroll (parser
, pragma_tok
);
40955 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40956 if (tok
->type
== CPP_PRAGMA
40957 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
40959 tok
= cp_lexer_consume_token (parser
->lexer
);
40960 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
40961 tok
= cp_lexer_peek_token (the_parser
->lexer
);
40965 if (tok
->type
!= CPP_KEYWORD
40966 || (tok
->keyword
!= RID_FOR
40967 && tok
->keyword
!= RID_WHILE
40968 && tok
->keyword
!= RID_DO
))
40970 cp_parser_error (parser
, "for, while or do statement expected");
40973 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
40978 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
40979 c_invoke_pragma_handler (id
);
40983 cp_parser_error (parser
, "expected declaration specifiers");
40987 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40991 /* The interface the pragma parsers have to the lexer. */
40994 pragma_lex (tree
*value
, location_t
*loc
)
40996 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
40997 enum cpp_ttype ret
= tok
->type
;
40999 *value
= tok
->u
.value
;
41001 *loc
= tok
->location
;
41003 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
41005 else if (ret
== CPP_STRING
)
41006 *value
= cp_parser_string_literal (the_parser
, false, false);
41009 if (ret
== CPP_KEYWORD
)
41011 cp_lexer_consume_token (the_parser
->lexer
);
41018 /* External interface. */
41020 /* Parse one entire translation unit. */
41023 c_parse_file (void)
41025 static bool already_called
= false;
41027 if (already_called
)
41028 fatal_error (input_location
,
41029 "inter-module optimizations not implemented for C++");
41030 already_called
= true;
41032 the_parser
= cp_parser_new ();
41033 push_deferring_access_checks (flag_access_control
41034 ? dk_no_deferred
: dk_no_check
);
41035 cp_parser_translation_unit (the_parser
);
41038 finish_translation_unit ();
41041 /* Create an identifier for a generic parameter type (a synthesized
41042 template parameter implied by `auto' or a concept identifier). */
41044 static GTY(()) int generic_parm_count
;
41046 make_generic_type_name ()
41049 sprintf (buf
, "auto:%d", ++generic_parm_count
);
41050 return get_identifier (buf
);
41053 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41054 (creating a new template parameter list if necessary). Returns the newly
41055 created template type parm. */
41058 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
41060 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
41062 /* Before committing to modifying any scope, if we're in an
41063 implicit template scope, and we're trying to synthesize a
41064 constrained parameter, try to find a previous parameter with
41065 the same name. This is the same-type rule for abbreviated
41066 function templates.
41068 NOTE: We can generate implicit parameters when tentatively
41069 parsing a nested name specifier, only to reject that parse
41070 later. However, matching the same template-id as part of a
41071 direct-declarator should generate an identical template
41072 parameter, so this rule will merge them. */
41073 if (parser
->implicit_template_scope
&& constr
)
41075 tree t
= parser
->implicit_template_parms
;
41078 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
41080 tree d
= TREE_VALUE (t
);
41081 if (TREE_CODE (d
) == PARM_DECL
)
41082 /* Return the TEMPLATE_PARM_INDEX. */
41083 d
= DECL_INITIAL (d
);
41086 t
= TREE_CHAIN (t
);
41090 /* We are either continuing a function template that already contains implicit
41091 template parameters, creating a new fully-implicit function template, or
41092 extending an existing explicit function template with implicit template
41095 cp_binding_level
*const entry_scope
= current_binding_level
;
41097 bool become_template
= false;
41098 cp_binding_level
*parent_scope
= 0;
41100 if (parser
->implicit_template_scope
)
41102 gcc_assert (parser
->implicit_template_parms
);
41104 current_binding_level
= parser
->implicit_template_scope
;
41108 /* Roll back to the existing template parameter scope (in the case of
41109 extending an explicit function template) or introduce a new template
41110 parameter scope ahead of the function parameter scope (or class scope
41111 in the case of out-of-line member definitions). The function scope is
41112 added back after template parameter synthesis below. */
41114 cp_binding_level
*scope
= entry_scope
;
41116 while (scope
->kind
== sk_function_parms
)
41118 parent_scope
= scope
;
41119 scope
= scope
->level_chain
;
41121 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
41123 /* If not defining a class, then any class scope is a scope level in
41124 an out-of-line member definition. In this case simply wind back
41125 beyond the first such scope to inject the template parameter list.
41126 Otherwise wind back to the class being defined. The latter can
41127 occur in class member friend declarations such as:
41133 friend void A::foo (auto);
41136 The template parameter list synthesized for the friend declaration
41137 must be injected in the scope of 'B'. This can also occur in
41138 erroneous cases such as:
41144 void B::foo (auto) {}
41147 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41148 but, nevertheless, the template parameter list synthesized for the
41149 declarator should be injected into the scope of 'A' as if the
41150 ill-formed template was specified explicitly. */
41152 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
41154 parent_scope
= scope
;
41155 scope
= scope
->level_chain
;
41159 current_binding_level
= scope
;
41161 if (scope
->kind
!= sk_template_parms
41162 || !function_being_declared_is_template_p (parser
))
41164 /* Introduce a new template parameter list for implicit template
41167 become_template
= true;
41169 parser
->implicit_template_scope
41170 = begin_scope (sk_template_parms
, NULL
);
41172 ++processing_template_decl
;
41174 parser
->fully_implicit_function_template_p
= true;
41175 ++parser
->num_template_parameter_lists
;
41179 /* Synthesize implicit template parameters at the end of the explicit
41180 template parameter list. */
41182 gcc_assert (current_template_parms
);
41184 parser
->implicit_template_scope
= scope
;
41186 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41187 parser
->implicit_template_parms
41188 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
41192 /* Synthesize a new template parameter and track the current template
41193 parameter chain with implicit_template_parms. */
41195 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
41196 tree synth_id
= make_generic_type_name ();
41197 tree synth_tmpl_parm
;
41198 bool non_type
= false;
41200 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
41202 = finish_template_type_parm (class_type_node
, synth_id
);
41203 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
41205 = finish_constrained_template_template_parm (proto
, synth_id
);
41208 synth_tmpl_parm
= copy_decl (proto
);
41209 DECL_NAME (synth_tmpl_parm
) = synth_id
;
41213 // Attach the constraint to the parm before processing.
41214 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
41215 TREE_TYPE (node
) = constr
;
41217 = process_template_parm (parser
->implicit_template_parms
,
41220 /*non_type=*/non_type
,
41221 /*param_pack=*/false);
41223 // Chain the new parameter to the list of implicit parameters.
41224 if (parser
->implicit_template_parms
)
41225 parser
->implicit_template_parms
41226 = TREE_CHAIN (parser
->implicit_template_parms
);
41228 parser
->implicit_template_parms
= new_parm
;
41230 tree new_decl
= get_local_decls ();
41232 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41233 new_decl
= DECL_INITIAL (new_decl
);
41235 /* If creating a fully implicit function template, start the new implicit
41236 template parameter list with this synthesized type, otherwise grow the
41237 current template parameter list. */
41239 if (become_template
)
41241 parent_scope
->level_chain
= current_binding_level
;
41243 tree new_parms
= make_tree_vec (1);
41244 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
41245 current_template_parms
= tree_cons (size_int (processing_template_decl
),
41246 new_parms
, current_template_parms
);
41250 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41251 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
41252 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
41253 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
41256 // If the new parameter was constrained, we need to add that to the
41257 // constraints in the template parameter list.
41258 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
41260 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
41261 reqs
= conjoin_constraints (reqs
, req
);
41262 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
41265 current_binding_level
= entry_scope
;
41270 /* Finish the declaration of a fully implicit function template. Such a
41271 template has no explicit template parameter list so has not been through the
41272 normal template head and tail processing. synthesize_implicit_template_parm
41273 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41274 provided if the declaration is a class member such that its template
41275 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41276 form is returned. Otherwise NULL_TREE is returned. */
41279 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
41281 gcc_assert (parser
->fully_implicit_function_template_p
);
41283 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
41284 && DECL_VIRTUAL_P (member_decl_opt
))
41286 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
41287 "implicit templates may not be %<virtual%>");
41288 DECL_VIRTUAL_P (member_decl_opt
) = false;
41291 if (member_decl_opt
)
41292 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
41293 end_template_decl ();
41295 parser
->fully_implicit_function_template_p
= false;
41296 parser
->implicit_template_parms
= 0;
41297 parser
->implicit_template_scope
= 0;
41298 --parser
->num_template_parameter_lists
;
41300 return member_decl_opt
;
41303 /* Like finish_fully_implicit_template, but to be used in error
41304 recovery, rearranging scopes so that we restore the state we had
41305 before synthesize_implicit_template_parm inserted the implement
41306 template parms scope. */
41309 abort_fully_implicit_template (cp_parser
*parser
)
41311 cp_binding_level
*return_to_scope
= current_binding_level
;
41313 if (parser
->implicit_template_scope
41314 && return_to_scope
!= parser
->implicit_template_scope
)
41316 cp_binding_level
*child
= return_to_scope
;
41317 for (cp_binding_level
*scope
= child
->level_chain
;
41318 scope
!= parser
->implicit_template_scope
;
41319 scope
= child
->level_chain
)
41321 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
41322 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
41323 current_binding_level
= parser
->implicit_template_scope
;
41326 return_to_scope
= return_to_scope
->level_chain
;
41328 finish_fully_implicit_template (parser
, NULL
);
41330 gcc_assert (current_binding_level
== return_to_scope
);
41333 /* Helper function for diagnostics that have complained about things
41334 being used with 'extern "C"' linkage.
41336 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41339 maybe_show_extern_c_location (void)
41341 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
41342 inform (the_parser
->innermost_linkage_specification_location
,
41343 "%<extern \"C\"%> linkage started here");
41346 #include "gt-cp-parser.h"