2 Copyright (C) 2000-2018 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 "c-family/name-hint.h"
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token
=
56 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant
{
62 /* floating-point literal */
66 /* %<__FUNCTION__%> */
68 /* %<__PRETTY_FUNCTION__%> */
76 /* %<typeid%> operator */
78 /* non-constant compound literals */
86 /* an array reference */
92 /* the address of a label */
106 /* calls to overloaded operators */
110 /* a comma operator */
112 /* a call to a constructor */
114 /* a transaction expression */
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error
{
124 /* is not a class or namespace */
126 /* is not a class, namespace, or enumeration */
130 /* The various kinds of required token */
131 enum required_token
{
133 RT_SEMICOLON
, /* ';' */
134 RT_OPEN_PAREN
, /* '(' */
135 RT_CLOSE_BRACE
, /* '}' */
136 RT_OPEN_BRACE
, /* '{' */
137 RT_CLOSE_SQUARE
, /* ']' */
138 RT_OPEN_SQUARE
, /* '[' */
142 RT_GREATER
, /* '>' */
144 RT_ELLIPSIS
, /* '...' */
148 RT_COLON_SCOPE
, /* ':' or '::' */
149 RT_CLOSE_PAREN
, /* ')' */
150 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
151 RT_PRAGMA_EOL
, /* end of line */
152 RT_NAME
, /* identifier */
154 /* The type is CPP_KEYWORD */
156 RT_DELETE
, /* delete */
157 RT_RETURN
, /* return */
158 RT_WHILE
, /* while */
159 RT_EXTERN
, /* extern */
160 RT_STATIC_ASSERT
, /* static_assert */
161 RT_DECLTYPE
, /* decltype */
162 RT_OPERATOR
, /* operator */
163 RT_CLASS
, /* class */
164 RT_TEMPLATE
, /* template */
165 RT_NAMESPACE
, /* namespace */
166 RT_USING
, /* using */
169 RT_CATCH
, /* catch */
170 RT_THROW
, /* throw */
171 RT_LABEL
, /* __label__ */
172 RT_AT_TRY
, /* @try */
173 RT_AT_SYNCHRONIZED
, /* @synchronized */
174 RT_AT_THROW
, /* @throw */
176 RT_SELECT
, /* selection-statement */
177 RT_ITERATION
, /* iteration-statement */
178 RT_JUMP
, /* jump-statement */
179 RT_CLASS_KEY
, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
194 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
196 saved (parser
->in_type_id_in_expr_p
)
197 { parser
->in_type_id_in_expr_p
= set
; }
198 ~type_id_in_expr_sentinel ()
199 { parser
->in_type_id_in_expr_p
= saved
; }
204 static cp_lexer
*cp_lexer_new_main
206 static cp_lexer
*cp_lexer_new_from_tokens
207 (cp_token_cache
*tokens
);
208 static void cp_lexer_destroy
210 static int cp_lexer_saving_tokens
212 static cp_token
*cp_lexer_token_at
213 (cp_lexer
*, cp_token_position
);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer
*, cp_token
*);
216 static inline cp_token
*cp_lexer_peek_token
218 static cp_token
*cp_lexer_peek_nth_token
219 (cp_lexer
*, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer
*, enum cpp_ttype
);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer
*, enum cpp_ttype
);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer
*, enum rid
);
226 static cp_token
*cp_lexer_consume_token
228 static void cp_lexer_purge_token
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer
*, cp_token_position
);
232 static void cp_lexer_save_tokens
234 static void cp_lexer_commit_tokens
236 static void cp_lexer_rollback_tokens
238 static void cp_lexer_print_token
239 (FILE *, cp_token
*);
240 static inline bool cp_lexer_debugging_p
242 static void cp_lexer_start_debugging
243 (cp_lexer
*) ATTRIBUTE_UNUSED
;
244 static void cp_lexer_stop_debugging
245 (cp_lexer
*) ATTRIBUTE_UNUSED
;
247 static cp_token_cache
*cp_token_cache_new
248 (cp_token
*, cp_token
*);
250 static void cp_parser_initial_pragma
253 static bool cp_parser_omp_declare_reduction_exprs
255 static void cp_finalize_oacc_routine
256 (cp_parser
*, tree
, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream
;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand
;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
278 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
279 cp_token
*start_token
, unsigned num
,
280 cp_token
*curr_token
)
282 unsigned i
, nprinted
;
286 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
292 num
= buffer
->length ();
294 if (start_token
== NULL
)
295 start_token
= buffer
->address ();
297 if (start_token
> buffer
->address ())
299 cp_lexer_print_token (file
, &(*buffer
)[0]);
300 fprintf (file
, " ... ");
305 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
307 if (token
== start_token
)
314 if (token
== curr_token
)
315 fprintf (file
, "[[");
317 cp_lexer_print_token (file
, token
);
319 if (token
== curr_token
)
320 fprintf (file
, "]]");
326 case CPP_CLOSE_BRACE
:
336 if (i
== num
&& i
< buffer
->length ())
338 fprintf (file
, " ... ");
339 cp_lexer_print_token (file
, &buffer
->last ());
342 fprintf (file
, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
349 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
351 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
355 debug (vec
<cp_token
, va_gc
> &ref
)
357 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
361 debug (vec
<cp_token
, va_gc
> *ptr
)
366 fprintf (stderr
, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
374 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
378 fprintf (file
, "%s: ", desc
);
379 print_node_brief (file
, "", t
, 0);
384 /* Dump parser context C to FILE. */
387 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
389 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
391 print_node_brief (file
, "", c
->object_type
, 0);
392 fprintf (file
, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
399 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
402 cp_parser_context
*c
;
404 fprintf (file
, "Parsing context stack:\n");
405 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
407 fprintf (file
, "\t#%u: ", i
);
408 cp_debug_print_context (file
, c
);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
416 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
419 fprintf (file
, "%s: true\n", desc
);
423 /* Print an unparsed function entry UF to FILE. */
426 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
429 cp_default_arg_entry
*default_arg_fn
;
432 fprintf (file
, "\tFunctions with default args:\n");
434 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
437 fprintf (file
, "\t\tClass type: ");
438 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
439 fprintf (file
, "\t\tDeclaration: ");
440 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
441 fprintf (file
, "\n");
444 fprintf (file
, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
448 print_node_brief (file
, "", fn
, 0);
451 fprintf (file
, "\n");
453 fprintf (file
, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
457 print_node_brief (file
, "", fn
, 0);
460 fprintf (file
, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
467 cp_debug_print_unparsed_queues (FILE *file
,
468 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
471 cp_unparsed_functions_entry
*uf
;
473 fprintf (file
, "Unparsed functions\n");
474 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
476 fprintf (file
, "#%u:\n", i
);
477 cp_debug_print_unparsed_function (file
, uf
);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
486 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
488 cp_token
*next_token
, *first_token
, *start_token
;
493 next_token
= parser
->lexer
->next_token
;
494 first_token
= parser
->lexer
->buffer
->address ();
495 start_token
= (next_token
> first_token
+ window_size
/ 2)
496 ? next_token
- window_size
/ 2
498 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
507 cp_debug_parser (FILE *file
, cp_parser
*parser
)
509 const size_t window_size
= 20;
511 expanded_location eloc
;
516 fprintf (file
, "Parser state\n\n");
517 fprintf (file
, "Number of tokens: %u\n",
518 vec_safe_length (parser
->lexer
->buffer
));
519 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
520 cp_debug_print_tree_if_set (file
, "Object scope",
521 parser
->object_scope
);
522 cp_debug_print_tree_if_set (file
, "Qualifying scope",
523 parser
->qualifying_scope
);
524 cp_debug_print_context_stack (file
, parser
->context
);
525 cp_debug_print_flag (file
, "Allow GNU extensions",
526 parser
->allow_gnu_extensions_p
);
527 cp_debug_print_flag (file
, "'>' token is greater-than",
528 parser
->greater_than_is_operator_p
);
529 cp_debug_print_flag (file
, "Default args allowed in current "
530 "parameter list", parser
->default_arg_ok_p
);
531 cp_debug_print_flag (file
, "Parsing integral constant-expression",
532 parser
->integral_constant_expression_p
);
533 cp_debug_print_flag (file
, "Allow non-constant expression in current "
534 "constant-expression",
535 parser
->allow_non_integral_constant_expression_p
);
536 cp_debug_print_flag (file
, "Seen non-constant expression",
537 parser
->non_integral_constant_expression_p
);
538 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
540 parser
->local_variables_forbidden_p
);
541 cp_debug_print_flag (file
, "In unbraced linkage specification",
542 parser
->in_unbraced_linkage_specification_p
);
543 cp_debug_print_flag (file
, "Parsing a declarator",
544 parser
->in_declarator_p
);
545 cp_debug_print_flag (file
, "In template argument list",
546 parser
->in_template_argument_list_p
);
547 cp_debug_print_flag (file
, "Parsing an iteration statement",
548 parser
->in_statement
& IN_ITERATION_STMT
);
549 cp_debug_print_flag (file
, "Parsing a switch statement",
550 parser
->in_statement
& IN_SWITCH_STMT
);
551 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
552 parser
->in_statement
& IN_OMP_BLOCK
);
553 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
554 parser
->in_statement
& IN_OMP_FOR
);
555 cp_debug_print_flag (file
, "Parsing an if statement",
556 parser
->in_statement
& IN_IF_STMT
);
557 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
558 "context", parser
->in_type_id_in_expr_p
);
559 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
560 parser
->implicit_extern_c
);
561 cp_debug_print_flag (file
, "String expressions should be translated "
562 "to execution character set",
563 parser
->translate_strings_p
);
564 cp_debug_print_flag (file
, "Parsing function body outside of a "
565 "local class", parser
->in_function_body
);
566 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
567 parser
->colon_corrects_to_scope_p
);
568 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
569 parser
->colon_doesnt_start_class_def_p
);
570 if (parser
->type_definition_forbidden_message
)
571 fprintf (file
, "Error message for forbidden type definitions: %s\n",
572 parser
->type_definition_forbidden_message
);
573 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
574 fprintf (file
, "Number of class definitions in progress: %u\n",
575 parser
->num_classes_being_defined
);
576 fprintf (file
, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser
->num_template_parameter_lists
);
578 cp_debug_parser_tokens (file
, parser
, window_size
);
579 token
= parser
->lexer
->next_token
;
580 fprintf (file
, "Next token to parse:\n");
581 fprintf (file
, "\tToken: ");
582 cp_lexer_print_token (file
, token
);
583 eloc
= expand_location (token
->location
);
584 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
585 fprintf (file
, "\tLine: %d\n", eloc
.line
);
586 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
590 debug (cp_parser
&ref
)
592 cp_debug_parser (stderr
, &ref
);
596 debug (cp_parser
*ptr
)
601 fprintf (stderr
, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
607 cp_lexer_alloc (void)
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
616 /* Initially we are not debugging. */
617 lexer
->debugging_p
= false;
619 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
621 /* Create the buffer. */
622 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
632 cp_lexer_new_main (void)
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token
);
642 lexer
= cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer
->buffer
->quick_push (token
);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token
.type
!= CPP_EOF
)
650 cp_lexer_get_preprocessor_token (lexer
, &token
);
651 vec_safe_push (lexer
->buffer
, token
);
654 lexer
->last_token
= lexer
->buffer
->address ()
655 + lexer
->buffer
->length ()
657 lexer
->next_token
= lexer
->buffer
->length ()
658 ? lexer
->buffer
->address ()
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
665 gcc_assert (!lexer
->next_token
->purged_p
);
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
673 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
675 cp_token
*first
= cache
->first
;
676 cp_token
*last
= cache
->last
;
677 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
679 /* We do not own the buffer. */
680 lexer
->buffer
= NULL
;
681 lexer
->next_token
= first
== last
? &eof_token
: first
;
682 lexer
->last_token
= last
;
684 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
686 /* Initially we are not debugging. */
687 lexer
->debugging_p
= false;
689 gcc_assert (!lexer
->next_token
->purged_p
);
693 /* Frees all resources associated with LEXER. */
696 cp_lexer_destroy (cp_lexer
*lexer
)
698 vec_free (lexer
->buffer
);
699 lexer
->saved_tokens
.release ();
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
713 cp_lexer_debugging_p (cp_lexer
*lexer
)
715 if (!LEXER_DEBUGGING_ENABLED_P
)
718 return lexer
->debugging_p
;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
725 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
727 return lexer
->next_token
- previous_p
;
730 static inline cp_token
*
731 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
737 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
739 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer
*lexer
)
745 if (lexer
->next_token
== &eof_token
)
746 return lexer
->last_token
- 1;
748 return cp_lexer_token_position (lexer
, true);
751 static inline cp_token
*
752 cp_lexer_previous_token (cp_lexer
*lexer
)
754 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
756 /* Skip past purged tokens. */
759 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
763 return cp_lexer_token_at (lexer
, tp
);
766 /* nonzero if we are presently saving tokens. */
769 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
771 return lexer
->saved_tokens
.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
780 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
782 static int is_extern_c
= 0;
784 /* Get a new token from the preprocessor. */
786 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
787 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
788 token
->keyword
= RID_MAX
;
789 token
->purged_p
= false;
790 token
->error_reported
= false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c
+= pending_lang_change
;
796 pending_lang_change
= 0;
797 token
->implicit_extern_c
= is_extern_c
> 0;
799 /* Check to see if this token is a keyword. */
800 if (token
->type
== CPP_NAME
)
802 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
804 /* Mark this token as a keyword. */
805 token
->type
= CPP_KEYWORD
;
806 /* Record which keyword. */
807 token
->keyword
= C_RID_CODE (token
->u
.value
);
811 if (warn_cxx11_compat
812 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
813 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
815 /* Warn about the C++0x keyword (but still treat it as
817 warning (OPT_Wc__11_compat
,
818 "identifier %qE is a keyword in C++11",
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
826 token
->keyword
= RID_MAX
;
829 else if (token
->type
== CPP_AT_NAME
)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token
->type
= CPP_KEYWORD
;
833 switch (C_RID_CODE (token
->u
.value
))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
843 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
844 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
845 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
846 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
847 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
848 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
849 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
850 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
851 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
856 /* Update the globals input_location and the input file stack from TOKEN. */
858 cp_lexer_set_source_position_from_token (cp_token
*token
)
860 if (token
->type
!= CPP_EOF
)
862 input_location
= token
->location
;
866 /* Update the globals input_location and the input file stack from LEXER. */
868 cp_lexer_set_source_position (cp_lexer
*lexer
)
870 cp_token
*token
= cp_lexer_peek_token (lexer
);
871 cp_lexer_set_source_position_from_token (token
);
874 /* Return a pointer to the next token in the token stream, but do not
877 static inline cp_token
*
878 cp_lexer_peek_token (cp_lexer
*lexer
)
880 if (cp_lexer_debugging_p (lexer
))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
883 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
884 putc ('\n', cp_lexer_debug_stream
);
886 return lexer
->next_token
;
889 /* Return true if the next token has the indicated TYPE. */
892 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
894 return cp_lexer_peek_token (lexer
)->type
== type
;
897 /* Return true if the next token does not have the indicated TYPE. */
900 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
902 return !cp_lexer_next_token_is (lexer
, type
);
905 /* Return true if the next token is the indicated KEYWORD. */
908 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
910 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
914 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
916 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
920 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
922 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
925 /* Return true if the next token is not the indicated KEYWORD. */
928 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
930 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
933 /* Return true if KEYWORD can start a decl-specifier. */
936 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
943 /* Storage classes. */
949 /* Elaborated type specifiers. */
955 /* Simple type specifiers. */
969 /* GNU extensions. */
972 /* C++0x extensions. */
974 case RID_UNDERLYING_TYPE
:
979 if (keyword
>= RID_FIRST_INT_N
980 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
981 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
987 /* Return true if the next token is a keyword for a decl-specifier. */
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
994 token
= cp_lexer_peek_token (lexer
);
995 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
998 /* Returns TRUE iff the token T begins a decltype type. */
1001 token_is_decltype (cp_token
*t
)
1003 return (t
->keyword
== RID_DECLTYPE
1004 || t
->type
== CPP_DECLTYPE
);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1010 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1012 cp_token
*t
= cp_lexer_peek_token (lexer
);
1013 return token_is_decltype (t
);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1020 saved_checks_value (struct tree_check
*check_value
)
1022 /* Perform any access checks that were deferred. */
1023 vec
<deferred_access_check
, va_gc
> *checks
;
1024 deferred_access_check
*chk
;
1025 checks
= check_value
->checks
;
1029 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1030 perform_or_defer_access_check (chk
->binfo
,
1032 chk
->diag_decl
, tf_warning_or_error
);
1034 /* Return the stored value. */
1035 return check_value
->value
;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1045 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1049 /* N is 1-based, not zero-based. */
1052 if (cp_lexer_debugging_p (lexer
))
1053 fprintf (cp_lexer_debug_stream
,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1057 token
= lexer
->next_token
;
1058 gcc_assert (!n
|| token
!= &eof_token
);
1062 if (token
== lexer
->last_token
)
1068 if (!token
->purged_p
)
1072 if (cp_lexer_debugging_p (lexer
))
1074 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1075 putc ('\n', cp_lexer_debug_stream
);
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1085 cp_lexer_consume_token (cp_lexer
* lexer
)
1087 cp_token
*token
= lexer
->next_token
;
1089 gcc_assert (token
!= &eof_token
);
1090 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1094 lexer
->next_token
++;
1095 if (lexer
->next_token
== lexer
->last_token
)
1097 lexer
->next_token
= &eof_token
;
1102 while (lexer
->next_token
->purged_p
);
1104 cp_lexer_set_source_position_from_token (token
);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer
))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1110 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1111 putc ('\n', cp_lexer_debug_stream
);
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1122 cp_lexer_purge_token (cp_lexer
*lexer
)
1124 cp_token
*tok
= lexer
->next_token
;
1126 gcc_assert (tok
!= &eof_token
);
1127 tok
->purged_p
= true;
1128 tok
->location
= UNKNOWN_LOCATION
;
1129 tok
->u
.value
= NULL_TREE
;
1130 tok
->keyword
= RID_MAX
;
1135 if (tok
== lexer
->last_token
)
1141 while (tok
->purged_p
);
1142 lexer
->next_token
= tok
;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1150 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1152 cp_token
*peek
= lexer
->next_token
;
1154 if (peek
== &eof_token
)
1155 peek
= lexer
->last_token
;
1157 gcc_assert (tok
< peek
);
1159 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1161 tok
->purged_p
= true;
1162 tok
->location
= UNKNOWN_LOCATION
;
1163 tok
->u
.value
= NULL_TREE
;
1164 tok
->keyword
= RID_MAX
;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1172 cp_lexer_save_tokens (cp_lexer
* lexer
)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer
))
1176 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1178 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1181 /* Commit to the portion of the token stream most recently saved. */
1184 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer
))
1188 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1190 lexer
->saved_tokens
.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1197 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer
))
1201 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1203 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1211 struct saved_token_sentinel
1216 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1218 len
= lexer
->saved_tokens
.length ();
1219 cp_lexer_save_tokens (lexer
);
1223 cp_lexer_rollback_tokens (lexer
);
1226 ~saved_token_sentinel()
1229 cp_lexer_commit_tokens (lexer
);
1230 gcc_assert (lexer
->saved_tokens
.length () == len
);
1234 /* Print a representation of the TOKEN on the STREAM. */
1237 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names
[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1248 /* C++ parser token types - see "Manifest constants", above. */
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token
->type
)
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token
->u
.value
))
1264 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1271 case CPP_UTF8STRING
:
1272 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1276 print_generic_expr (stream
, token
->u
.value
);
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token
->type
< ARRAY_SIZE(token_names
))
1283 fputs (token_names
[token
->type
], stream
);
1285 fprintf (stream
, "[%d]", token
->type
);
1291 debug (cp_token
&ref
)
1293 cp_lexer_print_token (stderr
, &ref
);
1294 fprintf (stderr
, "\n");
1298 debug (cp_token
*ptr
)
1303 fprintf (stderr
, "<nil>\n");
1307 /* Start emitting debugging information. */
1310 cp_lexer_start_debugging (cp_lexer
* lexer
)
1312 if (!LEXER_DEBUGGING_ENABLED_P
)
1313 fatal_error (input_location
,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer
->debugging_p
= true;
1317 cp_lexer_debug_stream
= stderr
;
1320 /* Stop emitting debugging information. */
1323 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1325 if (!LEXER_DEBUGGING_ENABLED_P
)
1326 fatal_error (input_location
,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer
->debugging_p
= false;
1330 cp_lexer_debug_stream
= NULL
;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache
*
1336 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1338 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1339 cache
->first
= first
;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1348 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1350 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser
->omp_declare_simd
= NULL
;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1362 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1364 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1366 if (fndecl
== error_mark_node
)
1368 parser
->omp_declare_simd
= NULL
;
1371 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1373 cp_ensure_no_omp_declare_simd (parser
);
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1383 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1385 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1387 error_at (parser
->oacc_routine
->loc
,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser
->oacc_routine
= NULL
;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1399 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1401 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator
*make_call_declarator
1412 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1413 static cp_declarator
*make_array_declarator
1414 (cp_declarator
*, tree
);
1415 static cp_declarator
*make_pointer_declarator
1416 (cp_cv_quals
, cp_declarator
*, tree
);
1417 static cp_declarator
*make_reference_declarator
1418 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1419 static cp_declarator
*make_ptrmem_declarator
1420 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1422 /* An erroneous declarator. */
1423 static cp_declarator
*cp_error_declarator
;
1425 /* The obstack on which declarators and related data structures are
1427 static struct obstack declarator_obstack
;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes
)
1434 return obstack_alloc (&declarator_obstack
, bytes
);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator
*
1441 make_declarator (cp_declarator_kind kind
)
1443 cp_declarator
*declarator
;
1445 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1446 declarator
->kind
= kind
;
1447 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1448 declarator
->attributes
= NULL_TREE
;
1449 declarator
->std_attributes
= NULL_TREE
;
1450 declarator
->declarator
= NULL
;
1451 declarator
->parameter_pack_p
= false;
1452 declarator
->id_loc
= UNKNOWN_LOCATION
;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1463 static cp_declarator
*
1464 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1465 special_function_kind sfk
)
1467 cp_declarator
*declarator
;
1469 /* It is valid to write:
1471 class C { void f(); };
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1479 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1480 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1482 gcc_assert (identifier_p (unqualified_name
)
1483 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1486 declarator
= make_declarator (cdk_id
);
1487 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1488 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1489 declarator
->u
.id
.sfk
= sfk
;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1503 cp_declarator
*declarator
;
1505 declarator
= make_declarator (cdk_pointer
);
1506 declarator
->declarator
= target
;
1507 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1508 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1511 declarator
->id_loc
= target
->id_loc
;
1512 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1513 target
->parameter_pack_p
= false;
1516 declarator
->parameter_pack_p
= false;
1518 declarator
->std_attributes
= attributes
;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1528 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1529 bool rvalue_ref
, tree attributes
)
1531 cp_declarator
*declarator
;
1533 declarator
= make_declarator (cdk_reference
);
1534 declarator
->declarator
= target
;
1535 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1536 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1539 declarator
->id_loc
= target
->id_loc
;
1540 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1541 target
->parameter_pack_p
= false;
1544 declarator
->parameter_pack_p
= false;
1546 declarator
->std_attributes
= attributes
;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1557 cp_declarator
*pointee
,
1560 cp_declarator
*declarator
;
1562 declarator
= make_declarator (cdk_ptrmem
);
1563 declarator
->declarator
= pointee
;
1564 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1565 declarator
->u
.pointer
.class_type
= class_type
;
1569 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1570 pointee
->parameter_pack_p
= false;
1573 declarator
->parameter_pack_p
= false;
1575 declarator
->std_attributes
= attributes
;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1586 make_call_declarator (cp_declarator
*target
,
1588 cp_cv_quals cv_qualifiers
,
1589 cp_virt_specifiers virt_specifiers
,
1590 cp_ref_qualifier ref_qualifier
,
1592 tree exception_specification
,
1593 tree late_return_type
,
1594 tree requires_clause
)
1596 cp_declarator
*declarator
;
1598 declarator
= make_declarator (cdk_function
);
1599 declarator
->declarator
= target
;
1600 declarator
->u
.function
.parameters
= parms
;
1601 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1602 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1603 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1604 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1605 declarator
->u
.function
.exception_specification
= exception_specification
;
1606 declarator
->u
.function
.late_return_type
= late_return_type
;
1607 declarator
->u
.function
.requires_clause
= requires_clause
;
1610 declarator
->id_loc
= target
->id_loc
;
1611 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1612 target
->parameter_pack_p
= false;
1615 declarator
->parameter_pack_p
= false;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1624 make_array_declarator (cp_declarator
*element
, tree bounds
)
1626 cp_declarator
*declarator
;
1628 declarator
= make_declarator (cdk_array
);
1629 declarator
->declarator
= element
;
1630 declarator
->u
.array
.bounds
= bounds
;
1633 declarator
->id_loc
= element
->id_loc
;
1634 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1635 element
->parameter_pack_p
= false;
1638 declarator
->parameter_pack_p
= false;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1646 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1648 if (declarator
&& declarator
->parameter_pack_p
)
1649 /* We already saw an ellipsis. */
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1657 while (declarator
&& !found
)
1659 switch ((int)declarator
->kind
)
1671 declarator
= declarator
->declarator
;
1679 cp_parameter_declarator
*no_parameters
;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator
*
1685 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1686 cp_declarator
*declarator
,
1687 tree default_argument
,
1689 bool template_parameter_pack_p
= false)
1691 cp_parameter_declarator
*parameter
;
1693 parameter
= ((cp_parameter_declarator
*)
1694 alloc_declarator (sizeof (cp_parameter_declarator
)));
1695 parameter
->next
= NULL
;
1696 if (decl_specifiers
)
1697 parameter
->decl_specifiers
= *decl_specifiers
;
1699 clear_decl_specs (¶meter
->decl_specifiers
);
1700 parameter
->declarator
= declarator
;
1701 parameter
->default_argument
= default_argument
;
1702 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1703 parameter
->loc
= loc
;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1711 function_declarator_p (const cp_declarator
*declarator
)
1715 if (declarator
->kind
== cdk_function
1716 && declarator
->declarator
->kind
== cdk_id
)
1718 if (declarator
->kind
== cdk_id
1719 || declarator
->kind
== cdk_decomp
1720 || declarator
->kind
== cdk_error
)
1722 declarator
= declarator
->declarator
;
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1788 CP_PARSER_FLAGS_NONE
= 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags
;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT
,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED
,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1828 PREC_LOGICAL_OR_EXPRESSION
,
1829 PREC_LOGICAL_AND_EXPRESSION
,
1830 PREC_INCLUSIVE_OR_EXPRESSION
,
1831 PREC_EXCLUSIVE_OR_EXPRESSION
,
1832 PREC_AND_EXPRESSION
,
1833 PREC_EQUALITY_EXPRESSION
,
1834 PREC_RELATIONAL_EXPRESSION
,
1835 PREC_SHIFT_EXPRESSION
,
1836 PREC_ADDITIVE_EXPRESSION
,
1837 PREC_MULTIPLICATIVE_EXPRESSION
,
1839 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type
;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type
;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec
;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type
;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type
;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec
;
1867 /* Location of the binary operation we are parsing. */
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1879 /* Constructors and destructors. */
1881 static cp_parser_context
*cp_parser_context_new
1882 (cp_parser_context
*);
1884 /* Class variables. */
1886 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1892 static const cp_parser_binary_operations_map_node binops
[] = {
1893 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1894 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1896 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1897 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1898 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1900 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1901 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1903 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1904 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1906 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1907 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1908 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1909 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1911 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1912 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1914 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1916 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1918 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1920 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1922 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1928 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context
*
1936 cp_parser_context_new (cp_parser_context
* next
)
1938 cp_parser_context
*context
;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list
!= NULL
)
1943 /* Pull the first entry from the free list. */
1944 context
= cp_parser_context_free_list
;
1945 cp_parser_context_free_list
= context
->next
;
1946 memset (context
, 0, sizeof (*context
));
1949 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1951 /* No errors have occurred yet in this context. */
1952 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context
->object_type
= next
->object_type
;
1960 /* Thread the stack. */
1961 context
->next
= next
;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1979 push_unparsed_function_queues (cp_parser
*parser
)
1981 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1982 vec_safe_push (parser
->unparsed_queues
, e
);
1986 pop_unparsed_function_queues (cp_parser
*parser
)
1988 release_tree_vector (unparsed_funs_with_definitions
);
1989 parser
->unparsed_queues
->pop ();
1994 /* Constructors and destructors. */
1996 static cp_parser
*cp_parser_new
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser
*, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2019 static tree cp_parser_userdef_string_literal
2021 static cp_expr cp_parser_userdef_numeric_literal
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser
*, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser
*, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser
*, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser
*, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser
*, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser
*, tree
, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2049 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2050 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser
*, tree
, tree
*, tree
*);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2060 static tree cp_parser_new_expression
2062 static vec
<tree
, va_gc
> *cp_parser_new_placement
2064 static tree cp_parser_new_type_id
2065 (cp_parser
*, tree
*);
2066 static cp_declarator
*cp_parser_new_declarator_opt
2068 static cp_declarator
*cp_parser_direct_new_declarator
2070 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2072 static tree cp_parser_delete_expression
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser
*, cp_expr
);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2084 static cp_expr cp_parser_expression
2085 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2090 static cp_expr cp_parser_lambda_expression
2092 static void cp_parser_lambda_introducer
2093 (cp_parser
*, tree
);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser
*, tree
);
2096 static void cp_parser_lambda_body
2097 (cp_parser
*, tree
);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser
*, tree
);
2105 static tree cp_parser_expression_statement
2106 (cp_parser
*, tree
);
2107 static tree cp_parser_compound_statement
2108 (cp_parser
*, tree
, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser
*, tree
);
2111 static tree cp_parser_selection_statement
2112 (cp_parser
*, bool *, vec
<tree
> *);
2113 static tree cp_parser_condition
2115 static tree cp_parser_iteration_statement
2116 (cp_parser
*, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser
*, tree
*decl
);
2119 static tree cp_parser_for
2120 (cp_parser
*, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser
*, tree
, tree
, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree
, tree
*, tree
*);
2129 static tree cp_parser_range_for_member_function
2131 static tree cp_parser_jump_statement
2133 static void cp_parser_declaration_statement
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser
*, bool *, const token_indent_info
&);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2145 static void cp_parser_declaration
2147 static void cp_parser_block_declaration
2148 (cp_parser
*, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser
*, bool, tree
*);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser
*, cp_decl_specifier_seq
*);
2157 static tree cp_parser_type_specifier
2158 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2162 static tree cp_parser_type_name
2163 (cp_parser
*, bool);
2164 static tree cp_parser_type_name
2166 static tree cp_parser_nonclass_name
2167 (cp_parser
* parser
);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser
*, bool, bool);
2170 static tree cp_parser_enum_specifier
2172 static void cp_parser_enumerator_list
2173 (cp_parser
*, tree
);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser
*, tree
);
2176 static tree cp_parser_namespace_name
2178 static void cp_parser_namespace_definition
2180 static void cp_parser_namespace_body
2182 static tree cp_parser_qualified_namespace_specifier
2184 static void cp_parser_namespace_alias_definition
2186 static bool cp_parser_using_declaration
2187 (cp_parser
*, bool);
2188 static void cp_parser_using_directive
2190 static tree cp_parser_alias_declaration
2192 static void cp_parser_asm_definition
2194 static void cp_parser_linkage_specification
2196 static void cp_parser_static_assert
2197 (cp_parser
*, bool);
2198 static tree cp_parser_decltype
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2207 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2208 static cp_declarator
*cp_parser_declarator
2209 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2210 static cp_declarator
*cp_parser_direct_declarator
2211 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2220 static tree cp_parser_tx_qualifier_opt
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2224 static tree cp_parser_declarator_id
2225 (cp_parser
*, bool);
2226 static tree cp_parser_type_id
2228 static tree cp_parser_template_type_arg
2230 static tree
cp_parser_trailing_type_id (cp_parser
*);
2231 static tree cp_parser_type_id_1
2232 (cp_parser
*, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2235 static tree cp_parser_parameter_declaration_clause
2237 static tree cp_parser_parameter_declaration_list
2239 static cp_parameter_declarator
*cp_parser_parameter_declaration
2240 (cp_parser
*, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser
*, bool);
2243 static void cp_parser_function_body
2244 (cp_parser
*, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser
*, bool *, bool *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser
*, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser
*, bool*);
2251 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2252 (cp_parser
*, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser
*, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser
*, tree
);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser
*, tree
);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser
*, tree
);
2265 static tree finish_fully_implicit_template
2266 (cp_parser
*, tree
);
2267 static void abort_fully_implicit_template
2270 /* Classes [gram.class] */
2272 static tree cp_parser_class_name
2273 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2276 static tree cp_parser_class_head
2277 (cp_parser
*, bool *);
2278 static enum tag_types cp_parser_class_key
2280 static void cp_parser_type_parameter_key
2281 (cp_parser
* parser
);
2282 static void cp_parser_member_specification_opt
2284 static void cp_parser_member_declaration
2286 static tree cp_parser_pure_specifier
2288 static tree cp_parser_constant_initializer
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2295 static tree cp_parser_base_specifier
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2302 static tree cp_parser_conversion_type_id
2304 static cp_declarator
*cp_parser_conversion_declarator_opt
2306 static void cp_parser_ctor_initializer_opt
2308 static void cp_parser_mem_initializer_list
2310 static tree cp_parser_mem_initializer
2312 static tree cp_parser_mem_initializer_id
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2319 static cp_expr cp_parser_operator
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser
*, bool);
2326 static tree cp_parser_template_parameter_list
2328 static tree cp_parser_template_parameter
2329 (cp_parser
*, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser
*, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser
*, bool, bool, enum tag_types
, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2336 static tree cp_parser_template_argument_list
2338 static tree cp_parser_template_argument
2340 static void cp_parser_explicit_instantiation
2342 static void cp_parser_explicit_specialization
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2349 static void cp_parser_function_try_block
2351 static void cp_parser_handler_seq
2353 static void cp_parser_handler
2355 static tree cp_parser_exception_declaration
2357 static tree cp_parser_throw_expression
2359 static tree cp_parser_exception_specification_opt
2361 static tree cp_parser_type_id_list
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2368 static tree cp_parser_asm_operand_list
2370 static tree cp_parser_asm_clobber_list
2372 static tree cp_parser_asm_label_list
2374 static bool cp_next_tokens_can_be_attribute_p
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2378 static bool cp_next_tokens_can_be_std_attribute_p
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser
*, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser
*, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser
*, size_t);
2386 static tree cp_parser_attributes_opt
2388 static tree cp_parser_gnu_attributes_opt
2390 static tree cp_parser_gnu_attribute_list
2392 static tree cp_parser_std_attribute
2393 (cp_parser
*, tree
);
2394 static tree cp_parser_std_attribute_spec
2396 static tree cp_parser_std_attribute_spec_seq
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser
*, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser
*, int *);
2402 static void cp_parser_label_declaration
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2409 static tree cp_parser_requires_clause_opt
2411 static tree cp_parser_requires_expression
2413 static tree cp_parser_requirement_parameter_list
2415 static tree cp_parser_requirement_body
2417 static tree cp_parser_requirement_list
2419 static tree cp_parser_requirement
2421 static tree cp_parser_simple_requirement
2423 static tree cp_parser_compound_requirement
2425 static tree cp_parser_type_requirement
2427 static tree cp_parser_nested_requirement
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser
*, cp_token
*);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser
*, enum rid
);
2436 static void cp_parser_function_transaction
2437 (cp_parser
*, enum rid
);
2438 static tree cp_parser_transaction_cancel
2441 enum pragma_context
{
2448 static bool cp_parser_pragma
2449 (cp_parser
*, enum pragma_context
, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2455 static tree cp_parser_objc_message_args
2457 static tree cp_parser_objc_message_expression
2459 static cp_expr cp_parser_objc_encode_expression
2461 static tree cp_parser_objc_defs_expression
2463 static tree cp_parser_objc_protocol_expression
2465 static tree cp_parser_objc_selector_expression
2467 static cp_expr cp_parser_objc_expression
2469 static bool cp_parser_objc_selector_p
2471 static tree cp_parser_objc_selector
2473 static tree cp_parser_objc_protocol_refs_opt
2475 static void cp_parser_objc_declaration
2476 (cp_parser
*, tree
);
2477 static tree cp_parser_objc_statement
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser
*, tree
*);
2481 static void cp_parser_objc_at_property_declaration
2483 static void cp_parser_objc_at_synthesize_declaration
2485 static void cp_parser_objc_at_dynamic_declaration
2487 static tree cp_parser_objc_struct_declaration
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser
*, tree
, location_t
);
2496 static tree cp_parser_maybe_treat_template_as_class
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser
*, cp_declarator
*, location_t
);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2502 static cp_expr cp_parser_simple_cast_expression
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser
*, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser
*, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser
*, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser
*, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec
<deferred_access_check
, va_gc
> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser
*, tree
);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2522 static tree cp_parser_save_nsdmi
2524 static tree cp_parser_enclosed_template_argument_list
2526 static void cp_parser_save_default_args
2527 (cp_parser
*, tree
);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser
*, tree
);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser
*, tree
, tree
, tree
);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser
*, tree
);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser
*, tree
);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser
*, enum rid
);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser
*, enum rid
);
2540 static bool cp_parser_declares_only_class_p
2542 static void cp_parser_set_storage_class
2543 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq
*decl_specs
,
2548 cp_decl_spec ds
, cp_token
*);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq
*);
2551 static void cp_parser_required_error
2552 (cp_parser
*, required_token
, bool, location_t
);
2553 static cp_token
*cp_parser_require
2554 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2555 static cp_token
*cp_parser_require_keyword
2556 (cp_parser
*, enum rid
, required_token
);
2557 static bool cp_parser_token_starts_function_definition_p
2559 static bool cp_parser_next_token_starts_class_definition_p
2561 static bool cp_parser_next_token_ends_template_argument_p
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser
*, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2569 static void cp_parser_check_class_key
2570 (enum tag_types
, tree type
);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type
, location_t location
);
2573 static bool cp_parser_optional_template_keyword
2575 static void cp_parser_pre_parsed_nested_name_specifier
2577 static bool cp_parser_cache_group
2578 (cp_parser
*, enum cpp_ttype
, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser
*parser
, bool nsdmi
);
2581 static void cp_parser_parse_tentatively
2583 static void cp_parser_commit_to_tentative_parse
2585 static void cp_parser_commit_to_topmost_tentative_parse
2587 static void cp_parser_abort_tentative_parse
2589 static bool cp_parser_parse_definitely
2591 static inline bool cp_parser_parsing_tentatively
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2595 static void cp_parser_error
2596 (cp_parser
*, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2599 static bool cp_parser_simulate_error
2601 static bool cp_parser_check_type_definition
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator
*, tree
, location_t type_location
);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser
*, non_integral_constant
);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser
*, tree
, location_t
);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser
*, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2619 static void cp_parser_skip_to_end_of_block_or_statement
2621 static bool cp_parser_skip_to_closing_brace
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser
*, cp_token
*);
2627 static bool cp_parser_error_occurred
2629 static bool cp_parser_allow_gnu_extensions_p
2631 static bool cp_parser_is_pure_string_literal
2633 static bool cp_parser_is_string_literal
2635 static bool cp_parser_is_keyword
2636 (cp_token
*, enum rid
);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser
*, tree
, location_t location
);
2639 static cp_declarator
* cp_parser_make_indirect_declarator
2640 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2641 static bool cp_parser_compound_literal_p
2643 static bool cp_parser_array_designator_p
2645 static bool cp_parser_init_statement_p
2647 static bool cp_parser_skip_to_closing_square_bracket
2650 /* Concept-related syntactic transformations */
2652 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2653 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand
;
2662 ++c_inhibit_evaluation_warnings
;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings
;
2668 --cp_unevaluated_operand
;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2677 cp_parser_parsing_tentatively (cp_parser
* parser
)
2679 return parser
->context
->next
!= NULL
;
2682 /* Returns nonzero if TOKEN is a string literal. */
2685 cp_parser_is_pure_string_literal (cp_token
* token
)
2687 return (token
->type
== CPP_STRING
||
2688 token
->type
== CPP_STRING16
||
2689 token
->type
== CPP_STRING32
||
2690 token
->type
== CPP_WSTRING
||
2691 token
->type
== CPP_UTF8STRING
);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2698 cp_parser_is_string_literal (cp_token
* token
)
2700 return (cp_parser_is_pure_string_literal (token
) ||
2701 token
->type
== CPP_STRING_USERDEF
||
2702 token
->type
== CPP_STRING16_USERDEF
||
2703 token
->type
== CPP_STRING32_USERDEF
||
2704 token
->type
== CPP_WSTRING_USERDEF
||
2705 token
->type
== CPP_UTF8STRING_USERDEF
);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2711 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2713 return token
->keyword
== keyword
;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token
*token
)
2722 if (token
->type
!= CPP_PRAGMA
)
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2740 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2741 location_t
*out_loc
)
2743 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2744 if (token2
->type
!= tok1_kind
)
2746 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2747 if (token3
->type
!= tok1_kind
)
2749 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2750 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2753 /* It must be at the start of the line. */
2754 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2755 if (LOCATION_COLUMN (start_loc
) != 1)
2758 /* We have a conflict marker. Construct a location of the form:
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc
= get_finish (token4
->location
);
2763 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2772 get_matching_symbol (required_token token_desc
)
2779 case RT_CLOSE_BRACE
:
2781 case RT_CLOSE_PAREN
:
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc
)
2795 return CPP_SEMICOLON
;
2797 return CPP_OPEN_PAREN
;
2798 case RT_CLOSE_BRACE
:
2799 return CPP_CLOSE_BRACE
;
2801 return CPP_OPEN_BRACE
;
2802 case RT_CLOSE_SQUARE
:
2803 return CPP_CLOSE_SQUARE
;
2804 case RT_OPEN_SQUARE
:
2805 return CPP_OPEN_SQUARE
;
2810 case RT_CLOSE_PAREN
:
2811 return CPP_CLOSE_PAREN
;
2814 /* Use CPP_EOF as a "no completions possible" code. */
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2839 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2840 required_token missing_token_desc
,
2841 location_t matching_location
)
2843 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token
);
2848 if (token
->type
== CPP_PRAGMA
)
2850 error_at (token
->location
,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser
, token
);
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token
->type
== CPP_LSHIFT
2858 || token
->type
== CPP_RSHIFT
2859 || token
->type
== CPP_EQ_EQ
)
2862 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2864 error_at (loc
, "version control conflict marker in file");
2869 gcc_rich_location
richloc (input_location
);
2871 bool added_matching_location
= false;
2873 if (missing_token_desc
!= RT_NONE
)
2875 /* Potentially supply a fix-it hint, suggesting to add the
2876 missing token immediately after the *previous* token.
2877 This may move the primary location within richloc. */
2878 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2879 location_t prev_token_loc
2880 = cp_lexer_previous_token (parser
->lexer
)->location
;
2881 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2883 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884 Attempt to consolidate diagnostics by printing it as a
2885 secondary range within the main diagnostic. */
2886 if (matching_location
!= UNKNOWN_LOCATION
)
2887 added_matching_location
2888 = richloc
.add_location_if_nearby (matching_location
);
2891 /* Actually emit the error. */
2892 c_parse_error (gmsgid
,
2893 /* Because c_parser_error does not understand
2894 CPP_KEYWORD, keywords are treated like
2896 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2897 token
->u
.value
, token
->flags
, &richloc
);
2899 if (missing_token_desc
!= RT_NONE
)
2901 /* If we weren't able to consolidate matching_location, then
2902 print it as a secondary diagnostic. */
2903 if (matching_location
!= UNKNOWN_LOCATION
2904 && !added_matching_location
)
2905 inform (matching_location
, "to match this %qs",
2906 get_matching_symbol (missing_token_desc
));
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911 FILE:LINE: MESSAGE before TOKEN
2912 where TOKEN is the next token in the input stream. MESSAGE
2913 (specified by the caller) is usually of the form "expected
2917 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2919 if (!cp_parser_simulate_error (parser
))
2920 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2923 /* Issue an error about name-lookup failing. NAME is the
2924 IDENTIFIER_NODE DECL is the result of
2925 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2926 the thing that we hoped to find. */
2929 cp_parser_name_lookup_error (cp_parser
* parser
,
2932 name_lookup_error desired
,
2933 location_t location
)
2935 /* If name lookup completely failed, tell the user that NAME was not
2937 if (decl
== error_mark_node
)
2939 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2940 error_at (location
, "%<%E::%E%> has not been declared",
2941 parser
->scope
, name
);
2942 else if (parser
->scope
== global_namespace
)
2943 error_at (location
, "%<::%E%> has not been declared", name
);
2944 else if (parser
->object_scope
2945 && !CLASS_TYPE_P (parser
->object_scope
))
2946 error_at (location
, "request for member %qE in non-class type %qT",
2947 name
, parser
->object_scope
);
2948 else if (parser
->object_scope
)
2949 error_at (location
, "%<%T::%E%> has not been declared",
2950 parser
->object_scope
, name
);
2952 error_at (location
, "%qE has not been declared", name
);
2954 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2959 error_at (location
, "%<%E::%E%> is not a type",
2960 parser
->scope
, name
);
2963 error_at (location
, "%<%E::%E%> is not a class or namespace",
2964 parser
->scope
, name
);
2968 "%<%E::%E%> is not a class, namespace, or enumeration",
2969 parser
->scope
, name
);
2976 else if (parser
->scope
== global_namespace
)
2981 error_at (location
, "%<::%E%> is not a type", name
);
2984 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2988 "%<::%E%> is not a class, namespace, or enumeration",
3000 error_at (location
, "%qE is not a type", name
);
3003 error_at (location
, "%qE is not a class or namespace", name
);
3007 "%qE is not a class, namespace, or enumeration", name
);
3015 /* If we are parsing tentatively, remember that an error has occurred
3016 during this tentative parse. Returns true if the error was
3017 simulated; false if a message should be issued by the caller. */
3020 cp_parser_simulate_error (cp_parser
* parser
)
3022 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3024 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3030 /* This function is called when a type is defined. If type
3031 definitions are forbidden at this point, an error message is
3035 cp_parser_check_type_definition (cp_parser
* parser
)
3037 /* If types are forbidden here, issue a message. */
3038 if (parser
->type_definition_forbidden_message
)
3040 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041 in the message need to be interpreted. */
3042 error (parser
->type_definition_forbidden_message
);
3048 /* This function is called when the DECLARATOR is processed. The TYPE
3049 was a type defined in the decl-specifiers. If it is invalid to
3050 define a type in the decl-specifiers for DECLARATOR, an error is
3051 issued. TYPE_LOCATION is the location of TYPE and is used
3052 for error reporting. */
3055 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3056 tree type
, location_t type_location
)
3058 /* [dcl.fct] forbids type definitions in return types.
3059 Unfortunately, it's not easy to know whether or not we are
3060 processing a return type until after the fact. */
3062 && (declarator
->kind
== cdk_pointer
3063 || declarator
->kind
== cdk_reference
3064 || declarator
->kind
== cdk_ptrmem
))
3065 declarator
= declarator
->declarator
;
3067 && declarator
->kind
== cdk_function
)
3069 error_at (type_location
,
3070 "new types may not be defined in a return type");
3071 inform (type_location
,
3072 "(perhaps a semicolon is missing after the definition of %qT)",
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078 "<" in any valid C++ program. If the next token is indeed "<",
3079 issue a message warning the user about what appears to be an
3080 invalid attempt to form a template-id. LOCATION is the location
3081 of the type-specifier (TYPE) */
3084 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3086 enum tag_types tag_type
,
3087 location_t location
)
3089 cp_token_position start
= 0;
3091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3093 if (TREE_CODE (type
) == TYPE_DECL
)
3094 type
= TREE_TYPE (type
);
3095 if (TYPE_P (type
) && !template_placeholder_p (type
))
3096 error_at (location
, "%qT is not a template", type
);
3097 else if (identifier_p (type
))
3099 if (tag_type
!= none_type
)
3100 error_at (location
, "%qE is not a class template", type
);
3102 error_at (location
, "%qE is not a template", type
);
3105 error_at (location
, "invalid template-id");
3106 /* Remember the location of the invalid "<". */
3107 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3108 start
= cp_lexer_token_position (parser
->lexer
, true);
3109 /* Consume the "<". */
3110 cp_lexer_consume_token (parser
->lexer
);
3111 /* Parse the template arguments. */
3112 cp_parser_enclosed_template_argument_list (parser
);
3113 /* Permanently remove the invalid template arguments so that
3114 this error message is not issued again. */
3116 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3120 /* If parsing an integral constant-expression, issue an error message
3121 about the fact that THING appeared and return true. Otherwise,
3122 return false. In either case, set
3123 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3126 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3127 non_integral_constant thing
)
3129 parser
->non_integral_constant_expression_p
= true;
3130 if (parser
->integral_constant_expression_p
)
3132 if (!parser
->allow_non_integral_constant_expression_p
)
3134 const char *msg
= NULL
;
3138 pedwarn (input_location
, OPT_Wpedantic
,
3139 "ISO C++ forbids using a floating-point literal "
3140 "in a constant-expression");
3143 error ("a cast to a type other than an integral or "
3144 "enumeration type cannot appear in a "
3145 "constant-expression");
3148 error ("%<typeid%> operator "
3149 "cannot appear in a constant-expression");
3152 error ("non-constant compound literals "
3153 "cannot appear in a constant-expression");
3156 error ("a function call "
3157 "cannot appear in a constant-expression");
3160 error ("an increment "
3161 "cannot appear in a constant-expression");
3164 error ("an decrement "
3165 "cannot appear in a constant-expression");
3168 error ("an array reference "
3169 "cannot appear in a constant-expression");
3171 case NIC_ADDR_LABEL
:
3172 error ("the address of a label "
3173 "cannot appear in a constant-expression");
3175 case NIC_OVERLOADED
:
3176 error ("calls to overloaded operators "
3177 "cannot appear in a constant-expression");
3179 case NIC_ASSIGNMENT
:
3180 error ("an assignment cannot appear in a constant-expression");
3183 error ("a comma operator "
3184 "cannot appear in a constant-expression");
3186 case NIC_CONSTRUCTOR
:
3187 error ("a call to a constructor "
3188 "cannot appear in a constant-expression");
3190 case NIC_TRANSACTION
:
3191 error ("a transaction expression "
3192 "cannot appear in a constant-expression");
3198 msg
= "__FUNCTION__";
3200 case NIC_PRETTY_FUNC
:
3201 msg
= "__PRETTY_FUNCTION__";
3221 case NIC_PREINCREMENT
:
3224 case NIC_PREDECREMENT
:
3237 error ("%qs cannot appear in a constant-expression", msg
);
3244 /* Emit a diagnostic for an invalid type name. This function commits
3245 to the current active tentative parse, if any. (Otherwise, the
3246 problematic construct might be encountered again later, resulting
3247 in duplicate error messages.) LOCATION is the location of ID. */
3250 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3251 location_t location
)
3253 tree decl
, ambiguous_decls
;
3254 cp_parser_commit_to_tentative_parse (parser
);
3255 /* Try to lookup the identifier. */
3256 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3257 /*is_template=*/false,
3258 /*is_namespace=*/false,
3259 /*check_dependency=*/true,
3260 &ambiguous_decls
, location
);
3261 if (ambiguous_decls
)
3262 /* If the lookup was ambiguous, an error will already have
3265 /* If the lookup found a template-name, it means that the user forgot
3266 to specify an argument list. Emit a useful error message. */
3267 if (DECL_TYPE_TEMPLATE_P (decl
))
3270 "invalid use of template-name %qE without an argument list",
3272 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3273 inform (location
, "class template argument deduction is only available "
3274 "with -std=c++17 or -std=gnu++17");
3275 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3277 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3278 error_at (location
, "invalid use of destructor %qD as a type", id
);
3279 else if (TREE_CODE (decl
) == TYPE_DECL
)
3280 /* Something like 'unsigned A a;' */
3281 error_at (location
, "invalid combination of multiple type-specifiers");
3282 else if (!parser
->scope
)
3284 /* Issue an error message. */
3286 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3287 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3290 gcc_rich_location
richloc (location
);
3291 richloc
.add_fixit_replace (hint
.suggestion ());
3293 "%qE does not name a type; did you mean %qs?",
3294 id
, hint
.suggestion ());
3297 error_at (location
, "%qE does not name a type", id
);
3298 /* If we're in a template class, it's possible that the user was
3299 referring to a type from a base class. For example:
3301 template <typename T> struct A { typedef T X; };
3302 template <typename T> struct B : public A<T> { X x; };
3304 The user should have said "typename A<T>::X". */
3305 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3306 inform (location
, "C++11 %<constexpr%> only available with "
3307 "-std=c++11 or -std=gnu++11");
3308 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3309 inform (location
, "C++11 %<noexcept%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (cxx_dialect
< cxx11
3312 && TREE_CODE (id
) == IDENTIFIER_NODE
3313 && id_equal (id
, "thread_local"))
3314 inform (location
, "C++11 %<thread_local%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3317 inform (location
, "%<concept%> only available with -fconcepts");
3318 else if (processing_template_decl
&& current_class_type
3319 && TYPE_BINFO (current_class_type
))
3323 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3327 tree base_type
= BINFO_TYPE (b
);
3328 if (CLASS_TYPE_P (base_type
)
3329 && dependent_type_p (base_type
))
3332 /* Go from a particular instantiation of the
3333 template (which will have an empty TYPE_FIELDs),
3334 to the main version. */
3335 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3336 for (field
= TYPE_FIELDS (base_type
);
3338 field
= DECL_CHAIN (field
))
3339 if (TREE_CODE (field
) == TYPE_DECL
3340 && DECL_NAME (field
) == id
)
3343 "(perhaps %<typename %T::%E%> was intended)",
3344 BINFO_TYPE (b
), id
);
3353 /* Here we diagnose qualified-ids where the scope is actually correct,
3354 but the identifier does not resolve to a valid type name. */
3355 else if (parser
->scope
!= error_mark_node
)
3357 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3360 error_at (location_of (id
),
3361 "%qE in namespace %qE does not name a template type",
3363 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3364 error_at (location_of (id
),
3365 "%qE in namespace %qE does not name a template type",
3366 TREE_OPERAND (id
, 0), parser
->scope
);
3368 error_at (location_of (id
),
3369 "%qE in namespace %qE does not name a type",
3372 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3373 else if (decl
== error_mark_node
)
3374 suggest_alternative_in_explicit_scope (location
, id
,
3377 else if (CLASS_TYPE_P (parser
->scope
)
3378 && constructor_name_p (id
, parser
->scope
))
3381 error_at (location
, "%<%T::%E%> names the constructor, not"
3382 " the type", parser
->scope
, id
);
3383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3384 error_at (location
, "and %qT has no template constructors",
3387 else if (TYPE_P (parser
->scope
)
3388 && dependent_scope_p (parser
->scope
))
3390 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3392 "need %<typename%> before %<%T::%D::%E%> because "
3393 "%<%T::%D%> is a dependent scope",
3394 TYPE_CONTEXT (parser
->scope
),
3395 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3397 TYPE_CONTEXT (parser
->scope
),
3398 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3400 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3401 "%qT is a dependent scope",
3402 parser
->scope
, id
, parser
->scope
);
3404 else if (TYPE_P (parser
->scope
))
3406 if (!COMPLETE_TYPE_P (parser
->scope
))
3407 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3409 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3410 error_at (location_of (id
),
3411 "%qE in %q#T does not name a template type",
3413 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3414 error_at (location_of (id
),
3415 "%qE in %q#T does not name a template type",
3416 TREE_OPERAND (id
, 0), parser
->scope
);
3418 error_at (location_of (id
),
3419 "%qE in %q#T does not name a type",
3422 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3429 /* Check for a common situation where a type-name should be present,
3430 but is not, and issue a sensible error message. Returns true if an
3431 invalid type-name was detected.
3433 The situation handled by this function are variable declarations of the
3434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435 Usually, `ID' should name a type, but if we got here it means that it
3436 does not. We try to emit the best possible error message depending on
3437 how exactly the id-expression looks like. */
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3443 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3445 /* Avoid duplicate error about ambiguous lookup. */
3446 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3448 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3449 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3453 cp_parser_parse_tentatively (parser
);
3454 id
= cp_parser_id_expression (parser
,
3455 /*template_keyword_p=*/false,
3456 /*check_dependency_p=*/true,
3457 /*template_p=*/NULL
,
3458 /*declarator_p=*/false,
3459 /*optional_p=*/false);
3460 /* If the next token is a (, this is a function with no explicit return
3461 type, i.e. constructor, destructor or conversion op. */
3462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3463 || TREE_CODE (id
) == TYPE_DECL
)
3465 cp_parser_abort_tentative_parse (parser
);
3468 if (!cp_parser_parse_definitely (parser
))
3471 /* Emit a diagnostic for the invalid type. */
3472 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3474 /* If we aren't in the middle of a declarator (i.e. in a
3475 parameter-declaration-clause), skip to the end of the declaration;
3476 there's no point in trying to process it. */
3477 if (!parser
->in_declarator_p
)
3478 cp_parser_skip_to_end_of_block_or_statement (parser
);
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3484 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485 found an unnested token of that type. */
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3493 unsigned paren_depth
= 0;
3494 unsigned brace_depth
= 0;
3495 unsigned square_depth
= 0;
3496 unsigned condop_depth
= 0;
3498 if (recovering
&& or_ttype
== CPP_EOF
3499 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3504 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3506 /* Have we found what we're looking for before the closing paren? */
3507 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3508 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3511 switch (token
->type
)
3514 case CPP_PRAGMA_EOL
:
3515 /* If we've run out of tokens, then there is no closing `)'. */
3518 /* This is good for lambda expression capture-lists. */
3519 case CPP_OPEN_SQUARE
:
3522 case CPP_CLOSE_SQUARE
:
3523 if (!square_depth
--)
3528 /* This matches the processing in skip_to_end_of_statement. */
3533 case CPP_OPEN_BRACE
:
3536 case CPP_CLOSE_BRACE
:
3541 case CPP_OPEN_PAREN
:
3546 case CPP_CLOSE_PAREN
:
3547 if (!brace_depth
&& !paren_depth
--)
3550 cp_lexer_consume_token (parser
->lexer
);
3556 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3561 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3569 /* Consume the token. */
3570 cp_lexer_consume_token (parser
->lexer
);
3574 /* Consume tokens up to, and including, the next non-nested closing `)'.
3575 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3576 are doing error recovery. Returns -1 if OR_COMMA is true and we
3577 found an unnested token of that type. */
3580 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3585 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3586 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3587 ttype
, consume_paren
);
3590 /* Consume tokens until we reach the end of the current statement.
3591 Normally, that will be just before consuming a `;'. However, if a
3592 non-nested `}' comes first, then we stop before consuming that. */
3595 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3597 unsigned nesting_depth
= 0;
3599 /* Unwind generic function template scope if necessary. */
3600 if (parser
->fully_implicit_function_template_p
)
3601 abort_fully_implicit_template (parser
);
3605 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3607 switch (token
->type
)
3610 case CPP_PRAGMA_EOL
:
3611 /* If we've run out of tokens, stop. */
3615 /* If the next token is a `;', we have reached the end of the
3621 case CPP_CLOSE_BRACE
:
3622 /* If this is a non-nested '}', stop before consuming it.
3623 That way, when confronted with something like:
3627 we stop before consuming the closing '}', even though we
3628 have not yet reached a `;'. */
3629 if (nesting_depth
== 0)
3632 /* If it is the closing '}' for a block that we have
3633 scanned, stop -- but only after consuming the token.
3639 we will stop after the body of the erroneously declared
3640 function, but before consuming the following `typedef'
3642 if (--nesting_depth
== 0)
3644 cp_lexer_consume_token (parser
->lexer
);
3649 case CPP_OPEN_BRACE
:
3657 /* Consume the token. */
3658 cp_lexer_consume_token (parser
->lexer
);
3662 /* This function is called at the end of a statement or declaration.
3663 If the next token is a semicolon, it is consumed; otherwise, error
3664 recovery is attempted. */
3667 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3669 /* Look for the trailing `;'. */
3670 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3672 /* If there is additional (erroneous) input, skip to the end of
3674 cp_parser_skip_to_end_of_statement (parser
);
3675 /* If the next token is now a `;', consume it. */
3676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3677 cp_lexer_consume_token (parser
->lexer
);
3681 /* Skip tokens until we have consumed an entire block, or until we
3682 have consumed a non-nested `;'. */
3685 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3687 int nesting_depth
= 0;
3689 /* Unwind generic function template scope if necessary. */
3690 if (parser
->fully_implicit_function_template_p
)
3691 abort_fully_implicit_template (parser
);
3693 while (nesting_depth
>= 0)
3695 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3697 switch (token
->type
)
3700 case CPP_PRAGMA_EOL
:
3701 /* If we've run out of tokens, stop. */
3705 /* Stop if this is an unnested ';'. */
3710 case CPP_CLOSE_BRACE
:
3711 /* Stop if this is an unnested '}', or closes the outermost
3714 if (nesting_depth
< 0)
3720 case CPP_OPEN_BRACE
:
3729 /* Consume the token. */
3730 cp_lexer_consume_token (parser
->lexer
);
3734 /* Skip tokens until a non-nested closing curly brace is the next
3735 token, or there are no more tokens. Return true in the first case,
3739 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3741 unsigned nesting_depth
= 0;
3745 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3747 switch (token
->type
)
3750 case CPP_PRAGMA_EOL
:
3751 /* If we've run out of tokens, stop. */
3754 case CPP_CLOSE_BRACE
:
3755 /* If the next token is a non-nested `}', then we have reached
3756 the end of the current block. */
3757 if (nesting_depth
-- == 0)
3761 case CPP_OPEN_BRACE
:
3762 /* If it the next token is a `{', then we are entering a new
3763 block. Consume the entire block. */
3771 /* Consume the token. */
3772 cp_lexer_consume_token (parser
->lexer
);
3776 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3777 parameter is the PRAGMA token, allowing us to purge the entire pragma
3781 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3785 parser
->lexer
->in_pragma
= false;
3788 token
= cp_lexer_consume_token (parser
->lexer
);
3789 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3791 /* Ensure that the pragma is not parsed again. */
3792 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3795 /* Require pragma end of line, resyncing with it as necessary. The
3796 arguments are as for cp_parser_skip_to_pragma_eol. */
3799 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3801 parser
->lexer
->in_pragma
= false;
3802 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3803 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3806 /* This is a simple wrapper around make_typename_type. When the id is
3807 an unresolved identifier node, we can provide a superior diagnostic
3808 using cp_parser_diagnose_invalid_type_name. */
3811 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3812 location_t id_location
)
3815 if (identifier_p (id
))
3817 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3818 /*complain=*/tf_none
);
3819 if (result
== error_mark_node
)
3820 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3823 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3826 /* This is a wrapper around the
3827 make_{pointer,ptrmem,reference}_declarator functions that decides
3828 which one to call based on the CODE and CLASS_TYPE arguments. The
3829 CODE argument should be one of the values returned by
3830 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3831 appertain to the pointer or reference. */
3833 static cp_declarator
*
3834 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3835 cp_cv_quals cv_qualifiers
,
3836 cp_declarator
*target
,
3839 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3840 return cp_error_declarator
;
3842 if (code
== INDIRECT_REF
)
3843 if (class_type
== NULL_TREE
)
3844 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3846 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3847 target
, attributes
);
3848 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3849 return make_reference_declarator (cv_qualifiers
, target
,
3851 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3852 return make_reference_declarator (cv_qualifiers
, target
,
3857 /* Create a new C++ parser. */
3860 cp_parser_new (void)
3866 /* cp_lexer_new_main is called before doing GC allocation because
3867 cp_lexer_new_main might load a PCH file. */
3868 lexer
= cp_lexer_new_main ();
3870 /* Initialize the binops_by_token so that we can get the tree
3871 directly from the token. */
3872 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3873 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3875 parser
= ggc_cleared_alloc
<cp_parser
> ();
3876 parser
->lexer
= lexer
;
3877 parser
->context
= cp_parser_context_new (NULL
);
3879 /* For now, we always accept GNU extensions. */
3880 parser
->allow_gnu_extensions_p
= 1;
3882 /* The `>' token is a greater-than operator, not the end of a
3884 parser
->greater_than_is_operator_p
= true;
3886 parser
->default_arg_ok_p
= true;
3888 /* We are not parsing a constant-expression. */
3889 parser
->integral_constant_expression_p
= false;
3890 parser
->allow_non_integral_constant_expression_p
= false;
3891 parser
->non_integral_constant_expression_p
= false;
3893 /* Local variable names are not forbidden. */
3894 parser
->local_variables_forbidden_p
= false;
3896 /* We are not processing an `extern "C"' declaration. */
3897 parser
->in_unbraced_linkage_specification_p
= false;
3899 /* We are not processing a declarator. */
3900 parser
->in_declarator_p
= false;
3902 /* We are not processing a template-argument-list. */
3903 parser
->in_template_argument_list_p
= false;
3905 /* We are not in an iteration statement. */
3906 parser
->in_statement
= 0;
3908 /* We are not in a switch statement. */
3909 parser
->in_switch_statement_p
= false;
3911 /* We are not parsing a type-id inside an expression. */
3912 parser
->in_type_id_in_expr_p
= false;
3914 /* Declarations aren't implicitly extern "C". */
3915 parser
->implicit_extern_c
= false;
3917 /* String literals should be translated to the execution character set. */
3918 parser
->translate_strings_p
= true;
3920 /* We are not parsing a function body. */
3921 parser
->in_function_body
= false;
3923 /* We can correct until told otherwise. */
3924 parser
->colon_corrects_to_scope_p
= true;
3926 /* The unparsed function queue is empty. */
3927 push_unparsed_function_queues (parser
);
3929 /* There are no classes being defined. */
3930 parser
->num_classes_being_defined
= 0;
3932 /* No template parameters apply. */
3933 parser
->num_template_parameter_lists
= 0;
3935 /* Special parsing data structures. */
3936 parser
->omp_declare_simd
= NULL
;
3937 parser
->oacc_routine
= NULL
;
3939 /* Not declaring an implicit function template. */
3940 parser
->auto_is_implicit_function_template_parm_p
= false;
3941 parser
->fully_implicit_function_template_p
= false;
3942 parser
->implicit_template_parms
= 0;
3943 parser
->implicit_template_scope
= 0;
3945 /* Allow constrained-type-specifiers. */
3946 parser
->prevent_constrained_type_specifiers
= 0;
3948 /* We haven't yet seen an 'extern "C"'. */
3949 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3954 /* Create a cp_lexer structure which will emit the tokens in CACHE
3955 and push it onto the parser's lexer stack. This is used for delayed
3956 parsing of in-class method bodies and default arguments, and should
3957 not be confused with tentative parsing. */
3959 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3961 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3962 lexer
->next
= parser
->lexer
;
3963 parser
->lexer
= lexer
;
3965 /* Move the current source position to that of the first token in the
3967 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3970 /* Pop the top lexer off the parser stack. This is never used for the
3971 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3973 cp_parser_pop_lexer (cp_parser
*parser
)
3975 cp_lexer
*lexer
= parser
->lexer
;
3976 parser
->lexer
= lexer
->next
;
3977 cp_lexer_destroy (lexer
);
3979 /* Put the current source position back where it was before this
3980 lexer was pushed. */
3981 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3984 /* Lexical conventions [gram.lex] */
3986 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3990 cp_parser_identifier (cp_parser
* parser
)
3994 /* Look for the identifier. */
3995 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3996 /* Return the value. */
3998 return cp_expr (token
->u
.value
, token
->location
);
4000 return error_mark_node
;
4003 /* Parse a sequence of adjacent string constants. Returns a
4004 TREE_STRING representing the combined, nul-terminated string
4005 constant. If TRANSLATE is true, translate the string to the
4006 execution character set. If WIDE_OK is true, a wide string is
4009 C++98 [lex.string] says that if a narrow string literal token is
4010 adjacent to a wide string literal token, the behavior is undefined.
4011 However, C99 6.4.5p4 says that this results in a wide string literal.
4012 We follow C99 here, for consistency with the C front end.
4014 This code is largely lifted from lex_string() in c-lex.c.
4016 FUTURE: ObjC++ will need to handle @-strings here. */
4018 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4019 bool lookup_udlit
= true)
4023 struct obstack str_ob
;
4024 cpp_string str
, istr
, *strs
;
4026 enum cpp_ttype type
, curr_type
;
4027 int have_suffix_p
= 0;
4029 tree suffix_id
= NULL_TREE
;
4030 bool curr_tok_is_userdef_p
= false;
4032 tok
= cp_lexer_peek_token (parser
->lexer
);
4033 if (!cp_parser_is_string_literal (tok
))
4035 cp_parser_error (parser
, "expected string-literal");
4036 return error_mark_node
;
4039 location_t loc
= tok
->location
;
4041 if (cpp_userdef_string_p (tok
->type
))
4043 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4044 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4045 curr_tok_is_userdef_p
= true;
4049 string_tree
= tok
->u
.value
;
4050 curr_type
= tok
->type
;
4054 /* Try to avoid the overhead of creating and destroying an obstack
4055 for the common case of just one string. */
4056 if (!cp_parser_is_string_literal
4057 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4059 cp_lexer_consume_token (parser
->lexer
);
4061 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4062 str
.len
= TREE_STRING_LENGTH (string_tree
);
4065 if (curr_tok_is_userdef_p
)
4067 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4069 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4072 curr_type
= tok
->type
;
4078 location_t last_tok_loc
= tok
->location
;
4079 gcc_obstack_init (&str_ob
);
4084 cp_lexer_consume_token (parser
->lexer
);
4086 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4087 str
.len
= TREE_STRING_LENGTH (string_tree
);
4089 if (curr_tok_is_userdef_p
)
4091 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4092 if (have_suffix_p
== 0)
4094 suffix_id
= curr_suffix_id
;
4097 else if (have_suffix_p
== 1
4098 && curr_suffix_id
!= suffix_id
)
4100 error ("inconsistent user-defined literal suffixes"
4101 " %qD and %qD in string literal",
4102 suffix_id
, curr_suffix_id
);
4105 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4108 curr_type
= tok
->type
;
4110 if (type
!= curr_type
)
4112 if (type
== CPP_STRING
)
4114 else if (curr_type
!= CPP_STRING
)
4116 rich_location
rich_loc (line_table
, tok
->location
);
4117 rich_loc
.add_range (last_tok_loc
, false);
4118 error_at (&rich_loc
,
4119 "unsupported non-standard concatenation "
4120 "of string literals");
4124 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4126 last_tok_loc
= tok
->location
;
4128 tok
= cp_lexer_peek_token (parser
->lexer
);
4129 if (cpp_userdef_string_p (tok
->type
))
4131 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4132 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4133 curr_tok_is_userdef_p
= true;
4137 string_tree
= tok
->u
.value
;
4138 curr_type
= tok
->type
;
4139 curr_tok_is_userdef_p
= false;
4142 while (cp_parser_is_string_literal (tok
));
4144 /* A string literal built by concatenation has its caret=start at
4145 the start of the initial string, and its finish at the finish of
4146 the final string literal. */
4147 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4149 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4152 if (type
!= CPP_STRING
&& !wide_ok
)
4154 cp_parser_error (parser
, "a wide string is invalid in this context");
4158 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4159 (parse_in
, strs
, count
, &istr
, type
))
4161 value
= build_string (istr
.len
, (const char *)istr
.text
);
4162 free (CONST_CAST (unsigned char *, istr
.text
));
4168 case CPP_UTF8STRING
:
4169 TREE_TYPE (value
) = char_array_type_node
;
4172 TREE_TYPE (value
) = char16_array_type_node
;
4175 TREE_TYPE (value
) = char32_array_type_node
;
4178 TREE_TYPE (value
) = wchar_array_type_node
;
4182 value
= fix_string_type (value
);
4186 tree literal
= build_userdef_literal (suffix_id
, value
,
4187 OT_NONE
, NULL_TREE
);
4189 value
= cp_parser_userdef_string_literal (literal
);
4195 /* cpp_interpret_string has issued an error. */
4196 value
= error_mark_node
;
4199 obstack_free (&str_ob
, 0);
4201 return cp_expr (value
, loc
);
4204 /* Look up a literal operator with the name and the exact arguments. */
4207 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4210 decl
= lookup_name (name
);
4211 if (!decl
|| !is_overloaded_fn (decl
))
4212 return error_mark_node
;
4214 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4219 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4220 if (parmtypes
!= NULL_TREE
)
4222 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4223 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4225 tree tparm
= TREE_VALUE (parmtypes
);
4226 tree targ
= TREE_TYPE ((*args
)[ix
]);
4227 bool ptr
= TYPE_PTR_P (tparm
);
4228 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4229 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4231 || !same_type_p (TREE_TYPE (tparm
),
4236 && ix
== vec_safe_length (args
)
4237 /* May be this should be sufficient_parms_p instead,
4238 depending on how exactly should user-defined literals
4239 work in presence of default arguments on the literal
4240 operator parameters. */
4241 && parmtypes
== void_list_node
)
4246 return error_mark_node
;
4249 /* Parse a user-defined char constant. Returns a call to a user-defined
4250 literal operator taking the character as an argument. */
4253 cp_parser_userdef_char_literal (cp_parser
*parser
)
4255 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4256 tree literal
= token
->u
.value
;
4257 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4258 tree value
= USERDEF_LITERAL_VALUE (literal
);
4259 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4262 /* Build up a call to the user-defined operator */
4263 /* Lookup the name we got back from the id-expression. */
4264 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4265 vec_safe_push (args
, value
);
4266 decl
= lookup_literal_operator (name
, args
);
4267 if (!decl
|| decl
== error_mark_node
)
4269 error ("unable to find character literal operator %qD with %qT argument",
4270 name
, TREE_TYPE (value
));
4271 release_tree_vector (args
);
4272 return error_mark_node
;
4274 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4275 release_tree_vector (args
);
4279 /* A subroutine of cp_parser_userdef_numeric_literal to
4280 create a char... template parameter pack from a string node. */
4283 make_char_string_pack (tree value
)
4286 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4287 const char *str
= TREE_STRING_POINTER (value
);
4288 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4289 tree argvec
= make_tree_vec (1);
4291 /* Fill in CHARVEC with all of the parameters. */
4292 charvec
= make_tree_vec (len
);
4293 for (i
= 0; i
< len
; ++i
)
4294 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4296 /* Build the argument packs. */
4297 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4299 TREE_VEC_ELT (argvec
, 0) = argpack
;
4304 /* A subroutine of cp_parser_userdef_numeric_literal to
4305 create a char... template parameter pack from a string node. */
4308 make_string_pack (tree value
)
4311 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4312 const unsigned char *str
4313 = (const unsigned char *) TREE_STRING_POINTER (value
);
4314 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4315 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4316 tree argvec
= make_tree_vec (2);
4318 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4319 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4321 /* First template parm is character type. */
4322 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4324 /* Fill in CHARVEC with all of the parameters. */
4325 charvec
= make_tree_vec (len
);
4326 for (int i
= 0; i
< len
; ++i
)
4327 TREE_VEC_ELT (charvec
, i
)
4328 = double_int_to_tree (str_char_type_node
,
4329 double_int::from_buffer (str
+ i
* sz
, sz
));
4331 /* Build the argument packs. */
4332 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4334 TREE_VEC_ELT (argvec
, 1) = argpack
;
4339 /* Parse a user-defined numeric constant. returns a call to a user-defined
4340 literal operator. */
4343 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4345 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4346 tree literal
= token
->u
.value
;
4347 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4348 tree value
= USERDEF_LITERAL_VALUE (literal
);
4349 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4350 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4351 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4353 vec
<tree
, va_gc
> *args
;
4355 /* Look for a literal operator taking the exact type of numeric argument
4356 as the literal value. */
4357 args
= make_tree_vector ();
4358 vec_safe_push (args
, value
);
4359 decl
= lookup_literal_operator (name
, args
);
4360 if (decl
&& decl
!= error_mark_node
)
4362 result
= finish_call_expr (decl
, &args
, false, true,
4363 tf_warning_or_error
);
4365 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4367 warning_at (token
->location
, OPT_Woverflow
,
4368 "integer literal exceeds range of %qT type",
4369 long_long_unsigned_type_node
);
4374 warning_at (token
->location
, OPT_Woverflow
,
4375 "floating literal exceeds range of %qT type",
4376 long_double_type_node
);
4377 else if (overflow
< 0)
4378 warning_at (token
->location
, OPT_Woverflow
,
4379 "floating literal truncated to zero");
4382 release_tree_vector (args
);
4385 release_tree_vector (args
);
4387 /* If the numeric argument didn't work, look for a raw literal
4388 operator taking a const char* argument consisting of the number
4389 in string format. */
4390 args
= make_tree_vector ();
4391 vec_safe_push (args
, num_string
);
4392 decl
= lookup_literal_operator (name
, args
);
4393 if (decl
&& decl
!= error_mark_node
)
4395 result
= finish_call_expr (decl
, &args
, false, true,
4396 tf_warning_or_error
);
4397 release_tree_vector (args
);
4400 release_tree_vector (args
);
4402 /* If the raw literal didn't work, look for a non-type template
4403 function with parameter pack char.... Call the function with
4404 template parameter characters representing the number. */
4405 args
= make_tree_vector ();
4406 decl
= lookup_literal_operator (name
, args
);
4407 if (decl
&& decl
!= error_mark_node
)
4409 tree tmpl_args
= make_char_string_pack (num_string
);
4410 decl
= lookup_template_function (decl
, tmpl_args
);
4411 result
= finish_call_expr (decl
, &args
, false, true,
4412 tf_warning_or_error
);
4413 release_tree_vector (args
);
4417 release_tree_vector (args
);
4419 /* In C++14 the standard library defines complex number suffixes that
4420 conflict with GNU extensions. Prefer them if <complex> is #included. */
4421 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4422 bool i14
= (cxx_dialect
> cxx11
4423 && (id_equal (suffix_id
, "i")
4424 || id_equal (suffix_id
, "if")
4425 || id_equal (suffix_id
, "il")));
4426 diagnostic_t kind
= DK_ERROR
;
4431 tree cxlit
= lookup_qualified_name (std_node
,
4432 get_identifier ("complex_literals"),
4434 if (cxlit
== error_mark_node
)
4436 /* No <complex>, so pedwarn and use GNU semantics. */
4438 opt
= OPT_Wpedantic
;
4443 = emit_diagnostic (kind
, input_location
, opt
,
4444 "unable to find numeric literal operator %qD", name
);
4447 /* Don't inform either. */;
4450 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4451 "(from <complex>) to enable the C++14 user-defined literal "
4454 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4455 "GNU built-in suffix");
4458 inform (token
->location
, "use -fext-numeric-literals "
4459 "to enable more built-in suffixes");
4461 if (kind
== DK_ERROR
)
4462 value
= error_mark_node
;
4465 /* Use the built-in semantics. */
4467 if (id_equal (suffix_id
, "i"))
4469 if (TREE_CODE (value
) == INTEGER_CST
)
4470 type
= integer_type_node
;
4472 type
= double_type_node
;
4474 else if (id_equal (suffix_id
, "if"))
4475 type
= float_type_node
;
4476 else /* if (id_equal (suffix_id, "il")) */
4477 type
= long_double_type_node
;
4479 value
= build_complex (build_complex_type (type
),
4480 fold_convert (type
, integer_zero_node
),
4481 fold_convert (type
, value
));
4484 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4485 /* Avoid repeated diagnostics. */
4486 token
->u
.value
= value
;
4490 /* Parse a user-defined string constant. Returns a call to a user-defined
4491 literal operator taking a character pointer and the length of the string
4495 cp_parser_userdef_string_literal (tree literal
)
4497 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4498 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4499 tree value
= USERDEF_LITERAL_VALUE (literal
);
4500 int len
= TREE_STRING_LENGTH (value
)
4501 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4503 vec
<tree
, va_gc
> *args
;
4505 /* Build up a call to the user-defined operator. */
4506 /* Lookup the name we got back from the id-expression. */
4507 args
= make_tree_vector ();
4508 vec_safe_push (args
, value
);
4509 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4510 decl
= lookup_literal_operator (name
, args
);
4512 if (decl
&& decl
!= error_mark_node
)
4514 result
= finish_call_expr (decl
, &args
, false, true,
4515 tf_warning_or_error
);
4516 release_tree_vector (args
);
4519 release_tree_vector (args
);
4521 /* Look for a template function with typename parameter CharT
4522 and parameter pack CharT... Call the function with
4523 template parameter characters representing the string. */
4524 args
= make_tree_vector ();
4525 decl
= lookup_literal_operator (name
, args
);
4526 if (decl
&& decl
!= error_mark_node
)
4528 tree tmpl_args
= make_string_pack (value
);
4529 decl
= lookup_template_function (decl
, tmpl_args
);
4530 result
= finish_call_expr (decl
, &args
, false, true,
4531 tf_warning_or_error
);
4532 release_tree_vector (args
);
4535 release_tree_vector (args
);
4537 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4538 name
, TREE_TYPE (value
), size_type_node
);
4539 return error_mark_node
;
4543 /* Basic concepts [gram.basic] */
4545 /* Parse a translation-unit.
4548 declaration-seq [opt]
4550 Returns TRUE if all went well. */
4553 cp_parser_translation_unit (cp_parser
* parser
)
4555 /* The address of the first non-permanent object on the declarator
4557 static void *declarator_obstack_base
;
4561 /* Create the declarator obstack, if necessary. */
4562 if (!cp_error_declarator
)
4564 gcc_obstack_init (&declarator_obstack
);
4565 /* Create the error declarator. */
4566 cp_error_declarator
= make_declarator (cdk_error
);
4567 /* Create the empty parameter list. */
4568 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4570 /* Remember where the base of the declarator obstack lies. */
4571 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4574 cp_parser_declaration_seq_opt (parser
);
4576 /* If there are no tokens left then all went well. */
4577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4579 /* Get rid of the token array; we don't need it any more. */
4580 cp_lexer_destroy (parser
->lexer
);
4581 parser
->lexer
= NULL
;
4583 /* This file might have been a context that's implicitly extern
4584 "C". If so, pop the lang context. (Only relevant for PCH.) */
4585 if (parser
->implicit_extern_c
)
4587 pop_lang_context ();
4588 parser
->implicit_extern_c
= false;
4592 finish_translation_unit ();
4598 cp_parser_error (parser
, "expected declaration");
4602 /* Make sure the declarator obstack was fully cleaned up. */
4603 gcc_assert (obstack_next_free (&declarator_obstack
)
4604 == declarator_obstack_base
);
4606 /* All went well. */
4610 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4611 decltype context. */
4613 static inline tsubst_flags_t
4614 complain_flags (bool decltype_p
)
4616 tsubst_flags_t complain
= tf_warning_or_error
;
4618 complain
|= tf_decltype
;
4622 /* We're about to parse a collection of statements. If we're currently
4623 parsing tentatively, set up a firewall so that any nested
4624 cp_parser_commit_to_tentative_parse won't affect the current context. */
4626 static cp_token_position
4627 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4629 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4632 cp_parser_parse_tentatively (parser
);
4633 cp_parser_commit_to_topmost_tentative_parse (parser
);
4634 return cp_lexer_token_position (parser
->lexer
, false);
4637 /* We've finished parsing the collection of statements. Wrap up the
4638 firewall and replace the relevant tokens with the parsed form. */
4641 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4647 /* Finish the firewall level. */
4648 cp_parser_parse_definitely (parser
);
4649 /* And remember the result of the parse for when we try again. */
4650 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4651 token
->type
= CPP_PREPARSED_EXPR
;
4652 token
->u
.value
= expr
;
4653 token
->keyword
= RID_MAX
;
4654 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4657 /* Like the above functions, but let the user modify the tokens. Used by
4658 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4659 later parses, so it makes sense to localize the effects of
4660 cp_parser_commit_to_tentative_parse. */
4662 struct tentative_firewall
4667 tentative_firewall (cp_parser
*p
): parser(p
)
4669 /* If we're currently parsing tentatively, start a committed level as a
4670 firewall and then an inner tentative parse. */
4671 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4673 cp_parser_parse_tentatively (parser
);
4674 cp_parser_commit_to_topmost_tentative_parse (parser
);
4675 cp_parser_parse_tentatively (parser
);
4679 ~tentative_firewall()
4683 /* Finish the inner tentative parse and the firewall, propagating any
4684 uncommitted error state to the outer tentative parse. */
4685 bool err
= cp_parser_error_occurred (parser
);
4686 cp_parser_parse_definitely (parser
);
4687 cp_parser_parse_definitely (parser
);
4689 cp_parser_simulate_error (parser
);
4694 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4695 This class is for tracking such a matching pair of symbols.
4696 In particular, it tracks the location of the first token,
4697 so that if the second token is missing, we can highlight the
4698 location of the first token when notifying the user about the
4701 template <typename traits_t
>
4705 /* token_pair's ctor. */
4706 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4708 /* If the next token is the opening symbol for this pair, consume it and
4710 Otherwise, issue an error and return false.
4711 In either case, record the location of the opening token. */
4713 bool require_open (cp_parser
*parser
)
4715 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4716 return cp_parser_require (parser
, traits_t::open_token_type
,
4717 traits_t::required_token_open
);
4720 /* Consume the next token from PARSER, recording its location as
4721 that of the opening token within the pair. */
4723 cp_token
* consume_open (cp_parser
*parser
)
4725 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4726 gcc_assert (tok
->type
== traits_t::open_token_type
);
4727 m_open_loc
= tok
->location
;
4731 /* If the next token is the closing symbol for this pair, consume it
4733 Otherwise, issue an error, highlighting the location of the
4734 corresponding opening token, and return NULL. */
4736 cp_token
*require_close (cp_parser
*parser
) const
4738 return cp_parser_require (parser
, traits_t::close_token_type
,
4739 traits_t::required_token_close
,
4744 location_t m_open_loc
;
4747 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4749 struct matching_paren_traits
4751 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4752 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4753 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4754 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4757 /* "matching_parens" is a token_pair<T> class for tracking matching
4758 pairs of parentheses. */
4760 typedef token_pair
<matching_paren_traits
> matching_parens
;
4762 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4764 struct matching_brace_traits
4766 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4767 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4768 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4769 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4772 /* "matching_braces" is a token_pair<T> class for tracking matching
4775 typedef token_pair
<matching_brace_traits
> matching_braces
;
4778 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4779 enclosing parentheses. */
4782 cp_parser_statement_expr (cp_parser
*parser
)
4784 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4786 /* Consume the '('. */
4787 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4788 matching_parens parens
;
4789 parens
.consume_open (parser
);
4790 /* Start the statement-expression. */
4791 tree expr
= begin_stmt_expr ();
4792 /* Parse the compound-statement. */
4793 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4795 expr
= finish_stmt_expr (expr
, false);
4796 /* Consume the ')'. */
4797 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4798 if (!parens
.require_close (parser
))
4799 cp_parser_skip_to_end_of_statement (parser
);
4801 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4802 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4803 return cp_expr (expr
, combined_loc
);
4806 /* Expressions [gram.expr] */
4808 /* Parse a fold-operator.
4811 - * / % ^ & | = < > << >>
4812 = -= *= /= %= ^= &= |= <<= >>=
4813 == != <= >= && || , .* ->*
4815 This returns the tree code corresponding to the matched operator
4816 as an int. When the current token matches a compound assignment
4817 opertor, the resulting tree code is the negative value of the
4818 non-assignment operator. */
4821 cp_parser_fold_operator (cp_token
*token
)
4823 switch (token
->type
)
4825 case CPP_PLUS
: return PLUS_EXPR
;
4826 case CPP_MINUS
: return MINUS_EXPR
;
4827 case CPP_MULT
: return MULT_EXPR
;
4828 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4829 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4830 case CPP_XOR
: return BIT_XOR_EXPR
;
4831 case CPP_AND
: return BIT_AND_EXPR
;
4832 case CPP_OR
: return BIT_IOR_EXPR
;
4833 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4834 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4836 case CPP_EQ
: return -NOP_EXPR
;
4837 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4838 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4839 case CPP_MULT_EQ
: return -MULT_EXPR
;
4840 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4841 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4842 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4843 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4844 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4845 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4846 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4848 case CPP_EQ_EQ
: return EQ_EXPR
;
4849 case CPP_NOT_EQ
: return NE_EXPR
;
4850 case CPP_LESS
: return LT_EXPR
;
4851 case CPP_GREATER
: return GT_EXPR
;
4852 case CPP_LESS_EQ
: return LE_EXPR
;
4853 case CPP_GREATER_EQ
: return GE_EXPR
;
4855 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4856 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4858 case CPP_COMMA
: return COMPOUND_EXPR
;
4860 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4861 case CPP_DEREF_STAR
: return MEMBER_REF
;
4863 default: return ERROR_MARK
;
4867 /* Returns true if CODE indicates a binary expression, which is not allowed in
4868 the LHS of a fold-expression. More codes will need to be added to use this
4869 function in other contexts. */
4872 is_binary_op (tree_code code
)
4877 case POINTER_PLUS_EXPR
:
4880 case TRUNC_DIV_EXPR
:
4881 case TRUNC_MOD_EXPR
:
4897 case TRUTH_ANDIF_EXPR
:
4898 case TRUTH_ORIF_EXPR
:
4911 /* If the next token is a suitable fold operator, consume it and return as
4912 the function above. */
4915 cp_parser_fold_operator (cp_parser
*parser
)
4917 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4918 int code
= cp_parser_fold_operator (token
);
4919 if (code
!= ERROR_MARK
)
4920 cp_lexer_consume_token (parser
->lexer
);
4924 /* Parse a fold-expression.
4927 ( ... folding-operator cast-expression)
4928 ( cast-expression folding-operator ... )
4929 ( cast-expression folding operator ... folding-operator cast-expression)
4931 Note that the '(' and ')' are matched in primary expression. */
4934 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4941 cp_lexer_consume_token (parser
->lexer
);
4942 int op
= cp_parser_fold_operator (parser
);
4943 if (op
== ERROR_MARK
)
4945 cp_parser_error (parser
, "expected binary operator");
4946 return error_mark_node
;
4949 tree expr
= cp_parser_cast_expression (parser
, false, false,
4951 if (expr
== error_mark_node
)
4952 return error_mark_node
;
4953 return finish_left_unary_fold_expr (expr
, op
);
4956 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4957 int op
= cp_parser_fold_operator (parser
);
4958 if (op
== ERROR_MARK
)
4960 cp_parser_error (parser
, "expected binary operator");
4961 return error_mark_node
;
4964 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4966 cp_parser_error (parser
, "expected ...");
4967 return error_mark_node
;
4969 cp_lexer_consume_token (parser
->lexer
);
4971 /* The operands of a fold-expression are cast-expressions, so binary or
4972 conditional expressions are not allowed. We check this here to avoid
4973 tentative parsing. */
4974 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4975 /* OK, the expression was parenthesized. */;
4976 else if (is_binary_op (TREE_CODE (expr1
)))
4977 error_at (location_of (expr1
),
4978 "binary expression in operand of fold-expression");
4979 else if (TREE_CODE (expr1
) == COND_EXPR
4980 || (REFERENCE_REF_P (expr1
)
4981 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
4982 error_at (location_of (expr1
),
4983 "conditional expression in operand of fold-expression");
4986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4987 return finish_right_unary_fold_expr (expr1
, op
);
4989 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4991 cp_parser_error (parser
, "mismatched operator in fold-expression");
4992 return error_mark_node
;
4994 cp_lexer_consume_token (parser
->lexer
);
4996 // Binary left or right fold.
4997 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4998 if (expr2
== error_mark_node
)
4999 return error_mark_node
;
5000 return finish_binary_fold_expr (expr1
, expr2
, op
);
5003 /* Parse a primary-expression.
5010 lambda-expression (C++11)
5015 ( compound-statement )
5016 __builtin_va_arg ( assignment-expression , type-id )
5017 __builtin_offsetof ( type-id , offsetof-expression )
5020 __has_nothrow_assign ( type-id )
5021 __has_nothrow_constructor ( type-id )
5022 __has_nothrow_copy ( type-id )
5023 __has_trivial_assign ( type-id )
5024 __has_trivial_constructor ( type-id )
5025 __has_trivial_copy ( type-id )
5026 __has_trivial_destructor ( type-id )
5027 __has_virtual_destructor ( type-id )
5028 __is_abstract ( type-id )
5029 __is_base_of ( type-id , type-id )
5030 __is_class ( type-id )
5031 __is_empty ( type-id )
5032 __is_enum ( type-id )
5033 __is_final ( type-id )
5034 __is_literal_type ( type-id )
5035 __is_pod ( type-id )
5036 __is_polymorphic ( type-id )
5037 __is_std_layout ( type-id )
5038 __is_trivial ( type-id )
5039 __is_union ( type-id )
5041 Objective-C++ Extension:
5049 ADDRESS_P is true iff this expression was immediately preceded by
5050 "&" and therefore might denote a pointer-to-member. CAST_P is true
5051 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5052 true iff this expression is a template argument.
5054 Returns a representation of the expression. Upon return, *IDK
5055 indicates what kind of id-expression (if any) was present. */
5058 cp_parser_primary_expression (cp_parser
*parser
,
5061 bool template_arg_p
,
5065 cp_token
*token
= NULL
;
5067 /* Assume the primary expression is not an id-expression. */
5068 *idk
= CP_ID_KIND_NONE
;
5070 /* Peek at the next token. */
5071 token
= cp_lexer_peek_token (parser
->lexer
);
5072 switch ((int) token
->type
)
5081 user-defined-literal */
5088 case CPP_PREPARSED_EXPR
:
5089 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5090 return cp_parser_userdef_numeric_literal (parser
);
5091 token
= cp_lexer_consume_token (parser
->lexer
);
5092 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5094 error_at (token
->location
,
5095 "fixed-point types not supported in C++");
5096 return error_mark_node
;
5098 /* Floating-point literals are only allowed in an integral
5099 constant expression if they are cast to an integral or
5100 enumeration type. */
5101 if (TREE_CODE (token
->u
.value
) == REAL_CST
5102 && parser
->integral_constant_expression_p
5105 /* CAST_P will be set even in invalid code like "int(2.7 +
5106 ...)". Therefore, we have to check that the next token
5107 is sure to end the cast. */
5110 cp_token
*next_token
;
5112 next_token
= cp_lexer_peek_token (parser
->lexer
);
5113 if (/* The comma at the end of an
5114 enumerator-definition. */
5115 next_token
->type
!= CPP_COMMA
5116 /* The curly brace at the end of an enum-specifier. */
5117 && next_token
->type
!= CPP_CLOSE_BRACE
5118 /* The end of a statement. */
5119 && next_token
->type
!= CPP_SEMICOLON
5120 /* The end of the cast-expression. */
5121 && next_token
->type
!= CPP_CLOSE_PAREN
5122 /* The end of an array bound. */
5123 && next_token
->type
!= CPP_CLOSE_SQUARE
5124 /* The closing ">" in a template-argument-list. */
5125 && (next_token
->type
!= CPP_GREATER
5126 || parser
->greater_than_is_operator_p
)
5127 /* C++0x only: A ">>" treated like two ">" tokens,
5128 in a template-argument-list. */
5129 && (next_token
->type
!= CPP_RSHIFT
5130 || (cxx_dialect
== cxx98
)
5131 || parser
->greater_than_is_operator_p
))
5135 /* If we are within a cast, then the constraint that the
5136 cast is to an integral or enumeration type will be
5137 checked at that point. If we are not within a cast, then
5138 this code is invalid. */
5140 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5142 return cp_expr (token
->u
.value
, token
->location
);
5144 case CPP_CHAR_USERDEF
:
5145 case CPP_CHAR16_USERDEF
:
5146 case CPP_CHAR32_USERDEF
:
5147 case CPP_WCHAR_USERDEF
:
5148 case CPP_UTF8CHAR_USERDEF
:
5149 return cp_parser_userdef_char_literal (parser
);
5155 case CPP_UTF8STRING
:
5156 case CPP_STRING_USERDEF
:
5157 case CPP_STRING16_USERDEF
:
5158 case CPP_STRING32_USERDEF
:
5159 case CPP_WSTRING_USERDEF
:
5160 case CPP_UTF8STRING_USERDEF
:
5161 /* ??? Should wide strings be allowed when parser->translate_strings_p
5162 is false (i.e. in attributes)? If not, we can kill the third
5163 argument to cp_parser_string_literal. */
5164 return cp_parser_string_literal (parser
,
5165 parser
->translate_strings_p
,
5168 case CPP_OPEN_PAREN
:
5169 /* If we see `( { ' then we are looking at the beginning of
5170 a GNU statement-expression. */
5171 if (cp_parser_allow_gnu_extensions_p (parser
)
5172 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5174 /* Statement-expressions are not allowed by the standard. */
5175 pedwarn (token
->location
, OPT_Wpedantic
,
5176 "ISO C++ forbids braced-groups within expressions");
5178 /* And they're not allowed outside of a function-body; you
5179 cannot, for example, write:
5181 int i = ({ int j = 3; j + 1; });
5183 at class or namespace scope. */
5184 if (!parser
->in_function_body
5185 || parser
->in_template_argument_list_p
)
5187 error_at (token
->location
,
5188 "statement-expressions are not allowed outside "
5189 "functions nor in template-argument lists");
5190 cp_parser_skip_to_end_of_block_or_statement (parser
);
5191 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5192 cp_lexer_consume_token (parser
->lexer
);
5193 return error_mark_node
;
5196 return cp_parser_statement_expr (parser
);
5198 /* Otherwise it's a normal parenthesized expression. */
5201 bool saved_greater_than_is_operator_p
;
5203 location_t open_paren_loc
= token
->location
;
5205 /* Consume the `('. */
5206 matching_parens parens
;
5207 parens
.consume_open (parser
);
5208 /* Within a parenthesized expression, a `>' token is always
5209 the greater-than operator. */
5210 saved_greater_than_is_operator_p
5211 = parser
->greater_than_is_operator_p
;
5212 parser
->greater_than_is_operator_p
= true;
5214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5215 /* Left fold expression. */
5218 /* Parse the parenthesized expression. */
5219 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5221 token
= cp_lexer_peek_token (parser
->lexer
);
5222 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5224 expr
= cp_parser_fold_expression (parser
, expr
);
5225 if (expr
!= error_mark_node
5226 && cxx_dialect
< cxx17
5227 && !in_system_header_at (input_location
))
5228 pedwarn (input_location
, 0, "fold-expressions only available "
5229 "with -std=c++17 or -std=gnu++17");
5232 /* Let the front end know that this expression was
5233 enclosed in parentheses. This matters in case, for
5234 example, the expression is of the form `A::B', since
5235 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5237 expr
= finish_parenthesized_expr (expr
);
5239 /* DR 705: Wrapping an unqualified name in parentheses
5240 suppresses arg-dependent lookup. We want to pass back
5241 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5242 (c++/37862), but none of the others. */
5243 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5244 *idk
= CP_ID_KIND_NONE
;
5246 /* The `>' token might be the end of a template-id or
5247 template-parameter-list now. */
5248 parser
->greater_than_is_operator_p
5249 = saved_greater_than_is_operator_p
;
5251 /* Consume the `)'. */
5252 token
= cp_lexer_peek_token (parser
->lexer
);
5253 location_t close_paren_loc
= token
->location
;
5254 expr
.set_range (open_paren_loc
, close_paren_loc
);
5255 if (!parens
.require_close (parser
)
5256 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5257 cp_parser_skip_to_end_of_statement (parser
);
5262 case CPP_OPEN_SQUARE
:
5264 if (c_dialect_objc ())
5266 /* We might have an Objective-C++ message. */
5267 cp_parser_parse_tentatively (parser
);
5268 tree msg
= cp_parser_objc_message_expression (parser
);
5269 /* If that works out, we're done ... */
5270 if (cp_parser_parse_definitely (parser
))
5272 /* ... else, fall though to see if it's a lambda. */
5274 cp_expr lam
= cp_parser_lambda_expression (parser
);
5275 /* Don't warn about a failed tentative parse. */
5276 if (cp_parser_error_occurred (parser
))
5277 return error_mark_node
;
5278 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5282 case CPP_OBJC_STRING
:
5283 if (c_dialect_objc ())
5284 /* We have an Objective-C++ string literal. */
5285 return cp_parser_objc_expression (parser
);
5286 cp_parser_error (parser
, "expected primary-expression");
5287 return error_mark_node
;
5290 switch (token
->keyword
)
5292 /* These two are the boolean literals. */
5294 cp_lexer_consume_token (parser
->lexer
);
5295 return cp_expr (boolean_true_node
, token
->location
);
5297 cp_lexer_consume_token (parser
->lexer
);
5298 return cp_expr (boolean_false_node
, token
->location
);
5300 /* The `__null' literal. */
5302 cp_lexer_consume_token (parser
->lexer
);
5303 return cp_expr (null_node
, token
->location
);
5305 /* The `nullptr' literal. */
5307 cp_lexer_consume_token (parser
->lexer
);
5308 return cp_expr (nullptr_node
, token
->location
);
5310 /* Recognize the `this' keyword. */
5312 cp_lexer_consume_token (parser
->lexer
);
5313 if (parser
->local_variables_forbidden_p
)
5315 error_at (token
->location
,
5316 "%<this%> may not be used in this context");
5317 return error_mark_node
;
5319 /* Pointers cannot appear in constant-expressions. */
5320 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5321 return error_mark_node
;
5322 return cp_expr (finish_this_expr (), token
->location
);
5324 /* The `operator' keyword can be the beginning of an
5329 case RID_FUNCTION_NAME
:
5330 case RID_PRETTY_FUNCTION_NAME
:
5331 case RID_C99_FUNCTION_NAME
:
5333 non_integral_constant name
;
5335 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5336 __func__ are the names of variables -- but they are
5337 treated specially. Therefore, they are handled here,
5338 rather than relying on the generic id-expression logic
5339 below. Grammatically, these names are id-expressions.
5341 Consume the token. */
5342 token
= cp_lexer_consume_token (parser
->lexer
);
5344 switch (token
->keyword
)
5346 case RID_FUNCTION_NAME
:
5347 name
= NIC_FUNC_NAME
;
5349 case RID_PRETTY_FUNCTION_NAME
:
5350 name
= NIC_PRETTY_FUNC
;
5352 case RID_C99_FUNCTION_NAME
:
5353 name
= NIC_C99_FUNC
;
5359 if (cp_parser_non_integral_constant_expression (parser
, name
))
5360 return error_mark_node
;
5362 /* Look up the name. */
5363 return finish_fname (token
->u
.value
);
5370 source_location type_location
;
5371 location_t start_loc
5372 = cp_lexer_peek_token (parser
->lexer
)->location
;
5373 /* The `__builtin_va_arg' construct is used to handle
5374 `va_arg'. Consume the `__builtin_va_arg' token. */
5375 cp_lexer_consume_token (parser
->lexer
);
5376 /* Look for the opening `('. */
5377 matching_parens parens
;
5378 parens
.require_open (parser
);
5379 /* Now, parse the assignment-expression. */
5380 expression
= cp_parser_assignment_expression (parser
);
5381 /* Look for the `,'. */
5382 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5383 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5384 /* Parse the type-id. */
5386 type_id_in_expr_sentinel
s (parser
);
5387 type
= cp_parser_type_id (parser
);
5389 /* Look for the closing `)'. */
5390 location_t finish_loc
5391 = cp_lexer_peek_token (parser
->lexer
)->location
;
5392 parens
.require_close (parser
);
5393 /* Using `va_arg' in a constant-expression is not
5395 if (cp_parser_non_integral_constant_expression (parser
,
5397 return error_mark_node
;
5398 /* Construct a location of the form:
5399 __builtin_va_arg (v, int)
5400 ~~~~~~~~~~~~~~~~~~~~~^~~~
5401 with the caret at the type, ranging from the start of the
5402 "__builtin_va_arg" token to the close paren. */
5403 location_t combined_loc
5404 = make_location (type_location
, start_loc
, finish_loc
);
5405 return build_x_va_arg (combined_loc
, expression
, type
);
5409 return cp_parser_builtin_offsetof (parser
);
5411 case RID_HAS_NOTHROW_ASSIGN
:
5412 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5413 case RID_HAS_NOTHROW_COPY
:
5414 case RID_HAS_TRIVIAL_ASSIGN
:
5415 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5416 case RID_HAS_TRIVIAL_COPY
:
5417 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5418 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5419 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5420 case RID_IS_ABSTRACT
:
5421 case RID_IS_AGGREGATE
:
5422 case RID_IS_BASE_OF
:
5427 case RID_IS_LITERAL_TYPE
:
5429 case RID_IS_POLYMORPHIC
:
5430 case RID_IS_SAME_AS
:
5431 case RID_IS_STD_LAYOUT
:
5432 case RID_IS_TRIVIAL
:
5433 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5434 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5435 case RID_IS_TRIVIALLY_COPYABLE
:
5437 case RID_IS_ASSIGNABLE
:
5438 case RID_IS_CONSTRUCTIBLE
:
5439 return cp_parser_trait_expr (parser
, token
->keyword
);
5443 return cp_parser_requires_expression (parser
);
5445 /* Objective-C++ expressions. */
5447 case RID_AT_PROTOCOL
:
5448 case RID_AT_SELECTOR
:
5449 return cp_parser_objc_expression (parser
);
5452 if (parser
->in_function_body
5453 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5456 error_at (token
->location
,
5457 "a template declaration cannot appear at block scope");
5458 cp_parser_skip_to_end_of_block_or_statement (parser
);
5459 return error_mark_node
;
5463 cp_parser_error (parser
, "expected primary-expression");
5464 return error_mark_node
;
5467 /* An id-expression can start with either an identifier, a
5468 `::' as the beginning of a qualified-id, or the "operator"
5472 case CPP_TEMPLATE_ID
:
5473 case CPP_NESTED_NAME_SPECIFIER
:
5476 cp_expr id_expression
;
5478 const char *error_msg
;
5481 cp_token
*id_expr_token
;
5483 /* Parse the id-expression. */
5485 = cp_parser_id_expression (parser
,
5486 /*template_keyword_p=*/false,
5487 /*check_dependency_p=*/true,
5489 /*declarator_p=*/false,
5490 /*optional_p=*/false);
5491 if (id_expression
== error_mark_node
)
5492 return error_mark_node
;
5493 id_expr_token
= token
;
5494 token
= cp_lexer_peek_token (parser
->lexer
);
5495 done
= (token
->type
!= CPP_OPEN_SQUARE
5496 && token
->type
!= CPP_OPEN_PAREN
5497 && token
->type
!= CPP_DOT
5498 && token
->type
!= CPP_DEREF
5499 && token
->type
!= CPP_PLUS_PLUS
5500 && token
->type
!= CPP_MINUS_MINUS
);
5501 /* If we have a template-id, then no further lookup is
5502 required. If the template-id was for a template-class, we
5503 will sometimes have a TYPE_DECL at this point. */
5504 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5505 || TREE_CODE (id_expression
) == TYPE_DECL
)
5506 decl
= id_expression
;
5507 /* Look up the name. */
5510 tree ambiguous_decls
;
5512 /* If we already know that this lookup is ambiguous, then
5513 we've already issued an error message; there's no reason
5515 if (id_expr_token
->type
== CPP_NAME
5516 && id_expr_token
->error_reported
)
5518 cp_parser_simulate_error (parser
);
5519 return error_mark_node
;
5522 decl
= cp_parser_lookup_name (parser
, id_expression
,
5525 /*is_namespace=*/false,
5526 /*check_dependency=*/true,
5528 id_expr_token
->location
);
5529 /* If the lookup was ambiguous, an error will already have
5531 if (ambiguous_decls
)
5532 return error_mark_node
;
5534 /* In Objective-C++, we may have an Objective-C 2.0
5535 dot-syntax for classes here. */
5536 if (c_dialect_objc ()
5537 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5538 && TREE_CODE (decl
) == TYPE_DECL
5539 && objc_is_class_name (decl
))
5542 cp_lexer_consume_token (parser
->lexer
);
5543 component
= cp_parser_identifier (parser
);
5544 if (component
== error_mark_node
)
5545 return error_mark_node
;
5547 tree result
= objc_build_class_component_ref (id_expression
,
5549 /* Build a location of the form:
5552 with caret at the start of the component name (at
5553 input_location), ranging from the start of the id_expression
5554 to the end of the component name. */
5555 location_t combined_loc
5556 = make_location (input_location
, id_expression
.get_start (),
5557 get_finish (input_location
));
5558 protected_set_expr_location (result
, combined_loc
);
5562 /* In Objective-C++, an instance variable (ivar) may be preferred
5563 to whatever cp_parser_lookup_name() found.
5564 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5565 rest of c-family, we have to do a little extra work to preserve
5566 any location information in cp_expr "decl". Given that
5567 objc_lookup_ivar is implemented in "c-family" and "objc", we
5568 have a trip through the pure "tree" type, rather than cp_expr.
5569 Naively copying it back to "decl" would implicitly give the
5570 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5571 store an EXPR_LOCATION. Hence we only update "decl" (and
5572 hence its location_t) if we get back a different tree node. */
5573 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5575 if (decl_tree
!= decl
.get_value ())
5576 decl
= cp_expr (decl_tree
);
5578 /* If name lookup gives us a SCOPE_REF, then the
5579 qualifying scope was dependent. */
5580 if (TREE_CODE (decl
) == SCOPE_REF
)
5582 /* At this point, we do not know if DECL is a valid
5583 integral constant expression. We assume that it is
5584 in fact such an expression, so that code like:
5586 template <int N> struct A {
5590 is accepted. At template-instantiation time, we
5591 will check that B<N>::i is actually a constant. */
5594 /* Check to see if DECL is a local variable in a context
5595 where that is forbidden. */
5596 if (parser
->local_variables_forbidden_p
5597 && local_variable_p (decl
))
5599 error_at (id_expr_token
->location
,
5600 "local variable %qD may not appear in this context",
5602 return error_mark_node
;
5606 decl
= (finish_id_expression
5607 (id_expression
, decl
, parser
->scope
,
5609 parser
->integral_constant_expression_p
,
5610 parser
->allow_non_integral_constant_expression_p
,
5611 &parser
->non_integral_constant_expression_p
,
5612 template_p
, done
, address_p
,
5615 id_expression
.get_location ()));
5617 cp_parser_error (parser
, error_msg
);
5618 decl
.set_location (id_expr_token
->location
);
5622 /* Anything else is an error. */
5624 cp_parser_error (parser
, "expected primary-expression");
5625 return error_mark_node
;
5629 static inline cp_expr
5630 cp_parser_primary_expression (cp_parser
*parser
,
5633 bool template_arg_p
,
5636 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5637 /*decltype*/false, idk
);
5640 /* Parse an id-expression.
5647 :: [opt] nested-name-specifier template [opt] unqualified-id
5649 :: operator-function-id
5652 Return a representation of the unqualified portion of the
5653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5654 a `::' or nested-name-specifier.
5656 Often, if the id-expression was a qualified-id, the caller will
5657 want to make a SCOPE_REF to represent the qualified-id. This
5658 function does not do this in order to avoid wastefully creating
5659 SCOPE_REFs when they are not required.
5661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5665 uninstantiated templates.
5667 If *TEMPLATE_P is non-NULL, it is set to true iff the
5668 `template' keyword is used to explicitly indicate that the entity
5669 named is a template.
5671 If DECLARATOR_P is true, the id-expression is appearing as part of
5672 a declarator, rather than as part of an expression. */
5675 cp_parser_id_expression (cp_parser
*parser
,
5676 bool template_keyword_p
,
5677 bool check_dependency_p
,
5682 bool global_scope_p
;
5683 bool nested_name_specifier_p
;
5685 /* Assume the `template' keyword was not used. */
5687 *template_p
= template_keyword_p
;
5689 /* Look for the optional `::' operator. */
5691 = (!template_keyword_p
5692 && (cp_parser_global_scope_opt (parser
,
5693 /*current_scope_valid_p=*/false)
5696 /* Look for the optional nested-name-specifier. */
5697 nested_name_specifier_p
5698 = (cp_parser_nested_name_specifier_opt (parser
,
5699 /*typename_keyword_p=*/false,
5706 /* If there is a nested-name-specifier, then we are looking at
5707 the first qualified-id production. */
5708 if (nested_name_specifier_p
)
5711 tree saved_object_scope
;
5712 tree saved_qualifying_scope
;
5713 cp_expr unqualified_id
;
5716 /* See if the next token is the `template' keyword. */
5718 template_p
= &is_template
;
5719 *template_p
= cp_parser_optional_template_keyword (parser
);
5720 /* Name lookup we do during the processing of the
5721 unqualified-id might obliterate SCOPE. */
5722 saved_scope
= parser
->scope
;
5723 saved_object_scope
= parser
->object_scope
;
5724 saved_qualifying_scope
= parser
->qualifying_scope
;
5725 /* Process the final unqualified-id. */
5726 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5729 /*optional_p=*/false);
5730 /* Restore the SAVED_SCOPE for our caller. */
5731 parser
->scope
= saved_scope
;
5732 parser
->object_scope
= saved_object_scope
;
5733 parser
->qualifying_scope
= saved_qualifying_scope
;
5735 return unqualified_id
;
5737 /* Otherwise, if we are in global scope, then we are looking at one
5738 of the other qualified-id productions. */
5739 else if (global_scope_p
)
5744 /* Peek at the next token. */
5745 token
= cp_lexer_peek_token (parser
->lexer
);
5747 /* If it's an identifier, and the next token is not a "<", then
5748 we can avoid the template-id case. This is an optimization
5749 for this common case. */
5750 if (token
->type
== CPP_NAME
5751 && !cp_parser_nth_token_starts_template_argument_list_p
5753 return cp_parser_identifier (parser
);
5755 cp_parser_parse_tentatively (parser
);
5756 /* Try a template-id. */
5757 id
= cp_parser_template_id (parser
,
5758 /*template_keyword_p=*/false,
5759 /*check_dependency_p=*/true,
5762 /* If that worked, we're done. */
5763 if (cp_parser_parse_definitely (parser
))
5766 /* Peek at the next token. (Changes in the token buffer may
5767 have invalidated the pointer obtained above.) */
5768 token
= cp_lexer_peek_token (parser
->lexer
);
5770 switch (token
->type
)
5773 return cp_parser_identifier (parser
);
5776 if (token
->keyword
== RID_OPERATOR
)
5777 return cp_parser_operator_function_id (parser
);
5781 cp_parser_error (parser
, "expected id-expression");
5782 return error_mark_node
;
5786 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5787 /*check_dependency_p=*/true,
5792 /* Parse an unqualified-id.
5796 operator-function-id
5797 conversion-function-id
5801 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5802 keyword, in a construct like `A::template ...'.
5804 Returns a representation of unqualified-id. For the `identifier'
5805 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5806 production a BIT_NOT_EXPR is returned; the operand of the
5807 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5808 other productions, see the documentation accompanying the
5809 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5810 names are looked up in uninstantiated templates. If DECLARATOR_P
5811 is true, the unqualified-id is appearing as part of a declarator,
5812 rather than as part of an expression. */
5815 cp_parser_unqualified_id (cp_parser
* parser
,
5816 bool template_keyword_p
,
5817 bool check_dependency_p
,
5823 /* Peek at the next token. */
5824 token
= cp_lexer_peek_token (parser
->lexer
);
5826 switch ((int) token
->type
)
5832 /* We don't know yet whether or not this will be a
5834 cp_parser_parse_tentatively (parser
);
5835 /* Try a template-id. */
5836 id
= cp_parser_template_id (parser
, template_keyword_p
,
5840 /* If it worked, we're done. */
5841 if (cp_parser_parse_definitely (parser
))
5843 /* Otherwise, it's an ordinary identifier. */
5844 return cp_parser_identifier (parser
);
5847 case CPP_TEMPLATE_ID
:
5848 return cp_parser_template_id (parser
, template_keyword_p
,
5856 tree qualifying_scope
;
5861 /* Consume the `~' token. */
5862 cp_lexer_consume_token (parser
->lexer
);
5863 /* Parse the class-name. The standard, as written, seems to
5866 template <typename T> struct S { ~S (); };
5867 template <typename T> S<T>::~S() {}
5869 is invalid, since `~' must be followed by a class-name, but
5870 `S<T>' is dependent, and so not known to be a class.
5871 That's not right; we need to look in uninstantiated
5872 templates. A further complication arises from:
5874 template <typename T> void f(T t) {
5878 Here, it is not possible to look up `T' in the scope of `T'
5879 itself. We must look in both the current scope, and the
5880 scope of the containing complete expression.
5882 Yet another issue is:
5891 The standard does not seem to say that the `S' in `~S'
5892 should refer to the type `S' and not the data member
5895 /* DR 244 says that we look up the name after the "~" in the
5896 same scope as we looked up the qualifying name. That idea
5897 isn't fully worked out; it's more complicated than that. */
5898 scope
= parser
->scope
;
5899 object_scope
= parser
->object_scope
;
5900 qualifying_scope
= parser
->qualifying_scope
;
5902 /* Check for invalid scopes. */
5903 if (scope
== error_mark_node
)
5905 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5906 cp_lexer_consume_token (parser
->lexer
);
5907 return error_mark_node
;
5909 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5911 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5912 error_at (token
->location
,
5913 "scope %qT before %<~%> is not a class-name",
5915 cp_parser_simulate_error (parser
);
5916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5917 cp_lexer_consume_token (parser
->lexer
);
5918 return error_mark_node
;
5920 gcc_assert (!scope
|| TYPE_P (scope
));
5922 /* If the name is of the form "X::~X" it's OK even if X is a
5924 token
= cp_lexer_peek_token (parser
->lexer
);
5926 && token
->type
== CPP_NAME
5927 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5929 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5930 || (CLASS_TYPE_P (scope
)
5931 && constructor_name_p (token
->u
.value
, scope
))))
5933 cp_lexer_consume_token (parser
->lexer
);
5934 return build_nt (BIT_NOT_EXPR
, scope
);
5937 /* ~auto means the destructor of whatever the object is. */
5938 if (cp_parser_is_keyword (token
, RID_AUTO
))
5940 if (cxx_dialect
< cxx14
)
5941 pedwarn (input_location
, 0,
5942 "%<~auto%> only available with "
5943 "-std=c++14 or -std=gnu++14");
5944 cp_lexer_consume_token (parser
->lexer
);
5945 return build_nt (BIT_NOT_EXPR
, make_auto ());
5948 /* If there was an explicit qualification (S::~T), first look
5949 in the scope given by the qualification (i.e., S).
5951 Note: in the calls to cp_parser_class_name below we pass
5952 typename_type so that lookup finds the injected-class-name
5953 rather than the constructor. */
5955 type_decl
= NULL_TREE
;
5958 cp_parser_parse_tentatively (parser
);
5959 type_decl
= cp_parser_class_name (parser
,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5966 if (cp_parser_parse_definitely (parser
))
5969 /* In "N::S::~S", look in "N" as well. */
5970 if (!done
&& scope
&& qualifying_scope
)
5972 cp_parser_parse_tentatively (parser
);
5973 parser
->scope
= qualifying_scope
;
5974 parser
->object_scope
= NULL_TREE
;
5975 parser
->qualifying_scope
= NULL_TREE
;
5977 = cp_parser_class_name (parser
,
5978 /*typename_keyword_p=*/false,
5979 /*template_keyword_p=*/false,
5981 /*check_dependency=*/false,
5982 /*class_head_p=*/false,
5984 if (cp_parser_parse_definitely (parser
))
5987 /* In "p->S::~T", look in the scope given by "*p" as well. */
5988 else if (!done
&& object_scope
)
5990 cp_parser_parse_tentatively (parser
);
5991 parser
->scope
= object_scope
;
5992 parser
->object_scope
= NULL_TREE
;
5993 parser
->qualifying_scope
= NULL_TREE
;
5995 = cp_parser_class_name (parser
,
5996 /*typename_keyword_p=*/false,
5997 /*template_keyword_p=*/false,
5999 /*check_dependency=*/false,
6000 /*class_head_p=*/false,
6002 if (cp_parser_parse_definitely (parser
))
6005 /* Look in the surrounding context. */
6008 parser
->scope
= NULL_TREE
;
6009 parser
->object_scope
= NULL_TREE
;
6010 parser
->qualifying_scope
= NULL_TREE
;
6011 if (processing_template_decl
)
6012 cp_parser_parse_tentatively (parser
);
6014 = cp_parser_class_name (parser
,
6015 /*typename_keyword_p=*/false,
6016 /*template_keyword_p=*/false,
6018 /*check_dependency=*/false,
6019 /*class_head_p=*/false,
6021 if (processing_template_decl
6022 && ! cp_parser_parse_definitely (parser
))
6024 /* We couldn't find a type with this name. If we're parsing
6025 tentatively, fail and try something else. */
6026 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6028 cp_parser_simulate_error (parser
);
6029 return error_mark_node
;
6031 /* Otherwise, accept it and check for a match at instantiation
6033 type_decl
= cp_parser_identifier (parser
);
6034 if (type_decl
!= error_mark_node
)
6035 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6039 /* If an error occurred, assume that the name of the
6040 destructor is the same as the name of the qualifying
6041 class. That allows us to keep parsing after running
6042 into ill-formed destructor names. */
6043 if (type_decl
== error_mark_node
&& scope
)
6044 return build_nt (BIT_NOT_EXPR
, scope
);
6045 else if (type_decl
== error_mark_node
)
6046 return error_mark_node
;
6048 /* Check that destructor name and scope match. */
6049 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6051 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6052 error_at (token
->location
,
6053 "declaration of %<~%T%> as member of %qT",
6055 cp_parser_simulate_error (parser
);
6056 return error_mark_node
;
6061 A typedef-name that names a class shall not be used as the
6062 identifier in the declarator for a destructor declaration. */
6064 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6065 && !DECL_SELF_REFERENCE_P (type_decl
)
6066 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6067 error_at (token
->location
,
6068 "typedef-name %qD used as destructor declarator",
6071 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6075 if (token
->keyword
== RID_OPERATOR
)
6079 /* This could be a template-id, so we try that first. */
6080 cp_parser_parse_tentatively (parser
);
6081 /* Try a template-id. */
6082 id
= cp_parser_template_id (parser
, template_keyword_p
,
6083 /*check_dependency_p=*/true,
6086 /* If that worked, we're done. */
6087 if (cp_parser_parse_definitely (parser
))
6089 /* We still don't know whether we're looking at an
6090 operator-function-id or a conversion-function-id. */
6091 cp_parser_parse_tentatively (parser
);
6092 /* Try an operator-function-id. */
6093 id
= cp_parser_operator_function_id (parser
);
6094 /* If that didn't work, try a conversion-function-id. */
6095 if (!cp_parser_parse_definitely (parser
))
6096 id
= cp_parser_conversion_function_id (parser
);
6105 cp_parser_error (parser
, "expected unqualified-id");
6106 return error_mark_node
;
6110 /* Parse an (optional) nested-name-specifier.
6112 nested-name-specifier: [C++98]
6113 class-or-namespace-name :: nested-name-specifier [opt]
6114 class-or-namespace-name :: template nested-name-specifier [opt]
6116 nested-name-specifier: [C++0x]
6119 nested-name-specifier identifier ::
6120 nested-name-specifier template [opt] simple-template-id ::
6122 PARSER->SCOPE should be set appropriately before this function is
6123 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6124 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6127 Sets PARSER->SCOPE to the class (TYPE) or namespace
6128 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6129 it unchanged if there is no nested-name-specifier. Returns the new
6130 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6132 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6133 part of a declaration and/or decl-specifier. */
6136 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6137 bool typename_keyword_p
,
6138 bool check_dependency_p
,
6140 bool is_declaration
,
6141 bool template_keyword_p
/* = false */)
6143 bool success
= false;
6144 cp_token_position start
= 0;
6147 /* Remember where the nested-name-specifier starts. */
6148 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6150 start
= cp_lexer_token_position (parser
->lexer
, false);
6151 push_deferring_access_checks (dk_deferred
);
6158 tree saved_qualifying_scope
;
6160 /* Spot cases that cannot be the beginning of a
6161 nested-name-specifier. */
6162 token
= cp_lexer_peek_token (parser
->lexer
);
6164 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6165 the already parsed nested-name-specifier. */
6166 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6168 /* Grab the nested-name-specifier and continue the loop. */
6169 cp_parser_pre_parsed_nested_name_specifier (parser
);
6170 /* If we originally encountered this nested-name-specifier
6171 with IS_DECLARATION set to false, we will not have
6172 resolved TYPENAME_TYPEs, so we must do so here. */
6174 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6176 new_scope
= resolve_typename_type (parser
->scope
,
6177 /*only_current_p=*/false);
6178 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6179 parser
->scope
= new_scope
;
6185 /* Spot cases that cannot be the beginning of a
6186 nested-name-specifier. On the second and subsequent times
6187 through the loop, we look for the `template' keyword. */
6188 if (success
&& token
->keyword
== RID_TEMPLATE
)
6190 /* A template-id can start a nested-name-specifier. */
6191 else if (token
->type
== CPP_TEMPLATE_ID
)
6193 /* DR 743: decltype can be used in a nested-name-specifier. */
6194 else if (token_is_decltype (token
))
6198 /* If the next token is not an identifier, then it is
6199 definitely not a type-name or namespace-name. */
6200 if (token
->type
!= CPP_NAME
)
6202 /* If the following token is neither a `<' (to begin a
6203 template-id), nor a `::', then we are not looking at a
6204 nested-name-specifier. */
6205 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6207 if (token
->type
== CPP_COLON
6208 && parser
->colon_corrects_to_scope_p
6209 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6211 gcc_rich_location
richloc (token
->location
);
6212 richloc
.add_fixit_replace ("::");
6214 "found %<:%> in nested-name-specifier, "
6216 token
->type
= CPP_SCOPE
;
6219 if (token
->type
!= CPP_SCOPE
6220 && !cp_parser_nth_token_starts_template_argument_list_p
6225 /* The nested-name-specifier is optional, so we parse
6227 cp_parser_parse_tentatively (parser
);
6229 /* Look for the optional `template' keyword, if this isn't the
6230 first time through the loop. */
6232 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6234 /* Save the old scope since the name lookup we are about to do
6235 might destroy it. */
6236 old_scope
= parser
->scope
;
6237 saved_qualifying_scope
= parser
->qualifying_scope
;
6238 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6239 look up names in "X<T>::I" in order to determine that "Y" is
6240 a template. So, if we have a typename at this point, we make
6241 an effort to look through it. */
6243 && !typename_keyword_p
6245 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6246 parser
->scope
= resolve_typename_type (parser
->scope
,
6247 /*only_current_p=*/false);
6248 /* Parse the qualifying entity. */
6250 = cp_parser_qualifying_entity (parser
,
6256 /* Look for the `::' token. */
6257 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6259 /* If we found what we wanted, we keep going; otherwise, we're
6261 if (!cp_parser_parse_definitely (parser
))
6263 bool error_p
= false;
6265 /* Restore the OLD_SCOPE since it was valid before the
6266 failed attempt at finding the last
6267 class-or-namespace-name. */
6268 parser
->scope
= old_scope
;
6269 parser
->qualifying_scope
= saved_qualifying_scope
;
6271 /* If the next token is a decltype, and the one after that is a
6272 `::', then the decltype has failed to resolve to a class or
6273 enumeration type. Give this error even when parsing
6274 tentatively since it can't possibly be valid--and we're going
6275 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6276 won't get another chance.*/
6277 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6278 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6281 token
= cp_lexer_consume_token (parser
->lexer
);
6282 error_at (token
->location
, "decltype evaluates to %qT, "
6283 "which is not a class or enumeration type",
6284 token
->u
.tree_check_value
->value
);
6285 parser
->scope
= error_mark_node
;
6289 cp_lexer_consume_token (parser
->lexer
);
6292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6293 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6295 /* If we have a non-type template-id followed by ::, it can't
6296 possibly be valid. */
6297 token
= cp_lexer_peek_token (parser
->lexer
);
6298 tree tid
= token
->u
.tree_check_value
->value
;
6299 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6300 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6302 tree tmpl
= NULL_TREE
;
6303 if (is_overloaded_fn (tid
))
6305 tree fns
= get_fns (tid
);
6306 if (OVL_SINGLE_P (fns
))
6307 tmpl
= OVL_FIRST (fns
);
6308 error_at (token
->location
, "function template-id %qD "
6309 "in nested-name-specifier", tid
);
6313 /* Variable template. */
6314 tmpl
= TREE_OPERAND (tid
, 0);
6315 gcc_assert (variable_template_p (tmpl
));
6316 error_at (token
->location
, "variable template-id %qD "
6317 "in nested-name-specifier", tid
);
6320 inform (DECL_SOURCE_LOCATION (tmpl
),
6321 "%qD declared here", tmpl
);
6323 parser
->scope
= error_mark_node
;
6327 cp_lexer_consume_token (parser
->lexer
);
6328 cp_lexer_consume_token (parser
->lexer
);
6332 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6334 /* If the next token is an identifier, and the one after
6335 that is a `::', then any valid interpretation would have
6336 found a class-or-namespace-name. */
6337 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6338 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6340 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6343 token
= cp_lexer_consume_token (parser
->lexer
);
6346 if (!token
->error_reported
)
6349 tree ambiguous_decls
;
6351 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6353 /*is_template=*/false,
6354 /*is_namespace=*/false,
6355 /*check_dependency=*/true,
6358 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6359 error_at (token
->location
,
6360 "%qD used without template arguments",
6362 else if (ambiguous_decls
)
6364 // cp_parser_lookup_name has the same diagnostic,
6365 // thus make sure to emit it at most once.
6366 if (cp_parser_uncommitted_to_tentative_parse_p
6369 error_at (token
->location
,
6370 "reference to %qD is ambiguous",
6372 print_candidates (ambiguous_decls
);
6374 decl
= error_mark_node
;
6378 if (cxx_dialect
!= cxx98
)
6379 cp_parser_name_lookup_error
6380 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6383 cp_parser_name_lookup_error
6384 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6388 parser
->scope
= error_mark_node
;
6390 /* Treat this as a successful nested-name-specifier
6395 If the name found is not a class-name (clause
6396 _class_) or namespace-name (_namespace.def_), the
6397 program is ill-formed. */
6400 cp_lexer_consume_token (parser
->lexer
);
6404 /* We've found one valid nested-name-specifier. */
6406 /* Name lookup always gives us a DECL. */
6407 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6408 new_scope
= TREE_TYPE (new_scope
);
6409 /* Uses of "template" must be followed by actual templates. */
6410 if (template_keyword_p
6411 && !(CLASS_TYPE_P (new_scope
)
6412 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6413 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6414 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6415 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6416 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6417 == TEMPLATE_ID_EXPR
)))
6418 permerror (input_location
, TYPE_P (new_scope
)
6419 ? G_("%qT is not a template")
6420 : G_("%qD is not a template"),
6422 /* If it is a class scope, try to complete it; we are about to
6423 be looking up names inside the class. */
6424 if (TYPE_P (new_scope
)
6425 /* Since checking types for dependency can be expensive,
6426 avoid doing it if the type is already complete. */
6427 && !COMPLETE_TYPE_P (new_scope
)
6428 /* Do not try to complete dependent types. */
6429 && !dependent_type_p (new_scope
))
6431 new_scope
= complete_type (new_scope
);
6432 /* If it is a typedef to current class, use the current
6433 class instead, as the typedef won't have any names inside
6435 if (!COMPLETE_TYPE_P (new_scope
)
6436 && currently_open_class (new_scope
))
6437 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6439 /* Make sure we look in the right scope the next time through
6441 parser
->scope
= new_scope
;
6444 /* If parsing tentatively, replace the sequence of tokens that makes
6445 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6446 token. That way, should we re-parse the token stream, we will
6447 not have to repeat the effort required to do the parse, nor will
6448 we issue duplicate error messages. */
6449 if (success
&& start
)
6453 token
= cp_lexer_token_at (parser
->lexer
, start
);
6454 /* Reset the contents of the START token. */
6455 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6456 /* Retrieve any deferred checks. Do not pop this access checks yet
6457 so the memory will not be reclaimed during token replacing below. */
6458 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6459 token
->u
.tree_check_value
->value
= parser
->scope
;
6460 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6461 token
->u
.tree_check_value
->qualifying_scope
=
6462 parser
->qualifying_scope
;
6463 token
->keyword
= RID_MAX
;
6465 /* Purge all subsequent tokens. */
6466 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6470 pop_to_parent_deferring_access_checks ();
6472 return success
? parser
->scope
: NULL_TREE
;
6475 /* Parse a nested-name-specifier. See
6476 cp_parser_nested_name_specifier_opt for details. This function
6477 behaves identically, except that it will an issue an error if no
6478 nested-name-specifier is present. */
6481 cp_parser_nested_name_specifier (cp_parser
*parser
,
6482 bool typename_keyword_p
,
6483 bool check_dependency_p
,
6485 bool is_declaration
)
6489 /* Look for the nested-name-specifier. */
6490 scope
= cp_parser_nested_name_specifier_opt (parser
,
6495 /* If it was not present, issue an error message. */
6498 cp_parser_error (parser
, "expected nested-name-specifier");
6499 parser
->scope
= NULL_TREE
;
6505 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6506 this is either a class-name or a namespace-name (which corresponds
6507 to the class-or-namespace-name production in the grammar). For
6508 C++0x, it can also be a type-name that refers to an enumeration
6509 type or a simple-template-id.
6511 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6512 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6513 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6514 TYPE_P is TRUE iff the next name should be taken as a class-name,
6515 even the same name is declared to be another entity in the same
6518 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6519 specified by the class-or-namespace-name. If neither is found the
6520 ERROR_MARK_NODE is returned. */
6523 cp_parser_qualifying_entity (cp_parser
*parser
,
6524 bool typename_keyword_p
,
6525 bool template_keyword_p
,
6526 bool check_dependency_p
,
6528 bool is_declaration
)
6531 tree saved_qualifying_scope
;
6532 tree saved_object_scope
;
6535 bool successful_parse_p
;
6537 /* DR 743: decltype can appear in a nested-name-specifier. */
6538 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6540 scope
= cp_parser_decltype (parser
);
6541 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6542 && !MAYBE_CLASS_TYPE_P (scope
))
6544 cp_parser_simulate_error (parser
);
6545 return error_mark_node
;
6547 if (TYPE_NAME (scope
))
6548 scope
= TYPE_NAME (scope
);
6552 /* Before we try to parse the class-name, we must save away the
6553 current PARSER->SCOPE since cp_parser_class_name will destroy
6555 saved_scope
= parser
->scope
;
6556 saved_qualifying_scope
= parser
->qualifying_scope
;
6557 saved_object_scope
= parser
->object_scope
;
6558 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6559 there is no need to look for a namespace-name. */
6560 only_class_p
= template_keyword_p
6561 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6563 cp_parser_parse_tentatively (parser
);
6564 scope
= cp_parser_class_name (parser
,
6567 type_p
? class_type
: none_type
,
6569 /*class_head_p=*/false,
6571 /*enum_ok=*/cxx_dialect
> cxx98
);
6572 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6573 /* If that didn't work, try for a namespace-name. */
6574 if (!only_class_p
&& !successful_parse_p
)
6576 /* Restore the saved scope. */
6577 parser
->scope
= saved_scope
;
6578 parser
->qualifying_scope
= saved_qualifying_scope
;
6579 parser
->object_scope
= saved_object_scope
;
6580 /* If we are not looking at an identifier followed by the scope
6581 resolution operator, then this is not part of a
6582 nested-name-specifier. (Note that this function is only used
6583 to parse the components of a nested-name-specifier.) */
6584 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6585 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6586 return error_mark_node
;
6587 scope
= cp_parser_namespace_name (parser
);
6593 /* Return true if we are looking at a compound-literal, false otherwise. */
6596 cp_parser_compound_literal_p (cp_parser
*parser
)
6598 cp_lexer_save_tokens (parser
->lexer
);
6600 /* Skip tokens until the next token is a closing parenthesis.
6601 If we find the closing `)', and the next token is a `{', then
6602 we are looking at a compound-literal. */
6603 bool compound_literal_p
6604 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6605 /*consume_paren=*/true)
6606 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6608 /* Roll back the tokens we skipped. */
6609 cp_lexer_rollback_tokens (parser
->lexer
);
6611 return compound_literal_p
;
6614 /* Return true if EXPR is the integer constant zero or a complex constant
6615 of zero, without any folding, but ignoring location wrappers. */
6618 literal_integer_zerop (const_tree expr
)
6620 STRIP_ANY_LOCATION_WRAPPER (expr
);
6621 return integer_zerop (expr
);
6624 /* Parse a postfix-expression.
6628 postfix-expression [ expression ]
6629 postfix-expression ( expression-list [opt] )
6630 simple-type-specifier ( expression-list [opt] )
6631 typename :: [opt] nested-name-specifier identifier
6632 ( expression-list [opt] )
6633 typename :: [opt] nested-name-specifier template [opt] template-id
6634 ( expression-list [opt] )
6635 postfix-expression . template [opt] id-expression
6636 postfix-expression -> template [opt] id-expression
6637 postfix-expression . pseudo-destructor-name
6638 postfix-expression -> pseudo-destructor-name
6639 postfix-expression ++
6640 postfix-expression --
6641 dynamic_cast < type-id > ( expression )
6642 static_cast < type-id > ( expression )
6643 reinterpret_cast < type-id > ( expression )
6644 const_cast < type-id > ( expression )
6645 typeid ( expression )
6651 ( type-id ) { initializer-list , [opt] }
6653 This extension is a GNU version of the C99 compound-literal
6654 construct. (The C99 grammar uses `type-name' instead of `type-id',
6655 but they are essentially the same concept.)
6657 If ADDRESS_P is true, the postfix expression is the operand of the
6658 `&' operator. CAST_P is true if this expression is the target of a
6661 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6662 class member access expressions [expr.ref].
6664 Returns a representation of the expression. */
6667 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6668 bool member_access_only_p
, bool decltype_p
,
6669 cp_id_kind
* pidk_return
)
6674 cp_id_kind idk
= CP_ID_KIND_NONE
;
6675 cp_expr postfix_expression
= NULL_TREE
;
6676 bool is_member_access
= false;
6678 /* Peek at the next token. */
6679 token
= cp_lexer_peek_token (parser
->lexer
);
6680 loc
= token
->location
;
6681 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6683 /* Some of the productions are determined by keywords. */
6684 keyword
= token
->keyword
;
6694 const char *saved_message
;
6695 bool saved_in_type_id_in_expr_p
;
6697 /* All of these can be handled in the same way from the point
6698 of view of parsing. Begin by consuming the token
6699 identifying the cast. */
6700 cp_lexer_consume_token (parser
->lexer
);
6702 /* New types cannot be defined in the cast. */
6703 saved_message
= parser
->type_definition_forbidden_message
;
6704 parser
->type_definition_forbidden_message
6705 = G_("types may not be defined in casts");
6707 /* Look for the opening `<'. */
6708 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6709 /* Parse the type to which we are casting. */
6710 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6711 parser
->in_type_id_in_expr_p
= true;
6712 type
= cp_parser_type_id (parser
);
6713 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6714 /* Look for the closing `>'. */
6715 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6716 /* Restore the old message. */
6717 parser
->type_definition_forbidden_message
= saved_message
;
6719 bool saved_greater_than_is_operator_p
6720 = parser
->greater_than_is_operator_p
;
6721 parser
->greater_than_is_operator_p
= true;
6723 /* And the expression which is being cast. */
6724 matching_parens parens
;
6725 parens
.require_open (parser
);
6726 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6727 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6729 location_t end_loc
= close_paren
?
6730 close_paren
->location
: UNKNOWN_LOCATION
;
6732 parser
->greater_than_is_operator_p
6733 = saved_greater_than_is_operator_p
;
6735 /* Only type conversions to integral or enumeration types
6736 can be used in constant-expressions. */
6737 if (!cast_valid_in_integral_constant_expression_p (type
)
6738 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6740 postfix_expression
= error_mark_node
;
6748 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6752 = build_static_cast (type
, expression
, tf_warning_or_error
);
6756 = build_reinterpret_cast (type
, expression
,
6757 tf_warning_or_error
);
6761 = build_const_cast (type
, expression
, tf_warning_or_error
);
6767 /* Construct a location e.g. :
6768 reinterpret_cast <int *> (expr)
6769 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6770 ranging from the start of the "*_cast" token to the final closing
6771 paren, with the caret at the start. */
6772 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6773 postfix_expression
.set_location (cp_cast_loc
);
6780 const char *saved_message
;
6781 bool saved_in_type_id_in_expr_p
;
6783 /* Consume the `typeid' token. */
6784 cp_lexer_consume_token (parser
->lexer
);
6785 /* Look for the `(' token. */
6786 matching_parens parens
;
6787 parens
.require_open (parser
);
6788 /* Types cannot be defined in a `typeid' expression. */
6789 saved_message
= parser
->type_definition_forbidden_message
;
6790 parser
->type_definition_forbidden_message
6791 = G_("types may not be defined in a %<typeid%> expression");
6792 /* We can't be sure yet whether we're looking at a type-id or an
6794 cp_parser_parse_tentatively (parser
);
6795 /* Try a type-id first. */
6796 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6797 parser
->in_type_id_in_expr_p
= true;
6798 type
= cp_parser_type_id (parser
);
6799 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6800 /* Look for the `)' token. Otherwise, we can't be sure that
6801 we're not looking at an expression: consider `typeid (int
6802 (3))', for example. */
6803 cp_token
*close_paren
= parens
.require_close (parser
);
6804 /* If all went well, simply lookup the type-id. */
6805 if (cp_parser_parse_definitely (parser
))
6806 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6807 /* Otherwise, fall back to the expression variant. */
6812 /* Look for an expression. */
6813 expression
= cp_parser_expression (parser
, & idk
);
6814 /* Compute its typeid. */
6815 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6816 /* Look for the `)' token. */
6817 close_paren
= parens
.require_close (parser
);
6819 /* Restore the saved message. */
6820 parser
->type_definition_forbidden_message
= saved_message
;
6821 /* `typeid' may not appear in an integral constant expression. */
6822 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6823 postfix_expression
= error_mark_node
;
6825 /* Construct a location e.g. :
6828 ranging from the start of the "typeid" token to the final closing
6829 paren, with the caret at the start. */
6832 location_t typeid_loc
6833 = make_location (start_loc
, start_loc
, close_paren
->location
);
6834 postfix_expression
.set_location (typeid_loc
);
6835 postfix_expression
.maybe_add_location_wrapper ();
6843 /* The syntax permitted here is the same permitted for an
6844 elaborated-type-specifier. */
6845 ++parser
->prevent_constrained_type_specifiers
;
6846 type
= cp_parser_elaborated_type_specifier (parser
,
6847 /*is_friend=*/false,
6848 /*is_declaration=*/false);
6849 --parser
->prevent_constrained_type_specifiers
;
6850 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6855 case RID_BUILTIN_SHUFFLE
:
6856 case RID_BUILTIN_LAUNDER
:
6858 vec
<tree
, va_gc
> *vec
;
6862 cp_lexer_consume_token (parser
->lexer
);
6863 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6864 /*cast_p=*/false, /*allow_expansion_p=*/true,
6865 /*non_constant_p=*/NULL
);
6868 postfix_expression
= error_mark_node
;
6872 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6878 if (vec
->length () == 1)
6880 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6883 error_at (loc
, "wrong number of arguments to "
6884 "%<__builtin_addressof%>");
6885 postfix_expression
= error_mark_node
;
6889 case RID_BUILTIN_LAUNDER
:
6890 if (vec
->length () == 1)
6891 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6892 tf_warning_or_error
);
6895 error_at (loc
, "wrong number of arguments to "
6896 "%<__builtin_launder%>");
6897 postfix_expression
= error_mark_node
;
6901 case RID_BUILTIN_SHUFFLE
:
6902 if (vec
->length () == 2)
6904 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6905 (*vec
)[1], tf_warning_or_error
);
6906 else if (vec
->length () == 3)
6908 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6909 (*vec
)[2], tf_warning_or_error
);
6912 error_at (loc
, "wrong number of arguments to "
6913 "%<__builtin_shuffle%>");
6914 postfix_expression
= error_mark_node
;
6928 /* If the next thing is a simple-type-specifier, we may be
6929 looking at a functional cast. We could also be looking at
6930 an id-expression. So, we try the functional cast, and if
6931 that doesn't work we fall back to the primary-expression. */
6932 cp_parser_parse_tentatively (parser
);
6933 /* Look for the simple-type-specifier. */
6934 ++parser
->prevent_constrained_type_specifiers
;
6935 type
= cp_parser_simple_type_specifier (parser
,
6936 /*decl_specs=*/NULL
,
6937 CP_PARSER_FLAGS_NONE
);
6938 --parser
->prevent_constrained_type_specifiers
;
6939 /* Parse the cast itself. */
6940 if (!cp_parser_error_occurred (parser
))
6942 = cp_parser_functional_cast (parser
, type
);
6943 /* If that worked, we're done. */
6944 if (cp_parser_parse_definitely (parser
))
6947 /* If the functional-cast didn't work out, try a
6948 compound-literal. */
6949 if (cp_parser_allow_gnu_extensions_p (parser
)
6950 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6952 cp_expr initializer
= NULL_TREE
;
6954 cp_parser_parse_tentatively (parser
);
6956 matching_parens parens
;
6957 parens
.consume_open (parser
);
6959 /* Avoid calling cp_parser_type_id pointlessly, see comment
6960 in cp_parser_cast_expression about c++/29234. */
6961 if (!cp_parser_compound_literal_p (parser
))
6962 cp_parser_simulate_error (parser
);
6965 /* Parse the type. */
6966 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6967 parser
->in_type_id_in_expr_p
= true;
6968 type
= cp_parser_type_id (parser
);
6969 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6970 parens
.require_close (parser
);
6973 /* If things aren't going well, there's no need to
6975 if (!cp_parser_error_occurred (parser
))
6977 bool non_constant_p
;
6978 /* Parse the brace-enclosed initializer list. */
6979 initializer
= cp_parser_braced_list (parser
,
6982 /* If that worked, we're definitely looking at a
6983 compound-literal expression. */
6984 if (cp_parser_parse_definitely (parser
))
6986 /* Warn the user that a compound literal is not
6987 allowed in standard C++. */
6988 pedwarn (input_location
, OPT_Wpedantic
,
6989 "ISO C++ forbids compound-literals");
6990 /* For simplicity, we disallow compound literals in
6991 constant-expressions. We could
6992 allow compound literals of integer type, whose
6993 initializer was a constant, in constant
6994 expressions. Permitting that usage, as a further
6995 extension, would not change the meaning of any
6996 currently accepted programs. (Of course, as
6997 compound literals are not part of ISO C++, the
6998 standard has nothing to say.) */
6999 if (cp_parser_non_integral_constant_expression (parser
,
7002 postfix_expression
= error_mark_node
;
7005 /* Form the representation of the compound-literal. */
7007 = finish_compound_literal (type
, initializer
,
7008 tf_warning_or_error
, fcl_c99
);
7009 postfix_expression
.set_location (initializer
.get_location ());
7014 /* It must be a primary-expression. */
7016 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7017 /*template_arg_p=*/false,
7024 /* Note that we don't need to worry about calling build_cplus_new on a
7025 class-valued CALL_EXPR in decltype when it isn't the end of the
7026 postfix-expression; unary_complex_lvalue will take care of that for
7029 /* Keep looping until the postfix-expression is complete. */
7032 if (idk
== CP_ID_KIND_UNQUALIFIED
7033 && identifier_p (postfix_expression
)
7034 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7035 /* It is not a Koenig lookup function call. */
7037 = unqualified_name_lookup_error (postfix_expression
);
7039 /* Peek at the next token. */
7040 token
= cp_lexer_peek_token (parser
->lexer
);
7042 switch (token
->type
)
7044 case CPP_OPEN_SQUARE
:
7045 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7047 cp_parser_error (parser
,
7048 "two consecutive %<[%> shall "
7049 "only introduce an attribute");
7050 return error_mark_node
;
7053 = cp_parser_postfix_open_square_expression (parser
,
7057 postfix_expression
.set_range (start_loc
,
7058 postfix_expression
.get_location ());
7060 idk
= CP_ID_KIND_NONE
;
7061 is_member_access
= false;
7064 case CPP_OPEN_PAREN
:
7065 /* postfix-expression ( expression-list [opt] ) */
7068 bool is_builtin_constant_p
;
7069 bool saved_integral_constant_expression_p
= false;
7070 bool saved_non_integral_constant_expression_p
= false;
7071 tsubst_flags_t complain
= complain_flags (decltype_p
);
7072 vec
<tree
, va_gc
> *args
;
7073 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7075 is_member_access
= false;
7077 is_builtin_constant_p
7078 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7079 if (is_builtin_constant_p
)
7081 /* The whole point of __builtin_constant_p is to allow
7082 non-constant expressions to appear as arguments. */
7083 saved_integral_constant_expression_p
7084 = parser
->integral_constant_expression_p
;
7085 saved_non_integral_constant_expression_p
7086 = parser
->non_integral_constant_expression_p
;
7087 parser
->integral_constant_expression_p
= false;
7089 args
= (cp_parser_parenthesized_expression_list
7091 /*cast_p=*/false, /*allow_expansion_p=*/true,
7092 /*non_constant_p=*/NULL
,
7093 /*close_paren_loc=*/&close_paren_loc
,
7094 /*wrap_locations_p=*/true));
7095 if (is_builtin_constant_p
)
7097 parser
->integral_constant_expression_p
7098 = saved_integral_constant_expression_p
;
7099 parser
->non_integral_constant_expression_p
7100 = saved_non_integral_constant_expression_p
;
7105 postfix_expression
= error_mark_node
;
7109 /* Function calls are not permitted in
7110 constant-expressions. */
7111 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7112 && cp_parser_non_integral_constant_expression (parser
,
7115 postfix_expression
= error_mark_node
;
7116 release_tree_vector (args
);
7121 if (idk
== CP_ID_KIND_UNQUALIFIED
7122 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7124 if (identifier_p (postfix_expression
))
7126 if (!args
->is_empty ())
7129 if (!any_type_dependent_arguments_p (args
))
7131 = perform_koenig_lookup (postfix_expression
, args
,
7136 = unqualified_fn_lookup_error (postfix_expression
);
7138 /* We do not perform argument-dependent lookup if
7139 normal lookup finds a non-function, in accordance
7140 with the expected resolution of DR 218. */
7141 else if (!args
->is_empty ()
7142 && is_overloaded_fn (postfix_expression
))
7144 tree fn
= get_first_fn (postfix_expression
);
7145 fn
= STRIP_TEMPLATE (fn
);
7147 /* Do not do argument dependent lookup if regular
7148 lookup finds a member function or a block-scope
7149 function declaration. [basic.lookup.argdep]/3 */
7150 if (!DECL_FUNCTION_MEMBER_P (fn
)
7151 && !DECL_LOCAL_FUNCTION_P (fn
))
7154 if (!any_type_dependent_arguments_p (args
))
7156 = perform_koenig_lookup (postfix_expression
, args
,
7162 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
7163 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
7164 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
7165 && vec_safe_length (args
) == 3)
7167 tree arg0
= (*args
)[0];
7168 tree arg1
= (*args
)[1];
7169 tree arg2
= (*args
)[2];
7170 int literal_mask
= ((literal_integer_zerop (arg1
) << 1)
7171 | (literal_integer_zerop (arg2
) << 2));
7172 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
7175 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7177 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7178 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7180 if (processing_template_decl
7181 && (type_dependent_object_expression_p (instance
)
7182 || (!BASELINK_P (fn
)
7183 && TREE_CODE (fn
) != FIELD_DECL
)
7184 || type_dependent_expression_p (fn
)
7185 || any_type_dependent_arguments_p (args
)))
7187 maybe_generic_this_capture (instance
, fn
);
7189 = build_min_nt_call_vec (postfix_expression
, args
);
7190 release_tree_vector (args
);
7194 if (BASELINK_P (fn
))
7197 = (build_new_method_call
7198 (instance
, fn
, &args
, NULL_TREE
,
7199 (idk
== CP_ID_KIND_QUALIFIED
7200 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7207 = finish_call_expr (postfix_expression
, &args
,
7208 /*disallow_virtual=*/false,
7212 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7213 || TREE_CODE (postfix_expression
) == MEMBER_REF
7214 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7215 postfix_expression
= (build_offset_ref_call_from_tree
7216 (postfix_expression
, &args
,
7218 else if (idk
== CP_ID_KIND_QUALIFIED
)
7219 /* A call to a static class member, or a namespace-scope
7222 = finish_call_expr (postfix_expression
, &args
,
7223 /*disallow_virtual=*/true,
7227 /* All other function calls. */
7229 = finish_call_expr (postfix_expression
, &args
,
7230 /*disallow_virtual=*/false,
7234 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7236 location_t combined_loc
= make_location (token
->location
,
7239 postfix_expression
.set_location (combined_loc
);
7242 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7243 idk
= CP_ID_KIND_NONE
;
7245 release_tree_vector (args
);
7251 /* postfix-expression . template [opt] id-expression
7252 postfix-expression . pseudo-destructor-name
7253 postfix-expression -> template [opt] id-expression
7254 postfix-expression -> pseudo-destructor-name */
7256 /* Consume the `.' or `->' operator. */
7257 cp_lexer_consume_token (parser
->lexer
);
7260 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7264 is_member_access
= true;
7268 /* postfix-expression ++ */
7269 /* Consume the `++' token. */
7270 cp_lexer_consume_token (parser
->lexer
);
7271 /* Generate a representation for the complete expression. */
7273 = finish_increment_expr (postfix_expression
,
7274 POSTINCREMENT_EXPR
);
7275 /* Increments may not appear in constant-expressions. */
7276 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7277 postfix_expression
= error_mark_node
;
7278 idk
= CP_ID_KIND_NONE
;
7279 is_member_access
= false;
7282 case CPP_MINUS_MINUS
:
7283 /* postfix-expression -- */
7284 /* Consume the `--' token. */
7285 cp_lexer_consume_token (parser
->lexer
);
7286 /* Generate a representation for the complete expression. */
7288 = finish_increment_expr (postfix_expression
,
7289 POSTDECREMENT_EXPR
);
7290 /* Decrements may not appear in constant-expressions. */
7291 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7292 postfix_expression
= error_mark_node
;
7293 idk
= CP_ID_KIND_NONE
;
7294 is_member_access
= false;
7298 if (pidk_return
!= NULL
)
7299 * pidk_return
= idk
;
7300 if (member_access_only_p
)
7301 return is_member_access
7302 ? postfix_expression
7303 : cp_expr (error_mark_node
);
7305 return postfix_expression
;
7309 /* We should never get here. */
7311 return error_mark_node
;
7314 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7315 by cp_parser_builtin_offsetof. We're looking for
7317 postfix-expression [ expression ]
7318 postfix-expression [ braced-init-list ] (C++11)
7320 FOR_OFFSETOF is set if we're being called in that context, which
7321 changes how we deal with integer constant expressions. */
7324 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7325 tree postfix_expression
,
7329 tree index
= NULL_TREE
;
7330 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7331 bool saved_greater_than_is_operator_p
;
7333 /* Consume the `[' token. */
7334 cp_lexer_consume_token (parser
->lexer
);
7336 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7337 parser
->greater_than_is_operator_p
= true;
7339 /* Parse the index expression. */
7340 /* ??? For offsetof, there is a question of what to allow here. If
7341 offsetof is not being used in an integral constant expression context,
7342 then we *could* get the right answer by computing the value at runtime.
7343 If we are in an integral constant expression context, then we might
7344 could accept any constant expression; hard to say without analysis.
7345 Rather than open the barn door too wide right away, allow only integer
7346 constant expressions here. */
7348 index
= cp_parser_constant_expression (parser
);
7351 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7353 bool expr_nonconst_p
;
7354 cp_lexer_set_source_position (parser
->lexer
);
7355 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7356 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7359 index
= cp_parser_expression (parser
);
7362 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7364 /* Look for the closing `]'. */
7365 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7367 /* Build the ARRAY_REF. */
7368 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7371 /* When not doing offsetof, array references are not permitted in
7372 constant-expressions. */
7374 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7375 postfix_expression
= error_mark_node
;
7377 return postfix_expression
;
7380 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7381 dereference of incomplete type, returns true if error_mark_node should
7382 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7383 and *DEPENDENT_P. */
7386 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7389 /* In a template, be permissive by treating an object expression
7390 of incomplete type as dependent (after a pedwarn). */
7391 diagnostic_t kind
= (processing_template_decl
7392 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7394 switch (TREE_CODE (*postfix_expression
))
7397 case REINTERPRET_CAST_EXPR
:
7398 case CONST_CAST_EXPR
:
7399 case STATIC_CAST_EXPR
:
7400 case DYNAMIC_CAST_EXPR
:
7401 case IMPLICIT_CONV_EXPR
:
7402 case VIEW_CONVERT_EXPR
:
7403 case NON_LVALUE_EXPR
:
7407 /* Don't emit any diagnostic for OVERLOADs. */
7411 /* Avoid clobbering e.g. DECLs. */
7412 if (!EXPR_P (*postfix_expression
))
7417 if (kind
== DK_IGNORED
)
7420 location_t exploc
= location_of (*postfix_expression
);
7421 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7422 if (!MAYBE_CLASS_TYPE_P (*scope
))
7424 if (kind
== DK_ERROR
)
7425 *scope
= *postfix_expression
= error_mark_node
;
7426 else if (processing_template_decl
)
7428 *dependent_p
= true;
7429 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7434 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7435 by cp_parser_builtin_offsetof. We're looking for
7437 postfix-expression . template [opt] id-expression
7438 postfix-expression . pseudo-destructor-name
7439 postfix-expression -> template [opt] id-expression
7440 postfix-expression -> pseudo-destructor-name
7442 FOR_OFFSETOF is set if we're being called in that context. That sorta
7443 limits what of the above we'll actually accept, but nevermind.
7444 TOKEN_TYPE is the "." or "->" token, which will already have been
7445 removed from the stream. */
7448 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7449 enum cpp_ttype token_type
,
7450 cp_expr postfix_expression
,
7451 bool for_offsetof
, cp_id_kind
*idk
,
7452 location_t location
)
7456 bool pseudo_destructor_p
;
7457 tree scope
= NULL_TREE
;
7458 location_t start_loc
= postfix_expression
.get_start ();
7460 /* If this is a `->' operator, dereference the pointer. */
7461 if (token_type
== CPP_DEREF
)
7462 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7463 tf_warning_or_error
);
7464 /* Check to see whether or not the expression is type-dependent and
7465 not the current instantiation. */
7466 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7467 /* The identifier following the `->' or `.' is not qualified. */
7468 parser
->scope
= NULL_TREE
;
7469 parser
->qualifying_scope
= NULL_TREE
;
7470 parser
->object_scope
= NULL_TREE
;
7471 *idk
= CP_ID_KIND_NONE
;
7473 /* Enter the scope corresponding to the type of the object
7474 given by the POSTFIX_EXPRESSION. */
7477 scope
= TREE_TYPE (postfix_expression
);
7478 /* According to the standard, no expression should ever have
7479 reference type. Unfortunately, we do not currently match
7480 the standard in this respect in that our internal representation
7481 of an expression may have reference type even when the standard
7482 says it does not. Therefore, we have to manually obtain the
7483 underlying type here. */
7484 scope
= non_reference (scope
);
7485 /* The type of the POSTFIX_EXPRESSION must be complete. */
7486 /* Unlike the object expression in other contexts, *this is not
7487 required to be of complete type for purposes of class member
7488 access (5.2.5) outside the member function body. */
7489 if (postfix_expression
!= current_class_ref
7490 && scope
!= error_mark_node
7491 && !(processing_template_decl
7492 && current_class_type
7493 && (same_type_ignoring_top_level_qualifiers_p
7494 (scope
, current_class_type
))))
7496 scope
= complete_type (scope
);
7497 if (!COMPLETE_TYPE_P (scope
)
7498 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7500 return error_mark_node
;
7505 /* Let the name lookup machinery know that we are processing a
7506 class member access expression. */
7507 parser
->context
->object_type
= scope
;
7508 /* If something went wrong, we want to be able to discern that case,
7509 as opposed to the case where there was no SCOPE due to the type
7510 of expression being dependent. */
7512 scope
= error_mark_node
;
7513 /* If the SCOPE was erroneous, make the various semantic analysis
7514 functions exit quickly -- and without issuing additional error
7516 if (scope
== error_mark_node
)
7517 postfix_expression
= error_mark_node
;
7522 /* Tell cp_parser_lookup_name that there was an object, even though it's
7524 parser
->context
->object_type
= unknown_type_node
;
7526 /* Assume this expression is not a pseudo-destructor access. */
7527 pseudo_destructor_p
= false;
7529 /* If the SCOPE is a scalar type, then, if this is a valid program,
7530 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7531 is type dependent, it can be pseudo-destructor-name or something else.
7532 Try to parse it as pseudo-destructor-name first. */
7533 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7538 cp_parser_parse_tentatively (parser
);
7539 /* Parse the pseudo-destructor-name. */
7541 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7544 && (cp_parser_error_occurred (parser
)
7545 || !SCALAR_TYPE_P (type
)))
7546 cp_parser_abort_tentative_parse (parser
);
7547 else if (cp_parser_parse_definitely (parser
))
7549 pseudo_destructor_p
= true;
7551 = finish_pseudo_destructor_expr (postfix_expression
,
7556 if (!pseudo_destructor_p
)
7558 /* If the SCOPE is not a scalar type, we are looking at an
7559 ordinary class member access expression, rather than a
7560 pseudo-destructor-name. */
7562 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7563 /* Parse the id-expression. */
7564 name
= (cp_parser_id_expression
7566 cp_parser_optional_template_keyword (parser
),
7567 /*check_dependency_p=*/true,
7569 /*declarator_p=*/false,
7570 /*optional_p=*/false));
7571 /* In general, build a SCOPE_REF if the member name is qualified.
7572 However, if the name was not dependent and has already been
7573 resolved; there is no need to build the SCOPE_REF. For example;
7575 struct X { void f(); };
7576 template <typename T> void f(T* t) { t->X::f(); }
7578 Even though "t" is dependent, "X::f" is not and has been resolved
7579 to a BASELINK; there is no need to include scope information. */
7581 /* But we do need to remember that there was an explicit scope for
7582 virtual function calls. */
7584 *idk
= CP_ID_KIND_QUALIFIED
;
7586 /* If the name is a template-id that names a type, we will get a
7587 TYPE_DECL here. That is invalid code. */
7588 if (TREE_CODE (name
) == TYPE_DECL
)
7590 error_at (token
->location
, "invalid use of %qD", name
);
7591 postfix_expression
= error_mark_node
;
7595 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7597 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7599 error_at (token
->location
, "%<%D::%D%> is not a class member",
7600 parser
->scope
, name
);
7601 postfix_expression
= error_mark_node
;
7604 name
= build_qualified_name (/*type=*/NULL_TREE
,
7608 parser
->scope
= NULL_TREE
;
7609 parser
->qualifying_scope
= NULL_TREE
;
7610 parser
->object_scope
= NULL_TREE
;
7612 if (parser
->scope
&& name
&& BASELINK_P (name
))
7613 adjust_result_of_qualified_name_lookup
7614 (name
, parser
->scope
, scope
);
7616 = finish_class_member_access_expr (postfix_expression
, name
,
7618 tf_warning_or_error
);
7619 /* Build a location e.g.:
7622 where the caret is at the deref token, ranging from
7623 the start of postfix_expression to the end of the access expr. */
7625 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7626 location_t combined_loc
7627 = make_location (input_location
, start_loc
, end_loc
);
7628 protected_set_expr_location (postfix_expression
, combined_loc
);
7632 /* We no longer need to look up names in the scope of the object on
7633 the left-hand side of the `.' or `->' operator. */
7634 parser
->context
->object_type
= NULL_TREE
;
7636 /* Outside of offsetof, these operators may not appear in
7637 constant-expressions. */
7639 && (cp_parser_non_integral_constant_expression
7640 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7641 postfix_expression
= error_mark_node
;
7643 return postfix_expression
;
7646 /* Parse a parenthesized expression-list.
7649 assignment-expression
7650 expression-list, assignment-expression
7655 identifier, expression-list
7657 CAST_P is true if this expression is the target of a cast.
7659 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7662 WRAP_LOCATIONS_P is true if expressions within this list for which
7663 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7664 their source locations.
7666 Returns a vector of trees. Each element is a representation of an
7667 assignment-expression. NULL is returned if the ( and or ) are
7668 missing. An empty, but allocated, vector is returned on no
7669 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7670 if we are parsing an attribute list for an attribute that wants a
7671 plain identifier argument, normal_attr for an attribute that wants
7672 an expression, or non_attr if we aren't parsing an attribute list. If
7673 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7674 not all of the expressions in the list were constant.
7675 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7676 will be written to with the location of the closing parenthesis. If
7677 an error occurs, it may or may not be written to. */
7679 static vec
<tree
, va_gc
> *
7680 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7681 int is_attribute_list
,
7683 bool allow_expansion_p
,
7684 bool *non_constant_p
,
7685 location_t
*close_paren_loc
,
7686 bool wrap_locations_p
)
7688 vec
<tree
, va_gc
> *expression_list
;
7689 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7690 tree identifier
= NULL_TREE
;
7691 bool saved_greater_than_is_operator_p
;
7693 /* Assume all the expressions will be constant. */
7695 *non_constant_p
= false;
7697 matching_parens parens
;
7698 if (!parens
.require_open (parser
))
7701 expression_list
= make_tree_vector ();
7703 /* Within a parenthesized expression, a `>' token is always
7704 the greater-than operator. */
7705 saved_greater_than_is_operator_p
7706 = parser
->greater_than_is_operator_p
;
7707 parser
->greater_than_is_operator_p
= true;
7709 cp_expr
expr (NULL_TREE
);
7711 /* Consume expressions until there are no more. */
7712 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7715 /* At the beginning of attribute lists, check to see if the
7716 next token is an identifier. */
7717 if (is_attribute_list
== id_attr
7718 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7722 /* Consume the identifier. */
7723 token
= cp_lexer_consume_token (parser
->lexer
);
7724 /* Save the identifier. */
7725 identifier
= token
->u
.value
;
7729 bool expr_non_constant_p
;
7731 /* Parse the next assignment-expression. */
7732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7734 /* A braced-init-list. */
7735 cp_lexer_set_source_position (parser
->lexer
);
7736 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7737 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7738 if (non_constant_p
&& expr_non_constant_p
)
7739 *non_constant_p
= true;
7741 else if (non_constant_p
)
7743 expr
= (cp_parser_constant_expression
7744 (parser
, /*allow_non_constant_p=*/true,
7745 &expr_non_constant_p
));
7746 if (expr_non_constant_p
)
7747 *non_constant_p
= true;
7750 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7754 expr
= instantiate_non_dependent_expr (expr
);
7756 /* If we have an ellipsis, then this is an expression
7758 if (allow_expansion_p
7759 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7761 /* Consume the `...'. */
7762 cp_lexer_consume_token (parser
->lexer
);
7764 /* Build the argument pack. */
7765 expr
= make_pack_expansion (expr
);
7768 if (wrap_locations_p
)
7769 expr
.maybe_add_location_wrapper ();
7771 /* Add it to the list. We add error_mark_node
7772 expressions to the list, so that we can still tell if
7773 the correct form for a parenthesized expression-list
7774 is found. That gives better errors. */
7775 vec_safe_push (expression_list
, expr
.get_value ());
7777 if (expr
== error_mark_node
)
7781 /* After the first item, attribute lists look the same as
7782 expression lists. */
7783 is_attribute_list
= non_attr
;
7786 /* If the next token isn't a `,', then we are done. */
7787 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7790 /* Otherwise, consume the `,' and keep going. */
7791 cp_lexer_consume_token (parser
->lexer
);
7794 if (close_paren_loc
)
7795 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7797 if (!parens
.require_close (parser
))
7802 /* We try and resync to an unnested comma, as that will give the
7803 user better diagnostics. */
7804 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7805 /*recovering=*/true,
7807 /*consume_paren=*/true);
7812 parser
->greater_than_is_operator_p
7813 = saved_greater_than_is_operator_p
;
7818 parser
->greater_than_is_operator_p
7819 = saved_greater_than_is_operator_p
;
7822 vec_safe_insert (expression_list
, 0, identifier
);
7824 return expression_list
;
7827 /* Parse a pseudo-destructor-name.
7829 pseudo-destructor-name:
7830 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7831 :: [opt] nested-name-specifier template template-id :: ~ type-name
7832 :: [opt] nested-name-specifier [opt] ~ type-name
7834 If either of the first two productions is used, sets *SCOPE to the
7835 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7836 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7837 or ERROR_MARK_NODE if the parse fails. */
7840 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7845 bool nested_name_specifier_p
;
7848 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7849 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7850 && !type_dependent_expression_p (object
))
7852 if (cxx_dialect
< cxx14
)
7853 pedwarn (input_location
, 0,
7854 "%<~auto%> only available with "
7855 "-std=c++14 or -std=gnu++14");
7856 cp_lexer_consume_token (parser
->lexer
);
7857 cp_lexer_consume_token (parser
->lexer
);
7859 *type
= TREE_TYPE (object
);
7863 /* Assume that things will not work out. */
7864 *type
= error_mark_node
;
7866 /* Look for the optional `::' operator. */
7867 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7868 /* Look for the optional nested-name-specifier. */
7869 nested_name_specifier_p
7870 = (cp_parser_nested_name_specifier_opt (parser
,
7871 /*typename_keyword_p=*/false,
7872 /*check_dependency_p=*/true,
7874 /*is_declaration=*/false)
7876 /* Now, if we saw a nested-name-specifier, we might be doing the
7877 second production. */
7878 if (nested_name_specifier_p
7879 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7881 /* Consume the `template' keyword. */
7882 cp_lexer_consume_token (parser
->lexer
);
7883 /* Parse the template-id. */
7884 cp_parser_template_id (parser
,
7885 /*template_keyword_p=*/true,
7886 /*check_dependency_p=*/false,
7888 /*is_declaration=*/true);
7889 /* Look for the `::' token. */
7890 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7892 /* If the next token is not a `~', then there might be some
7893 additional qualification. */
7894 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7896 /* At this point, we're looking for "type-name :: ~". The type-name
7897 must not be a class-name, since this is a pseudo-destructor. So,
7898 it must be either an enum-name, or a typedef-name -- both of which
7899 are just identifiers. So, we peek ahead to check that the "::"
7900 and "~" tokens are present; if they are not, then we can avoid
7901 calling type_name. */
7902 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7903 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7904 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7906 cp_parser_error (parser
, "non-scalar type");
7910 /* Look for the type-name. */
7911 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7912 if (*scope
== error_mark_node
)
7915 /* Look for the `::' token. */
7916 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7921 /* Look for the `~'. */
7922 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7924 /* Once we see the ~, this has to be a pseudo-destructor. */
7925 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7926 cp_parser_commit_to_topmost_tentative_parse (parser
);
7928 /* Look for the type-name again. We are not responsible for
7929 checking that it matches the first type-name. */
7930 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7933 /* Parse a unary-expression.
7939 unary-operator cast-expression
7940 sizeof unary-expression
7942 alignof ( type-id ) [C++0x]
7949 __extension__ cast-expression
7950 __alignof__ unary-expression
7951 __alignof__ ( type-id )
7952 alignof unary-expression [C++0x]
7953 __real__ cast-expression
7954 __imag__ cast-expression
7956 sizeof ( type-id ) { initializer-list , [opt] }
7957 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7958 __alignof__ ( type-id ) { initializer-list , [opt] }
7960 ADDRESS_P is true iff the unary-expression is appearing as the
7961 operand of the `&' operator. CAST_P is true if this expression is
7962 the target of a cast.
7964 Returns a representation of the expression. */
7967 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7968 bool address_p
, bool cast_p
, bool decltype_p
)
7971 enum tree_code unary_operator
;
7973 /* Peek at the next token. */
7974 token
= cp_lexer_peek_token (parser
->lexer
);
7975 /* Some keywords give away the kind of expression. */
7976 if (token
->type
== CPP_KEYWORD
)
7978 enum rid keyword
= token
->keyword
;
7987 location_t start_loc
= token
->location
;
7989 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7990 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
7992 /* Consume the token. */
7993 cp_lexer_consume_token (parser
->lexer
);
7994 /* Parse the operand. */
7995 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7997 if (TYPE_P (operand
))
7998 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8002 /* ISO C++ defines alignof only with types, not with
8003 expressions. So pedwarn if alignof is used with a non-
8004 type expression. However, __alignof__ is ok. */
8006 pedwarn (token
->location
, OPT_Wpedantic
,
8007 "ISO C++ does not allow %<alignof%> "
8010 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8012 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8013 SIZEOF_EXPR with the original operand. */
8014 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8016 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8018 if (!processing_template_decl
&& TYPE_P (operand
))
8020 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8021 build1 (NOP_EXPR
, operand
,
8023 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8026 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8027 TREE_SIDE_EFFECTS (ret
) = 0;
8028 TREE_READONLY (ret
) = 1;
8032 /* Construct a location e.g. :
8035 with start == caret at the start of the "alignof"/"sizeof"
8036 token, with the endpoint at the final closing paren. */
8037 location_t finish_loc
8038 = cp_lexer_previous_token (parser
->lexer
)->location
;
8039 location_t compound_loc
8040 = make_location (start_loc
, start_loc
, finish_loc
);
8042 cp_expr
ret_expr (ret
);
8043 ret_expr
.set_location (compound_loc
);
8044 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8049 return cp_parser_new_expression (parser
);
8052 return cp_parser_delete_expression (parser
);
8056 /* The saved value of the PEDANTIC flag. */
8060 /* Save away the PEDANTIC flag. */
8061 cp_parser_extension_opt (parser
, &saved_pedantic
);
8062 /* Parse the cast-expression. */
8063 expr
= cp_parser_simple_cast_expression (parser
);
8064 /* Restore the PEDANTIC flag. */
8065 pedantic
= saved_pedantic
;
8075 /* Consume the `__real__' or `__imag__' token. */
8076 cp_lexer_consume_token (parser
->lexer
);
8077 /* Parse the cast-expression. */
8078 expression
= cp_parser_simple_cast_expression (parser
);
8079 /* Create the complete representation. */
8080 return build_x_unary_op (token
->location
,
8081 (keyword
== RID_REALPART
8082 ? REALPART_EXPR
: IMAGPART_EXPR
),
8084 tf_warning_or_error
);
8088 case RID_TRANSACTION_ATOMIC
:
8089 case RID_TRANSACTION_RELAXED
:
8090 return cp_parser_transaction_expression (parser
, keyword
);
8095 const char *saved_message
;
8096 bool saved_integral_constant_expression_p
;
8097 bool saved_non_integral_constant_expression_p
;
8098 bool saved_greater_than_is_operator_p
;
8100 location_t start_loc
= token
->location
;
8102 cp_lexer_consume_token (parser
->lexer
);
8103 matching_parens parens
;
8104 parens
.require_open (parser
);
8106 saved_message
= parser
->type_definition_forbidden_message
;
8107 parser
->type_definition_forbidden_message
8108 = G_("types may not be defined in %<noexcept%> expressions");
8110 saved_integral_constant_expression_p
8111 = parser
->integral_constant_expression_p
;
8112 saved_non_integral_constant_expression_p
8113 = parser
->non_integral_constant_expression_p
;
8114 parser
->integral_constant_expression_p
= false;
8116 saved_greater_than_is_operator_p
8117 = parser
->greater_than_is_operator_p
;
8118 parser
->greater_than_is_operator_p
= true;
8120 ++cp_unevaluated_operand
;
8121 ++c_inhibit_evaluation_warnings
;
8122 ++cp_noexcept_operand
;
8123 expr
= cp_parser_expression (parser
);
8124 --cp_noexcept_operand
;
8125 --c_inhibit_evaluation_warnings
;
8126 --cp_unevaluated_operand
;
8128 parser
->greater_than_is_operator_p
8129 = saved_greater_than_is_operator_p
;
8131 parser
->integral_constant_expression_p
8132 = saved_integral_constant_expression_p
;
8133 parser
->non_integral_constant_expression_p
8134 = saved_non_integral_constant_expression_p
;
8136 parser
->type_definition_forbidden_message
= saved_message
;
8138 location_t finish_loc
8139 = cp_lexer_peek_token (parser
->lexer
)->location
;
8140 parens
.require_close (parser
);
8142 /* Construct a location of the form:
8145 with start == caret, finishing at the close-paren. */
8146 location_t noexcept_loc
8147 = make_location (start_loc
, start_loc
, finish_loc
);
8149 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8158 /* Look for the `:: new' and `:: delete', which also signal the
8159 beginning of a new-expression, or delete-expression,
8160 respectively. If the next token is `::', then it might be one of
8162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8166 /* See if the token after the `::' is one of the keywords in
8167 which we're interested. */
8168 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8169 /* If it's `new', we have a new-expression. */
8170 if (keyword
== RID_NEW
)
8171 return cp_parser_new_expression (parser
);
8172 /* Similarly, for `delete'. */
8173 else if (keyword
== RID_DELETE
)
8174 return cp_parser_delete_expression (parser
);
8177 /* Look for a unary operator. */
8178 unary_operator
= cp_parser_unary_operator (token
);
8179 /* The `++' and `--' operators can be handled similarly, even though
8180 they are not technically unary-operators in the grammar. */
8181 if (unary_operator
== ERROR_MARK
)
8183 if (token
->type
== CPP_PLUS_PLUS
)
8184 unary_operator
= PREINCREMENT_EXPR
;
8185 else if (token
->type
== CPP_MINUS_MINUS
)
8186 unary_operator
= PREDECREMENT_EXPR
;
8187 /* Handle the GNU address-of-label extension. */
8188 else if (cp_parser_allow_gnu_extensions_p (parser
)
8189 && token
->type
== CPP_AND_AND
)
8193 location_t start_loc
= token
->location
;
8195 /* Consume the '&&' token. */
8196 cp_lexer_consume_token (parser
->lexer
);
8197 /* Look for the identifier. */
8198 location_t finish_loc
8199 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8200 identifier
= cp_parser_identifier (parser
);
8201 /* Construct a location of the form:
8204 with caret==start at the "&&", finish at the end of the label. */
8205 location_t combined_loc
8206 = make_location (start_loc
, start_loc
, finish_loc
);
8207 /* Create an expression representing the address. */
8208 expression
= finish_label_address_expr (identifier
, combined_loc
);
8209 if (cp_parser_non_integral_constant_expression (parser
,
8211 expression
= error_mark_node
;
8215 if (unary_operator
!= ERROR_MARK
)
8217 cp_expr cast_expression
;
8218 cp_expr expression
= error_mark_node
;
8219 non_integral_constant non_constant_p
= NIC_NONE
;
8220 location_t loc
= token
->location
;
8221 tsubst_flags_t complain
= complain_flags (decltype_p
);
8223 /* Consume the operator token. */
8224 token
= cp_lexer_consume_token (parser
->lexer
);
8225 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8227 /* Parse the cast-expression. */
8229 = cp_parser_cast_expression (parser
,
8230 unary_operator
== ADDR_EXPR
,
8236 OP_TOKEN CAST_EXPRESSION
8237 ^~~~~~~~~~~~~~~~~~~~~~~~~
8238 with start==caret at the operator token, and
8239 extending to the end of the cast_expression. */
8240 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8242 /* Now, build an appropriate representation. */
8243 switch (unary_operator
)
8246 non_constant_p
= NIC_STAR
;
8247 expression
= build_x_indirect_ref (loc
, cast_expression
,
8250 /* TODO: build_x_indirect_ref does not always honor the
8251 location, so ensure it is set. */
8252 expression
.set_location (loc
);
8256 non_constant_p
= NIC_ADDR
;
8259 expression
= build_x_unary_op (loc
, unary_operator
,
8262 /* TODO: build_x_unary_op does not always honor the location,
8263 so ensure it is set. */
8264 expression
.set_location (loc
);
8267 case PREINCREMENT_EXPR
:
8268 case PREDECREMENT_EXPR
:
8269 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8270 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8273 /* Immediately fold negation of a constant, unless the constant is 0
8274 (since -0 == 0) or it would overflow. */
8275 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8276 && CONSTANT_CLASS_P (cast_expression
)
8277 && !integer_zerop (cast_expression
)
8278 && !TREE_OVERFLOW (cast_expression
))
8280 tree folded
= fold_build1 (unary_operator
,
8281 TREE_TYPE (cast_expression
),
8283 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8285 expression
= cp_expr (folded
, loc
);
8290 case UNARY_PLUS_EXPR
:
8291 case TRUTH_NOT_EXPR
:
8292 expression
= finish_unary_op_expr (loc
, unary_operator
,
8293 cast_expression
, complain
);
8300 if (non_constant_p
!= NIC_NONE
8301 && cp_parser_non_integral_constant_expression (parser
,
8303 expression
= error_mark_node
;
8308 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8309 /*member_access_only_p=*/false,
8314 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8315 unary-operator, the corresponding tree code is returned. */
8317 static enum tree_code
8318 cp_parser_unary_operator (cp_token
* token
)
8320 switch (token
->type
)
8323 return INDIRECT_REF
;
8329 return UNARY_PLUS_EXPR
;
8335 return TRUTH_NOT_EXPR
;
8338 return BIT_NOT_EXPR
;
8345 /* Parse a new-expression.
8348 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8349 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8351 Returns a representation of the expression. */
8354 cp_parser_new_expression (cp_parser
* parser
)
8356 bool global_scope_p
;
8357 vec
<tree
, va_gc
> *placement
;
8359 vec
<tree
, va_gc
> *initializer
;
8360 tree nelts
= NULL_TREE
;
8363 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8365 /* Look for the optional `::' operator. */
8367 = (cp_parser_global_scope_opt (parser
,
8368 /*current_scope_valid_p=*/false)
8370 /* Look for the `new' operator. */
8371 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8372 /* There's no easy way to tell a new-placement from the
8373 `( type-id )' construct. */
8374 cp_parser_parse_tentatively (parser
);
8375 /* Look for a new-placement. */
8376 placement
= cp_parser_new_placement (parser
);
8377 /* If that didn't work out, there's no new-placement. */
8378 if (!cp_parser_parse_definitely (parser
))
8380 if (placement
!= NULL
)
8381 release_tree_vector (placement
);
8385 /* If the next token is a `(', then we have a parenthesized
8387 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8390 const char *saved_message
= parser
->type_definition_forbidden_message
;
8392 /* Consume the `('. */
8393 matching_parens parens
;
8394 parens
.consume_open (parser
);
8396 /* Parse the type-id. */
8397 parser
->type_definition_forbidden_message
8398 = G_("types may not be defined in a new-expression");
8400 type_id_in_expr_sentinel
s (parser
);
8401 type
= cp_parser_type_id (parser
);
8403 parser
->type_definition_forbidden_message
= saved_message
;
8405 /* Look for the closing `)'. */
8406 parens
.require_close (parser
);
8407 token
= cp_lexer_peek_token (parser
->lexer
);
8408 /* There should not be a direct-new-declarator in this production,
8409 but GCC used to allowed this, so we check and emit a sensible error
8410 message for this case. */
8411 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8413 error_at (token
->location
,
8414 "array bound forbidden after parenthesized type-id");
8415 inform (token
->location
,
8416 "try removing the parentheses around the type-id");
8417 cp_parser_direct_new_declarator (parser
);
8420 /* Otherwise, there must be a new-type-id. */
8422 type
= cp_parser_new_type_id (parser
, &nelts
);
8424 /* If the next token is a `(' or '{', then we have a new-initializer. */
8425 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8426 if (token
->type
== CPP_OPEN_PAREN
8427 || token
->type
== CPP_OPEN_BRACE
)
8428 initializer
= cp_parser_new_initializer (parser
);
8432 /* A new-expression may not appear in an integral constant
8434 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8435 ret
= error_mark_node
;
8436 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8437 of a new-type-id or type-id of a new-expression, the new-expression shall
8438 contain a new-initializer of the form ( assignment-expression )".
8439 Additionally, consistently with the spirit of DR 1467, we want to accept
8440 'new auto { 2 }' too. */
8441 else if ((ret
= type_uses_auto (type
))
8442 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8443 && (vec_safe_length (initializer
) != 1
8444 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8445 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8447 error_at (token
->location
,
8448 "initialization of new-expression for type %<auto%> "
8449 "requires exactly one element");
8450 ret
= error_mark_node
;
8454 /* Construct a location e.g.:
8457 with caret == start at the start of the "new" token, and the end
8458 at the end of the final token we consumed. */
8459 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8460 location_t end_loc
= get_finish (end_tok
->location
);
8461 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8463 /* Create a representation of the new-expression. */
8464 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8465 tf_warning_or_error
);
8466 protected_set_expr_location (ret
, combined_loc
);
8469 if (placement
!= NULL
)
8470 release_tree_vector (placement
);
8471 if (initializer
!= NULL
)
8472 release_tree_vector (initializer
);
8477 /* Parse a new-placement.
8482 Returns the same representation as for an expression-list. */
8484 static vec
<tree
, va_gc
> *
8485 cp_parser_new_placement (cp_parser
* parser
)
8487 vec
<tree
, va_gc
> *expression_list
;
8489 /* Parse the expression-list. */
8490 expression_list
= (cp_parser_parenthesized_expression_list
8491 (parser
, non_attr
, /*cast_p=*/false,
8492 /*allow_expansion_p=*/true,
8493 /*non_constant_p=*/NULL
));
8495 if (expression_list
&& expression_list
->is_empty ())
8496 error ("expected expression-list or type-id");
8498 return expression_list
;
8501 /* Parse a new-type-id.
8504 type-specifier-seq new-declarator [opt]
8506 Returns the TYPE allocated. If the new-type-id indicates an array
8507 type, *NELTS is set to the number of elements in the last array
8508 bound; the TYPE will not include the last array bound. */
8511 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8513 cp_decl_specifier_seq type_specifier_seq
;
8514 cp_declarator
*new_declarator
;
8515 cp_declarator
*declarator
;
8516 cp_declarator
*outer_declarator
;
8517 const char *saved_message
;
8519 /* The type-specifier sequence must not contain type definitions.
8520 (It cannot contain declarations of new types either, but if they
8521 are not definitions we will catch that because they are not
8523 saved_message
= parser
->type_definition_forbidden_message
;
8524 parser
->type_definition_forbidden_message
8525 = G_("types may not be defined in a new-type-id");
8526 /* Parse the type-specifier-seq. */
8527 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8528 /*is_trailing_return=*/false,
8529 &type_specifier_seq
);
8530 /* Restore the old message. */
8531 parser
->type_definition_forbidden_message
= saved_message
;
8533 if (type_specifier_seq
.type
== error_mark_node
)
8534 return error_mark_node
;
8536 /* Parse the new-declarator. */
8537 new_declarator
= cp_parser_new_declarator_opt (parser
);
8539 /* Determine the number of elements in the last array dimension, if
8542 /* Skip down to the last array dimension. */
8543 declarator
= new_declarator
;
8544 outer_declarator
= NULL
;
8545 while (declarator
&& (declarator
->kind
== cdk_pointer
8546 || declarator
->kind
== cdk_ptrmem
))
8548 outer_declarator
= declarator
;
8549 declarator
= declarator
->declarator
;
8552 && declarator
->kind
== cdk_array
8553 && declarator
->declarator
8554 && declarator
->declarator
->kind
== cdk_array
)
8556 outer_declarator
= declarator
;
8557 declarator
= declarator
->declarator
;
8560 if (declarator
&& declarator
->kind
== cdk_array
)
8562 *nelts
= declarator
->u
.array
.bounds
;
8563 if (*nelts
== error_mark_node
)
8564 *nelts
= integer_one_node
;
8566 if (outer_declarator
)
8567 outer_declarator
->declarator
= declarator
->declarator
;
8569 new_declarator
= NULL
;
8572 return groktypename (&type_specifier_seq
, new_declarator
, false);
8575 /* Parse an (optional) new-declarator.
8578 ptr-operator new-declarator [opt]
8579 direct-new-declarator
8581 Returns the declarator. */
8583 static cp_declarator
*
8584 cp_parser_new_declarator_opt (cp_parser
* parser
)
8586 enum tree_code code
;
8587 tree type
, std_attributes
= NULL_TREE
;
8588 cp_cv_quals cv_quals
;
8590 /* We don't know if there's a ptr-operator next, or not. */
8591 cp_parser_parse_tentatively (parser
);
8592 /* Look for a ptr-operator. */
8593 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8594 /* If that worked, look for more new-declarators. */
8595 if (cp_parser_parse_definitely (parser
))
8597 cp_declarator
*declarator
;
8599 /* Parse another optional declarator. */
8600 declarator
= cp_parser_new_declarator_opt (parser
);
8602 declarator
= cp_parser_make_indirect_declarator
8603 (code
, type
, cv_quals
, declarator
, std_attributes
);
8608 /* If the next token is a `[', there is a direct-new-declarator. */
8609 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8610 return cp_parser_direct_new_declarator (parser
);
8615 /* Parse a direct-new-declarator.
8617 direct-new-declarator:
8619 direct-new-declarator [constant-expression]
8623 static cp_declarator
*
8624 cp_parser_direct_new_declarator (cp_parser
* parser
)
8626 cp_declarator
*declarator
= NULL
;
8633 /* Look for the opening `['. */
8634 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8636 token
= cp_lexer_peek_token (parser
->lexer
);
8637 expression
= cp_parser_expression (parser
);
8638 /* The standard requires that the expression have integral
8639 type. DR 74 adds enumeration types. We believe that the
8640 real intent is that these expressions be handled like the
8641 expression in a `switch' condition, which also allows
8642 classes with a single conversion to integral or
8643 enumeration type. */
8644 if (!processing_template_decl
)
8647 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8652 error_at (token
->location
,
8653 "expression in new-declarator must have integral "
8654 "or enumeration type");
8655 expression
= error_mark_node
;
8659 /* Look for the closing `]'. */
8660 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8662 /* Add this bound to the declarator. */
8663 declarator
= make_array_declarator (declarator
, expression
);
8665 /* If the next token is not a `[', then there are no more
8667 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8674 /* Parse a new-initializer.
8677 ( expression-list [opt] )
8680 Returns a representation of the expression-list. */
8682 static vec
<tree
, va_gc
> *
8683 cp_parser_new_initializer (cp_parser
* parser
)
8685 vec
<tree
, va_gc
> *expression_list
;
8687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8690 bool expr_non_constant_p
;
8691 cp_lexer_set_source_position (parser
->lexer
);
8692 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8693 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8694 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8695 expression_list
= make_tree_vector_single (t
);
8698 expression_list
= (cp_parser_parenthesized_expression_list
8699 (parser
, non_attr
, /*cast_p=*/false,
8700 /*allow_expansion_p=*/true,
8701 /*non_constant_p=*/NULL
));
8703 return expression_list
;
8706 /* Parse a delete-expression.
8709 :: [opt] delete cast-expression
8710 :: [opt] delete [ ] cast-expression
8712 Returns a representation of the expression. */
8715 cp_parser_delete_expression (cp_parser
* parser
)
8717 bool global_scope_p
;
8721 /* Look for the optional `::' operator. */
8723 = (cp_parser_global_scope_opt (parser
,
8724 /*current_scope_valid_p=*/false)
8726 /* Look for the `delete' keyword. */
8727 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8728 /* See if the array syntax is in use. */
8729 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8731 /* Consume the `[' token. */
8732 cp_lexer_consume_token (parser
->lexer
);
8733 /* Look for the `]' token. */
8734 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8735 /* Remember that this is the `[]' construct. */
8741 /* Parse the cast-expression. */
8742 expression
= cp_parser_simple_cast_expression (parser
);
8744 /* A delete-expression may not appear in an integral constant
8746 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8747 return error_mark_node
;
8749 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8750 tf_warning_or_error
);
8753 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8754 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8758 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8760 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8761 switch (token
->type
)
8767 case CPP_CLOSE_SQUARE
:
8768 case CPP_CLOSE_PAREN
:
8769 case CPP_CLOSE_BRACE
:
8770 case CPP_OPEN_BRACE
:
8774 case CPP_DEREF_STAR
:
8782 case CPP_GREATER_EQ
:
8803 case CPP_OPEN_PAREN
:
8804 /* In ((type ()) () the last () isn't a valid cast-expression,
8805 so the whole must be parsed as postfix-expression. */
8806 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8809 case CPP_OPEN_SQUARE
:
8810 /* '[' may start a primary-expression in obj-c++ and in C++11,
8811 as a lambda-expression, eg, '(void)[]{}'. */
8812 if (cxx_dialect
>= cxx11
)
8814 return c_dialect_objc ();
8817 case CPP_MINUS_MINUS
:
8818 /* '++' and '--' may or may not start a cast-expression:
8820 struct T { void operator++(int); };
8821 void f() { (T())++; }
8834 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8835 in the order: const_cast, static_cast, reinterpret_cast.
8837 Don't suggest dynamic_cast.
8839 Return the first legal cast kind found, or NULL otherwise. */
8842 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8846 /* Reuse the parser logic by attempting to build the various kinds of
8847 cast, with "complain" disabled.
8848 Identify the first such cast that is valid. */
8850 /* Don't attempt to run such logic within template processing. */
8851 if (processing_template_decl
)
8854 /* First try const_cast. */
8855 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8856 if (trial
!= error_mark_node
)
8857 return "const_cast";
8859 /* If that fails, try static_cast. */
8860 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8861 if (trial
!= error_mark_node
)
8862 return "static_cast";
8864 /* Finally, try reinterpret_cast. */
8865 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8866 if (trial
!= error_mark_node
)
8867 return "reinterpret_cast";
8869 /* No such cast possible. */
8873 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8874 suggesting how to convert a C-style cast of the form:
8878 to a C++-style cast.
8880 The primary range of RICHLOC is asssumed to be that of the original
8881 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8882 of the parens in the C-style cast. */
8885 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8886 location_t close_paren_loc
, tree orig_expr
,
8889 /* This function is non-trivial, so bail out now if the warning isn't
8890 going to be emitted. */
8891 if (!warn_old_style_cast
)
8894 /* Try to find a legal C++ cast, trying them in order:
8895 const_cast, static_cast, reinterpret_cast. */
8896 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8897 if (!cast_suggestion
)
8900 /* Replace the open paren with "CAST_SUGGESTION<". */
8902 pp_printf (&pp
, "%s<", cast_suggestion
);
8903 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8905 /* Replace the close paren with "> (". */
8906 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8908 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8909 rich_loc
->add_fixit_insert_after (")");
8913 /* Parse a cast-expression.
8917 ( type-id ) cast-expression
8919 ADDRESS_P is true iff the unary-expression is appearing as the
8920 operand of the `&' operator. CAST_P is true if this expression is
8921 the target of a cast.
8923 Returns a representation of the expression. */
8926 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8927 bool decltype_p
, cp_id_kind
* pidk
)
8929 /* If it's a `(', then we might be looking at a cast. */
8930 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8932 tree type
= NULL_TREE
;
8933 cp_expr
expr (NULL_TREE
);
8934 int cast_expression
= 0;
8935 const char *saved_message
;
8937 /* There's no way to know yet whether or not this is a cast.
8938 For example, `(int (3))' is a unary-expression, while `(int)
8939 3' is a cast. So, we resort to parsing tentatively. */
8940 cp_parser_parse_tentatively (parser
);
8941 /* Types may not be defined in a cast. */
8942 saved_message
= parser
->type_definition_forbidden_message
;
8943 parser
->type_definition_forbidden_message
8944 = G_("types may not be defined in casts");
8945 /* Consume the `('. */
8946 matching_parens parens
;
8947 cp_token
*open_paren
= parens
.consume_open (parser
);
8948 location_t open_paren_loc
= open_paren
->location
;
8949 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8951 /* A very tricky bit is that `(struct S) { 3 }' is a
8952 compound-literal (which we permit in C++ as an extension).
8953 But, that construct is not a cast-expression -- it is a
8954 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8955 is legal; if the compound-literal were a cast-expression,
8956 you'd need an extra set of parentheses.) But, if we parse
8957 the type-id, and it happens to be a class-specifier, then we
8958 will commit to the parse at that point, because we cannot
8959 undo the action that is done when creating a new class. So,
8960 then we cannot back up and do a postfix-expression.
8962 Another tricky case is the following (c++/29234):
8964 struct S { void operator () (); };
8971 As a type-id we parse the parenthesized S()() as a function
8972 returning a function, groktypename complains and we cannot
8973 back up in this case either.
8975 Therefore, we scan ahead to the closing `)', and check to see
8976 if the tokens after the `)' can start a cast-expression. Otherwise
8977 we are dealing with an unary-expression, a postfix-expression
8980 Yet another tricky case, in C++11, is the following (c++/54891):
8984 The issue is that usually, besides the case of lambda-expressions,
8985 the parenthesized type-id cannot be followed by '[', and, eg, we
8986 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8987 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8988 we don't commit, we try a cast-expression, then an unary-expression.
8990 Save tokens so that we can put them back. */
8991 cp_lexer_save_tokens (parser
->lexer
);
8993 /* We may be looking at a cast-expression. */
8994 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8995 /*consume_paren=*/true))
8997 = cp_parser_tokens_start_cast_expression (parser
);
8999 /* Roll back the tokens we skipped. */
9000 cp_lexer_rollback_tokens (parser
->lexer
);
9001 /* If we aren't looking at a cast-expression, simulate an error so
9002 that the call to cp_parser_error_occurred below returns true. */
9003 if (!cast_expression
)
9004 cp_parser_simulate_error (parser
);
9007 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9008 parser
->in_type_id_in_expr_p
= true;
9009 /* Look for the type-id. */
9010 type
= cp_parser_type_id (parser
);
9011 /* Look for the closing `)'. */
9012 cp_token
*close_paren
= parens
.require_close (parser
);
9014 close_paren_loc
= close_paren
->location
;
9015 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9018 /* Restore the saved message. */
9019 parser
->type_definition_forbidden_message
= saved_message
;
9021 /* At this point this can only be either a cast or a
9022 parenthesized ctor such as `(T ())' that looks like a cast to
9023 function returning T. */
9024 if (!cp_parser_error_occurred (parser
))
9026 /* Only commit if the cast-expression doesn't start with
9027 '++', '--', or '[' in C++11. */
9028 if (cast_expression
> 0)
9029 cp_parser_commit_to_topmost_tentative_parse (parser
);
9031 expr
= cp_parser_cast_expression (parser
,
9032 /*address_p=*/false,
9034 /*decltype_p=*/false,
9037 if (cp_parser_parse_definitely (parser
))
9039 /* Warn about old-style casts, if so requested. */
9040 if (warn_old_style_cast
9041 && !in_system_header_at (input_location
)
9042 && !VOID_TYPE_P (type
)
9043 && current_lang_name
!= lang_name_c
)
9045 gcc_rich_location
rich_loc (input_location
);
9046 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9048 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9049 "use of old-style cast to %q#T", type
);
9052 /* Only type conversions to integral or enumeration types
9053 can be used in constant-expressions. */
9054 if (!cast_valid_in_integral_constant_expression_p (type
)
9055 && cp_parser_non_integral_constant_expression (parser
,
9057 return error_mark_node
;
9059 /* Perform the cast. */
9063 with start==caret at the open paren, extending to the
9065 location_t cast_loc
= make_location (open_paren_loc
,
9067 expr
.get_finish ());
9068 expr
= build_c_cast (cast_loc
, type
, expr
);
9073 cp_parser_abort_tentative_parse (parser
);
9076 /* If we get here, then it's not a cast, so it must be a
9077 unary-expression. */
9078 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9079 cast_p
, decltype_p
);
9082 /* Parse a binary expression of the general form:
9086 pm-expression .* cast-expression
9087 pm-expression ->* cast-expression
9089 multiplicative-expression:
9091 multiplicative-expression * pm-expression
9092 multiplicative-expression / pm-expression
9093 multiplicative-expression % pm-expression
9095 additive-expression:
9096 multiplicative-expression
9097 additive-expression + multiplicative-expression
9098 additive-expression - multiplicative-expression
9102 shift-expression << additive-expression
9103 shift-expression >> additive-expression
9105 relational-expression:
9107 relational-expression < shift-expression
9108 relational-expression > shift-expression
9109 relational-expression <= shift-expression
9110 relational-expression >= shift-expression
9114 relational-expression:
9115 relational-expression <? shift-expression
9116 relational-expression >? shift-expression
9118 equality-expression:
9119 relational-expression
9120 equality-expression == relational-expression
9121 equality-expression != relational-expression
9125 and-expression & equality-expression
9127 exclusive-or-expression:
9129 exclusive-or-expression ^ and-expression
9131 inclusive-or-expression:
9132 exclusive-or-expression
9133 inclusive-or-expression | exclusive-or-expression
9135 logical-and-expression:
9136 inclusive-or-expression
9137 logical-and-expression && inclusive-or-expression
9139 logical-or-expression:
9140 logical-and-expression
9141 logical-or-expression || logical-and-expression
9143 All these are implemented with a single function like:
9146 simple-cast-expression
9147 binary-expression <token> binary-expression
9149 CAST_P is true if this expression is the target of a cast.
9151 The binops_by_token map is used to get the tree codes for each <token> type.
9152 binary-expressions are associated according to a precedence table. */
9154 #define TOKEN_PRECEDENCE(token) \
9155 (((token->type == CPP_GREATER \
9156 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9157 && !parser->greater_than_is_operator_p) \
9158 ? PREC_NOT_OPERATOR \
9159 : binops_by_token[token->type].prec)
9162 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9163 bool no_toplevel_fold_p
,
9165 enum cp_parser_prec prec
,
9168 cp_parser_expression_stack stack
;
9169 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9170 cp_parser_expression_stack_entry current
;
9173 enum tree_code rhs_type
;
9174 enum cp_parser_prec new_prec
, lookahead_prec
;
9177 /* Parse the first expression. */
9178 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9179 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9180 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9181 cast_p
, decltype_p
, pidk
);
9182 current
.prec
= prec
;
9184 if (cp_parser_error_occurred (parser
))
9185 return error_mark_node
;
9189 /* Get an operator token. */
9190 token
= cp_lexer_peek_token (parser
->lexer
);
9192 if (warn_cxx11_compat
9193 && token
->type
== CPP_RSHIFT
9194 && !parser
->greater_than_is_operator_p
)
9196 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9197 "%<>>%> operator is treated"
9198 " as two right angle brackets in C++11"))
9199 inform (token
->location
,
9200 "suggest parentheses around %<>>%> expression");
9203 new_prec
= TOKEN_PRECEDENCE (token
);
9204 if (new_prec
!= PREC_NOT_OPERATOR
9205 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9206 /* This is a fold-expression; handle it later. */
9207 new_prec
= PREC_NOT_OPERATOR
;
9209 /* Popping an entry off the stack means we completed a subexpression:
9210 - either we found a token which is not an operator (`>' where it is not
9211 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9212 will happen repeatedly;
9213 - or, we found an operator which has lower priority. This is the case
9214 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9216 if (new_prec
<= current
.prec
)
9225 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9226 current
.loc
= token
->location
;
9228 /* We used the operator token. */
9229 cp_lexer_consume_token (parser
->lexer
);
9231 /* For "false && x" or "true || x", x will never be executed;
9232 disable warnings while evaluating it. */
9233 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9234 c_inhibit_evaluation_warnings
+=
9235 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9236 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9237 c_inhibit_evaluation_warnings
+=
9238 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9240 /* Extract another operand. It may be the RHS of this expression
9241 or the LHS of a new, higher priority expression. */
9242 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9243 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9244 rhs
= cp_parser_simple_cast_expression (parser
);
9246 /* Get another operator token. Look up its precedence to avoid
9247 building a useless (immediately popped) stack entry for common
9248 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9249 token
= cp_lexer_peek_token (parser
->lexer
);
9250 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9251 if (lookahead_prec
!= PREC_NOT_OPERATOR
9252 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9253 lookahead_prec
= PREC_NOT_OPERATOR
;
9254 if (lookahead_prec
> new_prec
)
9256 /* ... and prepare to parse the RHS of the new, higher priority
9257 expression. Since precedence levels on the stack are
9258 monotonically increasing, we do not have to care about
9263 current
.lhs_type
= rhs_type
;
9264 current
.prec
= new_prec
;
9265 new_prec
= lookahead_prec
;
9269 lookahead_prec
= new_prec
;
9270 /* If the stack is not empty, we have parsed into LHS the right side
9271 (`4' in the example above) of an expression we had suspended.
9272 We can use the information on the stack to recover the LHS (`3')
9273 from the stack together with the tree code (`MULT_EXPR'), and
9274 the precedence of the higher level subexpression
9275 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9276 which will be used to actually build the additive expression. */
9278 rhs_type
= current
.lhs_type
;
9283 /* Undo the disabling of warnings done above. */
9284 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9285 c_inhibit_evaluation_warnings
-=
9286 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9287 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9288 c_inhibit_evaluation_warnings
-=
9289 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9291 if (warn_logical_not_paren
9292 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9293 && current
.lhs_type
== TRUTH_NOT_EXPR
9294 /* Avoid warning for !!x == y. */
9295 && (TREE_CODE (current
.lhs
) != NE_EXPR
9296 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9297 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9298 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9299 /* Avoid warning for !b == y where b is boolean. */
9300 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9301 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9303 /* Avoid warning for !!b == y where b is boolean. */
9304 && (!DECL_P (current
.lhs
)
9305 || TREE_TYPE (current
.lhs
) == NULL_TREE
9306 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9307 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9308 current
.lhs
, maybe_constant_value (rhs
));
9312 location_t combined_loc
= make_location (current
.loc
,
9313 current
.lhs
.get_start (),
9316 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9317 ERROR_MARK for everything that is not a binary expression.
9318 This makes warn_about_parentheses miss some warnings that
9319 involve unary operators. For unary expressions we should
9320 pass the correct tree_code unless the unary expression was
9321 surrounded by parentheses.
9323 if (no_toplevel_fold_p
9324 && lookahead_prec
<= current
.prec
9327 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9328 current
.lhs
= error_mark_node
;
9332 = build_min (current
.tree_type
,
9333 TREE_CODE_CLASS (current
.tree_type
)
9335 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9336 current
.lhs
.get_value (), rhs
.get_value ());
9337 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9342 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9343 current
.lhs
, current
.lhs_type
,
9344 rhs
, rhs_type
, &overload
,
9345 complain_flags (decltype_p
));
9346 /* TODO: build_x_binary_op doesn't always honor the location. */
9347 current
.lhs
.set_location (combined_loc
);
9349 current
.lhs_type
= current
.tree_type
;
9351 /* If the binary operator required the use of an overloaded operator,
9352 then this expression cannot be an integral constant-expression.
9353 An overloaded operator can be used even if both operands are
9354 otherwise permissible in an integral constant-expression if at
9355 least one of the operands is of enumeration type. */
9358 && cp_parser_non_integral_constant_expression (parser
,
9360 return error_mark_node
;
9367 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9368 bool no_toplevel_fold_p
,
9369 enum cp_parser_prec prec
,
9372 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9373 /*decltype*/false, prec
, pidk
);
9376 /* Parse the `? expression : assignment-expression' part of a
9377 conditional-expression. The LOGICAL_OR_EXPR is the
9378 logical-or-expression that started the conditional-expression.
9379 Returns a representation of the entire conditional-expression.
9381 This routine is used by cp_parser_assignment_expression.
9383 ? expression : assignment-expression
9387 ? : assignment-expression */
9390 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9392 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9393 cp_expr assignment_expr
;
9394 struct cp_token
*token
;
9395 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9397 /* Consume the `?' token. */
9398 cp_lexer_consume_token (parser
->lexer
);
9399 token
= cp_lexer_peek_token (parser
->lexer
);
9400 if (cp_parser_allow_gnu_extensions_p (parser
)
9401 && token
->type
== CPP_COLON
)
9403 pedwarn (token
->location
, OPT_Wpedantic
,
9404 "ISO C++ does not allow ?: with omitted middle operand");
9405 /* Implicit true clause. */
9407 c_inhibit_evaluation_warnings
+=
9408 folded_logical_or_expr
== truthvalue_true_node
;
9409 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9413 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9414 parser
->colon_corrects_to_scope_p
= false;
9415 /* Parse the expression. */
9416 c_inhibit_evaluation_warnings
+=
9417 folded_logical_or_expr
== truthvalue_false_node
;
9418 expr
= cp_parser_expression (parser
);
9419 c_inhibit_evaluation_warnings
+=
9420 ((folded_logical_or_expr
== truthvalue_true_node
)
9421 - (folded_logical_or_expr
== truthvalue_false_node
));
9422 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9425 /* The next token should be a `:'. */
9426 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9427 /* Parse the assignment-expression. */
9428 assignment_expr
= cp_parser_assignment_expression (parser
);
9429 c_inhibit_evaluation_warnings
-=
9430 folded_logical_or_expr
== truthvalue_true_node
;
9433 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9434 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9435 with the caret at the "?", ranging from the start of
9436 the logical_or_expr to the end of the assignment_expr. */
9437 loc
= make_location (loc
,
9438 logical_or_expr
.get_start (),
9439 assignment_expr
.get_finish ());
9441 /* Build the conditional-expression. */
9442 return build_x_conditional_expr (loc
, logical_or_expr
,
9445 tf_warning_or_error
);
9448 /* Parse an assignment-expression.
9450 assignment-expression:
9451 conditional-expression
9452 logical-or-expression assignment-operator assignment_expression
9455 CAST_P is true if this expression is the target of a cast.
9456 DECLTYPE_P is true if this expression is the operand of decltype.
9458 Returns a representation for the expression. */
9461 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9462 bool cast_p
, bool decltype_p
)
9466 /* If the next token is the `throw' keyword, then we're looking at
9467 a throw-expression. */
9468 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9469 expr
= cp_parser_throw_expression (parser
);
9470 /* Otherwise, it must be that we are looking at a
9471 logical-or-expression. */
9474 /* Parse the binary expressions (logical-or-expression). */
9475 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9477 PREC_NOT_OPERATOR
, pidk
);
9478 /* If the next token is a `?' then we're actually looking at a
9479 conditional-expression. */
9480 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9481 return cp_parser_question_colon_clause (parser
, expr
);
9484 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9486 /* If it's an assignment-operator, we're using the second
9488 enum tree_code assignment_operator
9489 = cp_parser_assignment_operator_opt (parser
);
9490 if (assignment_operator
!= ERROR_MARK
)
9492 bool non_constant_p
;
9494 /* Parse the right-hand side of the assignment. */
9495 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9498 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9499 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9501 /* An assignment may not appear in a
9502 constant-expression. */
9503 if (cp_parser_non_integral_constant_expression (parser
,
9505 return error_mark_node
;
9506 /* Build the assignment expression. Its default
9510 is the location of the '=' token as the
9511 caret, ranging from the start of the lhs to the
9513 loc
= make_location (loc
,
9516 expr
= build_x_modify_expr (loc
, expr
,
9517 assignment_operator
,
9519 complain_flags (decltype_p
));
9520 /* TODO: build_x_modify_expr doesn't honor the location,
9521 so we must set it here. */
9522 expr
.set_location (loc
);
9530 /* Parse an (optional) assignment-operator.
9532 assignment-operator: one of
9533 = *= /= %= += -= >>= <<= &= ^= |=
9537 assignment-operator: one of
9540 If the next token is an assignment operator, the corresponding tree
9541 code is returned, and the token is consumed. For example, for
9542 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9543 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9544 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9545 operator, ERROR_MARK is returned. */
9547 static enum tree_code
9548 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9553 /* Peek at the next token. */
9554 token
= cp_lexer_peek_token (parser
->lexer
);
9556 switch (token
->type
)
9567 op
= TRUNC_DIV_EXPR
;
9571 op
= TRUNC_MOD_EXPR
;
9603 /* Nothing else is an assignment operator. */
9607 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9608 if (op
!= ERROR_MARK
9609 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9612 /* If it was an assignment operator, consume it. */
9613 if (op
!= ERROR_MARK
)
9614 cp_lexer_consume_token (parser
->lexer
);
9619 /* Parse an expression.
9622 assignment-expression
9623 expression , assignment-expression
9625 CAST_P is true if this expression is the target of a cast.
9626 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9627 except possibly parenthesized or on the RHS of a comma (N3276).
9629 Returns a representation of the expression. */
9632 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9633 bool cast_p
, bool decltype_p
)
9635 cp_expr expression
= NULL_TREE
;
9636 location_t loc
= UNKNOWN_LOCATION
;
9640 cp_expr assignment_expression
;
9642 /* Parse the next assignment-expression. */
9643 assignment_expression
9644 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9646 /* We don't create a temporary for a call that is the immediate operand
9647 of decltype or on the RHS of a comma. But when we see a comma, we
9648 need to create a temporary for a call on the LHS. */
9649 if (decltype_p
&& !processing_template_decl
9650 && TREE_CODE (assignment_expression
) == CALL_EXPR
9651 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9652 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9653 assignment_expression
9654 = build_cplus_new (TREE_TYPE (assignment_expression
),
9655 assignment_expression
, tf_warning_or_error
);
9657 /* If this is the first assignment-expression, we can just
9660 expression
= assignment_expression
;
9663 /* Create a location with caret at the comma, ranging
9664 from the start of the LHS to the end of the RHS. */
9665 loc
= make_location (loc
,
9666 expression
.get_start (),
9667 assignment_expression
.get_finish ());
9668 expression
= build_x_compound_expr (loc
, expression
,
9669 assignment_expression
,
9670 complain_flags (decltype_p
));
9671 expression
.set_location (loc
);
9673 /* If the next token is not a comma, or we're in a fold-expression, then
9674 we are done with the expression. */
9675 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9676 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9678 /* Consume the `,'. */
9679 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9680 cp_lexer_consume_token (parser
->lexer
);
9681 /* A comma operator cannot appear in a constant-expression. */
9682 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9683 expression
= error_mark_node
;
9689 /* Parse a constant-expression.
9691 constant-expression:
9692 conditional-expression
9694 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9695 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9696 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9697 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9698 only parse a conditional-expression, otherwise parse an
9699 assignment-expression. See below for rationale. */
9702 cp_parser_constant_expression (cp_parser
* parser
,
9703 bool allow_non_constant_p
,
9704 bool *non_constant_p
,
9707 bool saved_integral_constant_expression_p
;
9708 bool saved_allow_non_integral_constant_expression_p
;
9709 bool saved_non_integral_constant_expression_p
;
9712 /* It might seem that we could simply parse the
9713 conditional-expression, and then check to see if it were
9714 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9715 one that the compiler can figure out is constant, possibly after
9716 doing some simplifications or optimizations. The standard has a
9717 precise definition of constant-expression, and we must honor
9718 that, even though it is somewhat more restrictive.
9724 is not a legal declaration, because `(2, 3)' is not a
9725 constant-expression. The `,' operator is forbidden in a
9726 constant-expression. However, GCC's constant-folding machinery
9727 will fold this operation to an INTEGER_CST for `3'. */
9729 /* Save the old settings. */
9730 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9731 saved_allow_non_integral_constant_expression_p
9732 = parser
->allow_non_integral_constant_expression_p
;
9733 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9734 /* We are now parsing a constant-expression. */
9735 parser
->integral_constant_expression_p
= true;
9736 parser
->allow_non_integral_constant_expression_p
9737 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9738 parser
->non_integral_constant_expression_p
= false;
9739 /* Although the grammar says "conditional-expression", when not STRICT_P,
9740 we parse an "assignment-expression", which also permits
9741 "throw-expression" and the use of assignment operators. In the case
9742 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9743 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9744 actually essential that we look for an assignment-expression.
9745 For example, cp_parser_initializer_clauses uses this function to
9746 determine whether a particular assignment-expression is in fact
9750 /* Parse the binary expressions (logical-or-expression). */
9751 expression
= cp_parser_binary_expression (parser
, false, false, false,
9752 PREC_NOT_OPERATOR
, NULL
);
9753 /* If the next token is a `?' then we're actually looking at
9754 a conditional-expression; otherwise we're done. */
9755 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9756 expression
= cp_parser_question_colon_clause (parser
, expression
);
9759 expression
= cp_parser_assignment_expression (parser
);
9760 /* Restore the old settings. */
9761 parser
->integral_constant_expression_p
9762 = saved_integral_constant_expression_p
;
9763 parser
->allow_non_integral_constant_expression_p
9764 = saved_allow_non_integral_constant_expression_p
;
9765 if (cxx_dialect
>= cxx11
)
9767 /* Require an rvalue constant expression here; that's what our
9768 callers expect. Reference constant expressions are handled
9769 separately in e.g. cp_parser_template_argument. */
9770 tree decay
= expression
;
9771 if (TREE_TYPE (expression
)
9772 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9773 decay
= build_address (expression
);
9774 bool is_const
= potential_rvalue_constant_expression (decay
);
9775 parser
->non_integral_constant_expression_p
= !is_const
;
9776 if (!is_const
&& !allow_non_constant_p
)
9777 require_potential_rvalue_constant_expression (decay
);
9779 if (allow_non_constant_p
)
9780 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9781 parser
->non_integral_constant_expression_p
9782 = saved_non_integral_constant_expression_p
;
9787 /* Parse __builtin_offsetof.
9789 offsetof-expression:
9790 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9792 offsetof-member-designator:
9794 | offsetof-member-designator "." id-expression
9795 | offsetof-member-designator "[" expression "]"
9796 | offsetof-member-designator "->" id-expression */
9799 cp_parser_builtin_offsetof (cp_parser
*parser
)
9801 int save_ice_p
, save_non_ice_p
;
9806 location_t finish_loc
;
9808 /* We're about to accept non-integral-constant things, but will
9809 definitely yield an integral constant expression. Save and
9810 restore these values around our local parsing. */
9811 save_ice_p
= parser
->integral_constant_expression_p
;
9812 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9814 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9816 /* Consume the "__builtin_offsetof" token. */
9817 cp_lexer_consume_token (parser
->lexer
);
9818 /* Consume the opening `('. */
9819 matching_parens parens
;
9820 parens
.require_open (parser
);
9821 /* Parse the type-id. */
9822 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9824 const char *saved_message
= parser
->type_definition_forbidden_message
;
9825 parser
->type_definition_forbidden_message
9826 = G_("types may not be defined within __builtin_offsetof");
9827 type
= cp_parser_type_id (parser
);
9828 parser
->type_definition_forbidden_message
= saved_message
;
9830 /* Look for the `,'. */
9831 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9832 token
= cp_lexer_peek_token (parser
->lexer
);
9834 /* Build the (type *)null that begins the traditional offsetof macro. */
9836 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9837 tf_warning_or_error
);
9839 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9840 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9841 true, &dummy
, token
->location
);
9844 token
= cp_lexer_peek_token (parser
->lexer
);
9845 switch (token
->type
)
9847 case CPP_OPEN_SQUARE
:
9848 /* offsetof-member-designator "[" expression "]" */
9849 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9854 /* offsetof-member-designator "->" identifier */
9855 expr
= grok_array_decl (token
->location
, expr
,
9856 integer_zero_node
, false);
9860 /* offsetof-member-designator "." identifier */
9861 cp_lexer_consume_token (parser
->lexer
);
9862 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9867 case CPP_CLOSE_PAREN
:
9868 /* Consume the ")" token. */
9869 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9870 cp_lexer_consume_token (parser
->lexer
);
9874 /* Error. We know the following require will fail, but
9875 that gives the proper error message. */
9876 parens
.require_close (parser
);
9877 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9878 expr
= error_mark_node
;
9884 /* Make a location of the form:
9885 __builtin_offsetof (struct s, f)
9886 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9887 with caret at the type-id, ranging from the start of the
9888 "_builtin_offsetof" token to the close paren. */
9889 loc
= make_location (loc
, start_loc
, finish_loc
);
9890 /* The result will be an INTEGER_CST, so we need to explicitly
9891 preserve the location. */
9892 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9895 parser
->integral_constant_expression_p
= save_ice_p
;
9896 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9898 expr
= expr
.maybe_add_location_wrapper ();
9902 /* Parse a trait expression.
9904 Returns a representation of the expression, the underlying type
9905 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9908 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9911 tree type1
, type2
= NULL_TREE
;
9912 bool binary
= false;
9913 bool variadic
= false;
9917 case RID_HAS_NOTHROW_ASSIGN
:
9918 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9920 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9921 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9923 case RID_HAS_NOTHROW_COPY
:
9924 kind
= CPTK_HAS_NOTHROW_COPY
;
9926 case RID_HAS_TRIVIAL_ASSIGN
:
9927 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9929 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9930 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9932 case RID_HAS_TRIVIAL_COPY
:
9933 kind
= CPTK_HAS_TRIVIAL_COPY
;
9935 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9936 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9938 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9939 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9941 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9942 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9944 case RID_IS_ABSTRACT
:
9945 kind
= CPTK_IS_ABSTRACT
;
9947 case RID_IS_AGGREGATE
:
9948 kind
= CPTK_IS_AGGREGATE
;
9950 case RID_IS_BASE_OF
:
9951 kind
= CPTK_IS_BASE_OF
;
9955 kind
= CPTK_IS_CLASS
;
9958 kind
= CPTK_IS_EMPTY
;
9961 kind
= CPTK_IS_ENUM
;
9964 kind
= CPTK_IS_FINAL
;
9966 case RID_IS_LITERAL_TYPE
:
9967 kind
= CPTK_IS_LITERAL_TYPE
;
9972 case RID_IS_POLYMORPHIC
:
9973 kind
= CPTK_IS_POLYMORPHIC
;
9975 case RID_IS_SAME_AS
:
9976 kind
= CPTK_IS_SAME_AS
;
9979 case RID_IS_STD_LAYOUT
:
9980 kind
= CPTK_IS_STD_LAYOUT
;
9982 case RID_IS_TRIVIAL
:
9983 kind
= CPTK_IS_TRIVIAL
;
9985 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9986 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9989 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9990 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9993 case RID_IS_TRIVIALLY_COPYABLE
:
9994 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9997 kind
= CPTK_IS_UNION
;
9999 case RID_UNDERLYING_TYPE
:
10000 kind
= CPTK_UNDERLYING_TYPE
;
10005 case RID_DIRECT_BASES
:
10006 kind
= CPTK_DIRECT_BASES
;
10008 case RID_IS_ASSIGNABLE
:
10009 kind
= CPTK_IS_ASSIGNABLE
;
10012 case RID_IS_CONSTRUCTIBLE
:
10013 kind
= CPTK_IS_CONSTRUCTIBLE
;
10017 gcc_unreachable ();
10020 /* Get location of initial token. */
10021 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10023 /* Consume the token. */
10024 cp_lexer_consume_token (parser
->lexer
);
10026 matching_parens parens
;
10027 parens
.require_open (parser
);
10030 type_id_in_expr_sentinel
s (parser
);
10031 type1
= cp_parser_type_id (parser
);
10034 if (type1
== error_mark_node
)
10035 return error_mark_node
;
10039 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10042 type_id_in_expr_sentinel
s (parser
);
10043 type2
= cp_parser_type_id (parser
);
10046 if (type2
== error_mark_node
)
10047 return error_mark_node
;
10051 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10053 cp_lexer_consume_token (parser
->lexer
);
10054 tree elt
= cp_parser_type_id (parser
);
10055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10057 cp_lexer_consume_token (parser
->lexer
);
10058 elt
= make_pack_expansion (elt
);
10060 if (elt
== error_mark_node
)
10061 return error_mark_node
;
10062 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10066 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10067 parens
.require_close (parser
);
10069 /* Construct a location of the form:
10070 __is_trivially_copyable(_Tp)
10071 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10072 with start == caret, finishing at the close-paren. */
10073 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10075 /* Complete the trait expression, which may mean either processing
10076 the trait expr now or saving it for template instantiation. */
10079 case CPTK_UNDERLYING_TYPE
:
10080 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10082 return cp_expr (finish_bases (type1
, false), trait_loc
);
10083 case CPTK_DIRECT_BASES
:
10084 return cp_expr (finish_bases (type1
, true), trait_loc
);
10086 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10090 /* Parse a lambda expression.
10093 lambda-introducer lambda-declarator [opt] compound-statement
10095 Returns a representation of the expression. */
10098 cp_parser_lambda_expression (cp_parser
* parser
)
10100 tree lambda_expr
= build_lambda_expr ();
10103 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10104 cp_token_position start
= 0;
10106 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10108 if (cp_unevaluated_operand
)
10110 if (!token
->error_reported
)
10112 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10113 "lambda-expression in unevaluated context");
10114 token
->error_reported
= true;
10118 else if (parser
->in_template_argument_list_p
)
10120 if (!token
->error_reported
)
10122 error_at (token
->location
, "lambda-expression in template-argument");
10123 token
->error_reported
= true;
10128 /* We may be in the middle of deferred access check. Disable
10130 push_deferring_access_checks (dk_no_deferred
);
10132 cp_parser_lambda_introducer (parser
, lambda_expr
);
10134 type
= begin_lambda_type (lambda_expr
);
10135 if (type
== error_mark_node
)
10136 return error_mark_node
;
10138 record_lambda_scope (lambda_expr
);
10140 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10141 determine_visibility (TYPE_NAME (type
));
10143 /* Now that we've started the type, add the capture fields for any
10144 explicit captures. */
10145 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10148 /* Inside the class, surrounding template-parameter-lists do not apply. */
10149 unsigned int saved_num_template_parameter_lists
10150 = parser
->num_template_parameter_lists
;
10151 unsigned char in_statement
= parser
->in_statement
;
10152 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10153 bool fully_implicit_function_template_p
10154 = parser
->fully_implicit_function_template_p
;
10155 tree implicit_template_parms
= parser
->implicit_template_parms
;
10156 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10157 bool auto_is_implicit_function_template_parm_p
10158 = parser
->auto_is_implicit_function_template_parm_p
;
10160 parser
->num_template_parameter_lists
= 0;
10161 parser
->in_statement
= 0;
10162 parser
->in_switch_statement_p
= false;
10163 parser
->fully_implicit_function_template_p
= false;
10164 parser
->implicit_template_parms
= 0;
10165 parser
->implicit_template_scope
= 0;
10166 parser
->auto_is_implicit_function_template_parm_p
= false;
10168 /* By virtue of defining a local class, a lambda expression has access to
10169 the private variables of enclosing classes. */
10171 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10173 if (ok
&& cp_parser_error_occurred (parser
))
10178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10179 && cp_parser_start_tentative_firewall (parser
))
10181 cp_parser_lambda_body (parser
, lambda_expr
);
10183 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10185 if (cp_parser_skip_to_closing_brace (parser
))
10186 cp_lexer_consume_token (parser
->lexer
);
10189 /* The capture list was built up in reverse order; fix that now. */
10190 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10191 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10194 maybe_add_lambda_conv_op (type
);
10196 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10198 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10199 parser
->in_statement
= in_statement
;
10200 parser
->in_switch_statement_p
= in_switch_statement_p
;
10201 parser
->fully_implicit_function_template_p
10202 = fully_implicit_function_template_p
;
10203 parser
->implicit_template_parms
= implicit_template_parms
;
10204 parser
->implicit_template_scope
= implicit_template_scope
;
10205 parser
->auto_is_implicit_function_template_parm_p
10206 = auto_is_implicit_function_template_parm_p
;
10209 /* This field is only used during parsing of the lambda. */
10210 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10212 /* This lambda shouldn't have any proxies left at this point. */
10213 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10214 /* And now that we're done, push proxies for an enclosing lambda. */
10215 insert_pending_capture_proxies ();
10218 lambda_expr
= build_lambda_object (lambda_expr
);
10220 lambda_expr
= error_mark_node
;
10222 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10224 pop_deferring_access_checks ();
10226 return lambda_expr
;
10229 /* Parse the beginning of a lambda expression.
10232 [ lambda-capture [opt] ]
10234 LAMBDA_EXPR is the current representation of the lambda expression. */
10237 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10239 /* Need commas after the first capture. */
10242 /* Eat the leading `['. */
10243 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10245 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10247 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10248 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10249 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10250 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10252 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10254 cp_lexer_consume_token (parser
->lexer
);
10258 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10260 cp_token
* capture_token
;
10262 tree capture_init_expr
;
10263 cp_id_kind idk
= CP_ID_KIND_NONE
;
10264 bool explicit_init_p
= false;
10266 enum capture_kind_type
10271 enum capture_kind_type capture_kind
= BY_COPY
;
10273 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10275 error ("expected end of capture-list");
10282 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10284 /* Possibly capture `this'. */
10285 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10287 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10288 if (cxx_dialect
< cxx2a
10289 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10290 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10291 "with by-copy capture default");
10292 cp_lexer_consume_token (parser
->lexer
);
10293 add_capture (lambda_expr
,
10294 /*id=*/this_identifier
,
10295 /*initializer=*/finish_this_expr (),
10296 /*by_reference_p=*/true,
10301 /* Possibly capture `*this'. */
10302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10303 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10305 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10306 if (cxx_dialect
< cxx17
)
10307 pedwarn (loc
, 0, "%<*this%> capture only available with "
10308 "-std=c++17 or -std=gnu++17");
10309 cp_lexer_consume_token (parser
->lexer
);
10310 cp_lexer_consume_token (parser
->lexer
);
10311 add_capture (lambda_expr
,
10312 /*id=*/this_identifier
,
10313 /*initializer=*/finish_this_expr (),
10314 /*by_reference_p=*/false,
10319 /* Remember whether we want to capture as a reference or not. */
10320 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10322 capture_kind
= BY_REFERENCE
;
10323 cp_lexer_consume_token (parser
->lexer
);
10326 /* Get the identifier. */
10327 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10328 capture_id
= cp_parser_identifier (parser
);
10330 if (capture_id
== error_mark_node
)
10331 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10332 delimiters, but I modified this to stop on unnested ']' as well. It
10333 was already changed to stop on unnested '}', so the
10334 "closing_parenthesis" name is no more misleading with my change. */
10336 cp_parser_skip_to_closing_parenthesis (parser
,
10337 /*recovering=*/true,
10339 /*consume_paren=*/true);
10343 /* Find the initializer for this capture. */
10344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10345 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10346 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10348 bool direct
, non_constant
;
10349 /* An explicit initializer exists. */
10350 if (cxx_dialect
< cxx14
)
10351 pedwarn (input_location
, 0,
10352 "lambda capture initializers "
10353 "only available with -std=c++14 or -std=gnu++14");
10354 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10355 &non_constant
, true);
10356 explicit_init_p
= true;
10357 if (capture_init_expr
== NULL_TREE
)
10359 error ("empty initializer for lambda init-capture");
10360 capture_init_expr
= error_mark_node
;
10365 const char* error_msg
;
10367 /* Turn the identifier into an id-expression. */
10369 = cp_parser_lookup_name_simple (parser
, capture_id
,
10370 capture_token
->location
);
10372 if (capture_init_expr
== error_mark_node
)
10374 unqualified_name_lookup_error (capture_id
);
10377 else if (!VAR_P (capture_init_expr
)
10378 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10380 error_at (capture_token
->location
,
10381 "capture of non-variable %qE",
10382 capture_init_expr
);
10383 if (DECL_P (capture_init_expr
))
10384 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10385 "%q#D declared here", capture_init_expr
);
10388 if (VAR_P (capture_init_expr
)
10389 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10391 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10392 "%qD with non-automatic storage duration",
10393 capture_init_expr
))
10394 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10395 "%q#D declared here", capture_init_expr
);
10400 = finish_id_expression
10405 /*integral_constant_expression_p=*/false,
10406 /*allow_non_integral_constant_expression_p=*/false,
10407 /*non_integral_constant_expression_p=*/NULL
,
10408 /*template_p=*/false,
10410 /*address_p=*/false,
10411 /*template_arg_p=*/false,
10413 capture_token
->location
);
10415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10417 cp_lexer_consume_token (parser
->lexer
);
10418 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10422 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10423 && !explicit_init_p
)
10425 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10426 && capture_kind
== BY_COPY
)
10427 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10428 "of %qD redundant with by-copy capture default",
10430 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10431 && capture_kind
== BY_REFERENCE
)
10432 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10433 "capture of %qD redundant with by-reference capture "
10434 "default", capture_id
);
10437 add_capture (lambda_expr
,
10440 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10443 /* If there is any qualification still in effect, clear it
10444 now; we will be starting fresh with the next capture. */
10445 parser
->scope
= NULL_TREE
;
10446 parser
->qualifying_scope
= NULL_TREE
;
10447 parser
->object_scope
= NULL_TREE
;
10450 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10453 /* Parse the (optional) middle of a lambda expression.
10456 < template-parameter-list [opt] >
10457 ( parameter-declaration-clause [opt] )
10458 attribute-specifier [opt]
10459 decl-specifier-seq [opt]
10460 exception-specification [opt]
10461 lambda-return-type-clause [opt]
10463 LAMBDA_EXPR is the current representation of the lambda expression. */
10466 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10468 /* 5.1.1.4 of the standard says:
10469 If a lambda-expression does not include a lambda-declarator, it is as if
10470 the lambda-declarator were ().
10471 This means an empty parameter list, no attributes, and no exception
10473 tree param_list
= void_list_node
;
10474 tree attributes
= NULL_TREE
;
10475 tree exception_spec
= NULL_TREE
;
10476 tree template_param_list
= NULL_TREE
;
10477 tree tx_qual
= NULL_TREE
;
10478 tree return_type
= NULL_TREE
;
10479 cp_decl_specifier_seq lambda_specs
;
10480 clear_decl_specs (&lambda_specs
);
10482 /* The template-parameter-list is optional, but must begin with
10483 an opening angle if present. */
10484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10486 if (cxx_dialect
< cxx14
)
10487 pedwarn (parser
->lexer
->next_token
->location
, 0,
10488 "lambda templates are only available with "
10489 "-std=c++14 or -std=gnu++14");
10490 else if (cxx_dialect
< cxx2a
)
10491 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10492 "lambda templates are only available with "
10493 "-std=c++2a or -std=gnu++2a");
10495 cp_lexer_consume_token (parser
->lexer
);
10497 template_param_list
= cp_parser_template_parameter_list (parser
);
10499 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10501 /* We just processed one more parameter list. */
10502 ++parser
->num_template_parameter_lists
;
10505 /* The parameter-declaration-clause is optional (unless
10506 template-parameter-list was given), but must begin with an
10507 opening parenthesis if present. */
10508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10510 matching_parens parens
;
10511 parens
.consume_open (parser
);
10513 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10515 /* Parse parameters. */
10516 param_list
= cp_parser_parameter_declaration_clause (parser
);
10518 /* Default arguments shall not be specified in the
10519 parameter-declaration-clause of a lambda-declarator. */
10520 if (cxx_dialect
< cxx14
)
10521 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10522 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10523 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10524 "default argument specified for lambda parameter");
10526 parens
.require_close (parser
);
10528 attributes
= cp_parser_attributes_opt (parser
);
10530 /* In the decl-specifier-seq of the lambda-declarator, each
10531 decl-specifier shall either be mutable or constexpr. */
10532 int declares_class_or_enum
;
10533 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10534 cp_parser_decl_specifier_seq (parser
,
10535 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10536 &lambda_specs
, &declares_class_or_enum
);
10537 if (lambda_specs
.storage_class
== sc_mutable
)
10539 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10540 if (lambda_specs
.conflicting_specifiers_p
)
10541 error_at (lambda_specs
.locations
[ds_storage_class
],
10542 "duplicate %<mutable%>");
10545 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10547 /* Parse optional exception specification. */
10548 exception_spec
= cp_parser_exception_specification_opt (parser
);
10550 /* Parse optional trailing return type. */
10551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10553 cp_lexer_consume_token (parser
->lexer
);
10554 return_type
= cp_parser_trailing_type_id (parser
);
10557 /* The function parameters must be in scope all the way until after the
10558 trailing-return-type in case of decltype. */
10559 pop_bindings_and_leave_scope ();
10561 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10562 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10564 /* Create the function call operator.
10566 Messing with declarators like this is no uglier than building up the
10567 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10570 cp_decl_specifier_seq return_type_specs
;
10571 cp_declarator
* declarator
;
10576 clear_decl_specs (&return_type_specs
);
10577 return_type_specs
.type
= make_auto ();
10579 if (lambda_specs
.locations
[ds_constexpr
])
10581 if (cxx_dialect
>= cxx17
)
10582 return_type_specs
.locations
[ds_constexpr
]
10583 = lambda_specs
.locations
[ds_constexpr
];
10585 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10586 "lambda only available with -std=c++17 or -std=gnu++17");
10589 p
= obstack_alloc (&declarator_obstack
, 0);
10591 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10593 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10594 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10595 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10596 VIRT_SPEC_UNSPECIFIED
,
10600 /*late_return_type=*/NULL_TREE
,
10601 /*requires_clause*/NULL_TREE
);
10602 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10604 declarator
->u
.function
.late_return_type
= return_type
;
10606 fco
= grokmethod (&return_type_specs
,
10609 if (fco
!= error_mark_node
)
10611 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10612 DECL_ARTIFICIAL (fco
) = 1;
10613 /* Give the object parameter a different name. */
10614 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10616 if (template_param_list
)
10618 fco
= finish_member_template_decl (fco
);
10619 finish_template_decl (template_param_list
);
10620 --parser
->num_template_parameter_lists
;
10622 else if (parser
->fully_implicit_function_template_p
)
10623 fco
= finish_fully_implicit_template (parser
, fco
);
10625 finish_member_declaration (fco
);
10627 obstack_free (&declarator_obstack
, p
);
10629 return (fco
!= error_mark_node
);
10633 /* Parse the body of a lambda expression, which is simply
10637 but which requires special handling.
10638 LAMBDA_EXPR is the current representation of the lambda expression. */
10641 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10643 bool nested
= (current_function_decl
!= NULL_TREE
);
10644 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10645 bool in_function_body
= parser
->in_function_body
;
10648 push_function_context ();
10650 /* Still increment function_depth so that we don't GC in the
10651 middle of an expression. */
10654 vec
<tree
> omp_privatization_save
;
10655 save_omp_privatization_clauses (omp_privatization_save
);
10656 /* Clear this in case we're in the middle of a default argument. */
10657 parser
->local_variables_forbidden_p
= false;
10658 parser
->in_function_body
= true;
10661 local_specialization_stack
s (lss_copy
);
10662 tree fco
= lambda_function (lambda_expr
);
10663 tree body
= start_lambda_function (fco
, lambda_expr
);
10664 matching_braces braces
;
10666 if (braces
.require_open (parser
))
10668 tree compound_stmt
= begin_compound_stmt (0);
10670 /* Originally C++11 required us to peek for 'return expr'; and
10671 process it specially here to deduce the return type. N3638
10672 removed the need for that. */
10674 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10675 cp_parser_label_declaration (parser
);
10676 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10677 braces
.require_close (parser
);
10679 finish_compound_stmt (compound_stmt
);
10682 finish_lambda_function (body
);
10685 restore_omp_privatization_clauses (omp_privatization_save
);
10686 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10687 parser
->in_function_body
= in_function_body
;
10689 pop_function_context();
10694 /* Statements [gram.stmt.stmt] */
10696 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10699 add_debug_begin_stmt (location_t loc
)
10701 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10703 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10704 /* A concept is never expanded normally. */
10707 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10708 SET_EXPR_LOCATION (stmt
, loc
);
10712 /* Parse a statement.
10716 expression-statement
10718 selection-statement
10719 iteration-statement
10721 declaration-statement
10728 attribute-specifier-seq (opt) expression-statement
10729 attribute-specifier-seq (opt) compound-statement
10730 attribute-specifier-seq (opt) selection-statement
10731 attribute-specifier-seq (opt) iteration-statement
10732 attribute-specifier-seq (opt) jump-statement
10733 declaration-statement
10734 attribute-specifier-seq (opt) try-block
10737 expression-statement
10745 IN_COMPOUND is true when the statement is nested inside a
10746 cp_parser_compound_statement; this matters for certain pragmas.
10748 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10749 is a (possibly labeled) if statement which is not enclosed in braces
10750 and has an else clause. This is used to implement -Wparentheses.
10752 CHAIN is a vector of if-else-if conditions. */
10755 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10756 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10757 location_t
*loc_after_labels
)
10759 tree statement
, std_attrs
= NULL_TREE
;
10761 location_t statement_location
, attrs_location
;
10766 /* There is no statement yet. */
10767 statement
= NULL_TREE
;
10769 saved_token_sentinel
saved_tokens (parser
->lexer
);
10770 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10771 if (c_dialect_objc ())
10772 /* In obj-c++, seeing '[[' might be the either the beginning of
10773 c++11 attributes, or a nested objc-message-expression. So
10774 let's parse the c++11 attributes tentatively. */
10775 cp_parser_parse_tentatively (parser
);
10776 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10777 if (c_dialect_objc ())
10779 if (!cp_parser_parse_definitely (parser
))
10780 std_attrs
= NULL_TREE
;
10783 /* Peek at the next token. */
10784 token
= cp_lexer_peek_token (parser
->lexer
);
10785 /* Remember the location of the first token in the statement. */
10786 statement_location
= token
->location
;
10787 add_debug_begin_stmt (statement_location
);
10788 /* If this is a keyword, then that will often determine what kind of
10789 statement we have. */
10790 if (token
->type
== CPP_KEYWORD
)
10792 enum rid keyword
= token
->keyword
;
10798 /* Looks like a labeled-statement with a case label.
10799 Parse the label, and then use tail recursion to parse
10801 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10802 in_compound
= false;
10807 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10813 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10820 statement
= cp_parser_jump_statement (parser
);
10823 /* Objective-C++ exception-handling constructs. */
10826 case RID_AT_FINALLY
:
10827 case RID_AT_SYNCHRONIZED
:
10829 statement
= cp_parser_objc_statement (parser
);
10833 statement
= cp_parser_try_block (parser
);
10836 case RID_NAMESPACE
:
10837 /* This must be a namespace alias definition. */
10838 cp_parser_declaration_statement (parser
);
10841 case RID_TRANSACTION_ATOMIC
:
10842 case RID_TRANSACTION_RELAXED
:
10843 case RID_SYNCHRONIZED
:
10844 case RID_ATOMIC_NOEXCEPT
:
10845 case RID_ATOMIC_CANCEL
:
10846 statement
= cp_parser_transaction (parser
, token
);
10848 case RID_TRANSACTION_CANCEL
:
10849 statement
= cp_parser_transaction_cancel (parser
);
10853 /* It might be a keyword like `int' that can start a
10854 declaration-statement. */
10858 else if (token
->type
== CPP_NAME
)
10860 /* If the next token is a `:', then we are looking at a
10861 labeled-statement. */
10862 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10863 if (token
->type
== CPP_COLON
)
10865 /* Looks like a labeled-statement with an ordinary label.
10866 Parse the label, and then use tail recursion to parse
10869 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10870 in_compound
= false;
10874 /* Anything that starts with a `{' must be a compound-statement. */
10875 else if (token
->type
== CPP_OPEN_BRACE
)
10876 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10877 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10878 a statement all its own. */
10879 else if (token
->type
== CPP_PRAGMA
)
10881 /* Only certain OpenMP pragmas are attached to statements, and thus
10882 are considered statements themselves. All others are not. In
10883 the context of a compound, accept the pragma as a "statement" and
10884 return so that we can check for a close brace. Otherwise we
10885 require a real statement and must go back and read one. */
10887 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10888 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10892 else if (token
->type
== CPP_EOF
)
10894 cp_parser_error (parser
, "expected statement");
10898 /* Everything else must be a declaration-statement or an
10899 expression-statement. Try for the declaration-statement
10900 first, unless we are looking at a `;', in which case we know that
10901 we have an expression-statement. */
10904 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10906 if (std_attrs
!= NULL_TREE
)
10908 /* Attributes should be parsed as part of the the
10909 declaration, so let's un-parse them. */
10910 saved_tokens
.rollback();
10911 std_attrs
= NULL_TREE
;
10914 cp_parser_parse_tentatively (parser
);
10915 /* Try to parse the declaration-statement. */
10916 cp_parser_declaration_statement (parser
);
10917 /* If that worked, we're done. */
10918 if (cp_parser_parse_definitely (parser
))
10921 /* All preceding labels have been parsed at this point. */
10922 if (loc_after_labels
!= NULL
)
10923 *loc_after_labels
= statement_location
;
10925 /* Look for an expression-statement instead. */
10926 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10928 /* Handle [[fallthrough]];. */
10929 if (attribute_fallthrough_p (std_attrs
))
10931 /* The next token after the fallthrough attribute is ';'. */
10932 if (statement
== NULL_TREE
)
10934 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10935 statement
= build_call_expr_internal_loc (statement_location
,
10937 void_type_node
, 0);
10938 finish_expr_stmt (statement
);
10941 warning_at (statement_location
, OPT_Wattributes
,
10942 "%<fallthrough%> attribute not followed by %<;%>");
10943 std_attrs
= NULL_TREE
;
10947 /* Set the line number for the statement. */
10948 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10949 SET_EXPR_LOCATION (statement
, statement_location
);
10951 /* Allow "[[fallthrough]];", but warn otherwise. */
10952 if (std_attrs
!= NULL_TREE
)
10953 warning_at (attrs_location
,
10955 "attributes at the beginning of statement are ignored");
10958 /* Append ATTR to attribute list ATTRS. */
10961 attr_chainon (tree attrs
, tree attr
)
10963 if (attrs
== error_mark_node
)
10964 return error_mark_node
;
10965 if (attr
== error_mark_node
)
10966 return error_mark_node
;
10967 return chainon (attrs
, attr
);
10970 /* Parse the label for a labeled-statement, i.e.
10973 case constant-expression :
10977 case constant-expression ... constant-expression : statement
10979 When a label is parsed without errors, the label is added to the
10980 parse tree by the finish_* functions, so this function doesn't
10981 have to return the label. */
10984 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10987 tree label
= NULL_TREE
;
10988 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10990 /* The next token should be an identifier. */
10991 token
= cp_lexer_peek_token (parser
->lexer
);
10992 if (token
->type
!= CPP_NAME
10993 && token
->type
!= CPP_KEYWORD
)
10995 cp_parser_error (parser
, "expected labeled-statement");
10999 /* Remember whether this case or a user-defined label is allowed to fall
11001 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11003 parser
->colon_corrects_to_scope_p
= false;
11004 switch (token
->keyword
)
11008 tree expr
, expr_hi
;
11009 cp_token
*ellipsis
;
11011 /* Consume the `case' token. */
11012 cp_lexer_consume_token (parser
->lexer
);
11013 /* Parse the constant-expression. */
11014 expr
= cp_parser_constant_expression (parser
);
11015 if (check_for_bare_parameter_packs (expr
))
11016 expr
= error_mark_node
;
11018 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11019 if (ellipsis
->type
== CPP_ELLIPSIS
)
11021 /* Consume the `...' token. */
11022 cp_lexer_consume_token (parser
->lexer
);
11023 expr_hi
= cp_parser_constant_expression (parser
);
11024 if (check_for_bare_parameter_packs (expr_hi
))
11025 expr_hi
= error_mark_node
;
11027 /* We don't need to emit warnings here, as the common code
11028 will do this for us. */
11031 expr_hi
= NULL_TREE
;
11033 if (parser
->in_switch_statement_p
)
11035 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11036 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11037 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11040 error_at (token
->location
,
11041 "case label %qE not within a switch statement",
11047 /* Consume the `default' token. */
11048 cp_lexer_consume_token (parser
->lexer
);
11050 if (parser
->in_switch_statement_p
)
11052 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11053 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11054 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11057 error_at (token
->location
, "case label not within a switch statement");
11061 /* Anything else must be an ordinary label. */
11062 label
= finish_label_stmt (cp_parser_identifier (parser
));
11063 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11064 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11068 /* Require the `:' token. */
11069 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11071 /* An ordinary label may optionally be followed by attributes.
11072 However, this is only permitted if the attributes are then
11073 followed by a semicolon. This is because, for backward
11074 compatibility, when parsing
11075 lab: __attribute__ ((unused)) int i;
11076 we want the attribute to attach to "i", not "lab". */
11077 if (label
!= NULL_TREE
11078 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11081 cp_parser_parse_tentatively (parser
);
11082 attrs
= cp_parser_gnu_attributes_opt (parser
);
11083 if (attrs
== NULL_TREE
11084 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11085 cp_parser_abort_tentative_parse (parser
);
11086 else if (!cp_parser_parse_definitely (parser
))
11089 attributes
= attr_chainon (attributes
, attrs
);
11092 if (attributes
!= NULL_TREE
)
11093 cplus_decl_attributes (&label
, attributes
, 0);
11095 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11098 /* Parse an expression-statement.
11100 expression-statement:
11103 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11104 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11105 indicates whether this expression-statement is part of an
11106 expression statement. */
11109 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11111 tree statement
= NULL_TREE
;
11112 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11113 location_t loc
= token
->location
;
11115 /* There might be attribute fallthrough. */
11116 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11118 /* If the next token is a ';', then there is no expression
11120 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11122 statement
= cp_parser_expression (parser
);
11123 if (statement
== error_mark_node
11124 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11126 cp_parser_skip_to_end_of_block_or_statement (parser
);
11127 return error_mark_node
;
11131 /* Handle [[fallthrough]];. */
11132 if (attribute_fallthrough_p (attr
))
11134 /* The next token after the fallthrough attribute is ';'. */
11135 if (statement
== NULL_TREE
)
11136 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11137 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11138 void_type_node
, 0);
11140 warning_at (loc
, OPT_Wattributes
,
11141 "%<fallthrough%> attribute not followed by %<;%>");
11145 /* Allow "[[fallthrough]];", but warn otherwise. */
11146 if (attr
!= NULL_TREE
)
11147 warning_at (loc
, OPT_Wattributes
,
11148 "attributes at the beginning of statement are ignored");
11150 /* Give a helpful message for "A<T>::type t;" and the like. */
11151 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11152 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11154 if (TREE_CODE (statement
) == SCOPE_REF
)
11155 error_at (token
->location
, "need %<typename%> before %qE because "
11156 "%qT is a dependent scope",
11157 statement
, TREE_OPERAND (statement
, 0));
11158 else if (is_overloaded_fn (statement
)
11159 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11162 tree fn
= get_first_fn (statement
);
11163 error_at (token
->location
,
11164 "%<%T::%D%> names the constructor, not the type",
11165 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11169 /* Consume the final `;'. */
11170 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11172 if (in_statement_expr
11173 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11174 /* This is the final expression statement of a statement
11176 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11177 else if (statement
)
11178 statement
= finish_expr_stmt (statement
);
11183 /* Parse a compound-statement.
11185 compound-statement:
11186 { statement-seq [opt] }
11190 compound-statement:
11191 { label-declaration-seq [opt] statement-seq [opt] }
11193 label-declaration-seq:
11195 label-declaration-seq label-declaration
11197 Returns a tree representing the statement. */
11200 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11201 int bcs_flags
, bool function_body
)
11203 tree compound_stmt
;
11204 matching_braces braces
;
11206 /* Consume the `{'. */
11207 if (!braces
.require_open (parser
))
11208 return error_mark_node
;
11209 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11210 && !function_body
&& cxx_dialect
< cxx14
)
11211 pedwarn (input_location
, OPT_Wpedantic
,
11212 "compound-statement in %<constexpr%> function");
11213 /* Begin the compound-statement. */
11214 compound_stmt
= begin_compound_stmt (bcs_flags
);
11215 /* If the next keyword is `__label__' we have a label declaration. */
11216 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11217 cp_parser_label_declaration (parser
);
11218 /* Parse an (optional) statement-seq. */
11219 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11220 /* Finish the compound-statement. */
11221 finish_compound_stmt (compound_stmt
);
11222 /* Consume the `}'. */
11223 braces
.require_close (parser
);
11225 return compound_stmt
;
11228 /* Parse an (optional) statement-seq.
11232 statement-seq [opt] statement */
11235 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11237 /* Scan statements until there aren't any more. */
11240 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11242 /* If we are looking at a `}', then we have run out of
11243 statements; the same is true if we have reached the end
11244 of file, or have stumbled upon a stray '@end'. */
11245 if (token
->type
== CPP_CLOSE_BRACE
11246 || token
->type
== CPP_EOF
11247 || token
->type
== CPP_PRAGMA_EOL
11248 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11251 /* If we are in a compound statement and find 'else' then
11252 something went wrong. */
11253 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11255 if (parser
->in_statement
& IN_IF_STMT
)
11259 token
= cp_lexer_consume_token (parser
->lexer
);
11260 error_at (token
->location
, "%<else%> without a previous %<if%>");
11264 /* Parse the statement. */
11265 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11269 /* Return true if this is the C++20 version of range-based-for with
11273 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11277 /* Save tokens so that we can put them back. */
11278 cp_lexer_save_tokens (parser
->lexer
);
11280 /* There has to be an unnested ; followed by an unnested :. */
11281 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11282 /*recovering=*/false,
11284 /*consume_paren=*/false) != -1)
11287 /* We found the semicolon, eat it now. */
11288 cp_lexer_consume_token (parser
->lexer
);
11290 /* Now look for ':' that is not nested in () or {}. */
11291 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11292 /*recovering=*/false,
11294 /*consume_paren=*/false) == -1);
11297 /* Roll back the tokens we skipped. */
11298 cp_lexer_rollback_tokens (parser
->lexer
);
11303 /* Return true if we're looking at (init; cond), false otherwise. */
11306 cp_parser_init_statement_p (cp_parser
*parser
)
11308 /* Save tokens so that we can put them back. */
11309 cp_lexer_save_tokens (parser
->lexer
);
11311 /* Look for ';' that is not nested in () or {}. */
11312 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11313 /*recovering=*/false,
11315 /*consume_paren=*/false);
11317 /* Roll back the tokens we skipped. */
11318 cp_lexer_rollback_tokens (parser
->lexer
);
11323 /* Parse a selection-statement.
11325 selection-statement:
11326 if ( init-statement [opt] condition ) statement
11327 if ( init-statement [opt] condition ) statement else statement
11328 switch ( init-statement [opt] condition ) statement
11330 Returns the new IF_STMT or SWITCH_STMT.
11332 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11333 is a (possibly labeled) if statement which is not enclosed in
11334 braces and has an else clause. This is used to implement
11337 CHAIN is a vector of if-else-if conditions. This is used to implement
11338 -Wduplicated-cond. */
11341 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11346 token_indent_info guard_tinfo
;
11351 /* Peek at the next token. */
11352 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11353 guard_tinfo
= get_token_indent_info (token
);
11355 /* See what kind of keyword it is. */
11356 keyword
= token
->keyword
;
11366 if (keyword
== RID_IF
11367 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11371 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11372 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11373 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11374 "with -std=c++17 or -std=gnu++17");
11377 /* Look for the `('. */
11378 matching_parens parens
;
11379 if (!parens
.require_open (parser
))
11381 cp_parser_skip_to_end_of_statement (parser
);
11382 return error_mark_node
;
11385 /* Begin the selection-statement. */
11386 if (keyword
== RID_IF
)
11388 statement
= begin_if_stmt ();
11389 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11392 statement
= begin_switch_stmt ();
11394 /* Parse the optional init-statement. */
11395 if (cp_parser_init_statement_p (parser
))
11398 if (cxx_dialect
< cxx17
)
11399 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11400 "init-statement in selection statements only available "
11401 "with -std=c++17 or -std=gnu++17");
11402 cp_parser_init_statement (parser
, &decl
);
11405 /* Parse the condition. */
11406 condition
= cp_parser_condition (parser
);
11407 /* Look for the `)'. */
11408 if (!parens
.require_close (parser
))
11409 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11410 /*consume_paren=*/true);
11412 if (keyword
== RID_IF
)
11415 unsigned char in_statement
;
11417 /* Add the condition. */
11418 condition
= finish_if_stmt_cond (condition
, statement
);
11420 if (warn_duplicated_cond
)
11421 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11424 /* Parse the then-clause. */
11425 in_statement
= parser
->in_statement
;
11426 parser
->in_statement
|= IN_IF_STMT
;
11428 /* Outside a template, the non-selected branch of a constexpr
11429 if is a 'discarded statement', i.e. unevaluated. */
11430 bool was_discarded
= in_discarded_stmt
;
11431 bool discard_then
= (cx
&& !processing_template_decl
11432 && integer_zerop (condition
));
11435 in_discarded_stmt
= true;
11436 ++c_inhibit_evaluation_warnings
;
11439 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11442 parser
->in_statement
= in_statement
;
11444 finish_then_clause (statement
);
11448 THEN_CLAUSE (statement
) = NULL_TREE
;
11449 in_discarded_stmt
= was_discarded
;
11450 --c_inhibit_evaluation_warnings
;
11453 /* If the next token is `else', parse the else-clause. */
11454 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11457 bool discard_else
= (cx
&& !processing_template_decl
11458 && integer_nonzerop (condition
));
11461 in_discarded_stmt
= true;
11462 ++c_inhibit_evaluation_warnings
;
11466 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11467 /* Consume the `else' keyword. */
11468 cp_lexer_consume_token (parser
->lexer
);
11469 if (warn_duplicated_cond
)
11471 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11475 /* We've got "if (COND) else if (COND2)". Start
11476 the condition chain and add COND as the first
11478 chain
= new vec
<tree
> ();
11479 if (!CONSTANT_CLASS_P (condition
)
11480 && !TREE_SIDE_EFFECTS (condition
))
11482 /* Wrap it in a NOP_EXPR so that we can set the
11483 location of the condition. */
11484 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11486 SET_EXPR_LOCATION (e
, token
->location
);
11487 chain
->safe_push (e
);
11490 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11493 /* This is if-else without subsequent if. Zap the
11494 condition chain; we would have already warned at
11500 begin_else_clause (statement
);
11501 /* Parse the else-clause. */
11502 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11503 guard_tinfo
, chain
);
11505 finish_else_clause (statement
);
11507 /* If we are currently parsing a then-clause, then
11508 IF_P will not be NULL. We set it to true to
11509 indicate that this if statement has an else clause.
11510 This may trigger the Wparentheses warning below
11511 when we get back up to the parent if statement. */
11517 ELSE_CLAUSE (statement
) = NULL_TREE
;
11518 in_discarded_stmt
= was_discarded
;
11519 --c_inhibit_evaluation_warnings
;
11524 /* This if statement does not have an else clause. If
11525 NESTED_IF is true, then the then-clause has an if
11526 statement which does have an else clause. We warn
11527 about the potential ambiguity. */
11529 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11530 "suggest explicit braces to avoid ambiguous"
11532 if (warn_duplicated_cond
)
11534 /* We don't need the condition chain anymore. */
11540 /* Now we're all done with the if-statement. */
11541 finish_if_stmt (statement
);
11545 bool in_switch_statement_p
;
11546 unsigned char in_statement
;
11548 /* Add the condition. */
11549 finish_switch_cond (condition
, statement
);
11551 /* Parse the body of the switch-statement. */
11552 in_switch_statement_p
= parser
->in_switch_statement_p
;
11553 in_statement
= parser
->in_statement
;
11554 parser
->in_switch_statement_p
= true;
11555 parser
->in_statement
|= IN_SWITCH_STMT
;
11556 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11558 parser
->in_switch_statement_p
= in_switch_statement_p
;
11559 parser
->in_statement
= in_statement
;
11561 /* Now we're all done with the switch-statement. */
11562 finish_switch_stmt (statement
);
11570 cp_parser_error (parser
, "expected selection-statement");
11571 return error_mark_node
;
11575 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11576 If we have seen at least one decl-specifier, and the next token
11577 is not a parenthesis, then we must be looking at a declaration.
11578 (After "int (" we might be looking at a functional cast.) */
11581 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11582 bool any_specifiers_p
)
11584 if (any_specifiers_p
11585 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11586 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11587 && !cp_parser_error_occurred (parser
))
11588 cp_parser_commit_to_tentative_parse (parser
);
11591 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11592 The declarator shall not specify a function or an array. Returns
11593 TRUE if the declarator is valid, FALSE otherwise. */
11596 cp_parser_check_condition_declarator (cp_parser
* parser
,
11597 cp_declarator
*declarator
,
11600 if (function_declarator_p (declarator
)
11601 || declarator
->kind
== cdk_array
)
11603 if (declarator
->kind
== cdk_array
)
11604 error_at (loc
, "condition declares an array");
11606 error_at (loc
, "condition declares a function");
11607 if (parser
->fully_implicit_function_template_p
)
11608 abort_fully_implicit_template (parser
);
11609 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11610 /*or_comma=*/false,
11611 /*consume_paren=*/false);
11618 /* Parse a condition.
11622 type-specifier-seq declarator = initializer-clause
11623 type-specifier-seq declarator braced-init-list
11628 type-specifier-seq declarator asm-specification [opt]
11629 attributes [opt] = assignment-expression
11631 Returns the expression that should be tested. */
11634 cp_parser_condition (cp_parser
* parser
)
11636 cp_decl_specifier_seq type_specifiers
;
11637 const char *saved_message
;
11638 int declares_class_or_enum
;
11640 /* Try the declaration first. */
11641 cp_parser_parse_tentatively (parser
);
11642 /* New types are not allowed in the type-specifier-seq for a
11644 saved_message
= parser
->type_definition_forbidden_message
;
11645 parser
->type_definition_forbidden_message
11646 = G_("types may not be defined in conditions");
11647 /* Parse the type-specifier-seq. */
11648 cp_parser_decl_specifier_seq (parser
,
11649 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11651 &declares_class_or_enum
);
11652 /* Restore the saved message. */
11653 parser
->type_definition_forbidden_message
= saved_message
;
11655 cp_parser_maybe_commit_to_declaration (parser
,
11656 type_specifiers
.any_specifiers_p
);
11658 /* If all is well, we might be looking at a declaration. */
11659 if (!cp_parser_error_occurred (parser
))
11662 tree asm_specification
;
11664 cp_declarator
*declarator
;
11665 tree initializer
= NULL_TREE
;
11666 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11668 /* Parse the declarator. */
11669 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11670 /*ctor_dtor_or_conv_p=*/NULL
,
11671 /*parenthesized_p=*/NULL
,
11672 /*member_p=*/false,
11673 /*friend_p=*/false);
11674 /* Parse the attributes. */
11675 attributes
= cp_parser_attributes_opt (parser
);
11676 /* Parse the asm-specification. */
11677 asm_specification
= cp_parser_asm_specification_opt (parser
);
11678 /* If the next token is not an `=' or '{', then we might still be
11679 looking at an expression. For example:
11683 looks like a decl-specifier-seq and a declarator -- but then
11684 there is no `=', so this is an expression. */
11685 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11686 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11687 cp_parser_simulate_error (parser
);
11689 /* If we did see an `=' or '{', then we are looking at a declaration
11691 if (cp_parser_parse_definitely (parser
))
11694 bool non_constant_p
;
11695 int flags
= LOOKUP_ONLYCONVERTING
;
11697 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11698 return error_mark_node
;
11700 /* Create the declaration. */
11701 decl
= start_decl (declarator
, &type_specifiers
,
11702 /*initialized_p=*/true,
11703 attributes
, /*prefix_attributes=*/NULL_TREE
,
11706 /* Parse the initializer. */
11707 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11709 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11710 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11713 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11715 /* Consume the `='. */
11716 cp_lexer_consume_token (parser
->lexer
);
11717 initializer
= cp_parser_initializer_clause (parser
,
11722 cp_parser_error (parser
, "expected initializer");
11723 initializer
= error_mark_node
;
11725 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11726 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11728 /* Process the initializer. */
11729 cp_finish_decl (decl
,
11730 initializer
, !non_constant_p
,
11735 pop_scope (pushed_scope
);
11737 return convert_from_reference (decl
);
11740 /* If we didn't even get past the declarator successfully, we are
11741 definitely not looking at a declaration. */
11743 cp_parser_abort_tentative_parse (parser
);
11745 /* Otherwise, we are looking at an expression. */
11746 return cp_parser_expression (parser
);
11749 /* Parses a for-statement or range-for-statement until the closing ')',
11753 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11755 tree init
, scope
, decl
;
11758 /* Begin the for-statement. */
11759 scope
= begin_for_scope (&init
);
11761 /* Parse the initialization. */
11762 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11765 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11767 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11771 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11772 unsigned short unroll
)
11774 /* Normal for loop */
11775 tree condition
= NULL_TREE
;
11776 tree expression
= NULL_TREE
;
11779 stmt
= begin_for_stmt (scope
, init
);
11780 /* The init-statement has already been parsed in
11781 cp_parser_init_statement, so no work is needed here. */
11782 finish_init_stmt (stmt
);
11784 /* If there's a condition, process it. */
11785 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11786 condition
= cp_parser_condition (parser
);
11789 cp_parser_error (parser
, "missing loop condition in loop with "
11790 "%<GCC ivdep%> pragma");
11791 condition
= error_mark_node
;
11795 cp_parser_error (parser
, "missing loop condition in loop with "
11796 "%<GCC unroll%> pragma");
11797 condition
= error_mark_node
;
11799 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11800 /* Look for the `;'. */
11801 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11803 /* If there's an expression, process it. */
11804 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11805 expression
= cp_parser_expression (parser
);
11806 finish_for_expr (expression
, stmt
);
11811 /* Tries to parse a range-based for-statement:
11814 decl-specifier-seq declarator : expression
11816 The decl-specifier-seq declarator and the `:' are already parsed by
11817 cp_parser_init_statement. If processing_template_decl it returns a
11818 newly created RANGE_FOR_STMT; if not, it is converted to a
11819 regular FOR_STMT. */
11822 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11823 bool ivdep
, unsigned short unroll
)
11825 tree stmt
, range_expr
;
11826 auto_vec
<cxx_binding
*, 16> bindings
;
11827 auto_vec
<tree
, 16> names
;
11828 tree decomp_first_name
= NULL_TREE
;
11829 unsigned int decomp_cnt
= 0;
11831 /* Get the range declaration momentarily out of the way so that
11832 the range expression doesn't clash with it. */
11833 if (range_decl
!= error_mark_node
)
11835 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11837 tree v
= DECL_VALUE_EXPR (range_decl
);
11838 /* For decomposition declaration get all of the corresponding
11839 declarations out of the way. */
11840 if (TREE_CODE (v
) == ARRAY_REF
11841 && VAR_P (TREE_OPERAND (v
, 0))
11842 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11844 tree d
= range_decl
;
11845 range_decl
= TREE_OPERAND (v
, 0);
11846 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11847 decomp_first_name
= d
;
11848 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11850 tree name
= DECL_NAME (d
);
11851 names
.safe_push (name
);
11852 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11853 IDENTIFIER_BINDING (name
)
11854 = IDENTIFIER_BINDING (name
)->previous
;
11858 if (names
.is_empty ())
11860 tree name
= DECL_NAME (range_decl
);
11861 names
.safe_push (name
);
11862 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11863 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11867 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11869 bool expr_non_constant_p
;
11870 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11873 range_expr
= cp_parser_expression (parser
);
11875 /* Put the range declaration(s) back into scope. */
11876 for (unsigned int i
= 0; i
< names
.length (); i
++)
11878 cxx_binding
*binding
= bindings
[i
];
11879 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11880 IDENTIFIER_BINDING (names
[i
]) = binding
;
11883 /* If in template, STMT is converted to a normal for-statement
11884 at instantiation. If not, it is done just ahead. */
11885 if (processing_template_decl
)
11887 if (check_for_bare_parameter_packs (range_expr
))
11888 range_expr
= error_mark_node
;
11889 stmt
= begin_range_for_stmt (scope
, init
);
11891 RANGE_FOR_IVDEP (stmt
) = 1;
11893 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11894 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11895 if (!type_dependent_expression_p (range_expr
)
11896 /* do_auto_deduction doesn't mess with template init-lists. */
11897 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11898 do_range_for_auto_deduction (range_decl
, range_expr
);
11902 stmt
= begin_for_stmt (scope
, init
);
11903 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11904 decomp_first_name
, decomp_cnt
, ivdep
,
11910 /* Subroutine of cp_convert_range_for: given the initializer expression,
11911 builds up the range temporary. */
11914 build_range_temp (tree range_expr
)
11916 tree range_type
, range_temp
;
11918 /* Find out the type deduced by the declaration
11919 `auto &&__range = range_expr'. */
11920 range_type
= cp_build_reference_type (make_auto (), true);
11921 range_type
= do_auto_deduction (range_type
, range_expr
,
11922 type_uses_auto (range_type
));
11924 /* Create the __range variable. */
11925 range_temp
= build_decl (input_location
, VAR_DECL
,
11926 get_identifier ("__for_range"), range_type
);
11927 TREE_USED (range_temp
) = 1;
11928 DECL_ARTIFICIAL (range_temp
) = 1;
11933 /* Used by cp_parser_range_for in template context: we aren't going to
11934 do a full conversion yet, but we still need to resolve auto in the
11935 type of the for-range-declaration if present. This is basically
11936 a shortcut version of cp_convert_range_for. */
11939 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11941 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11944 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11945 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11946 iter_type
= (cp_parser_perform_range_for_lookup
11947 (range_temp
, &begin_dummy
, &end_dummy
));
11950 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11952 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11954 tf_warning_or_error
);
11955 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11956 iter_decl
, auto_node
);
11961 /* Converts a range-based for-statement into a normal
11962 for-statement, as per the definition.
11964 for (RANGE_DECL : RANGE_EXPR)
11967 should be equivalent to:
11970 auto &&__range = RANGE_EXPR;
11971 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11975 RANGE_DECL = *__begin;
11980 If RANGE_EXPR is an array:
11981 BEGIN_EXPR = __range
11982 END_EXPR = __range + ARRAY_SIZE(__range)
11983 Else if RANGE_EXPR has a member 'begin' or 'end':
11984 BEGIN_EXPR = __range.begin()
11985 END_EXPR = __range.end()
11987 BEGIN_EXPR = begin(__range)
11988 END_EXPR = end(__range);
11990 If __range has a member 'begin' but not 'end', or vice versa, we must
11991 still use the second alternative (it will surely fail, however).
11992 When calling begin()/end() in the third alternative we must use
11993 argument dependent lookup, but always considering 'std' as an associated
11997 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11998 tree decomp_first_name
, unsigned int decomp_cnt
,
11999 bool ivdep
, unsigned short unroll
)
12002 tree iter_type
, begin_expr
, end_expr
;
12003 tree condition
, expression
;
12005 range_expr
= mark_lvalue_use (range_expr
);
12007 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12008 /* If an error happened previously do nothing or else a lot of
12009 unhelpful errors would be issued. */
12010 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12015 if (VAR_P (range_expr
)
12016 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12017 /* Can't bind a reference to an array of runtime bound. */
12018 range_temp
= range_expr
;
12021 range_temp
= build_range_temp (range_expr
);
12022 pushdecl (range_temp
);
12023 cp_finish_decl (range_temp
, range_expr
,
12024 /*is_constant_init*/false, NULL_TREE
,
12025 LOOKUP_ONLYCONVERTING
);
12026 range_temp
= convert_from_reference (range_temp
);
12028 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12029 &begin_expr
, &end_expr
);
12032 /* The new for initialization statement. */
12033 begin
= build_decl (input_location
, VAR_DECL
,
12034 get_identifier ("__for_begin"), iter_type
);
12035 TREE_USED (begin
) = 1;
12036 DECL_ARTIFICIAL (begin
) = 1;
12038 cp_finish_decl (begin
, begin_expr
,
12039 /*is_constant_init*/false, NULL_TREE
,
12040 LOOKUP_ONLYCONVERTING
);
12042 if (cxx_dialect
>= cxx17
)
12043 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12044 end
= build_decl (input_location
, VAR_DECL
,
12045 get_identifier ("__for_end"), iter_type
);
12046 TREE_USED (end
) = 1;
12047 DECL_ARTIFICIAL (end
) = 1;
12049 cp_finish_decl (end
, end_expr
,
12050 /*is_constant_init*/false, NULL_TREE
,
12051 LOOKUP_ONLYCONVERTING
);
12053 finish_init_stmt (statement
);
12055 /* The new for condition. */
12056 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12059 NULL
, tf_warning_or_error
);
12060 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12062 /* The new increment expression. */
12063 expression
= finish_unary_op_expr (input_location
,
12064 PREINCREMENT_EXPR
, begin
,
12065 tf_warning_or_error
);
12066 finish_for_expr (expression
, statement
);
12068 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12069 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12071 /* The declaration is initialized with *__begin inside the loop body. */
12072 cp_finish_decl (range_decl
,
12073 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12074 tf_warning_or_error
),
12075 /*is_constant_init*/false, NULL_TREE
,
12076 LOOKUP_ONLYCONVERTING
);
12077 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12078 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12083 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12084 We need to solve both at the same time because the method used
12085 depends on the existence of members begin or end.
12086 Returns the type deduced for the iterator expression. */
12089 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12091 if (error_operand_p (range
))
12093 *begin
= *end
= error_mark_node
;
12094 return error_mark_node
;
12097 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12099 error ("range-based %<for%> expression of type %qT "
12100 "has incomplete type", TREE_TYPE (range
));
12101 *begin
= *end
= error_mark_node
;
12102 return error_mark_node
;
12104 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12106 /* If RANGE is an array, we will use pointer arithmetic. */
12107 *begin
= decay_conversion (range
, tf_warning_or_error
);
12108 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12110 array_type_nelts_top (TREE_TYPE (range
)),
12112 return TREE_TYPE (*begin
);
12116 /* If it is not an array, we must do a bit of magic. */
12117 tree id_begin
, id_end
;
12118 tree member_begin
, member_end
;
12120 *begin
= *end
= error_mark_node
;
12122 id_begin
= get_identifier ("begin");
12123 id_end
= get_identifier ("end");
12124 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12125 /*protect=*/2, /*want_type=*/false,
12126 tf_warning_or_error
);
12127 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12128 /*protect=*/2, /*want_type=*/false,
12129 tf_warning_or_error
);
12131 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12133 /* Use the member functions. */
12134 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12135 *end
= cp_parser_range_for_member_function (range
, id_end
);
12139 /* Use global functions with ADL. */
12140 vec
<tree
, va_gc
> *vec
;
12141 vec
= make_tree_vector ();
12143 vec_safe_push (vec
, range
);
12145 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12146 tf_warning_or_error
);
12147 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12148 tf_warning_or_error
);
12149 member_end
= perform_koenig_lookup (id_end
, vec
,
12150 tf_warning_or_error
);
12151 *end
= finish_call_expr (member_end
, &vec
, false, true,
12152 tf_warning_or_error
);
12154 release_tree_vector (vec
);
12157 /* Last common checks. */
12158 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12160 /* If one of the expressions is an error do no more checks. */
12161 *begin
= *end
= error_mark_node
;
12162 return error_mark_node
;
12164 else if (type_dependent_expression_p (*begin
)
12165 || type_dependent_expression_p (*end
))
12166 /* Can happen, when, eg, in a template context, Koenig lookup
12167 can't resolve begin/end (c++/58503). */
12171 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12172 /* The unqualified type of the __begin and __end temporaries should
12173 be the same, as required by the multiple auto declaration. */
12174 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12176 if (cxx_dialect
>= cxx17
12177 && (build_x_binary_op (input_location
, NE_EXPR
,
12178 *begin
, ERROR_MARK
,
12181 != error_mark_node
))
12182 /* P0184R0 allows __begin and __end to have different types,
12183 but make sure they are comparable so we can give a better
12186 error ("inconsistent begin/end types in range-based %<for%> "
12187 "statement: %qT and %qT",
12188 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12195 /* Helper function for cp_parser_perform_range_for_lookup.
12196 Builds a tree for RANGE.IDENTIFIER(). */
12199 cp_parser_range_for_member_function (tree range
, tree identifier
)
12202 vec
<tree
, va_gc
> *vec
;
12204 member
= finish_class_member_access_expr (range
, identifier
,
12205 false, tf_warning_or_error
);
12206 if (member
== error_mark_node
)
12207 return error_mark_node
;
12209 vec
= make_tree_vector ();
12210 res
= finish_call_expr (member
, &vec
,
12211 /*disallow_virtual=*/false,
12212 /*koenig_p=*/false,
12213 tf_warning_or_error
);
12214 release_tree_vector (vec
);
12218 /* Parse an iteration-statement.
12220 iteration-statement:
12221 while ( condition ) statement
12222 do statement while ( expression ) ;
12223 for ( init-statement condition [opt] ; expression [opt] )
12226 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12229 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12230 unsigned short unroll
)
12235 unsigned char in_statement
;
12236 token_indent_info guard_tinfo
;
12238 /* Peek at the next token. */
12239 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12241 return error_mark_node
;
12243 guard_tinfo
= get_token_indent_info (token
);
12245 /* Remember whether or not we are already within an iteration
12247 in_statement
= parser
->in_statement
;
12249 /* See what kind of keyword it is. */
12250 keyword
= token
->keyword
;
12257 /* Begin the while-statement. */
12258 statement
= begin_while_stmt ();
12259 /* Look for the `('. */
12260 matching_parens parens
;
12261 parens
.require_open (parser
);
12262 /* Parse the condition. */
12263 condition
= cp_parser_condition (parser
);
12264 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12265 /* Look for the `)'. */
12266 parens
.require_close (parser
);
12267 /* Parse the dependent statement. */
12268 parser
->in_statement
= IN_ITERATION_STMT
;
12269 bool prev
= note_iteration_stmt_body_start ();
12270 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12271 note_iteration_stmt_body_end (prev
);
12272 parser
->in_statement
= in_statement
;
12273 /* We're done with the while-statement. */
12274 finish_while_stmt (statement
);
12282 /* Begin the do-statement. */
12283 statement
= begin_do_stmt ();
12284 /* Parse the body of the do-statement. */
12285 parser
->in_statement
= IN_ITERATION_STMT
;
12286 bool prev
= note_iteration_stmt_body_start ();
12287 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12288 note_iteration_stmt_body_end (prev
);
12289 parser
->in_statement
= in_statement
;
12290 finish_do_body (statement
);
12291 /* Look for the `while' keyword. */
12292 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12293 /* Look for the `('. */
12294 matching_parens parens
;
12295 parens
.require_open (parser
);
12296 /* Parse the expression. */
12297 expression
= cp_parser_expression (parser
);
12298 /* We're done with the do-statement. */
12299 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12300 /* Look for the `)'. */
12301 parens
.require_close (parser
);
12302 /* Look for the `;'. */
12303 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12309 /* Look for the `('. */
12310 matching_parens parens
;
12311 parens
.require_open (parser
);
12313 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12315 /* Look for the `)'. */
12316 parens
.require_close (parser
);
12318 /* Parse the body of the for-statement. */
12319 parser
->in_statement
= IN_ITERATION_STMT
;
12320 bool prev
= note_iteration_stmt_body_start ();
12321 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12322 note_iteration_stmt_body_end (prev
);
12323 parser
->in_statement
= in_statement
;
12325 /* We're done with the for-statement. */
12326 finish_for_stmt (statement
);
12331 cp_parser_error (parser
, "expected iteration-statement");
12332 statement
= error_mark_node
;
12339 /* Parse a init-statement or the declarator of a range-based-for.
12340 Returns true if a range-based-for declaration is seen.
12343 expression-statement
12344 simple-declaration */
12347 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12349 /* If the next token is a `;', then we have an empty
12350 expression-statement. Grammatically, this is also a
12351 simple-declaration, but an invalid one, because it does not
12352 declare anything. Therefore, if we did not handle this case
12353 specially, we would issue an error message about an invalid
12355 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12357 bool is_range_for
= false;
12358 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12360 /* Try to parse the init-statement. */
12361 if (cp_parser_range_based_for_with_init_p (parser
))
12364 cp_parser_parse_tentatively (parser
);
12365 /* Parse the declaration. */
12366 cp_parser_simple_declaration (parser
,
12367 /*function_definition_allowed_p=*/false,
12369 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12370 if (!cp_parser_parse_definitely (parser
))
12371 /* That didn't work, try to parse it as an expression-statement. */
12372 cp_parser_expression_statement (parser
, NULL_TREE
);
12374 if (cxx_dialect
< cxx2a
)
12376 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12377 "range-based %<for%> loops with initializer only "
12378 "available with -std=c++2a or -std=gnu++2a");
12379 *decl
= error_mark_node
;
12383 /* A colon is used in range-based for. */
12384 parser
->colon_corrects_to_scope_p
= false;
12386 /* We're going to speculatively look for a declaration, falling back
12387 to an expression, if necessary. */
12388 cp_parser_parse_tentatively (parser
);
12389 /* Parse the declaration. */
12390 cp_parser_simple_declaration (parser
,
12391 /*function_definition_allowed_p=*/false,
12393 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12394 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12396 /* It is a range-for, consume the ':'. */
12397 cp_lexer_consume_token (parser
->lexer
);
12398 is_range_for
= true;
12399 if (cxx_dialect
< cxx11
)
12401 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12402 "range-based %<for%> loops only available with "
12403 "-std=c++11 or -std=gnu++11");
12404 *decl
= error_mark_node
;
12408 /* The ';' is not consumed yet because we told
12409 cp_parser_simple_declaration not to. */
12410 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12412 if (cp_parser_parse_definitely (parser
))
12413 return is_range_for
;
12414 /* If the tentative parse failed, then we shall need to look for an
12415 expression-statement. */
12417 /* If we are here, it is an expression-statement. */
12418 cp_parser_expression_statement (parser
, NULL_TREE
);
12422 /* Parse a jump-statement.
12427 return expression [opt] ;
12428 return braced-init-list ;
12434 goto * expression ;
12436 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12439 cp_parser_jump_statement (cp_parser
* parser
)
12441 tree statement
= error_mark_node
;
12444 unsigned char in_statement
;
12446 /* Peek at the next token. */
12447 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12449 return error_mark_node
;
12451 /* See what kind of keyword it is. */
12452 keyword
= token
->keyword
;
12456 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12457 switch (in_statement
)
12460 error_at (token
->location
, "break statement not within loop or switch");
12463 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12464 || in_statement
== IN_ITERATION_STMT
);
12465 statement
= finish_break_stmt ();
12466 if (in_statement
== IN_ITERATION_STMT
)
12467 break_maybe_infinite_loop ();
12470 error_at (token
->location
, "invalid exit from OpenMP structured block");
12473 error_at (token
->location
, "break statement used with OpenMP for loop");
12476 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12480 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12483 error_at (token
->location
, "continue statement not within a loop");
12485 /* Fall through. */
12486 case IN_ITERATION_STMT
:
12488 statement
= finish_continue_stmt ();
12491 error_at (token
->location
, "invalid exit from OpenMP structured block");
12494 gcc_unreachable ();
12496 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12502 bool expr_non_constant_p
;
12504 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12506 cp_lexer_set_source_position (parser
->lexer
);
12507 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12508 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12510 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12511 expr
= cp_parser_expression (parser
);
12513 /* If the next token is a `;', then there is no
12516 /* Build the return-statement. */
12517 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12518 /* Don't deduce from a discarded return statement. */;
12520 statement
= finish_return_stmt (expr
);
12521 /* Look for the final `;'. */
12522 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12527 if (parser
->in_function_body
12528 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12530 error ("%<goto%> in %<constexpr%> function");
12531 cp_function_chain
->invalid_constexpr
= true;
12534 /* Create the goto-statement. */
12535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12537 /* Issue a warning about this use of a GNU extension. */
12538 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12539 /* Consume the '*' token. */
12540 cp_lexer_consume_token (parser
->lexer
);
12541 /* Parse the dependent expression. */
12542 finish_goto_stmt (cp_parser_expression (parser
));
12545 finish_goto_stmt (cp_parser_identifier (parser
));
12546 /* Look for the final `;'. */
12547 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12551 cp_parser_error (parser
, "expected jump-statement");
12558 /* Parse a declaration-statement.
12560 declaration-statement:
12561 block-declaration */
12564 cp_parser_declaration_statement (cp_parser
* parser
)
12568 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12569 p
= obstack_alloc (&declarator_obstack
, 0);
12571 /* Parse the block-declaration. */
12572 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12574 /* Free any declarators allocated. */
12575 obstack_free (&declarator_obstack
, p
);
12578 /* Some dependent statements (like `if (cond) statement'), are
12579 implicitly in their own scope. In other words, if the statement is
12580 a single statement (as opposed to a compound-statement), it is
12581 none-the-less treated as if it were enclosed in braces. Any
12582 declarations appearing in the dependent statement are out of scope
12583 after control passes that point. This function parses a statement,
12584 but ensures that is in its own scope, even if it is not a
12585 compound-statement.
12587 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12588 is a (possibly labeled) if statement which is not enclosed in
12589 braces and has an else clause. This is used to implement
12592 CHAIN is a vector of if-else-if conditions. This is used to implement
12595 Returns the new statement. */
12598 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12599 const token_indent_info
&guard_tinfo
,
12603 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12604 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12605 token_indent_info body_tinfo
12606 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12611 /* Mark if () ; with a special NOP_EXPR. */
12612 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12614 cp_lexer_consume_token (parser
->lexer
);
12615 statement
= add_stmt (build_empty_stmt (body_loc
));
12617 if (guard_tinfo
.keyword
== RID_IF
12618 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12619 warning_at (body_loc
, OPT_Wempty_body
,
12620 "suggest braces around empty body in an %<if%> statement");
12621 else if (guard_tinfo
.keyword
== RID_ELSE
)
12622 warning_at (body_loc
, OPT_Wempty_body
,
12623 "suggest braces around empty body in an %<else%> statement");
12625 /* if a compound is opened, we simply parse the statement directly. */
12626 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12627 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12628 /* If the token is not a `{', then we must take special action. */
12631 /* Create a compound-statement. */
12632 statement
= begin_compound_stmt (0);
12633 /* Parse the dependent-statement. */
12634 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12635 &body_loc_after_labels
);
12636 /* Finish the dummy compound-statement. */
12637 finish_compound_stmt (statement
);
12640 token_indent_info next_tinfo
12641 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12642 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12644 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12645 && next_tinfo
.type
!= CPP_SEMICOLON
)
12646 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12647 guard_tinfo
.location
, guard_tinfo
.keyword
);
12649 /* Return the statement. */
12653 /* For some dependent statements (like `while (cond) statement'), we
12654 have already created a scope. Therefore, even if the dependent
12655 statement is a compound-statement, we do not want to create another
12659 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12660 const token_indent_info
&guard_tinfo
)
12662 /* If the token is a `{', then we must take special action. */
12663 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12665 token_indent_info body_tinfo
12666 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12667 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12669 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12670 &loc_after_labels
);
12671 token_indent_info next_tinfo
12672 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12673 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12675 if (loc_after_labels
!= UNKNOWN_LOCATION
12676 && next_tinfo
.type
!= CPP_SEMICOLON
)
12677 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12678 guard_tinfo
.location
,
12679 guard_tinfo
.keyword
);
12683 /* Avoid calling cp_parser_compound_statement, so that we
12684 don't create a new scope. Do everything else by hand. */
12685 matching_braces braces
;
12686 braces
.require_open (parser
);
12687 /* If the next keyword is `__label__' we have a label declaration. */
12688 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12689 cp_parser_label_declaration (parser
);
12690 /* Parse an (optional) statement-seq. */
12691 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12692 braces
.require_close (parser
);
12696 /* Declarations [gram.dcl.dcl] */
12698 /* Parse an optional declaration-sequence.
12702 declaration-seq declaration */
12705 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12711 token
= cp_lexer_peek_token (parser
->lexer
);
12713 if (token
->type
== CPP_CLOSE_BRACE
12714 || token
->type
== CPP_EOF
12715 || token
->type
== CPP_PRAGMA_EOL
)
12718 if (token
->type
== CPP_SEMICOLON
)
12720 /* A declaration consisting of a single semicolon is
12721 invalid. Allow it unless we're being pedantic. */
12722 cp_lexer_consume_token (parser
->lexer
);
12723 if (!in_system_header_at (input_location
))
12724 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12728 /* If we're entering or exiting a region that's implicitly
12729 extern "C", modify the lang context appropriately. */
12730 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12732 push_lang_context (lang_name_c
);
12733 parser
->implicit_extern_c
= true;
12735 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12737 pop_lang_context ();
12738 parser
->implicit_extern_c
= false;
12741 if (token
->type
== CPP_PRAGMA
)
12743 /* A top-level declaration can consist solely of a #pragma.
12744 A nested declaration cannot, so this is done here and not
12745 in cp_parser_declaration. (A #pragma at block scope is
12746 handled in cp_parser_statement.) */
12747 cp_parser_pragma (parser
, pragma_external
, NULL
);
12751 /* Parse the declaration itself. */
12752 cp_parser_declaration (parser
);
12756 /* Parse a declaration.
12760 function-definition
12761 template-declaration
12762 explicit-instantiation
12763 explicit-specialization
12764 linkage-specification
12765 namespace-definition
12773 __extension__ declaration */
12776 cp_parser_declaration (cp_parser
* parser
)
12780 int saved_pedantic
;
12782 tree attributes
= NULL_TREE
;
12784 /* Check for the `__extension__' keyword. */
12785 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12787 /* Parse the qualified declaration. */
12788 cp_parser_declaration (parser
);
12789 /* Restore the PEDANTIC flag. */
12790 pedantic
= saved_pedantic
;
12795 /* Try to figure out what kind of declaration is present. */
12796 token1
= *cp_lexer_peek_token (parser
->lexer
);
12798 if (token1
.type
!= CPP_EOF
)
12799 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12802 token2
.type
= CPP_EOF
;
12803 token2
.keyword
= RID_MAX
;
12806 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12807 p
= obstack_alloc (&declarator_obstack
, 0);
12809 /* If the next token is `extern' and the following token is a string
12810 literal, then we have a linkage specification. */
12811 if (token1
.keyword
== RID_EXTERN
12812 && cp_parser_is_pure_string_literal (&token2
))
12813 cp_parser_linkage_specification (parser
);
12814 /* If the next token is `template', then we have either a template
12815 declaration, an explicit instantiation, or an explicit
12817 else if (token1
.keyword
== RID_TEMPLATE
)
12819 /* `template <>' indicates a template specialization. */
12820 if (token2
.type
== CPP_LESS
12821 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12822 cp_parser_explicit_specialization (parser
);
12823 /* `template <' indicates a template declaration. */
12824 else if (token2
.type
== CPP_LESS
)
12825 cp_parser_template_declaration (parser
, /*member_p=*/false);
12826 /* Anything else must be an explicit instantiation. */
12828 cp_parser_explicit_instantiation (parser
);
12830 /* If the next token is `export', then we have a template
12832 else if (token1
.keyword
== RID_EXPORT
)
12833 cp_parser_template_declaration (parser
, /*member_p=*/false);
12834 /* If the next token is `extern', 'static' or 'inline' and the one
12835 after that is `template', we have a GNU extended explicit
12836 instantiation directive. */
12837 else if (cp_parser_allow_gnu_extensions_p (parser
)
12838 && (token1
.keyword
== RID_EXTERN
12839 || token1
.keyword
== RID_STATIC
12840 || token1
.keyword
== RID_INLINE
)
12841 && token2
.keyword
== RID_TEMPLATE
)
12842 cp_parser_explicit_instantiation (parser
);
12843 /* If the next token is `namespace', check for a named or unnamed
12844 namespace definition. */
12845 else if (token1
.keyword
== RID_NAMESPACE
12846 && (/* A named namespace definition. */
12847 (token2
.type
== CPP_NAME
12848 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12850 || (token2
.type
== CPP_OPEN_SQUARE
12851 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12852 == CPP_OPEN_SQUARE
)
12853 /* An unnamed namespace definition. */
12854 || token2
.type
== CPP_OPEN_BRACE
12855 || token2
.keyword
== RID_ATTRIBUTE
))
12856 cp_parser_namespace_definition (parser
);
12857 /* An inline (associated) namespace definition. */
12858 else if (token1
.keyword
== RID_INLINE
12859 && token2
.keyword
== RID_NAMESPACE
)
12860 cp_parser_namespace_definition (parser
);
12861 /* Objective-C++ declaration/definition. */
12862 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12863 cp_parser_objc_declaration (parser
, NULL_TREE
);
12864 else if (c_dialect_objc ()
12865 && token1
.keyword
== RID_ATTRIBUTE
12866 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12867 cp_parser_objc_declaration (parser
, attributes
);
12868 /* At this point we may have a template declared by a concept
12870 else if (flag_concepts
12871 && cp_parser_template_declaration_after_export (parser
,
12872 /*member_p=*/false))
12875 /* Try to parse a block-declaration, or a function-definition. */
12876 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12878 /* Free any declarators allocated. */
12879 obstack_free (&declarator_obstack
, p
);
12882 /* Parse a block-declaration.
12887 namespace-alias-definition
12894 __extension__ block-declaration
12899 static_assert-declaration
12901 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12902 part of a declaration-statement. */
12905 cp_parser_block_declaration (cp_parser
*parser
,
12909 int saved_pedantic
;
12911 /* Check for the `__extension__' keyword. */
12912 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12914 /* Parse the qualified declaration. */
12915 cp_parser_block_declaration (parser
, statement_p
);
12916 /* Restore the PEDANTIC flag. */
12917 pedantic
= saved_pedantic
;
12922 /* Peek at the next token to figure out which kind of declaration is
12924 token1
= cp_lexer_peek_token (parser
->lexer
);
12926 /* If the next keyword is `asm', we have an asm-definition. */
12927 if (token1
->keyword
== RID_ASM
)
12930 cp_parser_commit_to_tentative_parse (parser
);
12931 cp_parser_asm_definition (parser
);
12933 /* If the next keyword is `namespace', we have a
12934 namespace-alias-definition. */
12935 else if (token1
->keyword
== RID_NAMESPACE
)
12936 cp_parser_namespace_alias_definition (parser
);
12937 /* If the next keyword is `using', we have a
12938 using-declaration, a using-directive, or an alias-declaration. */
12939 else if (token1
->keyword
== RID_USING
)
12944 cp_parser_commit_to_tentative_parse (parser
);
12945 /* If the token after `using' is `namespace', then we have a
12946 using-directive. */
12947 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12948 if (token2
->keyword
== RID_NAMESPACE
)
12949 cp_parser_using_directive (parser
);
12950 /* If the second token after 'using' is '=', then we have an
12951 alias-declaration. */
12952 else if (cxx_dialect
>= cxx11
12953 && token2
->type
== CPP_NAME
12954 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12955 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12956 cp_parser_alias_declaration (parser
);
12957 /* Otherwise, it's a using-declaration. */
12959 cp_parser_using_declaration (parser
,
12960 /*access_declaration_p=*/false);
12962 /* If the next keyword is `__label__' we have a misplaced label
12964 else if (token1
->keyword
== RID_LABEL
)
12966 cp_lexer_consume_token (parser
->lexer
);
12967 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12968 cp_parser_skip_to_end_of_statement (parser
);
12969 /* If the next token is now a `;', consume it. */
12970 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12971 cp_lexer_consume_token (parser
->lexer
);
12973 /* If the next token is `static_assert' we have a static assertion. */
12974 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12975 cp_parser_static_assert (parser
, /*member_p=*/false);
12976 /* Anything else must be a simple-declaration. */
12978 cp_parser_simple_declaration (parser
, !statement_p
,
12979 /*maybe_range_for_decl*/NULL
);
12982 /* Parse a simple-declaration.
12984 simple-declaration:
12985 decl-specifier-seq [opt] init-declarator-list [opt] ;
12986 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12987 brace-or-equal-initializer ;
12989 init-declarator-list:
12991 init-declarator-list , init-declarator
12993 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12994 function-definition as a simple-declaration.
12996 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12997 parsed declaration if it is an uninitialized single declarator not followed
12998 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12999 if present, will not be consumed. */
13002 cp_parser_simple_declaration (cp_parser
* parser
,
13003 bool function_definition_allowed_p
,
13004 tree
*maybe_range_for_decl
)
13006 cp_decl_specifier_seq decl_specifiers
;
13007 int declares_class_or_enum
;
13008 bool saw_declarator
;
13009 location_t comma_loc
= UNKNOWN_LOCATION
;
13010 location_t init_loc
= UNKNOWN_LOCATION
;
13012 if (maybe_range_for_decl
)
13013 *maybe_range_for_decl
= NULL_TREE
;
13015 /* Defer access checks until we know what is being declared; the
13016 checks for names appearing in the decl-specifier-seq should be
13017 done as if we were in the scope of the thing being declared. */
13018 push_deferring_access_checks (dk_deferred
);
13020 /* Parse the decl-specifier-seq. We have to keep track of whether
13021 or not the decl-specifier-seq declares a named class or
13022 enumeration type, since that is the only case in which the
13023 init-declarator-list is allowed to be empty.
13027 In a simple-declaration, the optional init-declarator-list can be
13028 omitted only when declaring a class or enumeration, that is when
13029 the decl-specifier-seq contains either a class-specifier, an
13030 elaborated-type-specifier, or an enum-specifier. */
13031 cp_parser_decl_specifier_seq (parser
,
13032 CP_PARSER_FLAGS_OPTIONAL
,
13034 &declares_class_or_enum
);
13035 /* We no longer need to defer access checks. */
13036 stop_deferring_access_checks ();
13038 /* In a block scope, a valid declaration must always have a
13039 decl-specifier-seq. By not trying to parse declarators, we can
13040 resolve the declaration/expression ambiguity more quickly. */
13041 if (!function_definition_allowed_p
13042 && !decl_specifiers
.any_specifiers_p
)
13044 cp_parser_error (parser
, "expected declaration");
13048 /* If the next two tokens are both identifiers, the code is
13049 erroneous. The usual cause of this situation is code like:
13053 where "T" should name a type -- but does not. */
13054 if (!decl_specifiers
.any_type_specifiers_p
13055 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13057 /* If parsing tentatively, we should commit; we really are
13058 looking at a declaration. */
13059 cp_parser_commit_to_tentative_parse (parser
);
13064 cp_parser_maybe_commit_to_declaration (parser
,
13065 decl_specifiers
.any_specifiers_p
);
13067 /* Look for C++17 decomposition declaration. */
13068 for (size_t n
= 1; ; n
++)
13069 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13070 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13072 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13073 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13074 && decl_specifiers
.any_specifiers_p
)
13077 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13078 maybe_range_for_decl
,
13081 /* The next token should be either a `,' or a `;'. */
13082 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13083 /* If it's a `;', we are done. */
13084 if (token
->type
== CPP_SEMICOLON
)
13086 else if (maybe_range_for_decl
)
13088 if (*maybe_range_for_decl
== NULL_TREE
)
13089 *maybe_range_for_decl
= error_mark_node
;
13092 /* Anything else is an error. */
13095 /* If we have already issued an error message we don't need
13096 to issue another one. */
13097 if ((decl
!= error_mark_node
13098 && DECL_INITIAL (decl
) != error_mark_node
)
13099 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13100 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13101 /* Skip tokens until we reach the end of the statement. */
13102 cp_parser_skip_to_end_of_statement (parser
);
13103 /* If the next token is now a `;', consume it. */
13104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13105 cp_lexer_consume_token (parser
->lexer
);
13113 bool auto_specifier_p
;
13114 /* NULL_TREE if both variable and function declaration are allowed,
13115 error_mark_node if function declaration are not allowed and
13116 a FUNCTION_DECL that should be diagnosed if it is followed by
13117 variable declarations. */
13118 tree auto_function_declaration
;
13120 last_type
= NULL_TREE
;
13122 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13123 auto_function_declaration
= NULL_TREE
;
13125 /* Keep going until we hit the `;' at the end of the simple
13127 saw_declarator
= false;
13128 while (cp_lexer_next_token_is_not (parser
->lexer
,
13132 bool function_definition_p
;
13134 tree auto_result
= NULL_TREE
;
13136 if (saw_declarator
)
13138 /* If we are processing next declarator, comma is expected */
13139 token
= cp_lexer_peek_token (parser
->lexer
);
13140 gcc_assert (token
->type
== CPP_COMMA
);
13141 cp_lexer_consume_token (parser
->lexer
);
13142 if (maybe_range_for_decl
)
13144 *maybe_range_for_decl
= error_mark_node
;
13145 if (comma_loc
== UNKNOWN_LOCATION
)
13146 comma_loc
= token
->location
;
13150 saw_declarator
= true;
13152 /* Parse the init-declarator. */
13153 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13155 function_definition_allowed_p
,
13156 /*member_p=*/false,
13157 declares_class_or_enum
,
13158 &function_definition_p
,
13159 maybe_range_for_decl
,
13162 /* If an error occurred while parsing tentatively, exit quickly.
13163 (That usually happens when in the body of a function; each
13164 statement is treated as a declaration-statement until proven
13166 if (cp_parser_error_occurred (parser
))
13169 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13171 /* If the init-declarator-list contains more than one
13172 init-declarator, they shall all form declarations of
13174 if (auto_function_declaration
== NULL_TREE
)
13175 auto_function_declaration
13176 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13177 else if (TREE_CODE (decl
) == FUNCTION_DECL
13178 || auto_function_declaration
!= error_mark_node
)
13180 error_at (decl_specifiers
.locations
[ds_type_spec
],
13181 "non-variable %qD in declaration with more than one "
13182 "declarator with placeholder type",
13183 TREE_CODE (decl
) == FUNCTION_DECL
13184 ? decl
: auto_function_declaration
);
13185 auto_function_declaration
= error_mark_node
;
13190 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13193 && last_type
!= error_mark_node
13194 && !same_type_p (auto_result
, last_type
))
13196 /* If the list of declarators contains more than one declarator,
13197 the type of each declared variable is determined as described
13198 above. If the type deduced for the template parameter U is not
13199 the same in each deduction, the program is ill-formed. */
13200 error_at (decl_specifiers
.locations
[ds_type_spec
],
13201 "inconsistent deduction for %qT: %qT and then %qT",
13202 decl_specifiers
.type
, last_type
, auto_result
);
13203 last_type
= error_mark_node
;
13206 last_type
= auto_result
;
13209 /* Handle function definitions specially. */
13210 if (function_definition_p
)
13212 /* If the next token is a `,', then we are probably
13213 processing something like:
13217 which is erroneous. */
13218 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13220 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13221 error_at (token
->location
,
13223 " declarations and function-definitions is forbidden");
13225 /* Otherwise, we're done with the list of declarators. */
13228 pop_deferring_access_checks ();
13232 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13233 *maybe_range_for_decl
= decl
;
13234 /* The next token should be either a `,' or a `;'. */
13235 token
= cp_lexer_peek_token (parser
->lexer
);
13236 /* If it's a `,', there are more declarators to come. */
13237 if (token
->type
== CPP_COMMA
)
13238 /* will be consumed next time around */;
13239 /* If it's a `;', we are done. */
13240 else if (token
->type
== CPP_SEMICOLON
)
13242 else if (maybe_range_for_decl
)
13244 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13245 permerror (decl_specifiers
.locations
[ds_type_spec
],
13246 "types may not be defined in a for-range-declaration");
13249 /* Anything else is an error. */
13252 /* If we have already issued an error message we don't need
13253 to issue another one. */
13254 if ((decl
!= error_mark_node
13255 && DECL_INITIAL (decl
) != error_mark_node
)
13256 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13257 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13258 /* Skip tokens until we reach the end of the statement. */
13259 cp_parser_skip_to_end_of_statement (parser
);
13260 /* If the next token is now a `;', consume it. */
13261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13262 cp_lexer_consume_token (parser
->lexer
);
13265 /* After the first time around, a function-definition is not
13266 allowed -- even if it was OK at first. For example:
13271 function_definition_allowed_p
= false;
13274 /* Issue an error message if no declarators are present, and the
13275 decl-specifier-seq does not itself declare a class or
13276 enumeration: [dcl.dcl]/3. */
13277 if (!saw_declarator
)
13279 if (cp_parser_declares_only_class_p (parser
))
13281 if (!declares_class_or_enum
13282 && decl_specifiers
.type
13283 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13284 /* Ensure an error is issued anyway when finish_decltype_type,
13285 called via cp_parser_decl_specifier_seq, returns a class or
13286 an enumeration (c++/51786). */
13287 decl_specifiers
.type
= NULL_TREE
;
13288 shadow_tag (&decl_specifiers
);
13290 /* Perform any deferred access checks. */
13291 perform_deferred_access_checks (tf_warning_or_error
);
13294 /* Consume the `;'. */
13296 if (!maybe_range_for_decl
)
13297 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13298 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13300 if (init_loc
!= UNKNOWN_LOCATION
)
13301 error_at (init_loc
, "initializer in range-based %<for%> loop");
13302 if (comma_loc
!= UNKNOWN_LOCATION
)
13303 error_at (comma_loc
,
13304 "multiple declarations in range-based %<for%> loop");
13308 pop_deferring_access_checks ();
13311 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13312 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13316 cp_parser_decomposition_declaration (cp_parser
*parser
,
13317 cp_decl_specifier_seq
*decl_specifiers
,
13318 tree
*maybe_range_for_decl
,
13319 location_t
*init_loc
)
13321 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13322 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13323 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13325 /* Parse the identifier-list. */
13326 auto_vec
<cp_expr
, 10> v
;
13327 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13330 cp_expr e
= cp_parser_identifier (parser
);
13331 if (e
.get_value () == error_mark_node
)
13334 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13336 cp_lexer_consume_token (parser
->lexer
);
13339 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13340 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13342 end_loc
= UNKNOWN_LOCATION
;
13343 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13345 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13346 cp_lexer_consume_token (parser
->lexer
);
13349 cp_parser_skip_to_end_of_statement (parser
);
13350 return error_mark_node
;
13354 if (cxx_dialect
< cxx17
)
13355 pedwarn (loc
, 0, "structured bindings only available with "
13356 "-std=c++17 or -std=gnu++17");
13359 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13360 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13361 declarator
->id_loc
= loc
;
13362 if (ref_qual
!= REF_QUAL_NONE
)
13363 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13364 ref_qual
== REF_QUAL_RVALUE
,
13366 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13367 NULL_TREE
, decl_specifiers
->attributes
,
13369 tree orig_decl
= decl
;
13373 cp_decl_specifier_seq decl_specs
;
13374 clear_decl_specs (&decl_specs
);
13375 decl_specs
.type
= make_auto ();
13377 FOR_EACH_VEC_ELT (v
, i
, e
)
13380 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13382 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13383 declarator
->id_loc
= e
.get_location ();
13384 tree elt_pushed_scope
;
13385 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13386 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13387 if (decl2
== error_mark_node
)
13388 decl
= error_mark_node
;
13389 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13391 /* Ensure we've diagnosed redeclaration if we aren't creating
13393 gcc_assert (errorcount
);
13394 decl
= error_mark_node
;
13398 if (elt_pushed_scope
)
13399 pop_scope (elt_pushed_scope
);
13404 error_at (loc
, "empty structured binding declaration");
13405 decl
= error_mark_node
;
13408 if (maybe_range_for_decl
== NULL
13409 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13411 bool non_constant_p
= false, is_direct_init
= false;
13412 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13413 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13415 if (initializer
== NULL_TREE
13416 || (TREE_CODE (initializer
) == TREE_LIST
13417 && TREE_CHAIN (initializer
))
13419 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13420 && CONSTRUCTOR_NELTS (initializer
) != 1))
13422 error_at (loc
, "invalid initializer for structured binding "
13424 initializer
= error_mark_node
;
13427 if (decl
!= error_mark_node
)
13429 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13430 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13431 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13432 cp_finish_decomp (decl
, prev
, v
.length ());
13435 else if (decl
!= error_mark_node
)
13437 *maybe_range_for_decl
= prev
;
13438 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13439 the underlying DECL. */
13440 cp_finish_decomp (decl
, prev
, v
.length ());
13444 pop_scope (pushed_scope
);
13446 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13448 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13449 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13455 /* Parse a decl-specifier-seq.
13457 decl-specifier-seq:
13458 decl-specifier-seq [opt] decl-specifier
13459 decl-specifier attribute-specifier-seq [opt] (C++11)
13462 storage-class-specifier
13473 Concepts Extension:
13478 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13480 The parser flags FLAGS is used to control type-specifier parsing.
13482 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13485 1: one of the decl-specifiers is an elaborated-type-specifier
13486 (i.e., a type declaration)
13487 2: one of the decl-specifiers is an enum-specifier or a
13488 class-specifier (i.e., a type definition)
13493 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13494 cp_parser_flags flags
,
13495 cp_decl_specifier_seq
*decl_specs
,
13496 int* declares_class_or_enum
)
13498 bool constructor_possible_p
= !parser
->in_declarator_p
;
13499 bool found_decl_spec
= false;
13500 cp_token
*start_token
= NULL
;
13503 /* Clear DECL_SPECS. */
13504 clear_decl_specs (decl_specs
);
13506 /* Assume no class or enumeration type is declared. */
13507 *declares_class_or_enum
= 0;
13509 /* Keep reading specifiers until there are no more to read. */
13512 bool constructor_p
;
13516 /* Peek at the next token. */
13517 token
= cp_lexer_peek_token (parser
->lexer
);
13519 /* Save the first token of the decl spec list for error
13522 start_token
= token
;
13523 /* Handle attributes. */
13524 if (cp_next_tokens_can_be_attribute_p (parser
))
13526 /* Parse the attributes. */
13527 tree attrs
= cp_parser_attributes_opt (parser
);
13529 /* In a sequence of declaration specifiers, c++11 attributes
13530 appertain to the type that precede them. In that case
13533 The attribute-specifier-seq affects the type only for
13534 the declaration it appears in, not other declarations
13535 involving the same type.
13537 But for now let's force the user to position the
13538 attribute either at the beginning of the declaration or
13539 after the declarator-id, which would clearly mean that it
13540 applies to the declarator. */
13541 if (cxx11_attribute_p (attrs
))
13543 if (!found_decl_spec
)
13544 /* The c++11 attribute is at the beginning of the
13545 declaration. It appertains to the entity being
13549 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13551 /* This is an attribute following a
13552 class-specifier. */
13553 if (decl_specs
->type_definition_p
)
13554 warn_misplaced_attr_for_class_type (token
->location
,
13560 decl_specs
->std_attributes
13561 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13562 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13563 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13569 decl_specs
->attributes
13570 = attr_chainon (decl_specs
->attributes
, attrs
);
13571 if (decl_specs
->locations
[ds_attribute
] == 0)
13572 decl_specs
->locations
[ds_attribute
] = token
->location
;
13575 /* Assume we will find a decl-specifier keyword. */
13576 found_decl_spec
= true;
13577 /* If the next token is an appropriate keyword, we can simply
13578 add it to the list. */
13579 switch (token
->keyword
)
13585 if (!at_class_scope_p ())
13587 gcc_rich_location
richloc (token
->location
);
13588 richloc
.add_fixit_remove ();
13589 error_at (&richloc
, "%<friend%> used outside of class");
13590 cp_lexer_purge_token (parser
->lexer
);
13595 /* Consume the token. */
13596 cp_lexer_consume_token (parser
->lexer
);
13600 case RID_CONSTEXPR
:
13602 cp_lexer_consume_token (parser
->lexer
);
13607 cp_lexer_consume_token (parser
->lexer
);
13610 /* function-specifier:
13617 cp_parser_function_specifier_opt (parser
, decl_specs
);
13624 /* Consume the token. */
13625 cp_lexer_consume_token (parser
->lexer
);
13626 /* A constructor declarator cannot appear in a typedef. */
13627 constructor_possible_p
= false;
13628 /* The "typedef" keyword can only occur in a declaration; we
13629 may as well commit at this point. */
13630 cp_parser_commit_to_tentative_parse (parser
);
13632 if (decl_specs
->storage_class
!= sc_none
)
13633 decl_specs
->conflicting_specifiers_p
= true;
13636 /* storage-class-specifier:
13646 if (cxx_dialect
== cxx98
)
13648 /* Consume the token. */
13649 cp_lexer_consume_token (parser
->lexer
);
13651 /* Complain about `auto' as a storage specifier, if
13652 we're complaining about C++0x compatibility. */
13653 gcc_rich_location
richloc (token
->location
);
13654 richloc
.add_fixit_remove ();
13655 warning_at (&richloc
, OPT_Wc__11_compat
,
13656 "%<auto%> changes meaning in C++11; "
13657 "please remove it");
13659 /* Set the storage class anyway. */
13660 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13664 /* C++0x auto type-specifier. */
13665 found_decl_spec
= false;
13672 /* Consume the token. */
13673 cp_lexer_consume_token (parser
->lexer
);
13674 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13678 /* Consume the token. */
13680 cp_lexer_consume_token (parser
->lexer
);
13684 /* We did not yet find a decl-specifier yet. */
13685 found_decl_spec
= false;
13689 if (found_decl_spec
13690 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13691 && token
->keyword
!= RID_CONSTEXPR
)
13692 error ("decl-specifier invalid in condition");
13694 if (found_decl_spec
13695 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13696 && token
->keyword
!= RID_MUTABLE
13697 && token
->keyword
!= RID_CONSTEXPR
)
13698 error_at (token
->location
, "%qD invalid in lambda",
13699 ridpointers
[token
->keyword
]);
13702 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13704 /* Constructors are a special case. The `S' in `S()' is not a
13705 decl-specifier; it is the beginning of the declarator. */
13707 = (!found_decl_spec
13708 && constructor_possible_p
13709 && (cp_parser_constructor_declarator_p
13710 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13712 /* If we don't have a DECL_SPEC yet, then we must be looking at
13713 a type-specifier. */
13714 if (!found_decl_spec
&& !constructor_p
)
13716 int decl_spec_declares_class_or_enum
;
13717 bool is_cv_qualifier
;
13721 = cp_parser_type_specifier (parser
, flags
,
13723 /*is_declaration=*/true,
13724 &decl_spec_declares_class_or_enum
,
13726 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13728 /* If this type-specifier referenced a user-defined type
13729 (a typedef, class-name, etc.), then we can't allow any
13730 more such type-specifiers henceforth.
13734 The longest sequence of decl-specifiers that could
13735 possibly be a type name is taken as the
13736 decl-specifier-seq of a declaration. The sequence shall
13737 be self-consistent as described below.
13741 As a general rule, at most one type-specifier is allowed
13742 in the complete decl-specifier-seq of a declaration. The
13743 only exceptions are the following:
13745 -- const or volatile can be combined with any other
13748 -- signed or unsigned can be combined with char, long,
13756 void g (const int Pc);
13758 Here, Pc is *not* part of the decl-specifier seq; it's
13759 the declarator. Therefore, once we see a type-specifier
13760 (other than a cv-qualifier), we forbid any additional
13761 user-defined types. We *do* still allow things like `int
13762 int' to be considered a decl-specifier-seq, and issue the
13763 error message later. */
13764 if (type_spec
&& !is_cv_qualifier
)
13765 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13766 /* A constructor declarator cannot follow a type-specifier. */
13769 constructor_possible_p
= false;
13770 found_decl_spec
= true;
13771 if (!is_cv_qualifier
)
13772 decl_specs
->any_type_specifiers_p
= true;
13776 /* If we still do not have a DECL_SPEC, then there are no more
13777 decl-specifiers. */
13778 if (!found_decl_spec
)
13781 decl_specs
->any_specifiers_p
= true;
13782 /* After we see one decl-specifier, further decl-specifiers are
13783 always optional. */
13784 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13787 /* Don't allow a friend specifier with a class definition. */
13788 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13789 && (*declares_class_or_enum
& 2))
13790 error_at (decl_specs
->locations
[ds_friend
],
13791 "class definition may not be declared a friend");
13794 /* Parse an (optional) storage-class-specifier.
13796 storage-class-specifier:
13805 storage-class-specifier:
13808 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13811 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13813 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13816 if (cxx_dialect
!= cxx98
)
13818 /* Fall through for C++98. */
13819 gcc_fallthrough ();
13826 /* Consume the token. */
13827 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13834 /* Parse an (optional) function-specifier.
13836 function-specifier:
13841 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13842 Updates DECL_SPECS, if it is non-NULL. */
13845 cp_parser_function_specifier_opt (cp_parser
* parser
,
13846 cp_decl_specifier_seq
*decl_specs
)
13848 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13849 switch (token
->keyword
)
13852 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13856 /* 14.5.2.3 [temp.mem]
13858 A member function template shall not be virtual. */
13859 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13860 && current_class_type
)
13861 error_at (token
->location
, "templates may not be %<virtual%>");
13863 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13867 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13874 /* Consume the token. */
13875 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13878 /* Parse a linkage-specification.
13880 linkage-specification:
13881 extern string-literal { declaration-seq [opt] }
13882 extern string-literal declaration */
13885 cp_parser_linkage_specification (cp_parser
* parser
)
13889 /* Look for the `extern' keyword. */
13890 cp_token
*extern_token
13891 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13893 /* Look for the string-literal. */
13894 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13895 linkage
= cp_parser_string_literal (parser
, false, false);
13897 /* Transform the literal into an identifier. If the literal is a
13898 wide-character string, or contains embedded NULs, then we can't
13899 handle it as the user wants. */
13900 if (strlen (TREE_STRING_POINTER (linkage
))
13901 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13903 cp_parser_error (parser
, "invalid linkage-specification");
13904 /* Assume C++ linkage. */
13905 linkage
= lang_name_cplusplus
;
13908 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13910 /* We're now using the new linkage. */
13911 push_lang_context (linkage
);
13913 /* Preserve the location of the the innermost linkage specification,
13914 tracking the locations of nested specifications via a local. */
13915 location_t saved_location
13916 = parser
->innermost_linkage_specification_location
;
13917 /* Construct a location ranging from the start of the "extern" to
13918 the end of the string-literal, with the caret at the start, e.g.:
13922 parser
->innermost_linkage_specification_location
13923 = make_location (extern_token
->location
,
13924 extern_token
->location
,
13925 get_finish (string_token
->location
));
13927 /* If the next token is a `{', then we're using the first
13929 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13931 cp_ensure_no_omp_declare_simd (parser
);
13932 cp_ensure_no_oacc_routine (parser
);
13934 /* Consume the `{' token. */
13935 matching_braces braces
;
13936 braces
.consume_open (parser
)->location
;
13937 /* Parse the declarations. */
13938 cp_parser_declaration_seq_opt (parser
);
13939 /* Look for the closing `}'. */
13940 braces
.require_close (parser
);
13942 /* Otherwise, there's just one declaration. */
13945 bool saved_in_unbraced_linkage_specification_p
;
13947 saved_in_unbraced_linkage_specification_p
13948 = parser
->in_unbraced_linkage_specification_p
;
13949 parser
->in_unbraced_linkage_specification_p
= true;
13950 cp_parser_declaration (parser
);
13951 parser
->in_unbraced_linkage_specification_p
13952 = saved_in_unbraced_linkage_specification_p
;
13955 /* We're done with the linkage-specification. */
13956 pop_lang_context ();
13958 /* Restore location of parent linkage specification, if any. */
13959 parser
->innermost_linkage_specification_location
= saved_location
;
13962 /* Parse a static_assert-declaration.
13964 static_assert-declaration:
13965 static_assert ( constant-expression , string-literal ) ;
13966 static_assert ( constant-expression ) ; (C++17)
13968 If MEMBER_P, this static_assert is a class member. */
13971 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13974 location_t token_loc
;
13978 /* Peek at the `static_assert' token so we can keep track of exactly
13979 where the static assertion started. */
13980 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13982 /* Look for the `static_assert' keyword. */
13983 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13987 /* We know we are in a static assertion; commit to any tentative
13989 if (cp_parser_parsing_tentatively (parser
))
13990 cp_parser_commit_to_tentative_parse (parser
);
13992 /* Parse the `(' starting the static assertion condition. */
13993 matching_parens parens
;
13994 parens
.require_open (parser
);
13996 /* Parse the constant-expression. Allow a non-constant expression
13997 here in order to give better diagnostics in finish_static_assert. */
13999 cp_parser_constant_expression (parser
,
14000 /*allow_non_constant_p=*/true,
14001 /*non_constant_p=*/&dummy
);
14003 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14005 if (cxx_dialect
< cxx17
)
14006 pedwarn (input_location
, OPT_Wpedantic
,
14007 "static_assert without a message "
14008 "only available with -std=c++17 or -std=gnu++17");
14010 cp_lexer_consume_token (parser
->lexer
);
14011 message
= build_string (1, "");
14012 TREE_TYPE (message
) = char_array_type_node
;
14013 fix_string_type (message
);
14017 /* Parse the separating `,'. */
14018 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14020 /* Parse the string-literal message. */
14021 message
= cp_parser_string_literal (parser
,
14022 /*translate=*/false,
14025 /* A `)' completes the static assertion. */
14026 if (!parens
.require_close (parser
))
14027 cp_parser_skip_to_closing_parenthesis (parser
,
14028 /*recovering=*/true,
14029 /*or_comma=*/false,
14030 /*consume_paren=*/true);
14033 /* A semicolon terminates the declaration. */
14034 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14036 /* Get the location for the static assertion. Use that of the
14037 condition if available, otherwise, use that of the "static_assert"
14039 location_t assert_loc
= condition
.get_location ();
14040 if (assert_loc
== UNKNOWN_LOCATION
)
14041 assert_loc
= token_loc
;
14043 /* Complete the static assertion, which may mean either processing
14044 the static assert now or saving it for template instantiation. */
14045 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14048 /* Parse the expression in decltype ( expression ). */
14051 cp_parser_decltype_expr (cp_parser
*parser
,
14052 bool &id_expression_or_member_access_p
)
14054 cp_token
*id_expr_start_token
;
14057 /* Since we're going to preserve any side-effects from this parse, set up a
14058 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14059 in the expression. */
14060 tentative_firewall
firewall (parser
);
14062 /* First, try parsing an id-expression. */
14063 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14064 cp_parser_parse_tentatively (parser
);
14065 expr
= cp_parser_id_expression (parser
,
14066 /*template_keyword_p=*/false,
14067 /*check_dependency_p=*/true,
14068 /*template_p=*/NULL
,
14069 /*declarator_p=*/false,
14070 /*optional_p=*/false);
14072 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14074 bool non_integral_constant_expression_p
= false;
14075 tree id_expression
= expr
;
14077 const char *error_msg
;
14079 if (identifier_p (expr
))
14080 /* Lookup the name we got back from the id-expression. */
14081 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14082 id_expr_start_token
->location
);
14084 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14085 /* A template without args is not a complete id-expression. */
14086 expr
= error_mark_node
;
14089 && expr
!= error_mark_node
14090 && TREE_CODE (expr
) != TYPE_DECL
14091 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14092 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14093 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14095 /* Complete lookup of the id-expression. */
14096 expr
= (finish_id_expression
14097 (id_expression
, expr
, parser
->scope
, &idk
,
14098 /*integral_constant_expression_p=*/false,
14099 /*allow_non_integral_constant_expression_p=*/true,
14100 &non_integral_constant_expression_p
,
14101 /*template_p=*/false,
14103 /*address_p=*/false,
14104 /*template_arg_p=*/false,
14106 id_expr_start_token
->location
));
14108 if (expr
== error_mark_node
)
14109 /* We found an id-expression, but it was something that we
14110 should not have found. This is an error, not something
14111 we can recover from, so note that we found an
14112 id-expression and we'll recover as gracefully as
14114 id_expression_or_member_access_p
= true;
14118 && expr
!= error_mark_node
14119 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14120 /* We have an id-expression. */
14121 id_expression_or_member_access_p
= true;
14124 if (!id_expression_or_member_access_p
)
14126 /* Abort the id-expression parse. */
14127 cp_parser_abort_tentative_parse (parser
);
14129 /* Parsing tentatively, again. */
14130 cp_parser_parse_tentatively (parser
);
14132 /* Parse a class member access. */
14133 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14134 /*cast_p=*/false, /*decltype*/true,
14135 /*member_access_only_p=*/true, NULL
);
14138 && expr
!= error_mark_node
14139 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14140 /* We have an id-expression. */
14141 id_expression_or_member_access_p
= true;
14144 if (id_expression_or_member_access_p
)
14145 /* We have parsed the complete id-expression or member access. */
14146 cp_parser_parse_definitely (parser
);
14149 /* Abort our attempt to parse an id-expression or member access
14151 cp_parser_abort_tentative_parse (parser
);
14153 /* Commit to the tentative_firewall so we get syntax errors. */
14154 cp_parser_commit_to_tentative_parse (parser
);
14156 /* Parse a full expression. */
14157 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14158 /*decltype_p=*/true);
14164 /* Parse a `decltype' type. Returns the type.
14166 simple-type-specifier:
14167 decltype ( expression )
14169 decltype ( auto ) */
14172 cp_parser_decltype (cp_parser
*parser
)
14174 bool id_expression_or_member_access_p
= false;
14175 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14177 if (start_token
->type
== CPP_DECLTYPE
)
14179 /* Already parsed. */
14180 cp_lexer_consume_token (parser
->lexer
);
14181 return saved_checks_value (start_token
->u
.tree_check_value
);
14184 /* Look for the `decltype' token. */
14185 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14186 return error_mark_node
;
14188 /* Parse the opening `('. */
14189 matching_parens parens
;
14190 if (!parens
.require_open (parser
))
14191 return error_mark_node
;
14193 push_deferring_access_checks (dk_deferred
);
14195 tree expr
= NULL_TREE
;
14197 if (cxx_dialect
>= cxx14
14198 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14199 /* decltype (auto) */
14200 cp_lexer_consume_token (parser
->lexer
);
14203 /* decltype (expression) */
14205 /* Types cannot be defined in a `decltype' expression. Save away the
14206 old message and set the new one. */
14207 const char *saved_message
= parser
->type_definition_forbidden_message
;
14208 parser
->type_definition_forbidden_message
14209 = G_("types may not be defined in %<decltype%> expressions");
14211 /* The restrictions on constant-expressions do not apply inside
14212 decltype expressions. */
14213 bool saved_integral_constant_expression_p
14214 = parser
->integral_constant_expression_p
;
14215 bool saved_non_integral_constant_expression_p
14216 = parser
->non_integral_constant_expression_p
;
14217 parser
->integral_constant_expression_p
= false;
14219 /* Within a parenthesized expression, a `>' token is always
14220 the greater-than operator. */
14221 bool saved_greater_than_is_operator_p
14222 = parser
->greater_than_is_operator_p
;
14223 parser
->greater_than_is_operator_p
= true;
14225 /* Do not actually evaluate the expression. */
14226 ++cp_unevaluated_operand
;
14228 /* Do not warn about problems with the expression. */
14229 ++c_inhibit_evaluation_warnings
;
14231 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14233 /* Go back to evaluating expressions. */
14234 --cp_unevaluated_operand
;
14235 --c_inhibit_evaluation_warnings
;
14237 /* The `>' token might be the end of a template-id or
14238 template-parameter-list now. */
14239 parser
->greater_than_is_operator_p
14240 = saved_greater_than_is_operator_p
;
14242 /* Restore the old message and the integral constant expression
14244 parser
->type_definition_forbidden_message
= saved_message
;
14245 parser
->integral_constant_expression_p
14246 = saved_integral_constant_expression_p
;
14247 parser
->non_integral_constant_expression_p
14248 = saved_non_integral_constant_expression_p
;
14251 /* Parse to the closing `)'. */
14252 if (!parens
.require_close (parser
))
14254 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14255 /*consume_paren=*/true);
14256 pop_deferring_access_checks ();
14257 return error_mark_node
;
14263 expr
= make_decltype_auto ();
14264 AUTO_IS_DECLTYPE (expr
) = true;
14267 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14268 tf_warning_or_error
);
14270 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14272 start_token
->type
= CPP_DECLTYPE
;
14273 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14274 start_token
->u
.tree_check_value
->value
= expr
;
14275 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14276 start_token
->keyword
= RID_MAX
;
14277 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14279 pop_to_parent_deferring_access_checks ();
14284 /* Special member functions [gram.special] */
14286 /* Parse a conversion-function-id.
14288 conversion-function-id:
14289 operator conversion-type-id
14291 Returns an IDENTIFIER_NODE representing the operator. */
14294 cp_parser_conversion_function_id (cp_parser
* parser
)
14298 tree saved_qualifying_scope
;
14299 tree saved_object_scope
;
14300 tree pushed_scope
= NULL_TREE
;
14302 /* Look for the `operator' token. */
14303 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14304 return error_mark_node
;
14305 /* When we parse the conversion-type-id, the current scope will be
14306 reset. However, we need that information in able to look up the
14307 conversion function later, so we save it here. */
14308 saved_scope
= parser
->scope
;
14309 saved_qualifying_scope
= parser
->qualifying_scope
;
14310 saved_object_scope
= parser
->object_scope
;
14311 /* We must enter the scope of the class so that the names of
14312 entities declared within the class are available in the
14313 conversion-type-id. For example, consider:
14320 S::operator I() { ... }
14322 In order to see that `I' is a type-name in the definition, we
14323 must be in the scope of `S'. */
14325 pushed_scope
= push_scope (saved_scope
);
14326 /* Parse the conversion-type-id. */
14327 type
= cp_parser_conversion_type_id (parser
);
14328 /* Leave the scope of the class, if any. */
14330 pop_scope (pushed_scope
);
14331 /* Restore the saved scope. */
14332 parser
->scope
= saved_scope
;
14333 parser
->qualifying_scope
= saved_qualifying_scope
;
14334 parser
->object_scope
= saved_object_scope
;
14335 /* If the TYPE is invalid, indicate failure. */
14336 if (type
== error_mark_node
)
14337 return error_mark_node
;
14338 return make_conv_op_name (type
);
14341 /* Parse a conversion-type-id:
14343 conversion-type-id:
14344 type-specifier-seq conversion-declarator [opt]
14346 Returns the TYPE specified. */
14349 cp_parser_conversion_type_id (cp_parser
* parser
)
14352 cp_decl_specifier_seq type_specifiers
;
14353 cp_declarator
*declarator
;
14354 tree type_specified
;
14355 const char *saved_message
;
14357 /* Parse the attributes. */
14358 attributes
= cp_parser_attributes_opt (parser
);
14360 saved_message
= parser
->type_definition_forbidden_message
;
14361 parser
->type_definition_forbidden_message
14362 = G_("types may not be defined in a conversion-type-id");
14364 /* Parse the type-specifiers. */
14365 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14366 /*is_trailing_return=*/false,
14369 parser
->type_definition_forbidden_message
= saved_message
;
14371 /* If that didn't work, stop. */
14372 if (type_specifiers
.type
== error_mark_node
)
14373 return error_mark_node
;
14374 /* Parse the conversion-declarator. */
14375 declarator
= cp_parser_conversion_declarator_opt (parser
);
14377 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14378 /*initialized=*/0, &attributes
);
14380 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14382 /* Don't give this error when parsing tentatively. This happens to
14383 work because we always parse this definitively once. */
14384 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14385 && type_uses_auto (type_specified
))
14387 if (cxx_dialect
< cxx14
)
14389 error ("invalid use of %<auto%> in conversion operator");
14390 return error_mark_node
;
14392 else if (template_parm_scope_p ())
14393 warning (0, "use of %<auto%> in member template "
14394 "conversion operator can never be deduced");
14397 return type_specified
;
14400 /* Parse an (optional) conversion-declarator.
14402 conversion-declarator:
14403 ptr-operator conversion-declarator [opt]
14407 static cp_declarator
*
14408 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14410 enum tree_code code
;
14411 tree class_type
, std_attributes
= NULL_TREE
;
14412 cp_cv_quals cv_quals
;
14414 /* We don't know if there's a ptr-operator next, or not. */
14415 cp_parser_parse_tentatively (parser
);
14416 /* Try the ptr-operator. */
14417 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14419 /* If it worked, look for more conversion-declarators. */
14420 if (cp_parser_parse_definitely (parser
))
14422 cp_declarator
*declarator
;
14424 /* Parse another optional declarator. */
14425 declarator
= cp_parser_conversion_declarator_opt (parser
);
14427 declarator
= cp_parser_make_indirect_declarator
14428 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14436 /* Parse an (optional) ctor-initializer.
14439 : mem-initializer-list */
14442 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14444 /* If the next token is not a `:', then there is no
14445 ctor-initializer. */
14446 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14448 /* Do default initialization of any bases and members. */
14449 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14450 finish_mem_initializers (NULL_TREE
);
14454 /* Consume the `:' token. */
14455 cp_lexer_consume_token (parser
->lexer
);
14456 /* And the mem-initializer-list. */
14457 cp_parser_mem_initializer_list (parser
);
14460 /* Parse a mem-initializer-list.
14462 mem-initializer-list:
14463 mem-initializer ... [opt]
14464 mem-initializer ... [opt] , mem-initializer-list */
14467 cp_parser_mem_initializer_list (cp_parser
* parser
)
14469 tree mem_initializer_list
= NULL_TREE
;
14470 tree target_ctor
= error_mark_node
;
14471 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14473 /* Let the semantic analysis code know that we are starting the
14474 mem-initializer-list. */
14475 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14476 error_at (token
->location
,
14477 "only constructors take member initializers");
14479 /* Loop through the list. */
14482 tree mem_initializer
;
14484 token
= cp_lexer_peek_token (parser
->lexer
);
14485 /* Parse the mem-initializer. */
14486 mem_initializer
= cp_parser_mem_initializer (parser
);
14487 /* If the next token is a `...', we're expanding member initializers. */
14488 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14490 || (mem_initializer
!= error_mark_node
14491 && check_for_bare_parameter_packs (TREE_PURPOSE
14492 (mem_initializer
))))
14494 /* Consume the `...'. */
14496 cp_lexer_consume_token (parser
->lexer
);
14498 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14499 can be expanded but members cannot. */
14500 if (mem_initializer
!= error_mark_node
14501 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14503 error_at (token
->location
,
14504 "cannot expand initializer for member %qD",
14505 TREE_PURPOSE (mem_initializer
));
14506 mem_initializer
= error_mark_node
;
14509 /* Construct the pack expansion type. */
14510 if (mem_initializer
!= error_mark_node
)
14511 mem_initializer
= make_pack_expansion (mem_initializer
);
14513 if (target_ctor
!= error_mark_node
14514 && mem_initializer
!= error_mark_node
)
14516 error ("mem-initializer for %qD follows constructor delegation",
14517 TREE_PURPOSE (mem_initializer
));
14518 mem_initializer
= error_mark_node
;
14520 /* Look for a target constructor. */
14521 if (mem_initializer
!= error_mark_node
14522 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14523 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14525 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14526 if (mem_initializer_list
)
14528 error ("constructor delegation follows mem-initializer for %qD",
14529 TREE_PURPOSE (mem_initializer_list
));
14530 mem_initializer
= error_mark_node
;
14532 target_ctor
= mem_initializer
;
14534 /* Add it to the list, unless it was erroneous. */
14535 if (mem_initializer
!= error_mark_node
)
14537 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14538 mem_initializer_list
= mem_initializer
;
14540 /* If the next token is not a `,', we're done. */
14541 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14543 /* Consume the `,' token. */
14544 cp_lexer_consume_token (parser
->lexer
);
14547 /* Perform semantic analysis. */
14548 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14549 finish_mem_initializers (mem_initializer_list
);
14552 /* Parse a mem-initializer.
14555 mem-initializer-id ( expression-list [opt] )
14556 mem-initializer-id braced-init-list
14561 ( expression-list [opt] )
14563 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14564 class) or FIELD_DECL (for a non-static data member) to initialize;
14565 the TREE_VALUE is the expression-list. An empty initialization
14566 list is represented by void_list_node. */
14569 cp_parser_mem_initializer (cp_parser
* parser
)
14571 tree mem_initializer_id
;
14572 tree expression_list
;
14574 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14576 /* Find out what is being initialized. */
14577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14579 permerror (token
->location
,
14580 "anachronistic old-style base class initializer");
14581 mem_initializer_id
= NULL_TREE
;
14585 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14586 if (mem_initializer_id
== error_mark_node
)
14587 return mem_initializer_id
;
14589 member
= expand_member_init (mem_initializer_id
);
14590 if (member
&& !DECL_P (member
))
14591 in_base_initializer
= 1;
14593 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14595 bool expr_non_constant_p
;
14596 cp_lexer_set_source_position (parser
->lexer
);
14597 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14598 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14599 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14600 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14604 vec
<tree
, va_gc
> *vec
;
14605 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14607 /*allow_expansion_p=*/true,
14608 /*non_constant_p=*/NULL
);
14610 return error_mark_node
;
14611 expression_list
= build_tree_list_vec (vec
);
14612 release_tree_vector (vec
);
14615 if (expression_list
== error_mark_node
)
14616 return error_mark_node
;
14617 if (!expression_list
)
14618 expression_list
= void_type_node
;
14620 in_base_initializer
= 0;
14622 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14625 /* Parse a mem-initializer-id.
14627 mem-initializer-id:
14628 :: [opt] nested-name-specifier [opt] class-name
14629 decltype-specifier (C++11)
14632 Returns a TYPE indicating the class to be initialized for the first
14633 production (and the second in C++11). Returns an IDENTIFIER_NODE
14634 indicating the data member to be initialized for the last production. */
14637 cp_parser_mem_initializer_id (cp_parser
* parser
)
14639 bool global_scope_p
;
14640 bool nested_name_specifier_p
;
14641 bool template_p
= false;
14644 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14646 /* `typename' is not allowed in this context ([temp.res]). */
14647 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14649 error_at (token
->location
,
14650 "keyword %<typename%> not allowed in this context (a qualified "
14651 "member initializer is implicitly a type)");
14652 cp_lexer_consume_token (parser
->lexer
);
14654 /* Look for the optional `::' operator. */
14656 = (cp_parser_global_scope_opt (parser
,
14657 /*current_scope_valid_p=*/false)
14659 /* Look for the optional nested-name-specifier. The simplest way to
14664 The keyword `typename' is not permitted in a base-specifier or
14665 mem-initializer; in these contexts a qualified name that
14666 depends on a template-parameter is implicitly assumed to be a
14669 is to assume that we have seen the `typename' keyword at this
14671 nested_name_specifier_p
14672 = (cp_parser_nested_name_specifier_opt (parser
,
14673 /*typename_keyword_p=*/true,
14674 /*check_dependency_p=*/true,
14676 /*is_declaration=*/true)
14678 if (nested_name_specifier_p
)
14679 template_p
= cp_parser_optional_template_keyword (parser
);
14680 /* If there is a `::' operator or a nested-name-specifier, then we
14681 are definitely looking for a class-name. */
14682 if (global_scope_p
|| nested_name_specifier_p
)
14683 return cp_parser_class_name (parser
,
14684 /*typename_keyword_p=*/true,
14685 /*template_keyword_p=*/template_p
,
14687 /*check_dependency_p=*/true,
14688 /*class_head_p=*/false,
14689 /*is_declaration=*/true);
14690 /* Otherwise, we could also be looking for an ordinary identifier. */
14691 cp_parser_parse_tentatively (parser
);
14692 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14693 /* Try a decltype-specifier. */
14694 id
= cp_parser_decltype (parser
);
14696 /* Otherwise, try a class-name. */
14697 id
= cp_parser_class_name (parser
,
14698 /*typename_keyword_p=*/true,
14699 /*template_keyword_p=*/false,
14701 /*check_dependency_p=*/true,
14702 /*class_head_p=*/false,
14703 /*is_declaration=*/true);
14704 /* If we found one, we're done. */
14705 if (cp_parser_parse_definitely (parser
))
14707 /* Otherwise, look for an ordinary identifier. */
14708 return cp_parser_identifier (parser
);
14711 /* Overloading [gram.over] */
14713 /* Parse an operator-function-id.
14715 operator-function-id:
14718 Returns an IDENTIFIER_NODE for the operator which is a
14719 human-readable spelling of the identifier, e.g., `operator +'. */
14722 cp_parser_operator_function_id (cp_parser
* parser
)
14724 /* Look for the `operator' keyword. */
14725 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14726 return error_mark_node
;
14727 /* And then the name of the operator itself. */
14728 return cp_parser_operator (parser
);
14731 /* Return an identifier node for a user-defined literal operator.
14732 The suffix identifier is chained to the operator name identifier. */
14735 cp_literal_operator_id (const char* name
)
14738 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14739 + strlen (name
) + 10);
14740 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14741 identifier
= get_identifier (buffer
);
14746 /* Parse an operator.
14749 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14750 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14751 || ++ -- , ->* -> () []
14758 Returns an IDENTIFIER_NODE for the operator which is a
14759 human-readable spelling of the identifier, e.g., `operator +'. */
14762 cp_parser_operator (cp_parser
* parser
)
14764 tree id
= NULL_TREE
;
14768 /* Peek at the next token. */
14769 token
= cp_lexer_peek_token (parser
->lexer
);
14771 location_t start_loc
= token
->location
;
14773 /* Figure out which operator we have. */
14774 enum tree_code op
= ERROR_MARK
;
14775 bool assop
= false;
14776 bool consumed
= false;
14777 switch (token
->type
)
14781 /* The keyword should be either `new' or `delete'. */
14782 if (token
->keyword
== RID_NEW
)
14784 else if (token
->keyword
== RID_DELETE
)
14789 /* Consume the `new' or `delete' token. */
14790 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14792 /* Peek at the next token. */
14793 token
= cp_lexer_peek_token (parser
->lexer
);
14794 /* If it's a `[' token then this is the array variant of the
14796 if (token
->type
== CPP_OPEN_SQUARE
)
14798 /* Consume the `[' token. */
14799 cp_lexer_consume_token (parser
->lexer
);
14800 /* Look for the `]' token. */
14801 if (cp_token
*close_token
14802 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14803 end_loc
= close_token
->location
;
14804 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14806 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14824 op
= TRUNC_DIV_EXPR
;
14828 op
= TRUNC_MOD_EXPR
;
14848 op
= TRUTH_NOT_EXPR
;
14881 op
= TRUNC_DIV_EXPR
;
14886 op
= TRUNC_MOD_EXPR
;
14912 case CPP_LSHIFT_EQ
:
14917 case CPP_RSHIFT_EQ
:
14934 case CPP_GREATER_EQ
:
14939 op
= TRUTH_ANDIF_EXPR
;
14943 op
= TRUTH_ORIF_EXPR
;
14946 case CPP_PLUS_PLUS
:
14947 op
= POSTINCREMENT_EXPR
;
14950 case CPP_MINUS_MINUS
:
14951 op
= PREDECREMENT_EXPR
;
14955 op
= COMPOUND_EXPR
;
14958 case CPP_DEREF_STAR
:
14963 op
= COMPONENT_REF
;
14966 case CPP_OPEN_PAREN
:
14968 /* Consume the `('. */
14969 matching_parens parens
;
14970 parens
.consume_open (parser
);
14971 /* Look for the matching `)'. */
14972 parens
.require_close (parser
);
14978 case CPP_OPEN_SQUARE
:
14979 /* Consume the `['. */
14980 cp_lexer_consume_token (parser
->lexer
);
14981 /* Look for the matching `]'. */
14982 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14987 case CPP_UTF8STRING
:
14988 case CPP_UTF8STRING_USERDEF
:
14995 case CPP_STRING_USERDEF
:
14996 case CPP_WSTRING_USERDEF
:
14997 case CPP_STRING16_USERDEF
:
14998 case CPP_STRING32_USERDEF
:
15000 tree str
, string_tree
;
15003 if (cxx_dialect
== cxx98
)
15004 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15006 /* Consume the string. */
15007 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15008 /*wide_ok=*/true, /*lookup_udlit=*/false);
15009 if (str
== error_mark_node
)
15010 return error_mark_node
;
15011 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15013 string_tree
= USERDEF_LITERAL_VALUE (str
);
15014 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
15019 /* Look for the suffix identifier. */
15020 token
= cp_lexer_peek_token (parser
->lexer
);
15021 if (token
->type
== CPP_NAME
)
15022 id
= cp_parser_identifier (parser
);
15023 else if (token
->type
== CPP_KEYWORD
)
15025 error ("unexpected keyword;"
15026 " remove space between quotes and suffix identifier");
15027 return error_mark_node
;
15031 error ("expected suffix identifier");
15032 return error_mark_node
;
15035 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15036 (TREE_TYPE (TREE_TYPE (string_tree
))));
15037 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15040 error ("expected empty string after %<operator%> keyword");
15041 return error_mark_node
;
15043 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15046 error ("invalid encoding prefix in literal operator");
15047 return error_mark_node
;
15049 if (id
!= error_mark_node
)
15051 const char *name
= IDENTIFIER_POINTER (id
);
15052 id
= cp_literal_operator_id (name
);
15058 /* Anything else is an error. */
15062 /* If we have selected an identifier, we need to consume the
15064 if (op
!= ERROR_MARK
)
15066 id
= ovl_op_identifier (assop
, op
);
15068 cp_lexer_consume_token (parser
->lexer
);
15070 /* Otherwise, no valid operator name was present. */
15073 cp_parser_error (parser
, "expected operator");
15074 id
= error_mark_node
;
15077 return cp_expr (id
, start_loc
);
15080 /* Parse a template-declaration.
15082 template-declaration:
15083 export [opt] template < template-parameter-list > declaration
15085 If MEMBER_P is TRUE, this template-declaration occurs within a
15088 The grammar rule given by the standard isn't correct. What
15089 is really meant is:
15091 template-declaration:
15092 export [opt] template-parameter-list-seq
15093 decl-specifier-seq [opt] init-declarator [opt] ;
15094 export [opt] template-parameter-list-seq
15095 function-definition
15097 template-parameter-list-seq:
15098 template-parameter-list-seq [opt]
15099 template < template-parameter-list >
15101 Concept Extensions:
15103 template-parameter-list-seq:
15104 template < template-parameter-list > requires-clause [opt]
15107 requires logical-or-expression */
15110 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15112 /* Check for `export'. */
15113 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15115 /* Consume the `export' token. */
15116 cp_lexer_consume_token (parser
->lexer
);
15117 /* Warn that we do not support `export'. */
15118 warning (0, "keyword %<export%> not implemented, and will be ignored");
15121 cp_parser_template_declaration_after_export (parser
, member_p
);
15124 /* Parse a template-parameter-list.
15126 template-parameter-list:
15128 template-parameter-list , template-parameter
15130 Returns a TREE_LIST. Each node represents a template parameter.
15131 The nodes are connected via their TREE_CHAINs. */
15134 cp_parser_template_parameter_list (cp_parser
* parser
)
15136 tree parameter_list
= NULL_TREE
;
15138 begin_template_parm_list ();
15140 /* The loop below parses the template parms. We first need to know
15141 the total number of template parms to be able to compute proper
15142 canonical types of each dependent type. So after the loop, when
15143 we know the total number of template parms,
15144 end_template_parm_list computes the proper canonical types and
15145 fixes up the dependent types accordingly. */
15150 bool is_parameter_pack
;
15151 location_t parm_loc
;
15153 /* Parse the template-parameter. */
15154 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15155 parameter
= cp_parser_template_parameter (parser
,
15157 &is_parameter_pack
);
15158 /* Add it to the list. */
15159 if (parameter
!= error_mark_node
)
15160 parameter_list
= process_template_parm (parameter_list
,
15164 is_parameter_pack
);
15167 tree err_parm
= build_tree_list (parameter
, parameter
);
15168 parameter_list
= chainon (parameter_list
, err_parm
);
15171 /* If the next token is not a `,', we're done. */
15172 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15174 /* Otherwise, consume the `,' token. */
15175 cp_lexer_consume_token (parser
->lexer
);
15178 return end_template_parm_list (parameter_list
);
15181 /* Parse a introduction-list.
15184 introduced-parameter
15185 introduction-list , introduced-parameter
15187 introduced-parameter:
15188 ...[opt] identifier
15190 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15191 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15192 WILDCARD_DECL will also have DECL_NAME set and token location in
15193 DECL_SOURCE_LOCATION. */
15196 cp_parser_introduction_list (cp_parser
*parser
)
15198 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15202 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15204 cp_lexer_consume_token (parser
->lexer
);
15206 /* Build placeholder. */
15207 tree parm
= build_nt (WILDCARD_DECL
);
15208 DECL_SOURCE_LOCATION (parm
)
15209 = cp_lexer_peek_token (parser
->lexer
)->location
;
15210 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15211 WILDCARD_PACK_P (parm
) = is_pack
;
15212 vec_safe_push (introduction_vec
, parm
);
15214 /* If the next token is not a `,', we're done. */
15215 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15217 /* Otherwise, consume the `,' token. */
15218 cp_lexer_consume_token (parser
->lexer
);
15221 /* Convert the vec into a TREE_VEC. */
15222 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15225 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15226 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15228 release_tree_vector (introduction_vec
);
15229 return introduction_list
;
15232 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15233 is an abstract declarator. */
15235 static inline cp_declarator
*
15236 get_id_declarator (cp_declarator
*declarator
)
15238 cp_declarator
*d
= declarator
;
15239 while (d
&& d
->kind
!= cdk_id
)
15244 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15245 is an abstract declarator. */
15248 get_unqualified_id (cp_declarator
*declarator
)
15250 declarator
= get_id_declarator (declarator
);
15252 return declarator
->u
.id
.unqualified_name
;
15257 /* Returns true if DECL represents a constrained-parameter. */
15260 is_constrained_parameter (tree decl
)
15263 && TREE_CODE (decl
) == TYPE_DECL
15264 && CONSTRAINED_PARM_CONCEPT (decl
)
15265 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15268 /* Returns true if PARM declares a constrained-parameter. */
15271 is_constrained_parameter (cp_parameter_declarator
*parm
)
15273 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15276 /* Check that the type parameter is only a declarator-id, and that its
15277 type is not cv-qualified. */
15280 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15281 cp_parameter_declarator
*parm
)
15283 if (!parm
->declarator
)
15286 if (parm
->declarator
->kind
!= cdk_id
)
15288 cp_parser_error (parser
, "invalid constrained type parameter");
15292 /* Don't allow cv-qualified type parameters. */
15293 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15294 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15296 cp_parser_error (parser
, "cv-qualified type parameter");
15303 /* Finish parsing/processing a template type parameter and checking
15304 various restrictions. */
15307 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15309 cp_parameter_declarator
* parmdecl
)
15311 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15312 return finish_template_type_parm (class_type_node
, id
);
15314 return error_mark_node
;
15318 finish_constrained_template_template_parm (tree proto
, tree id
)
15320 /* FIXME: This should probably be copied, and we may need to adjust
15321 the template parameter depths. */
15322 tree saved_parms
= current_template_parms
;
15323 begin_template_parm_list ();
15324 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15325 end_template_parm_list ();
15327 tree parm
= finish_template_template_parm (class_type_node
, id
);
15328 current_template_parms
= saved_parms
;
15333 /* Finish parsing/processing a template template parameter by borrowing
15334 the template parameter list from the prototype parameter. */
15337 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15340 cp_parameter_declarator
*parmdecl
)
15342 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15343 return error_mark_node
;
15344 return finish_constrained_template_template_parm (proto
, id
);
15347 /* Create a new non-type template parameter from the given PARM
15351 constrained_non_type_template_parm (bool *is_non_type
,
15352 cp_parameter_declarator
*parm
)
15354 *is_non_type
= true;
15355 cp_declarator
*decl
= parm
->declarator
;
15356 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15357 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15358 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15361 /* Build a constrained template parameter based on the PARMDECL
15362 declarator. The type of PARMDECL is the constrained type, which
15363 refers to the prototype template parameter that ultimately
15364 specifies the type of the declared parameter. */
15367 finish_constrained_parameter (cp_parser
*parser
,
15368 cp_parameter_declarator
*parmdecl
,
15370 bool *is_parameter_pack
)
15372 tree decl
= parmdecl
->decl_specifiers
.type
;
15373 tree id
= get_unqualified_id (parmdecl
->declarator
);
15374 tree def
= parmdecl
->default_argument
;
15375 tree proto
= DECL_INITIAL (decl
);
15377 /* A template parameter constrained by a variadic concept shall also
15378 be declared as a template parameter pack. */
15379 bool is_variadic
= template_parameter_pack_p (proto
);
15380 if (is_variadic
&& !*is_parameter_pack
)
15381 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15383 /* Build the parameter. Return an error if the declarator was invalid. */
15385 if (TREE_CODE (proto
) == TYPE_DECL
)
15386 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15387 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15388 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15391 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15392 if (parm
== error_mark_node
)
15393 return error_mark_node
;
15395 /* Finish the parameter decl and create a node attaching the
15396 default argument and constraint. */
15397 parm
= build_tree_list (def
, parm
);
15398 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15403 /* Returns true if the parsed type actually represents the declaration
15404 of a type template-parameter. */
15407 declares_constrained_type_template_parameter (tree type
)
15409 return (is_constrained_parameter (type
)
15410 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15414 /* Returns true if the parsed type actually represents the declaration of
15415 a template template-parameter. */
15418 declares_constrained_template_template_parameter (tree type
)
15420 return (is_constrained_parameter (type
)
15421 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15424 /* Parse a default argument for a type template-parameter.
15425 Note that diagnostics are handled in cp_parser_template_parameter. */
15428 cp_parser_default_type_template_argument (cp_parser
*parser
)
15430 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15432 /* Consume the `=' token. */
15433 cp_lexer_consume_token (parser
->lexer
);
15435 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15437 /* Parse the default-argument. */
15438 push_deferring_access_checks (dk_no_deferred
);
15439 tree default_argument
= cp_parser_type_id (parser
);
15440 pop_deferring_access_checks ();
15442 if (flag_concepts
&& type_uses_auto (default_argument
))
15444 error_at (token
->location
,
15445 "invalid use of %<auto%> in default template argument");
15446 return error_mark_node
;
15449 return default_argument
;
15452 /* Parse a default argument for a template template-parameter. */
15455 cp_parser_default_template_template_argument (cp_parser
*parser
)
15457 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15461 /* Consume the `='. */
15462 cp_lexer_consume_token (parser
->lexer
);
15463 /* Parse the id-expression. */
15464 push_deferring_access_checks (dk_no_deferred
);
15465 /* save token before parsing the id-expression, for error
15467 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15468 tree default_argument
15469 = cp_parser_id_expression (parser
,
15470 /*template_keyword_p=*/false,
15471 /*check_dependency_p=*/true,
15472 /*template_p=*/&is_template
,
15473 /*declarator_p=*/false,
15474 /*optional_p=*/false);
15475 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15476 /* If the id-expression was a template-id that refers to
15477 a template-class, we already have the declaration here,
15478 so no further lookup is needed. */
15481 /* Look up the name. */
15483 = cp_parser_lookup_name (parser
, default_argument
,
15485 /*is_template=*/is_template
,
15486 /*is_namespace=*/false,
15487 /*check_dependency=*/true,
15488 /*ambiguous_decls=*/NULL
,
15490 /* See if the default argument is valid. */
15491 default_argument
= check_template_template_default_arg (default_argument
);
15492 pop_deferring_access_checks ();
15493 return default_argument
;
15496 /* Parse a template-parameter.
15498 template-parameter:
15500 parameter-declaration
15502 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15503 the parameter. The TREE_PURPOSE is the default value, if any.
15504 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15505 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15506 set to true iff this parameter is a parameter pack. */
15509 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15510 bool *is_parameter_pack
)
15513 cp_parameter_declarator
*parameter_declarator
;
15516 /* Assume it is a type parameter or a template parameter. */
15517 *is_non_type
= false;
15518 /* Assume it not a parameter pack. */
15519 *is_parameter_pack
= false;
15520 /* Peek at the next token. */
15521 token
= cp_lexer_peek_token (parser
->lexer
);
15522 /* If it is `template', we have a type-parameter. */
15523 if (token
->keyword
== RID_TEMPLATE
)
15524 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15525 /* If it is `class' or `typename' we do not know yet whether it is a
15526 type parameter or a non-type parameter. Consider:
15528 template <typename T, typename T::X X> ...
15532 template <class C, class D*> ...
15534 Here, the first parameter is a type parameter, and the second is
15535 a non-type parameter. We can tell by looking at the token after
15536 the identifier -- if it is a `,', `=', or `>' then we have a type
15538 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15540 /* Peek at the token after `class' or `typename'. */
15541 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15542 /* If it's an ellipsis, we have a template type parameter
15544 if (token
->type
== CPP_ELLIPSIS
)
15545 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15546 /* If it's an identifier, skip it. */
15547 if (token
->type
== CPP_NAME
)
15548 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15549 /* Now, see if the token looks like the end of a template
15551 if (token
->type
== CPP_COMMA
15552 || token
->type
== CPP_EQ
15553 || token
->type
== CPP_GREATER
)
15554 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15557 /* Otherwise, it is a non-type parameter or a constrained parameter.
15561 When parsing a default template-argument for a non-type
15562 template-parameter, the first non-nested `>' is taken as the end
15563 of the template parameter-list rather than a greater-than
15565 parameter_declarator
15566 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15567 /*parenthesized_p=*/NULL
);
15569 if (!parameter_declarator
)
15570 return error_mark_node
;
15572 /* If the parameter declaration is marked as a parameter pack, set
15573 *IS_PARAMETER_PACK to notify the caller. */
15574 if (parameter_declarator
->template_parameter_pack_p
)
15575 *is_parameter_pack
= true;
15577 if (parameter_declarator
->default_argument
)
15579 /* Can happen in some cases of erroneous input (c++/34892). */
15580 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15581 /* Consume the `...' for better error recovery. */
15582 cp_lexer_consume_token (parser
->lexer
);
15585 // The parameter may have been constrained.
15586 if (is_constrained_parameter (parameter_declarator
))
15587 return finish_constrained_parameter (parser
,
15588 parameter_declarator
,
15590 is_parameter_pack
);
15592 // Now we're sure that the parameter is a non-type parameter.
15593 *is_non_type
= true;
15595 parm
= grokdeclarator (parameter_declarator
->declarator
,
15596 ¶meter_declarator
->decl_specifiers
,
15597 TPARM
, /*initialized=*/0,
15598 /*attrlist=*/NULL
);
15599 if (parm
== error_mark_node
)
15600 return error_mark_node
;
15602 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15605 /* Parse a type-parameter.
15608 class identifier [opt]
15609 class identifier [opt] = type-id
15610 typename identifier [opt]
15611 typename identifier [opt] = type-id
15612 template < template-parameter-list > class identifier [opt]
15613 template < template-parameter-list > class identifier [opt]
15616 GNU Extension (variadic templates):
15619 class ... identifier [opt]
15620 typename ... identifier [opt]
15622 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15623 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15624 the declaration of the parameter.
15626 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15629 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15634 /* Look for a keyword to tell us what kind of parameter this is. */
15635 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15637 return error_mark_node
;
15639 switch (token
->keyword
)
15645 tree default_argument
;
15647 /* If the next token is an ellipsis, we have a template
15649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15651 /* Consume the `...' token. */
15652 cp_lexer_consume_token (parser
->lexer
);
15653 maybe_warn_variadic_templates ();
15655 *is_parameter_pack
= true;
15658 /* If the next token is an identifier, then it names the
15660 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15661 identifier
= cp_parser_identifier (parser
);
15663 identifier
= NULL_TREE
;
15665 /* Create the parameter. */
15666 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15668 /* If the next token is an `=', we have a default argument. */
15669 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15672 = cp_parser_default_type_template_argument (parser
);
15674 /* Template parameter packs cannot have default
15676 if (*is_parameter_pack
)
15679 error_at (token
->location
,
15680 "template parameter pack %qD cannot have a "
15681 "default argument", identifier
);
15683 error_at (token
->location
,
15684 "template parameter packs cannot have "
15685 "default arguments");
15686 default_argument
= NULL_TREE
;
15688 else if (check_for_bare_parameter_packs (default_argument
))
15689 default_argument
= error_mark_node
;
15692 default_argument
= NULL_TREE
;
15694 /* Create the combined representation of the parameter and the
15695 default argument. */
15696 parameter
= build_tree_list (default_argument
, parameter
);
15703 tree default_argument
;
15705 /* Look for the `<'. */
15706 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15707 /* Parse the template-parameter-list. */
15708 cp_parser_template_parameter_list (parser
);
15709 /* Look for the `>'. */
15710 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15712 // If template requirements are present, parse them.
15715 tree reqs
= get_shorthand_constraints (current_template_parms
);
15716 if (tree r
= cp_parser_requires_clause_opt (parser
))
15717 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15718 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15721 /* Look for the `class' or 'typename' keywords. */
15722 cp_parser_type_parameter_key (parser
);
15723 /* If the next token is an ellipsis, we have a template
15725 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15727 /* Consume the `...' token. */
15728 cp_lexer_consume_token (parser
->lexer
);
15729 maybe_warn_variadic_templates ();
15731 *is_parameter_pack
= true;
15733 /* If the next token is an `=', then there is a
15734 default-argument. If the next token is a `>', we are at
15735 the end of the parameter-list. If the next token is a `,',
15736 then we are at the end of this parameter. */
15737 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15738 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15739 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15741 identifier
= cp_parser_identifier (parser
);
15742 /* Treat invalid names as if the parameter were nameless. */
15743 if (identifier
== error_mark_node
)
15744 identifier
= NULL_TREE
;
15747 identifier
= NULL_TREE
;
15749 /* Create the template parameter. */
15750 parameter
= finish_template_template_parm (class_type_node
,
15753 /* If the next token is an `=', then there is a
15754 default-argument. */
15755 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15758 = cp_parser_default_template_template_argument (parser
);
15760 /* Template parameter packs cannot have default
15762 if (*is_parameter_pack
)
15765 error_at (token
->location
,
15766 "template parameter pack %qD cannot "
15767 "have a default argument",
15770 error_at (token
->location
, "template parameter packs cannot "
15771 "have default arguments");
15772 default_argument
= NULL_TREE
;
15776 default_argument
= NULL_TREE
;
15778 /* Create the combined representation of the parameter and the
15779 default argument. */
15780 parameter
= build_tree_list (default_argument
, parameter
);
15785 gcc_unreachable ();
15792 /* Parse a template-id.
15795 template-name < template-argument-list [opt] >
15797 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15798 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15799 returned. Otherwise, if the template-name names a function, or set
15800 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15801 names a class, returns a TYPE_DECL for the specialization.
15803 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15804 uninstantiated templates. */
15807 cp_parser_template_id (cp_parser
*parser
,
15808 bool template_keyword_p
,
15809 bool check_dependency_p
,
15810 enum tag_types tag_type
,
15811 bool is_declaration
)
15816 cp_token_position start_of_id
= 0;
15817 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15818 bool is_identifier
;
15820 /* If the next token corresponds to a template-id, there is no need
15822 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15823 if (token
->type
== CPP_TEMPLATE_ID
)
15825 cp_lexer_consume_token (parser
->lexer
);
15826 return saved_checks_value (token
->u
.tree_check_value
);
15829 /* Avoid performing name lookup if there is no possibility of
15830 finding a template-id. */
15831 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15832 || (token
->type
== CPP_NAME
15833 && !cp_parser_nth_token_starts_template_argument_list_p
15836 cp_parser_error (parser
, "expected template-id");
15837 return error_mark_node
;
15840 /* Remember where the template-id starts. */
15841 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15842 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15844 push_deferring_access_checks (dk_deferred
);
15846 /* Parse the template-name. */
15847 is_identifier
= false;
15848 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15849 check_dependency_p
,
15853 if (templ
== error_mark_node
|| is_identifier
)
15855 pop_deferring_access_checks ();
15859 /* Since we're going to preserve any side-effects from this parse, set up a
15860 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15861 in the template arguments. */
15862 tentative_firewall
firewall (parser
);
15864 /* If we find the sequence `[:' after a template-name, it's probably
15865 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15866 parse correctly the argument list. */
15867 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15868 == CPP_OPEN_SQUARE
)
15869 && next_token
->flags
& DIGRAPH
15870 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15872 && !(next_token_2
->flags
& PREV_WHITE
))
15874 cp_parser_parse_tentatively (parser
);
15875 /* Change `:' into `::'. */
15876 next_token_2
->type
= CPP_SCOPE
;
15877 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15879 cp_lexer_consume_token (parser
->lexer
);
15881 /* Parse the arguments. */
15882 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15883 if (!cp_parser_parse_definitely (parser
))
15885 /* If we couldn't parse an argument list, then we revert our changes
15886 and return simply an error. Maybe this is not a template-id
15888 next_token_2
->type
= CPP_COLON
;
15889 cp_parser_error (parser
, "expected %<<%>");
15890 pop_deferring_access_checks ();
15891 return error_mark_node
;
15893 /* Otherwise, emit an error about the invalid digraph, but continue
15894 parsing because we got our argument list. */
15895 if (permerror (next_token
->location
,
15896 "%<<::%> cannot begin a template-argument list"))
15898 static bool hint
= false;
15899 inform (next_token
->location
,
15900 "%<<:%> is an alternate spelling for %<[%>."
15901 " Insert whitespace between %<<%> and %<::%>");
15902 if (!hint
&& !flag_permissive
)
15904 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15905 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15906 "accept your code)");
15913 /* Look for the `<' that starts the template-argument-list. */
15914 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15916 pop_deferring_access_checks ();
15917 return error_mark_node
;
15919 /* Parse the arguments. */
15920 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15923 /* Set the location to be of the form:
15924 template-name < template-argument-list [opt] >
15925 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15926 with caret == start at the start of the template-name,
15927 ranging until the closing '>'. */
15928 location_t finish_loc
15929 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15930 location_t combined_loc
15931 = make_location (token
->location
, token
->location
, finish_loc
);
15933 /* Check for concepts autos where they don't belong. We could
15934 identify types in some cases of idnetifier TEMPL, looking ahead
15935 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15936 types. We reject them in functions, but if what we have is an
15937 identifier, even with none_type we can't conclude it's NOT a
15938 type, we have to wait for template substitution. */
15939 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
15940 template_id
= error_mark_node
;
15941 /* Build a representation of the specialization. */
15942 else if (identifier_p (templ
))
15943 template_id
= build_min_nt_loc (combined_loc
,
15946 else if (DECL_TYPE_TEMPLATE_P (templ
)
15947 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15949 bool entering_scope
;
15950 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15951 template (rather than some instantiation thereof) only if
15952 is not nested within some other construct. For example, in
15953 "template <typename T> void f(T) { A<T>::", A<T> is just an
15954 instantiation of A. */
15955 entering_scope
= (template_parm_scope_p ()
15956 && cp_lexer_next_token_is (parser
->lexer
,
15959 = finish_template_type (templ
, arguments
, entering_scope
);
15961 /* A template-like identifier may be a partial concept id. */
15962 else if (flag_concepts
15963 && (template_id
= (cp_parser_maybe_partial_concept_id
15964 (parser
, templ
, arguments
))))
15965 return template_id
;
15966 else if (variable_template_p (templ
))
15968 template_id
= lookup_template_variable (templ
, arguments
);
15969 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15970 SET_EXPR_LOCATION (template_id
, combined_loc
);
15974 /* If it's not a class-template or a template-template, it should be
15975 a function-template. */
15976 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15977 || TREE_CODE (templ
) == OVERLOAD
15978 || BASELINK_P (templ
)));
15980 template_id
= lookup_template_function (templ
, arguments
);
15981 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15982 SET_EXPR_LOCATION (template_id
, combined_loc
);
15985 /* If parsing tentatively, replace the sequence of tokens that makes
15986 up the template-id with a CPP_TEMPLATE_ID token. That way,
15987 should we re-parse the token stream, we will not have to repeat
15988 the effort required to do the parse, nor will we issue duplicate
15989 error messages about problems during instantiation of the
15992 /* Don't do this if we had a parse error in a declarator; re-parsing
15993 might succeed if a name changes meaning (60361). */
15994 && !(cp_parser_error_occurred (parser
)
15995 && cp_parser_parsing_tentatively (parser
)
15996 && parser
->in_declarator_p
))
15998 /* Reset the contents of the START_OF_ID token. */
15999 token
->type
= CPP_TEMPLATE_ID
;
16000 token
->location
= combined_loc
;
16002 /* We must mark the lookup as kept, so we don't throw it away on
16003 the first parse. */
16004 if (is_overloaded_fn (template_id
))
16005 lookup_keep (get_fns (template_id
), true);
16007 /* Retrieve any deferred checks. Do not pop this access checks yet
16008 so the memory will not be reclaimed during token replacing below. */
16009 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16010 token
->u
.tree_check_value
->value
= template_id
;
16011 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16012 token
->keyword
= RID_MAX
;
16014 /* Purge all subsequent tokens. */
16015 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16017 /* ??? Can we actually assume that, if template_id ==
16018 error_mark_node, we will have issued a diagnostic to the
16019 user, as opposed to simply marking the tentative parse as
16021 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16022 error_at (token
->location
, "parse error in template argument list");
16025 pop_to_parent_deferring_access_checks ();
16026 return template_id
;
16029 /* Parse a template-name.
16034 The standard should actually say:
16038 operator-function-id
16040 A defect report has been filed about this issue.
16042 A conversion-function-id cannot be a template name because they cannot
16043 be part of a template-id. In fact, looking at this code:
16045 a.operator K<int>()
16047 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16048 It is impossible to call a templated conversion-function-id with an
16049 explicit argument list, since the only allowed template parameter is
16050 the type to which it is converting.
16052 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16053 `template' keyword, in a construction like:
16057 In that case `f' is taken to be a template-name, even though there
16058 is no way of knowing for sure.
16060 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16061 name refers to a set of overloaded functions, at least one of which
16062 is a template, or an IDENTIFIER_NODE with the name of the template,
16063 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16064 names are looked up inside uninstantiated templates. */
16067 cp_parser_template_name (cp_parser
* parser
,
16068 bool template_keyword_p
,
16069 bool check_dependency_p
,
16070 bool is_declaration
,
16071 enum tag_types tag_type
,
16072 bool *is_identifier
)
16076 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16078 /* If the next token is `operator', then we have either an
16079 operator-function-id or a conversion-function-id. */
16080 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16082 /* We don't know whether we're looking at an
16083 operator-function-id or a conversion-function-id. */
16084 cp_parser_parse_tentatively (parser
);
16085 /* Try an operator-function-id. */
16086 identifier
= cp_parser_operator_function_id (parser
);
16087 /* If that didn't work, try a conversion-function-id. */
16088 if (!cp_parser_parse_definitely (parser
))
16090 cp_parser_error (parser
, "expected template-name");
16091 return error_mark_node
;
16094 /* Look for the identifier. */
16096 identifier
= cp_parser_identifier (parser
);
16098 /* If we didn't find an identifier, we don't have a template-id. */
16099 if (identifier
== error_mark_node
)
16100 return error_mark_node
;
16102 /* If the name immediately followed the `template' keyword, then it
16103 is a template-name. However, if the next token is not `<', then
16104 we do not treat it as a template-name, since it is not being used
16105 as part of a template-id. This enables us to handle constructs
16108 template <typename T> struct S { S(); };
16109 template <typename T> S<T>::S();
16111 correctly. We would treat `S' as a template -- if it were `S<T>'
16112 -- but we do not if there is no `<'. */
16114 if (processing_template_decl
16115 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16117 /* In a declaration, in a dependent context, we pretend that the
16118 "template" keyword was present in order to improve error
16119 recovery. For example, given:
16121 template <typename T> void f(T::X<int>);
16123 we want to treat "X<int>" as a template-id. */
16125 && !template_keyword_p
16126 && parser
->scope
&& TYPE_P (parser
->scope
)
16127 && check_dependency_p
16128 && dependent_scope_p (parser
->scope
)
16129 /* Do not do this for dtors (or ctors), since they never
16130 need the template keyword before their name. */
16131 && !constructor_name_p (identifier
, parser
->scope
))
16133 cp_token_position start
= 0;
16135 /* Explain what went wrong. */
16136 error_at (token
->location
, "non-template %qD used as template",
16138 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16139 parser
->scope
, identifier
);
16140 /* If parsing tentatively, find the location of the "<" token. */
16141 if (cp_parser_simulate_error (parser
))
16142 start
= cp_lexer_token_position (parser
->lexer
, true);
16143 /* Parse the template arguments so that we can issue error
16144 messages about them. */
16145 cp_lexer_consume_token (parser
->lexer
);
16146 cp_parser_enclosed_template_argument_list (parser
);
16147 /* Skip tokens until we find a good place from which to
16148 continue parsing. */
16149 cp_parser_skip_to_closing_parenthesis (parser
,
16150 /*recovering=*/true,
16152 /*consume_paren=*/false);
16153 /* If parsing tentatively, permanently remove the
16154 template argument list. That will prevent duplicate
16155 error messages from being issued about the missing
16156 "template" keyword. */
16158 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16160 *is_identifier
= true;
16161 parser
->context
->object_type
= NULL_TREE
;
16165 /* If the "template" keyword is present, then there is generally
16166 no point in doing name-lookup, so we just return IDENTIFIER.
16167 But, if the qualifying scope is non-dependent then we can
16168 (and must) do name-lookup normally. */
16169 if (template_keyword_p
)
16171 tree scope
= (parser
->scope
? parser
->scope
16172 : parser
->context
->object_type
);
16173 if (scope
&& TYPE_P (scope
)
16174 && (!CLASS_TYPE_P (scope
)
16175 || (check_dependency_p
&& dependent_type_p (scope
))))
16177 /* We're optimizing away the call to cp_parser_lookup_name, but
16178 we still need to do this. */
16179 parser
->context
->object_type
= NULL_TREE
;
16185 /* Look up the name. */
16186 decl
= cp_parser_lookup_name (parser
, identifier
,
16188 /*is_template=*/true,
16189 /*is_namespace=*/false,
16190 check_dependency_p
,
16191 /*ambiguous_decls=*/NULL
,
16194 decl
= strip_using_decl (decl
);
16196 /* If DECL is a template, then the name was a template-name. */
16197 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16199 if (TREE_DEPRECATED (decl
)
16200 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16201 warn_deprecated_use (decl
, NULL_TREE
);
16205 /* The standard does not explicitly indicate whether a name that
16206 names a set of overloaded declarations, some of which are
16207 templates, is a template-name. However, such a name should
16208 be a template-name; otherwise, there is no way to form a
16209 template-id for the overloaded templates. */
16210 bool found
= false;
16212 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16213 !found
&& iter
; ++iter
)
16214 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16219 /* The name does not name a template. */
16220 cp_parser_error (parser
, "expected template-name");
16221 return error_mark_node
;
16225 /* If DECL is dependent, and refers to a function, then just return
16226 its name; we will look it up again during template instantiation. */
16227 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16229 tree scope
= ovl_scope (decl
);
16230 if (TYPE_P (scope
) && dependent_type_p (scope
))
16237 /* Parse a template-argument-list.
16239 template-argument-list:
16240 template-argument ... [opt]
16241 template-argument-list , template-argument ... [opt]
16243 Returns a TREE_VEC containing the arguments. */
16246 cp_parser_template_argument_list (cp_parser
* parser
)
16248 tree fixed_args
[10];
16249 unsigned n_args
= 0;
16250 unsigned alloced
= 10;
16251 tree
*arg_ary
= fixed_args
;
16253 bool saved_in_template_argument_list_p
;
16255 bool saved_non_ice_p
;
16257 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16258 parser
->in_template_argument_list_p
= true;
16259 /* Even if the template-id appears in an integral
16260 constant-expression, the contents of the argument list do
16262 saved_ice_p
= parser
->integral_constant_expression_p
;
16263 parser
->integral_constant_expression_p
= false;
16264 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16265 parser
->non_integral_constant_expression_p
= false;
16267 /* Parse the arguments. */
16273 /* Consume the comma. */
16274 cp_lexer_consume_token (parser
->lexer
);
16276 /* Parse the template-argument. */
16277 argument
= cp_parser_template_argument (parser
);
16279 /* If the next token is an ellipsis, we're expanding a template
16281 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16283 if (argument
== error_mark_node
)
16285 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16286 error_at (token
->location
,
16287 "expected parameter pack before %<...%>");
16289 /* Consume the `...' token. */
16290 cp_lexer_consume_token (parser
->lexer
);
16292 /* Make the argument into a TYPE_PACK_EXPANSION or
16293 EXPR_PACK_EXPANSION. */
16294 argument
= make_pack_expansion (argument
);
16297 if (n_args
== alloced
)
16301 if (arg_ary
== fixed_args
)
16303 arg_ary
= XNEWVEC (tree
, alloced
);
16304 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16307 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16309 arg_ary
[n_args
++] = argument
;
16311 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16313 vec
= make_tree_vec (n_args
);
16316 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16318 if (arg_ary
!= fixed_args
)
16320 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16321 parser
->integral_constant_expression_p
= saved_ice_p
;
16322 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16324 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16328 /* Parse a template-argument.
16331 assignment-expression
16335 The representation is that of an assignment-expression, type-id, or
16336 id-expression -- except that the qualified id-expression is
16337 evaluated, so that the value returned is either a DECL or an
16340 Although the standard says "assignment-expression", it forbids
16341 throw-expressions or assignments in the template argument.
16342 Therefore, we use "conditional-expression" instead. */
16345 cp_parser_template_argument (cp_parser
* parser
)
16350 bool maybe_type_id
= false;
16351 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16352 location_t loc
= 0;
16355 /* There's really no way to know what we're looking at, so we just
16356 try each alternative in order.
16360 In a template-argument, an ambiguity between a type-id and an
16361 expression is resolved to a type-id, regardless of the form of
16362 the corresponding template-parameter.
16364 Therefore, we try a type-id first. */
16365 cp_parser_parse_tentatively (parser
);
16366 argument
= cp_parser_template_type_arg (parser
);
16367 /* If there was no error parsing the type-id but the next token is a
16368 '>>', our behavior depends on which dialect of C++ we're
16369 parsing. In C++98, we probably found a typo for '> >'. But there
16370 are type-id which are also valid expressions. For instance:
16372 struct X { int operator >> (int); };
16373 template <int V> struct Foo {};
16376 Here 'X()' is a valid type-id of a function type, but the user just
16377 wanted to write the expression "X() >> 5". Thus, we remember that we
16378 found a valid type-id, but we still try to parse the argument as an
16379 expression to see what happens.
16381 In C++0x, the '>>' will be considered two separate '>'
16383 if (!cp_parser_error_occurred (parser
)
16384 && cxx_dialect
== cxx98
16385 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16387 maybe_type_id
= true;
16388 cp_parser_abort_tentative_parse (parser
);
16392 /* If the next token isn't a `,' or a `>', then this argument wasn't
16393 really finished. This means that the argument is not a valid
16395 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16396 cp_parser_error (parser
, "expected template-argument");
16397 /* If that worked, we're done. */
16398 if (cp_parser_parse_definitely (parser
))
16401 /* We're still not sure what the argument will be. */
16402 cp_parser_parse_tentatively (parser
);
16403 /* Try a template. */
16404 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16405 argument
= cp_parser_id_expression (parser
,
16406 /*template_keyword_p=*/false,
16407 /*check_dependency_p=*/true,
16409 /*declarator_p=*/false,
16410 /*optional_p=*/false);
16411 /* If the next token isn't a `,' or a `>', then this argument wasn't
16412 really finished. */
16413 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16414 cp_parser_error (parser
, "expected template-argument");
16415 if (!cp_parser_error_occurred (parser
))
16417 /* Figure out what is being referred to. If the id-expression
16418 was for a class template specialization, then we will have a
16419 TYPE_DECL at this point. There is no need to do name lookup
16420 at this point in that case. */
16421 if (TREE_CODE (argument
) != TYPE_DECL
)
16422 argument
= cp_parser_lookup_name (parser
, argument
,
16424 /*is_template=*/template_p
,
16425 /*is_namespace=*/false,
16426 /*check_dependency=*/true,
16427 /*ambiguous_decls=*/NULL
,
16428 argument_start_token
->location
);
16429 /* Handle a constrained-type-specifier for a non-type template
16431 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16433 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16434 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16435 cp_parser_error (parser
, "expected template-name");
16437 if (cp_parser_parse_definitely (parser
))
16439 if (TREE_DEPRECATED (argument
))
16440 warn_deprecated_use (argument
, NULL_TREE
);
16443 /* It must be a non-type argument. In C++17 any constant-expression is
16445 if (cxx_dialect
> cxx14
)
16448 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16450 -- an integral constant-expression of integral or enumeration
16453 -- the name of a non-type template-parameter; or
16455 -- the name of an object or function with external linkage...
16457 -- the address of an object or function with external linkage...
16459 -- a pointer to member... */
16460 /* Look for a non-type template parameter. */
16461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16463 cp_parser_parse_tentatively (parser
);
16464 argument
= cp_parser_primary_expression (parser
,
16465 /*address_p=*/false,
16467 /*template_arg_p=*/true,
16469 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16470 || !cp_parser_next_token_ends_template_argument_p (parser
))
16471 cp_parser_simulate_error (parser
);
16472 if (cp_parser_parse_definitely (parser
))
16476 /* If the next token is "&", the argument must be the address of an
16477 object or function with external linkage. */
16478 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16481 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16482 cp_lexer_consume_token (parser
->lexer
);
16484 /* See if we might have an id-expression. */
16485 token
= cp_lexer_peek_token (parser
->lexer
);
16486 if (token
->type
== CPP_NAME
16487 || token
->keyword
== RID_OPERATOR
16488 || token
->type
== CPP_SCOPE
16489 || token
->type
== CPP_TEMPLATE_ID
16490 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16492 cp_parser_parse_tentatively (parser
);
16493 argument
= cp_parser_primary_expression (parser
,
16496 /*template_arg_p=*/true,
16498 if (cp_parser_error_occurred (parser
)
16499 || !cp_parser_next_token_ends_template_argument_p (parser
))
16500 cp_parser_abort_tentative_parse (parser
);
16505 if (INDIRECT_REF_P (argument
))
16507 /* Strip the dereference temporarily. */
16508 gcc_assert (REFERENCE_REF_P (argument
));
16509 argument
= TREE_OPERAND (argument
, 0);
16512 /* If we're in a template, we represent a qualified-id referring
16513 to a static data member as a SCOPE_REF even if the scope isn't
16514 dependent so that we can check access control later. */
16516 if (TREE_CODE (probe
) == SCOPE_REF
)
16517 probe
= TREE_OPERAND (probe
, 1);
16520 /* A variable without external linkage might still be a
16521 valid constant-expression, so no error is issued here
16522 if the external-linkage check fails. */
16523 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16524 cp_parser_simulate_error (parser
);
16526 else if (is_overloaded_fn (argument
))
16527 /* All overloaded functions are allowed; if the external
16528 linkage test does not pass, an error will be issued
16532 && (TREE_CODE (argument
) == OFFSET_REF
16533 || TREE_CODE (argument
) == SCOPE_REF
))
16534 /* A pointer-to-member. */
16536 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16539 cp_parser_simulate_error (parser
);
16541 if (cp_parser_parse_definitely (parser
))
16544 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16545 tf_warning_or_error
);
16547 argument
= convert_from_reference (argument
);
16552 /* If the argument started with "&", there are no other valid
16553 alternatives at this point. */
16556 cp_parser_error (parser
, "invalid non-type template argument");
16557 return error_mark_node
;
16561 /* If the argument wasn't successfully parsed as a type-id followed
16562 by '>>', the argument can only be a constant expression now.
16563 Otherwise, we try parsing the constant-expression tentatively,
16564 because the argument could really be a type-id. */
16566 cp_parser_parse_tentatively (parser
);
16568 if (cxx_dialect
<= cxx14
)
16569 argument
= cp_parser_constant_expression (parser
);
16572 /* With C++17 generalized non-type template arguments we need to handle
16573 lvalue constant expressions, too. */
16574 argument
= cp_parser_assignment_expression (parser
);
16575 require_potential_constant_expression (argument
);
16578 if (!maybe_type_id
)
16580 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16581 cp_parser_error (parser
, "expected template-argument");
16582 if (cp_parser_parse_definitely (parser
))
16584 /* We did our best to parse the argument as a non type-id, but that
16585 was the only alternative that matched (albeit with a '>' after
16586 it). We can assume it's just a typo from the user, and a
16587 diagnostic will then be issued. */
16588 return cp_parser_template_type_arg (parser
);
16591 /* Parse an explicit-instantiation.
16593 explicit-instantiation:
16594 template declaration
16596 Although the standard says `declaration', what it really means is:
16598 explicit-instantiation:
16599 template decl-specifier-seq [opt] declarator [opt] ;
16601 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16602 supposed to be allowed. A defect report has been filed about this
16607 explicit-instantiation:
16608 storage-class-specifier template
16609 decl-specifier-seq [opt] declarator [opt] ;
16610 function-specifier template
16611 decl-specifier-seq [opt] declarator [opt] ; */
16614 cp_parser_explicit_instantiation (cp_parser
* parser
)
16616 int declares_class_or_enum
;
16617 cp_decl_specifier_seq decl_specifiers
;
16618 tree extension_specifier
= NULL_TREE
;
16620 timevar_push (TV_TEMPLATE_INST
);
16622 /* Look for an (optional) storage-class-specifier or
16623 function-specifier. */
16624 if (cp_parser_allow_gnu_extensions_p (parser
))
16626 extension_specifier
16627 = cp_parser_storage_class_specifier_opt (parser
);
16628 if (!extension_specifier
)
16629 extension_specifier
16630 = cp_parser_function_specifier_opt (parser
,
16631 /*decl_specs=*/NULL
);
16634 /* Look for the `template' keyword. */
16635 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16636 /* Let the front end know that we are processing an explicit
16638 begin_explicit_instantiation ();
16639 /* [temp.explicit] says that we are supposed to ignore access
16640 control while processing explicit instantiation directives. */
16641 push_deferring_access_checks (dk_no_check
);
16642 /* Parse a decl-specifier-seq. */
16643 cp_parser_decl_specifier_seq (parser
,
16644 CP_PARSER_FLAGS_OPTIONAL
,
16646 &declares_class_or_enum
);
16647 /* If there was exactly one decl-specifier, and it declared a class,
16648 and there's no declarator, then we have an explicit type
16650 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16654 type
= check_tag_decl (&decl_specifiers
,
16655 /*explicit_type_instantiation_p=*/true);
16656 /* Turn access control back on for names used during
16657 template instantiation. */
16658 pop_deferring_access_checks ();
16660 do_type_instantiation (type
, extension_specifier
,
16661 /*complain=*/tf_error
);
16665 cp_declarator
*declarator
;
16668 /* Parse the declarator. */
16670 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16671 /*ctor_dtor_or_conv_p=*/NULL
,
16672 /*parenthesized_p=*/NULL
,
16673 /*member_p=*/false,
16674 /*friend_p=*/false);
16675 if (declares_class_or_enum
& 2)
16676 cp_parser_check_for_definition_in_return_type (declarator
,
16677 decl_specifiers
.type
,
16678 decl_specifiers
.locations
[ds_type_spec
]);
16679 if (declarator
!= cp_error_declarator
)
16681 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16682 permerror (decl_specifiers
.locations
[ds_inline
],
16683 "explicit instantiation shall not use"
16684 " %<inline%> specifier");
16685 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16686 permerror (decl_specifiers
.locations
[ds_constexpr
],
16687 "explicit instantiation shall not use"
16688 " %<constexpr%> specifier");
16690 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16691 NORMAL
, 0, &decl_specifiers
.attributes
);
16692 /* Turn access control back on for names used during
16693 template instantiation. */
16694 pop_deferring_access_checks ();
16695 /* Do the explicit instantiation. */
16696 do_decl_instantiation (decl
, extension_specifier
);
16700 pop_deferring_access_checks ();
16701 /* Skip the body of the explicit instantiation. */
16702 cp_parser_skip_to_end_of_statement (parser
);
16705 /* We're done with the instantiation. */
16706 end_explicit_instantiation ();
16708 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16710 timevar_pop (TV_TEMPLATE_INST
);
16713 /* Parse an explicit-specialization.
16715 explicit-specialization:
16716 template < > declaration
16718 Although the standard says `declaration', what it really means is:
16720 explicit-specialization:
16721 template <> decl-specifier [opt] init-declarator [opt] ;
16722 template <> function-definition
16723 template <> explicit-specialization
16724 template <> template-declaration */
16727 cp_parser_explicit_specialization (cp_parser
* parser
)
16729 bool need_lang_pop
;
16730 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16732 /* Look for the `template' keyword. */
16733 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16734 /* Look for the `<'. */
16735 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16736 /* Look for the `>'. */
16737 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16738 /* We have processed another parameter list. */
16739 ++parser
->num_template_parameter_lists
;
16742 A template ... explicit specialization ... shall not have C
16744 if (current_lang_name
== lang_name_c
)
16746 error_at (token
->location
, "template specialization with C linkage");
16747 maybe_show_extern_c_location ();
16748 /* Give it C++ linkage to avoid confusing other parts of the
16750 push_lang_context (lang_name_cplusplus
);
16751 need_lang_pop
= true;
16754 need_lang_pop
= false;
16755 /* Let the front end know that we are beginning a specialization. */
16756 if (!begin_specialization ())
16758 end_specialization ();
16762 /* If the next keyword is `template', we need to figure out whether
16763 or not we're looking a template-declaration. */
16764 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16766 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16767 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16768 cp_parser_template_declaration_after_export (parser
,
16769 /*member_p=*/false);
16771 cp_parser_explicit_specialization (parser
);
16774 /* Parse the dependent declaration. */
16775 cp_parser_single_declaration (parser
,
16777 /*member_p=*/false,
16778 /*explicit_specialization_p=*/true,
16779 /*friend_p=*/NULL
);
16780 /* We're done with the specialization. */
16781 end_specialization ();
16782 /* For the erroneous case of a template with C linkage, we pushed an
16783 implicit C++ linkage scope; exit that scope now. */
16785 pop_lang_context ();
16786 /* We're done with this parameter list. */
16787 --parser
->num_template_parameter_lists
;
16790 /* Parse a type-specifier.
16793 simple-type-specifier
16796 elaborated-type-specifier
16804 Returns a representation of the type-specifier. For a
16805 class-specifier, enum-specifier, or elaborated-type-specifier, a
16806 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16808 The parser flags FLAGS is used to control type-specifier parsing.
16810 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16811 in a decl-specifier-seq.
16813 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16814 class-specifier, enum-specifier, or elaborated-type-specifier, then
16815 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16816 if a type is declared; 2 if it is defined. Otherwise, it is set to
16819 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16820 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16821 is set to FALSE. */
16824 cp_parser_type_specifier (cp_parser
* parser
,
16825 cp_parser_flags flags
,
16826 cp_decl_specifier_seq
*decl_specs
,
16827 bool is_declaration
,
16828 int* declares_class_or_enum
,
16829 bool* is_cv_qualifier
)
16831 tree type_spec
= NULL_TREE
;
16834 cp_decl_spec ds
= ds_last
;
16836 /* Assume this type-specifier does not declare a new type. */
16837 if (declares_class_or_enum
)
16838 *declares_class_or_enum
= 0;
16839 /* And that it does not specify a cv-qualifier. */
16840 if (is_cv_qualifier
)
16841 *is_cv_qualifier
= false;
16842 /* Peek at the next token. */
16843 token
= cp_lexer_peek_token (parser
->lexer
);
16845 /* If we're looking at a keyword, we can use that to guide the
16846 production we choose. */
16847 keyword
= token
->keyword
;
16851 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16852 goto elaborated_type_specifier
;
16854 /* Look for the enum-specifier. */
16855 type_spec
= cp_parser_enum_specifier (parser
);
16856 /* If that worked, we're done. */
16859 if (declares_class_or_enum
)
16860 *declares_class_or_enum
= 2;
16862 cp_parser_set_decl_spec_type (decl_specs
,
16865 /*type_definition_p=*/true);
16869 goto elaborated_type_specifier
;
16871 /* Any of these indicate either a class-specifier, or an
16872 elaborated-type-specifier. */
16876 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16877 goto elaborated_type_specifier
;
16879 /* Parse tentatively so that we can back up if we don't find a
16880 class-specifier. */
16881 cp_parser_parse_tentatively (parser
);
16882 /* Look for the class-specifier. */
16883 type_spec
= cp_parser_class_specifier (parser
);
16884 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16885 /* If that worked, we're done. */
16886 if (cp_parser_parse_definitely (parser
))
16888 if (declares_class_or_enum
)
16889 *declares_class_or_enum
= 2;
16891 cp_parser_set_decl_spec_type (decl_specs
,
16894 /*type_definition_p=*/true);
16898 /* Fall through. */
16899 elaborated_type_specifier
:
16900 /* We're declaring (not defining) a class or enum. */
16901 if (declares_class_or_enum
)
16902 *declares_class_or_enum
= 1;
16904 /* Fall through. */
16906 /* Look for an elaborated-type-specifier. */
16908 = (cp_parser_elaborated_type_specifier
16910 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16913 cp_parser_set_decl_spec_type (decl_specs
,
16916 /*type_definition_p=*/false);
16921 if (is_cv_qualifier
)
16922 *is_cv_qualifier
= true;
16927 if (is_cv_qualifier
)
16928 *is_cv_qualifier
= true;
16933 if (is_cv_qualifier
)
16934 *is_cv_qualifier
= true;
16938 /* The `__complex__' keyword is a GNU extension. */
16946 /* Handle simple keywords. */
16951 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16952 decl_specs
->any_specifiers_p
= true;
16954 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16957 /* If we do not already have a type-specifier, assume we are looking
16958 at a simple-type-specifier. */
16959 type_spec
= cp_parser_simple_type_specifier (parser
,
16963 /* If we didn't find a type-specifier, and a type-specifier was not
16964 optional in this context, issue an error message. */
16965 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16967 cp_parser_error (parser
, "expected type specifier");
16968 return error_mark_node
;
16974 /* Parse a simple-type-specifier.
16976 simple-type-specifier:
16977 :: [opt] nested-name-specifier [opt] type-name
16978 :: [opt] nested-name-specifier template template-id
16993 simple-type-specifier:
16995 decltype ( expression )
16998 __underlying_type ( type-id )
17002 nested-name-specifier(opt) template-name
17006 simple-type-specifier:
17008 __typeof__ unary-expression
17009 __typeof__ ( type-id )
17010 __typeof__ ( type-id ) { initializer-list , [opt] }
17012 Concepts Extension:
17014 simple-type-specifier:
17015 constrained-type-specifier
17017 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17018 appropriately updated. */
17021 cp_parser_simple_type_specifier (cp_parser
* parser
,
17022 cp_decl_specifier_seq
*decl_specs
,
17023 cp_parser_flags flags
)
17025 tree type
= NULL_TREE
;
17029 /* Peek at the next token. */
17030 token
= cp_lexer_peek_token (parser
->lexer
);
17032 /* If we're looking at a keyword, things are easy. */
17033 switch (token
->keyword
)
17037 decl_specs
->explicit_char_p
= true;
17038 type
= char_type_node
;
17041 type
= char16_type_node
;
17044 type
= char32_type_node
;
17047 type
= wchar_type_node
;
17050 type
= boolean_type_node
;
17053 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17054 type
= short_integer_type_node
;
17058 decl_specs
->explicit_int_p
= true;
17059 type
= integer_type_node
;
17065 idx
= token
->keyword
- RID_INT_N_0
;
17066 if (! int_n_enabled_p
[idx
])
17070 decl_specs
->explicit_intN_p
= true;
17071 decl_specs
->int_n_idx
= idx
;
17073 type
= int_n_trees
[idx
].signed_type
;
17077 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17078 type
= long_integer_type_node
;
17081 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17082 type
= integer_type_node
;
17085 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17086 type
= unsigned_type_node
;
17089 type
= float_type_node
;
17092 type
= double_type_node
;
17095 type
= void_type_node
;
17099 maybe_warn_cpp0x (CPP0X_AUTO
);
17100 if (parser
->auto_is_implicit_function_template_parm_p
)
17102 /* The 'auto' might be the placeholder return type for a function decl
17103 with trailing return type. */
17104 bool have_trailing_return_fn_decl
= false;
17106 cp_parser_parse_tentatively (parser
);
17107 cp_lexer_consume_token (parser
->lexer
);
17108 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17109 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17110 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17111 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17113 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17115 cp_lexer_consume_token (parser
->lexer
);
17116 cp_parser_skip_to_closing_parenthesis (parser
,
17117 /*recovering*/false,
17119 /*consume_paren*/true);
17123 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17125 have_trailing_return_fn_decl
= true;
17129 cp_lexer_consume_token (parser
->lexer
);
17131 cp_parser_abort_tentative_parse (parser
);
17133 if (have_trailing_return_fn_decl
)
17135 type
= make_auto ();
17139 if (cxx_dialect
>= cxx14
)
17141 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17142 type
= TREE_TYPE (type
);
17145 type
= error_mark_node
;
17147 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17149 if (cxx_dialect
< cxx14
)
17150 error_at (token
->location
,
17151 "use of %<auto%> in lambda parameter declaration "
17152 "only available with "
17153 "-std=c++14 or -std=gnu++14");
17155 else if (cxx_dialect
< cxx14
)
17156 error_at (token
->location
,
17157 "use of %<auto%> in parameter declaration "
17158 "only available with "
17159 "-std=c++14 or -std=gnu++14");
17160 else if (!flag_concepts
)
17161 pedwarn (token
->location
, 0,
17162 "use of %<auto%> in parameter declaration "
17163 "only available with -fconcepts");
17166 type
= make_auto ();
17170 /* Since DR 743, decltype can either be a simple-type-specifier by
17171 itself or begin a nested-name-specifier. Parsing it will replace
17172 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17173 handling below decide what to do. */
17174 cp_parser_decltype (parser
);
17175 cp_lexer_set_token_position (parser
->lexer
, token
);
17179 /* Consume the `typeof' token. */
17180 cp_lexer_consume_token (parser
->lexer
);
17181 /* Parse the operand to `typeof'. */
17182 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17183 /* If it is not already a TYPE, take its type. */
17184 if (!TYPE_P (type
))
17185 type
= finish_typeof (type
);
17188 cp_parser_set_decl_spec_type (decl_specs
, type
,
17190 /*type_definition_p=*/false);
17194 case RID_UNDERLYING_TYPE
:
17195 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17197 cp_parser_set_decl_spec_type (decl_specs
, type
,
17199 /*type_definition_p=*/false);
17204 case RID_DIRECT_BASES
:
17205 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17207 cp_parser_set_decl_spec_type (decl_specs
, type
,
17209 /*type_definition_p=*/false);
17215 /* If token is an already-parsed decltype not followed by ::,
17216 it's a simple-type-specifier. */
17217 if (token
->type
== CPP_DECLTYPE
17218 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17220 type
= saved_checks_value (token
->u
.tree_check_value
);
17223 cp_parser_set_decl_spec_type (decl_specs
, type
,
17225 /*type_definition_p=*/false);
17226 /* Remember that we are handling a decltype in order to
17227 implement the resolution of DR 1510 when the argument
17228 isn't instantiation dependent. */
17229 decl_specs
->decltype_p
= true;
17231 cp_lexer_consume_token (parser
->lexer
);
17235 /* If the type-specifier was for a built-in type, we're done. */
17238 /* Record the type. */
17240 && (token
->keyword
!= RID_SIGNED
17241 && token
->keyword
!= RID_UNSIGNED
17242 && token
->keyword
!= RID_SHORT
17243 && token
->keyword
!= RID_LONG
))
17244 cp_parser_set_decl_spec_type (decl_specs
,
17247 /*type_definition_p=*/false);
17249 decl_specs
->any_specifiers_p
= true;
17251 /* Consume the token. */
17252 cp_lexer_consume_token (parser
->lexer
);
17254 if (type
== error_mark_node
)
17255 return error_mark_node
;
17257 /* There is no valid C++ program where a non-template type is
17258 followed by a "<". That usually indicates that the user thought
17259 that the type was a template. */
17260 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17263 return TYPE_NAME (type
);
17266 /* The type-specifier must be a user-defined type. */
17267 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17272 /* Don't gobble tokens or issue error messages if this is an
17273 optional type-specifier. */
17274 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17275 cp_parser_parse_tentatively (parser
);
17277 token
= cp_lexer_peek_token (parser
->lexer
);
17279 /* Look for the optional `::' operator. */
17281 = (cp_parser_global_scope_opt (parser
,
17282 /*current_scope_valid_p=*/false)
17284 /* Look for the nested-name specifier. */
17286 = (cp_parser_nested_name_specifier_opt (parser
,
17287 /*typename_keyword_p=*/false,
17288 /*check_dependency_p=*/true,
17290 /*is_declaration=*/false)
17292 /* If we have seen a nested-name-specifier, and the next token
17293 is `template', then we are using the template-id production. */
17295 && cp_parser_optional_template_keyword (parser
))
17297 /* Look for the template-id. */
17298 type
= cp_parser_template_id (parser
,
17299 /*template_keyword_p=*/true,
17300 /*check_dependency_p=*/true,
17302 /*is_declaration=*/false);
17303 /* If the template-id did not name a type, we are out of
17305 if (TREE_CODE (type
) != TYPE_DECL
)
17307 cp_parser_error (parser
, "expected template-id for type");
17311 /* Otherwise, look for a type-name. */
17313 type
= cp_parser_type_name (parser
);
17314 /* Keep track of all name-lookups performed in class scopes. */
17318 && TREE_CODE (type
) == TYPE_DECL
17319 && identifier_p (DECL_NAME (type
)))
17320 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17321 /* If it didn't work out, we don't have a TYPE. */
17322 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17323 && !cp_parser_parse_definitely (parser
))
17325 if (!type
&& cxx_dialect
>= cxx17
)
17327 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17328 cp_parser_parse_tentatively (parser
);
17330 cp_parser_global_scope_opt (parser
,
17331 /*current_scope_valid_p=*/false);
17332 cp_parser_nested_name_specifier_opt (parser
,
17333 /*typename_keyword_p=*/false,
17334 /*check_dependency_p=*/true,
17336 /*is_declaration=*/false);
17337 tree name
= cp_parser_identifier (parser
);
17338 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17339 && parser
->scope
!= error_mark_node
)
17341 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17343 /*is_template=*/false,
17344 /*is_namespace=*/false,
17345 /*check_dependency=*/true,
17346 /*ambiguous_decls=*/NULL
,
17348 if (tmpl
&& tmpl
!= error_mark_node
17349 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17350 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17351 type
= make_template_placeholder (tmpl
);
17354 type
= error_mark_node
;
17355 if (!cp_parser_simulate_error (parser
))
17356 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17357 NLE_TYPE
, token
->location
);
17361 type
= error_mark_node
;
17363 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17364 && !cp_parser_parse_definitely (parser
))
17367 if (type
&& decl_specs
)
17368 cp_parser_set_decl_spec_type (decl_specs
, type
,
17370 /*type_definition_p=*/false);
17373 /* If we didn't get a type-name, issue an error message. */
17374 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17376 cp_parser_error (parser
, "expected type-name");
17377 return error_mark_node
;
17380 if (type
&& type
!= error_mark_node
)
17382 /* See if TYPE is an Objective-C type, and if so, parse and
17383 accept any protocol references following it. Do this before
17384 the cp_parser_check_for_invalid_template_id() call, because
17385 Objective-C types can be followed by '<...>' which would
17386 enclose protocol names rather than template arguments, and so
17387 everything is fine. */
17388 if (c_dialect_objc () && !parser
->scope
17389 && (objc_is_id (type
) || objc_is_class_name (type
)))
17391 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17392 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17394 /* Clobber the "unqualified" type previously entered into
17395 DECL_SPECS with the new, improved protocol-qualified version. */
17397 decl_specs
->type
= qual_type
;
17402 /* There is no valid C++ program where a non-template type is
17403 followed by a "<". That usually indicates that the user
17404 thought that the type was a template. */
17405 cp_parser_check_for_invalid_template_id (parser
, type
,
17413 /* Parse a type-name.
17419 simple-template-id [in c++0x]
17436 Returns a TYPE_DECL for the type. */
17439 cp_parser_type_name (cp_parser
* parser
)
17441 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17446 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17450 /* We can't know yet whether it is a class-name or not. */
17451 cp_parser_parse_tentatively (parser
);
17452 /* Try a class-name. */
17453 type_decl
= cp_parser_class_name (parser
,
17454 typename_keyword_p
,
17455 /*template_keyword_p=*/false,
17457 /*check_dependency_p=*/true,
17458 /*class_head_p=*/false,
17459 /*is_declaration=*/false);
17460 /* If it's not a class-name, keep looking. */
17461 if (!cp_parser_parse_definitely (parser
))
17463 if (cxx_dialect
< cxx11
)
17464 /* It must be a typedef-name or an enum-name. */
17465 return cp_parser_nonclass_name (parser
);
17467 cp_parser_parse_tentatively (parser
);
17468 /* It is either a simple-template-id representing an
17469 instantiation of an alias template... */
17470 type_decl
= cp_parser_template_id (parser
,
17471 /*template_keyword_p=*/false,
17472 /*check_dependency_p=*/true,
17474 /*is_declaration=*/false);
17475 /* Note that this must be an instantiation of an alias template
17476 because [temp.names]/6 says:
17478 A template-id that names an alias template specialization
17481 Whereas [temp.names]/7 says:
17483 A simple-template-id that names a class template
17484 specialization is a class-name.
17486 With concepts, this could also be a partial-concept-id that
17487 declares a non-type template parameter. */
17488 if (type_decl
!= NULL_TREE
17489 && TREE_CODE (type_decl
) == TYPE_DECL
17490 && TYPE_DECL_ALIAS_P (type_decl
))
17491 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17492 else if (is_constrained_parameter (type_decl
))
17493 /* Don't do anything. */ ;
17495 cp_parser_simulate_error (parser
);
17497 if (!cp_parser_parse_definitely (parser
))
17498 /* ... Or a typedef-name or an enum-name. */
17499 return cp_parser_nonclass_name (parser
);
17505 /* Check if DECL and ARGS can form a constrained-type-specifier.
17506 If ARGS is non-null, we try to form a concept check of the
17507 form DECL<?, ARGS> where ? is a wildcard that matches any
17508 kind of template argument. If ARGS is NULL, then we try to
17509 form a concept check of the form DECL<?>. */
17512 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17513 tree decl
, tree args
)
17515 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17517 /* If we a constrained-type-specifier cannot be deduced. */
17518 if (parser
->prevent_constrained_type_specifiers
)
17521 /* A constrained type specifier can only be found in an
17522 overload set or as a reference to a template declaration.
17524 FIXME: This might be masking a bug. It's possible that
17525 that the deduction below is causing template specializations
17526 to be formed with the wildcard as an argument. */
17527 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17530 /* Try to build a call expression that evaluates the
17531 concept. This can fail if the overload set refers
17532 only to non-templates. */
17533 tree placeholder
= build_nt (WILDCARD_DECL
);
17534 tree check
= build_concept_check (decl
, placeholder
, args
);
17535 if (check
== error_mark_node
)
17538 /* Deduce the checked constraint and the prototype parameter.
17540 FIXME: In certain cases, failure to deduce should be a
17541 diagnosable error. */
17544 if (!deduce_constrained_parameter (check
, conc
, proto
))
17547 /* In template parameter scope, this results in a constrained
17548 parameter. Return a descriptor of that parm. */
17549 if (processing_template_parmlist
)
17550 return build_constrained_parameter (conc
, proto
, args
);
17552 /* In a parameter-declaration-clause, constrained-type
17553 specifiers result in invented template parameters. */
17554 if (parser
->auto_is_implicit_function_template_parm_p
)
17556 tree x
= build_constrained_parameter (conc
, proto
, args
);
17557 return synthesize_implicit_template_parm (parser
, x
);
17561 /* Otherwise, we're in a context where the constrained
17562 type name is deduced and the constraint applies
17563 after deduction. */
17564 return make_constrained_auto (conc
, args
);
17570 /* If DECL refers to a concept, return a TYPE_DECL representing
17571 the result of using the constrained type specifier in the
17572 current context. DECL refers to a concept if
17574 - it is an overload set containing a function concept taking a single
17577 - it is a variable concept taking a single type argument. */
17580 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17583 && (TREE_CODE (decl
) == OVERLOAD
17584 || BASELINK_P (decl
)
17585 || variable_concept_p (decl
)))
17586 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17591 /* Check if DECL and ARGS form a partial-concept-id. If so,
17592 assign ID to the resulting constrained placeholder.
17594 Returns true if the partial-concept-id designates a placeholder
17595 and false otherwise. Note that *id is set to NULL_TREE in
17599 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17601 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17604 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17616 Returns a TYPE_DECL for the type. */
17619 cp_parser_nonclass_name (cp_parser
* parser
)
17624 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17625 identifier
= cp_parser_identifier (parser
);
17626 if (identifier
== error_mark_node
)
17627 return error_mark_node
;
17629 /* Look up the type-name. */
17630 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17632 type_decl
= strip_using_decl (type_decl
);
17634 /* If we found an overload set, then it may refer to a concept-name. */
17635 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17638 if (TREE_CODE (type_decl
) != TYPE_DECL
17639 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17641 /* See if this is an Objective-C type. */
17642 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17643 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17645 type_decl
= TYPE_NAME (type
);
17648 /* Issue an error if we did not find a type-name. */
17649 if (TREE_CODE (type_decl
) != TYPE_DECL
17650 /* In Objective-C, we have the complication that class names are
17651 normally type names and start declarations (eg, the
17652 "NSObject" in "NSObject *object;"), but can be used in an
17653 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17654 is an expression. So, a classname followed by a dot is not a
17655 valid type-name. */
17656 || (objc_is_class_name (TREE_TYPE (type_decl
))
17657 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17659 if (!cp_parser_simulate_error (parser
))
17660 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17661 NLE_TYPE
, token
->location
);
17662 return error_mark_node
;
17664 /* Remember that the name was used in the definition of the
17665 current class so that we can check later to see if the
17666 meaning would have been different after the class was
17667 entirely defined. */
17668 else if (type_decl
!= error_mark_node
17670 maybe_note_name_used_in_class (identifier
, type_decl
);
17675 /* Parse an elaborated-type-specifier. Note that the grammar given
17676 here incorporates the resolution to DR68.
17678 elaborated-type-specifier:
17679 class-key :: [opt] nested-name-specifier [opt] identifier
17680 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17681 enum-key :: [opt] nested-name-specifier [opt] identifier
17682 typename :: [opt] nested-name-specifier identifier
17683 typename :: [opt] nested-name-specifier template [opt]
17688 elaborated-type-specifier:
17689 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17690 class-key attributes :: [opt] nested-name-specifier [opt]
17691 template [opt] template-id
17692 enum attributes :: [opt] nested-name-specifier [opt] identifier
17694 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17695 declared `friend'. If IS_DECLARATION is TRUE, then this
17696 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17697 something is being declared.
17699 Returns the TYPE specified. */
17702 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17704 bool is_declaration
)
17706 enum tag_types tag_type
;
17708 tree type
= NULL_TREE
;
17709 tree attributes
= NULL_TREE
;
17711 cp_token
*token
= NULL
;
17713 /* See if we're looking at the `enum' keyword. */
17714 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17716 /* Consume the `enum' token. */
17717 cp_lexer_consume_token (parser
->lexer
);
17718 /* Remember that it's an enumeration type. */
17719 tag_type
= enum_type
;
17720 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17721 enums) is used here. */
17722 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17723 if (cp_parser_is_keyword (token
, RID_CLASS
)
17724 || cp_parser_is_keyword (token
, RID_STRUCT
))
17726 gcc_rich_location
richloc (token
->location
);
17727 richloc
.add_range (input_location
, false);
17728 richloc
.add_fixit_remove ();
17729 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17730 "a scoped enum must not use the %qD keyword",
17732 /* Consume the `struct' or `class' and parse it anyway. */
17733 cp_lexer_consume_token (parser
->lexer
);
17735 /* Parse the attributes. */
17736 attributes
= cp_parser_attributes_opt (parser
);
17738 /* Or, it might be `typename'. */
17739 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17742 /* Consume the `typename' token. */
17743 cp_lexer_consume_token (parser
->lexer
);
17744 /* Remember that it's a `typename' type. */
17745 tag_type
= typename_type
;
17747 /* Otherwise it must be a class-key. */
17750 tag_type
= cp_parser_class_key (parser
);
17751 if (tag_type
== none_type
)
17752 return error_mark_node
;
17753 /* Parse the attributes. */
17754 attributes
= cp_parser_attributes_opt (parser
);
17757 /* Look for the `::' operator. */
17758 globalscope
= cp_parser_global_scope_opt (parser
,
17759 /*current_scope_valid_p=*/false);
17760 /* Look for the nested-name-specifier. */
17761 tree nested_name_specifier
;
17762 if (tag_type
== typename_type
&& !globalscope
)
17764 nested_name_specifier
17765 = cp_parser_nested_name_specifier (parser
,
17766 /*typename_keyword_p=*/true,
17767 /*check_dependency_p=*/true,
17770 if (!nested_name_specifier
)
17771 return error_mark_node
;
17774 /* Even though `typename' is not present, the proposed resolution
17775 to Core Issue 180 says that in `class A<T>::B', `B' should be
17776 considered a type-name, even if `A<T>' is dependent. */
17777 nested_name_specifier
17778 = cp_parser_nested_name_specifier_opt (parser
,
17779 /*typename_keyword_p=*/true,
17780 /*check_dependency_p=*/true,
17783 /* For everything but enumeration types, consider a template-id.
17784 For an enumeration type, consider only a plain identifier. */
17785 if (tag_type
!= enum_type
)
17787 bool template_p
= false;
17790 /* Allow the `template' keyword. */
17791 template_p
= cp_parser_optional_template_keyword (parser
);
17792 /* If we didn't see `template', we don't know if there's a
17793 template-id or not. */
17795 cp_parser_parse_tentatively (parser
);
17796 /* Parse the template-id. */
17797 token
= cp_lexer_peek_token (parser
->lexer
);
17798 decl
= cp_parser_template_id (parser
, template_p
,
17799 /*check_dependency_p=*/true,
17802 /* If we didn't find a template-id, look for an ordinary
17804 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17806 /* We can get here when cp_parser_template_id, called by
17807 cp_parser_class_name with tag_type == none_type, succeeds
17808 and caches a BASELINK. Then, when called again here,
17809 instead of failing and returning an error_mark_node
17810 returns it (see template/typename17.C in C++11).
17811 ??? Could we diagnose this earlier? */
17812 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17814 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17815 type
= error_mark_node
;
17817 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17818 in effect, then we must assume that, upon instantiation, the
17819 template will correspond to a class. */
17820 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17821 && tag_type
== typename_type
)
17822 type
= make_typename_type (parser
->scope
, decl
,
17824 /*complain=*/tf_error
);
17825 /* If the `typename' keyword is in effect and DECL is not a type
17826 decl, then type is non existent. */
17827 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17829 else if (TREE_CODE (decl
) == TYPE_DECL
)
17831 type
= check_elaborated_type_specifier (tag_type
, decl
,
17832 /*allow_template_p=*/true);
17834 /* If the next token is a semicolon, this must be a specialization,
17835 instantiation, or friend declaration. Check the scope while we
17836 still know whether or not we had a nested-name-specifier. */
17837 if (type
!= error_mark_node
17838 && !nested_name_specifier
&& !is_friend
17839 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17840 check_unqualified_spec_or_inst (type
, token
->location
);
17842 else if (decl
== error_mark_node
)
17843 type
= error_mark_node
;
17848 token
= cp_lexer_peek_token (parser
->lexer
);
17849 identifier
= cp_parser_identifier (parser
);
17851 if (identifier
== error_mark_node
)
17853 parser
->scope
= NULL_TREE
;
17854 return error_mark_node
;
17857 /* For a `typename', we needn't call xref_tag. */
17858 if (tag_type
== typename_type
17859 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17860 return cp_parser_make_typename_type (parser
, identifier
,
17863 /* Template parameter lists apply only if we are not within a
17864 function parameter list. */
17865 bool template_parm_lists_apply
17866 = parser
->num_template_parameter_lists
;
17867 if (template_parm_lists_apply
)
17868 for (cp_binding_level
*s
= current_binding_level
;
17869 s
&& s
->kind
!= sk_template_parms
;
17870 s
= s
->level_chain
)
17871 if (s
->kind
== sk_function_parms
)
17872 template_parm_lists_apply
= false;
17874 /* Look up a qualified name in the usual way. */
17878 tree ambiguous_decls
;
17880 decl
= cp_parser_lookup_name (parser
, identifier
,
17882 /*is_template=*/false,
17883 /*is_namespace=*/false,
17884 /*check_dependency=*/true,
17888 /* If the lookup was ambiguous, an error will already have been
17890 if (ambiguous_decls
)
17891 return error_mark_node
;
17893 /* If we are parsing friend declaration, DECL may be a
17894 TEMPLATE_DECL tree node here. However, we need to check
17895 whether this TEMPLATE_DECL results in valid code. Consider
17896 the following example:
17899 template <class T> class C {};
17902 template <class T> friend class N::C; // #1, valid code
17904 template <class T> class Y {
17905 friend class N::C; // #2, invalid code
17908 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17909 name lookup of `N::C'. We see that friend declaration must
17910 be template for the code to be valid. Note that
17911 processing_template_decl does not work here since it is
17912 always 1 for the above two cases. */
17914 decl
= (cp_parser_maybe_treat_template_as_class
17915 (decl
, /*tag_name_p=*/is_friend
17916 && template_parm_lists_apply
));
17918 if (TREE_CODE (decl
) != TYPE_DECL
)
17920 cp_parser_diagnose_invalid_type_name (parser
,
17923 return error_mark_node
;
17926 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17928 bool allow_template
= (template_parm_lists_apply
17929 || DECL_SELF_REFERENCE_P (decl
));
17930 type
= check_elaborated_type_specifier (tag_type
, decl
,
17933 if (type
== error_mark_node
)
17934 return error_mark_node
;
17937 /* Forward declarations of nested types, such as
17942 are invalid unless all components preceding the final '::'
17943 are complete. If all enclosing types are complete, these
17944 declarations become merely pointless.
17946 Invalid forward declarations of nested types are errors
17947 caught elsewhere in parsing. Those that are pointless arrive
17950 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17951 && !is_friend
&& !processing_explicit_instantiation
)
17952 warning (0, "declaration %qD does not declare anything", decl
);
17954 type
= TREE_TYPE (decl
);
17958 /* An elaborated-type-specifier sometimes introduces a new type and
17959 sometimes names an existing type. Normally, the rule is that it
17960 introduces a new type only if there is not an existing type of
17961 the same name already in scope. For example, given:
17964 void f() { struct S s; }
17966 the `struct S' in the body of `f' is the same `struct S' as in
17967 the global scope; the existing definition is used. However, if
17968 there were no global declaration, this would introduce a new
17969 local class named `S'.
17971 An exception to this rule applies to the following code:
17973 namespace N { struct S; }
17975 Here, the elaborated-type-specifier names a new type
17976 unconditionally; even if there is already an `S' in the
17977 containing scope this declaration names a new type.
17978 This exception only applies if the elaborated-type-specifier
17979 forms the complete declaration:
17983 A declaration consisting solely of `class-key identifier ;' is
17984 either a redeclaration of the name in the current scope or a
17985 forward declaration of the identifier as a class name. It
17986 introduces the name into the current scope.
17988 We are in this situation precisely when the next token is a `;'.
17990 An exception to the exception is that a `friend' declaration does
17991 *not* name a new type; i.e., given:
17993 struct S { friend struct T; };
17995 `T' is not a new type in the scope of `S'.
17997 Also, `new struct S' or `sizeof (struct S)' never results in the
17998 definition of a new type; a new type can only be declared in a
17999 declaration context. */
18005 /* Friends have special name lookup rules. */
18006 ts
= ts_within_enclosing_non_class
;
18007 else if (is_declaration
18008 && cp_lexer_next_token_is (parser
->lexer
,
18010 /* This is a `class-key identifier ;' */
18016 (template_parm_lists_apply
18017 && (cp_parser_next_token_starts_class_definition_p (parser
)
18018 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18019 /* An unqualified name was used to reference this type, so
18020 there were no qualifying templates. */
18021 if (template_parm_lists_apply
18022 && !cp_parser_check_template_parameters (parser
,
18023 /*num_templates=*/0,
18024 /*template_id*/false,
18026 /*declarator=*/NULL
))
18027 return error_mark_node
;
18028 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18032 if (type
== error_mark_node
)
18033 return error_mark_node
;
18035 /* Allow attributes on forward declarations of classes. */
18038 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18039 warning (OPT_Wattributes
,
18040 "attributes ignored on uninstantiated type");
18041 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18042 && ! processing_explicit_instantiation
)
18043 warning (OPT_Wattributes
,
18044 "attributes ignored on template instantiation");
18045 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18046 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18048 warning (OPT_Wattributes
,
18049 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18052 if (tag_type
!= enum_type
)
18054 /* Indicate whether this class was declared as a `class' or as a
18056 if (CLASS_TYPE_P (type
))
18057 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18058 cp_parser_check_class_key (tag_type
, type
);
18061 /* A "<" cannot follow an elaborated type specifier. If that
18062 happens, the user was probably trying to form a template-id. */
18063 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18069 /* Parse an enum-specifier.
18072 enum-head { enumerator-list [opt] }
18073 enum-head { enumerator-list , } [C++0x]
18076 enum-key identifier [opt] enum-base [opt]
18077 enum-key nested-name-specifier identifier enum-base [opt]
18082 enum struct [C++0x]
18085 : type-specifier-seq
18087 opaque-enum-specifier:
18088 enum-key identifier enum-base [opt] ;
18091 enum-key attributes[opt] identifier [opt] enum-base [opt]
18092 { enumerator-list [opt] }attributes[opt]
18093 enum-key attributes[opt] identifier [opt] enum-base [opt]
18094 { enumerator-list, }attributes[opt] [C++0x]
18096 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18097 if the token stream isn't an enum-specifier after all. */
18100 cp_parser_enum_specifier (cp_parser
* parser
)
18103 tree type
= NULL_TREE
;
18105 tree nested_name_specifier
= NULL_TREE
;
18107 bool scoped_enum_p
= false;
18108 bool has_underlying_type
= false;
18109 bool nested_being_defined
= false;
18110 bool new_value_list
= false;
18111 bool is_new_type
= false;
18112 bool is_unnamed
= false;
18113 tree underlying_type
= NULL_TREE
;
18114 cp_token
*type_start_token
= NULL
;
18115 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18117 parser
->colon_corrects_to_scope_p
= false;
18119 /* Parse tentatively so that we can back up if we don't find a
18121 cp_parser_parse_tentatively (parser
);
18123 /* Caller guarantees that the current token is 'enum', an identifier
18124 possibly follows, and the token after that is an opening brace.
18125 If we don't have an identifier, fabricate an anonymous name for
18126 the enumeration being defined. */
18127 cp_lexer_consume_token (parser
->lexer
);
18129 /* Parse the "class" or "struct", which indicates a scoped
18130 enumeration type in C++0x. */
18131 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18132 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18134 if (cxx_dialect
< cxx11
)
18135 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18137 /* Consume the `struct' or `class' token. */
18138 cp_lexer_consume_token (parser
->lexer
);
18140 scoped_enum_p
= true;
18143 attributes
= cp_parser_attributes_opt (parser
);
18145 /* Clear the qualification. */
18146 parser
->scope
= NULL_TREE
;
18147 parser
->qualifying_scope
= NULL_TREE
;
18148 parser
->object_scope
= NULL_TREE
;
18150 /* Figure out in what scope the declaration is being placed. */
18151 prev_scope
= current_scope ();
18153 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18155 push_deferring_access_checks (dk_no_check
);
18156 nested_name_specifier
18157 = cp_parser_nested_name_specifier_opt (parser
,
18158 /*typename_keyword_p=*/true,
18159 /*check_dependency_p=*/false,
18161 /*is_declaration=*/false);
18163 if (nested_name_specifier
)
18167 identifier
= cp_parser_identifier (parser
);
18168 name
= cp_parser_lookup_name (parser
, identifier
,
18170 /*is_template=*/false,
18171 /*is_namespace=*/false,
18172 /*check_dependency=*/true,
18173 /*ambiguous_decls=*/NULL
,
18175 if (name
&& name
!= error_mark_node
)
18177 type
= TREE_TYPE (name
);
18178 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18180 /* Are template enums allowed in ISO? */
18181 if (template_parm_scope_p ())
18182 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18183 "%qD is an enumeration template", name
);
18184 /* ignore a typename reference, for it will be solved by name
18189 else if (nested_name_specifier
== error_mark_node
)
18190 /* We already issued an error. */;
18193 error_at (type_start_token
->location
,
18194 "%qD does not name an enumeration in %qT",
18195 identifier
, nested_name_specifier
);
18196 nested_name_specifier
= error_mark_node
;
18201 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18202 identifier
= cp_parser_identifier (parser
);
18205 identifier
= make_anon_name ();
18208 error_at (type_start_token
->location
,
18209 "unnamed scoped enum is not allowed");
18212 pop_deferring_access_checks ();
18214 /* Check for the `:' that denotes a specified underlying type in C++0x.
18215 Note that a ':' could also indicate a bitfield width, however. */
18216 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18218 cp_decl_specifier_seq type_specifiers
;
18220 /* Consume the `:'. */
18221 cp_lexer_consume_token (parser
->lexer
);
18223 /* Parse the type-specifier-seq. */
18224 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18225 /*is_trailing_return=*/false,
18228 /* At this point this is surely not elaborated type specifier. */
18229 if (!cp_parser_parse_definitely (parser
))
18232 if (cxx_dialect
< cxx11
)
18233 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18235 has_underlying_type
= true;
18237 /* If that didn't work, stop. */
18238 if (type_specifiers
.type
!= error_mark_node
)
18240 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18241 /*initialized=*/0, NULL
);
18242 if (underlying_type
== error_mark_node
18243 || check_for_bare_parameter_packs (underlying_type
))
18244 underlying_type
= NULL_TREE
;
18248 /* Look for the `{' but don't consume it yet. */
18249 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18251 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18253 cp_parser_error (parser
, "expected %<{%>");
18254 if (has_underlying_type
)
18260 /* An opaque-enum-specifier must have a ';' here. */
18261 if ((scoped_enum_p
|| underlying_type
)
18262 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18264 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18265 if (has_underlying_type
)
18273 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18276 if (nested_name_specifier
)
18278 if (CLASS_TYPE_P (nested_name_specifier
))
18280 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18281 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18282 push_scope (nested_name_specifier
);
18284 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18286 push_nested_namespace (nested_name_specifier
);
18290 /* Issue an error message if type-definitions are forbidden here. */
18291 if (!cp_parser_check_type_definition (parser
))
18292 type
= error_mark_node
;
18294 /* Create the new type. We do this before consuming the opening
18295 brace so the enum will be recorded as being on the line of its
18296 tag (or the 'enum' keyword, if there is no tag). */
18297 type
= start_enum (identifier
, type
, underlying_type
,
18298 attributes
, scoped_enum_p
, &is_new_type
);
18300 /* If the next token is not '{' it is an opaque-enum-specifier or an
18301 elaborated-type-specifier. */
18302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18304 timevar_push (TV_PARSE_ENUM
);
18305 if (nested_name_specifier
18306 && nested_name_specifier
!= error_mark_node
)
18308 /* The following catches invalid code such as:
18309 enum class S<int>::E { A, B, C }; */
18310 if (!processing_specialization
18311 && CLASS_TYPE_P (nested_name_specifier
)
18312 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18313 error_at (type_start_token
->location
, "cannot add an enumerator "
18314 "list to a template instantiation");
18316 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18318 error_at (type_start_token
->location
,
18319 "%<%T::%E%> has not been declared",
18320 TYPE_CONTEXT (nested_name_specifier
),
18321 nested_name_specifier
);
18322 type
= error_mark_node
;
18324 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18325 && !CLASS_TYPE_P (nested_name_specifier
))
18327 error_at (type_start_token
->location
, "nested name specifier "
18328 "%qT for enum declaration does not name a class "
18329 "or namespace", nested_name_specifier
);
18330 type
= error_mark_node
;
18332 /* If that scope does not contain the scope in which the
18333 class was originally declared, the program is invalid. */
18334 else if (prev_scope
&& !is_ancestor (prev_scope
,
18335 nested_name_specifier
))
18337 if (at_namespace_scope_p ())
18338 error_at (type_start_token
->location
,
18339 "declaration of %qD in namespace %qD which does not "
18341 type
, prev_scope
, nested_name_specifier
);
18343 error_at (type_start_token
->location
,
18344 "declaration of %qD in %qD which does not "
18346 type
, prev_scope
, nested_name_specifier
);
18347 type
= error_mark_node
;
18349 /* If that scope is the scope where the declaration is being placed
18350 the program is invalid. */
18351 else if (CLASS_TYPE_P (nested_name_specifier
)
18352 && CLASS_TYPE_P (prev_scope
)
18353 && same_type_p (nested_name_specifier
, prev_scope
))
18355 permerror (type_start_token
->location
,
18356 "extra qualification not allowed");
18357 nested_name_specifier
= NULL_TREE
;
18362 begin_scope (sk_scoped_enum
, type
);
18364 /* Consume the opening brace. */
18365 matching_braces braces
;
18366 braces
.consume_open (parser
);
18368 if (type
== error_mark_node
)
18369 ; /* Nothing to add */
18370 else if (OPAQUE_ENUM_P (type
)
18371 || (cxx_dialect
> cxx98
&& processing_specialization
))
18373 new_value_list
= true;
18374 SET_OPAQUE_ENUM_P (type
, false);
18375 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18379 error_at (type_start_token
->location
,
18380 "multiple definition of %q#T", type
);
18381 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18382 "previous definition here");
18383 type
= error_mark_node
;
18386 if (type
== error_mark_node
)
18387 cp_parser_skip_to_end_of_block_or_statement (parser
);
18388 /* If the next token is not '}', then there are some enumerators. */
18389 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18391 if (is_unnamed
&& !scoped_enum_p
)
18392 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18393 "ISO C++ forbids empty unnamed enum");
18396 cp_parser_enumerator_list (parser
, type
);
18398 /* Consume the final '}'. */
18399 braces
.require_close (parser
);
18403 timevar_pop (TV_PARSE_ENUM
);
18407 /* If a ';' follows, then it is an opaque-enum-specifier
18408 and additional restrictions apply. */
18409 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18412 error_at (type_start_token
->location
,
18413 "opaque-enum-specifier without name");
18414 else if (nested_name_specifier
)
18415 error_at (type_start_token
->location
,
18416 "opaque-enum-specifier must use a simple identifier");
18420 /* Look for trailing attributes to apply to this enumeration, and
18421 apply them if appropriate. */
18422 if (cp_parser_allow_gnu_extensions_p (parser
))
18424 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18425 cplus_decl_attributes (&type
,
18427 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18430 /* Finish up the enumeration. */
18431 if (type
!= error_mark_node
)
18433 if (new_value_list
)
18434 finish_enum_value_list (type
);
18436 finish_enum (type
);
18439 if (nested_name_specifier
)
18441 if (CLASS_TYPE_P (nested_name_specifier
))
18443 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18444 pop_scope (nested_name_specifier
);
18446 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18448 pop_nested_namespace (nested_name_specifier
);
18452 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18456 /* Parse an enumerator-list. The enumerators all have the indicated
18460 enumerator-definition
18461 enumerator-list , enumerator-definition */
18464 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18468 /* Parse an enumerator-definition. */
18469 cp_parser_enumerator_definition (parser
, type
);
18471 /* If the next token is not a ',', we've reached the end of
18473 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18475 /* Otherwise, consume the `,' and keep going. */
18476 cp_lexer_consume_token (parser
->lexer
);
18477 /* If the next token is a `}', there is a trailing comma. */
18478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18480 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18481 pedwarn (input_location
, OPT_Wpedantic
,
18482 "comma at end of enumerator list");
18488 /* Parse an enumerator-definition. The enumerator has the indicated
18491 enumerator-definition:
18493 enumerator = constant-expression
18500 enumerator-definition:
18501 enumerator attributes [opt]
18502 enumerator attributes [opt] = constant-expression */
18505 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18511 /* Save the input location because we are interested in the location
18512 of the identifier and not the location of the explicit value. */
18513 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18515 /* Look for the identifier. */
18516 identifier
= cp_parser_identifier (parser
);
18517 if (identifier
== error_mark_node
)
18520 /* Parse any specified attributes. */
18521 tree attrs
= cp_parser_attributes_opt (parser
);
18523 /* If the next token is an '=', then there is an explicit value. */
18524 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18526 /* Consume the `=' token. */
18527 cp_lexer_consume_token (parser
->lexer
);
18528 /* Parse the value. */
18529 value
= cp_parser_constant_expression (parser
);
18534 /* If we are processing a template, make sure the initializer of the
18535 enumerator doesn't contain any bare template parameter pack. */
18536 if (check_for_bare_parameter_packs (value
))
18537 value
= error_mark_node
;
18539 /* Create the enumerator. */
18540 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18543 /* Parse a namespace-name.
18546 original-namespace-name
18549 Returns the NAMESPACE_DECL for the namespace. */
18552 cp_parser_namespace_name (cp_parser
* parser
)
18555 tree namespace_decl
;
18557 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18559 /* Get the name of the namespace. */
18560 identifier
= cp_parser_identifier (parser
);
18561 if (identifier
== error_mark_node
)
18562 return error_mark_node
;
18564 /* Look up the identifier in the currently active scope. Look only
18565 for namespaces, due to:
18567 [basic.lookup.udir]
18569 When looking up a namespace-name in a using-directive or alias
18570 definition, only namespace names are considered.
18574 [basic.lookup.qual]
18576 During the lookup of a name preceding the :: scope resolution
18577 operator, object, function, and enumerator names are ignored.
18579 (Note that cp_parser_qualifying_entity only calls this
18580 function if the token after the name is the scope resolution
18582 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18584 /*is_template=*/false,
18585 /*is_namespace=*/true,
18586 /*check_dependency=*/true,
18587 /*ambiguous_decls=*/NULL
,
18589 /* If it's not a namespace, issue an error. */
18590 if (namespace_decl
== error_mark_node
18591 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18593 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18595 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18596 if (namespace_decl
== error_mark_node
18597 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18598 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18601 cp_parser_error (parser
, "expected namespace-name");
18602 namespace_decl
= error_mark_node
;
18605 return namespace_decl
;
18608 /* Parse a namespace-definition.
18610 namespace-definition:
18611 named-namespace-definition
18612 unnamed-namespace-definition
18614 named-namespace-definition:
18615 original-namespace-definition
18616 extension-namespace-definition
18618 original-namespace-definition:
18619 namespace identifier { namespace-body }
18621 extension-namespace-definition:
18622 namespace original-namespace-name { namespace-body }
18624 unnamed-namespace-definition:
18625 namespace { namespace-body } */
18628 cp_parser_namespace_definition (cp_parser
* parser
)
18631 int nested_definition_count
= 0;
18633 cp_ensure_no_omp_declare_simd (parser
);
18634 cp_ensure_no_oacc_routine (parser
);
18636 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18640 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18641 cp_lexer_consume_token (parser
->lexer
);
18644 /* Look for the `namespace' keyword. */
18646 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18648 /* Parse any specified attributes before the identifier. */
18649 tree attribs
= cp_parser_attributes_opt (parser
);
18653 identifier
= NULL_TREE
;
18655 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18657 identifier
= cp_parser_identifier (parser
);
18659 /* Parse any attributes specified after the identifier. */
18660 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18663 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18666 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18667 pedwarn (input_location
, OPT_Wpedantic
,
18668 "nested namespace definitions only available with "
18669 "-std=c++17 or -std=gnu++17");
18671 /* Nested namespace names can create new namespaces (unlike
18672 other qualified-ids). */
18673 if (int count
= identifier
? push_namespace (identifier
) : 0)
18674 nested_definition_count
+= count
;
18676 cp_parser_error (parser
, "nested namespace name required");
18677 cp_lexer_consume_token (parser
->lexer
);
18680 if (nested_definition_count
&& !identifier
)
18681 cp_parser_error (parser
, "namespace name required");
18683 if (nested_definition_count
&& attribs
)
18684 error_at (token
->location
,
18685 "a nested namespace definition cannot have attributes");
18686 if (nested_definition_count
&& is_inline
)
18687 error_at (token
->location
,
18688 "a nested namespace definition cannot be inline");
18690 /* Start the namespace. */
18691 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18693 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18695 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18697 /* Look for the `{' to validate starting the namespace. */
18698 matching_braces braces
;
18699 if (braces
.require_open (parser
))
18701 /* Parse the body of the namespace. */
18702 cp_parser_namespace_body (parser
);
18704 /* Look for the final `}'. */
18705 braces
.require_close (parser
);
18708 if (has_visibility
)
18709 pop_visibility (1);
18711 /* Pop the nested namespace definitions. */
18712 while (nested_definition_count
--)
18716 /* Parse a namespace-body.
18719 declaration-seq [opt] */
18722 cp_parser_namespace_body (cp_parser
* parser
)
18724 cp_parser_declaration_seq_opt (parser
);
18727 /* Parse a namespace-alias-definition.
18729 namespace-alias-definition:
18730 namespace identifier = qualified-namespace-specifier ; */
18733 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18736 tree namespace_specifier
;
18738 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18740 /* Look for the `namespace' keyword. */
18741 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18742 /* Look for the identifier. */
18743 identifier
= cp_parser_identifier (parser
);
18744 if (identifier
== error_mark_node
)
18746 /* Look for the `=' token. */
18747 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18748 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18750 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18751 /* Skip the definition. */
18752 cp_lexer_consume_token (parser
->lexer
);
18753 if (cp_parser_skip_to_closing_brace (parser
))
18754 cp_lexer_consume_token (parser
->lexer
);
18757 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18758 /* Look for the qualified-namespace-specifier. */
18759 namespace_specifier
18760 = cp_parser_qualified_namespace_specifier (parser
);
18761 /* Look for the `;' token. */
18762 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18764 /* Register the alias in the symbol table. */
18765 do_namespace_alias (identifier
, namespace_specifier
);
18768 /* Parse a qualified-namespace-specifier.
18770 qualified-namespace-specifier:
18771 :: [opt] nested-name-specifier [opt] namespace-name
18773 Returns a NAMESPACE_DECL corresponding to the specified
18777 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18779 /* Look for the optional `::'. */
18780 cp_parser_global_scope_opt (parser
,
18781 /*current_scope_valid_p=*/false);
18783 /* Look for the optional nested-name-specifier. */
18784 cp_parser_nested_name_specifier_opt (parser
,
18785 /*typename_keyword_p=*/false,
18786 /*check_dependency_p=*/true,
18788 /*is_declaration=*/true);
18790 return cp_parser_namespace_name (parser
);
18793 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18794 access declaration.
18797 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18798 using :: unqualified-id ;
18800 access-declaration:
18806 cp_parser_using_declaration (cp_parser
* parser
,
18807 bool access_declaration_p
)
18810 bool typename_p
= false;
18811 bool global_scope_p
;
18815 int oldcount
= errorcount
;
18816 cp_token
*diag_token
= NULL
;
18818 if (access_declaration_p
)
18820 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18821 cp_parser_parse_tentatively (parser
);
18825 /* Look for the `using' keyword. */
18826 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18829 /* Peek at the next token. */
18830 token
= cp_lexer_peek_token (parser
->lexer
);
18831 /* See if it's `typename'. */
18832 if (token
->keyword
== RID_TYPENAME
)
18834 /* Remember that we've seen it. */
18836 /* Consume the `typename' token. */
18837 cp_lexer_consume_token (parser
->lexer
);
18841 /* Look for the optional global scope qualification. */
18843 = (cp_parser_global_scope_opt (parser
,
18844 /*current_scope_valid_p=*/false)
18847 /* If we saw `typename', or didn't see `::', then there must be a
18848 nested-name-specifier present. */
18849 if (typename_p
|| !global_scope_p
)
18851 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18852 /*check_dependency_p=*/true,
18854 /*is_declaration=*/true);
18855 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18857 cp_parser_skip_to_end_of_block_or_statement (parser
);
18861 /* Otherwise, we could be in either of the two productions. In that
18862 case, treat the nested-name-specifier as optional. */
18864 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18865 /*typename_keyword_p=*/false,
18866 /*check_dependency_p=*/true,
18868 /*is_declaration=*/true);
18870 qscope
= global_namespace
;
18871 else if (UNSCOPED_ENUM_P (qscope
))
18872 qscope
= CP_TYPE_CONTEXT (qscope
);
18874 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18875 /* Something has already gone wrong; there's no need to parse
18876 further. Since an error has occurred, the return value of
18877 cp_parser_parse_definitely will be false, as required. */
18878 return cp_parser_parse_definitely (parser
);
18880 token
= cp_lexer_peek_token (parser
->lexer
);
18881 /* Parse the unqualified-id. */
18882 identifier
= cp_parser_unqualified_id (parser
,
18883 /*template_keyword_p=*/false,
18884 /*check_dependency_p=*/true,
18885 /*declarator_p=*/true,
18886 /*optional_p=*/false);
18888 if (access_declaration_p
)
18890 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18891 cp_parser_simulate_error (parser
);
18892 if (!cp_parser_parse_definitely (parser
))
18895 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18897 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18898 if (cxx_dialect
< cxx17
18899 && !in_system_header_at (ell
->location
))
18900 pedwarn (ell
->location
, 0,
18901 "pack expansion in using-declaration only available "
18902 "with -std=c++17 or -std=gnu++17");
18903 qscope
= make_pack_expansion (qscope
);
18906 /* The function we call to handle a using-declaration is different
18907 depending on what scope we are in. */
18908 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18910 else if (!identifier_p (identifier
)
18911 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18912 /* [namespace.udecl]
18914 A using declaration shall not name a template-id. */
18915 error_at (token
->location
,
18916 "a template-id may not appear in a using-declaration");
18919 if (at_class_scope_p ())
18921 /* Create the USING_DECL. */
18922 decl
= do_class_using_decl (qscope
, identifier
);
18924 if (decl
&& typename_p
)
18925 USING_DECL_TYPENAME_P (decl
) = 1;
18927 if (check_for_bare_parameter_packs (decl
))
18929 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18933 /* Add it to the list of members in this class. */
18934 finish_member_declaration (decl
);
18938 decl
= cp_parser_lookup_name_simple (parser
,
18941 if (decl
== error_mark_node
)
18942 cp_parser_name_lookup_error (parser
, identifier
,
18945 else if (check_for_bare_parameter_packs (decl
))
18947 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18950 else if (!at_namespace_scope_p ())
18951 finish_local_using_decl (decl
, qscope
, identifier
);
18953 finish_namespace_using_decl (decl
, qscope
, identifier
);
18957 if (!access_declaration_p
18958 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18960 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18961 if (cxx_dialect
< cxx17
)
18962 pedwarn (comma
->location
, 0,
18963 "comma-separated list in using-declaration only available "
18964 "with -std=c++17 or -std=gnu++17");
18968 /* Look for the final `;'. */
18969 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18971 if (access_declaration_p
&& errorcount
== oldcount
)
18972 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18973 "access declarations are deprecated "
18974 "in favour of using-declarations; "
18975 "suggestion: add the %<using%> keyword");
18980 /* Parse an alias-declaration.
18983 using identifier attribute-specifier-seq [opt] = type-id */
18986 cp_parser_alias_declaration (cp_parser
* parser
)
18988 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18989 location_t id_location
;
18990 cp_declarator
*declarator
;
18991 cp_decl_specifier_seq decl_specs
;
18993 const char *saved_message
= NULL
;
18995 /* Look for the `using' keyword. */
18996 cp_token
*using_token
18997 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18998 if (using_token
== NULL
)
18999 return error_mark_node
;
19001 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19002 id
= cp_parser_identifier (parser
);
19003 if (id
== error_mark_node
)
19004 return error_mark_node
;
19006 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19007 attributes
= cp_parser_attributes_opt (parser
);
19008 if (attributes
== error_mark_node
)
19009 return error_mark_node
;
19011 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19013 if (cp_parser_error_occurred (parser
))
19014 return error_mark_node
;
19016 cp_parser_commit_to_tentative_parse (parser
);
19018 /* Now we are going to parse the type-id of the declaration. */
19023 "A type-specifier-seq shall not define a class or enumeration
19024 unless it appears in the type-id of an alias-declaration (7.1.3) that
19025 is not the declaration of a template-declaration."
19027 In other words, if we currently are in an alias template, the
19028 type-id should not define a type.
19030 So let's set parser->type_definition_forbidden_message in that
19031 case; cp_parser_check_type_definition (called by
19032 cp_parser_class_specifier) will then emit an error if a type is
19033 defined in the type-id. */
19034 if (parser
->num_template_parameter_lists
)
19036 saved_message
= parser
->type_definition_forbidden_message
;
19037 parser
->type_definition_forbidden_message
=
19038 G_("types may not be defined in alias template declarations");
19041 type
= cp_parser_type_id (parser
);
19043 /* Restore the error message if need be. */
19044 if (parser
->num_template_parameter_lists
)
19045 parser
->type_definition_forbidden_message
= saved_message
;
19047 if (type
== error_mark_node
19048 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19050 cp_parser_skip_to_end_of_block_or_statement (parser
);
19051 return error_mark_node
;
19054 /* A typedef-name can also be introduced by an alias-declaration. The
19055 identifier following the using keyword becomes a typedef-name. It has
19056 the same semantics as if it were introduced by the typedef
19057 specifier. In particular, it does not define a new type and it shall
19058 not appear in the type-id. */
19060 clear_decl_specs (&decl_specs
);
19061 decl_specs
.type
= type
;
19062 if (attributes
!= NULL_TREE
)
19064 decl_specs
.attributes
= attributes
;
19065 set_and_check_decl_spec_loc (&decl_specs
,
19069 set_and_check_decl_spec_loc (&decl_specs
,
19072 set_and_check_decl_spec_loc (&decl_specs
,
19076 if (parser
->num_template_parameter_lists
19077 && !cp_parser_check_template_parameters (parser
,
19078 /*num_templates=*/0,
19079 /*template_id*/false,
19081 /*declarator=*/NULL
))
19082 return error_mark_node
;
19084 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
19085 declarator
->id_loc
= id_location
;
19087 member_p
= at_class_scope_p ();
19089 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19090 NULL_TREE
, attributes
);
19092 decl
= start_decl (declarator
, &decl_specs
, 0,
19093 attributes
, NULL_TREE
, &pushed_scope
);
19094 if (decl
== error_mark_node
)
19097 // Attach constraints to the alias declaration.
19098 if (flag_concepts
&& current_template_parms
)
19100 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19101 tree constr
= build_constraints (reqs
, NULL_TREE
);
19102 set_constraints (decl
, constr
);
19105 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19108 pop_scope (pushed_scope
);
19110 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19111 added into the symbol table; otherwise, return the TYPE_DECL. */
19112 if (DECL_LANG_SPECIFIC (decl
)
19113 && DECL_TEMPLATE_INFO (decl
)
19114 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19116 decl
= DECL_TI_TEMPLATE (decl
);
19118 check_member_template (decl
);
19124 /* Parse a using-directive.
19127 using namespace :: [opt] nested-name-specifier [opt]
19128 namespace-name ; */
19131 cp_parser_using_directive (cp_parser
* parser
)
19133 tree namespace_decl
;
19136 /* Look for the `using' keyword. */
19137 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19138 /* And the `namespace' keyword. */
19139 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19140 /* Look for the optional `::' operator. */
19141 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19142 /* And the optional nested-name-specifier. */
19143 cp_parser_nested_name_specifier_opt (parser
,
19144 /*typename_keyword_p=*/false,
19145 /*check_dependency_p=*/true,
19147 /*is_declaration=*/true);
19148 /* Get the namespace being used. */
19149 namespace_decl
= cp_parser_namespace_name (parser
);
19150 /* And any specified attributes. */
19151 attribs
= cp_parser_attributes_opt (parser
);
19153 /* Update the symbol table. */
19154 if (namespace_bindings_p ())
19155 finish_namespace_using_directive (namespace_decl
, attribs
);
19157 finish_local_using_directive (namespace_decl
, attribs
);
19159 /* Look for the final `;'. */
19160 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19163 /* Parse an asm-definition.
19166 asm ( string-literal ) ;
19171 asm volatile [opt] ( string-literal ) ;
19172 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19173 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19174 : asm-operand-list [opt] ) ;
19175 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19176 : asm-operand-list [opt]
19177 : asm-clobber-list [opt] ) ;
19178 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19179 : asm-clobber-list [opt]
19180 : asm-goto-list ) ; */
19183 cp_parser_asm_definition (cp_parser
* parser
)
19186 tree outputs
= NULL_TREE
;
19187 tree inputs
= NULL_TREE
;
19188 tree clobbers
= NULL_TREE
;
19189 tree labels
= NULL_TREE
;
19191 bool volatile_p
= false;
19192 bool extended_p
= false;
19193 bool invalid_inputs_p
= false;
19194 bool invalid_outputs_p
= false;
19195 bool goto_p
= false;
19196 required_token missing
= RT_NONE
;
19198 /* Look for the `asm' keyword. */
19199 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19201 if (parser
->in_function_body
19202 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19204 error ("%<asm%> in %<constexpr%> function");
19205 cp_function_chain
->invalid_constexpr
= true;
19208 /* See if the next token is `volatile'. */
19209 if (cp_parser_allow_gnu_extensions_p (parser
)
19210 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19212 /* Remember that we saw the `volatile' keyword. */
19214 /* Consume the token. */
19215 cp_lexer_consume_token (parser
->lexer
);
19217 if (cp_parser_allow_gnu_extensions_p (parser
)
19218 && parser
->in_function_body
19219 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19221 /* Remember that we saw the `goto' keyword. */
19223 /* Consume the token. */
19224 cp_lexer_consume_token (parser
->lexer
);
19226 /* Look for the opening `('. */
19227 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19229 /* Look for the string. */
19230 string
= cp_parser_string_literal (parser
, false, false);
19231 if (string
== error_mark_node
)
19233 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19234 /*consume_paren=*/true);
19238 /* If we're allowing GNU extensions, check for the extended assembly
19239 syntax. Unfortunately, the `:' tokens need not be separated by
19240 a space in C, and so, for compatibility, we tolerate that here
19241 too. Doing that means that we have to treat the `::' operator as
19243 if (cp_parser_allow_gnu_extensions_p (parser
)
19244 && parser
->in_function_body
19245 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19246 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19248 bool inputs_p
= false;
19249 bool clobbers_p
= false;
19250 bool labels_p
= false;
19252 /* The extended syntax was used. */
19255 /* Look for outputs. */
19256 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19258 /* Consume the `:'. */
19259 cp_lexer_consume_token (parser
->lexer
);
19260 /* Parse the output-operands. */
19261 if (cp_lexer_next_token_is_not (parser
->lexer
,
19263 && cp_lexer_next_token_is_not (parser
->lexer
,
19265 && cp_lexer_next_token_is_not (parser
->lexer
,
19269 outputs
= cp_parser_asm_operand_list (parser
);
19270 if (outputs
== error_mark_node
)
19271 invalid_outputs_p
= true;
19274 /* If the next token is `::', there are no outputs, and the
19275 next token is the beginning of the inputs. */
19276 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19277 /* The inputs are coming next. */
19280 /* Look for inputs. */
19282 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19284 /* Consume the `:' or `::'. */
19285 cp_lexer_consume_token (parser
->lexer
);
19286 /* Parse the output-operands. */
19287 if (cp_lexer_next_token_is_not (parser
->lexer
,
19289 && cp_lexer_next_token_is_not (parser
->lexer
,
19291 && cp_lexer_next_token_is_not (parser
->lexer
,
19294 inputs
= cp_parser_asm_operand_list (parser
);
19295 if (inputs
== error_mark_node
)
19296 invalid_inputs_p
= true;
19299 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19300 /* The clobbers are coming next. */
19303 /* Look for clobbers. */
19305 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19308 /* Consume the `:' or `::'. */
19309 cp_lexer_consume_token (parser
->lexer
);
19310 /* Parse the clobbers. */
19311 if (cp_lexer_next_token_is_not (parser
->lexer
,
19313 && cp_lexer_next_token_is_not (parser
->lexer
,
19315 clobbers
= cp_parser_asm_clobber_list (parser
);
19318 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19319 /* The labels are coming next. */
19322 /* Look for labels. */
19324 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19327 /* Consume the `:' or `::'. */
19328 cp_lexer_consume_token (parser
->lexer
);
19329 /* Parse the labels. */
19330 labels
= cp_parser_asm_label_list (parser
);
19333 if (goto_p
&& !labels_p
)
19334 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19337 missing
= RT_COLON_SCOPE
;
19339 /* Look for the closing `)'. */
19340 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19341 missing
? missing
: RT_CLOSE_PAREN
))
19342 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19343 /*consume_paren=*/true);
19344 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19346 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19348 /* Create the ASM_EXPR. */
19349 if (parser
->in_function_body
)
19351 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19352 inputs
, clobbers
, labels
);
19353 /* If the extended syntax was not used, mark the ASM_EXPR. */
19356 tree temp
= asm_stmt
;
19357 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19358 temp
= TREE_OPERAND (temp
, 0);
19360 ASM_INPUT_P (temp
) = 1;
19364 symtab
->finalize_toplevel_asm (string
);
19368 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19369 type that comes from the decl-specifier-seq. */
19372 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19374 for (cp_declarator
*d
= declarator
; d
;)
19384 if (TYPE_PTRMEMFUNC_P (type
))
19385 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19386 type
= TREE_TYPE (type
);
19394 /* Declarators [gram.dcl.decl] */
19396 /* Parse an init-declarator.
19399 declarator initializer [opt]
19404 declarator asm-specification [opt] attributes [opt] initializer [opt]
19406 function-definition:
19407 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19409 decl-specifier-seq [opt] declarator function-try-block
19413 function-definition:
19414 __extension__ function-definition
19418 function-definition:
19419 decl-specifier-seq [opt] declarator function-transaction-block
19421 The DECL_SPECIFIERS apply to this declarator. Returns a
19422 representation of the entity declared. If MEMBER_P is TRUE, then
19423 this declarator appears in a class scope. The new DECL created by
19424 this declarator is returned.
19426 The CHECKS are access checks that should be performed once we know
19427 what entity is being declared (and, therefore, what classes have
19430 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19431 for a function-definition here as well. If the declarator is a
19432 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19433 be TRUE upon return. By that point, the function-definition will
19434 have been completely parsed.
19436 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19439 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19440 parsed declaration if it is an uninitialized single declarator not followed
19441 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19442 if present, will not be consumed. If returned, this declarator will be
19443 created with SD_INITIALIZED but will not call cp_finish_decl.
19445 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19446 and there is an initializer, the pointed location_t is set to the
19447 location of the '=' or `(', or '{' in C++11 token introducing the
19451 cp_parser_init_declarator (cp_parser
* parser
,
19452 cp_decl_specifier_seq
*decl_specifiers
,
19453 vec
<deferred_access_check
, va_gc
> *checks
,
19454 bool function_definition_allowed_p
,
19456 int declares_class_or_enum
,
19457 bool* function_definition_p
,
19458 tree
* maybe_range_for_decl
,
19459 location_t
* init_loc
,
19462 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19463 *attributes_start_token
= NULL
;
19464 cp_declarator
*declarator
;
19465 tree prefix_attributes
;
19466 tree attributes
= NULL
;
19467 tree asm_specification
;
19469 tree decl
= NULL_TREE
;
19471 int is_initialized
;
19472 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19473 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19475 enum cpp_ttype initialization_kind
;
19476 bool is_direct_init
= false;
19477 bool is_non_constant_init
;
19478 int ctor_dtor_or_conv_p
;
19479 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19480 tree pushed_scope
= NULL_TREE
;
19481 bool range_for_decl_p
= false;
19482 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19483 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19485 /* Gather the attributes that were provided with the
19486 decl-specifiers. */
19487 prefix_attributes
= decl_specifiers
->attributes
;
19489 /* Assume that this is not the declarator for a function
19491 if (function_definition_p
)
19492 *function_definition_p
= false;
19494 /* Default arguments are only permitted for function parameters. */
19495 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19496 parser
->default_arg_ok_p
= false;
19498 /* Defer access checks while parsing the declarator; we cannot know
19499 what names are accessible until we know what is being
19501 resume_deferring_access_checks ();
19503 token
= cp_lexer_peek_token (parser
->lexer
);
19505 /* Parse the declarator. */
19507 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19508 &ctor_dtor_or_conv_p
,
19509 /*parenthesized_p=*/NULL
,
19510 member_p
, friend_p
);
19511 /* Gather up the deferred checks. */
19512 stop_deferring_access_checks ();
19514 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19516 /* If the DECLARATOR was erroneous, there's no need to go
19518 if (declarator
== cp_error_declarator
)
19519 return error_mark_node
;
19521 /* Check that the number of template-parameter-lists is OK. */
19522 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19524 return error_mark_node
;
19526 if (declares_class_or_enum
& 2)
19527 cp_parser_check_for_definition_in_return_type (declarator
,
19528 decl_specifiers
->type
,
19529 decl_specifiers
->locations
[ds_type_spec
]);
19531 /* Figure out what scope the entity declared by the DECLARATOR is
19532 located in. `grokdeclarator' sometimes changes the scope, so
19533 we compute it now. */
19534 scope
= get_scope_of_declarator (declarator
);
19536 /* Perform any lookups in the declared type which were thought to be
19537 dependent, but are not in the scope of the declarator. */
19538 decl_specifiers
->type
19539 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19541 /* If we're allowing GNU extensions, look for an
19542 asm-specification. */
19543 if (cp_parser_allow_gnu_extensions_p (parser
))
19545 /* Look for an asm-specification. */
19546 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19547 asm_specification
= cp_parser_asm_specification_opt (parser
);
19550 asm_specification
= NULL_TREE
;
19552 /* Look for attributes. */
19553 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19554 attributes
= cp_parser_attributes_opt (parser
);
19556 /* Peek at the next token. */
19557 token
= cp_lexer_peek_token (parser
->lexer
);
19559 bool bogus_implicit_tmpl
= false;
19561 if (function_declarator_p (declarator
))
19563 /* Handle C++17 deduction guides. */
19564 if (!decl_specifiers
->type
19565 && ctor_dtor_or_conv_p
<= 0
19566 && cxx_dialect
>= cxx17
)
19568 cp_declarator
*id
= get_id_declarator (declarator
);
19569 tree name
= id
->u
.id
.unqualified_name
;
19570 parser
->scope
= id
->u
.id
.qualifying_scope
;
19571 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19573 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19574 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19576 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19577 id
->u
.id
.sfk
= sfk_deduction_guide
;
19578 ctor_dtor_or_conv_p
= 1;
19582 /* Check to see if the token indicates the start of a
19583 function-definition. */
19584 if (cp_parser_token_starts_function_definition_p (token
))
19586 if (!function_definition_allowed_p
)
19588 /* If a function-definition should not appear here, issue an
19590 cp_parser_error (parser
,
19591 "a function-definition is not allowed here");
19592 return error_mark_node
;
19595 location_t func_brace_location
19596 = cp_lexer_peek_token (parser
->lexer
)->location
;
19598 /* Neither attributes nor an asm-specification are allowed
19599 on a function-definition. */
19600 if (asm_specification
)
19601 error_at (asm_spec_start_token
->location
,
19602 "an asm-specification is not allowed "
19603 "on a function-definition");
19605 error_at (attributes_start_token
->location
,
19606 "attributes are not allowed "
19607 "on a function-definition");
19608 /* This is a function-definition. */
19609 *function_definition_p
= true;
19611 /* Parse the function definition. */
19613 decl
= cp_parser_save_member_function_body (parser
,
19616 prefix_attributes
);
19619 (cp_parser_function_definition_from_specifiers_and_declarator
19620 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19622 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19624 /* This is where the prologue starts... */
19625 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19626 = func_brace_location
;
19632 else if (parser
->fully_implicit_function_template_p
)
19634 /* A non-template declaration involving a function parameter list
19635 containing an implicit template parameter will be made into a
19636 template. If the resulting declaration is not going to be an
19637 actual function then finish the template scope here to prevent it.
19638 An error message will be issued once we have a decl to talk about.
19640 FIXME probably we should do type deduction rather than create an
19641 implicit template, but the standard currently doesn't allow it. */
19642 bogus_implicit_tmpl
= true;
19643 finish_fully_implicit_template (parser
, NULL_TREE
);
19648 Only in function declarations for constructors, destructors, type
19649 conversions, and deduction guides can the decl-specifier-seq be omitted.
19651 We explicitly postpone this check past the point where we handle
19652 function-definitions because we tolerate function-definitions
19653 that are missing their return types in some modes. */
19654 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19656 cp_parser_error (parser
,
19657 "expected constructor, destructor, or type conversion");
19658 return error_mark_node
;
19661 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19662 if (token
->type
== CPP_EQ
19663 || token
->type
== CPP_OPEN_PAREN
19664 || token
->type
== CPP_OPEN_BRACE
)
19666 is_initialized
= SD_INITIALIZED
;
19667 initialization_kind
= token
->type
;
19668 if (maybe_range_for_decl
)
19669 *maybe_range_for_decl
= error_mark_node
;
19670 tmp_init_loc
= token
->location
;
19671 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19672 *init_loc
= tmp_init_loc
;
19674 if (token
->type
== CPP_EQ
19675 && function_declarator_p (declarator
))
19677 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19678 if (t2
->keyword
== RID_DEFAULT
)
19679 is_initialized
= SD_DEFAULTED
;
19680 else if (t2
->keyword
== RID_DELETE
)
19681 is_initialized
= SD_DELETED
;
19686 /* If the init-declarator isn't initialized and isn't followed by a
19687 `,' or `;', it's not a valid init-declarator. */
19688 if (token
->type
!= CPP_COMMA
19689 && token
->type
!= CPP_SEMICOLON
)
19691 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19692 range_for_decl_p
= true;
19695 if (!maybe_range_for_decl
)
19696 cp_parser_error (parser
, "expected initializer");
19697 return error_mark_node
;
19700 is_initialized
= SD_UNINITIALIZED
;
19701 initialization_kind
= CPP_EOF
;
19704 /* Because start_decl has side-effects, we should only call it if we
19705 know we're going ahead. By this point, we know that we cannot
19706 possibly be looking at any other construct. */
19707 cp_parser_commit_to_tentative_parse (parser
);
19709 /* Enter the newly declared entry in the symbol table. If we're
19710 processing a declaration in a class-specifier, we wait until
19711 after processing the initializer. */
19714 if (parser
->in_unbraced_linkage_specification_p
)
19715 decl_specifiers
->storage_class
= sc_extern
;
19716 decl
= start_decl (declarator
, decl_specifiers
,
19717 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19718 attributes
, prefix_attributes
, &pushed_scope
);
19719 cp_finalize_omp_declare_simd (parser
, decl
);
19720 cp_finalize_oacc_routine (parser
, decl
, false);
19721 /* Adjust location of decl if declarator->id_loc is more appropriate:
19722 set, and decl wasn't merged with another decl, in which case its
19723 location would be different from input_location, and more accurate. */
19725 && declarator
->id_loc
!= UNKNOWN_LOCATION
19726 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19727 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19730 /* Enter the SCOPE. That way unqualified names appearing in the
19731 initializer will be looked up in SCOPE. */
19732 pushed_scope
= push_scope (scope
);
19734 /* Perform deferred access control checks, now that we know in which
19735 SCOPE the declared entity resides. */
19736 if (!member_p
&& decl
)
19738 tree saved_current_function_decl
= NULL_TREE
;
19740 /* If the entity being declared is a function, pretend that we
19741 are in its scope. If it is a `friend', it may have access to
19742 things that would not otherwise be accessible. */
19743 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19745 saved_current_function_decl
= current_function_decl
;
19746 current_function_decl
= decl
;
19749 /* Perform access checks for template parameters. */
19750 cp_parser_perform_template_parameter_access_checks (checks
);
19752 /* Perform the access control checks for the declarator and the
19753 decl-specifiers. */
19754 perform_deferred_access_checks (tf_warning_or_error
);
19756 /* Restore the saved value. */
19757 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19758 current_function_decl
= saved_current_function_decl
;
19761 /* Parse the initializer. */
19762 initializer
= NULL_TREE
;
19763 is_direct_init
= false;
19764 is_non_constant_init
= true;
19765 if (is_initialized
)
19767 if (function_declarator_p (declarator
))
19769 if (initialization_kind
== CPP_EQ
)
19770 initializer
= cp_parser_pure_specifier (parser
);
19773 /* If the declaration was erroneous, we don't really
19774 know what the user intended, so just silently
19775 consume the initializer. */
19776 if (decl
!= error_mark_node
)
19777 error_at (tmp_init_loc
, "initializer provided for function");
19778 cp_parser_skip_to_closing_parenthesis (parser
,
19779 /*recovering=*/true,
19780 /*or_comma=*/false,
19781 /*consume_paren=*/true);
19786 /* We want to record the extra mangling scope for in-class
19787 initializers of class members and initializers of static data
19788 member templates. The former involves deferring
19789 parsing of the initializer until end of class as with default
19790 arguments. So right here we only handle the latter. */
19791 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19792 start_lambda_scope (decl
);
19793 initializer
= cp_parser_initializer (parser
,
19795 &is_non_constant_init
);
19796 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19797 finish_lambda_scope ();
19798 if (initializer
== error_mark_node
)
19799 cp_parser_skip_to_end_of_statement (parser
);
19803 /* The old parser allows attributes to appear after a parenthesized
19804 initializer. Mark Mitchell proposed removing this functionality
19805 on the GCC mailing lists on 2002-08-13. This parser accepts the
19806 attributes -- but ignores them. Made a permerror in GCC 8. */
19807 if (cp_parser_allow_gnu_extensions_p (parser
)
19808 && initialization_kind
== CPP_OPEN_PAREN
19809 && cp_parser_attributes_opt (parser
)
19810 && permerror (input_location
,
19811 "attributes after parenthesized initializer ignored"))
19814 if (flag_permissive
&& !hint
)
19817 inform (input_location
,
19818 "this flexibility is deprecated and will be removed");
19822 /* And now complain about a non-function implicit template. */
19823 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19824 error_at (DECL_SOURCE_LOCATION (decl
),
19825 "non-function %qD declared as implicit template", decl
);
19827 /* For an in-class declaration, use `grokfield' to create the
19833 pop_scope (pushed_scope
);
19834 pushed_scope
= NULL_TREE
;
19836 decl
= grokfield (declarator
, decl_specifiers
,
19837 initializer
, !is_non_constant_init
,
19838 /*asmspec=*/NULL_TREE
,
19839 attr_chainon (attributes
, prefix_attributes
));
19840 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19841 cp_parser_save_default_args (parser
, decl
);
19842 cp_finalize_omp_declare_simd (parser
, decl
);
19843 cp_finalize_oacc_routine (parser
, decl
, false);
19846 /* Finish processing the declaration. But, skip member
19848 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19850 cp_finish_decl (decl
,
19851 initializer
, !is_non_constant_init
,
19853 /* If the initializer is in parentheses, then this is
19854 a direct-initialization, which means that an
19855 `explicit' constructor is OK. Otherwise, an
19856 `explicit' constructor cannot be used. */
19857 ((is_direct_init
|| !is_initialized
)
19858 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19860 else if ((cxx_dialect
!= cxx98
) && friend_p
19861 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19862 /* Core issue #226 (C++0x only): A default template-argument
19863 shall not be specified in a friend class template
19865 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19866 /*is_partial=*/false, /*is_friend_decl=*/1);
19868 if (!friend_p
&& pushed_scope
)
19869 pop_scope (pushed_scope
);
19871 if (function_declarator_p (declarator
)
19872 && parser
->fully_implicit_function_template_p
)
19875 decl
= finish_fully_implicit_template (parser
, decl
);
19877 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19880 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19881 && type_uses_auto (decl_specifiers
->type
))
19882 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19887 /* Parse a declarator.
19891 ptr-operator declarator
19893 abstract-declarator:
19894 ptr-operator abstract-declarator [opt]
19895 direct-abstract-declarator
19900 attributes [opt] direct-declarator
19901 attributes [opt] ptr-operator declarator
19903 abstract-declarator:
19904 attributes [opt] ptr-operator abstract-declarator [opt]
19905 attributes [opt] direct-abstract-declarator
19907 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19908 detect constructors, destructors, deduction guides, or conversion operators.
19909 It is set to -1 if the declarator is a name, and +1 if it is a
19910 function. Otherwise it is set to zero. Usually you just want to
19911 test for >0, but internally the negative value is used.
19913 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19914 a decl-specifier-seq unless it declares a constructor, destructor,
19915 or conversion. It might seem that we could check this condition in
19916 semantic analysis, rather than parsing, but that makes it difficult
19917 to handle something like `f()'. We want to notice that there are
19918 no decl-specifiers, and therefore realize that this is an
19919 expression, not a declaration.)
19921 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19922 the declarator is a direct-declarator of the form "(...)".
19924 MEMBER_P is true iff this declarator is a member-declarator.
19926 FRIEND_P is true iff this declarator is a friend. */
19928 static cp_declarator
*
19929 cp_parser_declarator (cp_parser
* parser
,
19930 cp_parser_declarator_kind dcl_kind
,
19931 int* ctor_dtor_or_conv_p
,
19932 bool* parenthesized_p
,
19933 bool member_p
, bool friend_p
)
19935 cp_declarator
*declarator
;
19936 enum tree_code code
;
19937 cp_cv_quals cv_quals
;
19939 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19941 /* Assume this is not a constructor, destructor, or type-conversion
19943 if (ctor_dtor_or_conv_p
)
19944 *ctor_dtor_or_conv_p
= 0;
19946 if (cp_parser_allow_gnu_extensions_p (parser
))
19947 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19949 /* Check for the ptr-operator production. */
19950 cp_parser_parse_tentatively (parser
);
19951 /* Parse the ptr-operator. */
19952 code
= cp_parser_ptr_operator (parser
,
19957 /* If that worked, then we have a ptr-operator. */
19958 if (cp_parser_parse_definitely (parser
))
19960 /* If a ptr-operator was found, then this declarator was not
19962 if (parenthesized_p
)
19963 *parenthesized_p
= true;
19964 /* The dependent declarator is optional if we are parsing an
19965 abstract-declarator. */
19966 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19967 cp_parser_parse_tentatively (parser
);
19969 /* Parse the dependent declarator. */
19970 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19971 /*ctor_dtor_or_conv_p=*/NULL
,
19972 /*parenthesized_p=*/NULL
,
19973 /*member_p=*/false,
19976 /* If we are parsing an abstract-declarator, we must handle the
19977 case where the dependent declarator is absent. */
19978 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19979 && !cp_parser_parse_definitely (parser
))
19982 declarator
= cp_parser_make_indirect_declarator
19983 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19985 /* Everything else is a direct-declarator. */
19988 if (parenthesized_p
)
19989 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19991 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19992 ctor_dtor_or_conv_p
,
19993 member_p
, friend_p
);
19996 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19997 declarator
->attributes
= gnu_attributes
;
20001 /* Parse a direct-declarator or direct-abstract-declarator.
20005 direct-declarator ( parameter-declaration-clause )
20006 cv-qualifier-seq [opt]
20007 ref-qualifier [opt]
20008 exception-specification [opt]
20009 direct-declarator [ constant-expression [opt] ]
20012 direct-abstract-declarator:
20013 direct-abstract-declarator [opt]
20014 ( parameter-declaration-clause )
20015 cv-qualifier-seq [opt]
20016 ref-qualifier [opt]
20017 exception-specification [opt]
20018 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20019 ( abstract-declarator )
20021 Returns a representation of the declarator. DCL_KIND is
20022 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20023 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20024 we are parsing a direct-declarator. It is
20025 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20026 of ambiguity we prefer an abstract declarator, as per
20027 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20028 as for cp_parser_declarator. */
20030 static cp_declarator
*
20031 cp_parser_direct_declarator (cp_parser
* parser
,
20032 cp_parser_declarator_kind dcl_kind
,
20033 int* ctor_dtor_or_conv_p
,
20034 bool member_p
, bool friend_p
)
20037 cp_declarator
*declarator
= NULL
;
20038 tree scope
= NULL_TREE
;
20039 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20040 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20042 tree pushed_scope
= NULL_TREE
;
20043 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20047 /* Peek at the next token. */
20048 token
= cp_lexer_peek_token (parser
->lexer
);
20049 if (token
->type
== CPP_OPEN_PAREN
)
20051 /* This is either a parameter-declaration-clause, or a
20052 parenthesized declarator. When we know we are parsing a
20053 named declarator, it must be a parenthesized declarator
20054 if FIRST is true. For instance, `(int)' is a
20055 parameter-declaration-clause, with an omitted
20056 direct-abstract-declarator. But `((*))', is a
20057 parenthesized abstract declarator. Finally, when T is a
20058 template parameter `(T)' is a
20059 parameter-declaration-clause, and not a parenthesized
20062 We first try and parse a parameter-declaration-clause,
20063 and then try a nested declarator (if FIRST is true).
20065 It is not an error for it not to be a
20066 parameter-declaration-clause, even when FIRST is
20072 The first is the declaration of a function while the
20073 second is the definition of a variable, including its
20076 Having seen only the parenthesis, we cannot know which of
20077 these two alternatives should be selected. Even more
20078 complex are examples like:
20083 The former is a function-declaration; the latter is a
20084 variable initialization.
20086 Thus again, we try a parameter-declaration-clause, and if
20087 that fails, we back out and return. */
20089 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20092 bool is_declarator
= false;
20096 /* In a member-declarator, the only valid interpretation
20097 of a parenthesis is the start of a
20098 parameter-declaration-clause. (It is invalid to
20099 initialize a static data member with a parenthesized
20100 initializer; only the "=" form of initialization is
20103 cp_parser_parse_tentatively (parser
);
20105 /* Consume the `('. */
20106 matching_parens parens
;
20107 parens
.consume_open (parser
);
20110 /* If this is going to be an abstract declarator, we're
20111 in a declarator and we can't have default args. */
20112 parser
->default_arg_ok_p
= false;
20113 parser
->in_declarator_p
= true;
20116 begin_scope (sk_function_parms
, NULL_TREE
);
20118 /* Parse the parameter-declaration-clause. */
20119 params
= cp_parser_parameter_declaration_clause (parser
);
20121 /* Consume the `)'. */
20122 parens
.require_close (parser
);
20124 /* If all went well, parse the cv-qualifier-seq,
20125 ref-qualifier and the exception-specification. */
20126 if (member_p
|| cp_parser_parse_definitely (parser
))
20128 cp_cv_quals cv_quals
;
20129 cp_virt_specifiers virt_specifiers
;
20130 cp_ref_qualifier ref_qual
;
20131 tree exception_specification
;
20134 bool memfn
= (member_p
|| (pushed_scope
20135 && CLASS_TYPE_P (pushed_scope
)));
20137 is_declarator
= true;
20139 if (ctor_dtor_or_conv_p
)
20140 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20143 /* Parse the cv-qualifier-seq. */
20144 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20145 /* Parse the ref-qualifier. */
20146 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20147 /* Parse the tx-qualifier. */
20148 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20149 /* And the exception-specification. */
20150 exception_specification
20151 = cp_parser_exception_specification_opt (parser
);
20153 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20155 /* In here, we handle cases where attribute is used after
20156 the function declaration. For example:
20157 void func (int x) __attribute__((vector(..))); */
20158 tree gnu_attrs
= NULL_TREE
;
20159 tree requires_clause
= NULL_TREE
;
20160 late_return
= (cp_parser_late_return_type_opt
20161 (parser
, declarator
, requires_clause
,
20162 memfn
? cv_quals
: -1));
20164 /* Parse the virt-specifier-seq. */
20165 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20167 /* Create the function-declarator. */
20168 declarator
= make_call_declarator (declarator
,
20174 exception_specification
,
20177 declarator
->std_attributes
= attrs
;
20178 declarator
->attributes
= gnu_attrs
;
20179 /* Any subsequent parameter lists are to do with
20180 return type, so are not those of the declared
20182 parser
->default_arg_ok_p
= false;
20185 /* Remove the function parms from scope. */
20186 pop_bindings_and_leave_scope ();
20189 /* Repeat the main loop. */
20193 /* If this is the first, we can try a parenthesized
20197 bool saved_in_type_id_in_expr_p
;
20199 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20200 parser
->in_declarator_p
= saved_in_declarator_p
;
20202 open_paren
= token
;
20203 /* Consume the `('. */
20204 matching_parens parens
;
20205 parens
.consume_open (parser
);
20206 /* Parse the nested declarator. */
20207 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20208 parser
->in_type_id_in_expr_p
= true;
20210 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20211 /*parenthesized_p=*/NULL
,
20212 member_p
, friend_p
);
20213 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20215 /* Expect a `)'. */
20216 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20217 if (!parens
.require_close (parser
))
20218 declarator
= cp_error_declarator
;
20219 if (declarator
== cp_error_declarator
)
20222 goto handle_declarator
;
20224 /* Otherwise, we must be done. */
20228 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20229 && token
->type
== CPP_OPEN_SQUARE
20230 && !cp_next_tokens_can_be_attribute_p (parser
))
20232 /* Parse an array-declarator. */
20233 tree bounds
, attrs
;
20235 if (ctor_dtor_or_conv_p
)
20236 *ctor_dtor_or_conv_p
= 0;
20240 parser
->default_arg_ok_p
= false;
20241 parser
->in_declarator_p
= true;
20242 /* Consume the `['. */
20243 cp_lexer_consume_token (parser
->lexer
);
20244 /* Peek at the next token. */
20245 token
= cp_lexer_peek_token (parser
->lexer
);
20246 /* If the next token is `]', then there is no
20247 constant-expression. */
20248 if (token
->type
!= CPP_CLOSE_SQUARE
)
20250 bool non_constant_p
;
20252 = cp_parser_constant_expression (parser
,
20253 /*allow_non_constant=*/true,
20255 if (!non_constant_p
)
20257 else if (error_operand_p (bounds
))
20258 /* Already gave an error. */;
20259 else if (!parser
->in_function_body
20260 || current_binding_level
->kind
== sk_function_parms
)
20262 /* Normally, the array bound must be an integral constant
20263 expression. However, as an extension, we allow VLAs
20264 in function scopes as long as they aren't part of a
20265 parameter declaration. */
20266 cp_parser_error (parser
,
20267 "array bound is not an integer constant");
20268 bounds
= error_mark_node
;
20270 else if (processing_template_decl
20271 && !type_dependent_expression_p (bounds
))
20273 /* Remember this wasn't a constant-expression. */
20274 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20275 TREE_SIDE_EFFECTS (bounds
) = 1;
20279 bounds
= NULL_TREE
;
20280 /* Look for the closing `]'. */
20281 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20283 declarator
= cp_error_declarator
;
20287 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20288 declarator
= make_array_declarator (declarator
, bounds
);
20289 declarator
->std_attributes
= attrs
;
20291 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20294 tree qualifying_scope
;
20295 tree unqualified_name
;
20297 special_function_kind sfk
;
20299 bool pack_expansion_p
= false;
20300 cp_token
*declarator_id_start_token
;
20302 /* Parse a declarator-id */
20303 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20306 cp_parser_parse_tentatively (parser
);
20308 /* If we see an ellipsis, we should be looking at a
20310 if (token
->type
== CPP_ELLIPSIS
)
20312 /* Consume the `...' */
20313 cp_lexer_consume_token (parser
->lexer
);
20315 pack_expansion_p
= true;
20319 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20321 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20322 qualifying_scope
= parser
->scope
;
20327 if (!unqualified_name
&& pack_expansion_p
)
20329 /* Check whether an error occurred. */
20330 okay
= !cp_parser_error_occurred (parser
);
20332 /* We already consumed the ellipsis to mark a
20333 parameter pack, but we have no way to report it,
20334 so abort the tentative parse. We will be exiting
20335 immediately anyway. */
20336 cp_parser_abort_tentative_parse (parser
);
20339 okay
= cp_parser_parse_definitely (parser
);
20342 unqualified_name
= error_mark_node
;
20343 else if (unqualified_name
20344 && (qualifying_scope
20345 || (!identifier_p (unqualified_name
))))
20347 cp_parser_error (parser
, "expected unqualified-id");
20348 unqualified_name
= error_mark_node
;
20352 if (!unqualified_name
)
20354 if (unqualified_name
== error_mark_node
)
20356 declarator
= cp_error_declarator
;
20357 pack_expansion_p
= false;
20358 declarator
->parameter_pack_p
= false;
20362 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20364 if (qualifying_scope
&& at_namespace_scope_p ()
20365 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20367 /* In the declaration of a member of a template class
20368 outside of the class itself, the SCOPE will sometimes
20369 be a TYPENAME_TYPE. For example, given:
20371 template <typename T>
20372 int S<T>::R::i = 3;
20374 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20375 this context, we must resolve S<T>::R to an ordinary
20376 type, rather than a typename type.
20378 The reason we normally avoid resolving TYPENAME_TYPEs
20379 is that a specialization of `S' might render
20380 `S<T>::R' not a type. However, if `S' is
20381 specialized, then this `i' will not be used, so there
20382 is no harm in resolving the types here. */
20385 /* Resolve the TYPENAME_TYPE. */
20386 type
= resolve_typename_type (qualifying_scope
,
20387 /*only_current_p=*/false);
20388 /* If that failed, the declarator is invalid. */
20389 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20391 if (typedef_variant_p (type
))
20392 error_at (declarator_id_start_token
->location
,
20393 "cannot define member of dependent typedef "
20396 error_at (declarator_id_start_token
->location
,
20397 "%<%T::%E%> is not a type",
20398 TYPE_CONTEXT (qualifying_scope
),
20399 TYPE_IDENTIFIER (qualifying_scope
));
20401 qualifying_scope
= type
;
20406 if (unqualified_name
)
20410 if (qualifying_scope
20411 && CLASS_TYPE_P (qualifying_scope
))
20412 class_type
= qualifying_scope
;
20414 class_type
= current_class_type
;
20416 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20418 tree name_type
= TREE_TYPE (unqualified_name
);
20420 if (!class_type
|| !same_type_p (name_type
, class_type
))
20422 /* We do not attempt to print the declarator
20423 here because we do not have enough
20424 information about its original syntactic
20426 cp_parser_error (parser
, "invalid declarator");
20427 declarator
= cp_error_declarator
;
20430 else if (qualifying_scope
20431 && CLASSTYPE_USE_TEMPLATE (name_type
))
20433 error_at (declarator_id_start_token
->location
,
20434 "invalid use of constructor as a template");
20435 inform (declarator_id_start_token
->location
,
20436 "use %<%T::%D%> instead of %<%T::%D%> to "
20437 "name the constructor in a qualified name",
20439 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20440 class_type
, name_type
);
20441 declarator
= cp_error_declarator
;
20444 unqualified_name
= constructor_name (class_type
);
20449 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20450 sfk
= sfk_destructor
;
20451 else if (identifier_p (unqualified_name
)
20452 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20453 sfk
= sfk_conversion
;
20454 else if (/* There's no way to declare a constructor
20455 for an unnamed type, even if the type
20456 got a name for linkage purposes. */
20457 !TYPE_WAS_UNNAMED (class_type
)
20458 /* Handle correctly (c++/19200):
20472 friend void N::S();
20474 && (!friend_p
|| class_type
== qualifying_scope
)
20475 && constructor_name_p (unqualified_name
,
20477 sfk
= sfk_constructor
;
20478 else if (is_overloaded_fn (unqualified_name
)
20479 && DECL_CONSTRUCTOR_P (get_first_fn
20480 (unqualified_name
)))
20481 sfk
= sfk_constructor
;
20483 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20484 *ctor_dtor_or_conv_p
= -1;
20487 declarator
= make_id_declarator (qualifying_scope
,
20490 declarator
->std_attributes
= attrs
;
20491 declarator
->id_loc
= token
->location
;
20492 declarator
->parameter_pack_p
= pack_expansion_p
;
20494 if (pack_expansion_p
)
20495 maybe_warn_variadic_templates ();
20498 handle_declarator
:;
20499 scope
= get_scope_of_declarator (declarator
);
20502 /* Any names that appear after the declarator-id for a
20503 member are looked up in the containing scope. */
20504 if (at_function_scope_p ())
20506 /* But declarations with qualified-ids can't appear in a
20508 cp_parser_error (parser
, "qualified-id in declaration");
20509 declarator
= cp_error_declarator
;
20512 pushed_scope
= push_scope (scope
);
20514 parser
->in_declarator_p
= true;
20515 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20516 || (declarator
&& declarator
->kind
== cdk_id
))
20517 /* Default args are only allowed on function
20519 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20521 parser
->default_arg_ok_p
= false;
20530 /* For an abstract declarator, we might wind up with nothing at this
20531 point. That's an error; the declarator is not optional. */
20533 cp_parser_error (parser
, "expected declarator");
20534 else if (open_paren
)
20536 /* Record overly parenthesized declarator so we can give a
20537 diagnostic about confusing decl/expr disambiguation. */
20538 if (declarator
->kind
== cdk_array
)
20540 /* If the open and close parens are on different lines, this
20541 is probably a formatting thing, so ignore. */
20542 expanded_location open
= expand_location (open_paren
->location
);
20543 expanded_location close
= expand_location (close_paren
->location
);
20544 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20548 declarator
->parenthesized
= open_paren
->location
;
20551 /* If we entered a scope, we must exit it now. */
20553 pop_scope (pushed_scope
);
20555 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20556 parser
->in_declarator_p
= saved_in_declarator_p
;
20561 /* Parse a ptr-operator.
20564 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20565 * cv-qualifier-seq [opt]
20567 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20568 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20573 & cv-qualifier-seq [opt]
20575 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20576 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20577 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20578 filled in with the TYPE containing the member. *CV_QUALS is
20579 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20580 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20581 Note that the tree codes returned by this function have nothing
20582 to do with the types of trees that will be eventually be created
20583 to represent the pointer or reference type being parsed. They are
20584 just constants with suggestive names. */
20585 static enum tree_code
20586 cp_parser_ptr_operator (cp_parser
* parser
,
20588 cp_cv_quals
*cv_quals
,
20591 enum tree_code code
= ERROR_MARK
;
20593 tree attrs
= NULL_TREE
;
20595 /* Assume that it's not a pointer-to-member. */
20597 /* And that there are no cv-qualifiers. */
20598 *cv_quals
= TYPE_UNQUALIFIED
;
20600 /* Peek at the next token. */
20601 token
= cp_lexer_peek_token (parser
->lexer
);
20603 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20604 if (token
->type
== CPP_MULT
)
20605 code
= INDIRECT_REF
;
20606 else if (token
->type
== CPP_AND
)
20608 else if ((cxx_dialect
!= cxx98
) &&
20609 token
->type
== CPP_AND_AND
) /* C++0x only */
20610 code
= NON_LVALUE_EXPR
;
20612 if (code
!= ERROR_MARK
)
20614 /* Consume the `*', `&' or `&&'. */
20615 cp_lexer_consume_token (parser
->lexer
);
20617 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20618 `&', if we are allowing GNU extensions. (The only qualifier
20619 that can legally appear after `&' is `restrict', but that is
20620 enforced during semantic analysis. */
20621 if (code
== INDIRECT_REF
20622 || cp_parser_allow_gnu_extensions_p (parser
))
20623 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20625 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20626 if (attributes
!= NULL
)
20627 *attributes
= attrs
;
20631 /* Try the pointer-to-member case. */
20632 cp_parser_parse_tentatively (parser
);
20633 /* Look for the optional `::' operator. */
20634 cp_parser_global_scope_opt (parser
,
20635 /*current_scope_valid_p=*/false);
20636 /* Look for the nested-name specifier. */
20637 token
= cp_lexer_peek_token (parser
->lexer
);
20638 cp_parser_nested_name_specifier (parser
,
20639 /*typename_keyword_p=*/false,
20640 /*check_dependency_p=*/true,
20642 /*is_declaration=*/false);
20643 /* If we found it, and the next token is a `*', then we are
20644 indeed looking at a pointer-to-member operator. */
20645 if (!cp_parser_error_occurred (parser
)
20646 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20648 /* Indicate that the `*' operator was used. */
20649 code
= INDIRECT_REF
;
20651 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20652 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20653 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20654 error_at (token
->location
, "cannot form pointer to member of "
20655 "non-class %q#T", parser
->scope
);
20658 /* The type of which the member is a member is given by the
20660 *type
= parser
->scope
;
20661 /* The next name will not be qualified. */
20662 parser
->scope
= NULL_TREE
;
20663 parser
->qualifying_scope
= NULL_TREE
;
20664 parser
->object_scope
= NULL_TREE
;
20665 /* Look for optional c++11 attributes. */
20666 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20667 if (attributes
!= NULL
)
20668 *attributes
= attrs
;
20669 /* Look for the optional cv-qualifier-seq. */
20670 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20673 /* If that didn't work we don't have a ptr-operator. */
20674 if (!cp_parser_parse_definitely (parser
))
20675 cp_parser_error (parser
, "expected ptr-operator");
20681 /* Parse an (optional) cv-qualifier-seq.
20684 cv-qualifier cv-qualifier-seq [opt]
20695 Returns a bitmask representing the cv-qualifiers. */
20698 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20700 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20705 cp_cv_quals cv_qualifier
;
20707 /* Peek at the next token. */
20708 token
= cp_lexer_peek_token (parser
->lexer
);
20709 /* See if it's a cv-qualifier. */
20710 switch (token
->keyword
)
20713 cv_qualifier
= TYPE_QUAL_CONST
;
20717 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20721 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20725 cv_qualifier
= TYPE_UNQUALIFIED
;
20732 if (cv_quals
& cv_qualifier
)
20734 gcc_rich_location
richloc (token
->location
);
20735 richloc
.add_fixit_remove ();
20736 error_at (&richloc
, "duplicate cv-qualifier");
20737 cp_lexer_purge_token (parser
->lexer
);
20741 cp_lexer_consume_token (parser
->lexer
);
20742 cv_quals
|= cv_qualifier
;
20749 /* Parse an (optional) ref-qualifier
20755 Returns cp_ref_qualifier representing ref-qualifier. */
20757 static cp_ref_qualifier
20758 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20760 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20762 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20763 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20768 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20769 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20771 switch (token
->type
)
20774 curr_ref_qual
= REF_QUAL_LVALUE
;
20778 curr_ref_qual
= REF_QUAL_RVALUE
;
20782 curr_ref_qual
= REF_QUAL_NONE
;
20786 if (!curr_ref_qual
)
20790 error_at (token
->location
, "multiple ref-qualifiers");
20791 cp_lexer_purge_token (parser
->lexer
);
20795 ref_qual
= curr_ref_qual
;
20796 cp_lexer_consume_token (parser
->lexer
);
20803 /* Parse an optional tx-qualifier.
20807 transaction_safe_dynamic */
20810 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20812 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20813 if (token
->type
== CPP_NAME
)
20815 tree name
= token
->u
.value
;
20816 const char *p
= IDENTIFIER_POINTER (name
);
20817 const int len
= strlen ("transaction_safe");
20818 if (!strncmp (p
, "transaction_safe", len
))
20822 || !strcmp (p
, "_dynamic"))
20824 cp_lexer_consume_token (parser
->lexer
);
20827 error ("%qE requires %<-fgnu-tm%>", name
);
20838 /* Parse an (optional) virt-specifier-seq.
20840 virt-specifier-seq:
20841 virt-specifier virt-specifier-seq [opt]
20847 Returns a bitmask representing the virt-specifiers. */
20849 static cp_virt_specifiers
20850 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20852 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20857 cp_virt_specifiers virt_specifier
;
20859 /* Peek at the next token. */
20860 token
= cp_lexer_peek_token (parser
->lexer
);
20861 /* See if it's a virt-specifier-qualifier. */
20862 if (token
->type
!= CPP_NAME
)
20864 if (id_equal (token
->u
.value
, "override"))
20866 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20867 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20869 else if (id_equal (token
->u
.value
, "final"))
20871 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20872 virt_specifier
= VIRT_SPEC_FINAL
;
20874 else if (id_equal (token
->u
.value
, "__final"))
20876 virt_specifier
= VIRT_SPEC_FINAL
;
20881 if (virt_specifiers
& virt_specifier
)
20883 gcc_rich_location
richloc (token
->location
);
20884 richloc
.add_fixit_remove ();
20885 error_at (&richloc
, "duplicate virt-specifier");
20886 cp_lexer_purge_token (parser
->lexer
);
20890 cp_lexer_consume_token (parser
->lexer
);
20891 virt_specifiers
|= virt_specifier
;
20894 return virt_specifiers
;
20897 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20898 is in scope even though it isn't real. */
20901 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20905 if (current_class_ptr
)
20907 /* We don't clear this between NSDMIs. Is it already what we want? */
20908 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20909 if (DECL_P (current_class_ptr
)
20910 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20911 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20912 && cp_type_quals (type
) == quals
)
20916 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20917 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20918 current_class_ptr
= NULL_TREE
;
20920 = cp_build_fold_indirect_ref (this_parm
);
20921 current_class_ptr
= this_parm
;
20924 /* Return true iff our current scope is a non-static data member
20928 parsing_nsdmi (void)
20930 /* We recognize NSDMI context by the context-less 'this' pointer set up
20931 by the function above. */
20932 if (current_class_ptr
20933 && TREE_CODE (current_class_ptr
) == PARM_DECL
20934 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20939 /* Parse a late-specified return type, if any. This is not a separate
20940 non-terminal, but part of a function declarator, which looks like
20942 -> trailing-type-specifier-seq abstract-declarator(opt)
20944 Returns the type indicated by the type-id.
20946 In addition to this, parse any queued up #pragma omp declare simd
20947 clauses, and #pragma acc routine clauses.
20949 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20953 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20954 tree
& requires_clause
, cp_cv_quals quals
)
20957 tree type
= NULL_TREE
;
20958 bool declare_simd_p
= (parser
->omp_declare_simd
20960 && declarator
->kind
== cdk_id
);
20962 bool oacc_routine_p
= (parser
->oacc_routine
20964 && declarator
->kind
== cdk_id
);
20966 /* Peek at the next token. */
20967 token
= cp_lexer_peek_token (parser
->lexer
);
20968 /* A late-specified return type is indicated by an initial '->'. */
20969 if (token
->type
!= CPP_DEREF
20970 && token
->keyword
!= RID_REQUIRES
20971 && !(token
->type
== CPP_NAME
20972 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20973 && !(declare_simd_p
|| oacc_routine_p
))
20976 tree save_ccp
= current_class_ptr
;
20977 tree save_ccr
= current_class_ref
;
20980 /* DR 1207: 'this' is in scope in the trailing return type. */
20981 inject_this_parameter (current_class_type
, quals
);
20984 if (token
->type
== CPP_DEREF
)
20986 /* Consume the ->. */
20987 cp_lexer_consume_token (parser
->lexer
);
20989 type
= cp_parser_trailing_type_id (parser
);
20992 /* Function declarations may be followed by a trailing
20993 requires-clause. */
20994 requires_clause
= cp_parser_requires_clause_opt (parser
);
20996 if (declare_simd_p
)
20997 declarator
->attributes
20998 = cp_parser_late_parsing_omp_declare_simd (parser
,
20999 declarator
->attributes
);
21000 if (oacc_routine_p
)
21001 declarator
->attributes
21002 = cp_parser_late_parsing_oacc_routine (parser
,
21003 declarator
->attributes
);
21007 current_class_ptr
= save_ccp
;
21008 current_class_ref
= save_ccr
;
21014 /* Parse a declarator-id.
21018 :: [opt] nested-name-specifier [opt] type-name
21020 In the `id-expression' case, the value returned is as for
21021 cp_parser_id_expression if the id-expression was an unqualified-id.
21022 If the id-expression was a qualified-id, then a SCOPE_REF is
21023 returned. The first operand is the scope (either a NAMESPACE_DECL
21024 or TREE_TYPE), but the second is still just a representation of an
21028 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21031 /* The expression must be an id-expression. Assume that qualified
21032 names are the names of types so that:
21035 int S<T>::R::i = 3;
21037 will work; we must treat `S<T>::R' as the name of a type.
21038 Similarly, assume that qualified names are templates, where
21042 int S<T>::R<T>::i = 3;
21045 id
= cp_parser_id_expression (parser
,
21046 /*template_keyword_p=*/false,
21047 /*check_dependency_p=*/false,
21048 /*template_p=*/NULL
,
21049 /*declarator_p=*/true,
21051 if (id
&& BASELINK_P (id
))
21052 id
= BASELINK_FUNCTIONS (id
);
21056 /* Parse a type-id.
21059 type-specifier-seq abstract-declarator [opt]
21061 Returns the TYPE specified. */
21064 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21065 bool is_trailing_return
)
21067 cp_decl_specifier_seq type_specifier_seq
;
21068 cp_declarator
*abstract_declarator
;
21070 /* Parse the type-specifier-seq. */
21071 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21072 is_trailing_return
,
21073 &type_specifier_seq
);
21074 if (is_template_arg
&& type_specifier_seq
.type
21075 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21076 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21077 /* A bare template name as a template argument is a template template
21078 argument, not a placeholder, so fail parsing it as a type argument. */
21080 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21081 cp_parser_simulate_error (parser
);
21082 return error_mark_node
;
21084 if (type_specifier_seq
.type
== error_mark_node
)
21085 return error_mark_node
;
21087 /* There might or might not be an abstract declarator. */
21088 cp_parser_parse_tentatively (parser
);
21089 /* Look for the declarator. */
21090 abstract_declarator
21091 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21092 /*parenthesized_p=*/NULL
,
21093 /*member_p=*/false,
21094 /*friend_p=*/false);
21095 /* Check to see if there really was a declarator. */
21096 if (!cp_parser_parse_definitely (parser
))
21097 abstract_declarator
= NULL
;
21099 if (type_specifier_seq
.type
21100 /* The concepts TS allows 'auto' as a type-id. */
21101 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21102 /* None of the valid uses of 'auto' in C++14 involve the type-id
21103 nonterminal, but it is valid in a trailing-return-type. */
21104 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21105 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21107 /* A type-id with type 'auto' is only ok if the abstract declarator
21108 is a function declarator with a late-specified return type.
21110 A type-id with 'auto' is also valid in a trailing-return-type
21111 in a compound-requirement. */
21112 if (abstract_declarator
21113 && abstract_declarator
->kind
== cdk_function
21114 && abstract_declarator
->u
.function
.late_return_type
)
21116 else if (parser
->in_result_type_constraint_p
)
21120 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21121 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21123 error_at (loc
, "missing template arguments after %qT",
21125 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21129 error_at (loc
, "invalid use of %qT", auto_node
);
21130 return error_mark_node
;
21134 return groktypename (&type_specifier_seq
, abstract_declarator
,
21139 cp_parser_type_id (cp_parser
*parser
)
21141 return cp_parser_type_id_1 (parser
, false, false);
21145 cp_parser_template_type_arg (cp_parser
*parser
)
21148 const char *saved_message
= parser
->type_definition_forbidden_message
;
21149 parser
->type_definition_forbidden_message
21150 = G_("types may not be defined in template arguments");
21151 r
= cp_parser_type_id_1 (parser
, true, false);
21152 parser
->type_definition_forbidden_message
= saved_message
;
21153 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21155 error ("invalid use of %<auto%> in template argument");
21156 r
= error_mark_node
;
21162 cp_parser_trailing_type_id (cp_parser
*parser
)
21164 return cp_parser_type_id_1 (parser
, false, true);
21167 /* Parse a type-specifier-seq.
21169 type-specifier-seq:
21170 type-specifier type-specifier-seq [opt]
21174 type-specifier-seq:
21175 attributes type-specifier-seq [opt]
21177 If IS_DECLARATION is true, we are at the start of a "condition" or
21178 exception-declaration, so we might be followed by a declarator-id.
21180 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21181 i.e. we've just seen "->".
21183 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21186 cp_parser_type_specifier_seq (cp_parser
* parser
,
21187 bool is_declaration
,
21188 bool is_trailing_return
,
21189 cp_decl_specifier_seq
*type_specifier_seq
)
21191 bool seen_type_specifier
= false;
21192 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21193 cp_token
*start_token
= NULL
;
21195 /* Clear the TYPE_SPECIFIER_SEQ. */
21196 clear_decl_specs (type_specifier_seq
);
21198 /* In the context of a trailing return type, enum E { } is an
21199 elaborated-type-specifier followed by a function-body, not an
21201 if (is_trailing_return
)
21202 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21204 /* Parse the type-specifiers and attributes. */
21207 tree type_specifier
;
21208 bool is_cv_qualifier
;
21210 /* Check for attributes first. */
21211 if (cp_next_tokens_can_be_attribute_p (parser
))
21213 type_specifier_seq
->attributes
21214 = attr_chainon (type_specifier_seq
->attributes
,
21215 cp_parser_attributes_opt (parser
));
21219 /* record the token of the beginning of the type specifier seq,
21220 for error reporting purposes*/
21222 start_token
= cp_lexer_peek_token (parser
->lexer
);
21224 /* Look for the type-specifier. */
21225 type_specifier
= cp_parser_type_specifier (parser
,
21227 type_specifier_seq
,
21228 /*is_declaration=*/false,
21231 if (!type_specifier
)
21233 /* If the first type-specifier could not be found, this is not a
21234 type-specifier-seq at all. */
21235 if (!seen_type_specifier
)
21237 /* Set in_declarator_p to avoid skipping to the semicolon. */
21238 int in_decl
= parser
->in_declarator_p
;
21239 parser
->in_declarator_p
= true;
21241 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21242 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21243 cp_parser_error (parser
, "expected type-specifier");
21245 parser
->in_declarator_p
= in_decl
;
21247 type_specifier_seq
->type
= error_mark_node
;
21250 /* If subsequent type-specifiers could not be found, the
21251 type-specifier-seq is complete. */
21255 seen_type_specifier
= true;
21256 /* The standard says that a condition can be:
21258 type-specifier-seq declarator = assignment-expression
21265 we should treat the "S" as a declarator, not as a
21266 type-specifier. The standard doesn't say that explicitly for
21267 type-specifier-seq, but it does say that for
21268 decl-specifier-seq in an ordinary declaration. Perhaps it
21269 would be clearer just to allow a decl-specifier-seq here, and
21270 then add a semantic restriction that if any decl-specifiers
21271 that are not type-specifiers appear, the program is invalid. */
21272 if (is_declaration
&& !is_cv_qualifier
)
21273 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21277 /* Return whether the function currently being declared has an associated
21278 template parameter list. */
21281 function_being_declared_is_template_p (cp_parser
* parser
)
21283 if (!current_template_parms
|| processing_template_parmlist
)
21286 if (parser
->implicit_template_scope
)
21289 if (at_class_scope_p ()
21290 && TYPE_BEING_DEFINED (current_class_type
))
21291 return parser
->num_template_parameter_lists
!= 0;
21293 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21294 (current_class_type
));
21297 /* Parse a parameter-declaration-clause.
21299 parameter-declaration-clause:
21300 parameter-declaration-list [opt] ... [opt]
21301 parameter-declaration-list , ...
21303 Returns a representation for the parameter declarations. A return
21304 value of NULL indicates a parameter-declaration-clause consisting
21305 only of an ellipsis. */
21308 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21314 temp_override
<bool> cleanup
21315 (parser
->auto_is_implicit_function_template_parm_p
);
21317 if (!processing_specialization
21318 && !processing_template_parmlist
21319 && !processing_explicit_instantiation
21320 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21321 actual function or a random abstract declarator. */
21322 && parser
->default_arg_ok_p
)
21323 if (!current_function_decl
21324 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21325 parser
->auto_is_implicit_function_template_parm_p
= true;
21327 /* Peek at the next token. */
21328 token
= cp_lexer_peek_token (parser
->lexer
);
21329 /* Check for trivial parameter-declaration-clauses. */
21330 if (token
->type
== CPP_ELLIPSIS
)
21332 /* Consume the `...' token. */
21333 cp_lexer_consume_token (parser
->lexer
);
21336 else if (token
->type
== CPP_CLOSE_PAREN
)
21337 /* There are no parameters. */
21339 #ifndef NO_IMPLICIT_EXTERN_C
21340 if (in_system_header_at (input_location
)
21341 && current_class_type
== NULL
21342 && current_lang_name
== lang_name_c
)
21346 return void_list_node
;
21348 /* Check for `(void)', too, which is a special case. */
21349 else if (token
->keyword
== RID_VOID
21350 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21351 == CPP_CLOSE_PAREN
))
21353 /* Consume the `void' token. */
21354 cp_lexer_consume_token (parser
->lexer
);
21355 /* There are no parameters. */
21356 return void_list_node
;
21359 /* Parse the parameter-declaration-list. */
21360 parameters
= cp_parser_parameter_declaration_list (parser
);
21361 /* If a parse error occurred while parsing the
21362 parameter-declaration-list, then the entire
21363 parameter-declaration-clause is erroneous. */
21364 if (parameters
== error_mark_node
)
21367 /* Peek at the next token. */
21368 token
= cp_lexer_peek_token (parser
->lexer
);
21369 /* If it's a `,', the clause should terminate with an ellipsis. */
21370 if (token
->type
== CPP_COMMA
)
21372 /* Consume the `,'. */
21373 cp_lexer_consume_token (parser
->lexer
);
21374 /* Expect an ellipsis. */
21376 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21378 /* It might also be `...' if the optional trailing `,' was
21380 else if (token
->type
== CPP_ELLIPSIS
)
21382 /* Consume the `...' token. */
21383 cp_lexer_consume_token (parser
->lexer
);
21384 /* And remember that we saw it. */
21388 ellipsis_p
= false;
21390 /* Finish the parameter list. */
21392 parameters
= chainon (parameters
, void_list_node
);
21397 /* Parse a parameter-declaration-list.
21399 parameter-declaration-list:
21400 parameter-declaration
21401 parameter-declaration-list , parameter-declaration
21403 Returns a representation of the parameter-declaration-list, as for
21404 cp_parser_parameter_declaration_clause. However, the
21405 `void_list_node' is never appended to the list. */
21408 cp_parser_parameter_declaration_list (cp_parser
* parser
)
21410 tree parameters
= NULL_TREE
;
21411 tree
*tail
= ¶meters
;
21412 bool saved_in_unbraced_linkage_specification_p
;
21415 /* The special considerations that apply to a function within an
21416 unbraced linkage specifications do not apply to the parameters
21417 to the function. */
21418 saved_in_unbraced_linkage_specification_p
21419 = parser
->in_unbraced_linkage_specification_p
;
21420 parser
->in_unbraced_linkage_specification_p
= false;
21422 /* Look for more parameters. */
21425 cp_parameter_declarator
*parameter
;
21426 tree decl
= error_mark_node
;
21427 bool parenthesized_p
= false;
21429 /* Parse the parameter. */
21431 = cp_parser_parameter_declaration (parser
,
21432 /*template_parm_p=*/false,
21435 /* We don't know yet if the enclosing context is deprecated, so wait
21436 and warn in grokparms if appropriate. */
21437 deprecated_state
= DEPRECATED_SUPPRESS
;
21441 decl
= grokdeclarator (parameter
->declarator
,
21442 ¶meter
->decl_specifiers
,
21444 parameter
->default_argument
!= NULL_TREE
,
21445 ¶meter
->decl_specifiers
.attributes
);
21446 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21447 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21450 deprecated_state
= DEPRECATED_NORMAL
;
21452 /* If a parse error occurred parsing the parameter declaration,
21453 then the entire parameter-declaration-list is erroneous. */
21454 if (decl
== error_mark_node
)
21456 parameters
= error_mark_node
;
21460 if (parameter
->decl_specifiers
.attributes
)
21461 cplus_decl_attributes (&decl
,
21462 parameter
->decl_specifiers
.attributes
,
21464 if (DECL_NAME (decl
))
21465 decl
= pushdecl (decl
);
21467 if (decl
!= error_mark_node
)
21469 retrofit_lang_decl (decl
);
21470 DECL_PARM_INDEX (decl
) = ++index
;
21471 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21474 /* Add the new parameter to the list. */
21475 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21476 tail
= &TREE_CHAIN (*tail
);
21478 /* Peek at the next token. */
21479 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21480 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21481 /* These are for Objective-C++ */
21482 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21483 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21484 /* The parameter-declaration-list is complete. */
21486 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21490 /* Peek at the next token. */
21491 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21492 /* If it's an ellipsis, then the list is complete. */
21493 if (token
->type
== CPP_ELLIPSIS
)
21495 /* Otherwise, there must be more parameters. Consume the
21497 cp_lexer_consume_token (parser
->lexer
);
21498 /* When parsing something like:
21500 int i(float f, double d)
21502 we can tell after seeing the declaration for "f" that we
21503 are not looking at an initialization of a variable "i",
21504 but rather at the declaration of a function "i".
21506 Due to the fact that the parsing of template arguments
21507 (as specified to a template-id) requires backtracking we
21508 cannot use this technique when inside a template argument
21510 if (!parser
->in_template_argument_list_p
21511 && !parser
->in_type_id_in_expr_p
21512 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21513 /* However, a parameter-declaration of the form
21514 "float(f)" (which is a valid declaration of a
21515 parameter "f") can also be interpreted as an
21516 expression (the conversion of "f" to "float"). */
21517 && !parenthesized_p
)
21518 cp_parser_commit_to_tentative_parse (parser
);
21522 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21523 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21524 cp_parser_skip_to_closing_parenthesis (parser
,
21525 /*recovering=*/true,
21526 /*or_comma=*/false,
21527 /*consume_paren=*/false);
21532 parser
->in_unbraced_linkage_specification_p
21533 = saved_in_unbraced_linkage_specification_p
;
21535 /* Reset implicit_template_scope if we are about to leave the function
21536 parameter list that introduced it. Note that for out-of-line member
21537 definitions, there will be one or more class scopes before we get to
21538 the template parameter scope. */
21540 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21541 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21543 while (maybe_its
->kind
== sk_class
)
21544 maybe_its
= maybe_its
->level_chain
;
21545 if (maybe_its
== its
)
21547 parser
->implicit_template_parms
= 0;
21548 parser
->implicit_template_scope
= 0;
21555 /* Parse a parameter declaration.
21557 parameter-declaration:
21558 decl-specifier-seq ... [opt] declarator
21559 decl-specifier-seq declarator = assignment-expression
21560 decl-specifier-seq ... [opt] abstract-declarator [opt]
21561 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21563 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21564 declares a template parameter. (In that case, a non-nested `>'
21565 token encountered during the parsing of the assignment-expression
21566 is not interpreted as a greater-than operator.)
21568 Returns a representation of the parameter, or NULL if an error
21569 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21570 true iff the declarator is of the form "(p)". */
21572 static cp_parameter_declarator
*
21573 cp_parser_parameter_declaration (cp_parser
*parser
,
21574 bool template_parm_p
,
21575 bool *parenthesized_p
)
21577 int declares_class_or_enum
;
21578 cp_decl_specifier_seq decl_specifiers
;
21579 cp_declarator
*declarator
;
21580 tree default_argument
;
21581 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21582 const char *saved_message
;
21583 bool template_parameter_pack_p
= false;
21585 /* In a template parameter, `>' is not an operator.
21589 When parsing a default template-argument for a non-type
21590 template-parameter, the first non-nested `>' is taken as the end
21591 of the template parameter-list rather than a greater-than
21594 /* Type definitions may not appear in parameter types. */
21595 saved_message
= parser
->type_definition_forbidden_message
;
21596 parser
->type_definition_forbidden_message
21597 = G_("types may not be defined in parameter types");
21599 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21600 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21601 (current_template_parms
)) : 0);
21603 /* Parse the declaration-specifiers. */
21604 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21605 cp_parser_decl_specifier_seq (parser
,
21606 CP_PARSER_FLAGS_NONE
,
21608 &declares_class_or_enum
);
21610 /* Complain about missing 'typename' or other invalid type names. */
21611 if (!decl_specifiers
.any_type_specifiers_p
21612 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21613 decl_specifiers
.type
= error_mark_node
;
21615 /* If an error occurred, there's no reason to attempt to parse the
21616 rest of the declaration. */
21617 if (cp_parser_error_occurred (parser
))
21619 parser
->type_definition_forbidden_message
= saved_message
;
21623 /* Peek at the next token. */
21624 token
= cp_lexer_peek_token (parser
->lexer
);
21626 /* If the next token is a `)', `,', `=', `>', or `...', then there
21627 is no declarator. However, when variadic templates are enabled,
21628 there may be a declarator following `...'. */
21629 if (token
->type
== CPP_CLOSE_PAREN
21630 || token
->type
== CPP_COMMA
21631 || token
->type
== CPP_EQ
21632 || token
->type
== CPP_GREATER
)
21635 if (parenthesized_p
)
21636 *parenthesized_p
= false;
21638 /* Otherwise, there should be a declarator. */
21641 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21642 parser
->default_arg_ok_p
= false;
21644 /* After seeing a decl-specifier-seq, if the next token is not a
21645 "(", there is no possibility that the code is a valid
21646 expression. Therefore, if parsing tentatively, we commit at
21648 if (!parser
->in_template_argument_list_p
21649 /* In an expression context, having seen:
21653 we cannot be sure whether we are looking at a
21654 function-type (taking a "char" as a parameter) or a cast
21655 of some object of type "char" to "int". */
21656 && !parser
->in_type_id_in_expr_p
21657 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21658 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21659 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21660 cp_parser_commit_to_tentative_parse (parser
);
21661 /* Parse the declarator. */
21662 declarator_token_start
= token
;
21663 declarator
= cp_parser_declarator (parser
,
21664 CP_PARSER_DECLARATOR_EITHER
,
21665 /*ctor_dtor_or_conv_p=*/NULL
,
21667 /*member_p=*/false,
21668 /*friend_p=*/false);
21669 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21670 /* After the declarator, allow more attributes. */
21671 decl_specifiers
.attributes
21672 = attr_chainon (decl_specifiers
.attributes
,
21673 cp_parser_attributes_opt (parser
));
21675 /* If the declarator is a template parameter pack, remember that and
21676 clear the flag in the declarator itself so we don't get errors
21677 from grokdeclarator. */
21678 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21680 declarator
->parameter_pack_p
= false;
21681 template_parameter_pack_p
= true;
21685 /* If the next token is an ellipsis, and we have not seen a declarator
21686 name, and if either the type of the declarator contains parameter
21687 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21688 for, eg, abbreviated integral type names), then we actually have a
21689 parameter pack expansion expression. Otherwise, leave the ellipsis
21690 for a C-style variadic function. */
21691 token
= cp_lexer_peek_token (parser
->lexer
);
21693 /* If a function parameter pack was specified and an implicit template
21694 parameter was introduced during cp_parser_parameter_declaration,
21695 change any implicit parameters introduced into packs. */
21696 if (parser
->implicit_template_parms
21697 && (token
->type
== CPP_ELLIPSIS
21698 || (declarator
&& declarator
->parameter_pack_p
)))
21700 int latest_template_parm_idx
= TREE_VEC_LENGTH
21701 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21703 if (latest_template_parm_idx
!= template_parm_idx
)
21704 decl_specifiers
.type
= convert_generic_types_to_packs
21705 (decl_specifiers
.type
,
21706 template_parm_idx
, latest_template_parm_idx
);
21709 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21711 tree type
= decl_specifiers
.type
;
21713 if (type
&& DECL_P (type
))
21714 type
= TREE_TYPE (type
);
21717 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21718 && (template_parm_p
|| uses_parameter_packs (type
)))
21719 || (!type
&& template_parm_p
))
21720 && declarator_can_be_parameter_pack (declarator
))
21722 /* Consume the `...'. */
21723 cp_lexer_consume_token (parser
->lexer
);
21724 maybe_warn_variadic_templates ();
21726 /* Build a pack expansion type */
21727 if (template_parm_p
)
21728 template_parameter_pack_p
= true;
21729 else if (declarator
)
21730 declarator
->parameter_pack_p
= true;
21732 decl_specifiers
.type
= make_pack_expansion (type
);
21736 /* The restriction on defining new types applies only to the type
21737 of the parameter, not to the default argument. */
21738 parser
->type_definition_forbidden_message
= saved_message
;
21740 /* If the next token is `=', then process a default argument. */
21741 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21743 tree type
= decl_specifiers
.type
;
21744 token
= cp_lexer_peek_token (parser
->lexer
);
21745 /* If we are defining a class, then the tokens that make up the
21746 default argument must be saved and processed later. */
21747 if (!template_parm_p
&& at_class_scope_p ()
21748 && TYPE_BEING_DEFINED (current_class_type
)
21749 && !LAMBDA_TYPE_P (current_class_type
))
21750 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21752 // A constrained-type-specifier may declare a type template-parameter.
21753 else if (declares_constrained_type_template_parameter (type
))
21755 = cp_parser_default_type_template_argument (parser
);
21757 // A constrained-type-specifier may declare a template-template-parameter.
21758 else if (declares_constrained_template_template_parameter (type
))
21760 = cp_parser_default_template_template_argument (parser
);
21762 /* Outside of a class definition, we can just parse the
21763 assignment-expression. */
21766 = cp_parser_default_argument (parser
, template_parm_p
);
21768 if (!parser
->default_arg_ok_p
)
21770 permerror (token
->location
,
21771 "default arguments are only "
21772 "permitted for function parameters");
21774 else if ((declarator
&& declarator
->parameter_pack_p
)
21775 || template_parameter_pack_p
21776 || (decl_specifiers
.type
21777 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21779 /* Find the name of the parameter pack. */
21780 cp_declarator
*id_declarator
= declarator
;
21781 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21782 id_declarator
= id_declarator
->declarator
;
21784 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21785 error_at (declarator_token_start
->location
,
21787 ? G_("template parameter pack %qD "
21788 "cannot have a default argument")
21789 : G_("parameter pack %qD cannot have "
21790 "a default argument"),
21791 id_declarator
->u
.id
.unqualified_name
);
21793 error_at (declarator_token_start
->location
,
21795 ? G_("template parameter pack cannot have "
21796 "a default argument")
21797 : G_("parameter pack cannot have a "
21798 "default argument"));
21800 default_argument
= NULL_TREE
;
21804 default_argument
= NULL_TREE
;
21806 /* Generate a location for the parameter, ranging from the start of the
21807 initial token to the end of the final token (using input_location for
21808 the latter, set up by cp_lexer_set_source_position_from_token when
21811 If we have a identifier, then use it for the caret location, e.g.
21813 extern int callee (int one, int (*two)(int, int), float three);
21814 ~~~~~~^~~~~~~~~~~~~~
21816 otherwise, reuse the start location for the caret location e.g.:
21818 extern int callee (int one, int (*)(int, int), float three);
21822 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21823 ? declarator
->id_loc
21824 : decl_spec_token_start
->location
);
21825 location_t param_loc
= make_location (caret_loc
,
21826 decl_spec_token_start
->location
,
21829 return make_parameter_declarator (&decl_specifiers
,
21833 template_parameter_pack_p
);
21836 /* Parse a default argument and return it.
21838 TEMPLATE_PARM_P is true if this is a default argument for a
21839 non-type template parameter. */
21841 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21843 tree default_argument
= NULL_TREE
;
21844 bool saved_greater_than_is_operator_p
;
21845 bool saved_local_variables_forbidden_p
;
21846 bool non_constant_p
, is_direct_init
;
21848 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21850 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21851 parser
->greater_than_is_operator_p
= !template_parm_p
;
21852 /* Local variable names (and the `this' keyword) may not
21853 appear in a default argument. */
21854 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21855 parser
->local_variables_forbidden_p
= true;
21856 /* Parse the assignment-expression. */
21857 if (template_parm_p
)
21858 push_deferring_access_checks (dk_no_deferred
);
21859 tree saved_class_ptr
= NULL_TREE
;
21860 tree saved_class_ref
= NULL_TREE
;
21861 /* The "this" pointer is not valid in a default argument. */
21864 saved_class_ptr
= current_class_ptr
;
21865 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21866 saved_class_ref
= current_class_ref
;
21867 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21870 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21871 /* Restore the "this" pointer. */
21874 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21875 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21877 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21878 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21879 if (template_parm_p
)
21880 pop_deferring_access_checks ();
21881 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21882 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21884 return default_argument
;
21887 /* Parse a function-body.
21890 compound_statement */
21893 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21895 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21896 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21900 /* Parse a ctor-initializer-opt followed by a function-body. Return
21901 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21902 is true we are parsing a function-try-block. */
21905 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21906 bool in_function_try_block
)
21909 const bool check_body_p
=
21910 DECL_CONSTRUCTOR_P (current_function_decl
)
21911 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21914 /* Begin the function body. */
21915 body
= begin_function_body ();
21916 /* Parse the optional ctor-initializer. */
21917 cp_parser_ctor_initializer_opt (parser
);
21919 /* If we're parsing a constexpr constructor definition, we need
21920 to check that the constructor body is indeed empty. However,
21921 before we get to cp_parser_function_body lot of junk has been
21922 generated, so we can't just check that we have an empty block.
21923 Rather we take a snapshot of the outermost block, and check whether
21924 cp_parser_function_body changed its state. */
21927 list
= cur_stmt_list
;
21928 if (STATEMENT_LIST_TAIL (list
))
21929 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21931 /* Parse the function-body. */
21932 cp_parser_function_body (parser
, in_function_try_block
);
21934 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21935 /* Finish the function body. */
21936 finish_function_body (body
);
21939 /* Parse an initializer.
21942 = initializer-clause
21943 ( expression-list )
21945 Returns an expression representing the initializer. If no
21946 initializer is present, NULL_TREE is returned.
21948 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21949 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21950 set to TRUE if there is no initializer present. If there is an
21951 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21952 is set to true; otherwise it is set to false. */
21955 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21956 bool* non_constant_p
, bool subexpression_p
)
21961 /* Peek at the next token. */
21962 token
= cp_lexer_peek_token (parser
->lexer
);
21964 /* Let our caller know whether or not this initializer was
21966 *is_direct_init
= (token
->type
!= CPP_EQ
);
21967 /* Assume that the initializer is constant. */
21968 *non_constant_p
= false;
21970 if (token
->type
== CPP_EQ
)
21972 /* Consume the `='. */
21973 cp_lexer_consume_token (parser
->lexer
);
21974 /* Parse the initializer-clause. */
21975 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21977 else if (token
->type
== CPP_OPEN_PAREN
)
21979 vec
<tree
, va_gc
> *vec
;
21980 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21982 /*allow_expansion_p=*/true,
21985 return error_mark_node
;
21986 init
= build_tree_list_vec (vec
);
21987 release_tree_vector (vec
);
21989 else if (token
->type
== CPP_OPEN_BRACE
)
21991 cp_lexer_set_source_position (parser
->lexer
);
21992 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21993 init
= cp_parser_braced_list (parser
, non_constant_p
);
21994 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21998 /* Anything else is an error. */
21999 cp_parser_error (parser
, "expected initializer");
22000 init
= error_mark_node
;
22003 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22004 init
= error_mark_node
;
22009 /* Parse an initializer-clause.
22011 initializer-clause:
22012 assignment-expression
22015 Returns an expression representing the initializer.
22017 If the `assignment-expression' production is used the value
22018 returned is simply a representation for the expression.
22020 Otherwise, calls cp_parser_braced_list. */
22023 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22025 cp_expr initializer
;
22027 /* Assume the expression is constant. */
22028 *non_constant_p
= false;
22030 /* If it is not a `{', then we are looking at an
22031 assignment-expression. */
22032 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22035 = cp_parser_constant_expression (parser
,
22036 /*allow_non_constant_p=*/true,
22040 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22042 return initializer
;
22045 /* Parse a brace-enclosed initializer list.
22048 { initializer-list , [opt] }
22049 { designated-initializer-list , [opt] }
22052 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22053 the elements of the initializer-list (or NULL, if the last
22054 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22055 NULL_TREE. There is no way to detect whether or not the optional
22056 trailing `,' was provided. NON_CONSTANT_P is as for
22057 cp_parser_initializer. */
22060 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22063 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22065 /* Consume the `{' token. */
22066 matching_braces braces
;
22067 braces
.require_open (parser
);
22068 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22069 initializer
= make_node (CONSTRUCTOR
);
22070 /* If it's not a `}', then there is a non-trivial initializer. */
22071 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22073 /* Parse the initializer list. */
22074 CONSTRUCTOR_ELTS (initializer
)
22075 = cp_parser_initializer_list (parser
, non_constant_p
);
22076 /* A trailing `,' token is allowed. */
22077 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22078 cp_lexer_consume_token (parser
->lexer
);
22081 *non_constant_p
= false;
22082 /* Now, there should be a trailing `}'. */
22083 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22084 braces
.require_close (parser
);
22085 TREE_TYPE (initializer
) = init_list_type_node
;
22087 cp_expr
result (initializer
);
22088 /* Build a location of the form:
22091 with caret==start at the open brace, finish at the close brace. */
22092 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22093 result
.set_location (combined_loc
);
22097 /* Consume tokens up to, and including, the next non-nested closing `]'.
22098 Returns true iff we found a closing `]'. */
22101 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22103 unsigned square_depth
= 0;
22107 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22109 switch (token
->type
)
22112 case CPP_PRAGMA_EOL
:
22113 /* If we've run out of tokens, then there is no closing `]'. */
22116 case CPP_OPEN_SQUARE
:
22120 case CPP_CLOSE_SQUARE
:
22121 if (!square_depth
--)
22123 cp_lexer_consume_token (parser
->lexer
);
22132 /* Consume the token. */
22133 cp_lexer_consume_token (parser
->lexer
);
22137 /* Return true if we are looking at an array-designator, false otherwise. */
22140 cp_parser_array_designator_p (cp_parser
*parser
)
22142 /* Consume the `['. */
22143 cp_lexer_consume_token (parser
->lexer
);
22145 cp_lexer_save_tokens (parser
->lexer
);
22147 /* Skip tokens until the next token is a closing square bracket.
22148 If we find the closing `]', and the next token is a `=', then
22149 we are looking at an array designator. */
22150 bool array_designator_p
22151 = (cp_parser_skip_to_closing_square_bracket (parser
)
22152 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22154 /* Roll back the tokens we skipped. */
22155 cp_lexer_rollback_tokens (parser
->lexer
);
22157 return array_designator_p
;
22160 /* Parse an initializer-list.
22163 initializer-clause ... [opt]
22164 initializer-list , initializer-clause ... [opt]
22168 designated-initializer-list:
22169 designated-initializer-clause
22170 designated-initializer-list , designated-initializer-clause
22172 designated-initializer-clause:
22173 designator brace-or-equal-initializer
22181 designation initializer-clause ...[opt]
22182 initializer-list , designation initializer-clause ...[opt]
22187 [ constant-expression ] =
22189 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22190 for the initializer. If the INDEX of the elt is non-NULL, it is the
22191 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22192 as for cp_parser_initializer. */
22194 static vec
<constructor_elt
, va_gc
> *
22195 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22197 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22198 bool first_p
= true;
22199 tree first_designator
= NULL_TREE
;
22201 /* Assume all of the expressions are constant. */
22202 *non_constant_p
= false;
22204 /* Parse the rest of the list. */
22210 bool clause_non_constant_p
;
22211 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22213 /* Handle the C++2A syntax, '. id ='. */
22214 if ((cxx_dialect
>= cxx2a
22215 || cp_parser_allow_gnu_extensions_p (parser
))
22216 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22217 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22218 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22219 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22220 == CPP_OPEN_BRACE
)))
22222 if (cxx_dialect
< cxx2a
)
22223 pedwarn (loc
, OPT_Wpedantic
,
22224 "C++ designated initializers only available with "
22225 "-std=c++2a or -std=gnu++2a");
22226 /* Consume the `.'. */
22227 cp_lexer_consume_token (parser
->lexer
);
22228 /* Consume the identifier. */
22229 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22231 /* Consume the `='. */
22232 cp_lexer_consume_token (parser
->lexer
);
22234 /* Also, if the next token is an identifier and the following one is a
22235 colon, we are looking at the GNU designated-initializer
22237 else if (cp_parser_allow_gnu_extensions_p (parser
)
22238 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22239 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22242 /* Warn the user that they are using an extension. */
22243 pedwarn (loc
, OPT_Wpedantic
,
22244 "ISO C++ does not allow GNU designated initializers");
22245 /* Consume the identifier. */
22246 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22247 /* Consume the `:'. */
22248 cp_lexer_consume_token (parser
->lexer
);
22250 /* Also handle C99 array designators, '[ const ] ='. */
22251 else if (cp_parser_allow_gnu_extensions_p (parser
)
22252 && !c_dialect_objc ()
22253 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22255 /* In C++11, [ could start a lambda-introducer. */
22256 bool non_const
= false;
22258 cp_parser_parse_tentatively (parser
);
22260 if (!cp_parser_array_designator_p (parser
))
22262 cp_parser_simulate_error (parser
);
22263 designator
= NULL_TREE
;
22267 designator
= cp_parser_constant_expression (parser
, true,
22269 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22270 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22273 if (!cp_parser_parse_definitely (parser
))
22274 designator
= NULL_TREE
;
22276 && (!require_potential_rvalue_constant_expression
22278 designator
= NULL_TREE
;
22280 /* Warn the user that they are using an extension. */
22281 pedwarn (loc
, OPT_Wpedantic
,
22282 "ISO C++ does not allow C99 designated initializers");
22285 designator
= NULL_TREE
;
22289 first_designator
= designator
;
22292 else if (cxx_dialect
>= cxx2a
22293 && first_designator
!= error_mark_node
22294 && (!first_designator
!= !designator
))
22296 error_at (loc
, "either all initializer clauses should be designated "
22297 "or none of them should be");
22298 first_designator
= error_mark_node
;
22300 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22301 first_designator
= designator
;
22303 /* Parse the initializer. */
22304 initializer
= cp_parser_initializer_clause (parser
,
22305 &clause_non_constant_p
);
22306 /* If any clause is non-constant, so is the entire initializer. */
22307 if (clause_non_constant_p
)
22308 *non_constant_p
= true;
22310 /* If we have an ellipsis, this is an initializer pack
22312 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22314 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22316 /* Consume the `...'. */
22317 cp_lexer_consume_token (parser
->lexer
);
22319 if (designator
&& cxx_dialect
>= cxx2a
)
22321 "%<...%> not allowed in designated initializer list");
22323 /* Turn the initializer into an initializer expansion. */
22324 initializer
= make_pack_expansion (initializer
);
22327 /* Add it to the vector. */
22328 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22330 /* If the next token is not a comma, we have reached the end of
22332 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22335 /* Peek at the next token. */
22336 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22337 /* If the next token is a `}', then we're still done. An
22338 initializer-clause can have a trailing `,' after the
22339 initializer-list and before the closing `}'. */
22340 if (token
->type
== CPP_CLOSE_BRACE
)
22343 /* Consume the `,' token. */
22344 cp_lexer_consume_token (parser
->lexer
);
22347 /* The same identifier shall not appear in multiple designators
22348 of a designated-initializer-list. */
22349 if (first_designator
)
22352 tree designator
, val
;
22353 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22354 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22356 if (IDENTIFIER_MARKED (designator
))
22358 error_at (EXPR_LOC_OR_LOC (val
, input_location
),
22359 "%<.%s%> designator used multiple times in "
22360 "the same initializer list",
22361 IDENTIFIER_POINTER (designator
));
22362 (*v
)[i
].index
= NULL_TREE
;
22365 IDENTIFIER_MARKED (designator
) = 1;
22367 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22368 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22369 IDENTIFIER_MARKED (designator
) = 0;
22375 /* Classes [gram.class] */
22377 /* Parse a class-name.
22383 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22384 to indicate that names looked up in dependent types should be
22385 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22386 keyword has been used to indicate that the name that appears next
22387 is a template. TAG_TYPE indicates the explicit tag given before
22388 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22389 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22390 is the class being defined in a class-head. If ENUM_OK is TRUE,
22391 enum-names are also accepted.
22393 Returns the TYPE_DECL representing the class. */
22396 cp_parser_class_name (cp_parser
*parser
,
22397 bool typename_keyword_p
,
22398 bool template_keyword_p
,
22399 enum tag_types tag_type
,
22400 bool check_dependency_p
,
22402 bool is_declaration
,
22409 tree identifier
= NULL_TREE
;
22411 /* All class-names start with an identifier. */
22412 token
= cp_lexer_peek_token (parser
->lexer
);
22413 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22415 cp_parser_error (parser
, "expected class-name");
22416 return error_mark_node
;
22419 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22420 to a template-id, so we save it here. */
22421 scope
= parser
->scope
;
22422 if (scope
== error_mark_node
)
22423 return error_mark_node
;
22425 /* Any name names a type if we're following the `typename' keyword
22426 in a qualified name where the enclosing scope is type-dependent. */
22427 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22428 && dependent_type_p (scope
));
22429 /* Handle the common case (an identifier, but not a template-id)
22431 if (token
->type
== CPP_NAME
22432 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22434 cp_token
*identifier_token
;
22437 /* Look for the identifier. */
22438 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22439 ambiguous_p
= identifier_token
->error_reported
;
22440 identifier
= cp_parser_identifier (parser
);
22441 /* If the next token isn't an identifier, we are certainly not
22442 looking at a class-name. */
22443 if (identifier
== error_mark_node
)
22444 decl
= error_mark_node
;
22445 /* If we know this is a type-name, there's no need to look it
22447 else if (typename_p
)
22451 tree ambiguous_decls
;
22452 /* If we already know that this lookup is ambiguous, then
22453 we've already issued an error message; there's no reason
22457 cp_parser_simulate_error (parser
);
22458 return error_mark_node
;
22460 /* If the next token is a `::', then the name must be a type
22463 [basic.lookup.qual]
22465 During the lookup for a name preceding the :: scope
22466 resolution operator, object, function, and enumerator
22467 names are ignored. */
22468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22469 tag_type
= scope_type
;
22470 /* Look up the name. */
22471 decl
= cp_parser_lookup_name (parser
, identifier
,
22473 /*is_template=*/false,
22474 /*is_namespace=*/false,
22475 check_dependency_p
,
22477 identifier_token
->location
);
22478 if (ambiguous_decls
)
22480 if (cp_parser_parsing_tentatively (parser
))
22481 cp_parser_simulate_error (parser
);
22482 return error_mark_node
;
22488 /* Try a template-id. */
22489 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22490 check_dependency_p
,
22493 if (decl
== error_mark_node
)
22494 return error_mark_node
;
22497 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22499 /* If this is a typename, create a TYPENAME_TYPE. */
22500 if (typename_p
&& decl
!= error_mark_node
)
22502 decl
= make_typename_type (scope
, decl
, typename_type
,
22503 /*complain=*/tf_error
);
22504 if (decl
!= error_mark_node
)
22505 decl
= TYPE_NAME (decl
);
22508 decl
= strip_using_decl (decl
);
22510 /* Check to see that it is really the name of a class. */
22511 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22512 && identifier_p (TREE_OPERAND (decl
, 0))
22513 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22514 /* Situations like this:
22516 template <typename T> struct A {
22517 typename T::template X<int>::I i;
22520 are problematic. Is `T::template X<int>' a class-name? The
22521 standard does not seem to be definitive, but there is no other
22522 valid interpretation of the following `::'. Therefore, those
22523 names are considered class-names. */
22525 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22526 if (decl
!= error_mark_node
)
22527 decl
= TYPE_NAME (decl
);
22529 else if (TREE_CODE (decl
) != TYPE_DECL
22530 || TREE_TYPE (decl
) == error_mark_node
22531 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22532 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22533 /* In Objective-C 2.0, a classname followed by '.' starts a
22534 dot-syntax expression, and it's not a type-name. */
22535 || (c_dialect_objc ()
22536 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22537 && objc_is_class_name (decl
)))
22538 decl
= error_mark_node
;
22540 if (decl
== error_mark_node
)
22541 cp_parser_error (parser
, "expected class-name");
22542 else if (identifier
&& !parser
->scope
)
22543 maybe_note_name_used_in_class (identifier
, decl
);
22548 /* Parse a class-specifier.
22551 class-head { member-specification [opt] }
22553 Returns the TREE_TYPE representing the class. */
22556 cp_parser_class_specifier_1 (cp_parser
* parser
)
22559 tree attributes
= NULL_TREE
;
22560 bool nested_name_specifier_p
;
22561 unsigned saved_num_template_parameter_lists
;
22562 bool saved_in_function_body
;
22563 unsigned char in_statement
;
22564 bool in_switch_statement_p
;
22565 bool saved_in_unbraced_linkage_specification_p
;
22566 tree old_scope
= NULL_TREE
;
22567 tree scope
= NULL_TREE
;
22568 cp_token
*closing_brace
;
22570 push_deferring_access_checks (dk_no_deferred
);
22572 /* Parse the class-head. */
22573 type
= cp_parser_class_head (parser
,
22574 &nested_name_specifier_p
);
22575 /* If the class-head was a semantic disaster, skip the entire body
22579 cp_parser_skip_to_end_of_block_or_statement (parser
);
22580 pop_deferring_access_checks ();
22581 return error_mark_node
;
22584 /* Look for the `{'. */
22585 matching_braces braces
;
22586 if (!braces
.require_open (parser
))
22588 pop_deferring_access_checks ();
22589 return error_mark_node
;
22592 cp_ensure_no_omp_declare_simd (parser
);
22593 cp_ensure_no_oacc_routine (parser
);
22595 /* Issue an error message if type-definitions are forbidden here. */
22596 cp_parser_check_type_definition (parser
);
22597 /* Remember that we are defining one more class. */
22598 ++parser
->num_classes_being_defined
;
22599 /* Inside the class, surrounding template-parameter-lists do not
22601 saved_num_template_parameter_lists
22602 = parser
->num_template_parameter_lists
;
22603 parser
->num_template_parameter_lists
= 0;
22604 /* We are not in a function body. */
22605 saved_in_function_body
= parser
->in_function_body
;
22606 parser
->in_function_body
= false;
22607 /* Or in a loop. */
22608 in_statement
= parser
->in_statement
;
22609 parser
->in_statement
= 0;
22610 /* Or in a switch. */
22611 in_switch_statement_p
= parser
->in_switch_statement_p
;
22612 parser
->in_switch_statement_p
= false;
22613 /* We are not immediately inside an extern "lang" block. */
22614 saved_in_unbraced_linkage_specification_p
22615 = parser
->in_unbraced_linkage_specification_p
;
22616 parser
->in_unbraced_linkage_specification_p
= false;
22618 // Associate constraints with the type.
22620 type
= associate_classtype_constraints (type
);
22622 /* Start the class. */
22623 if (nested_name_specifier_p
)
22625 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22626 old_scope
= push_inner_scope (scope
);
22628 type
= begin_class_definition (type
);
22630 if (type
== error_mark_node
)
22631 /* If the type is erroneous, skip the entire body of the class. */
22632 cp_parser_skip_to_closing_brace (parser
);
22634 /* Parse the member-specification. */
22635 cp_parser_member_specification_opt (parser
);
22637 /* Look for the trailing `}'. */
22638 closing_brace
= braces
.require_close (parser
);
22639 /* Look for trailing attributes to apply to this class. */
22640 if (cp_parser_allow_gnu_extensions_p (parser
))
22641 attributes
= cp_parser_gnu_attributes_opt (parser
);
22642 if (type
!= error_mark_node
)
22643 type
= finish_struct (type
, attributes
);
22644 if (nested_name_specifier_p
)
22645 pop_inner_scope (old_scope
, scope
);
22647 /* We've finished a type definition. Check for the common syntax
22648 error of forgetting a semicolon after the definition. We need to
22649 be careful, as we can't just check for not-a-semicolon and be done
22650 with it; the user might have typed:
22652 class X { } c = ...;
22653 class X { } *p = ...;
22655 and so forth. Instead, enumerate all the possible tokens that
22656 might follow this production; if we don't see one of them, then
22657 complain and silently insert the semicolon. */
22659 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22660 bool want_semicolon
= true;
22662 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22663 /* Don't try to parse c++11 attributes here. As per the
22664 grammar, that should be a task for
22665 cp_parser_decl_specifier_seq. */
22666 want_semicolon
= false;
22668 switch (token
->type
)
22671 case CPP_SEMICOLON
:
22674 case CPP_OPEN_PAREN
:
22675 case CPP_CLOSE_PAREN
:
22677 want_semicolon
= false;
22680 /* While it's legal for type qualifiers and storage class
22681 specifiers to follow type definitions in the grammar, only
22682 compiler testsuites contain code like that. Assume that if
22683 we see such code, then what we're really seeing is a case
22687 const <type> var = ...;
22692 static <type> func (...) ...
22694 i.e. the qualifier or specifier applies to the next
22695 declaration. To do so, however, we need to look ahead one
22696 more token to see if *that* token is a type specifier.
22698 This code could be improved to handle:
22701 static const <type> var = ...; */
22703 if (keyword_is_decl_specifier (token
->keyword
))
22705 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22707 /* Handling user-defined types here would be nice, but very
22710 = (lookahead
->type
== CPP_KEYWORD
22711 && keyword_begins_type_specifier (lookahead
->keyword
));
22718 /* If we don't have a type, then something is very wrong and we
22719 shouldn't try to do anything clever. Likewise for not seeing the
22721 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22723 /* Locate the closing brace. */
22724 cp_token_position prev
22725 = cp_lexer_previous_token_position (parser
->lexer
);
22726 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22727 location_t loc
= prev_token
->location
;
22729 /* We want to suggest insertion of a ';' immediately *after* the
22730 closing brace, so, if we can, offset the location by 1 column. */
22731 location_t next_loc
= loc
;
22732 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22733 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22735 rich_location
richloc (line_table
, next_loc
);
22737 /* If we successfully offset the location, suggest the fix-it. */
22738 if (next_loc
!= loc
)
22739 richloc
.add_fixit_insert_before (next_loc
, ";");
22741 if (CLASSTYPE_DECLARED_CLASS (type
))
22742 error_at (&richloc
,
22743 "expected %<;%> after class definition");
22744 else if (TREE_CODE (type
) == RECORD_TYPE
)
22745 error_at (&richloc
,
22746 "expected %<;%> after struct definition");
22747 else if (TREE_CODE (type
) == UNION_TYPE
)
22748 error_at (&richloc
,
22749 "expected %<;%> after union definition");
22751 gcc_unreachable ();
22753 /* Unget one token and smash it to look as though we encountered
22754 a semicolon in the input stream. */
22755 cp_lexer_set_token_position (parser
->lexer
, prev
);
22756 token
= cp_lexer_peek_token (parser
->lexer
);
22757 token
->type
= CPP_SEMICOLON
;
22758 token
->keyword
= RID_MAX
;
22762 /* If this class is not itself within the scope of another class,
22763 then we need to parse the bodies of all of the queued function
22764 definitions. Note that the queued functions defined in a class
22765 are not always processed immediately following the
22766 class-specifier for that class. Consider:
22769 struct B { void f() { sizeof (A); } };
22772 If `f' were processed before the processing of `A' were
22773 completed, there would be no way to compute the size of `A'.
22774 Note that the nesting we are interested in here is lexical --
22775 not the semantic nesting given by TYPE_CONTEXT. In particular,
22778 struct A { struct B; };
22779 struct A::B { void f() { } };
22781 there is no need to delay the parsing of `A::B::f'. */
22782 if (--parser
->num_classes_being_defined
== 0)
22785 tree class_type
= NULL_TREE
;
22786 tree pushed_scope
= NULL_TREE
;
22788 cp_default_arg_entry
*e
;
22789 tree save_ccp
, save_ccr
;
22791 if (any_erroneous_template_args_p (type
))
22793 /* Skip default arguments, NSDMIs, etc, in order to improve
22794 error recovery (c++/71169, c++/71832). */
22795 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22796 vec_safe_truncate (unparsed_nsdmis
, 0);
22797 vec_safe_truncate (unparsed_classes
, 0);
22798 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22801 /* In a first pass, parse default arguments to the functions.
22802 Then, in a second pass, parse the bodies of the functions.
22803 This two-phased approach handles cases like:
22811 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22814 /* If there are default arguments that have not yet been processed,
22815 take care of them now. */
22816 if (class_type
!= e
->class_type
)
22819 pop_scope (pushed_scope
);
22820 class_type
= e
->class_type
;
22821 pushed_scope
= push_scope (class_type
);
22823 /* Make sure that any template parameters are in scope. */
22824 maybe_begin_member_template_processing (decl
);
22825 /* Parse the default argument expressions. */
22826 cp_parser_late_parsing_default_args (parser
, decl
);
22827 /* Remove any template parameters from the symbol table. */
22828 maybe_end_member_template_processing ();
22830 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22831 /* Now parse any NSDMIs. */
22832 save_ccp
= current_class_ptr
;
22833 save_ccr
= current_class_ref
;
22834 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22836 if (class_type
!= DECL_CONTEXT (decl
))
22839 pop_scope (pushed_scope
);
22840 class_type
= DECL_CONTEXT (decl
);
22841 pushed_scope
= push_scope (class_type
);
22843 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22844 cp_parser_late_parsing_nsdmi (parser
, decl
);
22846 vec_safe_truncate (unparsed_nsdmis
, 0);
22847 current_class_ptr
= save_ccp
;
22848 current_class_ref
= save_ccr
;
22850 pop_scope (pushed_scope
);
22852 /* Now do some post-NSDMI bookkeeping. */
22853 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22854 after_nsdmi_defaulted_late_checks (class_type
);
22855 vec_safe_truncate (unparsed_classes
, 0);
22856 after_nsdmi_defaulted_late_checks (type
);
22858 /* Now parse the body of the functions. */
22861 /* OpenMP UDRs need to be parsed before all other functions. */
22862 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22863 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22864 cp_parser_late_parsing_for_member (parser
, decl
);
22865 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22866 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22867 cp_parser_late_parsing_for_member (parser
, decl
);
22870 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22871 cp_parser_late_parsing_for_member (parser
, decl
);
22872 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22875 vec_safe_push (unparsed_classes
, type
);
22877 /* Put back any saved access checks. */
22878 pop_deferring_access_checks ();
22880 /* Restore saved state. */
22881 parser
->in_switch_statement_p
= in_switch_statement_p
;
22882 parser
->in_statement
= in_statement
;
22883 parser
->in_function_body
= saved_in_function_body
;
22884 parser
->num_template_parameter_lists
22885 = saved_num_template_parameter_lists
;
22886 parser
->in_unbraced_linkage_specification_p
22887 = saved_in_unbraced_linkage_specification_p
;
22893 cp_parser_class_specifier (cp_parser
* parser
)
22896 timevar_push (TV_PARSE_STRUCT
);
22897 ret
= cp_parser_class_specifier_1 (parser
);
22898 timevar_pop (TV_PARSE_STRUCT
);
22902 /* Parse a class-head.
22905 class-key identifier [opt] base-clause [opt]
22906 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22907 class-key nested-name-specifier [opt] template-id
22910 class-virt-specifier:
22914 class-key attributes identifier [opt] base-clause [opt]
22915 class-key attributes nested-name-specifier identifier base-clause [opt]
22916 class-key attributes nested-name-specifier [opt] template-id
22919 Upon return BASES is initialized to the list of base classes (or
22920 NULL, if there are none) in the same form returned by
22921 cp_parser_base_clause.
22923 Returns the TYPE of the indicated class. Sets
22924 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22925 involving a nested-name-specifier was used, and FALSE otherwise.
22927 Returns error_mark_node if this is not a class-head.
22929 Returns NULL_TREE if the class-head is syntactically valid, but
22930 semantically invalid in a way that means we should skip the entire
22931 body of the class. */
22934 cp_parser_class_head (cp_parser
* parser
,
22935 bool* nested_name_specifier_p
)
22937 tree nested_name_specifier
;
22938 enum tag_types class_key
;
22939 tree id
= NULL_TREE
;
22940 tree type
= NULL_TREE
;
22943 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22944 bool template_id_p
= false;
22945 bool qualified_p
= false;
22946 bool invalid_nested_name_p
= false;
22947 bool invalid_explicit_specialization_p
= false;
22948 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22949 tree pushed_scope
= NULL_TREE
;
22950 unsigned num_templates
;
22951 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22952 /* Assume no nested-name-specifier will be present. */
22953 *nested_name_specifier_p
= false;
22954 /* Assume no template parameter lists will be used in defining the
22957 parser
->colon_corrects_to_scope_p
= false;
22959 /* Look for the class-key. */
22960 class_key
= cp_parser_class_key (parser
);
22961 if (class_key
== none_type
)
22962 return error_mark_node
;
22964 location_t class_head_start_location
= input_location
;
22966 /* Parse the attributes. */
22967 attributes
= cp_parser_attributes_opt (parser
);
22969 /* If the next token is `::', that is invalid -- but sometimes
22970 people do try to write:
22974 Handle this gracefully by accepting the extra qualifier, and then
22975 issuing an error about it later if this really is a
22976 class-head. If it turns out just to be an elaborated type
22977 specifier, remain silent. */
22978 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22979 qualified_p
= true;
22981 push_deferring_access_checks (dk_no_check
);
22983 /* Determine the name of the class. Begin by looking for an
22984 optional nested-name-specifier. */
22985 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22986 nested_name_specifier
22987 = cp_parser_nested_name_specifier_opt (parser
,
22988 /*typename_keyword_p=*/false,
22989 /*check_dependency_p=*/false,
22991 /*is_declaration=*/false);
22992 /* If there was a nested-name-specifier, then there *must* be an
22995 cp_token
*bad_template_keyword
= NULL
;
22997 if (nested_name_specifier
)
22999 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23000 /* Although the grammar says `identifier', it really means
23001 `class-name' or `template-name'. You are only allowed to
23002 define a class that has already been declared with this
23005 The proposed resolution for Core Issue 180 says that wherever
23006 you see `class T::X' you should treat `X' as a type-name.
23008 It is OK to define an inaccessible class; for example:
23010 class A { class B; };
23013 We do not know if we will see a class-name, or a
23014 template-name. We look for a class-name first, in case the
23015 class-name is a template-id; if we looked for the
23016 template-name first we would stop after the template-name. */
23017 cp_parser_parse_tentatively (parser
);
23018 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23019 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23020 type
= cp_parser_class_name (parser
,
23021 /*typename_keyword_p=*/false,
23022 /*template_keyword_p=*/false,
23024 /*check_dependency_p=*/false,
23025 /*class_head_p=*/true,
23026 /*is_declaration=*/false);
23027 /* If that didn't work, ignore the nested-name-specifier. */
23028 if (!cp_parser_parse_definitely (parser
))
23030 invalid_nested_name_p
= true;
23031 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23032 id
= cp_parser_identifier (parser
);
23033 if (id
== error_mark_node
)
23036 /* If we could not find a corresponding TYPE, treat this
23037 declaration like an unqualified declaration. */
23038 if (type
== error_mark_node
)
23039 nested_name_specifier
= NULL_TREE
;
23040 /* Otherwise, count the number of templates used in TYPE and its
23041 containing scopes. */
23043 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23045 /* Otherwise, the identifier is optional. */
23048 /* We don't know whether what comes next is a template-id,
23049 an identifier, or nothing at all. */
23050 cp_parser_parse_tentatively (parser
);
23051 /* Check for a template-id. */
23052 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23053 id
= cp_parser_template_id (parser
,
23054 /*template_keyword_p=*/false,
23055 /*check_dependency_p=*/true,
23057 /*is_declaration=*/true);
23058 /* If that didn't work, it could still be an identifier. */
23059 if (!cp_parser_parse_definitely (parser
))
23061 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23063 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23064 id
= cp_parser_identifier (parser
);
23071 template_id_p
= true;
23076 pop_deferring_access_checks ();
23080 cp_parser_check_for_invalid_template_id (parser
, id
,
23082 type_start_token
->location
);
23084 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23086 /* If it's not a `:' or a `{' then we can't really be looking at a
23087 class-head, since a class-head only appears as part of a
23088 class-specifier. We have to detect this situation before calling
23089 xref_tag, since that has irreversible side-effects. */
23090 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23092 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23093 type
= error_mark_node
;
23097 /* At this point, we're going ahead with the class-specifier, even
23098 if some other problem occurs. */
23099 cp_parser_commit_to_tentative_parse (parser
);
23100 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23102 cp_parser_error (parser
,
23103 "cannot specify %<override%> for a class");
23104 type
= error_mark_node
;
23107 /* Issue the error about the overly-qualified name now. */
23110 cp_parser_error (parser
,
23111 "global qualification of class name is invalid");
23112 type
= error_mark_node
;
23115 else if (invalid_nested_name_p
)
23117 cp_parser_error (parser
,
23118 "qualified name does not name a class");
23119 type
= error_mark_node
;
23122 else if (nested_name_specifier
)
23126 if (bad_template_keyword
)
23127 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23128 keyword template shall not appear at the top level. */
23129 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23130 "keyword %<template%> not allowed in class-head-name");
23132 /* Reject typedef-names in class heads. */
23133 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23135 error_at (type_start_token
->location
,
23136 "invalid class name in declaration of %qD",
23142 /* Figure out in what scope the declaration is being placed. */
23143 scope
= current_scope ();
23144 /* If that scope does not contain the scope in which the
23145 class was originally declared, the program is invalid. */
23146 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23148 if (at_namespace_scope_p ())
23149 error_at (type_start_token
->location
,
23150 "declaration of %qD in namespace %qD which does not "
23152 type
, scope
, nested_name_specifier
);
23154 error_at (type_start_token
->location
,
23155 "declaration of %qD in %qD which does not enclose %qD",
23156 type
, scope
, nested_name_specifier
);
23162 A declarator-id shall not be qualified except for the
23163 definition of a ... nested class outside of its class
23164 ... [or] the definition or explicit instantiation of a
23165 class member of a namespace outside of its namespace. */
23166 if (scope
== nested_name_specifier
)
23168 permerror (nested_name_specifier_token_start
->location
,
23169 "extra qualification not allowed");
23170 nested_name_specifier
= NULL_TREE
;
23174 /* An explicit-specialization must be preceded by "template <>". If
23175 it is not, try to recover gracefully. */
23176 if (at_namespace_scope_p ()
23177 && parser
->num_template_parameter_lists
== 0
23178 && !processing_template_parmlist
23181 /* Build a location of this form:
23182 struct typename <ARGS>
23183 ^~~~~~~~~~~~~~~~~~~~~~
23184 with caret==start at the start token, and
23185 finishing at the end of the type. */
23186 location_t reported_loc
23187 = make_location (class_head_start_location
,
23188 class_head_start_location
,
23189 get_finish (type_start_token
->location
));
23190 rich_location
richloc (line_table
, reported_loc
);
23191 richloc
.add_fixit_insert_before (class_head_start_location
,
23193 error_at (&richloc
,
23194 "an explicit specialization must be preceded by"
23195 " %<template <>%>");
23196 invalid_explicit_specialization_p
= true;
23197 /* Take the same action that would have been taken by
23198 cp_parser_explicit_specialization. */
23199 ++parser
->num_template_parameter_lists
;
23200 begin_specialization ();
23202 /* There must be no "return" statements between this point and the
23203 end of this function; set "type "to the correct return value and
23204 use "goto done;" to return. */
23205 /* Make sure that the right number of template parameters were
23207 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23209 type_start_token
->location
,
23210 /*declarator=*/NULL
))
23212 /* If something went wrong, there is no point in even trying to
23213 process the class-definition. */
23218 /* Look up the type. */
23221 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23222 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23223 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23225 error_at (type_start_token
->location
,
23226 "function template %qD redeclared as a class template", id
);
23227 type
= error_mark_node
;
23231 type
= TREE_TYPE (id
);
23232 type
= maybe_process_partial_specialization (type
);
23234 /* Check the scope while we still know whether or not we had a
23235 nested-name-specifier. */
23236 if (type
!= error_mark_node
)
23237 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23239 if (nested_name_specifier
)
23240 pushed_scope
= push_scope (nested_name_specifier
);
23242 else if (nested_name_specifier
)
23248 template <typename T> struct S { struct T };
23249 template <typename T> struct S<T>::T { };
23251 we will get a TYPENAME_TYPE when processing the definition of
23252 `S::T'. We need to resolve it to the actual type before we
23253 try to define it. */
23254 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23256 class_type
= resolve_typename_type (TREE_TYPE (type
),
23257 /*only_current_p=*/false);
23258 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23259 type
= TYPE_NAME (class_type
);
23262 cp_parser_error (parser
, "could not resolve typename type");
23263 type
= error_mark_node
;
23267 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23268 == error_mark_node
)
23274 class_type
= current_class_type
;
23275 /* Enter the scope indicated by the nested-name-specifier. */
23276 pushed_scope
= push_scope (nested_name_specifier
);
23277 /* Get the canonical version of this type. */
23278 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23279 /* Call push_template_decl if it seems like we should be defining a
23280 template either from the template headers or the type we're
23281 defining, so that we diagnose both extra and missing headers. */
23282 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23283 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23284 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23286 type
= push_template_decl (type
);
23287 if (type
== error_mark_node
)
23294 type
= TREE_TYPE (type
);
23295 *nested_name_specifier_p
= true;
23297 else /* The name is not a nested name. */
23299 /* If the class was unnamed, create a dummy name. */
23301 id
= make_anon_name ();
23302 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23303 ? ts_within_enclosing_non_class
23305 type
= xref_tag (class_key
, id
, tag_scope
,
23306 parser
->num_template_parameter_lists
);
23309 /* Indicate whether this class was declared as a `class' or as a
23311 if (TREE_CODE (type
) == RECORD_TYPE
)
23312 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23313 cp_parser_check_class_key (class_key
, type
);
23315 /* If this type was already complete, and we see another definition,
23316 that's an error. */
23317 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23319 error_at (type_start_token
->location
, "redefinition of %q#T",
23321 inform (location_of (type
), "previous definition of %q#T",
23326 else if (type
== error_mark_node
)
23331 /* Apply attributes now, before any use of the class as a template
23332 argument in its base list. */
23333 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23334 fixup_attribute_variants (type
);
23337 /* We will have entered the scope containing the class; the names of
23338 base classes should be looked up in that context. For example:
23340 struct A { struct B {}; struct C; };
23341 struct A::C : B {};
23345 /* Get the list of base-classes, if there is one. */
23346 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23348 /* PR59482: enter the class scope so that base-specifiers are looked
23352 bases
= cp_parser_base_clause (parser
);
23353 /* PR59482: get out of the previously pushed class scope so that the
23354 subsequent pops pop the right thing. */
23361 /* If we're really defining a class, process the base classes.
23362 If they're invalid, fail. */
23363 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23364 xref_basetypes (type
, bases
);
23367 /* Leave the scope given by the nested-name-specifier. We will
23368 enter the class scope itself while processing the members. */
23370 pop_scope (pushed_scope
);
23372 if (invalid_explicit_specialization_p
)
23374 end_specialization ();
23375 --parser
->num_template_parameter_lists
;
23379 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23380 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23381 CLASSTYPE_FINAL (type
) = 1;
23383 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23387 /* Parse a class-key.
23394 Returns the kind of class-key specified, or none_type to indicate
23397 static enum tag_types
23398 cp_parser_class_key (cp_parser
* parser
)
23401 enum tag_types tag_type
;
23403 /* Look for the class-key. */
23404 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23408 /* Check to see if the TOKEN is a class-key. */
23409 tag_type
= cp_parser_token_is_class_key (token
);
23411 cp_parser_error (parser
, "expected class-key");
23415 /* Parse a type-parameter-key.
23417 type-parameter-key:
23423 cp_parser_type_parameter_key (cp_parser
* parser
)
23425 /* Look for the type-parameter-key. */
23426 enum tag_types tag_type
= none_type
;
23427 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23428 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23430 cp_lexer_consume_token (parser
->lexer
);
23431 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23432 /* typename is not allowed in a template template parameter
23433 by the standard until C++17. */
23434 pedwarn (token
->location
, OPT_Wpedantic
,
23435 "ISO C++ forbids typename key in template template parameter;"
23436 " use -std=c++17 or -std=gnu++17");
23439 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23444 /* Parse an (optional) member-specification.
23446 member-specification:
23447 member-declaration member-specification [opt]
23448 access-specifier : member-specification [opt] */
23451 cp_parser_member_specification_opt (cp_parser
* parser
)
23458 /* Peek at the next token. */
23459 token
= cp_lexer_peek_token (parser
->lexer
);
23460 /* If it's a `}', or EOF then we've seen all the members. */
23461 if (token
->type
== CPP_CLOSE_BRACE
23462 || token
->type
== CPP_EOF
23463 || token
->type
== CPP_PRAGMA_EOL
)
23466 /* See if this token is a keyword. */
23467 keyword
= token
->keyword
;
23471 case RID_PROTECTED
:
23473 /* Consume the access-specifier. */
23474 cp_lexer_consume_token (parser
->lexer
);
23475 /* Remember which access-specifier is active. */
23476 current_access_specifier
= token
->u
.value
;
23477 /* Look for the `:'. */
23478 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23482 /* Accept #pragmas at class scope. */
23483 if (token
->type
== CPP_PRAGMA
)
23485 cp_parser_pragma (parser
, pragma_member
, NULL
);
23489 /* Otherwise, the next construction must be a
23490 member-declaration. */
23491 cp_parser_member_declaration (parser
);
23496 /* Parse a member-declaration.
23498 member-declaration:
23499 decl-specifier-seq [opt] member-declarator-list [opt] ;
23500 function-definition ; [opt]
23501 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23503 template-declaration
23506 member-declarator-list:
23508 member-declarator-list , member-declarator
23511 declarator pure-specifier [opt]
23512 declarator constant-initializer [opt]
23513 identifier [opt] : constant-expression
23517 member-declaration:
23518 __extension__ member-declaration
23521 declarator attributes [opt] pure-specifier [opt]
23522 declarator attributes [opt] constant-initializer [opt]
23523 identifier [opt] attributes [opt] : constant-expression
23527 member-declaration:
23528 static_assert-declaration */
23531 cp_parser_member_declaration (cp_parser
* parser
)
23533 cp_decl_specifier_seq decl_specifiers
;
23534 tree prefix_attributes
;
23536 int declares_class_or_enum
;
23538 cp_token
*token
= NULL
;
23539 cp_token
*decl_spec_token_start
= NULL
;
23540 cp_token
*initializer_token_start
= NULL
;
23541 int saved_pedantic
;
23542 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23544 /* Check for the `__extension__' keyword. */
23545 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23548 cp_parser_member_declaration (parser
);
23549 /* Restore the old value of the PEDANTIC flag. */
23550 pedantic
= saved_pedantic
;
23555 /* Check for a template-declaration. */
23556 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23558 /* An explicit specialization here is an error condition, and we
23559 expect the specialization handler to detect and report this. */
23560 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23561 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23562 cp_parser_explicit_specialization (parser
);
23564 cp_parser_template_declaration (parser
, /*member_p=*/true);
23568 /* Check for a template introduction. */
23569 else if (cp_parser_template_declaration_after_export (parser
, true))
23572 /* Check for a using-declaration. */
23573 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23575 if (cxx_dialect
< cxx11
)
23577 /* Parse the using-declaration. */
23578 cp_parser_using_declaration (parser
,
23579 /*access_declaration_p=*/false);
23585 bool alias_decl_expected
;
23586 cp_parser_parse_tentatively (parser
);
23587 decl
= cp_parser_alias_declaration (parser
);
23588 /* Note that if we actually see the '=' token after the
23589 identifier, cp_parser_alias_declaration commits the
23590 tentative parse. In that case, we really expect an
23591 alias-declaration. Otherwise, we expect a using
23593 alias_decl_expected
=
23594 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23595 cp_parser_parse_definitely (parser
);
23597 if (alias_decl_expected
)
23598 finish_member_declaration (decl
);
23600 cp_parser_using_declaration (parser
,
23601 /*access_declaration_p=*/false);
23606 /* Check for @defs. */
23607 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23610 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23611 ivar
= ivar_chains
;
23615 ivar
= TREE_CHAIN (member
);
23616 TREE_CHAIN (member
) = NULL_TREE
;
23617 finish_member_declaration (member
);
23622 /* If the next token is `static_assert' we have a static assertion. */
23623 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23625 cp_parser_static_assert (parser
, /*member_p=*/true);
23629 parser
->colon_corrects_to_scope_p
= false;
23631 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23634 /* Parse the decl-specifier-seq. */
23635 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23636 cp_parser_decl_specifier_seq (parser
,
23637 CP_PARSER_FLAGS_OPTIONAL
,
23639 &declares_class_or_enum
);
23640 /* Check for an invalid type-name. */
23641 if (!decl_specifiers
.any_type_specifiers_p
23642 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23644 /* If there is no declarator, then the decl-specifier-seq should
23646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23648 /* If there was no decl-specifier-seq, and the next token is a
23649 `;', then we have something like:
23655 Each member-declaration shall declare at least one member
23656 name of the class. */
23657 if (!decl_specifiers
.any_specifiers_p
)
23659 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23660 if (!in_system_header_at (token
->location
))
23662 gcc_rich_location
richloc (token
->location
);
23663 richloc
.add_fixit_remove ();
23664 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23671 /* See if this declaration is a friend. */
23672 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23673 /* If there were decl-specifiers, check to see if there was
23674 a class-declaration. */
23675 type
= check_tag_decl (&decl_specifiers
,
23676 /*explicit_type_instantiation_p=*/false);
23677 /* Nested classes have already been added to the class, but
23678 a `friend' needs to be explicitly registered. */
23681 /* If the `friend' keyword was present, the friend must
23682 be introduced with a class-key. */
23683 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23684 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23685 "in C++03 a class-key must be used "
23686 "when declaring a friend");
23689 template <typename T> struct A {
23690 friend struct A<T>::B;
23693 A<T>::B will be represented by a TYPENAME_TYPE, and
23694 therefore not recognized by check_tag_decl. */
23697 type
= decl_specifiers
.type
;
23698 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23699 type
= TREE_TYPE (type
);
23701 if (!type
|| !TYPE_P (type
))
23702 error_at (decl_spec_token_start
->location
,
23703 "friend declaration does not name a class or "
23706 make_friend_class (current_class_type
, type
,
23707 /*complain=*/true);
23709 /* If there is no TYPE, an error message will already have
23711 else if (!type
|| type
== error_mark_node
)
23713 /* An anonymous aggregate has to be handled specially; such
23714 a declaration really declares a data member (with a
23715 particular type), as opposed to a nested class. */
23716 else if (ANON_AGGR_TYPE_P (type
))
23719 if (decl_specifiers
.storage_class
!= sc_none
)
23720 error_at (decl_spec_token_start
->location
,
23721 "a storage class on an anonymous aggregate "
23722 "in class scope is not allowed");
23724 /* Remove constructors and such from TYPE, now that we
23725 know it is an anonymous aggregate. */
23726 fixup_anonymous_aggr (type
);
23727 /* And make the corresponding data member. */
23728 decl
= build_decl (decl_spec_token_start
->location
,
23729 FIELD_DECL
, NULL_TREE
, type
);
23730 /* Add it to the class. */
23731 finish_member_declaration (decl
);
23734 cp_parser_check_access_in_redeclaration
23736 decl_spec_token_start
->location
);
23741 bool assume_semicolon
= false;
23743 /* Clear attributes from the decl_specifiers but keep them
23744 around as prefix attributes that apply them to the entity
23746 prefix_attributes
= decl_specifiers
.attributes
;
23747 decl_specifiers
.attributes
= NULL_TREE
;
23749 /* See if these declarations will be friends. */
23750 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23752 /* Keep going until we hit the `;' at the end of the
23754 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23756 tree attributes
= NULL_TREE
;
23757 tree first_attribute
;
23759 bool named_bitfld
= false;
23761 /* Peek at the next token. */
23762 token
= cp_lexer_peek_token (parser
->lexer
);
23764 /* The following code wants to know early if it is a bit-field
23765 or some other declaration. Attributes can appear before
23766 the `:' token. Skip over them without consuming any tokens
23767 to peek if they are followed by `:'. */
23768 if (cp_next_tokens_can_be_attribute_p (parser
)
23769 || (token
->type
== CPP_NAME
23770 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23771 && (named_bitfld
= true)))
23774 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23775 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23778 /* Check for a bitfield declaration. */
23779 if (token
->type
== CPP_COLON
23780 || (token
->type
== CPP_NAME
23781 && token
== cp_lexer_peek_token (parser
->lexer
)
23782 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23783 && (named_bitfld
= true)))
23787 tree late_attributes
= NULL_TREE
;
23790 identifier
= cp_parser_identifier (parser
);
23792 identifier
= NULL_TREE
;
23794 /* Look for attributes that apply to the bitfield. */
23795 attributes
= cp_parser_attributes_opt (parser
);
23797 /* Consume the `:' token. */
23798 cp_lexer_consume_token (parser
->lexer
);
23800 /* Get the width of the bitfield. */
23801 width
= cp_parser_constant_expression (parser
, false, NULL
,
23802 cxx_dialect
>= cxx11
);
23804 /* In C++2A and as extension for C++11 and above we allow
23805 default member initializers for bit-fields. */
23806 initializer
= NULL_TREE
;
23807 if (cxx_dialect
>= cxx11
23808 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23809 || cp_lexer_next_token_is (parser
->lexer
,
23813 = cp_lexer_peek_token (parser
->lexer
)->location
;
23814 if (cxx_dialect
< cxx2a
23815 && !in_system_header_at (loc
)
23816 && identifier
!= NULL_TREE
)
23818 "default member initializers for bit-fields "
23819 "only available with -std=c++2a or "
23822 initializer
= cp_parser_save_nsdmi (parser
);
23823 if (identifier
== NULL_TREE
)
23825 error_at (loc
, "default member initializer for "
23826 "unnamed bit-field");
23827 initializer
= NULL_TREE
;
23832 /* Look for attributes that apply to the bitfield after
23833 the `:' token and width. This is where GCC used to
23834 parse attributes in the past, pedwarn if there is
23835 a std attribute. */
23836 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23837 pedwarn (input_location
, OPT_Wpedantic
,
23838 "ISO C++ allows bit-field attributes only "
23839 "before the %<:%> token");
23841 late_attributes
= cp_parser_attributes_opt (parser
);
23844 attributes
= attr_chainon (attributes
, late_attributes
);
23846 /* Remember which attributes are prefix attributes and
23848 first_attribute
= attributes
;
23849 /* Combine the attributes. */
23850 attributes
= attr_chainon (prefix_attributes
, attributes
);
23852 /* Create the bitfield declaration. */
23853 decl
= grokbitfield (identifier
23854 ? make_id_declarator (NULL_TREE
,
23859 width
, initializer
,
23864 cp_declarator
*declarator
;
23865 tree asm_specification
;
23866 int ctor_dtor_or_conv_p
;
23868 /* Parse the declarator. */
23870 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23871 &ctor_dtor_or_conv_p
,
23872 /*parenthesized_p=*/NULL
,
23876 /* If something went wrong parsing the declarator, make sure
23877 that we at least consume some tokens. */
23878 if (declarator
== cp_error_declarator
)
23880 /* Skip to the end of the statement. */
23881 cp_parser_skip_to_end_of_statement (parser
);
23882 /* If the next token is not a semicolon, that is
23883 probably because we just skipped over the body of
23884 a function. So, we consume a semicolon if
23885 present, but do not issue an error message if it
23887 if (cp_lexer_next_token_is (parser
->lexer
,
23889 cp_lexer_consume_token (parser
->lexer
);
23893 if (declares_class_or_enum
& 2)
23894 cp_parser_check_for_definition_in_return_type
23895 (declarator
, decl_specifiers
.type
,
23896 decl_specifiers
.locations
[ds_type_spec
]);
23898 /* Look for an asm-specification. */
23899 asm_specification
= cp_parser_asm_specification_opt (parser
);
23900 /* Look for attributes that apply to the declaration. */
23901 attributes
= cp_parser_attributes_opt (parser
);
23902 /* Remember which attributes are prefix attributes and
23904 first_attribute
= attributes
;
23905 /* Combine the attributes. */
23906 attributes
= attr_chainon (prefix_attributes
, attributes
);
23908 /* If it's an `=', then we have a constant-initializer or a
23909 pure-specifier. It is not correct to parse the
23910 initializer before registering the member declaration
23911 since the member declaration should be in scope while
23912 its initializer is processed. However, the rest of the
23913 front end does not yet provide an interface that allows
23914 us to handle this correctly. */
23915 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23919 A pure-specifier shall be used only in the declaration of
23920 a virtual function.
23922 A member-declarator can contain a constant-initializer
23923 only if it declares a static member of integral or
23926 Therefore, if the DECLARATOR is for a function, we look
23927 for a pure-specifier; otherwise, we look for a
23928 constant-initializer. When we call `grokfield', it will
23929 perform more stringent semantics checks. */
23930 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23931 if (function_declarator_p (declarator
)
23932 || (decl_specifiers
.type
23933 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23934 && declarator
->kind
== cdk_id
23935 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23936 == FUNCTION_TYPE
)))
23937 initializer
= cp_parser_pure_specifier (parser
);
23938 else if (decl_specifiers
.storage_class
!= sc_static
)
23939 initializer
= cp_parser_save_nsdmi (parser
);
23940 else if (cxx_dialect
>= cxx11
)
23943 /* Don't require a constant rvalue in C++11, since we
23944 might want a reference constant. We'll enforce
23945 constancy later. */
23946 cp_lexer_consume_token (parser
->lexer
);
23947 /* Parse the initializer. */
23948 initializer
= cp_parser_initializer_clause (parser
,
23952 /* Parse the initializer. */
23953 initializer
= cp_parser_constant_initializer (parser
);
23955 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23956 && !function_declarator_p (declarator
))
23959 if (decl_specifiers
.storage_class
!= sc_static
)
23960 initializer
= cp_parser_save_nsdmi (parser
);
23962 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23964 /* Otherwise, there is no initializer. */
23966 initializer
= NULL_TREE
;
23968 /* See if we are probably looking at a function
23969 definition. We are certainly not looking at a
23970 member-declarator. Calling `grokfield' has
23971 side-effects, so we must not do it unless we are sure
23972 that we are looking at a member-declarator. */
23973 if (cp_parser_token_starts_function_definition_p
23974 (cp_lexer_peek_token (parser
->lexer
)))
23976 /* The grammar does not allow a pure-specifier to be
23977 used when a member function is defined. (It is
23978 possible that this fact is an oversight in the
23979 standard, since a pure function may be defined
23980 outside of the class-specifier. */
23981 if (initializer
&& initializer_token_start
)
23982 error_at (initializer_token_start
->location
,
23983 "pure-specifier on function-definition");
23984 decl
= cp_parser_save_member_function_body (parser
,
23988 if (parser
->fully_implicit_function_template_p
)
23989 decl
= finish_fully_implicit_template (parser
, decl
);
23990 /* If the member was not a friend, declare it here. */
23992 finish_member_declaration (decl
);
23993 /* Peek at the next token. */
23994 token
= cp_lexer_peek_token (parser
->lexer
);
23995 /* If the next token is a semicolon, consume it. */
23996 if (token
->type
== CPP_SEMICOLON
)
23998 location_t semicolon_loc
23999 = cp_lexer_consume_token (parser
->lexer
)->location
;
24000 gcc_rich_location
richloc (semicolon_loc
);
24001 richloc
.add_fixit_remove ();
24002 warning_at (&richloc
, OPT_Wextra_semi
,
24003 "extra %<;%> after in-class "
24004 "function definition");
24009 if (declarator
->kind
== cdk_function
)
24010 declarator
->id_loc
= token
->location
;
24011 /* Create the declaration. */
24012 decl
= grokfield (declarator
, &decl_specifiers
,
24013 initializer
, /*init_const_expr_p=*/true,
24014 asm_specification
, attributes
);
24015 if (parser
->fully_implicit_function_template_p
)
24018 finish_fully_implicit_template (parser
, 0);
24020 decl
= finish_fully_implicit_template (parser
, decl
);
24024 cp_finalize_omp_declare_simd (parser
, decl
);
24025 cp_finalize_oacc_routine (parser
, decl
, false);
24027 /* Reset PREFIX_ATTRIBUTES. */
24028 if (attributes
!= error_mark_node
)
24030 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24031 attributes
= TREE_CHAIN (attributes
);
24033 TREE_CHAIN (attributes
) = NULL_TREE
;
24036 /* If there is any qualification still in effect, clear it
24037 now; we will be starting fresh with the next declarator. */
24038 parser
->scope
= NULL_TREE
;
24039 parser
->qualifying_scope
= NULL_TREE
;
24040 parser
->object_scope
= NULL_TREE
;
24041 /* If it's a `,', then there are more declarators. */
24042 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24044 cp_lexer_consume_token (parser
->lexer
);
24045 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24047 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24048 gcc_rich_location
richloc (token
->location
);
24049 richloc
.add_fixit_remove ();
24050 error_at (&richloc
, "stray %<,%> at end of "
24051 "member declaration");
24054 /* If the next token isn't a `;', then we have a parse error. */
24055 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24058 /* The next token might be a ways away from where the
24059 actual semicolon is missing. Find the previous token
24060 and use that for our error position. */
24061 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24062 gcc_rich_location
richloc (token
->location
);
24063 richloc
.add_fixit_insert_after (";");
24064 error_at (&richloc
, "expected %<;%> at end of "
24065 "member declaration");
24067 /* Assume that the user meant to provide a semicolon. If
24068 we were to cp_parser_skip_to_end_of_statement, we might
24069 skip to a semicolon inside a member function definition
24070 and issue nonsensical error messages. */
24071 assume_semicolon
= true;
24076 /* Add DECL to the list of members. */
24078 /* Explicitly include, eg, NSDMIs, for better error
24079 recovery (c++/58650). */
24080 || !DECL_DECLARES_FUNCTION_P (decl
))
24081 finish_member_declaration (decl
);
24083 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24084 cp_parser_save_default_args (parser
, decl
);
24085 else if (TREE_CODE (decl
) == FIELD_DECL
24086 && DECL_INITIAL (decl
))
24087 /* Add DECL to the queue of NSDMI to be parsed later. */
24088 vec_safe_push (unparsed_nsdmis
, decl
);
24091 if (assume_semicolon
)
24096 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24098 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24101 /* Parse a pure-specifier.
24106 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24107 Otherwise, ERROR_MARK_NODE is returned. */
24110 cp_parser_pure_specifier (cp_parser
* parser
)
24114 /* Look for the `=' token. */
24115 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24116 return error_mark_node
;
24117 /* Look for the `0' token. */
24118 token
= cp_lexer_peek_token (parser
->lexer
);
24120 if (token
->type
== CPP_EOF
24121 || token
->type
== CPP_PRAGMA_EOL
)
24122 return error_mark_node
;
24124 cp_lexer_consume_token (parser
->lexer
);
24126 /* Accept = default or = delete in c++0x mode. */
24127 if (token
->keyword
== RID_DEFAULT
24128 || token
->keyword
== RID_DELETE
)
24130 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24131 return token
->u
.value
;
24134 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24135 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24137 cp_parser_error (parser
,
24138 "invalid pure specifier (only %<= 0%> is allowed)");
24139 cp_parser_skip_to_end_of_statement (parser
);
24140 return error_mark_node
;
24142 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24144 error_at (token
->location
, "templates may not be %<virtual%>");
24145 return error_mark_node
;
24148 return integer_zero_node
;
24151 /* Parse a constant-initializer.
24153 constant-initializer:
24154 = constant-expression
24156 Returns a representation of the constant-expression. */
24159 cp_parser_constant_initializer (cp_parser
* parser
)
24161 /* Look for the `=' token. */
24162 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24163 return error_mark_node
;
24165 /* It is invalid to write:
24167 struct S { static const int i = { 7 }; };
24170 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24172 cp_parser_error (parser
,
24173 "a brace-enclosed initializer is not allowed here");
24174 /* Consume the opening brace. */
24175 matching_braces braces
;
24176 braces
.consume_open (parser
);
24177 /* Skip the initializer. */
24178 cp_parser_skip_to_closing_brace (parser
);
24179 /* Look for the trailing `}'. */
24180 braces
.require_close (parser
);
24182 return error_mark_node
;
24185 return cp_parser_constant_expression (parser
);
24188 /* Derived classes [gram.class.derived] */
24190 /* Parse a base-clause.
24193 : base-specifier-list
24195 base-specifier-list:
24196 base-specifier ... [opt]
24197 base-specifier-list , base-specifier ... [opt]
24199 Returns a TREE_LIST representing the base-classes, in the order in
24200 which they were declared. The representation of each node is as
24201 described by cp_parser_base_specifier.
24203 In the case that no bases are specified, this function will return
24204 NULL_TREE, not ERROR_MARK_NODE. */
24207 cp_parser_base_clause (cp_parser
* parser
)
24209 tree bases
= NULL_TREE
;
24211 /* Look for the `:' that begins the list. */
24212 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24214 /* Scan the base-specifier-list. */
24219 bool pack_expansion_p
= false;
24221 /* Look for the base-specifier. */
24222 base
= cp_parser_base_specifier (parser
);
24223 /* Look for the (optional) ellipsis. */
24224 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24226 /* Consume the `...'. */
24227 cp_lexer_consume_token (parser
->lexer
);
24229 pack_expansion_p
= true;
24232 /* Add BASE to the front of the list. */
24233 if (base
&& base
!= error_mark_node
)
24235 if (pack_expansion_p
)
24236 /* Make this a pack expansion type. */
24237 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24239 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24241 TREE_CHAIN (base
) = bases
;
24245 /* Peek at the next token. */
24246 token
= cp_lexer_peek_token (parser
->lexer
);
24247 /* If it's not a comma, then the list is complete. */
24248 if (token
->type
!= CPP_COMMA
)
24250 /* Consume the `,'. */
24251 cp_lexer_consume_token (parser
->lexer
);
24254 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24255 base class had a qualified name. However, the next name that
24256 appears is certainly not qualified. */
24257 parser
->scope
= NULL_TREE
;
24258 parser
->qualifying_scope
= NULL_TREE
;
24259 parser
->object_scope
= NULL_TREE
;
24261 return nreverse (bases
);
24264 /* Parse a base-specifier.
24267 :: [opt] nested-name-specifier [opt] class-name
24268 virtual access-specifier [opt] :: [opt] nested-name-specifier
24270 access-specifier virtual [opt] :: [opt] nested-name-specifier
24273 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24274 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24275 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24276 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24279 cp_parser_base_specifier (cp_parser
* parser
)
24283 bool virtual_p
= false;
24284 bool duplicate_virtual_error_issued_p
= false;
24285 bool duplicate_access_error_issued_p
= false;
24286 bool class_scope_p
, template_p
;
24287 tree access
= access_default_node
;
24290 /* Process the optional `virtual' and `access-specifier'. */
24293 /* Peek at the next token. */
24294 token
= cp_lexer_peek_token (parser
->lexer
);
24295 /* Process `virtual'. */
24296 switch (token
->keyword
)
24299 /* If `virtual' appears more than once, issue an error. */
24300 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24302 cp_parser_error (parser
,
24303 "%<virtual%> specified more than once in base-specifier");
24304 duplicate_virtual_error_issued_p
= true;
24309 /* Consume the `virtual' token. */
24310 cp_lexer_consume_token (parser
->lexer
);
24315 case RID_PROTECTED
:
24317 /* If more than one access specifier appears, issue an
24319 if (access
!= access_default_node
24320 && !duplicate_access_error_issued_p
)
24322 cp_parser_error (parser
,
24323 "more than one access specifier in base-specifier");
24324 duplicate_access_error_issued_p
= true;
24327 access
= ridpointers
[(int) token
->keyword
];
24329 /* Consume the access-specifier. */
24330 cp_lexer_consume_token (parser
->lexer
);
24339 /* It is not uncommon to see programs mechanically, erroneously, use
24340 the 'typename' keyword to denote (dependent) qualified types
24341 as base classes. */
24342 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24344 token
= cp_lexer_peek_token (parser
->lexer
);
24345 if (!processing_template_decl
)
24346 error_at (token
->location
,
24347 "keyword %<typename%> not allowed outside of templates");
24349 error_at (token
->location
,
24350 "keyword %<typename%> not allowed in this context "
24351 "(the base class is implicitly a type)");
24352 cp_lexer_consume_token (parser
->lexer
);
24355 /* Look for the optional `::' operator. */
24356 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24357 /* Look for the nested-name-specifier. The simplest way to
24362 The keyword `typename' is not permitted in a base-specifier or
24363 mem-initializer; in these contexts a qualified name that
24364 depends on a template-parameter is implicitly assumed to be a
24367 is to pretend that we have seen the `typename' keyword at this
24369 cp_parser_nested_name_specifier_opt (parser
,
24370 /*typename_keyword_p=*/true,
24371 /*check_dependency_p=*/true,
24373 /*is_declaration=*/true);
24374 /* If the base class is given by a qualified name, assume that names
24375 we see are type names or templates, as appropriate. */
24376 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24377 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24380 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24381 /* DR 950 allows decltype as a base-specifier. */
24382 type
= cp_parser_decltype (parser
);
24385 /* Otherwise, look for the class-name. */
24386 type
= cp_parser_class_name (parser
,
24390 /*check_dependency_p=*/true,
24391 /*class_head_p=*/false,
24392 /*is_declaration=*/true);
24393 type
= TREE_TYPE (type
);
24396 if (type
== error_mark_node
)
24397 return error_mark_node
;
24399 return finish_base_specifier (type
, access
, virtual_p
);
24402 /* Exception handling [gram.exception] */
24404 /* Parse an (optional) noexcept-specification.
24406 noexcept-specification:
24407 noexcept ( constant-expression ) [opt]
24409 If no noexcept-specification is present, returns NULL_TREE.
24410 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24411 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24412 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24413 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24414 in which case a boolean condition is returned instead. */
24417 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24418 bool require_constexpr
,
24419 bool* consumed_expr
,
24423 const char *saved_message
;
24425 /* Peek at the next token. */
24426 token
= cp_lexer_peek_token (parser
->lexer
);
24428 /* Is it a noexcept-specification? */
24429 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24432 cp_lexer_consume_token (parser
->lexer
);
24434 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24436 matching_parens parens
;
24437 parens
.consume_open (parser
);
24439 if (require_constexpr
)
24441 /* Types may not be defined in an exception-specification. */
24442 saved_message
= parser
->type_definition_forbidden_message
;
24443 parser
->type_definition_forbidden_message
24444 = G_("types may not be defined in an exception-specification");
24446 expr
= cp_parser_constant_expression (parser
);
24448 /* Restore the saved message. */
24449 parser
->type_definition_forbidden_message
= saved_message
;
24453 expr
= cp_parser_expression (parser
);
24454 *consumed_expr
= true;
24457 parens
.require_close (parser
);
24461 expr
= boolean_true_node
;
24462 if (!require_constexpr
)
24463 *consumed_expr
= false;
24466 /* We cannot build a noexcept-spec right away because this will check
24467 that expr is a constexpr. */
24469 return build_noexcept_spec (expr
, tf_warning_or_error
);
24477 /* Parse an (optional) exception-specification.
24479 exception-specification:
24480 throw ( type-id-list [opt] )
24482 Returns a TREE_LIST representing the exception-specification. The
24483 TREE_VALUE of each node is a type. */
24486 cp_parser_exception_specification_opt (cp_parser
* parser
)
24490 const char *saved_message
;
24492 /* Peek at the next token. */
24493 token
= cp_lexer_peek_token (parser
->lexer
);
24495 /* Is it a noexcept-specification? */
24496 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24498 if (type_id_list
!= NULL_TREE
)
24499 return type_id_list
;
24501 /* If it's not `throw', then there's no exception-specification. */
24502 if (!cp_parser_is_keyword (token
, RID_THROW
))
24505 location_t loc
= token
->location
;
24507 /* Consume the `throw'. */
24508 cp_lexer_consume_token (parser
->lexer
);
24510 /* Look for the `('. */
24511 matching_parens parens
;
24512 parens
.require_open (parser
);
24514 /* Peek at the next token. */
24515 token
= cp_lexer_peek_token (parser
->lexer
);
24516 /* If it's not a `)', then there is a type-id-list. */
24517 if (token
->type
!= CPP_CLOSE_PAREN
)
24519 /* Types may not be defined in an exception-specification. */
24520 saved_message
= parser
->type_definition_forbidden_message
;
24521 parser
->type_definition_forbidden_message
24522 = G_("types may not be defined in an exception-specification");
24523 /* Parse the type-id-list. */
24524 type_id_list
= cp_parser_type_id_list (parser
);
24525 /* Restore the saved message. */
24526 parser
->type_definition_forbidden_message
= saved_message
;
24528 if (cxx_dialect
>= cxx17
)
24530 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24532 type_id_list
= NULL_TREE
;
24534 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24535 warning_at (loc
, OPT_Wdeprecated
,
24536 "dynamic exception specifications are deprecated in "
24539 /* In C++17, throw() is equivalent to noexcept (true). throw()
24540 is deprecated in C++11 and above as well, but is still widely used,
24541 so don't warn about it yet. */
24542 else if (cxx_dialect
>= cxx17
)
24543 type_id_list
= noexcept_true_spec
;
24545 type_id_list
= empty_except_spec
;
24547 /* Look for the `)'. */
24548 parens
.require_close (parser
);
24550 return type_id_list
;
24553 /* Parse an (optional) type-id-list.
24557 type-id-list , type-id ... [opt]
24559 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24560 in the order that the types were presented. */
24563 cp_parser_type_id_list (cp_parser
* parser
)
24565 tree types
= NULL_TREE
;
24572 token
= cp_lexer_peek_token (parser
->lexer
);
24574 /* Get the next type-id. */
24575 type
= cp_parser_type_id (parser
);
24576 /* Check for invalid 'auto'. */
24577 if (flag_concepts
&& type_uses_auto (type
))
24579 error_at (token
->location
,
24580 "invalid use of %<auto%> in exception-specification");
24581 type
= error_mark_node
;
24583 /* Parse the optional ellipsis. */
24584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24586 /* Consume the `...'. */
24587 cp_lexer_consume_token (parser
->lexer
);
24589 /* Turn the type into a pack expansion expression. */
24590 type
= make_pack_expansion (type
);
24592 /* Add it to the list. */
24593 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24594 /* Peek at the next token. */
24595 token
= cp_lexer_peek_token (parser
->lexer
);
24596 /* If it is not a `,', we are done. */
24597 if (token
->type
!= CPP_COMMA
)
24599 /* Consume the `,'. */
24600 cp_lexer_consume_token (parser
->lexer
);
24603 return nreverse (types
);
24606 /* Parse a try-block.
24609 try compound-statement handler-seq */
24612 cp_parser_try_block (cp_parser
* parser
)
24616 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24617 if (parser
->in_function_body
24618 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24619 error ("%<try%> in %<constexpr%> function");
24621 try_block
= begin_try_block ();
24622 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24623 finish_try_block (try_block
);
24624 cp_parser_handler_seq (parser
);
24625 finish_handler_sequence (try_block
);
24630 /* Parse a function-try-block.
24632 function-try-block:
24633 try ctor-initializer [opt] function-body handler-seq */
24636 cp_parser_function_try_block (cp_parser
* parser
)
24638 tree compound_stmt
;
24641 /* Look for the `try' keyword. */
24642 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24644 /* Let the rest of the front end know where we are. */
24645 try_block
= begin_function_try_block (&compound_stmt
);
24646 /* Parse the function-body. */
24647 cp_parser_ctor_initializer_opt_and_function_body
24648 (parser
, /*in_function_try_block=*/true);
24649 /* We're done with the `try' part. */
24650 finish_function_try_block (try_block
);
24651 /* Parse the handlers. */
24652 cp_parser_handler_seq (parser
);
24653 /* We're done with the handlers. */
24654 finish_function_handler_sequence (try_block
, compound_stmt
);
24657 /* Parse a handler-seq.
24660 handler handler-seq [opt] */
24663 cp_parser_handler_seq (cp_parser
* parser
)
24669 /* Parse the handler. */
24670 cp_parser_handler (parser
);
24671 /* Peek at the next token. */
24672 token
= cp_lexer_peek_token (parser
->lexer
);
24673 /* If it's not `catch' then there are no more handlers. */
24674 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24679 /* Parse a handler.
24682 catch ( exception-declaration ) compound-statement */
24685 cp_parser_handler (cp_parser
* parser
)
24690 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24691 handler
= begin_handler ();
24692 matching_parens parens
;
24693 parens
.require_open (parser
);
24694 declaration
= cp_parser_exception_declaration (parser
);
24695 finish_handler_parms (declaration
, handler
);
24696 parens
.require_close (parser
);
24697 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24698 finish_handler (handler
);
24701 /* Parse an exception-declaration.
24703 exception-declaration:
24704 type-specifier-seq declarator
24705 type-specifier-seq abstract-declarator
24709 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24710 ellipsis variant is used. */
24713 cp_parser_exception_declaration (cp_parser
* parser
)
24715 cp_decl_specifier_seq type_specifiers
;
24716 cp_declarator
*declarator
;
24717 const char *saved_message
;
24719 /* If it's an ellipsis, it's easy to handle. */
24720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24722 /* Consume the `...' token. */
24723 cp_lexer_consume_token (parser
->lexer
);
24727 /* Types may not be defined in exception-declarations. */
24728 saved_message
= parser
->type_definition_forbidden_message
;
24729 parser
->type_definition_forbidden_message
24730 = G_("types may not be defined in exception-declarations");
24732 /* Parse the type-specifier-seq. */
24733 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24734 /*is_trailing_return=*/false,
24736 /* If it's a `)', then there is no declarator. */
24737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24740 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24741 /*ctor_dtor_or_conv_p=*/NULL
,
24742 /*parenthesized_p=*/NULL
,
24743 /*member_p=*/false,
24744 /*friend_p=*/false);
24746 /* Restore the saved message. */
24747 parser
->type_definition_forbidden_message
= saved_message
;
24749 if (!type_specifiers
.any_specifiers_p
)
24750 return error_mark_node
;
24752 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24755 /* Parse a throw-expression.
24758 throw assignment-expression [opt]
24760 Returns a THROW_EXPR representing the throw-expression. */
24763 cp_parser_throw_expression (cp_parser
* parser
)
24768 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24769 token
= cp_lexer_peek_token (parser
->lexer
);
24770 /* Figure out whether or not there is an assignment-expression
24771 following the "throw" keyword. */
24772 if (token
->type
== CPP_COMMA
24773 || token
->type
== CPP_SEMICOLON
24774 || token
->type
== CPP_CLOSE_PAREN
24775 || token
->type
== CPP_CLOSE_SQUARE
24776 || token
->type
== CPP_CLOSE_BRACE
24777 || token
->type
== CPP_COLON
)
24778 expression
= NULL_TREE
;
24780 expression
= cp_parser_assignment_expression (parser
);
24782 return build_throw (expression
);
24785 /* GNU Extensions */
24787 /* Parse an (optional) asm-specification.
24790 asm ( string-literal )
24792 If the asm-specification is present, returns a STRING_CST
24793 corresponding to the string-literal. Otherwise, returns
24797 cp_parser_asm_specification_opt (cp_parser
* parser
)
24800 tree asm_specification
;
24802 /* Peek at the next token. */
24803 token
= cp_lexer_peek_token (parser
->lexer
);
24804 /* If the next token isn't the `asm' keyword, then there's no
24805 asm-specification. */
24806 if (!cp_parser_is_keyword (token
, RID_ASM
))
24809 /* Consume the `asm' token. */
24810 cp_lexer_consume_token (parser
->lexer
);
24811 /* Look for the `('. */
24812 matching_parens parens
;
24813 parens
.require_open (parser
);
24815 /* Look for the string-literal. */
24816 asm_specification
= cp_parser_string_literal (parser
, false, false);
24818 /* Look for the `)'. */
24819 parens
.require_close (parser
);
24821 return asm_specification
;
24824 /* Parse an asm-operand-list.
24828 asm-operand-list , asm-operand
24831 string-literal ( expression )
24832 [ string-literal ] string-literal ( expression )
24834 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24835 each node is the expression. The TREE_PURPOSE is itself a
24836 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24837 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24838 is a STRING_CST for the string literal before the parenthesis. Returns
24839 ERROR_MARK_NODE if any of the operands are invalid. */
24842 cp_parser_asm_operand_list (cp_parser
* parser
)
24844 tree asm_operands
= NULL_TREE
;
24845 bool invalid_operands
= false;
24849 tree string_literal
;
24853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24855 /* Consume the `[' token. */
24856 cp_lexer_consume_token (parser
->lexer
);
24857 /* Read the operand name. */
24858 name
= cp_parser_identifier (parser
);
24859 if (name
!= error_mark_node
)
24860 name
= build_string (IDENTIFIER_LENGTH (name
),
24861 IDENTIFIER_POINTER (name
));
24862 /* Look for the closing `]'. */
24863 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24867 /* Look for the string-literal. */
24868 string_literal
= cp_parser_string_literal (parser
, false, false);
24870 /* Look for the `('. */
24871 matching_parens parens
;
24872 parens
.require_open (parser
);
24873 /* Parse the expression. */
24874 expression
= cp_parser_expression (parser
);
24875 /* Look for the `)'. */
24876 parens
.require_close (parser
);
24878 if (name
== error_mark_node
24879 || string_literal
== error_mark_node
24880 || expression
== error_mark_node
)
24881 invalid_operands
= true;
24883 /* Add this operand to the list. */
24884 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24887 /* If the next token is not a `,', there are no more
24889 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24891 /* Consume the `,'. */
24892 cp_lexer_consume_token (parser
->lexer
);
24895 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24898 /* Parse an asm-clobber-list.
24902 asm-clobber-list , string-literal
24904 Returns a TREE_LIST, indicating the clobbers in the order that they
24905 appeared. The TREE_VALUE of each node is a STRING_CST. */
24908 cp_parser_asm_clobber_list (cp_parser
* parser
)
24910 tree clobbers
= NULL_TREE
;
24914 tree string_literal
;
24916 /* Look for the string literal. */
24917 string_literal
= cp_parser_string_literal (parser
, false, false);
24918 /* Add it to the list. */
24919 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24920 /* If the next token is not a `,', then the list is
24922 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24924 /* Consume the `,' token. */
24925 cp_lexer_consume_token (parser
->lexer
);
24931 /* Parse an asm-label-list.
24935 asm-label-list , identifier
24937 Returns a TREE_LIST, indicating the labels in the order that they
24938 appeared. The TREE_VALUE of each node is a label. */
24941 cp_parser_asm_label_list (cp_parser
* parser
)
24943 tree labels
= NULL_TREE
;
24947 tree identifier
, label
, name
;
24949 /* Look for the identifier. */
24950 identifier
= cp_parser_identifier (parser
);
24951 if (!error_operand_p (identifier
))
24953 label
= lookup_label (identifier
);
24954 if (TREE_CODE (label
) == LABEL_DECL
)
24956 TREE_USED (label
) = 1;
24957 check_goto (label
);
24958 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24959 IDENTIFIER_POINTER (identifier
));
24960 labels
= tree_cons (name
, label
, labels
);
24963 /* If the next token is not a `,', then the list is
24965 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24967 /* Consume the `,' token. */
24968 cp_lexer_consume_token (parser
->lexer
);
24971 return nreverse (labels
);
24974 /* Return TRUE iff the next tokens in the stream are possibly the
24975 beginning of a GNU extension attribute. */
24978 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24980 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24983 /* Return TRUE iff the next tokens in the stream are possibly the
24984 beginning of a standard C++-11 attribute specifier. */
24987 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24989 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24992 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24993 beginning of a standard C++-11 attribute specifier. */
24996 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24998 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25000 return (cxx_dialect
>= cxx11
25001 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25002 || (token
->type
== CPP_OPEN_SQUARE
25003 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25004 && token
->type
== CPP_OPEN_SQUARE
)));
25007 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25008 beginning of a GNU extension attribute. */
25011 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25013 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25015 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25018 /* Return true iff the next tokens can be the beginning of either a
25019 GNU attribute list, or a standard C++11 attribute sequence. */
25022 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25024 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25025 || cp_next_tokens_can_be_std_attribute_p (parser
));
25028 /* Return true iff the next Nth tokens can be the beginning of either
25029 a GNU attribute list, or a standard C++11 attribute sequence. */
25032 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25034 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25035 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25038 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25039 of GNU attributes, or return NULL. */
25042 cp_parser_attributes_opt (cp_parser
*parser
)
25044 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25045 return cp_parser_gnu_attributes_opt (parser
);
25046 return cp_parser_std_attribute_spec_seq (parser
);
25049 /* Parse an (optional) series of attributes.
25052 attributes attribute
25055 __attribute__ (( attribute-list [opt] ))
25057 The return value is as for cp_parser_gnu_attribute_list. */
25060 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25062 tree attributes
= NULL_TREE
;
25064 temp_override
<bool> cleanup
25065 (parser
->auto_is_implicit_function_template_parm_p
, false);
25070 tree attribute_list
;
25073 /* Peek at the next token. */
25074 token
= cp_lexer_peek_token (parser
->lexer
);
25075 /* If it's not `__attribute__', then we're done. */
25076 if (token
->keyword
!= RID_ATTRIBUTE
)
25079 /* Consume the `__attribute__' keyword. */
25080 cp_lexer_consume_token (parser
->lexer
);
25081 /* Look for the two `(' tokens. */
25082 matching_parens outer_parens
;
25083 outer_parens
.require_open (parser
);
25084 matching_parens inner_parens
;
25085 inner_parens
.require_open (parser
);
25087 /* Peek at the next token. */
25088 token
= cp_lexer_peek_token (parser
->lexer
);
25089 if (token
->type
!= CPP_CLOSE_PAREN
)
25090 /* Parse the attribute-list. */
25091 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25093 /* If the next token is a `)', then there is no attribute
25095 attribute_list
= NULL
;
25097 /* Look for the two `)' tokens. */
25098 if (!inner_parens
.require_close (parser
))
25100 if (!outer_parens
.require_close (parser
))
25103 cp_parser_skip_to_end_of_statement (parser
);
25105 /* Add these new attributes to the list. */
25106 attributes
= attr_chainon (attributes
, attribute_list
);
25112 /* Parse a GNU attribute-list.
25116 attribute-list , attribute
25120 identifier ( identifier )
25121 identifier ( identifier , expression-list )
25122 identifier ( expression-list )
25124 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25125 to an attribute. The TREE_PURPOSE of each node is the identifier
25126 indicating which attribute is in use. The TREE_VALUE represents
25127 the arguments, if any. */
25130 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25132 tree attribute_list
= NULL_TREE
;
25133 bool save_translate_strings_p
= parser
->translate_strings_p
;
25135 parser
->translate_strings_p
= false;
25142 /* Look for the identifier. We also allow keywords here; for
25143 example `__attribute__ ((const))' is legal. */
25144 token
= cp_lexer_peek_token (parser
->lexer
);
25145 if (token
->type
== CPP_NAME
25146 || token
->type
== CPP_KEYWORD
)
25148 tree arguments
= NULL_TREE
;
25150 /* Consume the token, but save it since we need it for the
25151 SIMD enabled function parsing. */
25152 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25154 /* Save away the identifier that indicates which attribute
25156 identifier
= (token
->type
== CPP_KEYWORD
)
25157 /* For keywords, use the canonical spelling, not the
25158 parsed identifier. */
25159 ? ridpointers
[(int) token
->keyword
]
25160 : id_token
->u
.value
;
25162 identifier
= canonicalize_attr_name (identifier
);
25163 attribute
= build_tree_list (identifier
, NULL_TREE
);
25165 /* Peek at the next token. */
25166 token
= cp_lexer_peek_token (parser
->lexer
);
25167 /* If it's an `(', then parse the attribute arguments. */
25168 if (token
->type
== CPP_OPEN_PAREN
)
25170 vec
<tree
, va_gc
> *vec
;
25171 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25172 ? id_attr
: normal_attr
);
25173 vec
= cp_parser_parenthesized_expression_list
25174 (parser
, attr_flag
, /*cast_p=*/false,
25175 /*allow_expansion_p=*/false,
25176 /*non_constant_p=*/NULL
);
25178 arguments
= error_mark_node
;
25181 arguments
= build_tree_list_vec (vec
);
25182 release_tree_vector (vec
);
25184 /* Save the arguments away. */
25185 TREE_VALUE (attribute
) = arguments
;
25188 if (arguments
!= error_mark_node
)
25190 /* Add this attribute to the list. */
25191 TREE_CHAIN (attribute
) = attribute_list
;
25192 attribute_list
= attribute
;
25195 token
= cp_lexer_peek_token (parser
->lexer
);
25197 /* Now, look for more attributes. If the next token isn't a
25198 `,', we're done. */
25199 if (token
->type
!= CPP_COMMA
)
25202 /* Consume the comma and keep going. */
25203 cp_lexer_consume_token (parser
->lexer
);
25205 parser
->translate_strings_p
= save_translate_strings_p
;
25207 /* We built up the list in reverse order. */
25208 return nreverse (attribute_list
);
25211 /* Parse a standard C++11 attribute.
25213 The returned representation is a TREE_LIST which TREE_PURPOSE is
25214 the scoped name of the attribute, and the TREE_VALUE is its
25217 Note that the scoped name of the attribute is itself a TREE_LIST
25218 which TREE_PURPOSE is the namespace of the attribute, and
25219 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25220 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25221 and which TREE_PURPOSE is directly the attribute name.
25223 Clients of the attribute code should use get_attribute_namespace
25224 and get_attribute_name to get the actual namespace and name of
25225 attributes, regardless of their being GNU or C++11 attributes.
25228 attribute-token attribute-argument-clause [opt]
25232 attribute-scoped-token
25234 attribute-scoped-token:
25235 attribute-namespace :: identifier
25237 attribute-namespace:
25240 attribute-argument-clause:
25241 ( balanced-token-seq )
25243 balanced-token-seq:
25244 balanced-token [opt]
25245 balanced-token-seq balanced-token
25248 ( balanced-token-seq )
25249 [ balanced-token-seq ]
25250 { balanced-token-seq }. */
25253 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25255 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25258 temp_override
<bool> cleanup
25259 (parser
->auto_is_implicit_function_template_parm_p
, false);
25261 /* First, parse name of the attribute, a.k.a attribute-token. */
25263 token
= cp_lexer_peek_token (parser
->lexer
);
25264 if (token
->type
== CPP_NAME
)
25265 attr_id
= token
->u
.value
;
25266 else if (token
->type
== CPP_KEYWORD
)
25267 attr_id
= ridpointers
[(int) token
->keyword
];
25268 else if (token
->flags
& NAMED_OP
)
25269 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25271 if (attr_id
== NULL_TREE
)
25274 cp_lexer_consume_token (parser
->lexer
);
25276 token
= cp_lexer_peek_token (parser
->lexer
);
25277 if (token
->type
== CPP_SCOPE
)
25279 /* We are seeing a scoped attribute token. */
25281 cp_lexer_consume_token (parser
->lexer
);
25283 error_at (token
->location
, "attribute using prefix used together "
25284 "with scoped attribute token");
25287 token
= cp_lexer_consume_token (parser
->lexer
);
25288 if (token
->type
== CPP_NAME
)
25289 attr_id
= token
->u
.value
;
25290 else if (token
->type
== CPP_KEYWORD
)
25291 attr_id
= ridpointers
[(int) token
->keyword
];
25292 else if (token
->flags
& NAMED_OP
)
25293 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25296 error_at (token
->location
,
25297 "expected an identifier for the attribute name");
25298 return error_mark_node
;
25301 attr_id
= canonicalize_attr_name (attr_id
);
25302 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25304 token
= cp_lexer_peek_token (parser
->lexer
);
25307 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25311 attr_id
= canonicalize_attr_name (attr_id
);
25312 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25314 /* C++11 noreturn attribute is equivalent to GNU's. */
25315 if (is_attribute_p ("noreturn", attr_id
))
25316 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25317 /* C++14 deprecated attribute is equivalent to GNU's. */
25318 else if (is_attribute_p ("deprecated", attr_id
))
25319 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25320 /* C++17 fallthrough attribute is equivalent to GNU's. */
25321 else if (is_attribute_p ("fallthrough", attr_id
))
25322 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25323 /* Transactional Memory TS optimize_for_synchronized attribute is
25324 equivalent to GNU transaction_callable. */
25325 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25326 TREE_PURPOSE (attribute
)
25327 = get_identifier ("transaction_callable");
25328 /* Transactional Memory attributes are GNU attributes. */
25329 else if (tm_attr_to_mask (attr_id
))
25330 TREE_PURPOSE (attribute
) = attr_id
;
25333 /* Now parse the optional argument clause of the attribute. */
25335 if (token
->type
!= CPP_OPEN_PAREN
)
25339 vec
<tree
, va_gc
> *vec
;
25340 int attr_flag
= normal_attr
;
25342 if (attr_ns
== get_identifier ("gnu")
25343 && attribute_takes_identifier_p (attr_id
))
25344 /* A GNU attribute that takes an identifier in parameter. */
25345 attr_flag
= id_attr
;
25347 vec
= cp_parser_parenthesized_expression_list
25348 (parser
, attr_flag
, /*cast_p=*/false,
25349 /*allow_expansion_p=*/true,
25350 /*non_constant_p=*/NULL
);
25352 arguments
= error_mark_node
;
25355 arguments
= build_tree_list_vec (vec
);
25356 release_tree_vector (vec
);
25359 if (arguments
== error_mark_node
)
25360 attribute
= error_mark_node
;
25362 TREE_VALUE (attribute
) = arguments
;
25368 /* Check that the attribute ATTRIBUTE appears at most once in the
25369 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25370 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25371 isn't implemented yet in GCC. */
25374 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25378 tree name
= get_attribute_name (attribute
);
25379 if (is_attribute_p ("noreturn", name
)
25380 && lookup_attribute ("noreturn", attributes
))
25381 error ("attribute %<noreturn%> can appear at most once "
25382 "in an attribute-list");
25383 else if (is_attribute_p ("deprecated", name
)
25384 && lookup_attribute ("deprecated", attributes
))
25385 error ("attribute %<deprecated%> can appear at most once "
25386 "in an attribute-list");
25390 /* Parse a list of standard C++-11 attributes.
25394 attribute-list , attribute[opt]
25396 attribute-list , attribute ...
25400 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25402 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25403 cp_token
*token
= NULL
;
25407 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25408 if (attribute
== error_mark_node
)
25410 if (attribute
!= NULL_TREE
)
25412 cp_parser_check_std_attribute (attributes
, attribute
);
25413 TREE_CHAIN (attribute
) = attributes
;
25414 attributes
= attribute
;
25416 token
= cp_lexer_peek_token (parser
->lexer
);
25417 if (token
->type
== CPP_ELLIPSIS
)
25419 cp_lexer_consume_token (parser
->lexer
);
25420 if (attribute
== NULL_TREE
)
25421 error_at (token
->location
,
25422 "expected attribute before %<...%>");
25425 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25426 if (pack
== error_mark_node
)
25427 return error_mark_node
;
25428 TREE_VALUE (attribute
) = pack
;
25430 token
= cp_lexer_peek_token (parser
->lexer
);
25432 if (token
->type
!= CPP_COMMA
)
25434 cp_lexer_consume_token (parser
->lexer
);
25436 attributes
= nreverse (attributes
);
25440 /* Parse a standard C++-11 attribute specifier.
25442 attribute-specifier:
25443 [ [ attribute-using-prefix [opt] attribute-list ] ]
25444 alignment-specifier
25446 attribute-using-prefix:
25447 using attribute-namespace :
25449 alignment-specifier:
25450 alignas ( type-id ... [opt] )
25451 alignas ( alignment-expression ... [opt] ). */
25454 cp_parser_std_attribute_spec (cp_parser
*parser
)
25456 tree attributes
= NULL_TREE
;
25457 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25459 if (token
->type
== CPP_OPEN_SQUARE
25460 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25462 tree attr_ns
= NULL_TREE
;
25464 cp_lexer_consume_token (parser
->lexer
);
25465 cp_lexer_consume_token (parser
->lexer
);
25467 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25469 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25470 if (token
->type
== CPP_NAME
)
25471 attr_ns
= token
->u
.value
;
25472 else if (token
->type
== CPP_KEYWORD
)
25473 attr_ns
= ridpointers
[(int) token
->keyword
];
25474 else if (token
->flags
& NAMED_OP
)
25475 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25478 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25480 if (cxx_dialect
< cxx17
25481 && !in_system_header_at (input_location
))
25482 pedwarn (input_location
, 0,
25483 "attribute using prefix only available "
25484 "with -std=c++17 or -std=gnu++17");
25486 cp_lexer_consume_token (parser
->lexer
);
25487 cp_lexer_consume_token (parser
->lexer
);
25488 cp_lexer_consume_token (parser
->lexer
);
25491 attr_ns
= NULL_TREE
;
25494 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25496 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25497 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25498 cp_parser_skip_to_end_of_statement (parser
);
25500 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25501 when we are sure that we have actually parsed them. */
25502 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25508 /* Look for an alignment-specifier. */
25510 token
= cp_lexer_peek_token (parser
->lexer
);
25512 if (token
->type
!= CPP_KEYWORD
25513 || token
->keyword
!= RID_ALIGNAS
)
25516 cp_lexer_consume_token (parser
->lexer
);
25517 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25519 matching_parens parens
;
25520 if (!parens
.require_open (parser
))
25521 return error_mark_node
;
25523 cp_parser_parse_tentatively (parser
);
25524 alignas_expr
= cp_parser_type_id (parser
);
25526 if (!cp_parser_parse_definitely (parser
))
25528 alignas_expr
= cp_parser_assignment_expression (parser
);
25529 if (alignas_expr
== error_mark_node
)
25530 cp_parser_skip_to_end_of_statement (parser
);
25531 if (alignas_expr
== NULL_TREE
25532 || alignas_expr
== error_mark_node
)
25533 return alignas_expr
;
25536 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25537 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25539 /* Handle alignas (pack...). */
25540 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25542 cp_lexer_consume_token (parser
->lexer
);
25543 alignas_expr
= make_pack_expansion (alignas_expr
);
25546 /* Something went wrong, so don't build the attribute. */
25547 if (alignas_expr
== error_mark_node
)
25548 return error_mark_node
;
25550 if (!parens
.require_close (parser
))
25551 return error_mark_node
;
25553 /* Build the C++-11 representation of an 'aligned'
25556 build_tree_list (build_tree_list (get_identifier ("gnu"),
25557 get_identifier ("aligned")),
25564 /* Parse a standard C++-11 attribute-specifier-seq.
25566 attribute-specifier-seq:
25567 attribute-specifier-seq [opt] attribute-specifier
25571 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25573 tree attr_specs
= NULL_TREE
;
25574 tree attr_last
= NULL_TREE
;
25578 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25579 if (attr_spec
== NULL_TREE
)
25581 if (attr_spec
== error_mark_node
)
25582 return error_mark_node
;
25585 TREE_CHAIN (attr_last
) = attr_spec
;
25587 attr_specs
= attr_last
= attr_spec
;
25588 attr_last
= tree_last (attr_last
);
25594 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25595 return index of the first token after balanced-token, or N on failure. */
25598 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25601 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25603 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25606 case CPP_PRAGMA_EOL
:
25607 /* Ran out of tokens. */
25609 case CPP_OPEN_PAREN
:
25612 case CPP_OPEN_BRACE
:
25615 case CPP_OPEN_SQUARE
:
25618 case CPP_CLOSE_PAREN
:
25621 case CPP_CLOSE_BRACE
:
25624 case CPP_CLOSE_SQUARE
:
25630 while (nparens
|| nbraces
|| nsquares
);
25634 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25635 return index of the first token after the GNU attribute tokens, or N on
25639 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25643 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25644 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25645 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25648 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25651 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25658 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25659 next token), return index of the first token after the standard C++11
25660 attribute tokens, or N on failure. */
25663 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25667 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25668 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25670 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25673 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25677 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25678 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25680 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25691 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25692 as the next token), return index of the first token after the attribute
25693 tokens, or N on failure. */
25696 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25698 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25699 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25700 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25703 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25704 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25705 current value of the PEDANTIC flag, regardless of whether or not
25706 the `__extension__' keyword is present. The caller is responsible
25707 for restoring the value of the PEDANTIC flag. */
25710 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25712 /* Save the old value of the PEDANTIC flag. */
25713 *saved_pedantic
= pedantic
;
25715 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25717 /* Consume the `__extension__' token. */
25718 cp_lexer_consume_token (parser
->lexer
);
25719 /* We're not being pedantic while the `__extension__' keyword is
25729 /* Parse a label declaration.
25732 __label__ label-declarator-seq ;
25734 label-declarator-seq:
25735 identifier , label-declarator-seq
25739 cp_parser_label_declaration (cp_parser
* parser
)
25741 /* Look for the `__label__' keyword. */
25742 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25748 /* Look for an identifier. */
25749 identifier
= cp_parser_identifier (parser
);
25750 /* If we failed, stop. */
25751 if (identifier
== error_mark_node
)
25753 /* Declare it as a label. */
25754 finish_label_decl (identifier
);
25755 /* If the next token is a `;', stop. */
25756 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25758 /* Look for the `,' separating the label declarations. */
25759 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25762 /* Look for the final `;'. */
25763 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25766 // -------------------------------------------------------------------------- //
25769 // Parse a requires clause.
25771 // requires-clause:
25772 // 'requires' logical-or-expression
25774 // The required logical-or-expression must be a constant expression. Note
25775 // that we don't check that the expression is constepxr here. We defer until
25776 // we analyze constraints and then, we only check atomic constraints.
25778 cp_parser_requires_clause (cp_parser
*parser
)
25780 // Parse the requires clause so that it is not automatically folded.
25781 ++processing_template_decl
;
25782 tree expr
= cp_parser_binary_expression (parser
, false, false,
25783 PREC_NOT_OPERATOR
, NULL
);
25784 if (check_for_bare_parameter_packs (expr
))
25785 expr
= error_mark_node
;
25786 --processing_template_decl
;
25790 // Optionally parse a requires clause:
25792 cp_parser_requires_clause_opt (cp_parser
*parser
)
25794 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25795 if (tok
->keyword
!= RID_REQUIRES
)
25797 if (!flag_concepts
&& tok
->type
== CPP_NAME
25798 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25800 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25801 "%<requires%> only available with -fconcepts");
25802 /* Parse and discard the requires-clause. */
25803 cp_lexer_consume_token (parser
->lexer
);
25804 cp_parser_requires_clause (parser
);
25808 cp_lexer_consume_token (parser
->lexer
);
25809 return cp_parser_requires_clause (parser
);
25813 /*---------------------------------------------------------------------------
25814 Requires expressions
25815 ---------------------------------------------------------------------------*/
25817 /* Parse a requires expression
25819 requirement-expression:
25820 'requires' requirement-parameter-list [opt] requirement-body */
25822 cp_parser_requires_expression (cp_parser
*parser
)
25824 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25825 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25827 /* A requires-expression shall appear only within a concept
25828 definition or a requires-clause.
25830 TODO: Implement this diagnostic correctly. */
25831 if (!processing_template_decl
)
25833 error_at (loc
, "a requires expression cannot appear outside a template");
25834 cp_parser_skip_to_end_of_statement (parser
);
25835 return error_mark_node
;
25840 /* Local parameters are delared as variables within the scope
25841 of the expression. They are not visible past the end of
25842 the expression. Expressions within the requires-expression
25843 are unevaluated. */
25844 struct scope_sentinel
25848 ++cp_unevaluated_operand
;
25849 begin_scope (sk_block
, NULL_TREE
);
25854 pop_bindings_and_leave_scope ();
25855 --cp_unevaluated_operand
;
25859 /* Parse the optional parameter list. */
25860 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25862 parms
= cp_parser_requirement_parameter_list (parser
);
25863 if (parms
== error_mark_node
)
25864 return error_mark_node
;
25869 /* Parse the requirement body. */
25870 reqs
= cp_parser_requirement_body (parser
);
25871 if (reqs
== error_mark_node
)
25872 return error_mark_node
;
25875 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25877 grokparms (parms
, &parms
);
25878 return finish_requires_expr (parms
, reqs
);
25881 /* Parse a parameterized requirement.
25883 requirement-parameter-list:
25884 '(' parameter-declaration-clause ')' */
25886 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25888 matching_parens parens
;
25889 if (!parens
.require_open (parser
))
25890 return error_mark_node
;
25892 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25894 if (!parens
.require_close (parser
))
25895 return error_mark_node
;
25900 /* Parse the body of a requirement.
25903 '{' requirement-list '}' */
25905 cp_parser_requirement_body (cp_parser
*parser
)
25907 matching_braces braces
;
25908 if (!braces
.require_open (parser
))
25909 return error_mark_node
;
25911 tree reqs
= cp_parser_requirement_list (parser
);
25913 if (!braces
.require_close (parser
))
25914 return error_mark_node
;
25919 /* Parse a list of requirements.
25923 requirement-list ';' requirement[opt] */
25925 cp_parser_requirement_list (cp_parser
*parser
)
25927 tree result
= NULL_TREE
;
25930 tree req
= cp_parser_requirement (parser
);
25931 if (req
== error_mark_node
)
25932 return error_mark_node
;
25934 result
= tree_cons (NULL_TREE
, req
, result
);
25936 /* If we see a semi-colon, consume it. */
25937 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25938 cp_lexer_consume_token (parser
->lexer
);
25940 /* Stop processing at the end of the list. */
25941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25945 /* Reverse the order of requirements so they are analyzed in
25946 declaration order. */
25947 return nreverse (result
);
25950 /* Parse a syntactic requirement or type requirement.
25954 compound-requirement
25956 nested-requirement */
25958 cp_parser_requirement (cp_parser
*parser
)
25960 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25961 return cp_parser_compound_requirement (parser
);
25962 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25963 return cp_parser_type_requirement (parser
);
25964 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25965 return cp_parser_nested_requirement (parser
);
25967 return cp_parser_simple_requirement (parser
);
25970 /* Parse a simple requirement.
25972 simple-requirement:
25975 cp_parser_simple_requirement (cp_parser
*parser
)
25977 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25978 if (!expr
|| expr
== error_mark_node
)
25979 return error_mark_node
;
25981 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25982 return error_mark_node
;
25984 return finish_simple_requirement (expr
);
25987 /* Parse a type requirement
25990 nested-name-specifier [opt] required-type-name ';'
25992 required-type-name:
25994 'template' [opt] simple-template-id */
25996 cp_parser_type_requirement (cp_parser
*parser
)
25998 cp_lexer_consume_token (parser
->lexer
);
26000 // Save the scope before parsing name specifiers.
26001 tree saved_scope
= parser
->scope
;
26002 tree saved_object_scope
= parser
->object_scope
;
26003 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26004 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26005 cp_parser_nested_name_specifier_opt (parser
,
26006 /*typename_keyword_p=*/true,
26007 /*check_dependency_p=*/false,
26009 /*is_declaration=*/false);
26012 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26014 cp_lexer_consume_token (parser
->lexer
);
26015 type
= cp_parser_template_id (parser
,
26016 /*template_keyword_p=*/true,
26017 /*check_dependency=*/false,
26018 /*tag_type=*/none_type
,
26019 /*is_declaration=*/false);
26020 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26021 /*complain=*/tf_error
);
26024 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26026 if (TREE_CODE (type
) == TYPE_DECL
)
26027 type
= TREE_TYPE (type
);
26029 parser
->scope
= saved_scope
;
26030 parser
->object_scope
= saved_object_scope
;
26031 parser
->qualifying_scope
= saved_qualifying_scope
;
26033 if (type
== error_mark_node
)
26034 cp_parser_skip_to_end_of_statement (parser
);
26036 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26037 return error_mark_node
;
26038 if (type
== error_mark_node
)
26039 return error_mark_node
;
26041 return finish_type_requirement (type
);
26044 /* Parse a compound requirement
26046 compound-requirement:
26047 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26049 cp_parser_compound_requirement (cp_parser
*parser
)
26051 /* Parse an expression enclosed in '{ }'s. */
26052 matching_braces braces
;
26053 if (!braces
.require_open (parser
))
26054 return error_mark_node
;
26056 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26057 if (!expr
|| expr
== error_mark_node
)
26058 return error_mark_node
;
26060 if (!braces
.require_close (parser
))
26061 return error_mark_node
;
26063 /* Parse the optional noexcept. */
26064 bool noexcept_p
= false;
26065 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26067 cp_lexer_consume_token (parser
->lexer
);
26071 /* Parse the optional trailing return type. */
26072 tree type
= NULL_TREE
;
26073 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26075 cp_lexer_consume_token (parser
->lexer
);
26076 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26077 parser
->in_result_type_constraint_p
= true;
26078 type
= cp_parser_trailing_type_id (parser
);
26079 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26080 if (type
== error_mark_node
)
26081 return error_mark_node
;
26084 return finish_compound_requirement (expr
, type
, noexcept_p
);
26087 /* Parse a nested requirement. This is the same as a requires clause.
26089 nested-requirement:
26092 cp_parser_nested_requirement (cp_parser
*parser
)
26094 cp_lexer_consume_token (parser
->lexer
);
26095 tree req
= cp_parser_requires_clause (parser
);
26096 if (req
== error_mark_node
)
26097 return error_mark_node
;
26098 return finish_nested_requirement (req
);
26101 /* Support Functions */
26103 /* Return the appropriate prefer_type argument for lookup_name_real based on
26104 tag_type and template_mem_access. */
26107 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26109 /* DR 141: When looking in the current enclosing context for a template-name
26110 after -> or ., only consider class templates. */
26111 if (template_mem_access
)
26115 case none_type
: return 0; // No preference.
26116 case scope_type
: return 1; // Type or namespace.
26117 default: return 2; // Type only.
26121 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26122 NAME should have one of the representations used for an
26123 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26124 is returned. If PARSER->SCOPE is a dependent type, then a
26125 SCOPE_REF is returned.
26127 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26128 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26129 was formed. Abstractly, such entities should not be passed to this
26130 function, because they do not need to be looked up, but it is
26131 simpler to check for this special case here, rather than at the
26134 In cases not explicitly covered above, this function returns a
26135 DECL, OVERLOAD, or baselink representing the result of the lookup.
26136 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26139 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26140 (e.g., "struct") that was used. In that case bindings that do not
26141 refer to types are ignored.
26143 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26146 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26149 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26152 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26153 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26154 NULL_TREE otherwise. */
26157 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26158 enum tag_types tag_type
,
26161 bool check_dependency
,
26162 tree
*ambiguous_decls
,
26163 location_t name_location
)
26166 tree object_type
= parser
->context
->object_type
;
26168 /* Assume that the lookup will be unambiguous. */
26169 if (ambiguous_decls
)
26170 *ambiguous_decls
= NULL_TREE
;
26172 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26173 no longer valid. Note that if we are parsing tentatively, and
26174 the parse fails, OBJECT_TYPE will be automatically restored. */
26175 parser
->context
->object_type
= NULL_TREE
;
26177 if (name
== error_mark_node
)
26178 return error_mark_node
;
26180 /* A template-id has already been resolved; there is no lookup to
26182 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26184 if (BASELINK_P (name
))
26186 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26187 == TEMPLATE_ID_EXPR
);
26191 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26192 it should already have been checked to make sure that the name
26193 used matches the type being destroyed. */
26194 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26198 /* Figure out to which type this destructor applies. */
26200 type
= parser
->scope
;
26201 else if (object_type
)
26202 type
= object_type
;
26204 type
= current_class_type
;
26205 /* If that's not a class type, there is no destructor. */
26206 if (!type
|| !CLASS_TYPE_P (type
))
26207 return error_mark_node
;
26209 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26210 lazily_declare_fn (sfk_destructor
, type
);
26212 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26215 return error_mark_node
;
26218 /* By this point, the NAME should be an ordinary identifier. If
26219 the id-expression was a qualified name, the qualifying scope is
26220 stored in PARSER->SCOPE at this point. */
26221 gcc_assert (identifier_p (name
));
26223 /* Perform the lookup. */
26228 if (parser
->scope
== error_mark_node
)
26229 return error_mark_node
;
26231 /* If the SCOPE is dependent, the lookup must be deferred until
26232 the template is instantiated -- unless we are explicitly
26233 looking up names in uninstantiated templates. Even then, we
26234 cannot look up the name if the scope is not a class type; it
26235 might, for example, be a template type parameter. */
26236 dependent_p
= (TYPE_P (parser
->scope
)
26237 && dependent_scope_p (parser
->scope
));
26238 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26240 /* Defer lookup. */
26241 decl
= error_mark_node
;
26244 tree pushed_scope
= NULL_TREE
;
26246 /* If PARSER->SCOPE is a dependent type, then it must be a
26247 class type, and we must not be checking dependencies;
26248 otherwise, we would have processed this lookup above. So
26249 that PARSER->SCOPE is not considered a dependent base by
26250 lookup_member, we must enter the scope here. */
26252 pushed_scope
= push_scope (parser
->scope
);
26254 /* If the PARSER->SCOPE is a template specialization, it
26255 may be instantiated during name lookup. In that case,
26256 errors may be issued. Even if we rollback the current
26257 tentative parse, those errors are valid. */
26258 decl
= lookup_qualified_name (parser
->scope
, name
,
26259 prefer_type_arg (tag_type
),
26260 /*complain=*/true);
26262 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26263 lookup result and the nested-name-specifier nominates a class C:
26264 * if the name specified after the nested-name-specifier, when
26265 looked up in C, is the injected-class-name of C (Clause 9), or
26266 * if the name specified after the nested-name-specifier is the
26267 same as the identifier or the simple-template-id's template-
26268 name in the last component of the nested-name-specifier,
26269 the name is instead considered to name the constructor of
26270 class C. [ Note: for example, the constructor is not an
26271 acceptable lookup result in an elaborated-type-specifier so
26272 the constructor would not be used in place of the
26273 injected-class-name. --end note ] Such a constructor name
26274 shall be used only in the declarator-id of a declaration that
26275 names a constructor or in a using-declaration. */
26276 if (tag_type
== none_type
26277 && DECL_SELF_REFERENCE_P (decl
)
26278 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26279 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26280 prefer_type_arg (tag_type
),
26281 /*complain=*/true);
26283 /* If we have a single function from a using decl, pull it out. */
26284 if (TREE_CODE (decl
) == OVERLOAD
26285 && !really_overloaded_fn (decl
))
26286 decl
= OVL_FUNCTION (decl
);
26289 pop_scope (pushed_scope
);
26292 /* If the scope is a dependent type and either we deferred lookup or
26293 we did lookup but didn't find the name, rememeber the name. */
26294 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26295 && dependent_type_p (parser
->scope
))
26301 /* The resolution to Core Issue 180 says that `struct
26302 A::B' should be considered a type-name, even if `A'
26304 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26305 /*complain=*/tf_error
);
26306 if (type
!= error_mark_node
)
26307 decl
= TYPE_NAME (type
);
26309 else if (is_template
26310 && (cp_parser_next_token_ends_template_argument_p (parser
)
26311 || cp_lexer_next_token_is (parser
->lexer
,
26313 decl
= make_unbound_class_template (parser
->scope
,
26315 /*complain=*/tf_error
);
26317 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26318 parser
->scope
, name
,
26321 parser
->qualifying_scope
= parser
->scope
;
26322 parser
->object_scope
= NULL_TREE
;
26324 else if (object_type
)
26326 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26327 OBJECT_TYPE is not a class. */
26328 if (CLASS_TYPE_P (object_type
))
26329 /* If the OBJECT_TYPE is a template specialization, it may
26330 be instantiated during name lookup. In that case, errors
26331 may be issued. Even if we rollback the current tentative
26332 parse, those errors are valid. */
26333 decl
= lookup_member (object_type
,
26336 prefer_type_arg (tag_type
),
26337 tf_warning_or_error
);
26342 /* Look it up in the enclosing context. DR 141: When looking for a
26343 template-name after -> or ., only consider class templates. */
26344 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26346 /*block_p=*/true, is_namespace
, 0);
26347 if (object_type
== unknown_type_node
)
26348 /* The object is type-dependent, so we can't look anything up; we used
26349 this to get the DR 141 behavior. */
26350 object_type
= NULL_TREE
;
26351 parser
->object_scope
= object_type
;
26352 parser
->qualifying_scope
= NULL_TREE
;
26356 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26358 /*block_p=*/true, is_namespace
, 0);
26359 parser
->qualifying_scope
= NULL_TREE
;
26360 parser
->object_scope
= NULL_TREE
;
26363 /* If the lookup failed, let our caller know. */
26364 if (!decl
|| decl
== error_mark_node
)
26365 return error_mark_node
;
26367 /* Pull out the template from an injected-class-name (or multiple). */
26369 decl
= maybe_get_template_decl_from_type_decl (decl
);
26371 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26372 if (TREE_CODE (decl
) == TREE_LIST
)
26374 if (ambiguous_decls
)
26375 *ambiguous_decls
= decl
;
26376 /* The error message we have to print is too complicated for
26377 cp_parser_error, so we incorporate its actions directly. */
26378 if (!cp_parser_simulate_error (parser
))
26380 error_at (name_location
, "reference to %qD is ambiguous",
26382 print_candidates (decl
);
26384 return error_mark_node
;
26387 gcc_assert (DECL_P (decl
)
26388 || TREE_CODE (decl
) == OVERLOAD
26389 || TREE_CODE (decl
) == SCOPE_REF
26390 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26391 || BASELINK_P (decl
));
26393 /* If we have resolved the name of a member declaration, check to
26394 see if the declaration is accessible. When the name resolves to
26395 set of overloaded functions, accessibility is checked when
26396 overload resolution is done.
26398 During an explicit instantiation, access is not checked at all,
26399 as per [temp.explicit]. */
26401 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26403 maybe_record_typedef_use (decl
);
26405 return cp_expr (decl
, name_location
);
26408 /* Like cp_parser_lookup_name, but for use in the typical case where
26409 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26410 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26413 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26415 return cp_parser_lookup_name (parser
, name
,
26417 /*is_template=*/false,
26418 /*is_namespace=*/false,
26419 /*check_dependency=*/true,
26420 /*ambiguous_decls=*/NULL
,
26424 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26425 the current context, return the TYPE_DECL. If TAG_NAME_P is
26426 true, the DECL indicates the class being defined in a class-head,
26427 or declared in an elaborated-type-specifier.
26429 Otherwise, return DECL. */
26432 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26434 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26435 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26438 template <typename T> struct B;
26441 template <typename T> struct A::B {};
26443 Similarly, in an elaborated-type-specifier:
26445 namespace N { struct X{}; }
26448 template <typename T> friend struct N::X;
26451 However, if the DECL refers to a class type, and we are in
26452 the scope of the class, then the name lookup automatically
26453 finds the TYPE_DECL created by build_self_reference rather
26454 than a TEMPLATE_DECL. For example, in:
26456 template <class T> struct S {
26460 there is no need to handle such case. */
26462 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26463 return DECL_TEMPLATE_RESULT (decl
);
26468 /* If too many, or too few, template-parameter lists apply to the
26469 declarator, issue an error message. Returns TRUE if all went well,
26470 and FALSE otherwise. */
26473 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26474 cp_declarator
*declarator
,
26475 location_t declarator_location
)
26477 switch (declarator
->kind
)
26481 unsigned num_templates
= 0;
26482 tree scope
= declarator
->u
.id
.qualifying_scope
;
26483 bool template_id_p
= false;
26486 num_templates
= num_template_headers_for_class (scope
);
26487 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26488 == TEMPLATE_ID_EXPR
)
26490 /* If the DECLARATOR has the form `X<y>' then it uses one
26491 additional level of template parameters. */
26493 template_id_p
= true;
26496 return cp_parser_check_template_parameters
26497 (parser
, num_templates
, template_id_p
, declarator_location
,
26504 case cdk_reference
:
26506 return (cp_parser_check_declarator_template_parameters
26507 (parser
, declarator
->declarator
, declarator_location
));
26514 gcc_unreachable ();
26519 /* NUM_TEMPLATES were used in the current declaration. If that is
26520 invalid, return FALSE and issue an error messages. Otherwise,
26521 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26522 declarator and we can print more accurate diagnostics. */
26525 cp_parser_check_template_parameters (cp_parser
* parser
,
26526 unsigned num_templates
,
26527 bool template_id_p
,
26528 location_t location
,
26529 cp_declarator
*declarator
)
26531 /* If there are the same number of template classes and parameter
26532 lists, that's OK. */
26533 if (parser
->num_template_parameter_lists
== num_templates
)
26535 /* If there are more, but only one more, and the name ends in an identifier,
26536 then we are declaring a primary template. That's OK too. */
26538 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26540 /* If there are more template classes than parameter lists, we have
26543 template <class T> void S<T>::R<T>::f (); */
26544 if (parser
->num_template_parameter_lists
< num_templates
)
26546 if (declarator
&& !current_function_decl
)
26547 error_at (location
, "specializing member %<%T::%E%> "
26548 "requires %<template<>%> syntax",
26549 declarator
->u
.id
.qualifying_scope
,
26550 declarator
->u
.id
.unqualified_name
);
26551 else if (declarator
)
26552 error_at (location
, "invalid declaration of %<%T::%E%>",
26553 declarator
->u
.id
.qualifying_scope
,
26554 declarator
->u
.id
.unqualified_name
);
26556 error_at (location
, "too few template-parameter-lists");
26559 /* Otherwise, there are too many template parameter lists. We have
26562 template <class T> template <class U> void S::f(); */
26563 error_at (location
, "too many template-parameter-lists");
26567 /* Parse an optional `::' token indicating that the following name is
26568 from the global namespace. If so, PARSER->SCOPE is set to the
26569 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26570 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26571 Returns the new value of PARSER->SCOPE, if the `::' token is
26572 present, and NULL_TREE otherwise. */
26575 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26579 /* Peek at the next token. */
26580 token
= cp_lexer_peek_token (parser
->lexer
);
26581 /* If we're looking at a `::' token then we're starting from the
26582 global namespace, not our current location. */
26583 if (token
->type
== CPP_SCOPE
)
26585 /* Consume the `::' token. */
26586 cp_lexer_consume_token (parser
->lexer
);
26587 /* Set the SCOPE so that we know where to start the lookup. */
26588 parser
->scope
= global_namespace
;
26589 parser
->qualifying_scope
= global_namespace
;
26590 parser
->object_scope
= NULL_TREE
;
26592 return parser
->scope
;
26594 else if (!current_scope_valid_p
)
26596 parser
->scope
= NULL_TREE
;
26597 parser
->qualifying_scope
= NULL_TREE
;
26598 parser
->object_scope
= NULL_TREE
;
26604 /* Returns TRUE if the upcoming token sequence is the start of a
26605 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26606 declarator is preceded by the `friend' specifier. */
26609 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26611 bool constructor_p
;
26612 bool outside_class_specifier_p
;
26613 tree nested_name_specifier
;
26614 cp_token
*next_token
;
26616 /* The common case is that this is not a constructor declarator, so
26617 try to avoid doing lots of work if at all possible. It's not
26618 valid declare a constructor at function scope. */
26619 if (parser
->in_function_body
)
26621 /* And only certain tokens can begin a constructor declarator. */
26622 next_token
= cp_lexer_peek_token (parser
->lexer
);
26623 if (next_token
->type
!= CPP_NAME
26624 && next_token
->type
!= CPP_SCOPE
26625 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26626 && next_token
->type
!= CPP_TEMPLATE_ID
)
26629 /* Parse tentatively; we are going to roll back all of the tokens
26631 cp_parser_parse_tentatively (parser
);
26632 /* Assume that we are looking at a constructor declarator. */
26633 constructor_p
= true;
26635 /* Look for the optional `::' operator. */
26636 cp_parser_global_scope_opt (parser
,
26637 /*current_scope_valid_p=*/false);
26638 /* Look for the nested-name-specifier. */
26639 nested_name_specifier
26640 = (cp_parser_nested_name_specifier_opt (parser
,
26641 /*typename_keyword_p=*/false,
26642 /*check_dependency_p=*/false,
26644 /*is_declaration=*/false));
26646 outside_class_specifier_p
= (!at_class_scope_p ()
26647 || !TYPE_BEING_DEFINED (current_class_type
)
26650 /* Outside of a class-specifier, there must be a
26651 nested-name-specifier. Except in C++17 mode, where we
26652 might be declaring a guiding declaration. */
26653 if (!nested_name_specifier
&& outside_class_specifier_p
26654 && cxx_dialect
< cxx17
)
26655 constructor_p
= false;
26656 else if (nested_name_specifier
== error_mark_node
)
26657 constructor_p
= false;
26659 /* If we have a class scope, this is easy; DR 147 says that S::S always
26660 names the constructor, and no other qualified name could. */
26661 if (constructor_p
&& nested_name_specifier
26662 && CLASS_TYPE_P (nested_name_specifier
))
26664 tree id
= cp_parser_unqualified_id (parser
,
26665 /*template_keyword_p=*/false,
26666 /*check_dependency_p=*/false,
26667 /*declarator_p=*/true,
26668 /*optional_p=*/false);
26669 if (is_overloaded_fn (id
))
26670 id
= DECL_NAME (get_first_fn (id
));
26671 if (!constructor_name_p (id
, nested_name_specifier
))
26672 constructor_p
= false;
26674 /* If we still think that this might be a constructor-declarator,
26675 look for a class-name. */
26676 else if (constructor_p
)
26680 template <typename T> struct S {
26684 we must recognize that the nested `S' names a class. */
26685 if (cxx_dialect
>= cxx17
)
26686 cp_parser_parse_tentatively (parser
);
26689 type_decl
= cp_parser_class_name (parser
,
26690 /*typename_keyword_p=*/false,
26691 /*template_keyword_p=*/false,
26693 /*check_dependency_p=*/false,
26694 /*class_head_p=*/false,
26695 /*is_declaration=*/false);
26697 if (cxx_dialect
>= cxx17
26698 && !cp_parser_parse_definitely (parser
))
26700 type_decl
= NULL_TREE
;
26701 tree tmpl
= cp_parser_template_name (parser
,
26702 /*template_keyword*/false,
26703 /*check_dependency_p*/false,
26704 /*is_declaration*/false,
26706 /*is_identifier*/NULL
);
26707 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26708 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26709 /* It's a deduction guide, return true. */;
26711 cp_parser_simulate_error (parser
);
26714 /* If there was no class-name, then this is not a constructor.
26715 Otherwise, if we are in a class-specifier and we aren't
26716 handling a friend declaration, check that its type matches
26717 current_class_type (c++/38313). Note: error_mark_node
26718 is left alone for error recovery purposes. */
26719 constructor_p
= (!cp_parser_error_occurred (parser
)
26720 && (outside_class_specifier_p
26721 || type_decl
== NULL_TREE
26722 || type_decl
== error_mark_node
26723 || same_type_p (current_class_type
,
26724 TREE_TYPE (type_decl
))));
26726 /* If we're still considering a constructor, we have to see a `(',
26727 to begin the parameter-declaration-clause, followed by either a
26728 `)', an `...', or a decl-specifier. We need to check for a
26729 type-specifier to avoid being fooled into thinking that:
26733 is a constructor. (It is actually a function named `f' that
26734 takes one parameter (of type `int') and returns a value of type
26737 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26738 constructor_p
= false;
26741 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26742 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26743 /* A parameter declaration begins with a decl-specifier,
26744 which is either the "attribute" keyword, a storage class
26745 specifier, or (usually) a type-specifier. */
26746 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26749 tree pushed_scope
= NULL_TREE
;
26750 unsigned saved_num_template_parameter_lists
;
26752 /* Names appearing in the type-specifier should be looked up
26753 in the scope of the class. */
26754 if (current_class_type
)
26756 else if (type_decl
)
26758 type
= TREE_TYPE (type_decl
);
26759 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26761 type
= resolve_typename_type (type
,
26762 /*only_current_p=*/false);
26763 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26765 cp_parser_abort_tentative_parse (parser
);
26769 pushed_scope
= push_scope (type
);
26772 /* Inside the constructor parameter list, surrounding
26773 template-parameter-lists do not apply. */
26774 saved_num_template_parameter_lists
26775 = parser
->num_template_parameter_lists
;
26776 parser
->num_template_parameter_lists
= 0;
26778 /* Look for the type-specifier. */
26779 cp_parser_type_specifier (parser
,
26780 CP_PARSER_FLAGS_NONE
,
26781 /*decl_specs=*/NULL
,
26782 /*is_declarator=*/true,
26783 /*declares_class_or_enum=*/NULL
,
26784 /*is_cv_qualifier=*/NULL
);
26786 parser
->num_template_parameter_lists
26787 = saved_num_template_parameter_lists
;
26789 /* Leave the scope of the class. */
26791 pop_scope (pushed_scope
);
26793 constructor_p
= !cp_parser_error_occurred (parser
);
26797 /* We did not really want to consume any tokens. */
26798 cp_parser_abort_tentative_parse (parser
);
26800 return constructor_p
;
26803 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26804 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26805 they must be performed once we are in the scope of the function.
26807 Returns the function defined. */
26810 cp_parser_function_definition_from_specifiers_and_declarator
26811 (cp_parser
* parser
,
26812 cp_decl_specifier_seq
*decl_specifiers
,
26814 const cp_declarator
*declarator
)
26819 /* Begin the function-definition. */
26820 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26822 /* The things we're about to see are not directly qualified by any
26823 template headers we've seen thus far. */
26824 reset_specialization ();
26826 /* If there were names looked up in the decl-specifier-seq that we
26827 did not check, check them now. We must wait until we are in the
26828 scope of the function to perform the checks, since the function
26829 might be a friend. */
26830 perform_deferred_access_checks (tf_warning_or_error
);
26834 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26835 parser
->omp_declare_simd
= NULL
;
26836 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26837 parser
->oacc_routine
= NULL
;
26842 /* Skip the entire function. */
26843 cp_parser_skip_to_end_of_block_or_statement (parser
);
26844 fn
= error_mark_node
;
26846 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26848 /* Seen already, skip it. An error message has already been output. */
26849 cp_parser_skip_to_end_of_block_or_statement (parser
);
26850 fn
= current_function_decl
;
26851 current_function_decl
= NULL_TREE
;
26852 /* If this is a function from a class, pop the nested class. */
26853 if (current_class_name
)
26854 pop_nested_class ();
26859 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26860 tv
= TV_PARSE_INLINE
;
26862 tv
= TV_PARSE_FUNC
;
26864 fn
= cp_parser_function_definition_after_declarator (parser
,
26865 /*inline_p=*/false);
26872 /* Parse the part of a function-definition that follows the
26873 declarator. INLINE_P is TRUE iff this function is an inline
26874 function defined within a class-specifier.
26876 Returns the function defined. */
26879 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26883 bool saved_in_unbraced_linkage_specification_p
;
26884 bool saved_in_function_body
;
26885 unsigned saved_num_template_parameter_lists
;
26887 bool fully_implicit_function_template_p
26888 = parser
->fully_implicit_function_template_p
;
26889 parser
->fully_implicit_function_template_p
= false;
26890 tree implicit_template_parms
26891 = parser
->implicit_template_parms
;
26892 parser
->implicit_template_parms
= 0;
26893 cp_binding_level
* implicit_template_scope
26894 = parser
->implicit_template_scope
;
26895 parser
->implicit_template_scope
= 0;
26897 saved_in_function_body
= parser
->in_function_body
;
26898 parser
->in_function_body
= true;
26899 /* If the next token is `return', then the code may be trying to
26900 make use of the "named return value" extension that G++ used to
26902 token
= cp_lexer_peek_token (parser
->lexer
);
26903 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26905 /* Consume the `return' keyword. */
26906 cp_lexer_consume_token (parser
->lexer
);
26907 /* Look for the identifier that indicates what value is to be
26909 cp_parser_identifier (parser
);
26910 /* Issue an error message. */
26911 error_at (token
->location
,
26912 "named return values are no longer supported");
26913 /* Skip tokens until we reach the start of the function body. */
26916 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26917 if (token
->type
== CPP_OPEN_BRACE
26918 || token
->type
== CPP_EOF
26919 || token
->type
== CPP_PRAGMA_EOL
)
26921 cp_lexer_consume_token (parser
->lexer
);
26924 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26925 anything declared inside `f'. */
26926 saved_in_unbraced_linkage_specification_p
26927 = parser
->in_unbraced_linkage_specification_p
;
26928 parser
->in_unbraced_linkage_specification_p
= false;
26929 /* Inside the function, surrounding template-parameter-lists do not
26931 saved_num_template_parameter_lists
26932 = parser
->num_template_parameter_lists
;
26933 parser
->num_template_parameter_lists
= 0;
26935 /* If the next token is `try', `__transaction_atomic', or
26936 `__transaction_relaxed`, then we are looking at either function-try-block
26937 or function-transaction-block. Note that all of these include the
26939 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26940 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26941 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26942 RID_TRANSACTION_RELAXED
))
26943 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26944 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26945 cp_parser_function_try_block (parser
);
26947 cp_parser_ctor_initializer_opt_and_function_body
26948 (parser
, /*in_function_try_block=*/false);
26950 /* Finish the function. */
26951 fn
= finish_function (inline_p
);
26952 /* Generate code for it, if necessary. */
26953 expand_or_defer_fn (fn
);
26954 /* Restore the saved values. */
26955 parser
->in_unbraced_linkage_specification_p
26956 = saved_in_unbraced_linkage_specification_p
;
26957 parser
->num_template_parameter_lists
26958 = saved_num_template_parameter_lists
;
26959 parser
->in_function_body
= saved_in_function_body
;
26961 parser
->fully_implicit_function_template_p
26962 = fully_implicit_function_template_p
;
26963 parser
->implicit_template_parms
26964 = implicit_template_parms
;
26965 parser
->implicit_template_scope
26966 = implicit_template_scope
;
26968 if (parser
->fully_implicit_function_template_p
)
26969 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26974 /* Parse a template-declaration body (following argument list). */
26977 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26978 tree parameter_list
,
26981 tree decl
= NULL_TREE
;
26982 bool friend_p
= false;
26984 /* We just processed one more parameter list. */
26985 ++parser
->num_template_parameter_lists
;
26987 /* Get the deferred access checks from the parameter list. These
26988 will be checked once we know what is being declared, as for a
26989 member template the checks must be performed in the scope of the
26990 class containing the member. */
26991 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26993 /* Tentatively parse for a new template parameter list, which can either be
26994 the template keyword or a template introduction. */
26995 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26997 else if (cxx_dialect
>= cxx11
26998 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26999 decl
= cp_parser_alias_declaration (parser
);
27002 /* There are no access checks when parsing a template, as we do not
27003 know if a specialization will be a friend. */
27004 push_deferring_access_checks (dk_no_check
);
27005 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27006 decl
= cp_parser_single_declaration (parser
,
27009 /*explicit_specialization_p=*/false,
27011 pop_deferring_access_checks ();
27013 /* If this is a member template declaration, let the front
27015 if (member_p
&& !friend_p
&& decl
)
27017 if (TREE_CODE (decl
) == TYPE_DECL
)
27018 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27020 decl
= finish_member_template_decl (decl
);
27022 else if (friend_p
&& decl
27023 && DECL_DECLARES_TYPE_P (decl
))
27024 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27025 /*complain=*/true);
27027 /* We are done with the current parameter list. */
27028 --parser
->num_template_parameter_lists
;
27030 pop_deferring_access_checks ();
27033 finish_template_decl (parameter_list
);
27035 /* Check the template arguments for a literal operator template. */
27037 && DECL_DECLARES_FUNCTION_P (decl
)
27038 && UDLIT_OPER_P (DECL_NAME (decl
)))
27041 if (parameter_list
== NULL_TREE
)
27045 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27046 if (num_parms
== 1)
27048 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27049 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27050 if (TREE_TYPE (parm
) != char_type_node
27051 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27054 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27056 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27057 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27058 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27059 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27060 if (parm
== error_mark_node
27061 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27062 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27070 if (cxx_dialect
>= cxx14
)
27071 error ("literal operator template %qD has invalid parameter list."
27072 " Expected non-type template argument pack <char...>"
27073 " or <typename CharT, CharT...>",
27076 error ("literal operator template %qD has invalid parameter list."
27077 " Expected non-type template argument pack <char...>",
27082 /* Register member declarations. */
27083 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27084 finish_member_declaration (decl
);
27085 /* If DECL is a function template, we must return to parse it later.
27086 (Even though there is no definition, there might be default
27087 arguments that need handling.) */
27088 if (member_p
&& decl
27089 && DECL_DECLARES_FUNCTION_P (decl
))
27090 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27093 /* Parse a template introduction header for a template-declaration. Returns
27094 false if tentative parse fails. */
27097 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27099 cp_parser_parse_tentatively (parser
);
27101 tree saved_scope
= parser
->scope
;
27102 tree saved_object_scope
= parser
->object_scope
;
27103 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27105 /* Look for the optional `::' operator. */
27106 cp_parser_global_scope_opt (parser
,
27107 /*current_scope_valid_p=*/false);
27108 /* Look for the nested-name-specifier. */
27109 cp_parser_nested_name_specifier_opt (parser
,
27110 /*typename_keyword_p=*/false,
27111 /*check_dependency_p=*/true,
27113 /*is_declaration=*/false);
27115 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27116 tree concept_name
= cp_parser_identifier (parser
);
27118 /* Look up the concept for which we will be matching
27119 template parameters. */
27120 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27122 parser
->scope
= saved_scope
;
27123 parser
->object_scope
= saved_object_scope
;
27124 parser
->qualifying_scope
= saved_qualifying_scope
;
27126 if (concept_name
== error_mark_node
)
27127 cp_parser_simulate_error (parser
);
27129 /* Look for opening brace for introduction. */
27130 matching_braces braces
;
27131 braces
.require_open (parser
);
27133 if (!cp_parser_parse_definitely (parser
))
27136 push_deferring_access_checks (dk_deferred
);
27138 /* Build vector of placeholder parameters and grab
27139 matching identifiers. */
27140 tree introduction_list
= cp_parser_introduction_list (parser
);
27142 /* The introduction-list shall not be empty. */
27143 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27146 error ("empty introduction-list");
27150 /* Look for closing brace for introduction. */
27151 if (!braces
.require_close (parser
))
27154 if (tmpl_decl
== error_mark_node
)
27156 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27161 /* Build and associate the constraint. */
27162 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27163 if (parms
&& parms
!= error_mark_node
)
27165 cp_parser_template_declaration_after_parameters (parser
, parms
,
27170 error_at (token
->location
, "no matching concept for template-introduction");
27174 /* Parse a normal template-declaration following the template keyword. */
27177 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27179 tree parameter_list
;
27180 bool need_lang_pop
;
27181 location_t location
= input_location
;
27183 /* Look for the `<' token. */
27184 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27186 if (at_class_scope_p () && current_function_decl
)
27188 /* 14.5.2.2 [temp.mem]
27190 A local class shall not have member templates. */
27191 error_at (location
,
27192 "invalid declaration of member template in local class");
27193 cp_parser_skip_to_end_of_block_or_statement (parser
);
27198 A template ... shall not have C linkage. */
27199 if (current_lang_name
== lang_name_c
)
27201 error_at (location
, "template with C linkage");
27202 maybe_show_extern_c_location ();
27203 /* Give it C++ linkage to avoid confusing other parts of the
27205 push_lang_context (lang_name_cplusplus
);
27206 need_lang_pop
= true;
27209 need_lang_pop
= false;
27211 /* We cannot perform access checks on the template parameter
27212 declarations until we know what is being declared, just as we
27213 cannot check the decl-specifier list. */
27214 push_deferring_access_checks (dk_deferred
);
27216 /* If the next token is `>', then we have an invalid
27217 specialization. Rather than complain about an invalid template
27218 parameter, issue an error message here. */
27219 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27221 cp_parser_error (parser
, "invalid explicit specialization");
27222 begin_specialization ();
27223 parameter_list
= NULL_TREE
;
27227 /* Parse the template parameters. */
27228 parameter_list
= cp_parser_template_parameter_list (parser
);
27231 /* Look for the `>'. */
27232 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27234 /* Manage template requirements */
27237 tree reqs
= get_shorthand_constraints (current_template_parms
);
27238 if (tree r
= cp_parser_requires_clause_opt (parser
))
27239 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27240 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27243 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27246 /* For the erroneous case of a template with C linkage, we pushed an
27247 implicit C++ linkage scope; exit that scope now. */
27249 pop_lang_context ();
27252 /* Parse a template-declaration, assuming that the `export' (and
27253 `extern') keywords, if present, has already been scanned. MEMBER_P
27254 is as for cp_parser_template_declaration. */
27257 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27259 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27261 cp_lexer_consume_token (parser
->lexer
);
27262 cp_parser_explicit_template_declaration (parser
, member_p
);
27265 else if (flag_concepts
)
27266 return cp_parser_template_introduction (parser
, member_p
);
27271 /* Perform the deferred access checks from a template-parameter-list.
27272 CHECKS is a TREE_LIST of access checks, as returned by
27273 get_deferred_access_checks. */
27276 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27278 ++processing_template_parmlist
;
27279 perform_access_checks (checks
, tf_warning_or_error
);
27280 --processing_template_parmlist
;
27283 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27284 `function-definition' sequence that follows a template header.
27285 If MEMBER_P is true, this declaration appears in a class scope.
27287 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27288 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27291 cp_parser_single_declaration (cp_parser
* parser
,
27292 vec
<deferred_access_check
, va_gc
> *checks
,
27294 bool explicit_specialization_p
,
27297 int declares_class_or_enum
;
27298 tree decl
= NULL_TREE
;
27299 cp_decl_specifier_seq decl_specifiers
;
27300 bool function_definition_p
= false;
27301 cp_token
*decl_spec_token_start
;
27303 /* This function is only used when processing a template
27305 gcc_assert (innermost_scope_kind () == sk_template_parms
27306 || innermost_scope_kind () == sk_template_spec
);
27308 /* Defer access checks until we know what is being declared. */
27309 push_deferring_access_checks (dk_deferred
);
27311 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27313 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27314 cp_parser_decl_specifier_seq (parser
,
27315 CP_PARSER_FLAGS_OPTIONAL
,
27317 &declares_class_or_enum
);
27319 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27321 /* There are no template typedefs. */
27322 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27324 error_at (decl_spec_token_start
->location
,
27325 "template declaration of %<typedef%>");
27326 decl
= error_mark_node
;
27329 /* Gather up the access checks that occurred the
27330 decl-specifier-seq. */
27331 stop_deferring_access_checks ();
27333 /* Check for the declaration of a template class. */
27334 if (declares_class_or_enum
)
27336 if (cp_parser_declares_only_class_p (parser
)
27337 || (declares_class_or_enum
& 2))
27339 // If this is a declaration, but not a definition, associate
27340 // any constraints with the type declaration. Constraints
27341 // are associated with definitions in cp_parser_class_specifier.
27342 if (declares_class_or_enum
== 1)
27343 associate_classtype_constraints (decl_specifiers
.type
);
27345 decl
= shadow_tag (&decl_specifiers
);
27350 friend template <typename T> struct A<T>::B;
27353 A<T>::B will be represented by a TYPENAME_TYPE, and
27354 therefore not recognized by shadow_tag. */
27355 if (friend_p
&& *friend_p
27357 && decl_specifiers
.type
27358 && TYPE_P (decl_specifiers
.type
))
27359 decl
= decl_specifiers
.type
;
27361 if (decl
&& decl
!= error_mark_node
)
27362 decl
= TYPE_NAME (decl
);
27364 decl
= error_mark_node
;
27366 /* Perform access checks for template parameters. */
27367 cp_parser_perform_template_parameter_access_checks (checks
);
27369 /* Give a helpful diagnostic for
27370 template <class T> struct A { } a;
27371 if we aren't already recovering from an error. */
27372 if (!cp_parser_declares_only_class_p (parser
)
27375 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27376 "a class template declaration must not declare "
27378 cp_parser_skip_to_end_of_block_or_statement (parser
);
27384 /* Complain about missing 'typename' or other invalid type names. */
27385 if (!decl_specifiers
.any_type_specifiers_p
27386 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27388 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27389 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27390 the rest of this declaration. */
27391 decl
= error_mark_node
;
27395 /* If it's not a template class, try for a template function. If
27396 the next token is a `;', then this declaration does not declare
27397 anything. But, if there were errors in the decl-specifiers, then
27398 the error might well have come from an attempted class-specifier.
27399 In that case, there's no need to warn about a missing declarator. */
27401 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27402 || decl_specifiers
.type
!= error_mark_node
))
27404 decl
= cp_parser_init_declarator (parser
,
27407 /*function_definition_allowed_p=*/true,
27409 declares_class_or_enum
,
27410 &function_definition_p
,
27413 /* 7.1.1-1 [dcl.stc]
27415 A storage-class-specifier shall not be specified in an explicit
27416 specialization... */
27418 && explicit_specialization_p
27419 && decl_specifiers
.storage_class
!= sc_none
)
27421 error_at (decl_spec_token_start
->location
,
27422 "explicit template specialization cannot have a storage class");
27423 decl
= error_mark_node
;
27426 if (decl
&& VAR_P (decl
))
27427 check_template_variable (decl
);
27430 /* Look for a trailing `;' after the declaration. */
27431 if (!function_definition_p
27432 && (decl
== error_mark_node
27433 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27434 cp_parser_skip_to_end_of_block_or_statement (parser
);
27437 pop_deferring_access_checks ();
27439 /* Clear any current qualification; whatever comes next is the start
27440 of something new. */
27441 parser
->scope
= NULL_TREE
;
27442 parser
->qualifying_scope
= NULL_TREE
;
27443 parser
->object_scope
= NULL_TREE
;
27448 /* Parse a cast-expression that is not the operand of a unary "&". */
27451 cp_parser_simple_cast_expression (cp_parser
*parser
)
27453 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27454 /*cast_p=*/false, /*decltype*/false, NULL
);
27457 /* Parse a functional cast to TYPE. Returns an expression
27458 representing the cast. */
27461 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27463 vec
<tree
, va_gc
> *vec
;
27464 tree expression_list
;
27468 location_t start_loc
= input_location
;
27471 type
= error_mark_node
;
27473 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27475 cp_lexer_set_source_position (parser
->lexer
);
27476 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27477 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27478 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27479 if (TREE_CODE (type
) == TYPE_DECL
)
27480 type
= TREE_TYPE (type
);
27482 cast
= finish_compound_literal (type
, expression_list
,
27483 tf_warning_or_error
, fcl_functional
);
27484 /* Create a location of the form:
27487 with caret == start at the start of the type name,
27488 finishing at the closing brace. */
27489 location_t finish_loc
27490 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27491 location_t combined_loc
= make_location (start_loc
, start_loc
,
27493 cast
.set_location (combined_loc
);
27498 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27500 /*allow_expansion_p=*/true,
27501 /*non_constant_p=*/NULL
);
27503 expression_list
= error_mark_node
;
27506 expression_list
= build_tree_list_vec (vec
);
27507 release_tree_vector (vec
);
27510 cast
= build_functional_cast (type
, expression_list
,
27511 tf_warning_or_error
);
27512 /* [expr.const]/1: In an integral constant expression "only type
27513 conversions to integral or enumeration type can be used". */
27514 if (TREE_CODE (type
) == TYPE_DECL
)
27515 type
= TREE_TYPE (type
);
27516 if (cast
!= error_mark_node
27517 && !cast_valid_in_integral_constant_expression_p (type
)
27518 && cp_parser_non_integral_constant_expression (parser
,
27520 return error_mark_node
;
27522 /* Create a location of the form:
27525 with caret == start at the start of the type name,
27526 finishing at the closing paren. */
27527 location_t finish_loc
27528 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27529 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27530 cast
.set_location (combined_loc
);
27534 /* Save the tokens that make up the body of a member function defined
27535 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27536 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27537 specifiers applied to the declaration. Returns the FUNCTION_DECL
27538 for the member function. */
27541 cp_parser_save_member_function_body (cp_parser
* parser
,
27542 cp_decl_specifier_seq
*decl_specifiers
,
27543 cp_declarator
*declarator
,
27549 bool function_try_block
= false;
27551 /* Create the FUNCTION_DECL. */
27552 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27553 cp_finalize_omp_declare_simd (parser
, fn
);
27554 cp_finalize_oacc_routine (parser
, fn
, true);
27555 /* If something went badly wrong, bail out now. */
27556 if (fn
== error_mark_node
)
27558 /* If there's a function-body, skip it. */
27559 if (cp_parser_token_starts_function_definition_p
27560 (cp_lexer_peek_token (parser
->lexer
)))
27561 cp_parser_skip_to_end_of_block_or_statement (parser
);
27562 return error_mark_node
;
27565 /* Remember it, if there default args to post process. */
27566 cp_parser_save_default_args (parser
, fn
);
27568 /* Save away the tokens that make up the body of the
27570 first
= parser
->lexer
->next_token
;
27572 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27573 cp_lexer_consume_token (parser
->lexer
);
27574 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27575 RID_TRANSACTION_ATOMIC
))
27577 cp_lexer_consume_token (parser
->lexer
);
27578 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27580 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27581 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27582 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27583 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27584 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27586 cp_lexer_consume_token (parser
->lexer
);
27587 cp_lexer_consume_token (parser
->lexer
);
27588 cp_lexer_consume_token (parser
->lexer
);
27589 cp_lexer_consume_token (parser
->lexer
);
27590 cp_lexer_consume_token (parser
->lexer
);
27593 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27594 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27596 cp_lexer_consume_token (parser
->lexer
);
27597 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27602 /* Handle function try blocks. */
27603 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27605 cp_lexer_consume_token (parser
->lexer
);
27606 function_try_block
= true;
27608 /* We can have braced-init-list mem-initializers before the fn body. */
27609 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27611 cp_lexer_consume_token (parser
->lexer
);
27612 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27614 /* cache_group will stop after an un-nested { } pair, too. */
27615 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27618 /* variadic mem-inits have ... after the ')'. */
27619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27620 cp_lexer_consume_token (parser
->lexer
);
27623 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27624 /* Handle function try blocks. */
27625 if (function_try_block
)
27626 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27627 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27628 last
= parser
->lexer
->next_token
;
27630 /* Save away the inline definition; we will process it when the
27631 class is complete. */
27632 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27633 DECL_PENDING_INLINE_P (fn
) = 1;
27635 /* We need to know that this was defined in the class, so that
27636 friend templates are handled correctly. */
27637 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27639 /* Add FN to the queue of functions to be parsed later. */
27640 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27645 /* Save the tokens that make up the in-class initializer for a non-static
27646 data member. Returns a DEFAULT_ARG. */
27649 cp_parser_save_nsdmi (cp_parser
* parser
)
27651 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27654 /* Parse a template-argument-list, as well as the trailing ">" (but
27655 not the opening "<"). See cp_parser_template_argument_list for the
27659 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27663 tree saved_qualifying_scope
;
27664 tree saved_object_scope
;
27665 bool saved_greater_than_is_operator_p
;
27666 int saved_unevaluated_operand
;
27667 int saved_inhibit_evaluation_warnings
;
27671 When parsing a template-id, the first non-nested `>' is taken as
27672 the end of the template-argument-list rather than a greater-than
27674 saved_greater_than_is_operator_p
27675 = parser
->greater_than_is_operator_p
;
27676 parser
->greater_than_is_operator_p
= false;
27677 /* Parsing the argument list may modify SCOPE, so we save it
27679 saved_scope
= parser
->scope
;
27680 saved_qualifying_scope
= parser
->qualifying_scope
;
27681 saved_object_scope
= parser
->object_scope
;
27682 /* We need to evaluate the template arguments, even though this
27683 template-id may be nested within a "sizeof". */
27684 saved_unevaluated_operand
= cp_unevaluated_operand
;
27685 cp_unevaluated_operand
= 0;
27686 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27687 c_inhibit_evaluation_warnings
= 0;
27688 /* Parse the template-argument-list itself. */
27689 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27690 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27691 arguments
= NULL_TREE
;
27693 arguments
= cp_parser_template_argument_list (parser
);
27694 /* Look for the `>' that ends the template-argument-list. If we find
27695 a '>>' instead, it's probably just a typo. */
27696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27698 if (cxx_dialect
!= cxx98
)
27700 /* In C++0x, a `>>' in a template argument list or cast
27701 expression is considered to be two separate `>'
27702 tokens. So, change the current token to a `>', but don't
27703 consume it: it will be consumed later when the outer
27704 template argument list (or cast expression) is parsed.
27705 Note that this replacement of `>' for `>>' is necessary
27706 even if we are parsing tentatively: in the tentative
27707 case, after calling
27708 cp_parser_enclosed_template_argument_list we will always
27709 throw away all of the template arguments and the first
27710 closing `>', either because the template argument list
27711 was erroneous or because we are replacing those tokens
27712 with a CPP_TEMPLATE_ID token. The second `>' (which will
27713 not have been thrown away) is needed either to close an
27714 outer template argument list or to complete a new-style
27716 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27717 token
->type
= CPP_GREATER
;
27719 else if (!saved_greater_than_is_operator_p
)
27721 /* If we're in a nested template argument list, the '>>' has
27722 to be a typo for '> >'. We emit the error message, but we
27723 continue parsing and we push a '>' as next token, so that
27724 the argument list will be parsed correctly. Note that the
27725 global source location is still on the token before the
27726 '>>', so we need to say explicitly where we want it. */
27727 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27728 gcc_rich_location
richloc (token
->location
);
27729 richloc
.add_fixit_replace ("> >");
27730 error_at (&richloc
, "%<>>%> should be %<> >%> "
27731 "within a nested template argument list");
27733 token
->type
= CPP_GREATER
;
27737 /* If this is not a nested template argument list, the '>>'
27738 is a typo for '>'. Emit an error message and continue.
27739 Same deal about the token location, but here we can get it
27740 right by consuming the '>>' before issuing the diagnostic. */
27741 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27742 error_at (token
->location
,
27743 "spurious %<>>%>, use %<>%> to terminate "
27744 "a template argument list");
27748 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27749 /* The `>' token might be a greater-than operator again now. */
27750 parser
->greater_than_is_operator_p
27751 = saved_greater_than_is_operator_p
;
27752 /* Restore the SAVED_SCOPE. */
27753 parser
->scope
= saved_scope
;
27754 parser
->qualifying_scope
= saved_qualifying_scope
;
27755 parser
->object_scope
= saved_object_scope
;
27756 cp_unevaluated_operand
= saved_unevaluated_operand
;
27757 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27762 /* MEMBER_FUNCTION is a member function, or a friend. If default
27763 arguments, or the body of the function have not yet been parsed,
27767 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27769 timevar_push (TV_PARSE_INMETH
);
27770 /* If this member is a template, get the underlying
27772 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27773 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27775 /* There should not be any class definitions in progress at this
27776 point; the bodies of members are only parsed outside of all class
27778 gcc_assert (parser
->num_classes_being_defined
== 0);
27779 /* While we're parsing the member functions we might encounter more
27780 classes. We want to handle them right away, but we don't want
27781 them getting mixed up with functions that are currently in the
27783 push_unparsed_function_queues (parser
);
27785 /* Make sure that any template parameters are in scope. */
27786 maybe_begin_member_template_processing (member_function
);
27788 /* If the body of the function has not yet been parsed, parse it
27790 if (DECL_PENDING_INLINE_P (member_function
))
27792 tree function_scope
;
27793 cp_token_cache
*tokens
;
27795 /* The function is no longer pending; we are processing it. */
27796 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27797 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27798 DECL_PENDING_INLINE_P (member_function
) = 0;
27800 /* If this is a local class, enter the scope of the containing
27802 function_scope
= current_function_decl
;
27803 if (function_scope
)
27804 push_function_context ();
27806 /* Push the body of the function onto the lexer stack. */
27807 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27809 /* Let the front end know that we going to be defining this
27811 start_preparsed_function (member_function
, NULL_TREE
,
27812 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27814 /* Don't do access checking if it is a templated function. */
27815 if (processing_template_decl
)
27816 push_deferring_access_checks (dk_no_check
);
27818 /* #pragma omp declare reduction needs special parsing. */
27819 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27821 parser
->lexer
->in_pragma
= true;
27822 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27823 finish_function (/*inline_p=*/true);
27824 cp_check_omp_declare_reduction (member_function
);
27827 /* Now, parse the body of the function. */
27828 cp_parser_function_definition_after_declarator (parser
,
27829 /*inline_p=*/true);
27831 if (processing_template_decl
)
27832 pop_deferring_access_checks ();
27834 /* Leave the scope of the containing function. */
27835 if (function_scope
)
27836 pop_function_context ();
27837 cp_parser_pop_lexer (parser
);
27840 /* Remove any template parameters from the symbol table. */
27841 maybe_end_member_template_processing ();
27843 /* Restore the queue. */
27844 pop_unparsed_function_queues (parser
);
27845 timevar_pop (TV_PARSE_INMETH
);
27848 /* If DECL contains any default args, remember it on the unparsed
27849 functions queue. */
27852 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27856 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27858 probe
= TREE_CHAIN (probe
))
27859 if (TREE_PURPOSE (probe
))
27861 cp_default_arg_entry entry
= {current_class_type
, decl
};
27862 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27867 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27868 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27869 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27870 from the parameter-type-list. */
27873 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27874 tree default_arg
, tree parmtype
)
27876 cp_token_cache
*tokens
;
27880 if (default_arg
== error_mark_node
)
27881 return error_mark_node
;
27883 /* Push the saved tokens for the default argument onto the parser's
27885 tokens
= DEFARG_TOKENS (default_arg
);
27886 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27888 start_lambda_scope (decl
);
27890 /* Parse the default argument. */
27891 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27892 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27893 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27895 finish_lambda_scope ();
27897 if (parsed_arg
== error_mark_node
)
27898 cp_parser_skip_to_end_of_statement (parser
);
27900 if (!processing_template_decl
)
27902 /* In a non-template class, check conversions now. In a template,
27903 we'll wait and instantiate these as needed. */
27904 if (TREE_CODE (decl
) == PARM_DECL
)
27905 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27906 tf_warning_or_error
);
27907 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27908 parsed_arg
= error_mark_node
;
27910 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27913 /* If the token stream has not been completely used up, then
27914 there was extra junk after the end of the default
27916 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27918 if (TREE_CODE (decl
) == PARM_DECL
)
27919 cp_parser_error (parser
, "expected %<,%>");
27921 cp_parser_error (parser
, "expected %<;%>");
27924 /* Revert to the main lexer. */
27925 cp_parser_pop_lexer (parser
);
27930 /* FIELD is a non-static data member with an initializer which we saved for
27931 later; parse it now. */
27934 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27938 maybe_begin_member_template_processing (field
);
27940 push_unparsed_function_queues (parser
);
27941 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27942 DECL_INITIAL (field
),
27944 pop_unparsed_function_queues (parser
);
27946 maybe_end_member_template_processing ();
27948 DECL_INITIAL (field
) = def
;
27951 /* FN is a FUNCTION_DECL which may contains a parameter with an
27952 unparsed DEFAULT_ARG. Parse the default args now. This function
27953 assumes that the current scope is the scope in which the default
27954 argument should be processed. */
27957 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27959 bool saved_local_variables_forbidden_p
;
27960 tree parm
, parmdecl
;
27962 /* While we're parsing the default args, we might (due to the
27963 statement expression extension) encounter more classes. We want
27964 to handle them right away, but we don't want them getting mixed
27965 up with default args that are currently in the queue. */
27966 push_unparsed_function_queues (parser
);
27968 /* Local variable names (and the `this' keyword) may not appear
27969 in a default argument. */
27970 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27971 parser
->local_variables_forbidden_p
= true;
27973 push_defarg_context (fn
);
27975 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27976 parmdecl
= DECL_ARGUMENTS (fn
);
27977 parm
&& parm
!= void_list_node
;
27978 parm
= TREE_CHAIN (parm
),
27979 parmdecl
= DECL_CHAIN (parmdecl
))
27981 tree default_arg
= TREE_PURPOSE (parm
);
27983 vec
<tree
, va_gc
> *insts
;
27990 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27991 /* This can happen for a friend declaration for a function
27992 already declared with default arguments. */
27996 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27998 TREE_VALUE (parm
));
27999 TREE_PURPOSE (parm
) = parsed_arg
;
28001 /* Update any instantiations we've already created. */
28002 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28003 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28004 TREE_PURPOSE (copy
) = parsed_arg
;
28007 pop_defarg_context ();
28009 /* Make sure no default arg is missing. */
28010 check_default_args (fn
);
28012 /* Restore the state of local_variables_forbidden_p. */
28013 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28015 /* Restore the queue. */
28016 pop_unparsed_function_queues (parser
);
28019 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28021 sizeof ... ( identifier )
28023 where the 'sizeof' token has already been consumed. */
28026 cp_parser_sizeof_pack (cp_parser
*parser
)
28028 /* Consume the `...'. */
28029 cp_lexer_consume_token (parser
->lexer
);
28030 maybe_warn_variadic_templates ();
28032 matching_parens parens
;
28033 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28035 parens
.consume_open (parser
);
28037 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28038 "%<sizeof...%> argument must be surrounded by parentheses");
28040 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28041 tree name
= cp_parser_identifier (parser
);
28042 if (name
== error_mark_node
)
28043 return error_mark_node
;
28044 /* The name is not qualified. */
28045 parser
->scope
= NULL_TREE
;
28046 parser
->qualifying_scope
= NULL_TREE
;
28047 parser
->object_scope
= NULL_TREE
;
28048 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28049 if (expr
== error_mark_node
)
28050 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28052 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28053 expr
= TREE_TYPE (expr
);
28054 else if (TREE_CODE (expr
) == CONST_DECL
)
28055 expr
= DECL_INITIAL (expr
);
28056 expr
= make_pack_expansion (expr
);
28057 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28060 parens
.require_close (parser
);
28065 /* Parse the operand of `sizeof' (or a similar operator). Returns
28066 either a TYPE or an expression, depending on the form of the
28067 input. The KEYWORD indicates which kind of expression we have
28071 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28073 tree expr
= NULL_TREE
;
28074 const char *saved_message
;
28076 bool saved_integral_constant_expression_p
;
28077 bool saved_non_integral_constant_expression_p
;
28079 /* If it's a `...', then we are computing the length of a parameter
28081 if (keyword
== RID_SIZEOF
28082 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28083 return cp_parser_sizeof_pack (parser
);
28085 /* Types cannot be defined in a `sizeof' expression. Save away the
28087 saved_message
= parser
->type_definition_forbidden_message
;
28088 /* And create the new one. */
28089 tmp
= concat ("types may not be defined in %<",
28090 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28091 "%> expressions", NULL
);
28092 parser
->type_definition_forbidden_message
= tmp
;
28094 /* The restrictions on constant-expressions do not apply inside
28095 sizeof expressions. */
28096 saved_integral_constant_expression_p
28097 = parser
->integral_constant_expression_p
;
28098 saved_non_integral_constant_expression_p
28099 = parser
->non_integral_constant_expression_p
;
28100 parser
->integral_constant_expression_p
= false;
28102 /* Do not actually evaluate the expression. */
28103 ++cp_unevaluated_operand
;
28104 ++c_inhibit_evaluation_warnings
;
28105 /* If it's a `(', then we might be looking at the type-id
28107 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28109 tree type
= NULL_TREE
;
28111 /* We can't be sure yet whether we're looking at a type-id or an
28113 cp_parser_parse_tentatively (parser
);
28115 matching_parens parens
;
28116 parens
.consume_open (parser
);
28118 /* Note: as a GNU Extension, compound literals are considered
28119 postfix-expressions as they are in C99, so they are valid
28120 arguments to sizeof. See comment in cp_parser_cast_expression
28122 if (cp_parser_compound_literal_p (parser
))
28123 cp_parser_simulate_error (parser
);
28126 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28127 parser
->in_type_id_in_expr_p
= true;
28128 /* Look for the type-id. */
28129 type
= cp_parser_type_id (parser
);
28130 /* Look for the closing `)'. */
28131 parens
.require_close (parser
);
28132 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28135 /* If all went well, then we're done. */
28136 if (cp_parser_parse_definitely (parser
))
28138 cp_decl_specifier_seq decl_specs
;
28140 /* Build a trivial decl-specifier-seq. */
28141 clear_decl_specs (&decl_specs
);
28142 decl_specs
.type
= type
;
28144 /* Call grokdeclarator to figure out what type this is. */
28145 expr
= grokdeclarator (NULL
,
28149 /*attrlist=*/NULL
);
28153 /* If the type-id production did not work out, then we must be
28154 looking at the unary-expression production. */
28156 expr
= cp_parser_unary_expression (parser
);
28158 /* Go back to evaluating expressions. */
28159 --cp_unevaluated_operand
;
28160 --c_inhibit_evaluation_warnings
;
28162 /* Free the message we created. */
28164 /* And restore the old one. */
28165 parser
->type_definition_forbidden_message
= saved_message
;
28166 parser
->integral_constant_expression_p
28167 = saved_integral_constant_expression_p
;
28168 parser
->non_integral_constant_expression_p
28169 = saved_non_integral_constant_expression_p
;
28174 /* If the current declaration has no declarator, return true. */
28177 cp_parser_declares_only_class_p (cp_parser
*parser
)
28179 /* If the next token is a `;' or a `,' then there is no
28181 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28182 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28185 /* Update the DECL_SPECS to reflect the storage class indicated by
28189 cp_parser_set_storage_class (cp_parser
*parser
,
28190 cp_decl_specifier_seq
*decl_specs
,
28194 cp_storage_class storage_class
;
28196 if (parser
->in_unbraced_linkage_specification_p
)
28198 error_at (token
->location
, "invalid use of %qD in linkage specification",
28199 ridpointers
[keyword
]);
28202 else if (decl_specs
->storage_class
!= sc_none
)
28204 decl_specs
->conflicting_specifiers_p
= true;
28208 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28209 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28210 && decl_specs
->gnu_thread_keyword_p
)
28212 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28213 "%<__thread%> before %qD", ridpointers
[keyword
]);
28219 storage_class
= sc_auto
;
28222 storage_class
= sc_register
;
28225 storage_class
= sc_static
;
28228 storage_class
= sc_extern
;
28231 storage_class
= sc_mutable
;
28234 gcc_unreachable ();
28236 decl_specs
->storage_class
= storage_class
;
28237 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28239 /* A storage class specifier cannot be applied alongside a typedef
28240 specifier. If there is a typedef specifier present then set
28241 conflicting_specifiers_p which will trigger an error later
28242 on in grokdeclarator. */
28243 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28244 decl_specs
->conflicting_specifiers_p
= true;
28247 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28248 is true, the type is a class or enum definition. */
28251 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28254 bool type_definition_p
)
28256 decl_specs
->any_specifiers_p
= true;
28258 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28259 (with, for example, in "typedef int wchar_t;") we remember that
28260 this is what happened. In system headers, we ignore these
28261 declarations so that G++ can work with system headers that are not
28263 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28264 && !type_definition_p
28265 && (type_spec
== boolean_type_node
28266 || type_spec
== char16_type_node
28267 || type_spec
== char32_type_node
28268 || type_spec
== wchar_type_node
)
28269 && (decl_specs
->type
28270 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28271 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28272 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28273 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28275 decl_specs
->redefined_builtin_type
= type_spec
;
28276 set_and_check_decl_spec_loc (decl_specs
,
28277 ds_redefined_builtin_type_spec
,
28279 if (!decl_specs
->type
)
28281 decl_specs
->type
= type_spec
;
28282 decl_specs
->type_definition_p
= false;
28283 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28286 else if (decl_specs
->type
)
28287 decl_specs
->multiple_types_p
= true;
28290 decl_specs
->type
= type_spec
;
28291 decl_specs
->type_definition_p
= type_definition_p
;
28292 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28293 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28297 /* True iff TOKEN is the GNU keyword __thread. */
28300 token_is__thread (cp_token
*token
)
28302 gcc_assert (token
->keyword
== RID_THREAD
);
28303 return id_equal (token
->u
.value
, "__thread");
28306 /* Set the location for a declarator specifier and check if it is
28309 DECL_SPECS is the sequence of declarator specifiers onto which to
28312 DS is the single declarator specifier to set which location is to
28313 be set onto the existing sequence of declarators.
28315 LOCATION is the location for the declarator specifier to
28319 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28320 cp_decl_spec ds
, cp_token
*token
)
28322 gcc_assert (ds
< ds_last
);
28324 if (decl_specs
== NULL
)
28327 source_location location
= token
->location
;
28329 if (decl_specs
->locations
[ds
] == 0)
28331 decl_specs
->locations
[ds
] = location
;
28332 if (ds
== ds_thread
)
28333 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28339 if (decl_specs
->locations
[ds_long_long
] != 0)
28340 error_at (location
,
28341 "%<long long long%> is too long for GCC");
28344 decl_specs
->locations
[ds_long_long
] = location
;
28345 pedwarn_cxx98 (location
,
28347 "ISO C++ 1998 does not support %<long long%>");
28350 else if (ds
== ds_thread
)
28352 bool gnu
= token_is__thread (token
);
28353 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28354 error_at (location
,
28355 "both %<__thread%> and %<thread_local%> specified");
28358 gcc_rich_location
richloc (location
);
28359 richloc
.add_fixit_remove ();
28360 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28365 static const char *const decl_spec_names
[] = {
28382 gcc_rich_location
richloc (location
);
28383 richloc
.add_fixit_remove ();
28384 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28389 /* Return true iff the declarator specifier DS is present in the
28390 sequence of declarator specifiers DECL_SPECS. */
28393 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28396 gcc_assert (ds
< ds_last
);
28398 if (decl_specs
== NULL
)
28401 return decl_specs
->locations
[ds
] != 0;
28404 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28405 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28408 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28410 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28413 /* Issue an error message indicating that TOKEN_DESC was expected.
28414 If KEYWORD is true, it indicated this function is called by
28415 cp_parser_require_keword and the required token can only be
28416 a indicated keyword.
28418 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28419 within any error as the location of an "opening" token matching
28420 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28421 RT_CLOSE_PAREN). */
28424 cp_parser_required_error (cp_parser
*parser
,
28425 required_token token_desc
,
28427 location_t matching_location
)
28429 if (cp_parser_simulate_error (parser
))
28432 const char *gmsgid
= NULL
;
28433 switch (token_desc
)
28436 gmsgid
= G_("expected %<new%>");
28439 gmsgid
= G_("expected %<delete%>");
28442 gmsgid
= G_("expected %<return%>");
28445 gmsgid
= G_("expected %<while%>");
28448 gmsgid
= G_("expected %<extern%>");
28450 case RT_STATIC_ASSERT
:
28451 gmsgid
= G_("expected %<static_assert%>");
28454 gmsgid
= G_("expected %<decltype%>");
28457 gmsgid
= G_("expected %<operator%>");
28460 gmsgid
= G_("expected %<class%>");
28463 gmsgid
= G_("expected %<template%>");
28466 gmsgid
= G_("expected %<namespace%>");
28469 gmsgid
= G_("expected %<using%>");
28472 gmsgid
= G_("expected %<asm%>");
28475 gmsgid
= G_("expected %<try%>");
28478 gmsgid
= G_("expected %<catch%>");
28481 gmsgid
= G_("expected %<throw%>");
28484 gmsgid
= G_("expected %<__label__%>");
28487 gmsgid
= G_("expected %<@try%>");
28489 case RT_AT_SYNCHRONIZED
:
28490 gmsgid
= G_("expected %<@synchronized%>");
28493 gmsgid
= G_("expected %<@throw%>");
28495 case RT_TRANSACTION_ATOMIC
:
28496 gmsgid
= G_("expected %<__transaction_atomic%>");
28498 case RT_TRANSACTION_RELAXED
:
28499 gmsgid
= G_("expected %<__transaction_relaxed%>");
28505 if (!gmsgid
&& !keyword
)
28507 switch (token_desc
)
28510 gmsgid
= G_("expected %<;%>");
28512 case RT_OPEN_PAREN
:
28513 gmsgid
= G_("expected %<(%>");
28515 case RT_CLOSE_BRACE
:
28516 gmsgid
= G_("expected %<}%>");
28518 case RT_OPEN_BRACE
:
28519 gmsgid
= G_("expected %<{%>");
28521 case RT_CLOSE_SQUARE
:
28522 gmsgid
= G_("expected %<]%>");
28524 case RT_OPEN_SQUARE
:
28525 gmsgid
= G_("expected %<[%>");
28528 gmsgid
= G_("expected %<,%>");
28531 gmsgid
= G_("expected %<::%>");
28534 gmsgid
= G_("expected %<<%>");
28537 gmsgid
= G_("expected %<>%>");
28540 gmsgid
= G_("expected %<=%>");
28543 gmsgid
= G_("expected %<...%>");
28546 gmsgid
= G_("expected %<*%>");
28549 gmsgid
= G_("expected %<~%>");
28552 gmsgid
= G_("expected %<:%>");
28554 case RT_COLON_SCOPE
:
28555 gmsgid
= G_("expected %<:%> or %<::%>");
28557 case RT_CLOSE_PAREN
:
28558 gmsgid
= G_("expected %<)%>");
28560 case RT_COMMA_CLOSE_PAREN
:
28561 gmsgid
= G_("expected %<,%> or %<)%>");
28563 case RT_PRAGMA_EOL
:
28564 gmsgid
= G_("expected end of line");
28567 gmsgid
= G_("expected identifier");
28570 gmsgid
= G_("expected selection-statement");
28573 gmsgid
= G_("expected iteration-statement");
28576 gmsgid
= G_("expected jump-statement");
28579 gmsgid
= G_("expected class-key");
28581 case RT_CLASS_TYPENAME_TEMPLATE
:
28582 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28585 gcc_unreachable ();
28590 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28594 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28595 issue an error message indicating that TOKEN_DESC was expected.
28597 Returns the token consumed, if the token had the appropriate type.
28598 Otherwise, returns NULL.
28600 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28601 within any error as the location of an "opening" token matching
28602 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28603 RT_CLOSE_PAREN). */
28606 cp_parser_require (cp_parser
* parser
,
28607 enum cpp_ttype type
,
28608 required_token token_desc
,
28609 location_t matching_location
)
28611 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28612 return cp_lexer_consume_token (parser
->lexer
);
28615 /* Output the MESSAGE -- unless we're parsing tentatively. */
28616 if (!cp_parser_simulate_error (parser
))
28617 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28618 matching_location
);
28623 /* An error message is produced if the next token is not '>'.
28624 All further tokens are skipped until the desired token is
28625 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28628 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28630 /* Current level of '< ... >'. */
28631 unsigned level
= 0;
28632 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28633 unsigned nesting_depth
= 0;
28635 /* Are we ready, yet? If not, issue error message. */
28636 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28639 /* Skip tokens until the desired token is found. */
28642 /* Peek at the next token. */
28643 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28646 if (!nesting_depth
)
28651 if (cxx_dialect
== cxx98
)
28652 /* C++0x views the `>>' operator as two `>' tokens, but
28655 else if (!nesting_depth
&& level
-- == 0)
28657 /* We've hit a `>>' where the first `>' closes the
28658 template argument list, and the second `>' is
28659 spurious. Just consume the `>>' and stop; we've
28660 already produced at least one error. */
28661 cp_lexer_consume_token (parser
->lexer
);
28664 /* Fall through for C++0x, so we handle the second `>' in
28666 gcc_fallthrough ();
28669 if (!nesting_depth
&& level
-- == 0)
28671 /* We've reached the token we want, consume it and stop. */
28672 cp_lexer_consume_token (parser
->lexer
);
28677 case CPP_OPEN_PAREN
:
28678 case CPP_OPEN_SQUARE
:
28682 case CPP_CLOSE_PAREN
:
28683 case CPP_CLOSE_SQUARE
:
28684 if (nesting_depth
-- == 0)
28689 case CPP_PRAGMA_EOL
:
28690 case CPP_SEMICOLON
:
28691 case CPP_OPEN_BRACE
:
28692 case CPP_CLOSE_BRACE
:
28693 /* The '>' was probably forgotten, don't look further. */
28700 /* Consume this token. */
28701 cp_lexer_consume_token (parser
->lexer
);
28705 /* If the next token is the indicated keyword, consume it. Otherwise,
28706 issue an error message indicating that TOKEN_DESC was expected.
28708 Returns the token consumed, if the token had the appropriate type.
28709 Otherwise, returns NULL. */
28712 cp_parser_require_keyword (cp_parser
* parser
,
28714 required_token token_desc
)
28716 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28718 if (token
&& token
->keyword
!= keyword
)
28720 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28728 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28729 function-definition. */
28732 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28734 return (/* An ordinary function-body begins with an `{'. */
28735 token
->type
== CPP_OPEN_BRACE
28736 /* A ctor-initializer begins with a `:'. */
28737 || token
->type
== CPP_COLON
28738 /* A function-try-block begins with `try'. */
28739 || token
->keyword
== RID_TRY
28740 /* A function-transaction-block begins with `__transaction_atomic'
28741 or `__transaction_relaxed'. */
28742 || token
->keyword
== RID_TRANSACTION_ATOMIC
28743 || token
->keyword
== RID_TRANSACTION_RELAXED
28744 /* The named return value extension begins with `return'. */
28745 || token
->keyword
== RID_RETURN
);
28748 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28752 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28756 token
= cp_lexer_peek_token (parser
->lexer
);
28757 return (token
->type
== CPP_OPEN_BRACE
28758 || (token
->type
== CPP_COLON
28759 && !parser
->colon_doesnt_start_class_def_p
));
28762 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28763 C++0x) ending a template-argument. */
28766 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28770 token
= cp_lexer_peek_token (parser
->lexer
);
28771 return (token
->type
== CPP_COMMA
28772 || token
->type
== CPP_GREATER
28773 || token
->type
== CPP_ELLIPSIS
28774 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28777 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28778 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28781 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28786 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28787 if (token
->type
== CPP_LESS
)
28789 /* Check for the sequence `<::' in the original code. It would be lexed as
28790 `[:', where `[' is a digraph, and there is no whitespace before
28792 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28795 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28796 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28802 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28803 or none_type otherwise. */
28805 static enum tag_types
28806 cp_parser_token_is_class_key (cp_token
* token
)
28808 switch (token
->keyword
)
28813 return record_type
;
28822 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28823 or none_type otherwise or if the token is null. */
28825 static enum tag_types
28826 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28831 switch (token
->keyword
)
28836 return typename_type
;
28843 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28846 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28848 if (type
== error_mark_node
)
28850 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28852 if (permerror (input_location
, "%qs tag used in naming %q#T",
28853 class_key
== union_type
? "union"
28854 : class_key
== record_type
? "struct" : "class",
28856 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28857 "%q#T was previously declared here", type
);
28861 /* Issue an error message if DECL is redeclared with different
28862 access than its original declaration [class.access.spec/3].
28863 This applies to nested classes, nested class templates and
28864 enumerations [class.mem/1]. */
28867 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28870 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28871 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28874 if ((TREE_PRIVATE (decl
)
28875 != (current_access_specifier
== access_private_node
))
28876 || (TREE_PROTECTED (decl
)
28877 != (current_access_specifier
== access_protected_node
)))
28878 error_at (location
, "%qD redeclared with different access", decl
);
28881 /* Look for the `template' keyword, as a syntactic disambiguator.
28882 Return TRUE iff it is present, in which case it will be
28886 cp_parser_optional_template_keyword (cp_parser
*parser
)
28888 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28890 /* In C++98 the `template' keyword can only be used within templates;
28891 outside templates the parser can always figure out what is a
28892 template and what is not. In C++11, per the resolution of DR 468,
28893 `template' is allowed in cases where it is not strictly necessary. */
28894 if (!processing_template_decl
28895 && pedantic
&& cxx_dialect
== cxx98
)
28897 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28898 pedwarn (token
->location
, OPT_Wpedantic
,
28899 "in C++98 %<template%> (as a disambiguator) is only "
28900 "allowed within templates");
28901 /* If this part of the token stream is rescanned, the same
28902 error message would be generated. So, we purge the token
28903 from the stream. */
28904 cp_lexer_purge_token (parser
->lexer
);
28909 /* Consume the `template' keyword. */
28910 cp_lexer_consume_token (parser
->lexer
);
28917 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28918 set PARSER->SCOPE, and perform other related actions. */
28921 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28923 struct tree_check
*check_value
;
28925 /* Get the stored value. */
28926 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28927 /* Set the scope from the stored value. */
28928 parser
->scope
= saved_checks_value (check_value
);
28929 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28930 parser
->object_scope
= NULL_TREE
;
28933 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28934 encounter the end of a block before what we were looking for. */
28937 cp_parser_cache_group (cp_parser
*parser
,
28938 enum cpp_ttype end
,
28943 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28945 /* Abort a parenthesized expression if we encounter a semicolon. */
28946 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28947 && token
->type
== CPP_SEMICOLON
)
28949 /* If we've reached the end of the file, stop. */
28950 if (token
->type
== CPP_EOF
28951 || (end
!= CPP_PRAGMA_EOL
28952 && token
->type
== CPP_PRAGMA_EOL
))
28954 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28955 /* We've hit the end of an enclosing block, so there's been some
28956 kind of syntax error. */
28959 /* Consume the token. */
28960 cp_lexer_consume_token (parser
->lexer
);
28961 /* See if it starts a new group. */
28962 if (token
->type
== CPP_OPEN_BRACE
)
28964 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28965 /* In theory this should probably check end == '}', but
28966 cp_parser_save_member_function_body needs it to exit
28967 after either '}' or ')' when called with ')'. */
28971 else if (token
->type
== CPP_OPEN_PAREN
)
28973 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28974 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28977 else if (token
->type
== CPP_PRAGMA
)
28978 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28979 else if (token
->type
== end
)
28984 /* Like above, for caching a default argument or NSDMI. Both of these are
28985 terminated by a non-nested comma, but it can be unclear whether or not a
28986 comma is nested in a template argument list unless we do more parsing.
28987 In order to handle this ambiguity, when we encounter a ',' after a '<'
28988 we try to parse what follows as a parameter-declaration-list (in the
28989 case of a default argument) or a member-declarator (in the case of an
28990 NSDMI). If that succeeds, then we stop caching. */
28993 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28995 unsigned depth
= 0;
28996 int maybe_template_id
= 0;
28997 cp_token
*first_token
;
28999 tree default_argument
;
29001 /* Add tokens until we have processed the entire default
29002 argument. We add the range [first_token, token). */
29003 first_token
= cp_lexer_peek_token (parser
->lexer
);
29004 if (first_token
->type
== CPP_OPEN_BRACE
)
29006 /* For list-initialization, this is straightforward. */
29007 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29008 token
= cp_lexer_peek_token (parser
->lexer
);
29014 /* Peek at the next token. */
29015 token
= cp_lexer_peek_token (parser
->lexer
);
29016 /* What we do depends on what token we have. */
29017 switch (token
->type
)
29019 /* In valid code, a default argument must be
29020 immediately followed by a `,' `)', or `...'. */
29022 if (depth
== 0 && maybe_template_id
)
29024 /* If we've seen a '<', we might be in a
29025 template-argument-list. Until Core issue 325 is
29026 resolved, we don't know how this situation ought
29027 to be handled, so try to DTRT. We check whether
29028 what comes after the comma is a valid parameter
29029 declaration list. If it is, then the comma ends
29030 the default argument; otherwise the default
29031 argument continues. */
29032 bool error
= false;
29035 /* Set ITALP so cp_parser_parameter_declaration_list
29036 doesn't decide to commit to this parse. */
29037 bool saved_italp
= parser
->in_template_argument_list_p
;
29038 parser
->in_template_argument_list_p
= true;
29040 cp_parser_parse_tentatively (parser
);
29044 /* Parse declarators until we reach a non-comma or
29045 somthing that cannot be an initializer.
29046 Just checking whether we're looking at a single
29047 declarator is insufficient. Consider:
29048 int var = tuple<T,U>::x;
29049 The template parameter 'U' looks exactly like a
29053 int ctor_dtor_or_conv_p
;
29054 cp_lexer_consume_token (parser
->lexer
);
29055 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29056 &ctor_dtor_or_conv_p
,
29057 /*parenthesized_p=*/NULL
,
29059 /*friend_p=*/false);
29060 peek
= cp_lexer_peek_token (parser
->lexer
);
29061 if (cp_parser_error_occurred (parser
))
29064 while (peek
->type
== CPP_COMMA
);
29065 /* If we met an '=' or ';' then the original comma
29066 was the end of the NSDMI. Otherwise assume
29067 we're still in the NSDMI. */
29068 error
= (peek
->type
!= CPP_EQ
29069 && peek
->type
!= CPP_SEMICOLON
);
29073 cp_lexer_consume_token (parser
->lexer
);
29074 begin_scope (sk_function_parms
, NULL_TREE
);
29075 if (cp_parser_parameter_declaration_list (parser
)
29076 == error_mark_node
)
29078 pop_bindings_and_leave_scope ();
29080 if (!cp_parser_error_occurred (parser
) && !error
)
29082 cp_parser_abort_tentative_parse (parser
);
29084 parser
->in_template_argument_list_p
= saved_italp
;
29088 case CPP_CLOSE_PAREN
:
29090 /* If we run into a non-nested `;', `}', or `]',
29091 then the code is invalid -- but the default
29092 argument is certainly over. */
29093 case CPP_SEMICOLON
:
29094 case CPP_CLOSE_BRACE
:
29095 case CPP_CLOSE_SQUARE
:
29097 /* Handle correctly int n = sizeof ... ( p ); */
29098 && token
->type
!= CPP_ELLIPSIS
)
29100 /* Update DEPTH, if necessary. */
29101 else if (token
->type
== CPP_CLOSE_PAREN
29102 || token
->type
== CPP_CLOSE_BRACE
29103 || token
->type
== CPP_CLOSE_SQUARE
)
29107 case CPP_OPEN_PAREN
:
29108 case CPP_OPEN_SQUARE
:
29109 case CPP_OPEN_BRACE
:
29115 /* This might be the comparison operator, or it might
29116 start a template argument list. */
29117 ++maybe_template_id
;
29121 if (cxx_dialect
== cxx98
)
29123 /* Fall through for C++0x, which treats the `>>'
29124 operator like two `>' tokens in certain
29126 gcc_fallthrough ();
29131 /* This might be an operator, or it might close a
29132 template argument list. But if a previous '<'
29133 started a template argument list, this will have
29134 closed it, so we can't be in one anymore. */
29135 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29136 if (maybe_template_id
< 0)
29137 maybe_template_id
= 0;
29141 /* If we run out of tokens, issue an error message. */
29143 case CPP_PRAGMA_EOL
:
29144 error_at (token
->location
, "file ends in default argument");
29145 return error_mark_node
;
29149 /* In these cases, we should look for template-ids.
29150 For example, if the default argument is
29151 `X<int, double>()', we need to do name lookup to
29152 figure out whether or not `X' is a template; if
29153 so, the `,' does not end the default argument.
29155 That is not yet done. */
29162 /* If we've reached the end, stop. */
29166 /* Add the token to the token block. */
29167 token
= cp_lexer_consume_token (parser
->lexer
);
29170 /* Create a DEFAULT_ARG to represent the unparsed default
29172 default_argument
= make_node (DEFAULT_ARG
);
29173 DEFARG_TOKENS (default_argument
)
29174 = cp_token_cache_new (first_token
, token
);
29175 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29177 return default_argument
;
29180 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29183 defarg_location (tree default_argument
)
29185 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29186 location_t start
= tokens
->first
->location
;
29187 location_t end
= tokens
->last
->location
;
29188 return make_location (start
, start
, end
);
29191 /* Begin parsing tentatively. We always save tokens while parsing
29192 tentatively so that if the tentative parsing fails we can restore the
29196 cp_parser_parse_tentatively (cp_parser
* parser
)
29198 /* Enter a new parsing context. */
29199 parser
->context
= cp_parser_context_new (parser
->context
);
29200 /* Begin saving tokens. */
29201 cp_lexer_save_tokens (parser
->lexer
);
29202 /* In order to avoid repetitive access control error messages,
29203 access checks are queued up until we are no longer parsing
29205 push_deferring_access_checks (dk_deferred
);
29208 /* Commit to the currently active tentative parse. */
29211 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29213 cp_parser_context
*context
;
29216 /* Mark all of the levels as committed. */
29217 lexer
= parser
->lexer
;
29218 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29220 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29222 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29223 while (!cp_lexer_saving_tokens (lexer
))
29224 lexer
= lexer
->next
;
29225 cp_lexer_commit_tokens (lexer
);
29229 /* Commit to the topmost currently active tentative parse.
29231 Note that this function shouldn't be called when there are
29232 irreversible side-effects while in a tentative state. For
29233 example, we shouldn't create a permanent entry in the symbol
29234 table, or issue an error message that might not apply if the
29235 tentative parse is aborted. */
29238 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29240 cp_parser_context
*context
= parser
->context
;
29241 cp_lexer
*lexer
= parser
->lexer
;
29245 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29247 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29249 while (!cp_lexer_saving_tokens (lexer
))
29250 lexer
= lexer
->next
;
29251 cp_lexer_commit_tokens (lexer
);
29255 /* Abort the currently active tentative parse. All consumed tokens
29256 will be rolled back, and no diagnostics will be issued. */
29259 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29261 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29262 || errorcount
> 0);
29263 cp_parser_simulate_error (parser
);
29264 /* Now, pretend that we want to see if the construct was
29265 successfully parsed. */
29266 cp_parser_parse_definitely (parser
);
29269 /* Stop parsing tentatively. If a parse error has occurred, restore the
29270 token stream. Otherwise, commit to the tokens we have consumed.
29271 Returns true if no error occurred; false otherwise. */
29274 cp_parser_parse_definitely (cp_parser
* parser
)
29276 bool error_occurred
;
29277 cp_parser_context
*context
;
29279 /* Remember whether or not an error occurred, since we are about to
29280 destroy that information. */
29281 error_occurred
= cp_parser_error_occurred (parser
);
29282 /* Remove the topmost context from the stack. */
29283 context
= parser
->context
;
29284 parser
->context
= context
->next
;
29285 /* If no parse errors occurred, commit to the tentative parse. */
29286 if (!error_occurred
)
29288 /* Commit to the tokens read tentatively, unless that was
29290 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29291 cp_lexer_commit_tokens (parser
->lexer
);
29293 pop_to_parent_deferring_access_checks ();
29295 /* Otherwise, if errors occurred, roll back our state so that things
29296 are just as they were before we began the tentative parse. */
29299 cp_lexer_rollback_tokens (parser
->lexer
);
29300 pop_deferring_access_checks ();
29302 /* Add the context to the front of the free list. */
29303 context
->next
= cp_parser_context_free_list
;
29304 cp_parser_context_free_list
= context
;
29306 return !error_occurred
;
29309 /* Returns true if we are parsing tentatively and are not committed to
29310 this tentative parse. */
29313 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29315 return (cp_parser_parsing_tentatively (parser
)
29316 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29319 /* Returns nonzero iff an error has occurred during the most recent
29320 tentative parse. */
29323 cp_parser_error_occurred (cp_parser
* parser
)
29325 return (cp_parser_parsing_tentatively (parser
)
29326 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29329 /* Returns nonzero if GNU extensions are allowed. */
29332 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29334 return parser
->allow_gnu_extensions_p
;
29337 /* Objective-C++ Productions */
29340 /* Parse an Objective-C expression, which feeds into a primary-expression
29344 objc-message-expression
29345 objc-string-literal
29346 objc-encode-expression
29347 objc-protocol-expression
29348 objc-selector-expression
29350 Returns a tree representation of the expression. */
29353 cp_parser_objc_expression (cp_parser
* parser
)
29355 /* Try to figure out what kind of declaration is present. */
29356 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29360 case CPP_OPEN_SQUARE
:
29361 return cp_parser_objc_message_expression (parser
);
29363 case CPP_OBJC_STRING
:
29364 kwd
= cp_lexer_consume_token (parser
->lexer
);
29365 return objc_build_string_object (kwd
->u
.value
);
29368 switch (kwd
->keyword
)
29370 case RID_AT_ENCODE
:
29371 return cp_parser_objc_encode_expression (parser
);
29373 case RID_AT_PROTOCOL
:
29374 return cp_parser_objc_protocol_expression (parser
);
29376 case RID_AT_SELECTOR
:
29377 return cp_parser_objc_selector_expression (parser
);
29384 error_at (kwd
->location
,
29385 "misplaced %<@%D%> Objective-C++ construct",
29387 cp_parser_skip_to_end_of_block_or_statement (parser
);
29390 return error_mark_node
;
29393 /* Parse an Objective-C message expression.
29395 objc-message-expression:
29396 [ objc-message-receiver objc-message-args ]
29398 Returns a representation of an Objective-C message. */
29401 cp_parser_objc_message_expression (cp_parser
* parser
)
29403 tree receiver
, messageargs
;
29405 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29406 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29407 receiver
= cp_parser_objc_message_receiver (parser
);
29408 messageargs
= cp_parser_objc_message_args (parser
);
29409 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29410 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29412 tree result
= objc_build_message_expr (receiver
, messageargs
);
29414 /* Construct a location e.g.
29417 ranging from the '[' to the ']', with the caret at the start. */
29418 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29419 protected_set_expr_location (result
, combined_loc
);
29424 /* Parse an objc-message-receiver.
29426 objc-message-receiver:
29428 simple-type-specifier
29430 Returns a representation of the type or expression. */
29433 cp_parser_objc_message_receiver (cp_parser
* parser
)
29437 /* An Objective-C message receiver may be either (1) a type
29438 or (2) an expression. */
29439 cp_parser_parse_tentatively (parser
);
29440 rcv
= cp_parser_expression (parser
);
29442 /* If that worked out, fine. */
29443 if (cp_parser_parse_definitely (parser
))
29446 cp_parser_parse_tentatively (parser
);
29447 rcv
= cp_parser_simple_type_specifier (parser
,
29448 /*decl_specs=*/NULL
,
29449 CP_PARSER_FLAGS_NONE
);
29451 if (cp_parser_parse_definitely (parser
))
29452 return objc_get_class_reference (rcv
);
29454 cp_parser_error (parser
, "objective-c++ message receiver expected");
29455 return error_mark_node
;
29458 /* Parse the arguments and selectors comprising an Objective-C message.
29463 objc-selector-args , objc-comma-args
29465 objc-selector-args:
29466 objc-selector [opt] : assignment-expression
29467 objc-selector-args objc-selector [opt] : assignment-expression
29470 assignment-expression
29471 objc-comma-args , assignment-expression
29473 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29474 selector arguments and TREE_VALUE containing a list of comma
29478 cp_parser_objc_message_args (cp_parser
* parser
)
29480 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29481 bool maybe_unary_selector_p
= true;
29482 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29484 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29486 tree selector
= NULL_TREE
, arg
;
29488 if (token
->type
!= CPP_COLON
)
29489 selector
= cp_parser_objc_selector (parser
);
29491 /* Detect if we have a unary selector. */
29492 if (maybe_unary_selector_p
29493 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29494 return build_tree_list (selector
, NULL_TREE
);
29496 maybe_unary_selector_p
= false;
29497 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29498 arg
= cp_parser_assignment_expression (parser
);
29501 = chainon (sel_args
,
29502 build_tree_list (selector
, arg
));
29504 token
= cp_lexer_peek_token (parser
->lexer
);
29507 /* Handle non-selector arguments, if any. */
29508 while (token
->type
== CPP_COMMA
)
29512 cp_lexer_consume_token (parser
->lexer
);
29513 arg
= cp_parser_assignment_expression (parser
);
29516 = chainon (addl_args
,
29517 build_tree_list (NULL_TREE
, arg
));
29519 token
= cp_lexer_peek_token (parser
->lexer
);
29522 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29524 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29525 return build_tree_list (error_mark_node
, error_mark_node
);
29528 return build_tree_list (sel_args
, addl_args
);
29531 /* Parse an Objective-C encode expression.
29533 objc-encode-expression:
29534 @encode objc-typename
29536 Returns an encoded representation of the type argument. */
29539 cp_parser_objc_encode_expression (cp_parser
* parser
)
29543 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29545 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29546 matching_parens parens
;
29547 parens
.require_open (parser
);
29548 token
= cp_lexer_peek_token (parser
->lexer
);
29549 type
= complete_type (cp_parser_type_id (parser
));
29550 parens
.require_close (parser
);
29554 error_at (token
->location
,
29555 "%<@encode%> must specify a type as an argument");
29556 return error_mark_node
;
29559 /* This happens if we find @encode(T) (where T is a template
29560 typename or something dependent on a template typename) when
29561 parsing a template. In that case, we can't compile it
29562 immediately, but we rather create an AT_ENCODE_EXPR which will
29563 need to be instantiated when the template is used.
29565 if (dependent_type_p (type
))
29567 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29568 TREE_READONLY (value
) = 1;
29573 /* Build a location of the form:
29576 with caret==start at the @ token, finishing at the close paren. */
29577 location_t combined_loc
29578 = make_location (start_loc
, start_loc
,
29579 cp_lexer_previous_token (parser
->lexer
)->location
);
29581 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29584 /* Parse an Objective-C @defs expression. */
29587 cp_parser_objc_defs_expression (cp_parser
*parser
)
29591 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29592 matching_parens parens
;
29593 parens
.require_open (parser
);
29594 name
= cp_parser_identifier (parser
);
29595 parens
.require_close (parser
);
29597 return objc_get_class_ivars (name
);
29600 /* Parse an Objective-C protocol expression.
29602 objc-protocol-expression:
29603 @protocol ( identifier )
29605 Returns a representation of the protocol expression. */
29608 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29611 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29613 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29614 matching_parens parens
;
29615 parens
.require_open (parser
);
29616 proto
= cp_parser_identifier (parser
);
29617 parens
.require_close (parser
);
29619 /* Build a location of the form:
29622 with caret==start at the @ token, finishing at the close paren. */
29623 location_t combined_loc
29624 = make_location (start_loc
, start_loc
,
29625 cp_lexer_previous_token (parser
->lexer
)->location
);
29626 tree result
= objc_build_protocol_expr (proto
);
29627 protected_set_expr_location (result
, combined_loc
);
29631 /* Parse an Objective-C selector expression.
29633 objc-selector-expression:
29634 @selector ( objc-method-signature )
29636 objc-method-signature:
29642 objc-selector-seq objc-selector :
29644 Returns a representation of the method selector. */
29647 cp_parser_objc_selector_expression (cp_parser
* parser
)
29649 tree sel_seq
= NULL_TREE
;
29650 bool maybe_unary_selector_p
= true;
29652 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29654 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29655 matching_parens parens
;
29656 parens
.require_open (parser
);
29657 token
= cp_lexer_peek_token (parser
->lexer
);
29659 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29660 || token
->type
== CPP_SCOPE
)
29662 tree selector
= NULL_TREE
;
29664 if (token
->type
!= CPP_COLON
29665 || token
->type
== CPP_SCOPE
)
29666 selector
= cp_parser_objc_selector (parser
);
29668 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29669 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29671 /* Detect if we have a unary selector. */
29672 if (maybe_unary_selector_p
)
29674 sel_seq
= selector
;
29675 goto finish_selector
;
29679 cp_parser_error (parser
, "expected %<:%>");
29682 maybe_unary_selector_p
= false;
29683 token
= cp_lexer_consume_token (parser
->lexer
);
29685 if (token
->type
== CPP_SCOPE
)
29688 = chainon (sel_seq
,
29689 build_tree_list (selector
, NULL_TREE
));
29691 = chainon (sel_seq
,
29692 build_tree_list (NULL_TREE
, NULL_TREE
));
29696 = chainon (sel_seq
,
29697 build_tree_list (selector
, NULL_TREE
));
29699 token
= cp_lexer_peek_token (parser
->lexer
);
29703 parens
.require_close (parser
);
29706 /* Build a location of the form:
29709 with caret==start at the @ token, finishing at the close paren. */
29710 location_t combined_loc
29711 = make_location (loc
, loc
,
29712 cp_lexer_previous_token (parser
->lexer
)->location
);
29713 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29714 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29715 protected_set_expr_location (result
, combined_loc
);
29719 /* Parse a list of identifiers.
29721 objc-identifier-list:
29723 objc-identifier-list , identifier
29725 Returns a TREE_LIST of identifier nodes. */
29728 cp_parser_objc_identifier_list (cp_parser
* parser
)
29734 identifier
= cp_parser_identifier (parser
);
29735 if (identifier
== error_mark_node
)
29736 return error_mark_node
;
29738 list
= build_tree_list (NULL_TREE
, identifier
);
29739 sep
= cp_lexer_peek_token (parser
->lexer
);
29741 while (sep
->type
== CPP_COMMA
)
29743 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29744 identifier
= cp_parser_identifier (parser
);
29745 if (identifier
== error_mark_node
)
29748 list
= chainon (list
, build_tree_list (NULL_TREE
,
29750 sep
= cp_lexer_peek_token (parser
->lexer
);
29756 /* Parse an Objective-C alias declaration.
29758 objc-alias-declaration:
29759 @compatibility_alias identifier identifier ;
29761 This function registers the alias mapping with the Objective-C front end.
29762 It returns nothing. */
29765 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29769 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29770 alias
= cp_parser_identifier (parser
);
29771 orig
= cp_parser_identifier (parser
);
29772 objc_declare_alias (alias
, orig
);
29773 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29776 /* Parse an Objective-C class forward-declaration.
29778 objc-class-declaration:
29779 @class objc-identifier-list ;
29781 The function registers the forward declarations with the Objective-C
29782 front end. It returns nothing. */
29785 cp_parser_objc_class_declaration (cp_parser
* parser
)
29787 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29792 id
= cp_parser_identifier (parser
);
29793 if (id
== error_mark_node
)
29796 objc_declare_class (id
);
29798 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29799 cp_lexer_consume_token (parser
->lexer
);
29803 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29806 /* Parse a list of Objective-C protocol references.
29808 objc-protocol-refs-opt:
29809 objc-protocol-refs [opt]
29811 objc-protocol-refs:
29812 < objc-identifier-list >
29814 Returns a TREE_LIST of identifiers, if any. */
29817 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29819 tree protorefs
= NULL_TREE
;
29821 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29823 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29824 protorefs
= cp_parser_objc_identifier_list (parser
);
29825 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29831 /* Parse a Objective-C visibility specification. */
29834 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29836 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29838 switch (vis
->keyword
)
29840 case RID_AT_PRIVATE
:
29841 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29843 case RID_AT_PROTECTED
:
29844 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29846 case RID_AT_PUBLIC
:
29847 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29849 case RID_AT_PACKAGE
:
29850 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29856 /* Eat '@private'/'@protected'/'@public'. */
29857 cp_lexer_consume_token (parser
->lexer
);
29860 /* Parse an Objective-C method type. Return 'true' if it is a class
29861 (+) method, and 'false' if it is an instance (-) method. */
29864 cp_parser_objc_method_type (cp_parser
* parser
)
29866 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29872 /* Parse an Objective-C protocol qualifier. */
29875 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29877 tree quals
= NULL_TREE
, node
;
29878 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29880 node
= token
->u
.value
;
29882 while (node
&& identifier_p (node
)
29883 && (node
== ridpointers
[(int) RID_IN
]
29884 || node
== ridpointers
[(int) RID_OUT
]
29885 || node
== ridpointers
[(int) RID_INOUT
]
29886 || node
== ridpointers
[(int) RID_BYCOPY
]
29887 || node
== ridpointers
[(int) RID_BYREF
]
29888 || node
== ridpointers
[(int) RID_ONEWAY
]))
29890 quals
= tree_cons (NULL_TREE
, node
, quals
);
29891 cp_lexer_consume_token (parser
->lexer
);
29892 token
= cp_lexer_peek_token (parser
->lexer
);
29893 node
= token
->u
.value
;
29899 /* Parse an Objective-C typename. */
29902 cp_parser_objc_typename (cp_parser
* parser
)
29904 tree type_name
= NULL_TREE
;
29906 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29908 tree proto_quals
, cp_type
= NULL_TREE
;
29910 matching_parens parens
;
29911 parens
.consume_open (parser
); /* Eat '('. */
29912 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29914 /* An ObjC type name may consist of just protocol qualifiers, in which
29915 case the type shall default to 'id'. */
29916 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29918 cp_type
= cp_parser_type_id (parser
);
29920 /* If the type could not be parsed, an error has already
29921 been produced. For error recovery, behave as if it had
29922 not been specified, which will use the default type
29924 if (cp_type
== error_mark_node
)
29926 cp_type
= NULL_TREE
;
29927 /* We need to skip to the closing parenthesis as
29928 cp_parser_type_id() does not seem to do it for
29930 cp_parser_skip_to_closing_parenthesis (parser
,
29931 /*recovering=*/true,
29932 /*or_comma=*/false,
29933 /*consume_paren=*/false);
29937 parens
.require_close (parser
);
29938 type_name
= build_tree_list (proto_quals
, cp_type
);
29944 /* Check to see if TYPE refers to an Objective-C selector name. */
29947 cp_parser_objc_selector_p (enum cpp_ttype type
)
29949 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29950 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29951 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29952 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29953 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29956 /* Parse an Objective-C selector. */
29959 cp_parser_objc_selector (cp_parser
* parser
)
29961 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29963 if (!cp_parser_objc_selector_p (token
->type
))
29965 error_at (token
->location
, "invalid Objective-C++ selector name");
29966 return error_mark_node
;
29969 /* C++ operator names are allowed to appear in ObjC selectors. */
29970 switch (token
->type
)
29972 case CPP_AND_AND
: return get_identifier ("and");
29973 case CPP_AND_EQ
: return get_identifier ("and_eq");
29974 case CPP_AND
: return get_identifier ("bitand");
29975 case CPP_OR
: return get_identifier ("bitor");
29976 case CPP_COMPL
: return get_identifier ("compl");
29977 case CPP_NOT
: return get_identifier ("not");
29978 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29979 case CPP_OR_OR
: return get_identifier ("or");
29980 case CPP_OR_EQ
: return get_identifier ("or_eq");
29981 case CPP_XOR
: return get_identifier ("xor");
29982 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29983 default: return token
->u
.value
;
29987 /* Parse an Objective-C params list. */
29990 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29992 tree params
= NULL_TREE
;
29993 bool maybe_unary_selector_p
= true;
29994 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29996 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29998 tree selector
= NULL_TREE
, type_name
, identifier
;
29999 tree parm_attr
= NULL_TREE
;
30001 if (token
->keyword
== RID_ATTRIBUTE
)
30004 if (token
->type
!= CPP_COLON
)
30005 selector
= cp_parser_objc_selector (parser
);
30007 /* Detect if we have a unary selector. */
30008 if (maybe_unary_selector_p
30009 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30011 params
= selector
; /* Might be followed by attributes. */
30015 maybe_unary_selector_p
= false;
30016 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30018 /* Something went quite wrong. There should be a colon
30019 here, but there is not. Stop parsing parameters. */
30022 type_name
= cp_parser_objc_typename (parser
);
30023 /* New ObjC allows attributes on parameters too. */
30024 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30025 parm_attr
= cp_parser_attributes_opt (parser
);
30026 identifier
= cp_parser_identifier (parser
);
30030 objc_build_keyword_decl (selector
,
30035 token
= cp_lexer_peek_token (parser
->lexer
);
30038 if (params
== NULL_TREE
)
30040 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30041 return error_mark_node
;
30044 /* We allow tail attributes for the method. */
30045 if (token
->keyword
== RID_ATTRIBUTE
)
30047 *attributes
= cp_parser_attributes_opt (parser
);
30048 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30049 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30051 cp_parser_error (parser
,
30052 "method attributes must be specified at the end");
30053 return error_mark_node
;
30056 if (params
== NULL_TREE
)
30058 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30059 return error_mark_node
;
30064 /* Parse the non-keyword Objective-C params. */
30067 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30070 tree params
= make_node (TREE_LIST
);
30071 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30072 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30074 while (token
->type
== CPP_COMMA
)
30076 cp_parameter_declarator
*parmdecl
;
30079 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30080 token
= cp_lexer_peek_token (parser
->lexer
);
30082 if (token
->type
== CPP_ELLIPSIS
)
30084 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30086 token
= cp_lexer_peek_token (parser
->lexer
);
30090 /* TODO: parse attributes for tail parameters. */
30091 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30092 parm
= grokdeclarator (parmdecl
->declarator
,
30093 &parmdecl
->decl_specifiers
,
30094 PARM
, /*initialized=*/0,
30095 /*attrlist=*/NULL
);
30097 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30098 token
= cp_lexer_peek_token (parser
->lexer
);
30101 /* We allow tail attributes for the method. */
30102 if (token
->keyword
== RID_ATTRIBUTE
)
30104 if (*attributes
== NULL_TREE
)
30106 *attributes
= cp_parser_attributes_opt (parser
);
30107 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30108 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30112 /* We have an error, but parse the attributes, so that we can
30114 *attributes
= cp_parser_attributes_opt (parser
);
30116 cp_parser_error (parser
,
30117 "method attributes must be specified at the end");
30118 return error_mark_node
;
30124 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30127 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30129 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30131 /* If the next token is `extern' and the following token is a string
30132 literal, then we have a linkage specification. */
30133 if (token
->keyword
== RID_EXTERN
30134 && cp_parser_is_pure_string_literal
30135 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30136 cp_parser_linkage_specification (parser
);
30137 /* Handle #pragma, if any. */
30138 else if (token
->type
== CPP_PRAGMA
)
30139 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30140 /* Allow stray semicolons. */
30141 else if (token
->type
== CPP_SEMICOLON
)
30142 cp_lexer_consume_token (parser
->lexer
);
30143 /* Mark methods as optional or required, when building protocols. */
30144 else if (token
->keyword
== RID_AT_OPTIONAL
)
30146 cp_lexer_consume_token (parser
->lexer
);
30147 objc_set_method_opt (true);
30149 else if (token
->keyword
== RID_AT_REQUIRED
)
30151 cp_lexer_consume_token (parser
->lexer
);
30152 objc_set_method_opt (false);
30154 else if (token
->keyword
== RID_NAMESPACE
)
30155 cp_parser_namespace_definition (parser
);
30156 /* Other stray characters must generate errors. */
30157 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30159 cp_lexer_consume_token (parser
->lexer
);
30160 error ("stray %qs between Objective-C++ methods",
30161 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30163 /* Finally, try to parse a block-declaration, or a function-definition. */
30165 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30168 /* Parse a method signature. */
30171 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30173 tree rettype
, kwdparms
, optparms
;
30174 bool ellipsis
= false;
30175 bool is_class_method
;
30177 is_class_method
= cp_parser_objc_method_type (parser
);
30178 rettype
= cp_parser_objc_typename (parser
);
30179 *attributes
= NULL_TREE
;
30180 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30181 if (kwdparms
== error_mark_node
)
30182 return error_mark_node
;
30183 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30184 if (optparms
== error_mark_node
)
30185 return error_mark_node
;
30187 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30191 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30194 cp_lexer_save_tokens (parser
->lexer
);
30195 tattr
= cp_parser_attributes_opt (parser
);
30196 gcc_assert (tattr
) ;
30198 /* If the attributes are followed by a method introducer, this is not allowed.
30199 Dump the attributes and flag the situation. */
30200 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30201 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30204 /* Otherwise, the attributes introduce some interstitial code, possibly so
30205 rewind to allow that check. */
30206 cp_lexer_rollback_tokens (parser
->lexer
);
30210 /* Parse an Objective-C method prototype list. */
30213 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30215 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30217 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30219 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30221 tree attributes
, sig
;
30222 bool is_class_method
;
30223 if (token
->type
== CPP_PLUS
)
30224 is_class_method
= true;
30226 is_class_method
= false;
30227 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30228 if (sig
== error_mark_node
)
30230 cp_parser_skip_to_end_of_block_or_statement (parser
);
30231 token
= cp_lexer_peek_token (parser
->lexer
);
30234 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30235 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30237 else if (token
->keyword
== RID_AT_PROPERTY
)
30238 cp_parser_objc_at_property_declaration (parser
);
30239 else if (token
->keyword
== RID_ATTRIBUTE
30240 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30241 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30243 "prefix attributes are ignored for methods");
30245 /* Allow for interspersed non-ObjC++ code. */
30246 cp_parser_objc_interstitial_code (parser
);
30248 token
= cp_lexer_peek_token (parser
->lexer
);
30251 if (token
->type
!= CPP_EOF
)
30252 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30254 cp_parser_error (parser
, "expected %<@end%>");
30256 objc_finish_interface ();
30259 /* Parse an Objective-C method definition list. */
30262 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30264 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30266 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30270 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30273 tree sig
, attribute
;
30274 bool is_class_method
;
30275 if (token
->type
== CPP_PLUS
)
30276 is_class_method
= true;
30278 is_class_method
= false;
30279 push_deferring_access_checks (dk_deferred
);
30280 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30281 if (sig
== error_mark_node
)
30283 cp_parser_skip_to_end_of_block_or_statement (parser
);
30284 token
= cp_lexer_peek_token (parser
->lexer
);
30287 objc_start_method_definition (is_class_method
, sig
, attribute
,
30290 /* For historical reasons, we accept an optional semicolon. */
30291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30292 cp_lexer_consume_token (parser
->lexer
);
30294 ptk
= cp_lexer_peek_token (parser
->lexer
);
30295 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30296 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30298 perform_deferred_access_checks (tf_warning_or_error
);
30299 stop_deferring_access_checks ();
30300 meth
= cp_parser_function_definition_after_declarator (parser
,
30302 pop_deferring_access_checks ();
30303 objc_finish_method_definition (meth
);
30306 /* The following case will be removed once @synthesize is
30307 completely implemented. */
30308 else if (token
->keyword
== RID_AT_PROPERTY
)
30309 cp_parser_objc_at_property_declaration (parser
);
30310 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30311 cp_parser_objc_at_synthesize_declaration (parser
);
30312 else if (token
->keyword
== RID_AT_DYNAMIC
)
30313 cp_parser_objc_at_dynamic_declaration (parser
);
30314 else if (token
->keyword
== RID_ATTRIBUTE
30315 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30316 warning_at (token
->location
, OPT_Wattributes
,
30317 "prefix attributes are ignored for methods");
30319 /* Allow for interspersed non-ObjC++ code. */
30320 cp_parser_objc_interstitial_code (parser
);
30322 token
= cp_lexer_peek_token (parser
->lexer
);
30325 if (token
->type
!= CPP_EOF
)
30326 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30328 cp_parser_error (parser
, "expected %<@end%>");
30330 objc_finish_implementation ();
30333 /* Parse Objective-C ivars. */
30336 cp_parser_objc_class_ivars (cp_parser
* parser
)
30338 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30340 if (token
->type
!= CPP_OPEN_BRACE
)
30341 return; /* No ivars specified. */
30343 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30344 token
= cp_lexer_peek_token (parser
->lexer
);
30346 while (token
->type
!= CPP_CLOSE_BRACE
30347 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30349 cp_decl_specifier_seq declspecs
;
30350 int decl_class_or_enum_p
;
30351 tree prefix_attributes
;
30353 cp_parser_objc_visibility_spec (parser
);
30355 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30358 cp_parser_decl_specifier_seq (parser
,
30359 CP_PARSER_FLAGS_OPTIONAL
,
30361 &decl_class_or_enum_p
);
30363 /* auto, register, static, extern, mutable. */
30364 if (declspecs
.storage_class
!= sc_none
)
30366 cp_parser_error (parser
, "invalid type for instance variable");
30367 declspecs
.storage_class
= sc_none
;
30370 /* thread_local. */
30371 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30373 cp_parser_error (parser
, "invalid type for instance variable");
30374 declspecs
.locations
[ds_thread
] = 0;
30378 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30380 cp_parser_error (parser
, "invalid type for instance variable");
30381 declspecs
.locations
[ds_typedef
] = 0;
30384 prefix_attributes
= declspecs
.attributes
;
30385 declspecs
.attributes
= NULL_TREE
;
30387 /* Keep going until we hit the `;' at the end of the
30389 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30391 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30392 cp_declarator
*declarator
= NULL
;
30393 int ctor_dtor_or_conv_p
;
30395 /* Check for a (possibly unnamed) bitfield declaration. */
30396 token
= cp_lexer_peek_token (parser
->lexer
);
30397 if (token
->type
== CPP_COLON
)
30400 if (token
->type
== CPP_NAME
30401 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30404 /* Get the name of the bitfield. */
30405 declarator
= make_id_declarator (NULL_TREE
,
30406 cp_parser_identifier (parser
),
30410 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30411 /* Get the width of the bitfield. */
30413 = cp_parser_constant_expression (parser
);
30417 /* Parse the declarator. */
30419 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30420 &ctor_dtor_or_conv_p
,
30421 /*parenthesized_p=*/NULL
,
30422 /*member_p=*/false,
30423 /*friend_p=*/false);
30426 /* Look for attributes that apply to the ivar. */
30427 attributes
= cp_parser_attributes_opt (parser
);
30428 /* Remember which attributes are prefix attributes and
30430 first_attribute
= attributes
;
30431 /* Combine the attributes. */
30432 attributes
= attr_chainon (prefix_attributes
, attributes
);
30435 /* Create the bitfield declaration. */
30436 decl
= grokbitfield (declarator
, &declspecs
,
30437 width
, NULL_TREE
, attributes
);
30439 decl
= grokfield (declarator
, &declspecs
,
30440 NULL_TREE
, /*init_const_expr_p=*/false,
30441 NULL_TREE
, attributes
);
30443 /* Add the instance variable. */
30444 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30445 objc_add_instance_variable (decl
);
30447 /* Reset PREFIX_ATTRIBUTES. */
30448 if (attributes
!= error_mark_node
)
30450 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30451 attributes
= TREE_CHAIN (attributes
);
30453 TREE_CHAIN (attributes
) = NULL_TREE
;
30456 token
= cp_lexer_peek_token (parser
->lexer
);
30458 if (token
->type
== CPP_COMMA
)
30460 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30466 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30467 token
= cp_lexer_peek_token (parser
->lexer
);
30470 if (token
->keyword
== RID_AT_END
)
30471 cp_parser_error (parser
, "expected %<}%>");
30473 /* Do not consume the RID_AT_END, so it will be read again as terminating
30474 the @interface of @implementation. */
30475 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30476 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30478 /* For historical reasons, we accept an optional semicolon. */
30479 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30480 cp_lexer_consume_token (parser
->lexer
);
30483 /* Parse an Objective-C protocol declaration. */
30486 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30488 tree proto
, protorefs
;
30491 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30492 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30494 tok
= cp_lexer_peek_token (parser
->lexer
);
30495 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30496 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30500 /* See if we have a forward declaration or a definition. */
30501 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30503 /* Try a forward declaration first. */
30504 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30510 id
= cp_parser_identifier (parser
);
30511 if (id
== error_mark_node
)
30514 objc_declare_protocol (id
, attributes
);
30516 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30517 cp_lexer_consume_token (parser
->lexer
);
30521 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30524 /* Ok, we got a full-fledged definition (or at least should). */
30527 proto
= cp_parser_identifier (parser
);
30528 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30529 objc_start_protocol (proto
, protorefs
, attributes
);
30530 cp_parser_objc_method_prototype_list (parser
);
30534 /* Parse an Objective-C superclass or category. */
30537 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30540 tree
*categ
, bool *is_class_extension
)
30542 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30544 *super
= *categ
= NULL_TREE
;
30545 *is_class_extension
= false;
30546 if (next
->type
== CPP_COLON
)
30548 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30549 *super
= cp_parser_identifier (parser
);
30551 else if (next
->type
== CPP_OPEN_PAREN
)
30553 matching_parens parens
;
30554 parens
.consume_open (parser
); /* Eat '('. */
30556 /* If there is no category name, and this is an @interface, we
30557 have a class extension. */
30558 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30560 *categ
= NULL_TREE
;
30561 *is_class_extension
= true;
30564 *categ
= cp_parser_identifier (parser
);
30566 parens
.require_close (parser
);
30570 /* Parse an Objective-C class interface. */
30573 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30575 tree name
, super
, categ
, protos
;
30576 bool is_class_extension
;
30578 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30579 name
= cp_parser_identifier (parser
);
30580 if (name
== error_mark_node
)
30582 /* It's hard to recover because even if valid @interface stuff
30583 is to follow, we can't compile it (or validate it) if we
30584 don't even know which class it refers to. Let's assume this
30585 was a stray '@interface' token in the stream and skip it.
30589 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30590 &is_class_extension
);
30591 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30593 /* We have either a class or a category on our hands. */
30594 if (categ
|| is_class_extension
)
30595 objc_start_category_interface (name
, categ
, protos
, attributes
);
30598 objc_start_class_interface (name
, super
, protos
, attributes
);
30599 /* Handle instance variable declarations, if any. */
30600 cp_parser_objc_class_ivars (parser
);
30601 objc_continue_interface ();
30604 cp_parser_objc_method_prototype_list (parser
);
30607 /* Parse an Objective-C class implementation. */
30610 cp_parser_objc_class_implementation (cp_parser
* parser
)
30612 tree name
, super
, categ
;
30613 bool is_class_extension
;
30615 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30616 name
= cp_parser_identifier (parser
);
30617 if (name
== error_mark_node
)
30619 /* It's hard to recover because even if valid @implementation
30620 stuff is to follow, we can't compile it (or validate it) if
30621 we don't even know which class it refers to. Let's assume
30622 this was a stray '@implementation' token in the stream and
30627 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30628 &is_class_extension
);
30630 /* We have either a class or a category on our hands. */
30632 objc_start_category_implementation (name
, categ
);
30635 objc_start_class_implementation (name
, super
);
30636 /* Handle instance variable declarations, if any. */
30637 cp_parser_objc_class_ivars (parser
);
30638 objc_continue_implementation ();
30641 cp_parser_objc_method_definition_list (parser
);
30644 /* Consume the @end token and finish off the implementation. */
30647 cp_parser_objc_end_implementation (cp_parser
* parser
)
30649 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30650 objc_finish_implementation ();
30653 /* Parse an Objective-C declaration. */
30656 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30658 /* Try to figure out what kind of declaration is present. */
30659 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30662 switch (kwd
->keyword
)
30667 error_at (kwd
->location
, "attributes may not be specified before"
30668 " the %<@%D%> Objective-C++ keyword",
30672 case RID_AT_IMPLEMENTATION
:
30673 warning_at (kwd
->location
, OPT_Wattributes
,
30674 "prefix attributes are ignored before %<@%D%>",
30681 switch (kwd
->keyword
)
30684 cp_parser_objc_alias_declaration (parser
);
30687 cp_parser_objc_class_declaration (parser
);
30689 case RID_AT_PROTOCOL
:
30690 cp_parser_objc_protocol_declaration (parser
, attributes
);
30692 case RID_AT_INTERFACE
:
30693 cp_parser_objc_class_interface (parser
, attributes
);
30695 case RID_AT_IMPLEMENTATION
:
30696 cp_parser_objc_class_implementation (parser
);
30699 cp_parser_objc_end_implementation (parser
);
30702 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30704 cp_parser_skip_to_end_of_block_or_statement (parser
);
30708 /* Parse an Objective-C try-catch-finally statement.
30710 objc-try-catch-finally-stmt:
30711 @try compound-statement objc-catch-clause-seq [opt]
30712 objc-finally-clause [opt]
30714 objc-catch-clause-seq:
30715 objc-catch-clause objc-catch-clause-seq [opt]
30718 @catch ( objc-exception-declaration ) compound-statement
30720 objc-finally-clause:
30721 @finally compound-statement
30723 objc-exception-declaration:
30724 parameter-declaration
30727 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30731 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30732 for C. Keep them in sync. */
30735 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30737 location_t location
;
30740 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30741 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30742 objc_maybe_warn_exceptions (location
);
30743 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30744 node, lest it get absorbed into the surrounding block. */
30745 stmt
= push_stmt_list ();
30746 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30747 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30749 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30751 cp_parameter_declarator
*parm
;
30752 tree parameter_declaration
= error_mark_node
;
30753 bool seen_open_paren
= false;
30754 matching_parens parens
;
30756 cp_lexer_consume_token (parser
->lexer
);
30757 if (parens
.require_open (parser
))
30758 seen_open_paren
= true;
30759 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30761 /* We have "@catch (...)" (where the '...' are literally
30762 what is in the code). Skip the '...'.
30763 parameter_declaration is set to NULL_TREE, and
30764 objc_being_catch_clauses() knows that that means
30766 cp_lexer_consume_token (parser
->lexer
);
30767 parameter_declaration
= NULL_TREE
;
30771 /* We have "@catch (NSException *exception)" or something
30772 like that. Parse the parameter declaration. */
30773 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30775 parameter_declaration
= error_mark_node
;
30777 parameter_declaration
= grokdeclarator (parm
->declarator
,
30778 &parm
->decl_specifiers
,
30779 PARM
, /*initialized=*/0,
30780 /*attrlist=*/NULL
);
30782 if (seen_open_paren
)
30783 parens
.require_close (parser
);
30786 /* If there was no open parenthesis, we are recovering from
30787 an error, and we are trying to figure out what mistake
30788 the user has made. */
30790 /* If there is an immediate closing parenthesis, the user
30791 probably forgot the opening one (ie, they typed "@catch
30792 NSException *e)". Parse the closing parenthesis and keep
30794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30795 cp_lexer_consume_token (parser
->lexer
);
30797 /* If these is no immediate closing parenthesis, the user
30798 probably doesn't know that parenthesis are required at
30799 all (ie, they typed "@catch NSException *e"). So, just
30800 forget about the closing parenthesis and keep going. */
30802 objc_begin_catch_clause (parameter_declaration
);
30803 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30804 objc_finish_catch_clause ();
30806 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30808 cp_lexer_consume_token (parser
->lexer
);
30809 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30810 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30811 node, lest it get absorbed into the surrounding block. */
30812 stmt
= push_stmt_list ();
30813 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30814 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30817 return objc_finish_try_stmt ();
30820 /* Parse an Objective-C synchronized statement.
30822 objc-synchronized-stmt:
30823 @synchronized ( expression ) compound-statement
30825 Returns NULL_TREE. */
30828 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30830 location_t location
;
30833 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30835 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30836 objc_maybe_warn_exceptions (location
);
30837 matching_parens parens
;
30838 parens
.require_open (parser
);
30839 lock
= cp_parser_expression (parser
);
30840 parens
.require_close (parser
);
30842 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30843 node, lest it get absorbed into the surrounding block. */
30844 stmt
= push_stmt_list ();
30845 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30847 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30850 /* Parse an Objective-C throw statement.
30853 @throw assignment-expression [opt] ;
30855 Returns a constructed '@throw' statement. */
30858 cp_parser_objc_throw_statement (cp_parser
*parser
)
30860 tree expr
= NULL_TREE
;
30861 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30863 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30865 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30866 expr
= cp_parser_expression (parser
);
30868 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30870 return objc_build_throw_stmt (loc
, expr
);
30873 /* Parse an Objective-C statement. */
30876 cp_parser_objc_statement (cp_parser
* parser
)
30878 /* Try to figure out what kind of declaration is present. */
30879 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30881 switch (kwd
->keyword
)
30884 return cp_parser_objc_try_catch_finally_statement (parser
);
30885 case RID_AT_SYNCHRONIZED
:
30886 return cp_parser_objc_synchronized_statement (parser
);
30888 return cp_parser_objc_throw_statement (parser
);
30890 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30892 cp_parser_skip_to_end_of_block_or_statement (parser
);
30895 return error_mark_node
;
30898 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30899 look ahead to see if an objc keyword follows the attributes. This
30900 is to detect the use of prefix attributes on ObjC @interface and
30904 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30906 cp_lexer_save_tokens (parser
->lexer
);
30907 *attrib
= cp_parser_attributes_opt (parser
);
30908 gcc_assert (*attrib
);
30909 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30911 cp_lexer_commit_tokens (parser
->lexer
);
30914 cp_lexer_rollback_tokens (parser
->lexer
);
30918 /* This routine is a minimal replacement for
30919 c_parser_struct_declaration () used when parsing the list of
30920 types/names or ObjC++ properties. For example, when parsing the
30923 @property (readonly) int a, b, c;
30925 this function is responsible for parsing "int a, int b, int c" and
30926 returning the declarations as CHAIN of DECLs.
30928 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30929 similar parsing. */
30931 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30933 tree decls
= NULL_TREE
;
30934 cp_decl_specifier_seq declspecs
;
30935 int decl_class_or_enum_p
;
30936 tree prefix_attributes
;
30938 cp_parser_decl_specifier_seq (parser
,
30939 CP_PARSER_FLAGS_NONE
,
30941 &decl_class_or_enum_p
);
30943 if (declspecs
.type
== error_mark_node
)
30944 return error_mark_node
;
30946 /* auto, register, static, extern, mutable. */
30947 if (declspecs
.storage_class
!= sc_none
)
30949 cp_parser_error (parser
, "invalid type for property");
30950 declspecs
.storage_class
= sc_none
;
30953 /* thread_local. */
30954 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30956 cp_parser_error (parser
, "invalid type for property");
30957 declspecs
.locations
[ds_thread
] = 0;
30961 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30963 cp_parser_error (parser
, "invalid type for property");
30964 declspecs
.locations
[ds_typedef
] = 0;
30967 prefix_attributes
= declspecs
.attributes
;
30968 declspecs
.attributes
= NULL_TREE
;
30970 /* Keep going until we hit the `;' at the end of the declaration. */
30971 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30973 tree attributes
, first_attribute
, decl
;
30974 cp_declarator
*declarator
;
30977 /* Parse the declarator. */
30978 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30979 NULL
, NULL
, false, false);
30981 /* Look for attributes that apply to the ivar. */
30982 attributes
= cp_parser_attributes_opt (parser
);
30983 /* Remember which attributes are prefix attributes and
30985 first_attribute
= attributes
;
30986 /* Combine the attributes. */
30987 attributes
= attr_chainon (prefix_attributes
, attributes
);
30989 decl
= grokfield (declarator
, &declspecs
,
30990 NULL_TREE
, /*init_const_expr_p=*/false,
30991 NULL_TREE
, attributes
);
30993 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30994 return error_mark_node
;
30996 /* Reset PREFIX_ATTRIBUTES. */
30997 if (attributes
!= error_mark_node
)
30999 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31000 attributes
= TREE_CHAIN (attributes
);
31002 TREE_CHAIN (attributes
) = NULL_TREE
;
31005 DECL_CHAIN (decl
) = decls
;
31008 token
= cp_lexer_peek_token (parser
->lexer
);
31009 if (token
->type
== CPP_COMMA
)
31011 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31020 /* Parse an Objective-C @property declaration. The syntax is:
31022 objc-property-declaration:
31023 '@property' objc-property-attributes[opt] struct-declaration ;
31025 objc-property-attributes:
31026 '(' objc-property-attribute-list ')'
31028 objc-property-attribute-list:
31029 objc-property-attribute
31030 objc-property-attribute-list, objc-property-attribute
31032 objc-property-attribute
31033 'getter' = identifier
31034 'setter' = identifier
31043 @property NSString *name;
31044 @property (readonly) id object;
31045 @property (retain, nonatomic, getter=getTheName) id name;
31046 @property int a, b, c;
31048 PS: This function is identical to
31049 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31051 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31053 /* The following variables hold the attributes of the properties as
31054 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31055 seen. When we see an attribute, we set them to 'true' (if they
31056 are boolean properties) or to the identifier (if they have an
31057 argument, ie, for getter and setter). Note that here we only
31058 parse the list of attributes, check the syntax and accumulate the
31059 attributes that we find. objc_add_property_declaration() will
31060 then process the information. */
31061 bool property_assign
= false;
31062 bool property_copy
= false;
31063 tree property_getter_ident
= NULL_TREE
;
31064 bool property_nonatomic
= false;
31065 bool property_readonly
= false;
31066 bool property_readwrite
= false;
31067 bool property_retain
= false;
31068 tree property_setter_ident
= NULL_TREE
;
31070 /* 'properties' is the list of properties that we read. Usually a
31071 single one, but maybe more (eg, in "@property int a, b, c;" there
31076 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31078 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31080 /* Parse the optional attribute list... */
31081 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31084 matching_parens parens
;
31085 parens
.consume_open (parser
);
31089 bool syntax_error
= false;
31090 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31093 if (token
->type
!= CPP_NAME
)
31095 cp_parser_error (parser
, "expected identifier");
31098 keyword
= C_RID_CODE (token
->u
.value
);
31099 cp_lexer_consume_token (parser
->lexer
);
31102 case RID_ASSIGN
: property_assign
= true; break;
31103 case RID_COPY
: property_copy
= true; break;
31104 case RID_NONATOMIC
: property_nonatomic
= true; break;
31105 case RID_READONLY
: property_readonly
= true; break;
31106 case RID_READWRITE
: property_readwrite
= true; break;
31107 case RID_RETAIN
: property_retain
= true; break;
31111 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31113 if (keyword
== RID_GETTER
)
31114 cp_parser_error (parser
,
31115 "missing %<=%> (after %<getter%> attribute)");
31117 cp_parser_error (parser
,
31118 "missing %<=%> (after %<setter%> attribute)");
31119 syntax_error
= true;
31122 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31123 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31125 cp_parser_error (parser
, "expected identifier");
31126 syntax_error
= true;
31129 if (keyword
== RID_SETTER
)
31131 if (property_setter_ident
!= NULL_TREE
)
31133 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31134 cp_lexer_consume_token (parser
->lexer
);
31137 property_setter_ident
= cp_parser_objc_selector (parser
);
31138 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31139 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31141 cp_lexer_consume_token (parser
->lexer
);
31145 if (property_getter_ident
!= NULL_TREE
)
31147 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31148 cp_lexer_consume_token (parser
->lexer
);
31151 property_getter_ident
= cp_parser_objc_selector (parser
);
31155 cp_parser_error (parser
, "unknown property attribute");
31156 syntax_error
= true;
31163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31164 cp_lexer_consume_token (parser
->lexer
);
31169 /* FIXME: "@property (setter, assign);" will generate a spurious
31170 "error: expected ‘)’ before ‘,’ token". This is because
31171 cp_parser_require, unlike the C counterpart, will produce an
31172 error even if we are in error recovery. */
31173 if (!parens
.require_close (parser
))
31175 cp_parser_skip_to_closing_parenthesis (parser
,
31176 /*recovering=*/true,
31177 /*or_comma=*/false,
31178 /*consume_paren=*/true);
31182 /* ... and the property declaration(s). */
31183 properties
= cp_parser_objc_struct_declaration (parser
);
31185 if (properties
== error_mark_node
)
31187 cp_parser_skip_to_end_of_statement (parser
);
31188 /* If the next token is now a `;', consume it. */
31189 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31190 cp_lexer_consume_token (parser
->lexer
);
31194 if (properties
== NULL_TREE
)
31195 cp_parser_error (parser
, "expected identifier");
31198 /* Comma-separated properties are chained together in
31199 reverse order; add them one by one. */
31200 properties
= nreverse (properties
);
31202 for (; properties
; properties
= TREE_CHAIN (properties
))
31203 objc_add_property_declaration (loc
, copy_node (properties
),
31204 property_readonly
, property_readwrite
,
31205 property_assign
, property_retain
,
31206 property_copy
, property_nonatomic
,
31207 property_getter_ident
, property_setter_ident
);
31210 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31213 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31215 objc-synthesize-declaration:
31216 @synthesize objc-synthesize-identifier-list ;
31218 objc-synthesize-identifier-list:
31219 objc-synthesize-identifier
31220 objc-synthesize-identifier-list, objc-synthesize-identifier
31222 objc-synthesize-identifier
31224 identifier = identifier
31227 @synthesize MyProperty;
31228 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31230 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31231 for C. Keep them in sync.
31234 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31236 tree list
= NULL_TREE
;
31238 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31240 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31243 tree property
, ivar
;
31244 property
= cp_parser_identifier (parser
);
31245 if (property
== error_mark_node
)
31247 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31250 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31252 cp_lexer_consume_token (parser
->lexer
);
31253 ivar
= cp_parser_identifier (parser
);
31254 if (ivar
== error_mark_node
)
31256 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31262 list
= chainon (list
, build_tree_list (ivar
, property
));
31263 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31264 cp_lexer_consume_token (parser
->lexer
);
31268 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31269 objc_add_synthesize_declaration (loc
, list
);
31272 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31274 objc-dynamic-declaration:
31275 @dynamic identifier-list ;
31278 @dynamic MyProperty;
31279 @dynamic MyProperty, AnotherProperty;
31281 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31282 for C. Keep them in sync.
31285 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31287 tree list
= NULL_TREE
;
31289 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31291 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31295 property
= cp_parser_identifier (parser
);
31296 if (property
== error_mark_node
)
31298 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31301 list
= chainon (list
, build_tree_list (NULL
, property
));
31302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31303 cp_lexer_consume_token (parser
->lexer
);
31307 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31308 objc_add_dynamic_declaration (loc
, list
);
31312 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31314 /* Returns name of the next clause.
31315 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31316 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31317 returned and the token is consumed. */
31319 static pragma_omp_clause
31320 cp_parser_omp_clause_name (cp_parser
*parser
)
31322 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31324 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31325 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31326 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31327 result
= PRAGMA_OMP_CLAUSE_IF
;
31328 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31329 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31330 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31331 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31332 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31333 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31334 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31335 result
= PRAGMA_OMP_CLAUSE_FOR
;
31336 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31338 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31339 const char *p
= IDENTIFIER_POINTER (id
);
31344 if (!strcmp ("aligned", p
))
31345 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31346 else if (!strcmp ("async", p
))
31347 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31350 if (!strcmp ("collapse", p
))
31351 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31352 else if (!strcmp ("copy", p
))
31353 result
= PRAGMA_OACC_CLAUSE_COPY
;
31354 else if (!strcmp ("copyin", p
))
31355 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31356 else if (!strcmp ("copyout", p
))
31357 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31358 else if (!strcmp ("copyprivate", p
))
31359 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31360 else if (!strcmp ("create", p
))
31361 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31364 if (!strcmp ("defaultmap", p
))
31365 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31366 else if (!strcmp ("depend", p
))
31367 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31368 else if (!strcmp ("device", p
))
31369 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31370 else if (!strcmp ("deviceptr", p
))
31371 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31372 else if (!strcmp ("device_resident", p
))
31373 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31374 else if (!strcmp ("dist_schedule", p
))
31375 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31378 if (!strcmp ("final", p
))
31379 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31380 else if (!strcmp ("firstprivate", p
))
31381 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31382 else if (!strcmp ("from", p
))
31383 result
= PRAGMA_OMP_CLAUSE_FROM
;
31386 if (!strcmp ("gang", p
))
31387 result
= PRAGMA_OACC_CLAUSE_GANG
;
31388 else if (!strcmp ("grainsize", p
))
31389 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31392 if (!strcmp ("hint", p
))
31393 result
= PRAGMA_OMP_CLAUSE_HINT
;
31394 else if (!strcmp ("host", p
))
31395 result
= PRAGMA_OACC_CLAUSE_HOST
;
31398 if (!strcmp ("inbranch", p
))
31399 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31400 else if (!strcmp ("independent", p
))
31401 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31402 else if (!strcmp ("is_device_ptr", p
))
31403 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31406 if (!strcmp ("lastprivate", p
))
31407 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31408 else if (!strcmp ("linear", p
))
31409 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31410 else if (!strcmp ("link", p
))
31411 result
= PRAGMA_OMP_CLAUSE_LINK
;
31414 if (!strcmp ("map", p
))
31415 result
= PRAGMA_OMP_CLAUSE_MAP
;
31416 else if (!strcmp ("mergeable", p
))
31417 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31420 if (!strcmp ("nogroup", p
))
31421 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31422 else if (!strcmp ("notinbranch", p
))
31423 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31424 else if (!strcmp ("nowait", p
))
31425 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31426 else if (!strcmp ("num_gangs", p
))
31427 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31428 else if (!strcmp ("num_tasks", p
))
31429 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31430 else if (!strcmp ("num_teams", p
))
31431 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31432 else if (!strcmp ("num_threads", p
))
31433 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31434 else if (!strcmp ("num_workers", p
))
31435 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31438 if (!strcmp ("ordered", p
))
31439 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31442 if (!strcmp ("parallel", p
))
31443 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31444 else if (!strcmp ("present", p
))
31445 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31446 else if (!strcmp ("present_or_copy", p
)
31447 || !strcmp ("pcopy", p
))
31448 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31449 else if (!strcmp ("present_or_copyin", p
)
31450 || !strcmp ("pcopyin", p
))
31451 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31452 else if (!strcmp ("present_or_copyout", p
)
31453 || !strcmp ("pcopyout", p
))
31454 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31455 else if (!strcmp ("present_or_create", p
)
31456 || !strcmp ("pcreate", p
))
31457 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31458 else if (!strcmp ("priority", p
))
31459 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31460 else if (!strcmp ("proc_bind", p
))
31461 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31464 if (!strcmp ("reduction", p
))
31465 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31468 if (!strcmp ("safelen", p
))
31469 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31470 else if (!strcmp ("schedule", p
))
31471 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31472 else if (!strcmp ("sections", p
))
31473 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31474 else if (!strcmp ("self", p
))
31475 result
= PRAGMA_OACC_CLAUSE_SELF
;
31476 else if (!strcmp ("seq", p
))
31477 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31478 else if (!strcmp ("shared", p
))
31479 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31480 else if (!strcmp ("simd", p
))
31481 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31482 else if (!strcmp ("simdlen", p
))
31483 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31486 if (!strcmp ("taskgroup", p
))
31487 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31488 else if (!strcmp ("thread_limit", p
))
31489 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31490 else if (!strcmp ("threads", p
))
31491 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31492 else if (!strcmp ("tile", p
))
31493 result
= PRAGMA_OACC_CLAUSE_TILE
;
31494 else if (!strcmp ("to", p
))
31495 result
= PRAGMA_OMP_CLAUSE_TO
;
31498 if (!strcmp ("uniform", p
))
31499 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31500 else if (!strcmp ("untied", p
))
31501 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31502 else if (!strcmp ("use_device", p
))
31503 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31504 else if (!strcmp ("use_device_ptr", p
))
31505 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31508 if (!strcmp ("vector", p
))
31509 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31510 else if (!strcmp ("vector_length", p
))
31511 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31514 if (!strcmp ("wait", p
))
31515 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31516 else if (!strcmp ("worker", p
))
31517 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31522 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31523 cp_lexer_consume_token (parser
->lexer
);
31528 /* Validate that a clause of the given type does not already exist. */
31531 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31532 const char *name
, location_t location
)
31536 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31537 if (OMP_CLAUSE_CODE (c
) == code
)
31539 error_at (location
, "too many %qs clauses", name
);
31547 variable-list , identifier
31549 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31550 colon). An opening parenthesis will have been consumed by the caller.
31552 If KIND is nonzero, create the appropriate node and install the decl
31553 in OMP_CLAUSE_DECL and add the node to the head of the list.
31555 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31556 return the list created.
31558 COLON can be NULL if only closing parenthesis should end the list,
31559 or pointer to bool which will receive false if the list is terminated
31560 by closing parenthesis or true if the list is terminated by colon. */
31563 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31564 tree list
, bool *colon
)
31567 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31570 parser
->colon_corrects_to_scope_p
= false;
31577 token
= cp_lexer_peek_token (parser
->lexer
);
31579 && current_class_ptr
31580 && cp_parser_is_keyword (token
, RID_THIS
))
31582 decl
= finish_this_expr ();
31583 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31584 || CONVERT_EXPR_P (decl
))
31585 decl
= TREE_OPERAND (decl
, 0);
31586 cp_lexer_consume_token (parser
->lexer
);
31590 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31591 /*check_dependency_p=*/true,
31592 /*template_p=*/NULL
,
31593 /*declarator_p=*/false,
31594 /*optional_p=*/false);
31595 if (name
== error_mark_node
)
31598 if (identifier_p (name
))
31599 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31602 if (decl
== error_mark_node
)
31603 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31606 if (decl
== error_mark_node
)
31608 else if (kind
!= 0)
31612 case OMP_CLAUSE__CACHE_
:
31613 /* The OpenACC cache directive explicitly only allows "array
31614 elements or subarrays". */
31615 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31617 error_at (token
->location
, "expected %<[%>");
31618 decl
= error_mark_node
;
31622 case OMP_CLAUSE_MAP
:
31623 case OMP_CLAUSE_FROM
:
31624 case OMP_CLAUSE_TO
:
31625 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31628 = cp_lexer_peek_token (parser
->lexer
)->location
;
31629 cp_id_kind idk
= CP_ID_KIND_NONE
;
31630 cp_lexer_consume_token (parser
->lexer
);
31631 decl
= convert_from_reference (decl
);
31633 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31638 case OMP_CLAUSE_DEPEND
:
31639 case OMP_CLAUSE_REDUCTION
:
31640 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31642 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31644 parser
->colon_corrects_to_scope_p
= false;
31645 cp_lexer_consume_token (parser
->lexer
);
31646 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31647 low_bound
= cp_parser_expression (parser
);
31649 parser
->colon_corrects_to_scope_p
31650 = saved_colon_corrects_to_scope_p
;
31651 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31652 length
= integer_one_node
;
31655 /* Look for `:'. */
31656 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31658 if (!cp_lexer_next_token_is (parser
->lexer
,
31660 length
= cp_parser_expression (parser
);
31662 /* Look for the closing `]'. */
31663 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31667 decl
= tree_cons (low_bound
, length
, decl
);
31674 tree u
= build_omp_clause (token
->location
, kind
);
31675 OMP_CLAUSE_DECL (u
) = decl
;
31676 OMP_CLAUSE_CHAIN (u
) = list
;
31680 list
= tree_cons (decl
, NULL_TREE
, list
);
31683 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31685 cp_lexer_consume_token (parser
->lexer
);
31689 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31691 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31694 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31698 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31702 /* Try to resync to an unnested comma. Copied from
31703 cp_parser_parenthesized_expression_list. */
31706 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31707 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31708 /*recovering=*/true,
31710 /*consume_paren=*/true);
31718 /* Similarly, but expect leading and trailing parenthesis. This is a very
31719 common case for omp clauses. */
31722 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31724 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31725 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31730 copy ( variable-list )
31731 copyin ( variable-list )
31732 copyout ( variable-list )
31733 create ( variable-list )
31734 delete ( variable-list )
31735 present ( variable-list )
31736 present_or_copy ( variable-list )
31737 pcopy ( variable-list )
31738 present_or_copyin ( variable-list )
31739 pcopyin ( variable-list )
31740 present_or_copyout ( variable-list )
31741 pcopyout ( variable-list )
31742 present_or_create ( variable-list )
31743 pcreate ( variable-list ) */
31746 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31749 enum gomp_map_kind kind
;
31752 case PRAGMA_OACC_CLAUSE_COPY
:
31753 kind
= GOMP_MAP_FORCE_TOFROM
;
31755 case PRAGMA_OACC_CLAUSE_COPYIN
:
31756 kind
= GOMP_MAP_FORCE_TO
;
31758 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31759 kind
= GOMP_MAP_FORCE_FROM
;
31761 case PRAGMA_OACC_CLAUSE_CREATE
:
31762 kind
= GOMP_MAP_FORCE_ALLOC
;
31764 case PRAGMA_OACC_CLAUSE_DELETE
:
31765 kind
= GOMP_MAP_DELETE
;
31767 case PRAGMA_OACC_CLAUSE_DEVICE
:
31768 kind
= GOMP_MAP_FORCE_TO
;
31770 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31771 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31773 case PRAGMA_OACC_CLAUSE_HOST
:
31774 case PRAGMA_OACC_CLAUSE_SELF
:
31775 kind
= GOMP_MAP_FORCE_FROM
;
31777 case PRAGMA_OACC_CLAUSE_LINK
:
31778 kind
= GOMP_MAP_LINK
;
31780 case PRAGMA_OACC_CLAUSE_PRESENT
:
31781 kind
= GOMP_MAP_FORCE_PRESENT
;
31783 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31784 kind
= GOMP_MAP_TOFROM
;
31786 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31787 kind
= GOMP_MAP_TO
;
31789 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31790 kind
= GOMP_MAP_FROM
;
31792 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31793 kind
= GOMP_MAP_ALLOC
;
31796 gcc_unreachable ();
31799 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31801 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31802 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31808 deviceptr ( variable-list ) */
31811 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31813 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31816 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31817 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31818 variable-list must only allow for pointer variables. */
31819 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31820 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31822 tree v
= TREE_PURPOSE (t
);
31823 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31824 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31825 OMP_CLAUSE_DECL (u
) = v
;
31826 OMP_CLAUSE_CHAIN (u
) = list
;
31840 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31841 enum omp_clause_code code
,
31842 tree list
, location_t location
)
31844 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31845 tree c
= build_omp_clause (location
, code
);
31846 OMP_CLAUSE_CHAIN (c
) = list
;
31851 num_gangs ( expression )
31852 num_workers ( expression )
31853 vector_length ( expression ) */
31856 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31857 const char *str
, tree list
)
31859 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31861 matching_parens parens
;
31862 if (!parens
.require_open (parser
))
31865 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31867 if (t
== error_mark_node
31868 || !parens
.require_close (parser
))
31870 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31871 /*or_comma=*/false,
31872 /*consume_paren=*/true);
31876 check_no_duplicate_clause (list
, code
, str
, loc
);
31878 tree c
= build_omp_clause (loc
, code
);
31879 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31880 OMP_CLAUSE_CHAIN (c
) = list
;
31886 gang [( gang-arg-list )]
31887 worker [( [num:] int-expr )]
31888 vector [( [length:] int-expr )]
31890 where gang-arg is one of:
31895 and size-expr may be:
31902 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31903 const char *str
, tree list
)
31905 const char *id
= "num";
31906 cp_lexer
*lexer
= parser
->lexer
;
31907 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31908 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31910 if (kind
== OMP_CLAUSE_VECTOR
)
31913 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31915 matching_parens parens
;
31916 parens
.consume_open (parser
);
31920 cp_token
*next
= cp_lexer_peek_token (lexer
);
31923 /* Gang static argument. */
31924 if (kind
== OMP_CLAUSE_GANG
31925 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31927 cp_lexer_consume_token (lexer
);
31929 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31930 goto cleanup_error
;
31933 if (ops
[idx
] != NULL
)
31935 cp_parser_error (parser
, "too many %<static%> arguments");
31936 goto cleanup_error
;
31939 /* Check for the '*' argument. */
31940 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31941 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31942 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31945 cp_lexer_consume_token (lexer
);
31946 ops
[idx
] = integer_minus_one_node
;
31948 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31950 cp_lexer_consume_token (lexer
);
31956 /* Worker num: argument and vector length: arguments. */
31957 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31958 && id_equal (next
->u
.value
, id
)
31959 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31961 cp_lexer_consume_token (lexer
); /* id */
31962 cp_lexer_consume_token (lexer
); /* ':' */
31965 /* Now collect the actual argument. */
31966 if (ops
[idx
] != NULL_TREE
)
31968 cp_parser_error (parser
, "unexpected argument");
31969 goto cleanup_error
;
31972 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31974 if (expr
== error_mark_node
)
31975 goto cleanup_error
;
31977 mark_exp_read (expr
);
31980 if (kind
== OMP_CLAUSE_GANG
31981 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31983 cp_lexer_consume_token (lexer
);
31990 if (!parens
.require_close (parser
))
31991 goto cleanup_error
;
31994 check_no_duplicate_clause (list
, kind
, str
, loc
);
31996 c
= build_omp_clause (loc
, kind
);
31999 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32001 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32002 OMP_CLAUSE_CHAIN (c
) = list
;
32007 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32012 tile ( size-expr-list ) */
32015 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32017 tree c
, expr
= error_mark_node
;
32018 tree tile
= NULL_TREE
;
32020 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32021 so, but the spec authors never considered such a case and have
32022 differing opinions on what it might mean, including 'not
32024 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32025 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32028 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32033 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32037 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32038 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32040 cp_lexer_consume_token (parser
->lexer
);
32041 expr
= integer_zero_node
;
32044 expr
= cp_parser_constant_expression (parser
);
32046 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32048 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32050 /* Consume the trailing ')'. */
32051 cp_lexer_consume_token (parser
->lexer
);
32053 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32054 tile
= nreverse (tile
);
32055 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32056 OMP_CLAUSE_CHAIN (c
) = list
;
32061 Parse wait clause or directive parameters. */
32064 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32066 vec
<tree
, va_gc
> *args
;
32069 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32071 /*allow_expansion_p=*/true,
32072 /*non_constant_p=*/NULL
);
32074 if (args
== NULL
|| args
->length () == 0)
32076 cp_parser_error (parser
, "expected integer expression before ')'");
32078 release_tree_vector (args
);
32082 args_tree
= build_tree_list_vec (args
);
32084 release_tree_vector (args
);
32086 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32088 tree targ
= TREE_VALUE (t
);
32090 if (targ
!= error_mark_node
)
32092 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32093 error ("%<wait%> expression must be integral");
32096 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32098 targ
= mark_rvalue_use (targ
);
32099 OMP_CLAUSE_DECL (c
) = targ
;
32100 OMP_CLAUSE_CHAIN (c
) = list
;
32110 wait ( int-expr-list ) */
32113 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32115 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32117 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32120 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32126 collapse ( constant-expression ) */
32129 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32135 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32136 matching_parens parens
;
32137 if (!parens
.require_open (parser
))
32140 num
= cp_parser_constant_expression (parser
);
32142 if (!parens
.require_close (parser
))
32143 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32144 /*or_comma=*/false,
32145 /*consume_paren=*/true);
32147 if (num
== error_mark_node
)
32149 num
= fold_non_dependent_expr (num
);
32150 if (!tree_fits_shwi_p (num
)
32151 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32152 || (n
= tree_to_shwi (num
)) <= 0
32155 error_at (loc
, "collapse argument needs positive constant integer expression");
32159 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32160 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32161 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32162 OMP_CLAUSE_CHAIN (c
) = list
;
32163 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32169 default ( none | shared )
32172 default ( none | present ) */
32175 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32176 location_t location
, bool is_oacc
)
32178 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32181 matching_parens parens
;
32182 if (!parens
.require_open (parser
))
32184 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32186 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32187 const char *p
= IDENTIFIER_POINTER (id
);
32192 if (strcmp ("none", p
) != 0)
32194 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32198 if (strcmp ("present", p
) != 0 || !is_oacc
)
32200 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32204 if (strcmp ("shared", p
) != 0 || is_oacc
)
32206 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32213 cp_lexer_consume_token (parser
->lexer
);
32219 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32221 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32224 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32225 || !parens
.require_close (parser
))
32226 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32227 /*or_comma=*/false,
32228 /*consume_paren=*/true);
32230 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32233 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32234 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32235 OMP_CLAUSE_CHAIN (c
) = list
;
32236 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32242 final ( expression ) */
32245 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32249 matching_parens parens
;
32250 if (!parens
.require_open (parser
))
32253 t
= cp_parser_condition (parser
);
32255 if (t
== error_mark_node
32256 || !parens
.require_close (parser
))
32257 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32258 /*or_comma=*/false,
32259 /*consume_paren=*/true);
32261 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32263 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32264 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32265 OMP_CLAUSE_CHAIN (c
) = list
;
32274 if ( directive-name-modifier : expression )
32276 directive-name-modifier:
32277 parallel | task | taskloop | target data | target | target update
32278 | target enter data | target exit data */
32281 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32285 enum tree_code if_modifier
= ERROR_MARK
;
32287 matching_parens parens
;
32288 if (!parens
.require_open (parser
))
32291 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32293 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32294 const char *p
= IDENTIFIER_POINTER (id
);
32297 if (strcmp ("parallel", p
) == 0)
32298 if_modifier
= OMP_PARALLEL
;
32299 else if (strcmp ("task", p
) == 0)
32300 if_modifier
= OMP_TASK
;
32301 else if (strcmp ("taskloop", p
) == 0)
32302 if_modifier
= OMP_TASKLOOP
;
32303 else if (strcmp ("target", p
) == 0)
32305 if_modifier
= OMP_TARGET
;
32306 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32308 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32309 p
= IDENTIFIER_POINTER (id
);
32310 if (strcmp ("data", p
) == 0)
32311 if_modifier
= OMP_TARGET_DATA
;
32312 else if (strcmp ("update", p
) == 0)
32313 if_modifier
= OMP_TARGET_UPDATE
;
32314 else if (strcmp ("enter", p
) == 0)
32315 if_modifier
= OMP_TARGET_ENTER_DATA
;
32316 else if (strcmp ("exit", p
) == 0)
32317 if_modifier
= OMP_TARGET_EXIT_DATA
;
32318 if (if_modifier
!= OMP_TARGET
)
32323 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32324 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32326 if_modifier
= ERROR_MARK
;
32328 if (if_modifier
== OMP_TARGET_ENTER_DATA
32329 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32331 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32333 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32334 p
= IDENTIFIER_POINTER (id
);
32335 if (strcmp ("data", p
) == 0)
32341 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32342 error_at (loc
, "expected %<data%>");
32343 if_modifier
= ERROR_MARK
;
32348 if (if_modifier
!= ERROR_MARK
)
32350 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32353 cp_lexer_consume_token (parser
->lexer
);
32360 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32361 error_at (loc
, "expected %<:%>");
32363 if_modifier
= ERROR_MARK
;
32368 t
= cp_parser_condition (parser
);
32370 if (t
== error_mark_node
32371 || !parens
.require_close (parser
))
32372 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32373 /*or_comma=*/false,
32374 /*consume_paren=*/true);
32376 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32377 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32379 if (if_modifier
!= ERROR_MARK
32380 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32382 const char *p
= NULL
;
32383 switch (if_modifier
)
32385 case OMP_PARALLEL
: p
= "parallel"; break;
32386 case OMP_TASK
: p
= "task"; break;
32387 case OMP_TASKLOOP
: p
= "taskloop"; break;
32388 case OMP_TARGET_DATA
: p
= "target data"; break;
32389 case OMP_TARGET
: p
= "target"; break;
32390 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32391 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32392 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32393 default: gcc_unreachable ();
32395 error_at (location
, "too many %<if%> clauses with %qs modifier",
32399 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32402 error_at (location
, "too many %<if%> clauses");
32404 error_at (location
, "too many %<if%> clauses without modifier");
32407 else if (if_modifier
== ERROR_MARK
32408 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32410 error_at (location
, "if any %<if%> clause has modifier, then all "
32411 "%<if%> clauses have to use modifier");
32416 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32417 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32418 OMP_CLAUSE_IF_EXPR (c
) = t
;
32419 OMP_CLAUSE_CHAIN (c
) = list
;
32428 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32429 tree list
, location_t location
)
32433 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32436 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32437 OMP_CLAUSE_CHAIN (c
) = list
;
32445 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32446 tree list
, location_t location
)
32450 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32452 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32453 OMP_CLAUSE_CHAIN (c
) = list
;
32458 num_threads ( expression ) */
32461 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32462 location_t location
)
32466 matching_parens parens
;
32467 if (!parens
.require_open (parser
))
32470 t
= cp_parser_expression (parser
);
32472 if (t
== error_mark_node
32473 || !parens
.require_close (parser
))
32474 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32475 /*or_comma=*/false,
32476 /*consume_paren=*/true);
32478 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32479 "num_threads", location
);
32481 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32482 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32483 OMP_CLAUSE_CHAIN (c
) = list
;
32489 num_tasks ( expression ) */
32492 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32493 location_t location
)
32497 matching_parens parens
;
32498 if (!parens
.require_open (parser
))
32501 t
= cp_parser_expression (parser
);
32503 if (t
== error_mark_node
32504 || !parens
.require_close (parser
))
32505 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32506 /*or_comma=*/false,
32507 /*consume_paren=*/true);
32509 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32510 "num_tasks", location
);
32512 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32513 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32514 OMP_CLAUSE_CHAIN (c
) = list
;
32520 grainsize ( expression ) */
32523 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32524 location_t location
)
32528 matching_parens parens
;
32529 if (!parens
.require_open (parser
))
32532 t
= cp_parser_expression (parser
);
32534 if (t
== error_mark_node
32535 || !parens
.require_close (parser
))
32536 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32537 /*or_comma=*/false,
32538 /*consume_paren=*/true);
32540 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32541 "grainsize", location
);
32543 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32544 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32545 OMP_CLAUSE_CHAIN (c
) = list
;
32551 priority ( expression ) */
32554 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32555 location_t location
)
32559 matching_parens parens
;
32560 if (!parens
.require_open (parser
))
32563 t
= cp_parser_expression (parser
);
32565 if (t
== error_mark_node
32566 || !parens
.require_close (parser
))
32567 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32568 /*or_comma=*/false,
32569 /*consume_paren=*/true);
32571 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32572 "priority", location
);
32574 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32575 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32576 OMP_CLAUSE_CHAIN (c
) = list
;
32582 hint ( expression ) */
32585 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32586 location_t location
)
32590 matching_parens parens
;
32591 if (!parens
.require_open (parser
))
32594 t
= cp_parser_expression (parser
);
32596 if (t
== error_mark_node
32597 || !parens
.require_close (parser
))
32598 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32599 /*or_comma=*/false,
32600 /*consume_paren=*/true);
32602 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32604 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32605 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32606 OMP_CLAUSE_CHAIN (c
) = list
;
32612 defaultmap ( tofrom : scalar ) */
32615 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32616 location_t location
)
32621 matching_parens parens
;
32622 if (!parens
.require_open (parser
))
32625 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32627 cp_parser_error (parser
, "expected %<tofrom%>");
32630 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32631 p
= IDENTIFIER_POINTER (id
);
32632 if (strcmp (p
, "tofrom") != 0)
32634 cp_parser_error (parser
, "expected %<tofrom%>");
32637 cp_lexer_consume_token (parser
->lexer
);
32638 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32641 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32643 cp_parser_error (parser
, "expected %<scalar%>");
32646 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32647 p
= IDENTIFIER_POINTER (id
);
32648 if (strcmp (p
, "scalar") != 0)
32650 cp_parser_error (parser
, "expected %<scalar%>");
32653 cp_lexer_consume_token (parser
->lexer
);
32654 if (!parens
.require_close (parser
))
32657 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32660 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32661 OMP_CLAUSE_CHAIN (c
) = list
;
32665 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32666 /*or_comma=*/false,
32667 /*consume_paren=*/true);
32675 ordered ( constant-expression ) */
32678 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32679 tree list
, location_t location
)
32681 tree c
, num
= NULL_TREE
;
32684 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32685 "ordered", location
);
32687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32689 matching_parens parens
;
32690 parens
.consume_open (parser
);
32692 num
= cp_parser_constant_expression (parser
);
32694 if (!parens
.require_close (parser
))
32695 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32696 /*or_comma=*/false,
32697 /*consume_paren=*/true);
32699 if (num
== error_mark_node
)
32701 num
= fold_non_dependent_expr (num
);
32702 if (!tree_fits_shwi_p (num
)
32703 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32704 || (n
= tree_to_shwi (num
)) <= 0
32707 error_at (location
,
32708 "ordered argument needs positive constant integer "
32714 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32715 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32716 OMP_CLAUSE_CHAIN (c
) = list
;
32721 reduction ( reduction-operator : variable-list )
32723 reduction-operator:
32724 One of: + * - & ^ | && ||
32728 reduction-operator:
32729 One of: + * - & ^ | && || min max
32733 reduction-operator:
32734 One of: + * - & ^ | && ||
32738 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32740 enum tree_code code
= ERROR_MARK
;
32741 tree nlist
, c
, id
= NULL_TREE
;
32743 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32746 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32748 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32749 case CPP_MULT
: code
= MULT_EXPR
; break;
32750 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32751 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32752 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32753 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32754 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32755 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32759 if (code
!= ERROR_MARK
)
32760 cp_lexer_consume_token (parser
->lexer
);
32763 bool saved_colon_corrects_to_scope_p
;
32764 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32765 parser
->colon_corrects_to_scope_p
= false;
32766 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32767 /*check_dependency_p=*/true,
32768 /*template_p=*/NULL
,
32769 /*declarator_p=*/false,
32770 /*optional_p=*/false);
32771 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32772 if (identifier_p (id
))
32774 const char *p
= IDENTIFIER_POINTER (id
);
32776 if (strcmp (p
, "min") == 0)
32778 else if (strcmp (p
, "max") == 0)
32780 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32782 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32784 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32786 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32787 code
= BIT_AND_EXPR
;
32788 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32789 code
= BIT_IOR_EXPR
;
32790 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32791 code
= BIT_XOR_EXPR
;
32792 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32793 code
= TRUTH_ANDIF_EXPR
;
32794 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32795 code
= TRUTH_ORIF_EXPR
;
32796 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32797 tree scope
= parser
->scope
;
32799 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32800 parser
->scope
= NULL_TREE
;
32801 parser
->qualifying_scope
= NULL_TREE
;
32802 parser
->object_scope
= NULL_TREE
;
32806 error ("invalid reduction-identifier");
32808 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32809 /*or_comma=*/false,
32810 /*consume_paren=*/true);
32815 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32818 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32820 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32822 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32823 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32830 schedule ( schedule-kind )
32831 schedule ( schedule-kind , expression )
32834 static | dynamic | guided | runtime | auto
32837 schedule ( schedule-modifier : schedule-kind )
32838 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32846 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32849 int modifiers
= 0, nmodifiers
= 0;
32851 matching_parens parens
;
32852 if (!parens
.require_open (parser
))
32855 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32857 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32859 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32860 const char *p
= IDENTIFIER_POINTER (id
);
32861 if (strcmp ("simd", p
) == 0)
32862 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32863 else if (strcmp ("monotonic", p
) == 0)
32864 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32865 else if (strcmp ("nonmonotonic", p
) == 0)
32866 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32869 cp_lexer_consume_token (parser
->lexer
);
32870 if (nmodifiers
++ == 0
32871 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32872 cp_lexer_consume_token (parser
->lexer
);
32875 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32882 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32883 const char *p
= IDENTIFIER_POINTER (id
);
32888 if (strcmp ("dynamic", p
) != 0)
32890 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32894 if (strcmp ("guided", p
) != 0)
32896 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32900 if (strcmp ("runtime", p
) != 0)
32902 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32909 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32910 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32911 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32912 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32915 cp_lexer_consume_token (parser
->lexer
);
32917 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32918 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32919 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32920 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32922 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32927 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32930 cp_lexer_consume_token (parser
->lexer
);
32932 token
= cp_lexer_peek_token (parser
->lexer
);
32933 t
= cp_parser_assignment_expression (parser
);
32935 if (t
== error_mark_node
)
32937 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32938 error_at (token
->location
, "schedule %<runtime%> does not take "
32939 "a %<chunk_size%> parameter");
32940 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32941 error_at (token
->location
, "schedule %<auto%> does not take "
32942 "a %<chunk_size%> parameter");
32944 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32946 if (!parens
.require_close (parser
))
32949 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32952 OMP_CLAUSE_SCHEDULE_KIND (c
)
32953 = (enum omp_clause_schedule_kind
)
32954 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32956 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32957 OMP_CLAUSE_CHAIN (c
) = list
;
32961 cp_parser_error (parser
, "invalid schedule kind");
32963 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32964 /*or_comma=*/false,
32965 /*consume_paren=*/true);
32973 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32974 tree list
, location_t location
)
32978 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32980 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32981 OMP_CLAUSE_CHAIN (c
) = list
;
32990 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32991 tree list
, location_t location
)
32993 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32994 tree c
= build_omp_clause (location
, code
);
32995 OMP_CLAUSE_CHAIN (c
) = list
;
33006 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33007 enum omp_clause_code code
,
33008 tree list
, location_t location
)
33010 tree c
= build_omp_clause (location
, code
);
33011 OMP_CLAUSE_CHAIN (c
) = list
;
33019 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33020 tree list
, location_t location
)
33022 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33023 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33024 OMP_CLAUSE_CHAIN (c
) = list
;
33033 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33034 enum omp_clause_code code
,
33035 tree list
, location_t location
)
33037 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33038 tree c
= build_omp_clause (location
, code
);
33039 OMP_CLAUSE_CHAIN (c
) = list
;
33044 num_teams ( expression ) */
33047 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33048 location_t location
)
33052 matching_parens parens
;
33053 if (!parens
.require_open (parser
))
33056 t
= cp_parser_expression (parser
);
33058 if (t
== error_mark_node
33059 || !parens
.require_close (parser
))
33060 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33061 /*or_comma=*/false,
33062 /*consume_paren=*/true);
33064 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33065 "num_teams", location
);
33067 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33068 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33069 OMP_CLAUSE_CHAIN (c
) = list
;
33075 thread_limit ( expression ) */
33078 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33079 location_t location
)
33083 matching_parens parens
;
33084 if (!parens
.require_open (parser
))
33087 t
= cp_parser_expression (parser
);
33089 if (t
== error_mark_node
33090 || !parens
.require_close (parser
))
33091 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33092 /*or_comma=*/false,
33093 /*consume_paren=*/true);
33095 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33096 "thread_limit", location
);
33098 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33099 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33100 OMP_CLAUSE_CHAIN (c
) = list
;
33106 aligned ( variable-list )
33107 aligned ( variable-list : constant-expression ) */
33110 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33112 tree nlist
, c
, alignment
= NULL_TREE
;
33115 matching_parens parens
;
33116 if (!parens
.require_open (parser
))
33119 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33124 alignment
= cp_parser_constant_expression (parser
);
33126 if (!parens
.require_close (parser
))
33127 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33128 /*or_comma=*/false,
33129 /*consume_paren=*/true);
33131 if (alignment
== error_mark_node
)
33132 alignment
= NULL_TREE
;
33135 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33136 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33142 linear ( variable-list )
33143 linear ( variable-list : expression )
33146 linear ( modifier ( variable-list ) )
33147 linear ( modifier ( variable-list ) : expression ) */
33150 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33153 tree nlist
, c
, step
= integer_one_node
;
33155 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33157 matching_parens parens
;
33158 if (!parens
.require_open (parser
))
33161 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33163 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33164 const char *p
= IDENTIFIER_POINTER (id
);
33166 if (strcmp ("ref", p
) == 0)
33167 kind
= OMP_CLAUSE_LINEAR_REF
;
33168 else if (strcmp ("val", p
) == 0)
33169 kind
= OMP_CLAUSE_LINEAR_VAL
;
33170 else if (strcmp ("uval", p
) == 0)
33171 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33172 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33173 cp_lexer_consume_token (parser
->lexer
);
33175 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33178 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33179 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33183 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33184 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33186 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33187 else if (!parens
.require_close (parser
))
33188 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33189 /*or_comma=*/false,
33190 /*consume_paren=*/true);
33197 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33198 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33200 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33201 cp_parser_parse_tentatively (parser
);
33202 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33203 /*check_dependency_p=*/true,
33204 /*template_p=*/NULL
,
33205 /*declarator_p=*/false,
33206 /*optional_p=*/false);
33207 if (step
!= error_mark_node
)
33208 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33209 if (step
== error_mark_node
)
33212 cp_parser_abort_tentative_parse (parser
);
33214 else if (!cp_parser_parse_definitely (parser
))
33218 step
= cp_parser_expression (parser
);
33220 if (!parens
.require_close (parser
))
33221 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33222 /*or_comma=*/false,
33223 /*consume_paren=*/true);
33225 if (step
== error_mark_node
)
33229 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33231 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33232 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33239 safelen ( constant-expression ) */
33242 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33243 location_t location
)
33247 matching_parens parens
;
33248 if (!parens
.require_open (parser
))
33251 t
= cp_parser_constant_expression (parser
);
33253 if (t
== error_mark_node
33254 || !parens
.require_close (parser
))
33255 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33256 /*or_comma=*/false,
33257 /*consume_paren=*/true);
33259 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33261 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33262 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33263 OMP_CLAUSE_CHAIN (c
) = list
;
33269 simdlen ( constant-expression ) */
33272 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33273 location_t location
)
33277 matching_parens parens
;
33278 if (!parens
.require_open (parser
))
33281 t
= cp_parser_constant_expression (parser
);
33283 if (t
== error_mark_node
33284 || !parens
.require_close (parser
))
33285 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33286 /*or_comma=*/false,
33287 /*consume_paren=*/true);
33289 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33291 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33292 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33293 OMP_CLAUSE_CHAIN (c
) = list
;
33300 identifier [+/- integer]
33301 vec , identifier [+/- integer]
33305 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33310 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33312 cp_parser_error (parser
, "expected identifier");
33316 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33318 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33319 tree t
, identifier
= cp_parser_identifier (parser
);
33320 tree addend
= NULL
;
33322 if (identifier
== error_mark_node
)
33323 t
= error_mark_node
;
33326 t
= cp_parser_lookup_name_simple
33327 (parser
, identifier
,
33328 cp_lexer_peek_token (parser
->lexer
)->location
);
33329 if (t
== error_mark_node
)
33330 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33335 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33337 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33339 addend
= integer_zero_node
;
33340 goto add_to_vector
;
33342 cp_lexer_consume_token (parser
->lexer
);
33344 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33346 cp_parser_error (parser
, "expected integer");
33350 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33351 if (TREE_CODE (addend
) != INTEGER_CST
)
33353 cp_parser_error (parser
, "expected integer");
33356 cp_lexer_consume_token (parser
->lexer
);
33359 if (t
!= error_mark_node
)
33361 vec
= tree_cons (addend
, t
, vec
);
33363 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33366 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33369 cp_lexer_consume_token (parser
->lexer
);
33372 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33374 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33375 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33376 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33377 OMP_CLAUSE_CHAIN (u
) = list
;
33384 depend ( depend-kind : variable-list )
33392 depend ( sink : vec ) */
33395 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33398 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33400 matching_parens parens
;
33401 if (!parens
.require_open (parser
))
33404 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33406 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33407 const char *p
= IDENTIFIER_POINTER (id
);
33409 if (strcmp ("in", p
) == 0)
33410 kind
= OMP_CLAUSE_DEPEND_IN
;
33411 else if (strcmp ("inout", p
) == 0)
33412 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33413 else if (strcmp ("out", p
) == 0)
33414 kind
= OMP_CLAUSE_DEPEND_OUT
;
33415 else if (strcmp ("source", p
) == 0)
33416 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33417 else if (strcmp ("sink", p
) == 0)
33418 kind
= OMP_CLAUSE_DEPEND_SINK
;
33425 cp_lexer_consume_token (parser
->lexer
);
33427 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33429 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33430 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33431 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33432 OMP_CLAUSE_CHAIN (c
) = list
;
33433 if (!parens
.require_close (parser
))
33434 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33435 /*or_comma=*/false,
33436 /*consume_paren=*/true);
33440 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33443 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33444 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33447 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33450 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33451 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33456 cp_parser_error (parser
, "invalid depend kind");
33458 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33459 /*or_comma=*/false,
33460 /*consume_paren=*/true);
33465 map ( map-kind : variable-list )
33466 map ( variable-list )
33469 alloc | to | from | tofrom
33473 alloc | to | from | tofrom | release | delete
33475 map ( always [,] map-kind: variable-list ) */
33478 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33481 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33482 bool always
= false;
33484 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33487 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33489 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33490 const char *p
= IDENTIFIER_POINTER (id
);
33492 if (strcmp ("always", p
) == 0)
33495 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33497 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33498 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33500 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33504 cp_lexer_consume_token (parser
->lexer
);
33506 cp_lexer_consume_token (parser
->lexer
);
33511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33512 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33514 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33515 const char *p
= IDENTIFIER_POINTER (id
);
33517 if (strcmp ("alloc", p
) == 0)
33518 kind
= GOMP_MAP_ALLOC
;
33519 else if (strcmp ("to", p
) == 0)
33520 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33521 else if (strcmp ("from", p
) == 0)
33522 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33523 else if (strcmp ("tofrom", p
) == 0)
33524 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33525 else if (strcmp ("release", p
) == 0)
33526 kind
= GOMP_MAP_RELEASE
;
33529 cp_parser_error (parser
, "invalid map kind");
33530 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33531 /*or_comma=*/false,
33532 /*consume_paren=*/true);
33535 cp_lexer_consume_token (parser
->lexer
);
33536 cp_lexer_consume_token (parser
->lexer
);
33538 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33539 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33541 kind
= GOMP_MAP_DELETE
;
33542 cp_lexer_consume_token (parser
->lexer
);
33543 cp_lexer_consume_token (parser
->lexer
);
33546 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33549 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33550 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33556 device ( expression ) */
33559 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33560 location_t location
)
33564 matching_parens parens
;
33565 if (!parens
.require_open (parser
))
33568 t
= cp_parser_expression (parser
);
33570 if (t
== error_mark_node
33571 || !parens
.require_close (parser
))
33572 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33573 /*or_comma=*/false,
33574 /*consume_paren=*/true);
33576 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33577 "device", location
);
33579 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33580 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33581 OMP_CLAUSE_CHAIN (c
) = list
;
33587 dist_schedule ( static )
33588 dist_schedule ( static , expression ) */
33591 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33592 location_t location
)
33596 matching_parens parens
;
33597 if (!parens
.require_open (parser
))
33600 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33602 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33604 cp_lexer_consume_token (parser
->lexer
);
33606 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33608 cp_lexer_consume_token (parser
->lexer
);
33610 t
= cp_parser_assignment_expression (parser
);
33612 if (t
== error_mark_node
)
33614 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33616 if (!parens
.require_close (parser
))
33619 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33622 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33624 OMP_CLAUSE_CHAIN (c
) = list
;
33628 cp_parser_error (parser
, "invalid dist_schedule kind");
33630 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33631 /*or_comma=*/false,
33632 /*consume_paren=*/true);
33637 proc_bind ( proc-bind-kind )
33640 master | close | spread */
33643 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33644 location_t location
)
33647 enum omp_clause_proc_bind_kind kind
;
33649 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33652 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33654 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33655 const char *p
= IDENTIFIER_POINTER (id
);
33657 if (strcmp ("master", p
) == 0)
33658 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33659 else if (strcmp ("close", p
) == 0)
33660 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33661 else if (strcmp ("spread", p
) == 0)
33662 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33669 cp_lexer_consume_token (parser
->lexer
);
33670 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33673 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33674 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33676 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33677 OMP_CLAUSE_CHAIN (c
) = list
;
33681 cp_parser_error (parser
, "invalid depend kind");
33683 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33684 /*or_comma=*/false,
33685 /*consume_paren=*/true);
33690 async [( int-expr )] */
33693 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33696 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33698 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33700 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33702 matching_parens parens
;
33703 parens
.consume_open (parser
);
33705 t
= cp_parser_expression (parser
);
33706 if (t
== error_mark_node
33707 || !parens
.require_close (parser
))
33708 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33709 /*or_comma=*/false,
33710 /*consume_paren=*/true);
33713 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33715 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33716 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33717 OMP_CLAUSE_CHAIN (c
) = list
;
33723 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33724 is a bitmask in MASK. Return the list of clauses found. */
33727 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33728 const char *where
, cp_token
*pragma_tok
,
33729 bool finish_p
= true)
33731 tree clauses
= NULL
;
33734 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33737 pragma_omp_clause c_kind
;
33738 omp_clause_code code
;
33739 const char *c_name
;
33740 tree prev
= clauses
;
33742 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33743 cp_lexer_consume_token (parser
->lexer
);
33745 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33746 c_kind
= cp_parser_omp_clause_name (parser
);
33750 case PRAGMA_OACC_CLAUSE_ASYNC
:
33751 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33754 case PRAGMA_OACC_CLAUSE_AUTO
:
33755 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33759 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33760 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33761 c_name
= "collapse";
33763 case PRAGMA_OACC_CLAUSE_COPY
:
33764 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33767 case PRAGMA_OACC_CLAUSE_COPYIN
:
33768 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33771 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33772 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33773 c_name
= "copyout";
33775 case PRAGMA_OACC_CLAUSE_CREATE
:
33776 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33779 case PRAGMA_OACC_CLAUSE_DELETE
:
33780 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33783 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33784 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33785 c_name
= "default";
33787 case PRAGMA_OACC_CLAUSE_DEVICE
:
33788 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33791 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33792 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33793 c_name
= "deviceptr";
33795 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33796 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33797 c_name
= "device_resident";
33799 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33800 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33802 c_name
= "firstprivate";
33804 case PRAGMA_OACC_CLAUSE_GANG
:
33806 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33809 case PRAGMA_OACC_CLAUSE_HOST
:
33810 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33813 case PRAGMA_OACC_CLAUSE_IF
:
33814 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33817 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33818 clauses
= cp_parser_oacc_simple_clause (parser
,
33819 OMP_CLAUSE_INDEPENDENT
,
33821 c_name
= "independent";
33823 case PRAGMA_OACC_CLAUSE_LINK
:
33824 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33827 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33828 code
= OMP_CLAUSE_NUM_GANGS
;
33829 c_name
= "num_gangs";
33830 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33833 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33834 c_name
= "num_workers";
33835 code
= OMP_CLAUSE_NUM_WORKERS
;
33836 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33839 case PRAGMA_OACC_CLAUSE_PRESENT
:
33840 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33841 c_name
= "present";
33843 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33844 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33845 c_name
= "present_or_copy";
33847 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33848 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33849 c_name
= "present_or_copyin";
33851 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33852 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33853 c_name
= "present_or_copyout";
33855 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33856 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33857 c_name
= "present_or_create";
33859 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33860 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33862 c_name
= "private";
33864 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33865 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33866 c_name
= "reduction";
33868 case PRAGMA_OACC_CLAUSE_SELF
:
33869 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33872 case PRAGMA_OACC_CLAUSE_SEQ
:
33873 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33877 case PRAGMA_OACC_CLAUSE_TILE
:
33878 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33881 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33882 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33884 c_name
= "use_device";
33886 case PRAGMA_OACC_CLAUSE_VECTOR
:
33888 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33891 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33892 c_name
= "vector_length";
33893 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33894 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33897 case PRAGMA_OACC_CLAUSE_WAIT
:
33898 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33901 case PRAGMA_OACC_CLAUSE_WORKER
:
33903 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33907 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33913 if (((mask
>> c_kind
) & 1) == 0)
33915 /* Remove the invalid clause(s) from the list to avoid
33916 confusing the rest of the compiler. */
33918 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33923 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33926 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33931 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33932 is a bitmask in MASK. Return the list of clauses found; the result
33933 of clause default goes in *pdefault. */
33936 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33937 const char *where
, cp_token
*pragma_tok
,
33938 bool finish_p
= true)
33940 tree clauses
= NULL
;
33942 cp_token
*token
= NULL
;
33944 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33946 pragma_omp_clause c_kind
;
33947 const char *c_name
;
33948 tree prev
= clauses
;
33950 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33951 cp_lexer_consume_token (parser
->lexer
);
33953 token
= cp_lexer_peek_token (parser
->lexer
);
33954 c_kind
= cp_parser_omp_clause_name (parser
);
33958 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33959 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33961 c_name
= "collapse";
33963 case PRAGMA_OMP_CLAUSE_COPYIN
:
33964 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33967 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33968 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33970 c_name
= "copyprivate";
33972 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33973 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33974 token
->location
, false);
33975 c_name
= "default";
33977 case PRAGMA_OMP_CLAUSE_FINAL
:
33978 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33981 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33982 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33984 c_name
= "firstprivate";
33986 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33987 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33989 c_name
= "grainsize";
33991 case PRAGMA_OMP_CLAUSE_HINT
:
33992 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33996 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33997 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33999 c_name
= "defaultmap";
34001 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
34002 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34004 c_name
= "use_device_ptr";
34006 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
34007 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
34009 c_name
= "is_device_ptr";
34011 case PRAGMA_OMP_CLAUSE_IF
:
34012 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
34016 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
34017 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
34019 c_name
= "lastprivate";
34021 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
34022 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
34024 c_name
= "mergeable";
34026 case PRAGMA_OMP_CLAUSE_NOWAIT
:
34027 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
34030 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
34031 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
34033 c_name
= "num_tasks";
34035 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
34036 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
34038 c_name
= "num_threads";
34040 case PRAGMA_OMP_CLAUSE_ORDERED
:
34041 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
34043 c_name
= "ordered";
34045 case PRAGMA_OMP_CLAUSE_PRIORITY
:
34046 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
34048 c_name
= "priority";
34050 case PRAGMA_OMP_CLAUSE_PRIVATE
:
34051 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34053 c_name
= "private";
34055 case PRAGMA_OMP_CLAUSE_REDUCTION
:
34056 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
34057 c_name
= "reduction";
34059 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
34060 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
34062 c_name
= "schedule";
34064 case PRAGMA_OMP_CLAUSE_SHARED
:
34065 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
34069 case PRAGMA_OMP_CLAUSE_UNTIED
:
34070 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
34074 case PRAGMA_OMP_CLAUSE_INBRANCH
:
34075 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
34076 clauses
, token
->location
);
34077 c_name
= "inbranch";
34079 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
34080 clauses
= cp_parser_omp_clause_branch (parser
,
34081 OMP_CLAUSE_NOTINBRANCH
,
34082 clauses
, token
->location
);
34083 c_name
= "notinbranch";
34085 case PRAGMA_OMP_CLAUSE_PARALLEL
:
34086 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
34087 clauses
, token
->location
);
34088 c_name
= "parallel";
34092 error_at (token
->location
, "%qs must be the first clause of %qs",
34097 case PRAGMA_OMP_CLAUSE_FOR
:
34098 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34099 clauses
, token
->location
);
34102 goto clause_not_first
;
34104 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34105 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34106 clauses
, token
->location
);
34107 c_name
= "sections";
34109 goto clause_not_first
;
34111 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34112 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34113 clauses
, token
->location
);
34114 c_name
= "taskgroup";
34116 goto clause_not_first
;
34118 case PRAGMA_OMP_CLAUSE_LINK
:
34119 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34122 case PRAGMA_OMP_CLAUSE_TO
:
34123 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34124 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34127 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34130 case PRAGMA_OMP_CLAUSE_FROM
:
34131 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34134 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34135 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34137 c_name
= "uniform";
34139 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34140 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34142 c_name
= "num_teams";
34144 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34145 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34147 c_name
= "thread_limit";
34149 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34150 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34151 c_name
= "aligned";
34153 case PRAGMA_OMP_CLAUSE_LINEAR
:
34155 bool declare_simd
= false;
34156 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34157 declare_simd
= true;
34158 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34162 case PRAGMA_OMP_CLAUSE_DEPEND
:
34163 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34167 case PRAGMA_OMP_CLAUSE_MAP
:
34168 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34171 case PRAGMA_OMP_CLAUSE_DEVICE
:
34172 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34176 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34177 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34179 c_name
= "dist_schedule";
34181 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34182 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34184 c_name
= "proc_bind";
34186 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34187 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34189 c_name
= "safelen";
34191 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34192 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34194 c_name
= "simdlen";
34196 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34197 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34199 c_name
= "nogroup";
34201 case PRAGMA_OMP_CLAUSE_THREADS
:
34203 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34204 clauses
, token
->location
);
34205 c_name
= "threads";
34207 case PRAGMA_OMP_CLAUSE_SIMD
:
34209 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34210 clauses
, token
->location
);
34214 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34220 if (((mask
>> c_kind
) & 1) == 0)
34222 /* Remove the invalid clause(s) from the list to avoid
34223 confusing the rest of the compiler. */
34225 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34229 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34232 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34233 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34235 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34244 In practice, we're also interested in adding the statement to an
34245 outer node. So it is convenient if we work around the fact that
34246 cp_parser_statement calls add_stmt. */
34249 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34251 unsigned save
= parser
->in_statement
;
34253 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34254 This preserves the "not within loop or switch" style error messages
34255 for nonsense cases like
34261 if (parser
->in_statement
)
34262 parser
->in_statement
= IN_OMP_BLOCK
;
34268 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34270 parser
->in_statement
= save
;
34274 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34276 tree stmt
= begin_omp_structured_block ();
34277 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34279 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34281 cp_parser_end_omp_structured_block (parser
, save
);
34282 return finish_omp_structured_block (stmt
);
34286 # pragma omp atomic new-line
34290 x binop= expr | x++ | ++x | x-- | --x
34292 +, *, -, /, &, ^, |, <<, >>
34294 where x is an lvalue expression with scalar type.
34297 # pragma omp atomic new-line
34300 # pragma omp atomic read new-line
34303 # pragma omp atomic write new-line
34306 # pragma omp atomic update new-line
34309 # pragma omp atomic capture new-line
34312 # pragma omp atomic capture new-line
34320 expression-stmt | x = x binop expr
34322 v = expression-stmt
34324 { v = x; update-stmt; } | { update-stmt; v = x; }
34328 expression-stmt | x = x binop expr | x = expr binop x
34332 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34334 where x and v are lvalue expressions with scalar type. */
34337 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34339 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34340 tree rhs1
= NULL_TREE
, orig_lhs
;
34341 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34342 bool structured_block
= false;
34343 bool seq_cst
= false;
34345 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34347 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34348 const char *p
= IDENTIFIER_POINTER (id
);
34350 if (!strcmp (p
, "seq_cst"))
34353 cp_lexer_consume_token (parser
->lexer
);
34354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34355 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34356 cp_lexer_consume_token (parser
->lexer
);
34359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34361 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34362 const char *p
= IDENTIFIER_POINTER (id
);
34364 if (!strcmp (p
, "read"))
34365 code
= OMP_ATOMIC_READ
;
34366 else if (!strcmp (p
, "write"))
34368 else if (!strcmp (p
, "update"))
34370 else if (!strcmp (p
, "capture"))
34371 code
= OMP_ATOMIC_CAPTURE_NEW
;
34375 cp_lexer_consume_token (parser
->lexer
);
34379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34380 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34381 cp_lexer_consume_token (parser
->lexer
);
34383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34385 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34386 const char *p
= IDENTIFIER_POINTER (id
);
34388 if (!strcmp (p
, "seq_cst"))
34391 cp_lexer_consume_token (parser
->lexer
);
34395 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34399 case OMP_ATOMIC_READ
:
34400 case NOP_EXPR
: /* atomic write */
34401 v
= cp_parser_unary_expression (parser
);
34402 if (v
== error_mark_node
)
34404 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34406 if (code
== NOP_EXPR
)
34407 lhs
= cp_parser_expression (parser
);
34409 lhs
= cp_parser_unary_expression (parser
);
34410 if (lhs
== error_mark_node
)
34412 if (code
== NOP_EXPR
)
34414 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34422 case OMP_ATOMIC_CAPTURE_NEW
:
34423 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34425 cp_lexer_consume_token (parser
->lexer
);
34426 structured_block
= true;
34430 v
= cp_parser_unary_expression (parser
);
34431 if (v
== error_mark_node
)
34433 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34441 lhs
= cp_parser_unary_expression (parser
);
34443 switch (TREE_CODE (lhs
))
34448 case POSTINCREMENT_EXPR
:
34449 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34450 code
= OMP_ATOMIC_CAPTURE_OLD
;
34452 case PREINCREMENT_EXPR
:
34453 lhs
= TREE_OPERAND (lhs
, 0);
34454 opcode
= PLUS_EXPR
;
34455 rhs
= integer_one_node
;
34458 case POSTDECREMENT_EXPR
:
34459 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34460 code
= OMP_ATOMIC_CAPTURE_OLD
;
34462 case PREDECREMENT_EXPR
:
34463 lhs
= TREE_OPERAND (lhs
, 0);
34464 opcode
= MINUS_EXPR
;
34465 rhs
= integer_one_node
;
34468 case COMPOUND_EXPR
:
34469 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34470 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34471 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34472 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34473 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34474 (TREE_OPERAND (lhs
, 1), 0), 0)))
34476 /* Undo effects of boolean_increment for post {in,de}crement. */
34477 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34480 if (TREE_CODE (lhs
) == MODIFY_EXPR
34481 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34483 /* Undo effects of boolean_increment. */
34484 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34486 /* This is pre or post increment. */
34487 rhs
= TREE_OPERAND (lhs
, 1);
34488 lhs
= TREE_OPERAND (lhs
, 0);
34490 if (code
== OMP_ATOMIC_CAPTURE_NEW
34491 && !structured_block
34492 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34493 code
= OMP_ATOMIC_CAPTURE_OLD
;
34499 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34502 opcode
= MULT_EXPR
;
34505 opcode
= TRUNC_DIV_EXPR
;
34508 opcode
= PLUS_EXPR
;
34511 opcode
= MINUS_EXPR
;
34513 case CPP_LSHIFT_EQ
:
34514 opcode
= LSHIFT_EXPR
;
34516 case CPP_RSHIFT_EQ
:
34517 opcode
= RSHIFT_EXPR
;
34520 opcode
= BIT_AND_EXPR
;
34523 opcode
= BIT_IOR_EXPR
;
34526 opcode
= BIT_XOR_EXPR
;
34529 enum cp_parser_prec oprec
;
34531 cp_lexer_consume_token (parser
->lexer
);
34532 cp_parser_parse_tentatively (parser
);
34533 rhs1
= cp_parser_simple_cast_expression (parser
);
34534 if (rhs1
== error_mark_node
)
34536 cp_parser_abort_tentative_parse (parser
);
34537 cp_parser_simple_cast_expression (parser
);
34540 token
= cp_lexer_peek_token (parser
->lexer
);
34541 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34543 cp_parser_abort_tentative_parse (parser
);
34544 cp_parser_parse_tentatively (parser
);
34545 rhs
= cp_parser_binary_expression (parser
, false, true,
34546 PREC_NOT_OPERATOR
, NULL
);
34547 if (rhs
== error_mark_node
)
34549 cp_parser_abort_tentative_parse (parser
);
34550 cp_parser_binary_expression (parser
, false, true,
34551 PREC_NOT_OPERATOR
, NULL
);
34554 switch (TREE_CODE (rhs
))
34557 case TRUNC_DIV_EXPR
:
34566 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34568 if (cp_parser_parse_definitely (parser
))
34570 opcode
= TREE_CODE (rhs
);
34571 rhs1
= TREE_OPERAND (rhs
, 0);
34572 rhs
= TREE_OPERAND (rhs
, 1);
34582 cp_parser_abort_tentative_parse (parser
);
34583 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34585 rhs
= cp_parser_expression (parser
);
34586 if (rhs
== error_mark_node
)
34592 cp_parser_error (parser
,
34593 "invalid form of %<#pragma omp atomic%>");
34596 if (!cp_parser_parse_definitely (parser
))
34598 switch (token
->type
)
34600 case CPP_SEMICOLON
:
34601 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34603 code
= OMP_ATOMIC_CAPTURE_OLD
;
34608 cp_lexer_consume_token (parser
->lexer
);
34611 else if (structured_block
)
34618 cp_parser_error (parser
,
34619 "invalid form of %<#pragma omp atomic%>");
34622 opcode
= MULT_EXPR
;
34625 opcode
= TRUNC_DIV_EXPR
;
34628 opcode
= PLUS_EXPR
;
34631 opcode
= MINUS_EXPR
;
34634 opcode
= LSHIFT_EXPR
;
34637 opcode
= RSHIFT_EXPR
;
34640 opcode
= BIT_AND_EXPR
;
34643 opcode
= BIT_IOR_EXPR
;
34646 opcode
= BIT_XOR_EXPR
;
34649 cp_parser_error (parser
,
34650 "invalid operator for %<#pragma omp atomic%>");
34653 oprec
= TOKEN_PRECEDENCE (token
);
34654 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34655 if (commutative_tree_code (opcode
))
34656 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34657 cp_lexer_consume_token (parser
->lexer
);
34658 rhs
= cp_parser_binary_expression (parser
, false, false,
34660 if (rhs
== error_mark_node
)
34665 cp_parser_error (parser
,
34666 "invalid operator for %<#pragma omp atomic%>");
34669 cp_lexer_consume_token (parser
->lexer
);
34671 rhs
= cp_parser_expression (parser
);
34672 if (rhs
== error_mark_node
)
34677 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34679 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34681 v
= cp_parser_unary_expression (parser
);
34682 if (v
== error_mark_node
)
34684 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34686 lhs1
= cp_parser_unary_expression (parser
);
34687 if (lhs1
== error_mark_node
)
34690 if (structured_block
)
34692 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34693 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34696 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34697 if (!structured_block
)
34698 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34702 cp_parser_skip_to_end_of_block_or_statement (parser
);
34703 if (structured_block
)
34705 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34706 cp_lexer_consume_token (parser
->lexer
);
34707 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34709 cp_parser_skip_to_end_of_block_or_statement (parser
);
34710 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34711 cp_lexer_consume_token (parser
->lexer
);
34718 # pragma omp barrier new-line */
34721 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34723 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34724 finish_omp_barrier ();
34728 # pragma omp critical [(name)] new-line
34732 # pragma omp critical [(name) [hint(expression)]] new-line
34733 structured-block */
34735 #define OMP_CRITICAL_CLAUSE_MASK \
34736 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34739 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34741 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34743 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34745 matching_parens parens
;
34746 parens
.consume_open (parser
);
34748 name
= cp_parser_identifier (parser
);
34750 if (name
== error_mark_node
34751 || !parens
.require_close (parser
))
34752 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34753 /*or_comma=*/false,
34754 /*consume_paren=*/true);
34755 if (name
== error_mark_node
)
34758 clauses
= cp_parser_omp_all_clauses (parser
,
34759 OMP_CRITICAL_CLAUSE_MASK
,
34760 "#pragma omp critical", pragma_tok
);
34763 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34765 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34766 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34770 # pragma omp flush flush-vars[opt] new-line
34773 ( variable-list ) */
34776 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34778 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34779 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34780 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34782 finish_omp_flush ();
34785 /* Helper function, to parse omp for increment expression. */
34788 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34790 tree cond
= cp_parser_binary_expression (parser
, false, true,
34791 PREC_NOT_OPERATOR
, NULL
);
34792 if (cond
== error_mark_node
34793 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34795 cp_parser_skip_to_end_of_statement (parser
);
34796 return error_mark_node
;
34799 switch (TREE_CODE (cond
))
34807 /* Fall through: OpenMP disallows NE_EXPR. */
34808 gcc_fallthrough ();
34810 return error_mark_node
;
34813 /* If decl is an iterator, preserve LHS and RHS of the relational
34814 expr until finish_omp_for. */
34816 && (type_dependent_expression_p (decl
)
34817 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34820 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34822 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34823 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34824 /*overload=*/NULL
, tf_warning_or_error
);
34827 /* Helper function, to parse omp for increment expression. */
34830 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34832 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34838 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34840 op
= (token
->type
== CPP_PLUS_PLUS
34841 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34842 cp_lexer_consume_token (parser
->lexer
);
34843 lhs
= cp_parser_simple_cast_expression (parser
);
34845 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34846 return error_mark_node
;
34847 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34850 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34852 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34853 return error_mark_node
;
34855 token
= cp_lexer_peek_token (parser
->lexer
);
34856 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34858 op
= (token
->type
== CPP_PLUS_PLUS
34859 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34860 cp_lexer_consume_token (parser
->lexer
);
34861 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34864 op
= cp_parser_assignment_operator_opt (parser
);
34865 if (op
== ERROR_MARK
)
34866 return error_mark_node
;
34868 if (op
!= NOP_EXPR
)
34870 rhs
= cp_parser_assignment_expression (parser
);
34871 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34872 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34875 lhs
= cp_parser_binary_expression (parser
, false, false,
34876 PREC_ADDITIVE_EXPRESSION
, NULL
);
34877 token
= cp_lexer_peek_token (parser
->lexer
);
34878 decl_first
= (lhs
== decl
34879 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34882 if (token
->type
!= CPP_PLUS
34883 && token
->type
!= CPP_MINUS
)
34884 return error_mark_node
;
34888 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34889 cp_lexer_consume_token (parser
->lexer
);
34890 rhs
= cp_parser_binary_expression (parser
, false, false,
34891 PREC_ADDITIVE_EXPRESSION
, NULL
);
34892 token
= cp_lexer_peek_token (parser
->lexer
);
34893 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34895 if (lhs
== NULL_TREE
)
34897 if (op
== PLUS_EXPR
)
34900 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34901 tf_warning_or_error
);
34904 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34905 ERROR_MARK
, NULL
, tf_warning_or_error
);
34908 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34913 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34914 || op
== MINUS_EXPR
)
34915 return error_mark_node
;
34916 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34919 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34921 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34924 /* Parse the initialization statement of an OpenMP for loop.
34926 Return true if the resulting construct should have an
34927 OMP_CLAUSE_PRIVATE added to it. */
34930 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34931 tree
&this_pre_body
,
34932 vec
<tree
, va_gc
> *for_block
,
34938 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34941 tree add_private_clause
= NULL_TREE
;
34943 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34947 integer-type var = lb
34948 random-access-iterator-type var = lb
34949 pointer-type var = lb
34951 cp_decl_specifier_seq type_specifiers
;
34953 /* First, try to parse as an initialized declaration. See
34954 cp_parser_condition, from whence the bulk of this is copied. */
34956 cp_parser_parse_tentatively (parser
);
34957 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34958 /*is_trailing_return=*/false,
34960 if (cp_parser_parse_definitely (parser
))
34962 /* If parsing a type specifier seq succeeded, then this
34963 MUST be a initialized declaration. */
34964 tree asm_specification
, attributes
;
34965 cp_declarator
*declarator
;
34967 declarator
= cp_parser_declarator (parser
,
34968 CP_PARSER_DECLARATOR_NAMED
,
34969 /*ctor_dtor_or_conv_p=*/NULL
,
34970 /*parenthesized_p=*/NULL
,
34971 /*member_p=*/false,
34972 /*friend_p=*/false);
34973 attributes
= cp_parser_attributes_opt (parser
);
34974 asm_specification
= cp_parser_asm_specification_opt (parser
);
34976 if (declarator
== cp_error_declarator
)
34977 cp_parser_skip_to_end_of_statement (parser
);
34981 tree pushed_scope
, auto_node
;
34983 decl
= start_decl (declarator
, &type_specifiers
,
34984 SD_INITIALIZED
, attributes
,
34985 /*prefix_attributes=*/NULL_TREE
,
34988 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34989 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34991 if (cp_lexer_next_token_is (parser
->lexer
,
34993 error ("parenthesized initialization is not allowed in "
34994 "OpenMP %<for%> loop");
34996 /* Trigger an error. */
34997 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34999 init
= error_mark_node
;
35000 cp_parser_skip_to_end_of_statement (parser
);
35002 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
35003 || type_dependent_expression_p (decl
)
35006 bool is_direct_init
, is_non_constant_init
;
35008 init
= cp_parser_initializer (parser
,
35010 &is_non_constant_init
);
35015 = do_auto_deduction (TREE_TYPE (decl
), init
,
35018 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
35019 && !type_dependent_expression_p (decl
))
35023 cp_finish_decl (decl
, init
, !is_non_constant_init
,
35025 LOOKUP_ONLYCONVERTING
);
35027 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
35029 vec_safe_push (for_block
, this_pre_body
);
35034 init
= pop_stmt_list (this_pre_body
);
35035 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
35037 tree_stmt_iterator i
= tsi_start (init
);
35038 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35039 while (!tsi_end_p (i
))
35041 tree t
= tsi_stmt (i
);
35042 if (TREE_CODE (t
) == DECL_EXPR
35043 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
35046 vec_safe_push (for_block
, t
);
35051 if (tsi_one_before_end_p (i
))
35053 tree t
= tsi_stmt (i
);
35055 free_stmt_list (init
);
35060 this_pre_body
= NULL_TREE
;
35065 cp_lexer_consume_token (parser
->lexer
);
35066 init
= cp_parser_assignment_expression (parser
);
35069 if (TYPE_REF_P (TREE_TYPE (decl
)))
35070 init
= error_mark_node
;
35072 cp_finish_decl (decl
, NULL_TREE
,
35073 /*init_const_expr_p=*/false,
35075 LOOKUP_ONLYCONVERTING
);
35079 pop_scope (pushed_scope
);
35085 /* If parsing a type specifier sequence failed, then
35086 this MUST be a simple expression. */
35087 cp_parser_parse_tentatively (parser
);
35088 decl
= cp_parser_primary_expression (parser
, false, false,
35090 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
35091 if (!cp_parser_error_occurred (parser
)
35093 && (TREE_CODE (decl
) == COMPONENT_REF
35094 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
35096 cp_parser_abort_tentative_parse (parser
);
35097 cp_parser_parse_tentatively (parser
);
35098 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35099 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35100 /*check_dependency_p=*/true,
35101 /*template_p=*/NULL
,
35102 /*declarator_p=*/false,
35103 /*optional_p=*/false);
35104 if (name
!= error_mark_node
35105 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35107 decl
= cp_parser_lookup_name_simple (parser
, name
,
35109 if (TREE_CODE (decl
) == FIELD_DECL
)
35110 add_private_clause
= omp_privatize_field (decl
, false);
35112 cp_parser_abort_tentative_parse (parser
);
35113 cp_parser_parse_tentatively (parser
);
35114 decl
= cp_parser_primary_expression (parser
, false, false,
35117 if (!cp_parser_error_occurred (parser
)
35120 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35124 cp_parser_parse_definitely (parser
);
35125 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35126 rhs
= cp_parser_assignment_expression (parser
);
35128 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35131 tf_warning_or_error
));
35132 if (!add_private_clause
)
35133 add_private_clause
= decl
;
35138 cp_parser_abort_tentative_parse (parser
);
35139 init
= cp_parser_expression (parser
);
35142 if (TREE_CODE (init
) == MODIFY_EXPR
35143 || TREE_CODE (init
) == MODOP_EXPR
)
35144 real_decl
= TREE_OPERAND (init
, 0);
35148 return add_private_clause
;
35151 /* Parse the restricted form of the for statement allowed by OpenMP. */
35154 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35155 tree
*cclauses
, bool *if_p
)
35157 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35158 tree real_decl
, initv
, condv
, incrv
, declv
;
35159 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35160 location_t loc_first
;
35161 bool collapse_err
= false;
35162 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35163 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35164 auto_vec
<tree
, 4> orig_inits
;
35165 bool tiling
= false;
35167 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35168 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35169 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35170 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35173 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35175 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35176 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35179 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35182 if (ordered
&& ordered
< collapse
)
35184 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35185 "%<ordered%> clause parameter is less than %<collapse%>");
35186 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35187 = build_int_cst (NULL_TREE
, collapse
);
35188 ordered
= collapse
;
35192 for (tree
*pc
= &clauses
; *pc
; )
35193 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35195 error_at (OMP_CLAUSE_LOCATION (*pc
),
35196 "%<linear%> clause may not be specified together "
35197 "with %<ordered%> clause with a parameter");
35198 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35201 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35204 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35205 count
= ordered
? ordered
: collapse
;
35207 declv
= make_tree_vec (count
);
35208 initv
= make_tree_vec (count
);
35209 condv
= make_tree_vec (count
);
35210 incrv
= make_tree_vec (count
);
35212 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35214 for (i
= 0; i
< count
; i
++)
35216 int bracecount
= 0;
35217 tree add_private_clause
= NULL_TREE
;
35220 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35223 cp_parser_error (parser
, "for statement expected");
35226 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35228 matching_parens parens
;
35229 if (!parens
.require_open (parser
))
35232 init
= orig_init
= decl
= real_decl
= NULL
;
35233 this_pre_body
= push_stmt_list ();
35236 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35237 init
, orig_init
, decl
, real_decl
);
35239 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35242 this_pre_body
= pop_stmt_list (this_pre_body
);
35246 pre_body
= push_stmt_list ();
35248 add_stmt (this_pre_body
);
35249 pre_body
= pop_stmt_list (pre_body
);
35252 pre_body
= this_pre_body
;
35257 if (cclauses
!= NULL
35258 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35259 && real_decl
!= NULL_TREE
)
35262 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35263 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35264 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35266 error_at (loc
, "iteration variable %qD"
35267 " should not be firstprivate", real_decl
);
35268 *c
= OMP_CLAUSE_CHAIN (*c
);
35270 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35271 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35273 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35275 *c
= OMP_CLAUSE_CHAIN (*c
);
35276 if (code
== OMP_SIMD
)
35278 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35279 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35283 OMP_CLAUSE_CHAIN (l
) = clauses
;
35286 add_private_clause
= NULL_TREE
;
35290 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35291 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35292 add_private_clause
= NULL_TREE
;
35293 c
= &OMP_CLAUSE_CHAIN (*c
);
35297 if (add_private_clause
)
35300 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35302 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35303 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35304 && OMP_CLAUSE_DECL (c
) == decl
)
35306 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35307 && OMP_CLAUSE_DECL (c
) == decl
)
35308 error_at (loc
, "iteration variable %qD "
35309 "should not be firstprivate",
35311 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35312 && OMP_CLAUSE_DECL (c
) == decl
)
35313 error_at (loc
, "iteration variable %qD should not be reduction",
35318 if (code
!= OMP_SIMD
)
35319 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35320 else if (collapse
== 1)
35321 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35323 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35324 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35325 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35328 OMP_CLAUSE_CHAIN (c
) = clauses
;
35330 /* For linear, signal that we need to fill up
35331 the so far unknown linear step. */
35332 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35333 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35339 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35340 cond
= cp_parser_omp_for_cond (parser
, decl
);
35341 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35344 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35346 /* If decl is an iterator, preserve the operator on decl
35347 until finish_omp_for. */
35349 && ((processing_template_decl
35350 && (TREE_TYPE (real_decl
) == NULL_TREE
35351 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
35352 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35353 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35355 incr
= cp_parser_expression (parser
);
35356 if (!EXPR_HAS_LOCATION (incr
))
35357 protected_set_expr_location (incr
, input_location
);
35360 if (!parens
.require_close (parser
))
35361 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35362 /*or_comma=*/false,
35363 /*consume_paren=*/true);
35365 TREE_VEC_ELT (declv
, i
) = decl
;
35366 TREE_VEC_ELT (initv
, i
) = init
;
35367 TREE_VEC_ELT (condv
, i
) = cond
;
35368 TREE_VEC_ELT (incrv
, i
) = incr
;
35371 orig_inits
.safe_grow_cleared (i
+ 1);
35372 orig_inits
[i
] = orig_init
;
35375 if (i
== count
- 1)
35378 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35379 in between the collapsed for loops to be still considered perfectly
35380 nested. Hopefully the final version clarifies this.
35381 For now handle (multiple) {'s and empty statements. */
35382 cp_parser_parse_tentatively (parser
);
35385 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35387 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35389 cp_lexer_consume_token (parser
->lexer
);
35392 else if (bracecount
35393 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35394 cp_lexer_consume_token (parser
->lexer
);
35397 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35398 error_at (loc
, "not enough for loops to collapse");
35399 collapse_err
= true;
35400 cp_parser_abort_tentative_parse (parser
);
35408 cp_parser_parse_definitely (parser
);
35409 nbraces
+= bracecount
;
35416 /* Note that we saved the original contents of this flag when we entered
35417 the structured block, and so we don't need to re-save it here. */
35418 parser
->in_statement
= IN_OMP_FOR
;
35420 /* Note that the grammar doesn't call for a structured block here,
35421 though the loop as a whole is a structured block. */
35422 body
= push_stmt_list ();
35423 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35424 body
= pop_stmt_list (body
);
35426 if (declv
== NULL_TREE
)
35429 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35430 body
, pre_body
, &orig_inits
, clauses
);
35434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35436 cp_lexer_consume_token (parser
->lexer
);
35439 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35440 cp_lexer_consume_token (parser
->lexer
);
35445 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35446 "collapsed loops not perfectly nested");
35448 collapse_err
= true;
35449 cp_parser_statement_seq_opt (parser
, NULL
);
35450 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35455 while (!for_block
->is_empty ())
35457 tree t
= for_block
->pop ();
35458 if (TREE_CODE (t
) == STATEMENT_LIST
)
35459 add_stmt (pop_stmt_list (t
));
35463 release_tree_vector (for_block
);
35468 /* Helper function for OpenMP parsing, split clauses and call
35469 finish_omp_clauses on each of the set of clauses afterwards. */
35472 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35473 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35476 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35477 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35479 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35483 #pragma omp simd simd-clause[optseq] new-line
35486 #define OMP_SIMD_CLAUSE_MASK \
35487 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35497 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35498 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35501 tree clauses
, sb
, ret
;
35503 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35505 strcat (p_name
, " simd");
35506 mask
|= OMP_SIMD_CLAUSE_MASK
;
35508 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35512 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35513 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35514 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35515 OMP_CLAUSE_ORDERED
);
35516 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35518 error_at (OMP_CLAUSE_LOCATION (c
),
35519 "%<ordered%> clause with parameter may not be specified "
35520 "on %qs construct", p_name
);
35521 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35525 sb
= begin_omp_structured_block ();
35526 save
= cp_parser_begin_omp_structured_block (parser
);
35528 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35530 cp_parser_end_omp_structured_block (parser
, save
);
35531 add_stmt (finish_omp_structured_block (sb
));
35537 #pragma omp for for-clause[optseq] new-line
35541 #pragma omp for simd for-simd-clause[optseq] new-line
35544 #define OMP_FOR_CLAUSE_MASK \
35545 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35556 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35557 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35560 tree clauses
, sb
, ret
;
35562 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35564 strcat (p_name
, " for");
35565 mask
|= OMP_FOR_CLAUSE_MASK
;
35566 /* parallel for{, simd} disallows nowait clause, but for
35567 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35568 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35569 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35570 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35571 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35572 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35574 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35576 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35577 const char *p
= IDENTIFIER_POINTER (id
);
35579 if (strcmp (p
, "simd") == 0)
35581 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35582 if (cclauses
== NULL
)
35583 cclauses
= cclauses_buf
;
35585 cp_lexer_consume_token (parser
->lexer
);
35586 if (!flag_openmp
) /* flag_openmp_simd */
35587 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35589 sb
= begin_omp_structured_block ();
35590 save
= cp_parser_begin_omp_structured_block (parser
);
35591 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35593 cp_parser_end_omp_structured_block (parser
, save
);
35594 tree body
= finish_omp_structured_block (sb
);
35597 ret
= make_node (OMP_FOR
);
35598 TREE_TYPE (ret
) = void_type_node
;
35599 OMP_FOR_BODY (ret
) = body
;
35600 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35601 SET_EXPR_LOCATION (ret
, loc
);
35606 if (!flag_openmp
) /* flag_openmp_simd */
35608 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35612 /* Composite distribute parallel for disallows linear clause. */
35613 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35614 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35616 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35620 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35621 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35624 sb
= begin_omp_structured_block ();
35625 save
= cp_parser_begin_omp_structured_block (parser
);
35627 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35629 cp_parser_end_omp_structured_block (parser
, save
);
35630 add_stmt (finish_omp_structured_block (sb
));
35636 # pragma omp master new-line
35637 structured-block */
35640 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35642 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35643 return c_finish_omp_master (input_location
,
35644 cp_parser_omp_structured_block (parser
, if_p
));
35648 # pragma omp ordered new-line
35652 # pragma omp ordered ordered-clauses new-line
35653 structured-block */
35655 #define OMP_ORDERED_CLAUSE_MASK \
35656 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35659 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35660 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35663 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35664 enum pragma_context context
, bool *if_p
)
35666 location_t loc
= pragma_tok
->location
;
35668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35670 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35671 const char *p
= IDENTIFIER_POINTER (id
);
35673 if (strcmp (p
, "depend") == 0)
35675 if (!flag_openmp
) /* flag_openmp_simd */
35677 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35680 if (context
== pragma_stmt
)
35682 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35683 "%<depend%> clause may only be used in compound "
35685 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35689 = cp_parser_omp_all_clauses (parser
,
35690 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35691 "#pragma omp ordered", pragma_tok
);
35692 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35698 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35699 "#pragma omp ordered", pragma_tok
);
35701 if (!flag_openmp
/* flag_openmp_simd */
35702 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35705 c_finish_omp_ordered (loc
, clauses
,
35706 cp_parser_omp_structured_block (parser
, if_p
));
35713 { section-sequence }
35716 section-directive[opt] structured-block
35717 section-sequence section-directive structured-block */
35720 cp_parser_omp_sections_scope (cp_parser
*parser
)
35722 tree stmt
, substmt
;
35723 bool error_suppress
= false;
35726 matching_braces braces
;
35727 if (!braces
.require_open (parser
))
35730 stmt
= push_stmt_list ();
35732 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35733 != PRAGMA_OMP_SECTION
)
35735 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35736 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35737 add_stmt (substmt
);
35742 tok
= cp_lexer_peek_token (parser
->lexer
);
35743 if (tok
->type
== CPP_CLOSE_BRACE
)
35745 if (tok
->type
== CPP_EOF
)
35748 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35750 cp_lexer_consume_token (parser
->lexer
);
35751 cp_parser_require_pragma_eol (parser
, tok
);
35752 error_suppress
= false;
35754 else if (!error_suppress
)
35756 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35757 error_suppress
= true;
35760 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35761 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35762 add_stmt (substmt
);
35764 braces
.require_close (parser
);
35766 substmt
= pop_stmt_list (stmt
);
35768 stmt
= make_node (OMP_SECTIONS
);
35769 TREE_TYPE (stmt
) = void_type_node
;
35770 OMP_SECTIONS_BODY (stmt
) = substmt
;
35777 # pragma omp sections sections-clause[optseq] newline
35780 #define OMP_SECTIONS_CLAUSE_MASK \
35781 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35788 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35789 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35792 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35794 strcat (p_name
, " sections");
35795 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35797 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35799 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35803 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35804 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35807 ret
= cp_parser_omp_sections_scope (parser
);
35809 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35815 # pragma omp parallel parallel-clause[optseq] new-line
35817 # pragma omp parallel for parallel-for-clause[optseq] new-line
35819 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35823 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35824 structured-block */
35826 #define OMP_PARALLEL_CLAUSE_MASK \
35827 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35838 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35839 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35842 tree stmt
, clauses
, block
;
35844 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35846 strcat (p_name
, " parallel");
35847 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35848 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35849 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35850 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35851 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35853 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35855 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35856 if (cclauses
== NULL
)
35857 cclauses
= cclauses_buf
;
35859 cp_lexer_consume_token (parser
->lexer
);
35860 if (!flag_openmp
) /* flag_openmp_simd */
35861 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35863 block
= begin_omp_parallel ();
35864 save
= cp_parser_begin_omp_structured_block (parser
);
35865 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35867 cp_parser_end_omp_structured_block (parser
, save
);
35868 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35870 if (ret
== NULL_TREE
)
35872 OMP_PARALLEL_COMBINED (stmt
) = 1;
35875 /* When combined with distribute, parallel has to be followed by for.
35876 #pragma omp target parallel is allowed though. */
35878 && (mask
& (OMP_CLAUSE_MASK_1
35879 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35881 error_at (loc
, "expected %<for%> after %qs", p_name
);
35882 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35885 else if (!flag_openmp
) /* flag_openmp_simd */
35887 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35890 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35892 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35893 const char *p
= IDENTIFIER_POINTER (id
);
35894 if (strcmp (p
, "sections") == 0)
35896 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35897 cclauses
= cclauses_buf
;
35899 cp_lexer_consume_token (parser
->lexer
);
35900 block
= begin_omp_parallel ();
35901 save
= cp_parser_begin_omp_structured_block (parser
);
35902 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35903 cp_parser_end_omp_structured_block (parser
, save
);
35904 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35906 OMP_PARALLEL_COMBINED (stmt
) = 1;
35911 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35915 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35916 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35919 block
= begin_omp_parallel ();
35920 save
= cp_parser_begin_omp_structured_block (parser
);
35921 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35922 cp_parser_end_omp_structured_block (parser
, save
);
35923 stmt
= finish_omp_parallel (clauses
, block
);
35928 # pragma omp single single-clause[optseq] new-line
35929 structured-block */
35931 #define OMP_SINGLE_CLAUSE_MASK \
35932 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35938 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35940 tree stmt
= make_node (OMP_SINGLE
);
35941 TREE_TYPE (stmt
) = void_type_node
;
35943 OMP_SINGLE_CLAUSES (stmt
)
35944 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35945 "#pragma omp single", pragma_tok
);
35946 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35948 return add_stmt (stmt
);
35952 # pragma omp task task-clause[optseq] new-line
35953 structured-block */
35955 #define OMP_TASK_CLAUSE_MASK \
35956 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35968 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35970 tree clauses
, block
;
35973 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35974 "#pragma omp task", pragma_tok
);
35975 block
= begin_omp_task ();
35976 save
= cp_parser_begin_omp_structured_block (parser
);
35977 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35978 cp_parser_end_omp_structured_block (parser
, save
);
35979 return finish_omp_task (clauses
, block
);
35983 # pragma omp taskwait new-line */
35986 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35988 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35989 finish_omp_taskwait ();
35993 # pragma omp taskyield new-line */
35996 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35998 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35999 finish_omp_taskyield ();
36003 # pragma omp taskgroup new-line
36004 structured-block */
36007 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36009 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36010 return c_finish_omp_taskgroup (input_location
,
36011 cp_parser_omp_structured_block (parser
,
36017 # pragma omp threadprivate (variable-list) */
36020 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
36024 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36025 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36027 finish_omp_threadprivate (vars
);
36031 # pragma omp cancel cancel-clause[optseq] new-line */
36033 #define OMP_CANCEL_CLAUSE_MASK \
36034 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36041 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
36043 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
36044 "#pragma omp cancel", pragma_tok
);
36045 finish_omp_cancel (clauses
);
36049 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36051 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36052 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36058 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
36059 enum pragma_context context
)
36062 bool point_seen
= false;
36064 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36066 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36067 const char *p
= IDENTIFIER_POINTER (id
);
36069 if (strcmp (p
, "point") == 0)
36071 cp_lexer_consume_token (parser
->lexer
);
36077 cp_parser_error (parser
, "expected %<point%>");
36078 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36082 if (context
!= pragma_compound
)
36084 if (context
== pragma_stmt
)
36085 error_at (pragma_tok
->location
,
36086 "%<#pragma %s%> may only be used in compound statements",
36087 "omp cancellation point");
36089 cp_parser_error (parser
, "expected declaration specifiers");
36090 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36094 clauses
= cp_parser_omp_all_clauses (parser
,
36095 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
36096 "#pragma omp cancellation point",
36098 finish_omp_cancellation_point (clauses
);
36102 #pragma omp distribute distribute-clause[optseq] new-line
36105 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36106 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36113 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36114 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36117 tree clauses
, sb
, ret
;
36119 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36121 strcat (p_name
, " distribute");
36122 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36124 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36126 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36127 const char *p
= IDENTIFIER_POINTER (id
);
36129 bool parallel
= false;
36131 if (strcmp (p
, "simd") == 0)
36134 parallel
= strcmp (p
, "parallel") == 0;
36135 if (parallel
|| simd
)
36137 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36138 if (cclauses
== NULL
)
36139 cclauses
= cclauses_buf
;
36140 cp_lexer_consume_token (parser
->lexer
);
36141 if (!flag_openmp
) /* flag_openmp_simd */
36144 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36147 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36150 sb
= begin_omp_structured_block ();
36151 save
= cp_parser_begin_omp_structured_block (parser
);
36153 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36156 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36158 cp_parser_end_omp_structured_block (parser
, save
);
36159 tree body
= finish_omp_structured_block (sb
);
36162 ret
= make_node (OMP_DISTRIBUTE
);
36163 TREE_TYPE (ret
) = void_type_node
;
36164 OMP_FOR_BODY (ret
) = body
;
36165 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36166 SET_EXPR_LOCATION (ret
, loc
);
36171 if (!flag_openmp
) /* flag_openmp_simd */
36173 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36177 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36181 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36182 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36185 sb
= begin_omp_structured_block ();
36186 save
= cp_parser_begin_omp_structured_block (parser
);
36188 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36190 cp_parser_end_omp_structured_block (parser
, save
);
36191 add_stmt (finish_omp_structured_block (sb
));
36197 # pragma omp teams teams-clause[optseq] new-line
36198 structured-block */
36200 #define OMP_TEAMS_CLAUSE_MASK \
36201 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36210 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36211 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36214 tree clauses
, sb
, ret
;
36216 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36218 strcat (p_name
, " teams");
36219 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36221 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36223 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36224 const char *p
= IDENTIFIER_POINTER (id
);
36225 if (strcmp (p
, "distribute") == 0)
36227 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36228 if (cclauses
== NULL
)
36229 cclauses
= cclauses_buf
;
36231 cp_lexer_consume_token (parser
->lexer
);
36232 if (!flag_openmp
) /* flag_openmp_simd */
36233 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36235 sb
= begin_omp_structured_block ();
36236 save
= cp_parser_begin_omp_structured_block (parser
);
36237 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36239 cp_parser_end_omp_structured_block (parser
, save
);
36240 tree body
= finish_omp_structured_block (sb
);
36243 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36244 ret
= make_node (OMP_TEAMS
);
36245 TREE_TYPE (ret
) = void_type_node
;
36246 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36247 OMP_TEAMS_BODY (ret
) = body
;
36248 OMP_TEAMS_COMBINED (ret
) = 1;
36249 SET_EXPR_LOCATION (ret
, loc
);
36250 return add_stmt (ret
);
36253 if (!flag_openmp
) /* flag_openmp_simd */
36255 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36259 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36263 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36264 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36267 tree stmt
= make_node (OMP_TEAMS
);
36268 TREE_TYPE (stmt
) = void_type_node
;
36269 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36270 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36271 SET_EXPR_LOCATION (stmt
, loc
);
36273 return add_stmt (stmt
);
36277 # pragma omp target data target-data-clause[optseq] new-line
36278 structured-block */
36280 #define OMP_TARGET_DATA_CLAUSE_MASK \
36281 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36287 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36290 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36291 "#pragma omp target data", pragma_tok
);
36293 for (tree
*pc
= &clauses
; *pc
;)
36295 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36296 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36299 case GOMP_MAP_ALWAYS_TO
:
36300 case GOMP_MAP_FROM
:
36301 case GOMP_MAP_ALWAYS_FROM
:
36302 case GOMP_MAP_TOFROM
:
36303 case GOMP_MAP_ALWAYS_TOFROM
:
36304 case GOMP_MAP_ALLOC
:
36307 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36308 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36309 case GOMP_MAP_ALWAYS_POINTER
:
36313 error_at (OMP_CLAUSE_LOCATION (*pc
),
36314 "%<#pragma omp target data%> with map-type other "
36315 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36316 "on %<map%> clause");
36317 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36320 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36326 error_at (pragma_tok
->location
,
36327 "%<#pragma omp target data%> must contain at least "
36328 "one %<map%> clause");
36332 tree stmt
= make_node (OMP_TARGET_DATA
);
36333 TREE_TYPE (stmt
) = void_type_node
;
36334 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36336 keep_next_level (true);
36337 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36339 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36340 return add_stmt (stmt
);
36344 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36345 structured-block */
36347 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36348 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36355 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36356 enum pragma_context context
)
36358 bool data_seen
= false;
36359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36361 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36362 const char *p
= IDENTIFIER_POINTER (id
);
36364 if (strcmp (p
, "data") == 0)
36366 cp_lexer_consume_token (parser
->lexer
);
36372 cp_parser_error (parser
, "expected %<data%>");
36373 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36377 if (context
== pragma_stmt
)
36379 error_at (pragma_tok
->location
,
36380 "%<#pragma %s%> may only be used in compound statements",
36381 "omp target enter data");
36382 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36387 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36388 "#pragma omp target enter data", pragma_tok
);
36390 for (tree
*pc
= &clauses
; *pc
;)
36392 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36393 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36396 case GOMP_MAP_ALWAYS_TO
:
36397 case GOMP_MAP_ALLOC
:
36400 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36401 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36402 case GOMP_MAP_ALWAYS_POINTER
:
36406 error_at (OMP_CLAUSE_LOCATION (*pc
),
36407 "%<#pragma omp target enter data%> with map-type other "
36408 "than %<to%> or %<alloc%> on %<map%> clause");
36409 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36412 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36418 error_at (pragma_tok
->location
,
36419 "%<#pragma omp target enter data%> must contain at least "
36420 "one %<map%> clause");
36424 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36425 TREE_TYPE (stmt
) = void_type_node
;
36426 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36427 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36428 return add_stmt (stmt
);
36432 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36433 structured-block */
36435 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36436 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36443 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36444 enum pragma_context context
)
36446 bool data_seen
= false;
36447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36449 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36450 const char *p
= IDENTIFIER_POINTER (id
);
36452 if (strcmp (p
, "data") == 0)
36454 cp_lexer_consume_token (parser
->lexer
);
36460 cp_parser_error (parser
, "expected %<data%>");
36461 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36465 if (context
== pragma_stmt
)
36467 error_at (pragma_tok
->location
,
36468 "%<#pragma %s%> may only be used in compound statements",
36469 "omp target exit data");
36470 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36475 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36476 "#pragma omp target exit data", pragma_tok
);
36478 for (tree
*pc
= &clauses
; *pc
;)
36480 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36481 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36483 case GOMP_MAP_FROM
:
36484 case GOMP_MAP_ALWAYS_FROM
:
36485 case GOMP_MAP_RELEASE
:
36486 case GOMP_MAP_DELETE
:
36489 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36490 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36491 case GOMP_MAP_ALWAYS_POINTER
:
36495 error_at (OMP_CLAUSE_LOCATION (*pc
),
36496 "%<#pragma omp target exit data%> with map-type other "
36497 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36499 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36502 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36508 error_at (pragma_tok
->location
,
36509 "%<#pragma omp target exit data%> must contain at least "
36510 "one %<map%> clause");
36514 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36515 TREE_TYPE (stmt
) = void_type_node
;
36516 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36517 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36518 return add_stmt (stmt
);
36522 # pragma omp target update target-update-clause[optseq] new-line */
36524 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36525 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36533 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36534 enum pragma_context context
)
36536 if (context
== pragma_stmt
)
36538 error_at (pragma_tok
->location
,
36539 "%<#pragma %s%> may only be used in compound statements",
36540 "omp target update");
36541 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36546 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36547 "#pragma omp target update", pragma_tok
);
36548 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36549 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36551 error_at (pragma_tok
->location
,
36552 "%<#pragma omp target update%> must contain at least one "
36553 "%<from%> or %<to%> clauses");
36557 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36558 TREE_TYPE (stmt
) = void_type_node
;
36559 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36560 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36566 # pragma omp target target-clause[optseq] new-line
36567 structured-block */
36569 #define OMP_TARGET_CLAUSE_MASK \
36570 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36581 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36582 enum pragma_context context
, bool *if_p
)
36584 tree
*pc
= NULL
, stmt
;
36586 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36588 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36589 const char *p
= IDENTIFIER_POINTER (id
);
36590 enum tree_code ccode
= ERROR_MARK
;
36592 if (strcmp (p
, "teams") == 0)
36594 else if (strcmp (p
, "parallel") == 0)
36595 ccode
= OMP_PARALLEL
;
36596 else if (strcmp (p
, "simd") == 0)
36598 if (ccode
!= ERROR_MARK
)
36600 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36601 char p_name
[sizeof ("#pragma omp target teams distribute "
36602 "parallel for simd")];
36604 cp_lexer_consume_token (parser
->lexer
);
36605 strcpy (p_name
, "#pragma omp target");
36606 if (!flag_openmp
) /* flag_openmp_simd */
36612 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36613 OMP_TARGET_CLAUSE_MASK
,
36617 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36618 OMP_TARGET_CLAUSE_MASK
,
36622 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36623 OMP_TARGET_CLAUSE_MASK
,
36627 gcc_unreachable ();
36629 return stmt
!= NULL_TREE
;
36631 keep_next_level (true);
36632 tree sb
= begin_omp_structured_block (), ret
;
36633 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36637 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36638 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36642 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36643 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36647 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36648 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36652 gcc_unreachable ();
36654 cp_parser_end_omp_structured_block (parser
, save
);
36655 tree body
= finish_omp_structured_block (sb
);
36656 if (ret
== NULL_TREE
)
36658 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36660 /* For combined target teams, ensure the num_teams and
36661 thread_limit clause expressions are evaluated on the host,
36662 before entering the target construct. */
36664 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36665 c
; c
= OMP_CLAUSE_CHAIN (c
))
36666 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36667 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36668 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36670 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36671 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36672 if (expr
== error_mark_node
)
36674 tree tmp
= TARGET_EXPR_SLOT (expr
);
36676 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36677 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36678 OMP_CLAUSE_FIRSTPRIVATE
);
36679 OMP_CLAUSE_DECL (tc
) = tmp
;
36680 OMP_CLAUSE_CHAIN (tc
)
36681 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36682 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36685 tree stmt
= make_node (OMP_TARGET
);
36686 TREE_TYPE (stmt
) = void_type_node
;
36687 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36688 OMP_TARGET_BODY (stmt
) = body
;
36689 OMP_TARGET_COMBINED (stmt
) = 1;
36690 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36692 pc
= &OMP_TARGET_CLAUSES (stmt
);
36693 goto check_clauses
;
36695 else if (!flag_openmp
) /* flag_openmp_simd */
36697 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36700 else if (strcmp (p
, "data") == 0)
36702 cp_lexer_consume_token (parser
->lexer
);
36703 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36706 else if (strcmp (p
, "enter") == 0)
36708 cp_lexer_consume_token (parser
->lexer
);
36709 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36712 else if (strcmp (p
, "exit") == 0)
36714 cp_lexer_consume_token (parser
->lexer
);
36715 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36718 else if (strcmp (p
, "update") == 0)
36720 cp_lexer_consume_token (parser
->lexer
);
36721 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36724 if (!flag_openmp
) /* flag_openmp_simd */
36726 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36730 stmt
= make_node (OMP_TARGET
);
36731 TREE_TYPE (stmt
) = void_type_node
;
36733 OMP_TARGET_CLAUSES (stmt
)
36734 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36735 "#pragma omp target", pragma_tok
);
36736 pc
= &OMP_TARGET_CLAUSES (stmt
);
36737 keep_next_level (true);
36738 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36740 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36746 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36747 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36750 case GOMP_MAP_ALWAYS_TO
:
36751 case GOMP_MAP_FROM
:
36752 case GOMP_MAP_ALWAYS_FROM
:
36753 case GOMP_MAP_TOFROM
:
36754 case GOMP_MAP_ALWAYS_TOFROM
:
36755 case GOMP_MAP_ALLOC
:
36756 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36757 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36758 case GOMP_MAP_ALWAYS_POINTER
:
36761 error_at (OMP_CLAUSE_LOCATION (*pc
),
36762 "%<#pragma omp target%> with map-type other "
36763 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36764 "on %<map%> clause");
36765 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36768 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36774 # pragma acc cache (variable-list) new-line
36778 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36780 tree stmt
, clauses
;
36782 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36783 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36785 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36787 stmt
= make_node (OACC_CACHE
);
36788 TREE_TYPE (stmt
) = void_type_node
;
36789 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36790 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36797 # pragma acc data oacc-data-clause[optseq] new-line
36798 structured-block */
36800 #define OACC_DATA_CLAUSE_MASK \
36801 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36814 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36816 tree stmt
, clauses
, block
;
36819 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36820 "#pragma acc data", pragma_tok
);
36822 block
= begin_omp_parallel ();
36823 save
= cp_parser_begin_omp_structured_block (parser
);
36824 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36825 cp_parser_end_omp_structured_block (parser
, save
);
36826 stmt
= finish_oacc_data (clauses
, block
);
36831 # pragma acc host_data <clauses> new-line
36832 structured-block */
36834 #define OACC_HOST_DATA_CLAUSE_MASK \
36835 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36838 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36840 tree stmt
, clauses
, block
;
36843 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36844 "#pragma acc host_data", pragma_tok
);
36846 block
= begin_omp_parallel ();
36847 save
= cp_parser_begin_omp_structured_block (parser
);
36848 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36849 cp_parser_end_omp_structured_block (parser
, save
);
36850 stmt
= finish_oacc_host_data (clauses
, block
);
36855 # pragma acc declare oacc-data-clause[optseq] new-line
36858 #define OACC_DECLARE_CLAUSE_MASK \
36859 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36873 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36875 tree clauses
, stmt
;
36876 bool error
= false;
36878 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36879 "#pragma acc declare", pragma_tok
, true);
36882 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36884 error_at (pragma_tok
->location
,
36885 "no valid clauses specified in %<#pragma acc declare%>");
36889 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36891 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36892 tree decl
= OMP_CLAUSE_DECL (t
);
36893 if (!DECL_P (decl
))
36895 error_at (loc
, "array section in %<#pragma acc declare%>");
36899 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36900 switch (OMP_CLAUSE_MAP_KIND (t
))
36902 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36903 case GOMP_MAP_FORCE_ALLOC
:
36904 case GOMP_MAP_FORCE_TO
:
36905 case GOMP_MAP_FORCE_DEVICEPTR
:
36906 case GOMP_MAP_DEVICE_RESIDENT
:
36909 case GOMP_MAP_LINK
:
36910 if (!global_bindings_p ()
36911 && (TREE_STATIC (decl
)
36912 || !DECL_EXTERNAL (decl
)))
36915 "%qD must be a global variable in "
36916 "%<#pragma acc declare link%>",
36924 if (global_bindings_p ())
36926 error_at (loc
, "invalid OpenACC clause at file scope");
36930 if (DECL_EXTERNAL (decl
))
36933 "invalid use of %<extern%> variable %qD "
36934 "in %<#pragma acc declare%>", decl
);
36938 else if (TREE_PUBLIC (decl
))
36941 "invalid use of %<global%> variable %qD "
36942 "in %<#pragma acc declare%>", decl
);
36949 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36950 || lookup_attribute ("omp declare target link",
36951 DECL_ATTRIBUTES (decl
)))
36953 error_at (loc
, "variable %qD used more than once with "
36954 "%<#pragma acc declare%>", decl
);
36963 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36964 id
= get_identifier ("omp declare target link");
36966 id
= get_identifier ("omp declare target");
36968 DECL_ATTRIBUTES (decl
)
36969 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36970 if (global_bindings_p ())
36972 symtab_node
*node
= symtab_node::get (decl
);
36975 node
->offloadable
= 1;
36976 if (ENABLE_OFFLOADING
)
36978 g
->have_offload
= true;
36979 if (is_a
<varpool_node
*> (node
))
36980 vec_safe_push (offload_vars
, decl
);
36987 if (error
|| global_bindings_p ())
36990 stmt
= make_node (OACC_DECLARE
);
36991 TREE_TYPE (stmt
) = void_type_node
;
36992 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36993 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37001 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37005 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37007 LOC is the location of the #pragma token.
37010 #define OACC_ENTER_DATA_CLAUSE_MASK \
37011 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37019 #define OACC_EXIT_DATA_CLAUSE_MASK \
37020 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37027 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37030 location_t loc
= pragma_tok
->location
;
37031 tree stmt
, clauses
;
37032 const char *p
= "";
37034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37035 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37037 if (strcmp (p
, "data") != 0)
37039 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
37040 enter
? "enter" : "exit");
37041 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37045 cp_lexer_consume_token (parser
->lexer
);
37048 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
37049 "#pragma acc enter data", pragma_tok
);
37051 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
37052 "#pragma acc exit data", pragma_tok
);
37054 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37056 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
37057 enter
? "enter" : "exit");
37061 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
37062 TREE_TYPE (stmt
) = void_type_node
;
37063 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
37064 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37070 # pragma acc loop oacc-loop-clause[optseq] new-line
37071 structured-block */
37073 #define OACC_LOOP_CLAUSE_MASK \
37074 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37086 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
37087 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
37089 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
37091 strcat (p_name
, " loop");
37092 mask
|= OACC_LOOP_CLAUSE_MASK
;
37094 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37098 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
37100 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
37102 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
37105 tree block
= begin_omp_structured_block ();
37106 int save
= cp_parser_begin_omp_structured_block (parser
);
37107 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
37108 cp_parser_end_omp_structured_block (parser
, save
);
37109 add_stmt (finish_omp_structured_block (block
));
37115 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37120 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37124 #define OACC_KERNELS_CLAUSE_MASK \
37125 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37143 #define OACC_PARALLEL_CLAUSE_MASK \
37144 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37166 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37167 char *p_name
, bool *if_p
)
37169 omp_clause_mask mask
;
37170 enum tree_code code
;
37171 switch (cp_parser_pragma_kind (pragma_tok
))
37173 case PRAGMA_OACC_KERNELS
:
37174 strcat (p_name
, " kernels");
37175 mask
= OACC_KERNELS_CLAUSE_MASK
;
37176 code
= OACC_KERNELS
;
37178 case PRAGMA_OACC_PARALLEL
:
37179 strcat (p_name
, " parallel");
37180 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37181 code
= OACC_PARALLEL
;
37184 gcc_unreachable ();
37187 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37190 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37191 if (strcmp (p
, "loop") == 0)
37193 cp_lexer_consume_token (parser
->lexer
);
37194 tree block
= begin_omp_parallel ();
37196 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37198 return finish_omp_construct (code
, block
, clauses
);
37202 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37204 tree block
= begin_omp_parallel ();
37205 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37206 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37207 cp_parser_end_omp_structured_block (parser
, save
);
37208 return finish_omp_construct (code
, block
, clauses
);
37212 # pragma acc update oacc-update-clause[optseq] new-line
37215 #define OACC_UPDATE_CLAUSE_MASK \
37216 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37224 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37226 tree stmt
, clauses
;
37228 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37229 "#pragma acc update", pragma_tok
);
37231 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37233 error_at (pragma_tok
->location
,
37234 "%<#pragma acc update%> must contain at least one "
37235 "%<device%> or %<host%> or %<self%> clause");
37239 stmt
= make_node (OACC_UPDATE
);
37240 TREE_TYPE (stmt
) = void_type_node
;
37241 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37242 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37248 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37250 LOC is the location of the #pragma token.
37253 #define OACC_WAIT_CLAUSE_MASK \
37254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37257 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37259 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37260 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37262 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37263 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37265 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37266 "#pragma acc wait", pragma_tok
);
37268 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37269 stmt
= finish_expr_stmt (stmt
);
37275 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37277 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37278 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37286 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37287 enum pragma_context context
)
37289 bool first_p
= parser
->omp_declare_simd
== NULL
;
37290 cp_omp_declare_simd_data data
;
37293 data
.error_seen
= false;
37294 data
.fndecl_seen
= false;
37295 data
.tokens
= vNULL
;
37296 data
.clauses
= NULL_TREE
;
37297 /* It is safe to take the address of a local variable; it will only be
37298 used while this scope is live. */
37299 parser
->omp_declare_simd
= &data
;
37302 /* Store away all pragma tokens. */
37303 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37304 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37305 cp_lexer_consume_token (parser
->lexer
);
37306 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37307 parser
->omp_declare_simd
->error_seen
= true;
37308 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37309 struct cp_token_cache
*cp
37310 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37311 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37315 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37316 cp_parser_pragma (parser
, context
, NULL
);
37319 case pragma_external
:
37320 cp_parser_declaration (parser
);
37322 case pragma_member
:
37323 cp_parser_member_declaration (parser
);
37325 case pragma_objc_icode
:
37326 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37329 cp_parser_declaration_statement (parser
);
37332 if (parser
->omp_declare_simd
37333 && !parser
->omp_declare_simd
->error_seen
37334 && !parser
->omp_declare_simd
->fndecl_seen
)
37335 error_at (pragma_tok
->location
,
37336 "%<#pragma omp declare simd%> not immediately followed by "
37337 "function declaration or definition");
37338 data
.tokens
.release ();
37339 parser
->omp_declare_simd
= NULL
;
37343 /* Finalize #pragma omp declare simd clauses after direct declarator has
37344 been parsed, and put that into "omp declare simd" attribute. */
37347 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37349 struct cp_token_cache
*ce
;
37350 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37353 if (!data
->error_seen
&& data
->fndecl_seen
)
37355 error ("%<#pragma omp declare simd%> not immediately followed by "
37356 "a single function declaration or definition");
37357 data
->error_seen
= true;
37359 if (data
->error_seen
)
37362 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37366 cp_parser_push_lexer_for_tokens (parser
, ce
);
37367 parser
->lexer
->in_pragma
= true;
37368 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37369 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37370 cp_lexer_consume_token (parser
->lexer
);
37371 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37372 "#pragma omp declare simd", pragma_tok
);
37373 cp_parser_pop_lexer (parser
);
37375 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37376 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37377 TREE_CHAIN (c
) = attrs
;
37378 if (processing_template_decl
)
37379 ATTR_IS_DEPENDENT (c
) = 1;
37383 data
->fndecl_seen
= true;
37389 # pragma omp declare target new-line
37390 declarations and definitions
37391 # pragma omp end declare target new-line
37394 # pragma omp declare target ( extended-list ) new-line
37396 # pragma omp declare target declare-target-clauses[seq] new-line */
37398 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37399 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37403 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37405 tree clauses
= NULL_TREE
;
37406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37408 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37409 "#pragma omp declare target", pragma_tok
);
37410 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37412 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37414 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37415 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37419 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37420 scope_chain
->omp_declare_target_attribute
++;
37423 if (scope_chain
->omp_declare_target_attribute
)
37424 error_at (pragma_tok
->location
,
37425 "%<#pragma omp declare target%> with clauses in between "
37426 "%<#pragma omp declare target%> without clauses and "
37427 "%<#pragma omp end declare target%>");
37428 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37430 tree t
= OMP_CLAUSE_DECL (c
), id
;
37431 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37432 tree at2
= lookup_attribute ("omp declare target link",
37433 DECL_ATTRIBUTES (t
));
37434 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37436 id
= get_identifier ("omp declare target link");
37437 std::swap (at1
, at2
);
37440 id
= get_identifier ("omp declare target");
37443 error_at (OMP_CLAUSE_LOCATION (c
),
37444 "%qD specified both in declare target %<link%> and %<to%>"
37450 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37451 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37454 symtab_node
*node
= symtab_node::get (t
);
37457 node
->offloadable
= 1;
37458 if (ENABLE_OFFLOADING
)
37460 g
->have_offload
= true;
37461 if (is_a
<varpool_node
*> (node
))
37462 vec_safe_push (offload_vars
, t
);
37470 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37472 const char *p
= "";
37473 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37475 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37476 p
= IDENTIFIER_POINTER (id
);
37478 if (strcmp (p
, "declare") == 0)
37480 cp_lexer_consume_token (parser
->lexer
);
37482 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37484 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37485 p
= IDENTIFIER_POINTER (id
);
37487 if (strcmp (p
, "target") == 0)
37488 cp_lexer_consume_token (parser
->lexer
);
37491 cp_parser_error (parser
, "expected %<target%>");
37492 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37498 cp_parser_error (parser
, "expected %<declare%>");
37499 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37502 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37503 if (!scope_chain
->omp_declare_target_attribute
)
37504 error_at (pragma_tok
->location
,
37505 "%<#pragma omp end declare target%> without corresponding "
37506 "%<#pragma omp declare target%>");
37508 scope_chain
->omp_declare_target_attribute
--;
37511 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37512 expression and optional initializer clause of
37513 #pragma omp declare reduction. We store the expression(s) as
37514 either 3, 6 or 7 special statements inside of the artificial function's
37515 body. The first two statements are DECL_EXPRs for the artificial
37516 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37517 expression that uses those variables.
37518 If there was any INITIALIZER clause, this is followed by further statements,
37519 the fourth and fifth statements are DECL_EXPRs for the artificial
37520 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37521 constructor variant (first token after open paren is not omp_priv),
37522 then the sixth statement is a statement with the function call expression
37523 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37524 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37525 to initialize the OMP_PRIV artificial variable and there is seventh
37526 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37529 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37531 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37532 gcc_assert (TYPE_REF_P (type
));
37533 type
= TREE_TYPE (type
);
37534 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37535 DECL_ARTIFICIAL (omp_out
) = 1;
37536 pushdecl (omp_out
);
37537 add_decl_expr (omp_out
);
37538 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37539 DECL_ARTIFICIAL (omp_in
) = 1;
37541 add_decl_expr (omp_in
);
37543 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37545 keep_next_level (true);
37546 tree block
= begin_omp_structured_block ();
37547 combiner
= cp_parser_expression (parser
);
37548 finish_expr_stmt (combiner
);
37549 block
= finish_omp_structured_block (block
);
37552 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37555 const char *p
= "";
37556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37558 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37559 p
= IDENTIFIER_POINTER (id
);
37562 if (strcmp (p
, "initializer") == 0)
37564 cp_lexer_consume_token (parser
->lexer
);
37565 matching_parens parens
;
37566 if (!parens
.require_open (parser
))
37570 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37572 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37573 p
= IDENTIFIER_POINTER (id
);
37576 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37577 DECL_ARTIFICIAL (omp_priv
) = 1;
37578 pushdecl (omp_priv
);
37579 add_decl_expr (omp_priv
);
37580 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37581 DECL_ARTIFICIAL (omp_orig
) = 1;
37582 pushdecl (omp_orig
);
37583 add_decl_expr (omp_orig
);
37585 keep_next_level (true);
37586 block
= begin_omp_structured_block ();
37589 if (strcmp (p
, "omp_priv") == 0)
37591 bool is_direct_init
, is_non_constant_init
;
37593 cp_lexer_consume_token (parser
->lexer
);
37594 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37595 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37596 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37597 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37599 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37600 == CPP_CLOSE_PAREN
))
37602 finish_omp_structured_block (block
);
37603 error ("invalid initializer clause");
37606 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37607 &is_non_constant_init
);
37608 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37609 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37613 cp_parser_parse_tentatively (parser
);
37614 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37615 /*check_dependency_p=*/true,
37616 /*template_p=*/NULL
,
37617 /*declarator_p=*/false,
37618 /*optional_p=*/false);
37619 vec
<tree
, va_gc
> *args
;
37620 if (fn_name
== error_mark_node
37621 || cp_parser_error_occurred (parser
)
37622 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37623 || ((args
= cp_parser_parenthesized_expression_list
37624 (parser
, non_attr
, /*cast_p=*/false,
37625 /*allow_expansion_p=*/true,
37626 /*non_constant_p=*/NULL
)),
37627 cp_parser_error_occurred (parser
)))
37629 finish_omp_structured_block (block
);
37630 cp_parser_abort_tentative_parse (parser
);
37631 cp_parser_error (parser
, "expected id-expression (arguments)");
37636 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37637 if (arg
== omp_priv
37638 || (TREE_CODE (arg
) == ADDR_EXPR
37639 && TREE_OPERAND (arg
, 0) == omp_priv
))
37641 cp_parser_abort_tentative_parse (parser
);
37642 if (arg
== NULL_TREE
)
37643 error ("one of the initializer call arguments should be %<omp_priv%>"
37644 " or %<&omp_priv%>");
37645 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37647 finish_expr_stmt (initializer
);
37650 block
= finish_omp_structured_block (block
);
37651 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37655 add_decl_expr (omp_orig
);
37657 if (!parens
.require_close (parser
))
37661 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37662 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37669 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37670 initializer-clause[opt] new-line
37672 initializer-clause:
37673 initializer (omp_priv initializer)
37674 initializer (function-name (argument-list)) */
37677 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37678 enum pragma_context
)
37680 auto_vec
<tree
> types
;
37681 enum tree_code reduc_code
= ERROR_MARK
;
37682 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37684 cp_token
*first_token
;
37685 cp_token_cache
*cp
;
37689 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37690 p
= obstack_alloc (&declarator_obstack
, 0);
37692 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37695 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37698 reduc_code
= PLUS_EXPR
;
37701 reduc_code
= MULT_EXPR
;
37704 reduc_code
= MINUS_EXPR
;
37707 reduc_code
= BIT_AND_EXPR
;
37710 reduc_code
= BIT_XOR_EXPR
;
37713 reduc_code
= BIT_IOR_EXPR
;
37716 reduc_code
= TRUTH_ANDIF_EXPR
;
37719 reduc_code
= TRUTH_ORIF_EXPR
;
37722 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37725 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37726 "%<|%>, %<&&%>, %<||%> or identifier");
37730 if (reduc_code
!= ERROR_MARK
)
37731 cp_lexer_consume_token (parser
->lexer
);
37733 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37734 if (reduc_id
== error_mark_node
)
37737 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37740 /* Types may not be defined in declare reduction type list. */
37741 const char *saved_message
;
37742 saved_message
= parser
->type_definition_forbidden_message
;
37743 parser
->type_definition_forbidden_message
37744 = G_("types may not be defined in declare reduction type list");
37745 bool saved_colon_corrects_to_scope_p
;
37746 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37747 parser
->colon_corrects_to_scope_p
= false;
37748 bool saved_colon_doesnt_start_class_def_p
;
37749 saved_colon_doesnt_start_class_def_p
37750 = parser
->colon_doesnt_start_class_def_p
;
37751 parser
->colon_doesnt_start_class_def_p
= true;
37755 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37756 type
= cp_parser_type_id (parser
);
37757 if (type
== error_mark_node
)
37759 else if (ARITHMETIC_TYPE_P (type
)
37760 && (orig_reduc_id
== NULL_TREE
37761 || (TREE_CODE (type
) != COMPLEX_TYPE
37762 && (id_equal (orig_reduc_id
, "min")
37763 || id_equal (orig_reduc_id
, "max")))))
37764 error_at (loc
, "predeclared arithmetic type %qT in "
37765 "%<#pragma omp declare reduction%>", type
);
37766 else if (TREE_CODE (type
) == FUNCTION_TYPE
37767 || TREE_CODE (type
) == METHOD_TYPE
37768 || TREE_CODE (type
) == ARRAY_TYPE
)
37769 error_at (loc
, "function or array type %qT in "
37770 "%<#pragma omp declare reduction%>", type
);
37771 else if (TYPE_REF_P (type
))
37772 error_at (loc
, "reference type %qT in "
37773 "%<#pragma omp declare reduction%>", type
);
37774 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37775 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37776 "%<#pragma omp declare reduction%>", type
);
37778 types
.safe_push (type
);
37780 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37781 cp_lexer_consume_token (parser
->lexer
);
37786 /* Restore the saved message. */
37787 parser
->type_definition_forbidden_message
= saved_message
;
37788 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37789 parser
->colon_doesnt_start_class_def_p
37790 = saved_colon_doesnt_start_class_def_p
;
37792 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37793 || types
.is_empty ())
37796 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37800 first_token
= cp_lexer_peek_token (parser
->lexer
);
37803 FOR_EACH_VEC_ELT (types
, i
, type
)
37806 = build_function_type_list (void_type_node
,
37807 cp_build_reference_type (type
, false),
37809 tree this_reduc_id
= reduc_id
;
37810 if (!dependent_type_p (type
))
37811 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37812 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37813 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37814 DECL_ARTIFICIAL (fndecl
) = 1;
37815 DECL_EXTERNAL (fndecl
) = 1;
37816 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37817 DECL_IGNORED_P (fndecl
) = 1;
37818 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37819 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37820 DECL_ATTRIBUTES (fndecl
)
37821 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37822 DECL_ATTRIBUTES (fndecl
));
37823 if (processing_template_decl
)
37824 fndecl
= push_template_decl (fndecl
);
37825 bool block_scope
= false;
37826 tree block
= NULL_TREE
;
37827 if (current_function_decl
)
37829 block_scope
= true;
37830 DECL_CONTEXT (fndecl
) = global_namespace
;
37831 if (!processing_template_decl
)
37834 else if (current_class_type
)
37838 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37839 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37840 cp_lexer_consume_token (parser
->lexer
);
37841 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37843 cp
= cp_token_cache_new (first_token
,
37844 cp_lexer_peek_nth_token (parser
->lexer
,
37847 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37848 finish_member_declaration (fndecl
);
37849 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37850 DECL_PENDING_INLINE_P (fndecl
) = 1;
37851 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37856 DECL_CONTEXT (fndecl
) = current_namespace
;
37860 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37862 block
= begin_omp_structured_block ();
37865 cp_parser_push_lexer_for_tokens (parser
, cp
);
37866 parser
->lexer
->in_pragma
= true;
37868 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37871 finish_function (/*inline_p=*/false);
37873 DECL_CONTEXT (fndecl
) = current_function_decl
;
37875 cp_parser_pop_lexer (parser
);
37879 cp_parser_pop_lexer (parser
);
37881 finish_function (/*inline_p=*/false);
37884 DECL_CONTEXT (fndecl
) = current_function_decl
;
37885 block
= finish_omp_structured_block (block
);
37886 if (TREE_CODE (block
) == BIND_EXPR
)
37887 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37888 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37889 DECL_SAVED_TREE (fndecl
) = block
;
37890 if (processing_template_decl
)
37891 add_decl_expr (fndecl
);
37893 cp_check_omp_declare_reduction (fndecl
);
37894 if (cp
== NULL
&& types
.length () > 1)
37895 cp
= cp_token_cache_new (first_token
,
37896 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37897 if (errs
!= errorcount
)
37901 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37904 /* Free any declarators allocated. */
37905 obstack_free (&declarator_obstack
, p
);
37909 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37910 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37911 initializer-clause[opt] new-line
37912 #pragma omp declare target new-line */
37915 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37916 enum pragma_context context
)
37918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37920 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37921 const char *p
= IDENTIFIER_POINTER (id
);
37923 if (strcmp (p
, "simd") == 0)
37925 cp_lexer_consume_token (parser
->lexer
);
37926 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37930 cp_ensure_no_omp_declare_simd (parser
);
37931 if (strcmp (p
, "reduction") == 0)
37933 cp_lexer_consume_token (parser
->lexer
);
37934 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37938 if (!flag_openmp
) /* flag_openmp_simd */
37940 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37943 if (strcmp (p
, "target") == 0)
37945 cp_lexer_consume_token (parser
->lexer
);
37946 cp_parser_omp_declare_target (parser
, pragma_tok
);
37950 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37952 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37957 #pragma omp taskloop taskloop-clause[optseq] new-line
37960 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37963 #define OMP_TASKLOOP_CLAUSE_MASK \
37964 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37980 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37981 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37984 tree clauses
, sb
, ret
;
37986 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37988 strcat (p_name
, " taskloop");
37989 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37991 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37993 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37994 const char *p
= IDENTIFIER_POINTER (id
);
37996 if (strcmp (p
, "simd") == 0)
37998 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37999 if (cclauses
== NULL
)
38000 cclauses
= cclauses_buf
;
38002 cp_lexer_consume_token (parser
->lexer
);
38003 if (!flag_openmp
) /* flag_openmp_simd */
38004 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38006 sb
= begin_omp_structured_block ();
38007 save
= cp_parser_begin_omp_structured_block (parser
);
38008 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38010 cp_parser_end_omp_structured_block (parser
, save
);
38011 tree body
= finish_omp_structured_block (sb
);
38014 ret
= make_node (OMP_TASKLOOP
);
38015 TREE_TYPE (ret
) = void_type_node
;
38016 OMP_FOR_BODY (ret
) = body
;
38017 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38018 SET_EXPR_LOCATION (ret
, loc
);
38023 if (!flag_openmp
) /* flag_openmp_simd */
38025 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38029 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38033 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
38034 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38037 sb
= begin_omp_structured_block ();
38038 save
= cp_parser_begin_omp_structured_block (parser
);
38040 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
38043 cp_parser_end_omp_structured_block (parser
, save
);
38044 add_stmt (finish_omp_structured_block (sb
));
38051 # pragma acc routine oacc-routine-clause[optseq] new-line
38052 function-definition
38054 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38057 #define OACC_ROUTINE_CLAUSE_MASK \
38058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38064 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38065 component, which must resolve to a declared namespace-scope
38066 function. The clauses are either processed directly (for a named
38067 function), or defered until the immediatley following declaration
38071 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
38072 enum pragma_context context
)
38074 gcc_checking_assert (context
== pragma_external
);
38075 /* The checking for "another pragma following this one" in the "no optional
38076 '( name )'" case makes sure that we dont re-enter. */
38077 gcc_checking_assert (parser
->oacc_routine
== NULL
);
38079 cp_oacc_routine_data data
;
38080 data
.error_seen
= false;
38081 data
.fndecl_seen
= false;
38082 data
.tokens
= vNULL
;
38083 data
.clauses
= NULL_TREE
;
38084 data
.loc
= pragma_tok
->location
;
38085 /* It is safe to take the address of a local variable; it will only be
38086 used while this scope is live. */
38087 parser
->oacc_routine
= &data
;
38089 /* Look for optional '( name )'. */
38090 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38092 matching_parens parens
;
38093 parens
.consume_open (parser
); /* '(' */
38095 /* We parse the name as an id-expression. If it resolves to
38096 anything other than a non-overloaded function at namespace
38097 scope, it's an error. */
38098 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38099 tree name
= cp_parser_id_expression (parser
,
38100 /*template_keyword_p=*/false,
38101 /*check_dependency_p=*/false,
38102 /*template_p=*/NULL
,
38103 /*declarator_p=*/false,
38104 /*optional_p=*/false);
38105 tree decl
= (identifier_p (name
)
38106 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
38108 if (name
!= error_mark_node
&& decl
== error_mark_node
)
38109 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38111 if (decl
== error_mark_node
38112 || !parens
.require_close (parser
))
38114 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38115 parser
->oacc_routine
= NULL
;
38120 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38121 "#pragma acc routine",
38122 cp_lexer_peek_token (parser
->lexer
));
38124 if (decl
&& is_overloaded_fn (decl
)
38125 && (TREE_CODE (decl
) != FUNCTION_DECL
38126 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38128 error_at (name_loc
,
38129 "%<#pragma acc routine%> names a set of overloads");
38130 parser
->oacc_routine
= NULL
;
38134 /* Perhaps we should use the same rule as declarations in different
38136 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38138 error_at (name_loc
,
38139 "%qD does not refer to a namespace scope function", decl
);
38140 parser
->oacc_routine
= NULL
;
38144 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38146 error_at (name_loc
, "%qD does not refer to a function", decl
);
38147 parser
->oacc_routine
= NULL
;
38151 cp_finalize_oacc_routine (parser
, decl
, false);
38152 parser
->oacc_routine
= NULL
;
38154 else /* No optional '( name )'. */
38156 /* Store away all pragma tokens. */
38157 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38158 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38159 cp_lexer_consume_token (parser
->lexer
);
38160 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38161 parser
->oacc_routine
->error_seen
= true;
38162 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38163 struct cp_token_cache
*cp
38164 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38165 parser
->oacc_routine
->tokens
.safe_push (cp
);
38167 /* Emit a helpful diagnostic if there's another pragma following this
38169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38171 cp_ensure_no_oacc_routine (parser
);
38172 data
.tokens
.release ();
38173 /* ..., and then just keep going. */
38177 /* We only have to consider the pragma_external case here. */
38178 cp_parser_declaration (parser
);
38179 if (parser
->oacc_routine
38180 && !parser
->oacc_routine
->fndecl_seen
)
38181 cp_ensure_no_oacc_routine (parser
);
38183 parser
->oacc_routine
= NULL
;
38184 data
.tokens
.release ();
38188 /* Finalize #pragma acc routine clauses after direct declarator has
38192 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38194 struct cp_token_cache
*ce
;
38195 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38197 if (!data
->error_seen
&& data
->fndecl_seen
)
38199 error_at (data
->loc
,
38200 "%<#pragma acc routine%> not immediately followed by "
38201 "a single function declaration or definition");
38202 data
->error_seen
= true;
38204 if (data
->error_seen
)
38207 gcc_checking_assert (data
->tokens
.length () == 1);
38208 ce
= data
->tokens
[0];
38210 cp_parser_push_lexer_for_tokens (parser
, ce
);
38211 parser
->lexer
->in_pragma
= true;
38212 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38214 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38215 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38216 parser
->oacc_routine
->clauses
38217 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38218 "#pragma acc routine", pragma_tok
);
38219 cp_parser_pop_lexer (parser
);
38220 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38226 /* Apply any saved OpenACC routine clauses to a just-parsed
38230 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38232 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38234 /* Keep going if we're in error reporting mode. */
38235 if (parser
->oacc_routine
->error_seen
38236 || fndecl
== error_mark_node
)
38239 if (parser
->oacc_routine
->fndecl_seen
)
38241 error_at (parser
->oacc_routine
->loc
,
38242 "%<#pragma acc routine%> not immediately followed by"
38243 " a single function declaration or definition");
38244 parser
->oacc_routine
= NULL
;
38247 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38249 cp_ensure_no_oacc_routine (parser
);
38253 if (oacc_get_fn_attrib (fndecl
))
38255 error_at (parser
->oacc_routine
->loc
,
38256 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38257 parser
->oacc_routine
= NULL
;
38261 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38263 error_at (parser
->oacc_routine
->loc
,
38265 ? G_("%<#pragma acc routine%> must be applied before use")
38266 : G_("%<#pragma acc routine%> must be applied before "
38268 parser
->oacc_routine
= NULL
;
38272 /* Process the routine's dimension clauses. */
38273 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38274 oacc_replace_fn_attrib (fndecl
, dims
);
38276 /* Add an "omp declare target" attribute. */
38277 DECL_ATTRIBUTES (fndecl
)
38278 = tree_cons (get_identifier ("omp declare target"),
38279 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38281 /* Don't unset parser->oacc_routine here: we may still need it to
38282 diagnose wrong usage. But, remember that we've used this "#pragma acc
38284 parser
->oacc_routine
->fndecl_seen
= true;
38288 /* Main entry point to OpenMP statement pragmas. */
38291 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38294 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38295 omp_clause_mask
mask (0);
38297 switch (cp_parser_pragma_kind (pragma_tok
))
38299 case PRAGMA_OACC_ATOMIC
:
38300 cp_parser_omp_atomic (parser
, pragma_tok
);
38302 case PRAGMA_OACC_CACHE
:
38303 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38305 case PRAGMA_OACC_DATA
:
38306 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38308 case PRAGMA_OACC_ENTER_DATA
:
38309 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38311 case PRAGMA_OACC_EXIT_DATA
:
38312 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38314 case PRAGMA_OACC_HOST_DATA
:
38315 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38317 case PRAGMA_OACC_KERNELS
:
38318 case PRAGMA_OACC_PARALLEL
:
38319 strcpy (p_name
, "#pragma acc");
38320 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38323 case PRAGMA_OACC_LOOP
:
38324 strcpy (p_name
, "#pragma acc");
38325 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38328 case PRAGMA_OACC_UPDATE
:
38329 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38331 case PRAGMA_OACC_WAIT
:
38332 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38334 case PRAGMA_OMP_ATOMIC
:
38335 cp_parser_omp_atomic (parser
, pragma_tok
);
38337 case PRAGMA_OMP_CRITICAL
:
38338 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38340 case PRAGMA_OMP_DISTRIBUTE
:
38341 strcpy (p_name
, "#pragma omp");
38342 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38345 case PRAGMA_OMP_FOR
:
38346 strcpy (p_name
, "#pragma omp");
38347 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38350 case PRAGMA_OMP_MASTER
:
38351 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38353 case PRAGMA_OMP_PARALLEL
:
38354 strcpy (p_name
, "#pragma omp");
38355 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38358 case PRAGMA_OMP_SECTIONS
:
38359 strcpy (p_name
, "#pragma omp");
38360 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38362 case PRAGMA_OMP_SIMD
:
38363 strcpy (p_name
, "#pragma omp");
38364 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38367 case PRAGMA_OMP_SINGLE
:
38368 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38370 case PRAGMA_OMP_TASK
:
38371 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38373 case PRAGMA_OMP_TASKGROUP
:
38374 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38376 case PRAGMA_OMP_TASKLOOP
:
38377 strcpy (p_name
, "#pragma omp");
38378 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38381 case PRAGMA_OMP_TEAMS
:
38382 strcpy (p_name
, "#pragma omp");
38383 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38387 gcc_unreachable ();
38390 protected_set_expr_location (stmt
, pragma_tok
->location
);
38393 /* Transactional Memory parsing routines. */
38395 /* Parse a transaction attribute.
38401 We use this instead of cp_parser_attributes_opt for transactions to avoid
38402 the pedwarn in C++98 mode. */
38405 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38408 tree attr_name
, attr
= NULL
;
38410 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38411 return cp_parser_attributes_opt (parser
);
38413 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38415 cp_lexer_consume_token (parser
->lexer
);
38416 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38419 token
= cp_lexer_peek_token (parser
->lexer
);
38420 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38422 token
= cp_lexer_consume_token (parser
->lexer
);
38424 attr_name
= (token
->type
== CPP_KEYWORD
38425 /* For keywords, use the canonical spelling,
38426 not the parsed identifier. */
38427 ? ridpointers
[(int) token
->keyword
]
38429 attr
= build_tree_list (attr_name
, NULL_TREE
);
38432 cp_parser_error (parser
, "expected identifier");
38434 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38436 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38440 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38442 transaction-statement:
38443 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38445 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38449 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38451 unsigned char old_in
= parser
->in_transaction
;
38452 unsigned char this_in
= 1, new_in
;
38453 enum rid keyword
= token
->keyword
;
38454 tree stmt
, attrs
, noex
;
38456 cp_lexer_consume_token (parser
->lexer
);
38458 if (keyword
== RID_TRANSACTION_RELAXED
38459 || keyword
== RID_SYNCHRONIZED
)
38460 this_in
|= TM_STMT_ATTR_RELAXED
;
38463 attrs
= cp_parser_txn_attribute_opt (parser
);
38465 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38468 /* Parse a noexcept specification. */
38469 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38470 noex
= boolean_true_node
;
38471 else if (keyword
== RID_ATOMIC_CANCEL
)
38473 /* cancel-and-throw is unimplemented. */
38474 sorry ("atomic_cancel");
38478 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38480 /* Keep track if we're in the lexical scope of an outer transaction. */
38481 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38483 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38485 parser
->in_transaction
= new_in
;
38486 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38487 parser
->in_transaction
= old_in
;
38489 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38494 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38496 transaction-expression:
38497 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38498 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38502 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38504 unsigned char old_in
= parser
->in_transaction
;
38505 unsigned char this_in
= 1;
38509 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38511 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38512 || keyword
== RID_TRANSACTION_RELAXED
);
38516 keyword
== RID_TRANSACTION_RELAXED
38517 ? G_("%<__transaction_relaxed%> without transactional memory "
38519 : G_("%<__transaction_atomic%> without transactional memory "
38520 "support enabled"));
38522 token
= cp_parser_require_keyword (parser
, keyword
,
38523 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38524 : RT_TRANSACTION_RELAXED
));
38525 gcc_assert (token
!= NULL
);
38527 if (keyword
== RID_TRANSACTION_RELAXED
)
38528 this_in
|= TM_STMT_ATTR_RELAXED
;
38530 /* Set this early. This might mean that we allow transaction_cancel in
38531 an expression that we find out later actually has to be a constexpr.
38532 However, we expect that cxx_constant_value will be able to deal with
38533 this; also, if the noexcept has no constexpr, then what we parse next
38534 really is a transaction's body. */
38535 parser
->in_transaction
= this_in
;
38537 /* Parse a noexcept specification. */
38538 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38541 if (!noex
|| !noex_expr
38542 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38544 matching_parens parens
;
38545 parens
.require_open (parser
);
38547 expr
= cp_parser_expression (parser
);
38548 expr
= finish_parenthesized_expr (expr
);
38550 parens
.require_close (parser
);
38554 /* The only expression that is available got parsed for the noexcept
38555 already. noexcept is true then. */
38557 noex
= boolean_true_node
;
38560 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38561 parser
->in_transaction
= old_in
;
38563 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38564 return error_mark_node
;
38566 return (flag_tm
? expr
: error_mark_node
);
38569 /* Parse a function-transaction-block.
38571 function-transaction-block:
38572 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38574 __transaction_atomic txn-attribute[opt] function-try-block
38575 __transaction_relaxed ctor-initializer[opt] function-body
38576 __transaction_relaxed function-try-block
38580 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38582 unsigned char old_in
= parser
->in_transaction
;
38583 unsigned char new_in
= 1;
38584 tree compound_stmt
, stmt
, attrs
;
38587 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38588 || keyword
== RID_TRANSACTION_RELAXED
);
38589 token
= cp_parser_require_keyword (parser
, keyword
,
38590 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38591 : RT_TRANSACTION_RELAXED
));
38592 gcc_assert (token
!= NULL
);
38594 if (keyword
== RID_TRANSACTION_RELAXED
)
38595 new_in
|= TM_STMT_ATTR_RELAXED
;
38598 attrs
= cp_parser_txn_attribute_opt (parser
);
38600 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38603 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38605 parser
->in_transaction
= new_in
;
38607 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38608 cp_parser_function_try_block (parser
);
38610 cp_parser_ctor_initializer_opt_and_function_body
38611 (parser
, /*in_function_try_block=*/false);
38613 parser
->in_transaction
= old_in
;
38615 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38618 /* Parse a __transaction_cancel statement.
38621 __transaction_cancel txn-attribute[opt] ;
38622 __transaction_cancel txn-attribute[opt] throw-expression ;
38624 ??? Cancel and throw is not yet implemented. */
38627 cp_parser_transaction_cancel (cp_parser
*parser
)
38630 bool is_outer
= false;
38633 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38634 RT_TRANSACTION_CANCEL
);
38635 gcc_assert (token
!= NULL
);
38637 attrs
= cp_parser_txn_attribute_opt (parser
);
38639 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38641 /* ??? Parse cancel-and-throw here. */
38643 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38647 error_at (token
->location
, "%<__transaction_cancel%> without "
38648 "transactional memory support enabled");
38649 return error_mark_node
;
38651 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38653 error_at (token
->location
, "%<__transaction_cancel%> within a "
38654 "%<__transaction_relaxed%>");
38655 return error_mark_node
;
38659 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38660 && !is_tm_may_cancel_outer (current_function_decl
))
38662 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38663 "within outer %<__transaction_atomic%>");
38664 error_at (token
->location
,
38665 " or a %<transaction_may_cancel_outer%> function");
38666 return error_mark_node
;
38669 else if (parser
->in_transaction
== 0)
38671 error_at (token
->location
, "%<__transaction_cancel%> not within "
38672 "%<__transaction_atomic%>");
38673 return error_mark_node
;
38676 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38684 static GTY (()) cp_parser
*the_parser
;
38687 /* Special handling for the first token or line in the file. The first
38688 thing in the file might be #pragma GCC pch_preprocess, which loads a
38689 PCH file, which is a GC collection point. So we need to handle this
38690 first pragma without benefit of an existing lexer structure.
38692 Always returns one token to the caller in *FIRST_TOKEN. This is
38693 either the true first token of the file, or the first token after
38694 the initial pragma. */
38697 cp_parser_initial_pragma (cp_token
*first_token
)
38701 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38702 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38705 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38706 if (first_token
->type
== CPP_STRING
)
38708 name
= first_token
->u
.value
;
38710 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38711 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38712 error_at (first_token
->location
,
38713 "junk at end of %<#pragma GCC pch_preprocess%>");
38716 error_at (first_token
->location
, "expected string literal");
38718 /* Skip to the end of the pragma. */
38719 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38720 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38722 /* Now actually load the PCH file. */
38724 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38726 /* Read one more token to return to our caller. We have to do this
38727 after reading the PCH file in, since its pointers have to be
38729 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38732 /* Parse a pragma GCC ivdep. */
38735 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38737 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38741 /* Parse a pragma GCC unroll. */
38743 static unsigned short
38744 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38746 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38747 tree expr
= cp_parser_constant_expression (parser
);
38748 unsigned short unroll
;
38749 expr
= maybe_constant_value (expr
);
38750 HOST_WIDE_INT lunroll
= 0;
38751 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38752 || TREE_CODE (expr
) != INTEGER_CST
38753 || (lunroll
= tree_to_shwi (expr
)) < 0
38754 || lunroll
>= USHRT_MAX
)
38756 error_at (location
, "%<#pragma GCC unroll%> requires an"
38757 " assignment-expression that evaluates to a non-negative"
38758 " integral constant less than %u", USHRT_MAX
);
38763 unroll
= (unsigned short)lunroll
;
38767 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38771 /* Normal parsing of a pragma token. Here we can (and must) use the
38775 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38777 cp_token
*pragma_tok
;
38782 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38783 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38784 parser
->lexer
->in_pragma
= true;
38786 id
= cp_parser_pragma_kind (pragma_tok
);
38787 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38788 cp_ensure_no_omp_declare_simd (parser
);
38791 case PRAGMA_GCC_PCH_PREPROCESS
:
38792 error_at (pragma_tok
->location
,
38793 "%<#pragma GCC pch_preprocess%> must be first");
38796 case PRAGMA_OMP_BARRIER
:
38799 case pragma_compound
:
38800 cp_parser_omp_barrier (parser
, pragma_tok
);
38803 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38804 "used in compound statements", "omp barrier");
38811 case PRAGMA_OMP_FLUSH
:
38814 case pragma_compound
:
38815 cp_parser_omp_flush (parser
, pragma_tok
);
38818 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38819 "used in compound statements", "omp flush");
38826 case PRAGMA_OMP_TASKWAIT
:
38829 case pragma_compound
:
38830 cp_parser_omp_taskwait (parser
, pragma_tok
);
38833 error_at (pragma_tok
->location
,
38834 "%<#pragma %s%> may only be used in compound statements",
38842 case PRAGMA_OMP_TASKYIELD
:
38845 case pragma_compound
:
38846 cp_parser_omp_taskyield (parser
, pragma_tok
);
38849 error_at (pragma_tok
->location
,
38850 "%<#pragma %s%> may only be used in compound statements",
38858 case PRAGMA_OMP_CANCEL
:
38861 case pragma_compound
:
38862 cp_parser_omp_cancel (parser
, pragma_tok
);
38865 error_at (pragma_tok
->location
,
38866 "%<#pragma %s%> may only be used in compound statements",
38874 case PRAGMA_OMP_CANCELLATION_POINT
:
38875 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38878 case PRAGMA_OMP_THREADPRIVATE
:
38879 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38882 case PRAGMA_OMP_DECLARE
:
38883 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38885 case PRAGMA_OACC_DECLARE
:
38886 cp_parser_oacc_declare (parser
, pragma_tok
);
38889 case PRAGMA_OACC_ENTER_DATA
:
38890 if (context
== pragma_stmt
)
38892 error_at (pragma_tok
->location
,
38893 "%<#pragma %s%> may only be used in compound statements",
38897 else if (context
!= pragma_compound
)
38899 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38902 case PRAGMA_OACC_EXIT_DATA
:
38903 if (context
== pragma_stmt
)
38905 error_at (pragma_tok
->location
,
38906 "%<#pragma %s%> may only be used in compound statements",
38910 else if (context
!= pragma_compound
)
38912 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38915 case PRAGMA_OACC_ROUTINE
:
38916 if (context
!= pragma_external
)
38918 error_at (pragma_tok
->location
,
38919 "%<#pragma acc routine%> must be at file scope");
38922 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38925 case PRAGMA_OACC_UPDATE
:
38926 if (context
== pragma_stmt
)
38928 error_at (pragma_tok
->location
,
38929 "%<#pragma %s%> may only be used in compound statements",
38933 else if (context
!= pragma_compound
)
38935 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38938 case PRAGMA_OACC_WAIT
:
38939 if (context
== pragma_stmt
)
38941 error_at (pragma_tok
->location
,
38942 "%<#pragma %s%> may only be used in compound statements",
38946 else if (context
!= pragma_compound
)
38948 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38951 case PRAGMA_OACC_ATOMIC
:
38952 case PRAGMA_OACC_CACHE
:
38953 case PRAGMA_OACC_DATA
:
38954 case PRAGMA_OACC_HOST_DATA
:
38955 case PRAGMA_OACC_KERNELS
:
38956 case PRAGMA_OACC_PARALLEL
:
38957 case PRAGMA_OACC_LOOP
:
38958 case PRAGMA_OMP_ATOMIC
:
38959 case PRAGMA_OMP_CRITICAL
:
38960 case PRAGMA_OMP_DISTRIBUTE
:
38961 case PRAGMA_OMP_FOR
:
38962 case PRAGMA_OMP_MASTER
:
38963 case PRAGMA_OMP_PARALLEL
:
38964 case PRAGMA_OMP_SECTIONS
:
38965 case PRAGMA_OMP_SIMD
:
38966 case PRAGMA_OMP_SINGLE
:
38967 case PRAGMA_OMP_TASK
:
38968 case PRAGMA_OMP_TASKGROUP
:
38969 case PRAGMA_OMP_TASKLOOP
:
38970 case PRAGMA_OMP_TEAMS
:
38971 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38973 stmt
= push_omp_privatization_clauses (false);
38974 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38975 pop_omp_privatization_clauses (stmt
);
38978 case PRAGMA_OMP_ORDERED
:
38979 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38981 stmt
= push_omp_privatization_clauses (false);
38982 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38983 pop_omp_privatization_clauses (stmt
);
38986 case PRAGMA_OMP_TARGET
:
38987 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38989 stmt
= push_omp_privatization_clauses (false);
38990 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38991 pop_omp_privatization_clauses (stmt
);
38994 case PRAGMA_OMP_END_DECLARE_TARGET
:
38995 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38998 case PRAGMA_OMP_SECTION
:
38999 error_at (pragma_tok
->location
,
39000 "%<#pragma omp section%> may only be used in "
39001 "%<#pragma omp sections%> construct");
39006 if (context
== pragma_external
)
39008 error_at (pragma_tok
->location
,
39009 "%<#pragma GCC ivdep%> must be inside a function");
39012 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
39013 unsigned short unroll
;
39014 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39015 if (tok
->type
== CPP_PRAGMA
39016 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
39018 tok
= cp_lexer_consume_token (parser
->lexer
);
39019 unroll
= cp_parser_pragma_unroll (parser
, tok
);
39020 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39024 if (tok
->type
!= CPP_KEYWORD
39025 || (tok
->keyword
!= RID_FOR
39026 && tok
->keyword
!= RID_WHILE
39027 && tok
->keyword
!= RID_DO
))
39029 cp_parser_error (parser
, "for, while or do statement expected");
39032 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39036 case PRAGMA_UNROLL
:
39038 if (context
== pragma_external
)
39040 error_at (pragma_tok
->location
,
39041 "%<#pragma GCC unroll%> must be inside a function");
39044 const unsigned short unroll
39045 = cp_parser_pragma_unroll (parser
, pragma_tok
);
39047 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39048 if (tok
->type
== CPP_PRAGMA
39049 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
39051 tok
= cp_lexer_consume_token (parser
->lexer
);
39052 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
39053 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39057 if (tok
->type
!= CPP_KEYWORD
39058 || (tok
->keyword
!= RID_FOR
39059 && tok
->keyword
!= RID_WHILE
39060 && tok
->keyword
!= RID_DO
))
39062 cp_parser_error (parser
, "for, while or do statement expected");
39065 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39070 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
39071 c_invoke_pragma_handler (id
);
39075 cp_parser_error (parser
, "expected declaration specifiers");
39079 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39083 /* The interface the pragma parsers have to the lexer. */
39086 pragma_lex (tree
*value
, location_t
*loc
)
39088 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39089 enum cpp_ttype ret
= tok
->type
;
39091 *value
= tok
->u
.value
;
39093 *loc
= tok
->location
;
39095 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
39097 else if (ret
== CPP_STRING
)
39098 *value
= cp_parser_string_literal (the_parser
, false, false);
39101 if (ret
== CPP_KEYWORD
)
39103 cp_lexer_consume_token (the_parser
->lexer
);
39110 /* External interface. */
39112 /* Parse one entire translation unit. */
39115 c_parse_file (void)
39117 static bool already_called
= false;
39119 if (already_called
)
39120 fatal_error (input_location
,
39121 "inter-module optimizations not implemented for C++");
39122 already_called
= true;
39124 the_parser
= cp_parser_new ();
39125 push_deferring_access_checks (flag_access_control
39126 ? dk_no_deferred
: dk_no_check
);
39127 cp_parser_translation_unit (the_parser
);
39131 /* Create an identifier for a generic parameter type (a synthesized
39132 template parameter implied by `auto' or a concept identifier). */
39134 static GTY(()) int generic_parm_count
;
39136 make_generic_type_name ()
39139 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39140 return get_identifier (buf
);
39143 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39144 (creating a new template parameter list if necessary). Returns the newly
39145 created template type parm. */
39148 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39150 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39152 /* Before committing to modifying any scope, if we're in an
39153 implicit template scope, and we're trying to synthesize a
39154 constrained parameter, try to find a previous parameter with
39155 the same name. This is the same-type rule for abbreviated
39156 function templates.
39158 NOTE: We can generate implicit parameters when tentatively
39159 parsing a nested name specifier, only to reject that parse
39160 later. However, matching the same template-id as part of a
39161 direct-declarator should generate an identical template
39162 parameter, so this rule will merge them. */
39163 if (parser
->implicit_template_scope
&& constr
)
39165 tree t
= parser
->implicit_template_parms
;
39168 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39170 tree d
= TREE_VALUE (t
);
39171 if (TREE_CODE (d
) == PARM_DECL
)
39172 /* Return the TEMPLATE_PARM_INDEX. */
39173 d
= DECL_INITIAL (d
);
39176 t
= TREE_CHAIN (t
);
39180 /* We are either continuing a function template that already contains implicit
39181 template parameters, creating a new fully-implicit function template, or
39182 extending an existing explicit function template with implicit template
39185 cp_binding_level
*const entry_scope
= current_binding_level
;
39187 bool become_template
= false;
39188 cp_binding_level
*parent_scope
= 0;
39190 if (parser
->implicit_template_scope
)
39192 gcc_assert (parser
->implicit_template_parms
);
39194 current_binding_level
= parser
->implicit_template_scope
;
39198 /* Roll back to the existing template parameter scope (in the case of
39199 extending an explicit function template) or introduce a new template
39200 parameter scope ahead of the function parameter scope (or class scope
39201 in the case of out-of-line member definitions). The function scope is
39202 added back after template parameter synthesis below. */
39204 cp_binding_level
*scope
= entry_scope
;
39206 while (scope
->kind
== sk_function_parms
)
39208 parent_scope
= scope
;
39209 scope
= scope
->level_chain
;
39211 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39213 /* If not defining a class, then any class scope is a scope level in
39214 an out-of-line member definition. In this case simply wind back
39215 beyond the first such scope to inject the template parameter list.
39216 Otherwise wind back to the class being defined. The latter can
39217 occur in class member friend declarations such as:
39223 friend void A::foo (auto);
39226 The template parameter list synthesized for the friend declaration
39227 must be injected in the scope of 'B'. This can also occur in
39228 erroneous cases such as:
39234 void B::foo (auto) {}
39237 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39238 but, nevertheless, the template parameter list synthesized for the
39239 declarator should be injected into the scope of 'A' as if the
39240 ill-formed template was specified explicitly. */
39242 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39244 parent_scope
= scope
;
39245 scope
= scope
->level_chain
;
39249 current_binding_level
= scope
;
39251 if (scope
->kind
!= sk_template_parms
39252 || !function_being_declared_is_template_p (parser
))
39254 /* Introduce a new template parameter list for implicit template
39257 become_template
= true;
39259 parser
->implicit_template_scope
39260 = begin_scope (sk_template_parms
, NULL
);
39262 ++processing_template_decl
;
39264 parser
->fully_implicit_function_template_p
= true;
39265 ++parser
->num_template_parameter_lists
;
39269 /* Synthesize implicit template parameters at the end of the explicit
39270 template parameter list. */
39272 gcc_assert (current_template_parms
);
39274 parser
->implicit_template_scope
= scope
;
39276 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39277 parser
->implicit_template_parms
39278 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39282 /* Synthesize a new template parameter and track the current template
39283 parameter chain with implicit_template_parms. */
39285 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39286 tree synth_id
= make_generic_type_name ();
39287 tree synth_tmpl_parm
;
39288 bool non_type
= false;
39290 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39292 = finish_template_type_parm (class_type_node
, synth_id
);
39293 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39295 = finish_constrained_template_template_parm (proto
, synth_id
);
39298 synth_tmpl_parm
= copy_decl (proto
);
39299 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39303 // Attach the constraint to the parm before processing.
39304 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39305 TREE_TYPE (node
) = constr
;
39307 = process_template_parm (parser
->implicit_template_parms
,
39310 /*non_type=*/non_type
,
39311 /*param_pack=*/false);
39313 // Chain the new parameter to the list of implicit parameters.
39314 if (parser
->implicit_template_parms
)
39315 parser
->implicit_template_parms
39316 = TREE_CHAIN (parser
->implicit_template_parms
);
39318 parser
->implicit_template_parms
= new_parm
;
39320 tree new_decl
= get_local_decls ();
39322 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39323 new_decl
= DECL_INITIAL (new_decl
);
39325 /* If creating a fully implicit function template, start the new implicit
39326 template parameter list with this synthesized type, otherwise grow the
39327 current template parameter list. */
39329 if (become_template
)
39331 parent_scope
->level_chain
= current_binding_level
;
39333 tree new_parms
= make_tree_vec (1);
39334 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39335 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39336 new_parms
, current_template_parms
);
39340 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39341 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39342 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39343 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39346 // If the new parameter was constrained, we need to add that to the
39347 // constraints in the template parameter list.
39348 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39350 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39351 reqs
= conjoin_constraints (reqs
, req
);
39352 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39355 current_binding_level
= entry_scope
;
39360 /* Finish the declaration of a fully implicit function template. Such a
39361 template has no explicit template parameter list so has not been through the
39362 normal template head and tail processing. synthesize_implicit_template_parm
39363 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39364 provided if the declaration is a class member such that its template
39365 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39366 form is returned. Otherwise NULL_TREE is returned. */
39369 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39371 gcc_assert (parser
->fully_implicit_function_template_p
);
39373 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39374 && DECL_VIRTUAL_P (member_decl_opt
))
39376 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39377 "implicit templates may not be %<virtual%>");
39378 DECL_VIRTUAL_P (member_decl_opt
) = false;
39381 if (member_decl_opt
)
39382 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39383 end_template_decl ();
39385 parser
->fully_implicit_function_template_p
= false;
39386 parser
->implicit_template_parms
= 0;
39387 parser
->implicit_template_scope
= 0;
39388 --parser
->num_template_parameter_lists
;
39390 return member_decl_opt
;
39393 /* Like finish_fully_implicit_template, but to be used in error
39394 recovery, rearranging scopes so that we restore the state we had
39395 before synthesize_implicit_template_parm inserted the implement
39396 template parms scope. */
39399 abort_fully_implicit_template (cp_parser
*parser
)
39401 cp_binding_level
*return_to_scope
= current_binding_level
;
39403 if (parser
->implicit_template_scope
39404 && return_to_scope
!= parser
->implicit_template_scope
)
39406 cp_binding_level
*child
= return_to_scope
;
39407 for (cp_binding_level
*scope
= child
->level_chain
;
39408 scope
!= parser
->implicit_template_scope
;
39409 scope
= child
->level_chain
)
39411 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
39412 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
39413 current_binding_level
= parser
->implicit_template_scope
;
39416 return_to_scope
= return_to_scope
->level_chain
;
39418 finish_fully_implicit_template (parser
, NULL
);
39420 gcc_assert (current_binding_level
== return_to_scope
);
39423 /* Helper function for diagnostics that have complained about things
39424 being used with 'extern "C"' linkage.
39426 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39429 maybe_show_extern_c_location (void)
39431 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39432 inform (the_parser
->innermost_linkage_specification_location
,
39433 "%<extern \"C\"%> linkage started here");
39436 #include "gt-cp-parser.h"