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
2238 (cp_parser
*, bool *);
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 *);
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
);
2268 /* Classes [gram.class] */
2270 static tree cp_parser_class_name
2271 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2272 static tree cp_parser_class_specifier
2274 static tree cp_parser_class_head
2275 (cp_parser
*, bool *);
2276 static enum tag_types cp_parser_class_key
2278 static void cp_parser_type_parameter_key
2279 (cp_parser
* parser
);
2280 static void cp_parser_member_specification_opt
2282 static void cp_parser_member_declaration
2284 static tree cp_parser_pure_specifier
2286 static tree cp_parser_constant_initializer
2289 /* Derived classes [gram.class.derived] */
2291 static tree cp_parser_base_clause
2293 static tree cp_parser_base_specifier
2296 /* Special member functions [gram.special] */
2298 static tree cp_parser_conversion_function_id
2300 static tree cp_parser_conversion_type_id
2302 static cp_declarator
*cp_parser_conversion_declarator_opt
2304 static void cp_parser_ctor_initializer_opt
2306 static void cp_parser_mem_initializer_list
2308 static tree cp_parser_mem_initializer
2310 static tree cp_parser_mem_initializer_id
2313 /* Overloading [gram.over] */
2315 static cp_expr cp_parser_operator_function_id
2317 static cp_expr cp_parser_operator
2320 /* Templates [gram.temp] */
2322 static void cp_parser_template_declaration
2323 (cp_parser
*, bool);
2324 static tree cp_parser_template_parameter_list
2326 static tree cp_parser_template_parameter
2327 (cp_parser
*, bool *, bool *);
2328 static tree cp_parser_type_parameter
2329 (cp_parser
*, bool *);
2330 static tree cp_parser_template_id
2331 (cp_parser
*, bool, bool, enum tag_types
, bool);
2332 static tree cp_parser_template_name
2333 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2334 static tree cp_parser_template_argument_list
2336 static tree cp_parser_template_argument
2338 static void cp_parser_explicit_instantiation
2340 static void cp_parser_explicit_specialization
2343 /* Exception handling [gram.exception] */
2345 static tree cp_parser_try_block
2347 static void cp_parser_function_try_block
2349 static void cp_parser_handler_seq
2351 static void cp_parser_handler
2353 static tree cp_parser_exception_declaration
2355 static tree cp_parser_throw_expression
2357 static tree cp_parser_exception_specification_opt
2359 static tree cp_parser_type_id_list
2362 /* GNU Extensions */
2364 static tree cp_parser_asm_specification_opt
2366 static tree cp_parser_asm_operand_list
2368 static tree cp_parser_asm_clobber_list
2370 static tree cp_parser_asm_label_list
2372 static bool cp_next_tokens_can_be_attribute_p
2374 static bool cp_next_tokens_can_be_gnu_attribute_p
2376 static bool cp_next_tokens_can_be_std_attribute_p
2378 static bool cp_nth_tokens_can_be_std_attribute_p
2379 (cp_parser
*, size_t);
2380 static bool cp_nth_tokens_can_be_gnu_attribute_p
2381 (cp_parser
*, size_t);
2382 static bool cp_nth_tokens_can_be_attribute_p
2383 (cp_parser
*, size_t);
2384 static tree cp_parser_attributes_opt
2386 static tree cp_parser_gnu_attributes_opt
2388 static tree cp_parser_gnu_attribute_list
2390 static tree cp_parser_std_attribute
2391 (cp_parser
*, tree
);
2392 static tree cp_parser_std_attribute_spec
2394 static tree cp_parser_std_attribute_spec_seq
2396 static size_t cp_parser_skip_attributes_opt
2397 (cp_parser
*, size_t);
2398 static bool cp_parser_extension_opt
2399 (cp_parser
*, int *);
2400 static void cp_parser_label_declaration
2403 /* Concept Extensions */
2405 static tree cp_parser_requires_clause
2407 static tree cp_parser_requires_clause_opt
2409 static tree cp_parser_requires_expression
2411 static tree cp_parser_requirement_parameter_list
2413 static tree cp_parser_requirement_body
2415 static tree cp_parser_requirement_list
2417 static tree cp_parser_requirement
2419 static tree cp_parser_simple_requirement
2421 static tree cp_parser_compound_requirement
2423 static tree cp_parser_type_requirement
2425 static tree cp_parser_nested_requirement
2428 /* Transactional Memory Extensions */
2430 static tree cp_parser_transaction
2431 (cp_parser
*, cp_token
*);
2432 static tree cp_parser_transaction_expression
2433 (cp_parser
*, enum rid
);
2434 static void cp_parser_function_transaction
2435 (cp_parser
*, enum rid
);
2436 static tree cp_parser_transaction_cancel
2439 enum pragma_context
{
2446 static bool cp_parser_pragma
2447 (cp_parser
*, enum pragma_context
, bool *);
2449 /* Objective-C++ Productions */
2451 static tree cp_parser_objc_message_receiver
2453 static tree cp_parser_objc_message_args
2455 static tree cp_parser_objc_message_expression
2457 static cp_expr cp_parser_objc_encode_expression
2459 static tree cp_parser_objc_defs_expression
2461 static tree cp_parser_objc_protocol_expression
2463 static tree cp_parser_objc_selector_expression
2465 static cp_expr cp_parser_objc_expression
2467 static bool cp_parser_objc_selector_p
2469 static tree cp_parser_objc_selector
2471 static tree cp_parser_objc_protocol_refs_opt
2473 static void cp_parser_objc_declaration
2474 (cp_parser
*, tree
);
2475 static tree cp_parser_objc_statement
2477 static bool cp_parser_objc_valid_prefix_attributes
2478 (cp_parser
*, tree
*);
2479 static void cp_parser_objc_at_property_declaration
2481 static void cp_parser_objc_at_synthesize_declaration
2483 static void cp_parser_objc_at_dynamic_declaration
2485 static tree cp_parser_objc_struct_declaration
2488 /* Utility Routines */
2490 static cp_expr cp_parser_lookup_name
2491 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2492 static tree cp_parser_lookup_name_simple
2493 (cp_parser
*, tree
, location_t
);
2494 static tree cp_parser_maybe_treat_template_as_class
2496 static bool cp_parser_check_declarator_template_parameters
2497 (cp_parser
*, cp_declarator
*, location_t
);
2498 static bool cp_parser_check_template_parameters
2499 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2500 static cp_expr cp_parser_simple_cast_expression
2502 static tree cp_parser_global_scope_opt
2503 (cp_parser
*, bool);
2504 static bool cp_parser_constructor_declarator_p
2505 (cp_parser
*, bool);
2506 static tree cp_parser_function_definition_from_specifiers_and_declarator
2507 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2508 static tree cp_parser_function_definition_after_declarator
2509 (cp_parser
*, bool);
2510 static bool cp_parser_template_declaration_after_export
2511 (cp_parser
*, bool);
2512 static void cp_parser_perform_template_parameter_access_checks
2513 (vec
<deferred_access_check
, va_gc
> *);
2514 static tree cp_parser_single_declaration
2515 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2516 static cp_expr cp_parser_functional_cast
2517 (cp_parser
*, tree
);
2518 static tree cp_parser_save_member_function_body
2519 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2520 static tree cp_parser_save_nsdmi
2522 static tree cp_parser_enclosed_template_argument_list
2524 static void cp_parser_save_default_args
2525 (cp_parser
*, tree
);
2526 static void cp_parser_late_parsing_for_member
2527 (cp_parser
*, tree
);
2528 static tree cp_parser_late_parse_one_default_arg
2529 (cp_parser
*, tree
, tree
, tree
);
2530 static void cp_parser_late_parsing_nsdmi
2531 (cp_parser
*, tree
);
2532 static void cp_parser_late_parsing_default_args
2533 (cp_parser
*, tree
);
2534 static tree cp_parser_sizeof_operand
2535 (cp_parser
*, enum rid
);
2536 static cp_expr cp_parser_trait_expr
2537 (cp_parser
*, enum rid
);
2538 static bool cp_parser_declares_only_class_p
2540 static void cp_parser_set_storage_class
2541 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2542 static void cp_parser_set_decl_spec_type
2543 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2544 static void set_and_check_decl_spec_loc
2545 (cp_decl_specifier_seq
*decl_specs
,
2546 cp_decl_spec ds
, cp_token
*);
2547 static bool cp_parser_friend_p
2548 (const cp_decl_specifier_seq
*);
2549 static void cp_parser_required_error
2550 (cp_parser
*, required_token
, bool, location_t
);
2551 static cp_token
*cp_parser_require
2552 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2553 static cp_token
*cp_parser_require_keyword
2554 (cp_parser
*, enum rid
, required_token
);
2555 static bool cp_parser_token_starts_function_definition_p
2557 static bool cp_parser_next_token_starts_class_definition_p
2559 static bool cp_parser_next_token_ends_template_argument_p
2561 static bool cp_parser_nth_token_starts_template_argument_list_p
2562 (cp_parser
*, size_t);
2563 static enum tag_types cp_parser_token_is_class_key
2565 static enum tag_types cp_parser_token_is_type_parameter_key
2567 static void cp_parser_check_class_key
2568 (enum tag_types
, tree type
);
2569 static void cp_parser_check_access_in_redeclaration
2570 (tree type
, location_t location
);
2571 static bool cp_parser_optional_template_keyword
2573 static void cp_parser_pre_parsed_nested_name_specifier
2575 static bool cp_parser_cache_group
2576 (cp_parser
*, enum cpp_ttype
, unsigned);
2577 static tree cp_parser_cache_defarg
2578 (cp_parser
*parser
, bool nsdmi
);
2579 static void cp_parser_parse_tentatively
2581 static void cp_parser_commit_to_tentative_parse
2583 static void cp_parser_commit_to_topmost_tentative_parse
2585 static void cp_parser_abort_tentative_parse
2587 static bool cp_parser_parse_definitely
2589 static inline bool cp_parser_parsing_tentatively
2591 static bool cp_parser_uncommitted_to_tentative_parse_p
2593 static void cp_parser_error
2594 (cp_parser
*, const char *);
2595 static void cp_parser_name_lookup_error
2596 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2597 static bool cp_parser_simulate_error
2599 static bool cp_parser_check_type_definition
2601 static void cp_parser_check_for_definition_in_return_type
2602 (cp_declarator
*, tree
, location_t type_location
);
2603 static void cp_parser_check_for_invalid_template_id
2604 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2605 static bool cp_parser_non_integral_constant_expression
2606 (cp_parser
*, non_integral_constant
);
2607 static void cp_parser_diagnose_invalid_type_name
2608 (cp_parser
*, tree
, location_t
);
2609 static bool cp_parser_parse_and_diagnose_invalid_type_name
2611 static int cp_parser_skip_to_closing_parenthesis
2612 (cp_parser
*, bool, bool, bool);
2613 static void cp_parser_skip_to_end_of_statement
2615 static void cp_parser_consume_semicolon_at_end_of_statement
2617 static void cp_parser_skip_to_end_of_block_or_statement
2619 static bool cp_parser_skip_to_closing_brace
2621 static void cp_parser_skip_to_end_of_template_parameter_list
2623 static void cp_parser_skip_to_pragma_eol
2624 (cp_parser
*, cp_token
*);
2625 static bool cp_parser_error_occurred
2627 static bool cp_parser_allow_gnu_extensions_p
2629 static bool cp_parser_is_pure_string_literal
2631 static bool cp_parser_is_string_literal
2633 static bool cp_parser_is_keyword
2634 (cp_token
*, enum rid
);
2635 static tree cp_parser_make_typename_type
2636 (cp_parser
*, tree
, location_t location
);
2637 static cp_declarator
* cp_parser_make_indirect_declarator
2638 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2639 static bool cp_parser_compound_literal_p
2641 static bool cp_parser_array_designator_p
2643 static bool cp_parser_init_statement_p
2645 static bool cp_parser_skip_to_closing_square_bracket
2648 /* Concept-related syntactic transformations */
2650 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2651 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2653 // -------------------------------------------------------------------------- //
2654 // Unevaluated Operand Guard
2656 // Implementation of an RAII helper for unevaluated operand parsing.
2657 cp_unevaluated::cp_unevaluated ()
2659 ++cp_unevaluated_operand
;
2660 ++c_inhibit_evaluation_warnings
;
2663 cp_unevaluated::~cp_unevaluated ()
2665 --c_inhibit_evaluation_warnings
;
2666 --cp_unevaluated_operand
;
2669 // -------------------------------------------------------------------------- //
2670 // Tentative Parsing
2672 /* Returns nonzero if we are parsing tentatively. */
2675 cp_parser_parsing_tentatively (cp_parser
* parser
)
2677 return parser
->context
->next
!= NULL
;
2680 /* Returns nonzero if TOKEN is a string literal. */
2683 cp_parser_is_pure_string_literal (cp_token
* token
)
2685 return (token
->type
== CPP_STRING
||
2686 token
->type
== CPP_STRING16
||
2687 token
->type
== CPP_STRING32
||
2688 token
->type
== CPP_WSTRING
||
2689 token
->type
== CPP_UTF8STRING
);
2692 /* Returns nonzero if TOKEN is a string literal
2693 of a user-defined string literal. */
2696 cp_parser_is_string_literal (cp_token
* token
)
2698 return (cp_parser_is_pure_string_literal (token
) ||
2699 token
->type
== CPP_STRING_USERDEF
||
2700 token
->type
== CPP_STRING16_USERDEF
||
2701 token
->type
== CPP_STRING32_USERDEF
||
2702 token
->type
== CPP_WSTRING_USERDEF
||
2703 token
->type
== CPP_UTF8STRING_USERDEF
);
2706 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2709 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2711 return token
->keyword
== keyword
;
2714 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2717 static enum pragma_kind
2718 cp_parser_pragma_kind (cp_token
*token
)
2720 if (token
->type
!= CPP_PRAGMA
)
2722 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2723 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2726 /* Helper function for cp_parser_error.
2727 Having peeked a token of kind TOK1_KIND that might signify
2728 a conflict marker, peek successor tokens to determine
2729 if we actually do have a conflict marker.
2730 Specifically, we consider a run of 7 '<', '=' or '>' characters
2731 at the start of a line as a conflict marker.
2732 These come through the lexer as three pairs and a single,
2733 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2734 If it returns true, *OUT_LOC is written to with the location/range
2738 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2739 location_t
*out_loc
)
2741 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2742 if (token2
->type
!= tok1_kind
)
2744 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2745 if (token3
->type
!= tok1_kind
)
2747 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2748 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2751 /* It must be at the start of the line. */
2752 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2753 if (LOCATION_COLUMN (start_loc
) != 1)
2756 /* We have a conflict marker. Construct a location of the form:
2759 with start == caret, finishing at the end of the marker. */
2760 location_t finish_loc
= get_finish (token4
->location
);
2761 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2766 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2770 get_matching_symbol (required_token token_desc
)
2777 case RT_CLOSE_BRACE
:
2779 case RT_CLOSE_PAREN
:
2784 /* Attempt to convert TOKEN_DESC from a required_token to an
2785 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2787 static enum cpp_ttype
2788 get_required_cpp_ttype (required_token token_desc
)
2793 return CPP_SEMICOLON
;
2795 return CPP_OPEN_PAREN
;
2796 case RT_CLOSE_BRACE
:
2797 return CPP_CLOSE_BRACE
;
2799 return CPP_OPEN_BRACE
;
2800 case RT_CLOSE_SQUARE
:
2801 return CPP_CLOSE_SQUARE
;
2802 case RT_OPEN_SQUARE
:
2803 return CPP_OPEN_SQUARE
;
2808 case RT_CLOSE_PAREN
:
2809 return CPP_CLOSE_PAREN
;
2812 /* Use CPP_EOF as a "no completions possible" code. */
2818 /* Subroutine of cp_parser_error and cp_parser_required_error.
2820 Issue a diagnostic of the form
2821 FILE:LINE: MESSAGE before TOKEN
2822 where TOKEN is the next token in the input stream. MESSAGE
2823 (specified by the caller) is usually of the form "expected
2826 This bypasses the check for tentative passing, and potentially
2827 adds material needed by cp_parser_required_error.
2829 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2830 suggesting insertion of the missing token.
2832 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2833 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2837 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2838 required_token missing_token_desc
,
2839 location_t matching_location
)
2841 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2842 /* This diagnostic makes more sense if it is tagged to the line
2843 of the token we just peeked at. */
2844 cp_lexer_set_source_position_from_token (token
);
2846 if (token
->type
== CPP_PRAGMA
)
2848 error_at (token
->location
,
2849 "%<#pragma%> is not allowed here");
2850 cp_parser_skip_to_pragma_eol (parser
, token
);
2854 /* If this is actually a conflict marker, report it as such. */
2855 if (token
->type
== CPP_LSHIFT
2856 || token
->type
== CPP_RSHIFT
2857 || token
->type
== CPP_EQ_EQ
)
2860 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2862 error_at (loc
, "version control conflict marker in file");
2867 gcc_rich_location
richloc (input_location
);
2869 bool added_matching_location
= false;
2871 if (missing_token_desc
!= RT_NONE
)
2873 /* Potentially supply a fix-it hint, suggesting to add the
2874 missing token immediately after the *previous* token.
2875 This may move the primary location within richloc. */
2876 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2877 location_t prev_token_loc
2878 = cp_lexer_previous_token (parser
->lexer
)->location
;
2879 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2881 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2882 Attempt to consolidate diagnostics by printing it as a
2883 secondary range within the main diagnostic. */
2884 if (matching_location
!= UNKNOWN_LOCATION
)
2885 added_matching_location
2886 = richloc
.add_location_if_nearby (matching_location
);
2889 /* Actually emit the error. */
2890 c_parse_error (gmsgid
,
2891 /* Because c_parser_error does not understand
2892 CPP_KEYWORD, keywords are treated like
2894 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2895 token
->u
.value
, token
->flags
, &richloc
);
2897 if (missing_token_desc
!= RT_NONE
)
2899 /* If we weren't able to consolidate matching_location, then
2900 print it as a secondary diagnostic. */
2901 if (matching_location
!= UNKNOWN_LOCATION
2902 && !added_matching_location
)
2903 inform (matching_location
, "to match this %qs",
2904 get_matching_symbol (missing_token_desc
));
2908 /* If not parsing tentatively, issue a diagnostic of the form
2909 FILE:LINE: MESSAGE before TOKEN
2910 where TOKEN is the next token in the input stream. MESSAGE
2911 (specified by the caller) is usually of the form "expected
2915 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2917 if (!cp_parser_simulate_error (parser
))
2918 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2921 /* Issue an error about name-lookup failing. NAME is the
2922 IDENTIFIER_NODE DECL is the result of
2923 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2924 the thing that we hoped to find. */
2927 cp_parser_name_lookup_error (cp_parser
* parser
,
2930 name_lookup_error desired
,
2931 location_t location
)
2933 /* If name lookup completely failed, tell the user that NAME was not
2935 if (decl
== error_mark_node
)
2937 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2938 error_at (location
, "%<%E::%E%> has not been declared",
2939 parser
->scope
, name
);
2940 else if (parser
->scope
== global_namespace
)
2941 error_at (location
, "%<::%E%> has not been declared", name
);
2942 else if (parser
->object_scope
2943 && !CLASS_TYPE_P (parser
->object_scope
))
2944 error_at (location
, "request for member %qE in non-class type %qT",
2945 name
, parser
->object_scope
);
2946 else if (parser
->object_scope
)
2947 error_at (location
, "%<%T::%E%> has not been declared",
2948 parser
->object_scope
, name
);
2950 error_at (location
, "%qE has not been declared", name
);
2952 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2957 error_at (location
, "%<%E::%E%> is not a type",
2958 parser
->scope
, name
);
2961 error_at (location
, "%<%E::%E%> is not a class or namespace",
2962 parser
->scope
, name
);
2966 "%<%E::%E%> is not a class, namespace, or enumeration",
2967 parser
->scope
, name
);
2974 else if (parser
->scope
== global_namespace
)
2979 error_at (location
, "%<::%E%> is not a type", name
);
2982 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2986 "%<::%E%> is not a class, namespace, or enumeration",
2998 error_at (location
, "%qE is not a type", name
);
3001 error_at (location
, "%qE is not a class or namespace", name
);
3005 "%qE is not a class, namespace, or enumeration", name
);
3013 /* If we are parsing tentatively, remember that an error has occurred
3014 during this tentative parse. Returns true if the error was
3015 simulated; false if a message should be issued by the caller. */
3018 cp_parser_simulate_error (cp_parser
* parser
)
3020 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3022 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3028 /* This function is called when a type is defined. If type
3029 definitions are forbidden at this point, an error message is
3033 cp_parser_check_type_definition (cp_parser
* parser
)
3035 /* If types are forbidden here, issue a message. */
3036 if (parser
->type_definition_forbidden_message
)
3038 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3039 in the message need to be interpreted. */
3040 error (parser
->type_definition_forbidden_message
);
3046 /* This function is called when the DECLARATOR is processed. The TYPE
3047 was a type defined in the decl-specifiers. If it is invalid to
3048 define a type in the decl-specifiers for DECLARATOR, an error is
3049 issued. TYPE_LOCATION is the location of TYPE and is used
3050 for error reporting. */
3053 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3054 tree type
, location_t type_location
)
3056 /* [dcl.fct] forbids type definitions in return types.
3057 Unfortunately, it's not easy to know whether or not we are
3058 processing a return type until after the fact. */
3060 && (declarator
->kind
== cdk_pointer
3061 || declarator
->kind
== cdk_reference
3062 || declarator
->kind
== cdk_ptrmem
))
3063 declarator
= declarator
->declarator
;
3065 && declarator
->kind
== cdk_function
)
3067 error_at (type_location
,
3068 "new types may not be defined in a return type");
3069 inform (type_location
,
3070 "(perhaps a semicolon is missing after the definition of %qT)",
3075 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3076 "<" in any valid C++ program. If the next token is indeed "<",
3077 issue a message warning the user about what appears to be an
3078 invalid attempt to form a template-id. LOCATION is the location
3079 of the type-specifier (TYPE) */
3082 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3084 enum tag_types tag_type
,
3085 location_t location
)
3087 cp_token_position start
= 0;
3089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3091 if (TREE_CODE (type
) == TYPE_DECL
)
3092 type
= TREE_TYPE (type
);
3093 if (TYPE_P (type
) && !template_placeholder_p (type
))
3094 error_at (location
, "%qT is not a template", type
);
3095 else if (identifier_p (type
))
3097 if (tag_type
!= none_type
)
3098 error_at (location
, "%qE is not a class template", type
);
3100 error_at (location
, "%qE is not a template", type
);
3103 error_at (location
, "invalid template-id");
3104 /* Remember the location of the invalid "<". */
3105 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3106 start
= cp_lexer_token_position (parser
->lexer
, true);
3107 /* Consume the "<". */
3108 cp_lexer_consume_token (parser
->lexer
);
3109 /* Parse the template arguments. */
3110 cp_parser_enclosed_template_argument_list (parser
);
3111 /* Permanently remove the invalid template arguments so that
3112 this error message is not issued again. */
3114 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3118 /* If parsing an integral constant-expression, issue an error message
3119 about the fact that THING appeared and return true. Otherwise,
3120 return false. In either case, set
3121 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3124 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3125 non_integral_constant thing
)
3127 parser
->non_integral_constant_expression_p
= true;
3128 if (parser
->integral_constant_expression_p
)
3130 if (!parser
->allow_non_integral_constant_expression_p
)
3132 const char *msg
= NULL
;
3136 pedwarn (input_location
, OPT_Wpedantic
,
3137 "ISO C++ forbids using a floating-point literal "
3138 "in a constant-expression");
3141 error ("a cast to a type other than an integral or "
3142 "enumeration type cannot appear in a "
3143 "constant-expression");
3146 error ("%<typeid%> operator "
3147 "cannot appear in a constant-expression");
3150 error ("non-constant compound literals "
3151 "cannot appear in a constant-expression");
3154 error ("a function call "
3155 "cannot appear in a constant-expression");
3158 error ("an increment "
3159 "cannot appear in a constant-expression");
3162 error ("an decrement "
3163 "cannot appear in a constant-expression");
3166 error ("an array reference "
3167 "cannot appear in a constant-expression");
3169 case NIC_ADDR_LABEL
:
3170 error ("the address of a label "
3171 "cannot appear in a constant-expression");
3173 case NIC_OVERLOADED
:
3174 error ("calls to overloaded operators "
3175 "cannot appear in a constant-expression");
3177 case NIC_ASSIGNMENT
:
3178 error ("an assignment cannot appear in a constant-expression");
3181 error ("a comma operator "
3182 "cannot appear in a constant-expression");
3184 case NIC_CONSTRUCTOR
:
3185 error ("a call to a constructor "
3186 "cannot appear in a constant-expression");
3188 case NIC_TRANSACTION
:
3189 error ("a transaction expression "
3190 "cannot appear in a constant-expression");
3196 msg
= "__FUNCTION__";
3198 case NIC_PRETTY_FUNC
:
3199 msg
= "__PRETTY_FUNCTION__";
3219 case NIC_PREINCREMENT
:
3222 case NIC_PREDECREMENT
:
3235 error ("%qs cannot appear in a constant-expression", msg
);
3242 /* Emit a diagnostic for an invalid type name. This function commits
3243 to the current active tentative parse, if any. (Otherwise, the
3244 problematic construct might be encountered again later, resulting
3245 in duplicate error messages.) LOCATION is the location of ID. */
3248 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3249 location_t location
)
3251 tree decl
, ambiguous_decls
;
3252 cp_parser_commit_to_tentative_parse (parser
);
3253 /* Try to lookup the identifier. */
3254 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3255 /*is_template=*/false,
3256 /*is_namespace=*/false,
3257 /*check_dependency=*/true,
3258 &ambiguous_decls
, location
);
3259 if (ambiguous_decls
)
3260 /* If the lookup was ambiguous, an error will already have
3263 /* If the lookup found a template-name, it means that the user forgot
3264 to specify an argument list. Emit a useful error message. */
3265 if (DECL_TYPE_TEMPLATE_P (decl
))
3268 "invalid use of template-name %qE without an argument list",
3270 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3271 inform (location
, "class template argument deduction is only available "
3272 "with -std=c++17 or -std=gnu++17");
3273 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3275 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3276 error_at (location
, "invalid use of destructor %qD as a type", id
);
3277 else if (TREE_CODE (decl
) == TYPE_DECL
)
3278 /* Something like 'unsigned A a;' */
3279 error_at (location
, "invalid combination of multiple type-specifiers");
3280 else if (!parser
->scope
)
3282 /* Issue an error message. */
3284 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3285 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3288 gcc_rich_location
richloc (location
);
3289 richloc
.add_fixit_replace (hint
.suggestion ());
3291 "%qE does not name a type; did you mean %qs?",
3292 id
, hint
.suggestion ());
3295 error_at (location
, "%qE does not name a type", id
);
3296 /* If we're in a template class, it's possible that the user was
3297 referring to a type from a base class. For example:
3299 template <typename T> struct A { typedef T X; };
3300 template <typename T> struct B : public A<T> { X x; };
3302 The user should have said "typename A<T>::X". */
3303 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3304 inform (location
, "C++11 %<constexpr%> only available with "
3305 "-std=c++11 or -std=gnu++11");
3306 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3307 inform (location
, "C++11 %<noexcept%> only available with "
3308 "-std=c++11 or -std=gnu++11");
3309 else if (cxx_dialect
< cxx11
3310 && TREE_CODE (id
) == IDENTIFIER_NODE
3311 && id_equal (id
, "thread_local"))
3312 inform (location
, "C++11 %<thread_local%> only available with "
3313 "-std=c++11 or -std=gnu++11");
3314 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3315 inform (location
, "%<concept%> only available with -fconcepts");
3316 else if (processing_template_decl
&& current_class_type
3317 && TYPE_BINFO (current_class_type
))
3321 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3325 tree base_type
= BINFO_TYPE (b
);
3326 if (CLASS_TYPE_P (base_type
)
3327 && dependent_type_p (base_type
))
3330 /* Go from a particular instantiation of the
3331 template (which will have an empty TYPE_FIELDs),
3332 to the main version. */
3333 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3334 for (field
= TYPE_FIELDS (base_type
);
3336 field
= DECL_CHAIN (field
))
3337 if (TREE_CODE (field
) == TYPE_DECL
3338 && DECL_NAME (field
) == id
)
3341 "(perhaps %<typename %T::%E%> was intended)",
3342 BINFO_TYPE (b
), id
);
3351 /* Here we diagnose qualified-ids where the scope is actually correct,
3352 but the identifier does not resolve to a valid type name. */
3353 else if (parser
->scope
!= error_mark_node
)
3355 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3358 error_at (location_of (id
),
3359 "%qE in namespace %qE does not name a template type",
3361 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3362 error_at (location_of (id
),
3363 "%qE in namespace %qE does not name a template type",
3364 TREE_OPERAND (id
, 0), parser
->scope
);
3366 error_at (location_of (id
),
3367 "%qE in namespace %qE does not name a type",
3370 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3371 else if (decl
== error_mark_node
)
3372 suggest_alternative_in_explicit_scope (location
, id
,
3375 else if (CLASS_TYPE_P (parser
->scope
)
3376 && constructor_name_p (id
, parser
->scope
))
3379 error_at (location
, "%<%T::%E%> names the constructor, not"
3380 " the type", parser
->scope
, id
);
3381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3382 error_at (location
, "and %qT has no template constructors",
3385 else if (TYPE_P (parser
->scope
)
3386 && dependent_scope_p (parser
->scope
))
3388 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3390 "need %<typename%> before %<%T::%D::%E%> because "
3391 "%<%T::%D%> is a dependent scope",
3392 TYPE_CONTEXT (parser
->scope
),
3393 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3395 TYPE_CONTEXT (parser
->scope
),
3396 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3398 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3399 "%qT is a dependent scope",
3400 parser
->scope
, id
, parser
->scope
);
3402 else if (TYPE_P (parser
->scope
))
3404 if (!COMPLETE_TYPE_P (parser
->scope
))
3405 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3407 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3408 error_at (location_of (id
),
3409 "%qE in %q#T does not name a template type",
3411 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3412 error_at (location_of (id
),
3413 "%qE in %q#T does not name a template type",
3414 TREE_OPERAND (id
, 0), parser
->scope
);
3416 error_at (location_of (id
),
3417 "%qE in %q#T does not name a type",
3420 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3427 /* Check for a common situation where a type-name should be present,
3428 but is not, and issue a sensible error message. Returns true if an
3429 invalid type-name was detected.
3431 The situation handled by this function are variable declarations of the
3432 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3433 Usually, `ID' should name a type, but if we got here it means that it
3434 does not. We try to emit the best possible error message depending on
3435 how exactly the id-expression looks like. */
3438 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3441 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3443 /* Avoid duplicate error about ambiguous lookup. */
3444 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3446 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3447 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3451 cp_parser_parse_tentatively (parser
);
3452 id
= cp_parser_id_expression (parser
,
3453 /*template_keyword_p=*/false,
3454 /*check_dependency_p=*/true,
3455 /*template_p=*/NULL
,
3456 /*declarator_p=*/true,
3457 /*optional_p=*/false);
3458 /* If the next token is a (, this is a function with no explicit return
3459 type, i.e. constructor, destructor or conversion op. */
3460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3461 || TREE_CODE (id
) == TYPE_DECL
)
3463 cp_parser_abort_tentative_parse (parser
);
3466 if (!cp_parser_parse_definitely (parser
))
3469 /* Emit a diagnostic for the invalid type. */
3470 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3472 /* If we aren't in the middle of a declarator (i.e. in a
3473 parameter-declaration-clause), skip to the end of the declaration;
3474 there's no point in trying to process it. */
3475 if (!parser
->in_declarator_p
)
3476 cp_parser_skip_to_end_of_block_or_statement (parser
);
3480 /* Consume tokens up to, and including, the next non-nested closing `)'.
3481 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3482 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3483 found an unnested token of that type. */
3486 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3491 unsigned paren_depth
= 0;
3492 unsigned brace_depth
= 0;
3493 unsigned square_depth
= 0;
3495 if (recovering
&& or_ttype
== CPP_EOF
3496 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3501 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3503 /* Have we found what we're looking for before the closing paren? */
3504 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3505 && !brace_depth
&& !paren_depth
&& !square_depth
)
3508 switch (token
->type
)
3511 case CPP_PRAGMA_EOL
:
3512 /* If we've run out of tokens, then there is no closing `)'. */
3515 /* This is good for lambda expression capture-lists. */
3516 case CPP_OPEN_SQUARE
:
3519 case CPP_CLOSE_SQUARE
:
3520 if (!square_depth
--)
3525 /* This matches the processing in skip_to_end_of_statement. */
3530 case CPP_OPEN_BRACE
:
3533 case CPP_CLOSE_BRACE
:
3538 case CPP_OPEN_PAREN
:
3543 case CPP_CLOSE_PAREN
:
3544 if (!brace_depth
&& !paren_depth
--)
3547 cp_lexer_consume_token (parser
->lexer
);
3556 /* Consume the token. */
3557 cp_lexer_consume_token (parser
->lexer
);
3561 /* Consume tokens up to, and including, the next non-nested closing `)'.
3562 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3563 are doing error recovery. Returns -1 if OR_COMMA is true and we
3564 found an unnested token of that type. */
3567 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3572 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3573 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3574 ttype
, consume_paren
);
3577 /* Consume tokens until we reach the end of the current statement.
3578 Normally, that will be just before consuming a `;'. However, if a
3579 non-nested `}' comes first, then we stop before consuming that. */
3582 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3584 unsigned nesting_depth
= 0;
3586 /* Unwind generic function template scope if necessary. */
3587 if (parser
->fully_implicit_function_template_p
)
3588 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3592 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3594 switch (token
->type
)
3597 case CPP_PRAGMA_EOL
:
3598 /* If we've run out of tokens, stop. */
3602 /* If the next token is a `;', we have reached the end of the
3608 case CPP_CLOSE_BRACE
:
3609 /* If this is a non-nested '}', stop before consuming it.
3610 That way, when confronted with something like:
3614 we stop before consuming the closing '}', even though we
3615 have not yet reached a `;'. */
3616 if (nesting_depth
== 0)
3619 /* If it is the closing '}' for a block that we have
3620 scanned, stop -- but only after consuming the token.
3626 we will stop after the body of the erroneously declared
3627 function, but before consuming the following `typedef'
3629 if (--nesting_depth
== 0)
3631 cp_lexer_consume_token (parser
->lexer
);
3636 case CPP_OPEN_BRACE
:
3644 /* Consume the token. */
3645 cp_lexer_consume_token (parser
->lexer
);
3649 /* This function is called at the end of a statement or declaration.
3650 If the next token is a semicolon, it is consumed; otherwise, error
3651 recovery is attempted. */
3654 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3656 /* Look for the trailing `;'. */
3657 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3659 /* If there is additional (erroneous) input, skip to the end of
3661 cp_parser_skip_to_end_of_statement (parser
);
3662 /* If the next token is now a `;', consume it. */
3663 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3664 cp_lexer_consume_token (parser
->lexer
);
3668 /* Skip tokens until we have consumed an entire block, or until we
3669 have consumed a non-nested `;'. */
3672 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3674 int nesting_depth
= 0;
3676 /* Unwind generic function template scope if necessary. */
3677 if (parser
->fully_implicit_function_template_p
)
3678 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3680 while (nesting_depth
>= 0)
3682 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3684 switch (token
->type
)
3687 case CPP_PRAGMA_EOL
:
3688 /* If we've run out of tokens, stop. */
3692 /* Stop if this is an unnested ';'. */
3697 case CPP_CLOSE_BRACE
:
3698 /* Stop if this is an unnested '}', or closes the outermost
3701 if (nesting_depth
< 0)
3707 case CPP_OPEN_BRACE
:
3716 /* Consume the token. */
3717 cp_lexer_consume_token (parser
->lexer
);
3721 /* Skip tokens until a non-nested closing curly brace is the next
3722 token, or there are no more tokens. Return true in the first case,
3726 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3728 unsigned nesting_depth
= 0;
3732 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3734 switch (token
->type
)
3737 case CPP_PRAGMA_EOL
:
3738 /* If we've run out of tokens, stop. */
3741 case CPP_CLOSE_BRACE
:
3742 /* If the next token is a non-nested `}', then we have reached
3743 the end of the current block. */
3744 if (nesting_depth
-- == 0)
3748 case CPP_OPEN_BRACE
:
3749 /* If it the next token is a `{', then we are entering a new
3750 block. Consume the entire block. */
3758 /* Consume the token. */
3759 cp_lexer_consume_token (parser
->lexer
);
3763 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3764 parameter is the PRAGMA token, allowing us to purge the entire pragma
3768 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3772 parser
->lexer
->in_pragma
= false;
3775 token
= cp_lexer_consume_token (parser
->lexer
);
3776 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3778 /* Ensure that the pragma is not parsed again. */
3779 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3782 /* Require pragma end of line, resyncing with it as necessary. The
3783 arguments are as for cp_parser_skip_to_pragma_eol. */
3786 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3788 parser
->lexer
->in_pragma
= false;
3789 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3790 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3793 /* This is a simple wrapper around make_typename_type. When the id is
3794 an unresolved identifier node, we can provide a superior diagnostic
3795 using cp_parser_diagnose_invalid_type_name. */
3798 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3799 location_t id_location
)
3802 if (identifier_p (id
))
3804 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3805 /*complain=*/tf_none
);
3806 if (result
== error_mark_node
)
3807 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3810 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3813 /* This is a wrapper around the
3814 make_{pointer,ptrmem,reference}_declarator functions that decides
3815 which one to call based on the CODE and CLASS_TYPE arguments. The
3816 CODE argument should be one of the values returned by
3817 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3818 appertain to the pointer or reference. */
3820 static cp_declarator
*
3821 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3822 cp_cv_quals cv_qualifiers
,
3823 cp_declarator
*target
,
3826 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3827 return cp_error_declarator
;
3829 if (code
== INDIRECT_REF
)
3830 if (class_type
== NULL_TREE
)
3831 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3833 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3834 target
, attributes
);
3835 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3836 return make_reference_declarator (cv_qualifiers
, target
,
3838 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3839 return make_reference_declarator (cv_qualifiers
, target
,
3844 /* Create a new C++ parser. */
3847 cp_parser_new (void)
3853 /* cp_lexer_new_main is called before doing GC allocation because
3854 cp_lexer_new_main might load a PCH file. */
3855 lexer
= cp_lexer_new_main ();
3857 /* Initialize the binops_by_token so that we can get the tree
3858 directly from the token. */
3859 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3860 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3862 parser
= ggc_cleared_alloc
<cp_parser
> ();
3863 parser
->lexer
= lexer
;
3864 parser
->context
= cp_parser_context_new (NULL
);
3866 /* For now, we always accept GNU extensions. */
3867 parser
->allow_gnu_extensions_p
= 1;
3869 /* The `>' token is a greater-than operator, not the end of a
3871 parser
->greater_than_is_operator_p
= true;
3873 parser
->default_arg_ok_p
= true;
3875 /* We are not parsing a constant-expression. */
3876 parser
->integral_constant_expression_p
= false;
3877 parser
->allow_non_integral_constant_expression_p
= false;
3878 parser
->non_integral_constant_expression_p
= false;
3880 /* Local variable names are not forbidden. */
3881 parser
->local_variables_forbidden_p
= false;
3883 /* We are not processing an `extern "C"' declaration. */
3884 parser
->in_unbraced_linkage_specification_p
= false;
3886 /* We are not processing a declarator. */
3887 parser
->in_declarator_p
= false;
3889 /* We are not processing a template-argument-list. */
3890 parser
->in_template_argument_list_p
= false;
3892 /* We are not in an iteration statement. */
3893 parser
->in_statement
= 0;
3895 /* We are not in a switch statement. */
3896 parser
->in_switch_statement_p
= false;
3898 /* We are not parsing a type-id inside an expression. */
3899 parser
->in_type_id_in_expr_p
= false;
3901 /* Declarations aren't implicitly extern "C". */
3902 parser
->implicit_extern_c
= false;
3904 /* String literals should be translated to the execution character set. */
3905 parser
->translate_strings_p
= true;
3907 /* We are not parsing a function body. */
3908 parser
->in_function_body
= false;
3910 /* We can correct until told otherwise. */
3911 parser
->colon_corrects_to_scope_p
= true;
3913 /* The unparsed function queue is empty. */
3914 push_unparsed_function_queues (parser
);
3916 /* There are no classes being defined. */
3917 parser
->num_classes_being_defined
= 0;
3919 /* No template parameters apply. */
3920 parser
->num_template_parameter_lists
= 0;
3922 /* Special parsing data structures. */
3923 parser
->omp_declare_simd
= NULL
;
3924 parser
->oacc_routine
= NULL
;
3926 /* Not declaring an implicit function template. */
3927 parser
->auto_is_implicit_function_template_parm_p
= false;
3928 parser
->fully_implicit_function_template_p
= false;
3929 parser
->implicit_template_parms
= 0;
3930 parser
->implicit_template_scope
= 0;
3932 /* Allow constrained-type-specifiers. */
3933 parser
->prevent_constrained_type_specifiers
= 0;
3935 /* We haven't yet seen an 'extern "C"'. */
3936 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3941 /* Create a cp_lexer structure which will emit the tokens in CACHE
3942 and push it onto the parser's lexer stack. This is used for delayed
3943 parsing of in-class method bodies and default arguments, and should
3944 not be confused with tentative parsing. */
3946 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3948 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3949 lexer
->next
= parser
->lexer
;
3950 parser
->lexer
= lexer
;
3952 /* Move the current source position to that of the first token in the
3954 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3957 /* Pop the top lexer off the parser stack. This is never used for the
3958 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3960 cp_parser_pop_lexer (cp_parser
*parser
)
3962 cp_lexer
*lexer
= parser
->lexer
;
3963 parser
->lexer
= lexer
->next
;
3964 cp_lexer_destroy (lexer
);
3966 /* Put the current source position back where it was before this
3967 lexer was pushed. */
3968 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3971 /* Lexical conventions [gram.lex] */
3973 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3977 cp_parser_identifier (cp_parser
* parser
)
3981 /* Look for the identifier. */
3982 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3983 /* Return the value. */
3985 return cp_expr (token
->u
.value
, token
->location
);
3987 return error_mark_node
;
3990 /* Parse a sequence of adjacent string constants. Returns a
3991 TREE_STRING representing the combined, nul-terminated string
3992 constant. If TRANSLATE is true, translate the string to the
3993 execution character set. If WIDE_OK is true, a wide string is
3996 C++98 [lex.string] says that if a narrow string literal token is
3997 adjacent to a wide string literal token, the behavior is undefined.
3998 However, C99 6.4.5p4 says that this results in a wide string literal.
3999 We follow C99 here, for consistency with the C front end.
4001 This code is largely lifted from lex_string() in c-lex.c.
4003 FUTURE: ObjC++ will need to handle @-strings here. */
4005 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4006 bool lookup_udlit
= true)
4010 struct obstack str_ob
;
4011 cpp_string str
, istr
, *strs
;
4013 enum cpp_ttype type
, curr_type
;
4014 int have_suffix_p
= 0;
4016 tree suffix_id
= NULL_TREE
;
4017 bool curr_tok_is_userdef_p
= false;
4019 tok
= cp_lexer_peek_token (parser
->lexer
);
4020 if (!cp_parser_is_string_literal (tok
))
4022 cp_parser_error (parser
, "expected string-literal");
4023 return error_mark_node
;
4026 location_t loc
= tok
->location
;
4028 if (cpp_userdef_string_p (tok
->type
))
4030 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4031 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4032 curr_tok_is_userdef_p
= true;
4036 string_tree
= tok
->u
.value
;
4037 curr_type
= tok
->type
;
4041 /* Try to avoid the overhead of creating and destroying an obstack
4042 for the common case of just one string. */
4043 if (!cp_parser_is_string_literal
4044 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4046 cp_lexer_consume_token (parser
->lexer
);
4048 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4049 str
.len
= TREE_STRING_LENGTH (string_tree
);
4052 if (curr_tok_is_userdef_p
)
4054 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4056 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4059 curr_type
= tok
->type
;
4065 location_t last_tok_loc
= tok
->location
;
4066 gcc_obstack_init (&str_ob
);
4071 cp_lexer_consume_token (parser
->lexer
);
4073 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4074 str
.len
= TREE_STRING_LENGTH (string_tree
);
4076 if (curr_tok_is_userdef_p
)
4078 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4079 if (have_suffix_p
== 0)
4081 suffix_id
= curr_suffix_id
;
4084 else if (have_suffix_p
== 1
4085 && curr_suffix_id
!= suffix_id
)
4087 error ("inconsistent user-defined literal suffixes"
4088 " %qD and %qD in string literal",
4089 suffix_id
, curr_suffix_id
);
4092 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4095 curr_type
= tok
->type
;
4097 if (type
!= curr_type
)
4099 if (type
== CPP_STRING
)
4101 else if (curr_type
!= CPP_STRING
)
4103 rich_location
rich_loc (line_table
, tok
->location
);
4104 rich_loc
.add_range (last_tok_loc
, false);
4105 error_at (&rich_loc
,
4106 "unsupported non-standard concatenation "
4107 "of string literals");
4111 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4113 last_tok_loc
= tok
->location
;
4115 tok
= cp_lexer_peek_token (parser
->lexer
);
4116 if (cpp_userdef_string_p (tok
->type
))
4118 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4119 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4120 curr_tok_is_userdef_p
= true;
4124 string_tree
= tok
->u
.value
;
4125 curr_type
= tok
->type
;
4126 curr_tok_is_userdef_p
= false;
4129 while (cp_parser_is_string_literal (tok
));
4131 /* A string literal built by concatenation has its caret=start at
4132 the start of the initial string, and its finish at the finish of
4133 the final string literal. */
4134 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4136 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4139 if (type
!= CPP_STRING
&& !wide_ok
)
4141 cp_parser_error (parser
, "a wide string is invalid in this context");
4145 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4146 (parse_in
, strs
, count
, &istr
, type
))
4148 value
= build_string (istr
.len
, (const char *)istr
.text
);
4149 free (CONST_CAST (unsigned char *, istr
.text
));
4155 case CPP_UTF8STRING
:
4156 TREE_TYPE (value
) = char_array_type_node
;
4159 TREE_TYPE (value
) = char16_array_type_node
;
4162 TREE_TYPE (value
) = char32_array_type_node
;
4165 TREE_TYPE (value
) = wchar_array_type_node
;
4169 value
= fix_string_type (value
);
4173 tree literal
= build_userdef_literal (suffix_id
, value
,
4174 OT_NONE
, NULL_TREE
);
4176 value
= cp_parser_userdef_string_literal (literal
);
4182 /* cpp_interpret_string has issued an error. */
4183 value
= error_mark_node
;
4186 obstack_free (&str_ob
, 0);
4188 return cp_expr (value
, loc
);
4191 /* Look up a literal operator with the name and the exact arguments. */
4194 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4197 decl
= lookup_name (name
);
4198 if (!decl
|| !is_overloaded_fn (decl
))
4199 return error_mark_node
;
4201 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4206 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4207 if (parmtypes
!= NULL_TREE
)
4209 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4210 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4212 tree tparm
= TREE_VALUE (parmtypes
);
4213 tree targ
= TREE_TYPE ((*args
)[ix
]);
4214 bool ptr
= TYPE_PTR_P (tparm
);
4215 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4216 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4218 || !same_type_p (TREE_TYPE (tparm
),
4223 && ix
== vec_safe_length (args
)
4224 /* May be this should be sufficient_parms_p instead,
4225 depending on how exactly should user-defined literals
4226 work in presence of default arguments on the literal
4227 operator parameters. */
4228 && parmtypes
== void_list_node
)
4233 return error_mark_node
;
4236 /* Parse a user-defined char constant. Returns a call to a user-defined
4237 literal operator taking the character as an argument. */
4240 cp_parser_userdef_char_literal (cp_parser
*parser
)
4242 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4243 tree literal
= token
->u
.value
;
4244 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4245 tree value
= USERDEF_LITERAL_VALUE (literal
);
4246 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4249 /* Build up a call to the user-defined operator */
4250 /* Lookup the name we got back from the id-expression. */
4251 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4252 vec_safe_push (args
, value
);
4253 decl
= lookup_literal_operator (name
, args
);
4254 if (!decl
|| decl
== error_mark_node
)
4256 error ("unable to find character literal operator %qD with %qT argument",
4257 name
, TREE_TYPE (value
));
4258 release_tree_vector (args
);
4259 return error_mark_node
;
4261 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4262 release_tree_vector (args
);
4266 /* A subroutine of cp_parser_userdef_numeric_literal to
4267 create a char... template parameter pack from a string node. */
4270 make_char_string_pack (tree value
)
4273 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4274 const char *str
= TREE_STRING_POINTER (value
);
4275 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4276 tree argvec
= make_tree_vec (1);
4278 /* Fill in CHARVEC with all of the parameters. */
4279 charvec
= make_tree_vec (len
);
4280 for (i
= 0; i
< len
; ++i
)
4281 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4283 /* Build the argument packs. */
4284 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4286 TREE_VEC_ELT (argvec
, 0) = argpack
;
4291 /* A subroutine of cp_parser_userdef_numeric_literal to
4292 create a char... template parameter pack from a string node. */
4295 make_string_pack (tree value
)
4298 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4299 const unsigned char *str
4300 = (const unsigned char *) TREE_STRING_POINTER (value
);
4301 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4302 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4303 tree argvec
= make_tree_vec (2);
4305 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4306 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4308 /* First template parm is character type. */
4309 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4311 /* Fill in CHARVEC with all of the parameters. */
4312 charvec
= make_tree_vec (len
);
4313 for (int i
= 0; i
< len
; ++i
)
4314 TREE_VEC_ELT (charvec
, i
)
4315 = double_int_to_tree (str_char_type_node
,
4316 double_int::from_buffer (str
+ i
* sz
, sz
));
4318 /* Build the argument packs. */
4319 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4321 TREE_VEC_ELT (argvec
, 1) = argpack
;
4326 /* Parse a user-defined numeric constant. returns a call to a user-defined
4327 literal operator. */
4330 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4332 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4333 tree literal
= token
->u
.value
;
4334 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4335 tree value
= USERDEF_LITERAL_VALUE (literal
);
4336 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4337 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4338 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4340 vec
<tree
, va_gc
> *args
;
4342 /* Look for a literal operator taking the exact type of numeric argument
4343 as the literal value. */
4344 args
= make_tree_vector ();
4345 vec_safe_push (args
, value
);
4346 decl
= lookup_literal_operator (name
, args
);
4347 if (decl
&& decl
!= error_mark_node
)
4349 result
= finish_call_expr (decl
, &args
, false, true,
4350 tf_warning_or_error
);
4352 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4354 warning_at (token
->location
, OPT_Woverflow
,
4355 "integer literal exceeds range of %qT type",
4356 long_long_unsigned_type_node
);
4361 warning_at (token
->location
, OPT_Woverflow
,
4362 "floating literal exceeds range of %qT type",
4363 long_double_type_node
);
4364 else if (overflow
< 0)
4365 warning_at (token
->location
, OPT_Woverflow
,
4366 "floating literal truncated to zero");
4369 release_tree_vector (args
);
4372 release_tree_vector (args
);
4374 /* If the numeric argument didn't work, look for a raw literal
4375 operator taking a const char* argument consisting of the number
4376 in string format. */
4377 args
= make_tree_vector ();
4378 vec_safe_push (args
, num_string
);
4379 decl
= lookup_literal_operator (name
, args
);
4380 if (decl
&& decl
!= error_mark_node
)
4382 result
= finish_call_expr (decl
, &args
, false, true,
4383 tf_warning_or_error
);
4384 release_tree_vector (args
);
4387 release_tree_vector (args
);
4389 /* If the raw literal didn't work, look for a non-type template
4390 function with parameter pack char.... Call the function with
4391 template parameter characters representing the number. */
4392 args
= make_tree_vector ();
4393 decl
= lookup_literal_operator (name
, args
);
4394 if (decl
&& decl
!= error_mark_node
)
4396 tree tmpl_args
= make_char_string_pack (num_string
);
4397 decl
= lookup_template_function (decl
, tmpl_args
);
4398 result
= finish_call_expr (decl
, &args
, false, true,
4399 tf_warning_or_error
);
4400 release_tree_vector (args
);
4404 release_tree_vector (args
);
4406 /* In C++14 the standard library defines complex number suffixes that
4407 conflict with GNU extensions. Prefer them if <complex> is #included. */
4408 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4409 bool i14
= (cxx_dialect
> cxx11
4410 && (id_equal (suffix_id
, "i")
4411 || id_equal (suffix_id
, "if")
4412 || id_equal (suffix_id
, "il")));
4413 diagnostic_t kind
= DK_ERROR
;
4418 tree cxlit
= lookup_qualified_name (std_node
,
4419 get_identifier ("complex_literals"),
4421 if (cxlit
== error_mark_node
)
4423 /* No <complex>, so pedwarn and use GNU semantics. */
4425 opt
= OPT_Wpedantic
;
4430 = emit_diagnostic (kind
, input_location
, opt
,
4431 "unable to find numeric literal operator %qD", name
);
4434 /* Don't inform either. */;
4437 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4438 "(from <complex>) to enable the C++14 user-defined literal "
4441 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4442 "GNU built-in suffix");
4445 inform (token
->location
, "use -fext-numeric-literals "
4446 "to enable more built-in suffixes");
4448 if (kind
== DK_ERROR
)
4449 value
= error_mark_node
;
4452 /* Use the built-in semantics. */
4454 if (id_equal (suffix_id
, "i"))
4456 if (TREE_CODE (value
) == INTEGER_CST
)
4457 type
= integer_type_node
;
4459 type
= double_type_node
;
4461 else if (id_equal (suffix_id
, "if"))
4462 type
= float_type_node
;
4463 else /* if (id_equal (suffix_id, "il")) */
4464 type
= long_double_type_node
;
4466 value
= build_complex (build_complex_type (type
),
4467 fold_convert (type
, integer_zero_node
),
4468 fold_convert (type
, value
));
4471 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4472 /* Avoid repeated diagnostics. */
4473 token
->u
.value
= value
;
4477 /* Parse a user-defined string constant. Returns a call to a user-defined
4478 literal operator taking a character pointer and the length of the string
4482 cp_parser_userdef_string_literal (tree literal
)
4484 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4485 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4486 tree value
= USERDEF_LITERAL_VALUE (literal
);
4487 int len
= TREE_STRING_LENGTH (value
)
4488 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4490 vec
<tree
, va_gc
> *args
;
4492 /* Build up a call to the user-defined operator. */
4493 /* Lookup the name we got back from the id-expression. */
4494 args
= make_tree_vector ();
4495 vec_safe_push (args
, value
);
4496 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4497 decl
= lookup_literal_operator (name
, args
);
4499 if (decl
&& decl
!= error_mark_node
)
4501 result
= finish_call_expr (decl
, &args
, false, true,
4502 tf_warning_or_error
);
4503 release_tree_vector (args
);
4506 release_tree_vector (args
);
4508 /* Look for a template function with typename parameter CharT
4509 and parameter pack CharT... Call the function with
4510 template parameter characters representing the string. */
4511 args
= make_tree_vector ();
4512 decl
= lookup_literal_operator (name
, args
);
4513 if (decl
&& decl
!= error_mark_node
)
4515 tree tmpl_args
= make_string_pack (value
);
4516 decl
= lookup_template_function (decl
, tmpl_args
);
4517 result
= finish_call_expr (decl
, &args
, false, true,
4518 tf_warning_or_error
);
4519 release_tree_vector (args
);
4522 release_tree_vector (args
);
4524 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4525 name
, TREE_TYPE (value
), size_type_node
);
4526 return error_mark_node
;
4530 /* Basic concepts [gram.basic] */
4532 /* Parse a translation-unit.
4535 declaration-seq [opt]
4537 Returns TRUE if all went well. */
4540 cp_parser_translation_unit (cp_parser
* parser
)
4542 /* The address of the first non-permanent object on the declarator
4544 static void *declarator_obstack_base
;
4548 /* Create the declarator obstack, if necessary. */
4549 if (!cp_error_declarator
)
4551 gcc_obstack_init (&declarator_obstack
);
4552 /* Create the error declarator. */
4553 cp_error_declarator
= make_declarator (cdk_error
);
4554 /* Create the empty parameter list. */
4555 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4557 /* Remember where the base of the declarator obstack lies. */
4558 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4561 cp_parser_declaration_seq_opt (parser
);
4563 /* If there are no tokens left then all went well. */
4564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4566 /* Get rid of the token array; we don't need it any more. */
4567 cp_lexer_destroy (parser
->lexer
);
4568 parser
->lexer
= NULL
;
4570 /* This file might have been a context that's implicitly extern
4571 "C". If so, pop the lang context. (Only relevant for PCH.) */
4572 if (parser
->implicit_extern_c
)
4574 pop_lang_context ();
4575 parser
->implicit_extern_c
= false;
4579 finish_translation_unit ();
4585 cp_parser_error (parser
, "expected declaration");
4589 /* Make sure the declarator obstack was fully cleaned up. */
4590 gcc_assert (obstack_next_free (&declarator_obstack
)
4591 == declarator_obstack_base
);
4593 /* All went well. */
4597 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4598 decltype context. */
4600 static inline tsubst_flags_t
4601 complain_flags (bool decltype_p
)
4603 tsubst_flags_t complain
= tf_warning_or_error
;
4605 complain
|= tf_decltype
;
4609 /* We're about to parse a collection of statements. If we're currently
4610 parsing tentatively, set up a firewall so that any nested
4611 cp_parser_commit_to_tentative_parse won't affect the current context. */
4613 static cp_token_position
4614 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4616 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4619 cp_parser_parse_tentatively (parser
);
4620 cp_parser_commit_to_topmost_tentative_parse (parser
);
4621 return cp_lexer_token_position (parser
->lexer
, false);
4624 /* We've finished parsing the collection of statements. Wrap up the
4625 firewall and replace the relevant tokens with the parsed form. */
4628 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4634 /* Finish the firewall level. */
4635 cp_parser_parse_definitely (parser
);
4636 /* And remember the result of the parse for when we try again. */
4637 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4638 token
->type
= CPP_PREPARSED_EXPR
;
4639 token
->u
.value
= expr
;
4640 token
->keyword
= RID_MAX
;
4641 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4644 /* Like the above functions, but let the user modify the tokens. Used by
4645 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4646 later parses, so it makes sense to localize the effects of
4647 cp_parser_commit_to_tentative_parse. */
4649 struct tentative_firewall
4654 tentative_firewall (cp_parser
*p
): parser(p
)
4656 /* If we're currently parsing tentatively, start a committed level as a
4657 firewall and then an inner tentative parse. */
4658 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4660 cp_parser_parse_tentatively (parser
);
4661 cp_parser_commit_to_topmost_tentative_parse (parser
);
4662 cp_parser_parse_tentatively (parser
);
4666 ~tentative_firewall()
4670 /* Finish the inner tentative parse and the firewall, propagating any
4671 uncommitted error state to the outer tentative parse. */
4672 bool err
= cp_parser_error_occurred (parser
);
4673 cp_parser_parse_definitely (parser
);
4674 cp_parser_parse_definitely (parser
);
4676 cp_parser_simulate_error (parser
);
4681 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4682 This class is for tracking such a matching pair of symbols.
4683 In particular, it tracks the location of the first token,
4684 so that if the second token is missing, we can highlight the
4685 location of the first token when notifying the user about the
4688 template <typename traits_t
>
4692 /* token_pair's ctor. */
4693 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4695 /* If the next token is the opening symbol for this pair, consume it and
4697 Otherwise, issue an error and return false.
4698 In either case, record the location of the opening token. */
4700 bool require_open (cp_parser
*parser
)
4702 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4703 return cp_parser_require (parser
, traits_t::open_token_type
,
4704 traits_t::required_token_open
);
4707 /* Consume the next token from PARSER, recording its location as
4708 that of the opening token within the pair. */
4710 cp_token
* consume_open (cp_parser
*parser
)
4712 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4713 gcc_assert (tok
->type
== traits_t::open_token_type
);
4714 m_open_loc
= tok
->location
;
4718 /* If the next token is the closing symbol for this pair, consume it
4720 Otherwise, issue an error, highlighting the location of the
4721 corresponding opening token, and return NULL. */
4723 cp_token
*require_close (cp_parser
*parser
) const
4725 return cp_parser_require (parser
, traits_t::close_token_type
,
4726 traits_t::required_token_close
,
4731 location_t m_open_loc
;
4734 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4736 struct matching_paren_traits
4738 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4739 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4740 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4741 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4744 /* "matching_parens" is a token_pair<T> class for tracking matching
4745 pairs of parentheses. */
4747 typedef token_pair
<matching_paren_traits
> matching_parens
;
4749 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4751 struct matching_brace_traits
4753 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4754 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4755 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4756 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4759 /* "matching_braces" is a token_pair<T> class for tracking matching
4762 typedef token_pair
<matching_brace_traits
> matching_braces
;
4765 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4766 enclosing parentheses. */
4769 cp_parser_statement_expr (cp_parser
*parser
)
4771 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4773 /* Consume the '('. */
4774 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4775 matching_parens parens
;
4776 parens
.consume_open (parser
);
4777 /* Start the statement-expression. */
4778 tree expr
= begin_stmt_expr ();
4779 /* Parse the compound-statement. */
4780 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4782 expr
= finish_stmt_expr (expr
, false);
4783 /* Consume the ')'. */
4784 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4785 if (!parens
.require_close (parser
))
4786 cp_parser_skip_to_end_of_statement (parser
);
4788 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4789 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4790 return cp_expr (expr
, combined_loc
);
4793 /* Expressions [gram.expr] */
4795 /* Parse a fold-operator.
4798 - * / % ^ & | = < > << >>
4799 = -= *= /= %= ^= &= |= <<= >>=
4800 == != <= >= && || , .* ->*
4802 This returns the tree code corresponding to the matched operator
4803 as an int. When the current token matches a compound assignment
4804 opertor, the resulting tree code is the negative value of the
4805 non-assignment operator. */
4808 cp_parser_fold_operator (cp_token
*token
)
4810 switch (token
->type
)
4812 case CPP_PLUS
: return PLUS_EXPR
;
4813 case CPP_MINUS
: return MINUS_EXPR
;
4814 case CPP_MULT
: return MULT_EXPR
;
4815 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4816 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4817 case CPP_XOR
: return BIT_XOR_EXPR
;
4818 case CPP_AND
: return BIT_AND_EXPR
;
4819 case CPP_OR
: return BIT_IOR_EXPR
;
4820 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4821 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4823 case CPP_EQ
: return -NOP_EXPR
;
4824 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4825 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4826 case CPP_MULT_EQ
: return -MULT_EXPR
;
4827 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4828 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4829 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4830 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4831 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4832 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4833 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4835 case CPP_EQ_EQ
: return EQ_EXPR
;
4836 case CPP_NOT_EQ
: return NE_EXPR
;
4837 case CPP_LESS
: return LT_EXPR
;
4838 case CPP_GREATER
: return GT_EXPR
;
4839 case CPP_LESS_EQ
: return LE_EXPR
;
4840 case CPP_GREATER_EQ
: return GE_EXPR
;
4842 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4843 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4845 case CPP_COMMA
: return COMPOUND_EXPR
;
4847 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4848 case CPP_DEREF_STAR
: return MEMBER_REF
;
4850 default: return ERROR_MARK
;
4854 /* Returns true if CODE indicates a binary expression, which is not allowed in
4855 the LHS of a fold-expression. More codes will need to be added to use this
4856 function in other contexts. */
4859 is_binary_op (tree_code code
)
4864 case POINTER_PLUS_EXPR
:
4867 case TRUNC_DIV_EXPR
:
4868 case TRUNC_MOD_EXPR
:
4884 case TRUTH_ANDIF_EXPR
:
4885 case TRUTH_ORIF_EXPR
:
4898 /* If the next token is a suitable fold operator, consume it and return as
4899 the function above. */
4902 cp_parser_fold_operator (cp_parser
*parser
)
4904 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4905 int code
= cp_parser_fold_operator (token
);
4906 if (code
!= ERROR_MARK
)
4907 cp_lexer_consume_token (parser
->lexer
);
4911 /* Parse a fold-expression.
4914 ( ... folding-operator cast-expression)
4915 ( cast-expression folding-operator ... )
4916 ( cast-expression folding operator ... folding-operator cast-expression)
4918 Note that the '(' and ')' are matched in primary expression. */
4921 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4926 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4928 cp_lexer_consume_token (parser
->lexer
);
4929 int op
= cp_parser_fold_operator (parser
);
4930 if (op
== ERROR_MARK
)
4932 cp_parser_error (parser
, "expected binary operator");
4933 return error_mark_node
;
4936 tree expr
= cp_parser_cast_expression (parser
, false, false,
4938 if (expr
== error_mark_node
)
4939 return error_mark_node
;
4940 return finish_left_unary_fold_expr (expr
, op
);
4943 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4944 int op
= cp_parser_fold_operator (parser
);
4945 if (op
== ERROR_MARK
)
4947 cp_parser_error (parser
, "expected binary operator");
4948 return error_mark_node
;
4951 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4953 cp_parser_error (parser
, "expected ...");
4954 return error_mark_node
;
4956 cp_lexer_consume_token (parser
->lexer
);
4958 /* The operands of a fold-expression are cast-expressions, so binary or
4959 conditional expressions are not allowed. We check this here to avoid
4960 tentative parsing. */
4961 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4962 /* OK, the expression was parenthesized. */;
4963 else if (is_binary_op (TREE_CODE (expr1
)))
4964 error_at (location_of (expr1
),
4965 "binary expression in operand of fold-expression");
4966 else if (TREE_CODE (expr1
) == COND_EXPR
4967 || (REFERENCE_REF_P (expr1
)
4968 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
4969 error_at (location_of (expr1
),
4970 "conditional expression in operand of fold-expression");
4973 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4974 return finish_right_unary_fold_expr (expr1
, op
);
4976 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4978 cp_parser_error (parser
, "mismatched operator in fold-expression");
4979 return error_mark_node
;
4981 cp_lexer_consume_token (parser
->lexer
);
4983 // Binary left or right fold.
4984 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4985 if (expr2
== error_mark_node
)
4986 return error_mark_node
;
4987 return finish_binary_fold_expr (expr1
, expr2
, op
);
4990 /* Parse a primary-expression.
4997 lambda-expression (C++11)
5002 ( compound-statement )
5003 __builtin_va_arg ( assignment-expression , type-id )
5004 __builtin_offsetof ( type-id , offsetof-expression )
5007 __has_nothrow_assign ( type-id )
5008 __has_nothrow_constructor ( type-id )
5009 __has_nothrow_copy ( type-id )
5010 __has_trivial_assign ( type-id )
5011 __has_trivial_constructor ( type-id )
5012 __has_trivial_copy ( type-id )
5013 __has_trivial_destructor ( type-id )
5014 __has_virtual_destructor ( type-id )
5015 __is_abstract ( type-id )
5016 __is_base_of ( type-id , type-id )
5017 __is_class ( type-id )
5018 __is_empty ( type-id )
5019 __is_enum ( type-id )
5020 __is_final ( type-id )
5021 __is_literal_type ( type-id )
5022 __is_pod ( type-id )
5023 __is_polymorphic ( type-id )
5024 __is_std_layout ( type-id )
5025 __is_trivial ( type-id )
5026 __is_union ( type-id )
5028 Objective-C++ Extension:
5036 ADDRESS_P is true iff this expression was immediately preceded by
5037 "&" and therefore might denote a pointer-to-member. CAST_P is true
5038 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5039 true iff this expression is a template argument.
5041 Returns a representation of the expression. Upon return, *IDK
5042 indicates what kind of id-expression (if any) was present. */
5045 cp_parser_primary_expression (cp_parser
*parser
,
5048 bool template_arg_p
,
5052 cp_token
*token
= NULL
;
5054 /* Assume the primary expression is not an id-expression. */
5055 *idk
= CP_ID_KIND_NONE
;
5057 /* Peek at the next token. */
5058 token
= cp_lexer_peek_token (parser
->lexer
);
5059 switch ((int) token
->type
)
5068 user-defined-literal */
5075 case CPP_PREPARSED_EXPR
:
5076 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5077 return cp_parser_userdef_numeric_literal (parser
);
5078 token
= cp_lexer_consume_token (parser
->lexer
);
5079 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5081 error_at (token
->location
,
5082 "fixed-point types not supported in C++");
5083 return error_mark_node
;
5085 /* Floating-point literals are only allowed in an integral
5086 constant expression if they are cast to an integral or
5087 enumeration type. */
5088 if (TREE_CODE (token
->u
.value
) == REAL_CST
5089 && parser
->integral_constant_expression_p
5092 /* CAST_P will be set even in invalid code like "int(2.7 +
5093 ...)". Therefore, we have to check that the next token
5094 is sure to end the cast. */
5097 cp_token
*next_token
;
5099 next_token
= cp_lexer_peek_token (parser
->lexer
);
5100 if (/* The comma at the end of an
5101 enumerator-definition. */
5102 next_token
->type
!= CPP_COMMA
5103 /* The curly brace at the end of an enum-specifier. */
5104 && next_token
->type
!= CPP_CLOSE_BRACE
5105 /* The end of a statement. */
5106 && next_token
->type
!= CPP_SEMICOLON
5107 /* The end of the cast-expression. */
5108 && next_token
->type
!= CPP_CLOSE_PAREN
5109 /* The end of an array bound. */
5110 && next_token
->type
!= CPP_CLOSE_SQUARE
5111 /* The closing ">" in a template-argument-list. */
5112 && (next_token
->type
!= CPP_GREATER
5113 || parser
->greater_than_is_operator_p
)
5114 /* C++0x only: A ">>" treated like two ">" tokens,
5115 in a template-argument-list. */
5116 && (next_token
->type
!= CPP_RSHIFT
5117 || (cxx_dialect
== cxx98
)
5118 || parser
->greater_than_is_operator_p
))
5122 /* If we are within a cast, then the constraint that the
5123 cast is to an integral or enumeration type will be
5124 checked at that point. If we are not within a cast, then
5125 this code is invalid. */
5127 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5129 return cp_expr (token
->u
.value
, token
->location
);
5131 case CPP_CHAR_USERDEF
:
5132 case CPP_CHAR16_USERDEF
:
5133 case CPP_CHAR32_USERDEF
:
5134 case CPP_WCHAR_USERDEF
:
5135 case CPP_UTF8CHAR_USERDEF
:
5136 return cp_parser_userdef_char_literal (parser
);
5142 case CPP_UTF8STRING
:
5143 case CPP_STRING_USERDEF
:
5144 case CPP_STRING16_USERDEF
:
5145 case CPP_STRING32_USERDEF
:
5146 case CPP_WSTRING_USERDEF
:
5147 case CPP_UTF8STRING_USERDEF
:
5148 /* ??? Should wide strings be allowed when parser->translate_strings_p
5149 is false (i.e. in attributes)? If not, we can kill the third
5150 argument to cp_parser_string_literal. */
5151 return cp_parser_string_literal (parser
,
5152 parser
->translate_strings_p
,
5155 case CPP_OPEN_PAREN
:
5156 /* If we see `( { ' then we are looking at the beginning of
5157 a GNU statement-expression. */
5158 if (cp_parser_allow_gnu_extensions_p (parser
)
5159 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5161 /* Statement-expressions are not allowed by the standard. */
5162 pedwarn (token
->location
, OPT_Wpedantic
,
5163 "ISO C++ forbids braced-groups within expressions");
5165 /* And they're not allowed outside of a function-body; you
5166 cannot, for example, write:
5168 int i = ({ int j = 3; j + 1; });
5170 at class or namespace scope. */
5171 if (!parser
->in_function_body
5172 || parser
->in_template_argument_list_p
)
5174 error_at (token
->location
,
5175 "statement-expressions are not allowed outside "
5176 "functions nor in template-argument lists");
5177 cp_parser_skip_to_end_of_block_or_statement (parser
);
5178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5179 cp_lexer_consume_token (parser
->lexer
);
5180 return error_mark_node
;
5183 return cp_parser_statement_expr (parser
);
5185 /* Otherwise it's a normal parenthesized expression. */
5188 bool saved_greater_than_is_operator_p
;
5190 location_t open_paren_loc
= token
->location
;
5192 /* Consume the `('. */
5193 matching_parens parens
;
5194 parens
.consume_open (parser
);
5195 /* Within a parenthesized expression, a `>' token is always
5196 the greater-than operator. */
5197 saved_greater_than_is_operator_p
5198 = parser
->greater_than_is_operator_p
;
5199 parser
->greater_than_is_operator_p
= true;
5201 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5202 /* Left fold expression. */
5205 /* Parse the parenthesized expression. */
5206 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5208 token
= cp_lexer_peek_token (parser
->lexer
);
5209 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5211 expr
= cp_parser_fold_expression (parser
, expr
);
5212 if (expr
!= error_mark_node
5213 && cxx_dialect
< cxx17
5214 && !in_system_header_at (input_location
))
5215 pedwarn (input_location
, 0, "fold-expressions only available "
5216 "with -std=c++17 or -std=gnu++17");
5219 /* Let the front end know that this expression was
5220 enclosed in parentheses. This matters in case, for
5221 example, the expression is of the form `A::B', since
5222 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5224 expr
= finish_parenthesized_expr (expr
);
5226 /* DR 705: Wrapping an unqualified name in parentheses
5227 suppresses arg-dependent lookup. We want to pass back
5228 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5229 (c++/37862), but none of the others. */
5230 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5231 *idk
= CP_ID_KIND_NONE
;
5233 /* The `>' token might be the end of a template-id or
5234 template-parameter-list now. */
5235 parser
->greater_than_is_operator_p
5236 = saved_greater_than_is_operator_p
;
5238 /* Consume the `)'. */
5239 token
= cp_lexer_peek_token (parser
->lexer
);
5240 location_t close_paren_loc
= token
->location
;
5241 expr
.set_range (open_paren_loc
, close_paren_loc
);
5242 if (!parens
.require_close (parser
)
5243 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5244 cp_parser_skip_to_end_of_statement (parser
);
5249 case CPP_OPEN_SQUARE
:
5251 if (c_dialect_objc ())
5253 /* We might have an Objective-C++ message. */
5254 cp_parser_parse_tentatively (parser
);
5255 tree msg
= cp_parser_objc_message_expression (parser
);
5256 /* If that works out, we're done ... */
5257 if (cp_parser_parse_definitely (parser
))
5259 /* ... else, fall though to see if it's a lambda. */
5261 cp_expr lam
= cp_parser_lambda_expression (parser
);
5262 /* Don't warn about a failed tentative parse. */
5263 if (cp_parser_error_occurred (parser
))
5264 return error_mark_node
;
5265 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5269 case CPP_OBJC_STRING
:
5270 if (c_dialect_objc ())
5271 /* We have an Objective-C++ string literal. */
5272 return cp_parser_objc_expression (parser
);
5273 cp_parser_error (parser
, "expected primary-expression");
5274 return error_mark_node
;
5277 switch (token
->keyword
)
5279 /* These two are the boolean literals. */
5281 cp_lexer_consume_token (parser
->lexer
);
5282 return cp_expr (boolean_true_node
, token
->location
);
5284 cp_lexer_consume_token (parser
->lexer
);
5285 return cp_expr (boolean_false_node
, token
->location
);
5287 /* The `__null' literal. */
5289 cp_lexer_consume_token (parser
->lexer
);
5290 return cp_expr (null_node
, token
->location
);
5292 /* The `nullptr' literal. */
5294 cp_lexer_consume_token (parser
->lexer
);
5295 return cp_expr (nullptr_node
, token
->location
);
5297 /* Recognize the `this' keyword. */
5299 cp_lexer_consume_token (parser
->lexer
);
5300 if (parser
->local_variables_forbidden_p
)
5302 error_at (token
->location
,
5303 "%<this%> may not be used in this context");
5304 return error_mark_node
;
5306 /* Pointers cannot appear in constant-expressions. */
5307 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5308 return error_mark_node
;
5309 return cp_expr (finish_this_expr (), token
->location
);
5311 /* The `operator' keyword can be the beginning of an
5316 case RID_FUNCTION_NAME
:
5317 case RID_PRETTY_FUNCTION_NAME
:
5318 case RID_C99_FUNCTION_NAME
:
5320 non_integral_constant name
;
5322 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5323 __func__ are the names of variables -- but they are
5324 treated specially. Therefore, they are handled here,
5325 rather than relying on the generic id-expression logic
5326 below. Grammatically, these names are id-expressions.
5328 Consume the token. */
5329 token
= cp_lexer_consume_token (parser
->lexer
);
5331 switch (token
->keyword
)
5333 case RID_FUNCTION_NAME
:
5334 name
= NIC_FUNC_NAME
;
5336 case RID_PRETTY_FUNCTION_NAME
:
5337 name
= NIC_PRETTY_FUNC
;
5339 case RID_C99_FUNCTION_NAME
:
5340 name
= NIC_C99_FUNC
;
5346 if (cp_parser_non_integral_constant_expression (parser
, name
))
5347 return error_mark_node
;
5349 /* Look up the name. */
5350 return finish_fname (token
->u
.value
);
5357 source_location type_location
;
5358 location_t start_loc
5359 = cp_lexer_peek_token (parser
->lexer
)->location
;
5360 /* The `__builtin_va_arg' construct is used to handle
5361 `va_arg'. Consume the `__builtin_va_arg' token. */
5362 cp_lexer_consume_token (parser
->lexer
);
5363 /* Look for the opening `('. */
5364 matching_parens parens
;
5365 parens
.require_open (parser
);
5366 /* Now, parse the assignment-expression. */
5367 expression
= cp_parser_assignment_expression (parser
);
5368 /* Look for the `,'. */
5369 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5370 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5371 /* Parse the type-id. */
5373 type_id_in_expr_sentinel
s (parser
);
5374 type
= cp_parser_type_id (parser
);
5376 /* Look for the closing `)'. */
5377 location_t finish_loc
5378 = cp_lexer_peek_token (parser
->lexer
)->location
;
5379 parens
.require_close (parser
);
5380 /* Using `va_arg' in a constant-expression is not
5382 if (cp_parser_non_integral_constant_expression (parser
,
5384 return error_mark_node
;
5385 /* Construct a location of the form:
5386 __builtin_va_arg (v, int)
5387 ~~~~~~~~~~~~~~~~~~~~~^~~~
5388 with the caret at the type, ranging from the start of the
5389 "__builtin_va_arg" token to the close paren. */
5390 location_t combined_loc
5391 = make_location (type_location
, start_loc
, finish_loc
);
5392 return build_x_va_arg (combined_loc
, expression
, type
);
5396 return cp_parser_builtin_offsetof (parser
);
5398 case RID_HAS_NOTHROW_ASSIGN
:
5399 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5400 case RID_HAS_NOTHROW_COPY
:
5401 case RID_HAS_TRIVIAL_ASSIGN
:
5402 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5403 case RID_HAS_TRIVIAL_COPY
:
5404 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5405 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5406 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5407 case RID_IS_ABSTRACT
:
5408 case RID_IS_AGGREGATE
:
5409 case RID_IS_BASE_OF
:
5414 case RID_IS_LITERAL_TYPE
:
5416 case RID_IS_POLYMORPHIC
:
5417 case RID_IS_SAME_AS
:
5418 case RID_IS_STD_LAYOUT
:
5419 case RID_IS_TRIVIAL
:
5420 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5421 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5422 case RID_IS_TRIVIALLY_COPYABLE
:
5424 case RID_IS_ASSIGNABLE
:
5425 case RID_IS_CONSTRUCTIBLE
:
5426 return cp_parser_trait_expr (parser
, token
->keyword
);
5430 return cp_parser_requires_expression (parser
);
5432 /* Objective-C++ expressions. */
5434 case RID_AT_PROTOCOL
:
5435 case RID_AT_SELECTOR
:
5436 return cp_parser_objc_expression (parser
);
5439 if (parser
->in_function_body
5440 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5443 error_at (token
->location
,
5444 "a template declaration cannot appear at block scope");
5445 cp_parser_skip_to_end_of_block_or_statement (parser
);
5446 return error_mark_node
;
5450 cp_parser_error (parser
, "expected primary-expression");
5451 return error_mark_node
;
5454 /* An id-expression can start with either an identifier, a
5455 `::' as the beginning of a qualified-id, or the "operator"
5459 case CPP_TEMPLATE_ID
:
5460 case CPP_NESTED_NAME_SPECIFIER
:
5463 cp_expr id_expression
;
5465 const char *error_msg
;
5468 cp_token
*id_expr_token
;
5470 /* Parse the id-expression. */
5472 = cp_parser_id_expression (parser
,
5473 /*template_keyword_p=*/false,
5474 /*check_dependency_p=*/true,
5476 /*declarator_p=*/false,
5477 /*optional_p=*/false);
5478 if (id_expression
== error_mark_node
)
5479 return error_mark_node
;
5480 id_expr_token
= token
;
5481 token
= cp_lexer_peek_token (parser
->lexer
);
5482 done
= (token
->type
!= CPP_OPEN_SQUARE
5483 && token
->type
!= CPP_OPEN_PAREN
5484 && token
->type
!= CPP_DOT
5485 && token
->type
!= CPP_DEREF
5486 && token
->type
!= CPP_PLUS_PLUS
5487 && token
->type
!= CPP_MINUS_MINUS
);
5488 /* If we have a template-id, then no further lookup is
5489 required. If the template-id was for a template-class, we
5490 will sometimes have a TYPE_DECL at this point. */
5491 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5492 || TREE_CODE (id_expression
) == TYPE_DECL
)
5493 decl
= id_expression
;
5494 /* Look up the name. */
5497 tree ambiguous_decls
;
5499 /* If we already know that this lookup is ambiguous, then
5500 we've already issued an error message; there's no reason
5502 if (id_expr_token
->type
== CPP_NAME
5503 && id_expr_token
->error_reported
)
5505 cp_parser_simulate_error (parser
);
5506 return error_mark_node
;
5509 decl
= cp_parser_lookup_name (parser
, id_expression
,
5512 /*is_namespace=*/false,
5513 /*check_dependency=*/true,
5515 id_expr_token
->location
);
5516 /* If the lookup was ambiguous, an error will already have
5518 if (ambiguous_decls
)
5519 return error_mark_node
;
5521 /* In Objective-C++, we may have an Objective-C 2.0
5522 dot-syntax for classes here. */
5523 if (c_dialect_objc ()
5524 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5525 && TREE_CODE (decl
) == TYPE_DECL
5526 && objc_is_class_name (decl
))
5529 cp_lexer_consume_token (parser
->lexer
);
5530 component
= cp_parser_identifier (parser
);
5531 if (component
== error_mark_node
)
5532 return error_mark_node
;
5534 tree result
= objc_build_class_component_ref (id_expression
,
5536 /* Build a location of the form:
5539 with caret at the start of the component name (at
5540 input_location), ranging from the start of the id_expression
5541 to the end of the component name. */
5542 location_t combined_loc
5543 = make_location (input_location
, id_expression
.get_start (),
5544 get_finish (input_location
));
5545 protected_set_expr_location (result
, combined_loc
);
5549 /* In Objective-C++, an instance variable (ivar) may be preferred
5550 to whatever cp_parser_lookup_name() found.
5551 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5552 rest of c-family, we have to do a little extra work to preserve
5553 any location information in cp_expr "decl". Given that
5554 objc_lookup_ivar is implemented in "c-family" and "objc", we
5555 have a trip through the pure "tree" type, rather than cp_expr.
5556 Naively copying it back to "decl" would implicitly give the
5557 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5558 store an EXPR_LOCATION. Hence we only update "decl" (and
5559 hence its location_t) if we get back a different tree node. */
5560 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5562 if (decl_tree
!= decl
.get_value ())
5563 decl
= cp_expr (decl_tree
);
5565 /* If name lookup gives us a SCOPE_REF, then the
5566 qualifying scope was dependent. */
5567 if (TREE_CODE (decl
) == SCOPE_REF
)
5569 /* At this point, we do not know if DECL is a valid
5570 integral constant expression. We assume that it is
5571 in fact such an expression, so that code like:
5573 template <int N> struct A {
5577 is accepted. At template-instantiation time, we
5578 will check that B<N>::i is actually a constant. */
5581 /* Check to see if DECL is a local variable in a context
5582 where that is forbidden. */
5583 if (parser
->local_variables_forbidden_p
5584 && local_variable_p (decl
))
5586 /* It might be that we only found DECL because we are
5587 trying to be generous with pre-ISO scoping rules.
5588 For example, consider:
5592 for (int i = 0; i < 10; ++i) {}
5593 extern void f(int j = i);
5596 Here, name look up will originally find the out
5597 of scope `i'. We need to issue a warning message,
5598 but then use the global `i'. */
5599 decl
= check_for_out_of_scope_variable (decl
);
5600 if (local_variable_p (decl
))
5602 error_at (id_expr_token
->location
,
5603 "local variable %qD may not appear in this context",
5605 return error_mark_node
;
5610 decl
= (finish_id_expression
5611 (id_expression
, decl
, parser
->scope
,
5613 parser
->integral_constant_expression_p
,
5614 parser
->allow_non_integral_constant_expression_p
,
5615 &parser
->non_integral_constant_expression_p
,
5616 template_p
, done
, address_p
,
5619 id_expression
.get_location ()));
5621 cp_parser_error (parser
, error_msg
);
5622 decl
.set_location (id_expr_token
->location
);
5626 /* Anything else is an error. */
5628 cp_parser_error (parser
, "expected primary-expression");
5629 return error_mark_node
;
5633 static inline cp_expr
5634 cp_parser_primary_expression (cp_parser
*parser
,
5637 bool template_arg_p
,
5640 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5641 /*decltype*/false, idk
);
5644 /* Parse an id-expression.
5651 :: [opt] nested-name-specifier template [opt] unqualified-id
5653 :: operator-function-id
5656 Return a representation of the unqualified portion of the
5657 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5658 a `::' or nested-name-specifier.
5660 Often, if the id-expression was a qualified-id, the caller will
5661 want to make a SCOPE_REF to represent the qualified-id. This
5662 function does not do this in order to avoid wastefully creating
5663 SCOPE_REFs when they are not required.
5665 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5668 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5669 uninstantiated templates.
5671 If *TEMPLATE_P is non-NULL, it is set to true iff the
5672 `template' keyword is used to explicitly indicate that the entity
5673 named is a template.
5675 If DECLARATOR_P is true, the id-expression is appearing as part of
5676 a declarator, rather than as part of an expression. */
5679 cp_parser_id_expression (cp_parser
*parser
,
5680 bool template_keyword_p
,
5681 bool check_dependency_p
,
5686 bool global_scope_p
;
5687 bool nested_name_specifier_p
;
5689 /* Assume the `template' keyword was not used. */
5691 *template_p
= template_keyword_p
;
5693 /* Look for the optional `::' operator. */
5695 = (!template_keyword_p
5696 && (cp_parser_global_scope_opt (parser
,
5697 /*current_scope_valid_p=*/false)
5700 /* Look for the optional nested-name-specifier. */
5701 nested_name_specifier_p
5702 = (cp_parser_nested_name_specifier_opt (parser
,
5703 /*typename_keyword_p=*/false,
5710 /* If there is a nested-name-specifier, then we are looking at
5711 the first qualified-id production. */
5712 if (nested_name_specifier_p
)
5715 tree saved_object_scope
;
5716 tree saved_qualifying_scope
;
5717 cp_expr unqualified_id
;
5720 /* See if the next token is the `template' keyword. */
5722 template_p
= &is_template
;
5723 *template_p
= cp_parser_optional_template_keyword (parser
);
5724 /* Name lookup we do during the processing of the
5725 unqualified-id might obliterate SCOPE. */
5726 saved_scope
= parser
->scope
;
5727 saved_object_scope
= parser
->object_scope
;
5728 saved_qualifying_scope
= parser
->qualifying_scope
;
5729 /* Process the final unqualified-id. */
5730 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5733 /*optional_p=*/false);
5734 /* Restore the SAVED_SCOPE for our caller. */
5735 parser
->scope
= saved_scope
;
5736 parser
->object_scope
= saved_object_scope
;
5737 parser
->qualifying_scope
= saved_qualifying_scope
;
5739 return unqualified_id
;
5741 /* Otherwise, if we are in global scope, then we are looking at one
5742 of the other qualified-id productions. */
5743 else if (global_scope_p
)
5748 /* Peek at the next token. */
5749 token
= cp_lexer_peek_token (parser
->lexer
);
5751 /* If it's an identifier, and the next token is not a "<", then
5752 we can avoid the template-id case. This is an optimization
5753 for this common case. */
5754 if (token
->type
== CPP_NAME
5755 && !cp_parser_nth_token_starts_template_argument_list_p
5757 return cp_parser_identifier (parser
);
5759 cp_parser_parse_tentatively (parser
);
5760 /* Try a template-id. */
5761 id
= cp_parser_template_id (parser
,
5762 /*template_keyword_p=*/false,
5763 /*check_dependency_p=*/true,
5766 /* If that worked, we're done. */
5767 if (cp_parser_parse_definitely (parser
))
5770 /* Peek at the next token. (Changes in the token buffer may
5771 have invalidated the pointer obtained above.) */
5772 token
= cp_lexer_peek_token (parser
->lexer
);
5774 switch (token
->type
)
5777 return cp_parser_identifier (parser
);
5780 if (token
->keyword
== RID_OPERATOR
)
5781 return cp_parser_operator_function_id (parser
);
5785 cp_parser_error (parser
, "expected id-expression");
5786 return error_mark_node
;
5790 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5791 /*check_dependency_p=*/true,
5796 /* Parse an unqualified-id.
5800 operator-function-id
5801 conversion-function-id
5805 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5806 keyword, in a construct like `A::template ...'.
5808 Returns a representation of unqualified-id. For the `identifier'
5809 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5810 production a BIT_NOT_EXPR is returned; the operand of the
5811 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5812 other productions, see the documentation accompanying the
5813 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5814 names are looked up in uninstantiated templates. If DECLARATOR_P
5815 is true, the unqualified-id is appearing as part of a declarator,
5816 rather than as part of an expression. */
5819 cp_parser_unqualified_id (cp_parser
* parser
,
5820 bool template_keyword_p
,
5821 bool check_dependency_p
,
5827 /* Peek at the next token. */
5828 token
= cp_lexer_peek_token (parser
->lexer
);
5830 switch ((int) token
->type
)
5836 /* We don't know yet whether or not this will be a
5838 cp_parser_parse_tentatively (parser
);
5839 /* Try a template-id. */
5840 id
= cp_parser_template_id (parser
, template_keyword_p
,
5844 /* If it worked, we're done. */
5845 if (cp_parser_parse_definitely (parser
))
5847 /* Otherwise, it's an ordinary identifier. */
5848 return cp_parser_identifier (parser
);
5851 case CPP_TEMPLATE_ID
:
5852 return cp_parser_template_id (parser
, template_keyword_p
,
5860 tree qualifying_scope
;
5865 /* Consume the `~' token. */
5866 cp_lexer_consume_token (parser
->lexer
);
5867 /* Parse the class-name. The standard, as written, seems to
5870 template <typename T> struct S { ~S (); };
5871 template <typename T> S<T>::~S() {}
5873 is invalid, since `~' must be followed by a class-name, but
5874 `S<T>' is dependent, and so not known to be a class.
5875 That's not right; we need to look in uninstantiated
5876 templates. A further complication arises from:
5878 template <typename T> void f(T t) {
5882 Here, it is not possible to look up `T' in the scope of `T'
5883 itself. We must look in both the current scope, and the
5884 scope of the containing complete expression.
5886 Yet another issue is:
5895 The standard does not seem to say that the `S' in `~S'
5896 should refer to the type `S' and not the data member
5899 /* DR 244 says that we look up the name after the "~" in the
5900 same scope as we looked up the qualifying name. That idea
5901 isn't fully worked out; it's more complicated than that. */
5902 scope
= parser
->scope
;
5903 object_scope
= parser
->object_scope
;
5904 qualifying_scope
= parser
->qualifying_scope
;
5906 /* Check for invalid scopes. */
5907 if (scope
== error_mark_node
)
5909 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5910 cp_lexer_consume_token (parser
->lexer
);
5911 return error_mark_node
;
5913 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5915 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5916 error_at (token
->location
,
5917 "scope %qT before %<~%> is not a class-name",
5919 cp_parser_simulate_error (parser
);
5920 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5921 cp_lexer_consume_token (parser
->lexer
);
5922 return error_mark_node
;
5924 gcc_assert (!scope
|| TYPE_P (scope
));
5926 /* If the name is of the form "X::~X" it's OK even if X is a
5928 token
= cp_lexer_peek_token (parser
->lexer
);
5930 && token
->type
== CPP_NAME
5931 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5933 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5934 || (CLASS_TYPE_P (scope
)
5935 && constructor_name_p (token
->u
.value
, scope
))))
5937 cp_lexer_consume_token (parser
->lexer
);
5938 return build_nt (BIT_NOT_EXPR
, scope
);
5941 /* ~auto means the destructor of whatever the object is. */
5942 if (cp_parser_is_keyword (token
, RID_AUTO
))
5944 if (cxx_dialect
< cxx14
)
5945 pedwarn (input_location
, 0,
5946 "%<~auto%> only available with "
5947 "-std=c++14 or -std=gnu++14");
5948 cp_lexer_consume_token (parser
->lexer
);
5949 return build_nt (BIT_NOT_EXPR
, make_auto ());
5952 /* If there was an explicit qualification (S::~T), first look
5953 in the scope given by the qualification (i.e., S).
5955 Note: in the calls to cp_parser_class_name below we pass
5956 typename_type so that lookup finds the injected-class-name
5957 rather than the constructor. */
5959 type_decl
= NULL_TREE
;
5962 cp_parser_parse_tentatively (parser
);
5963 type_decl
= cp_parser_class_name (parser
,
5964 /*typename_keyword_p=*/false,
5965 /*template_keyword_p=*/false,
5967 /*check_dependency=*/false,
5968 /*class_head_p=*/false,
5970 if (cp_parser_parse_definitely (parser
))
5973 /* In "N::S::~S", look in "N" as well. */
5974 if (!done
&& scope
&& qualifying_scope
)
5976 cp_parser_parse_tentatively (parser
);
5977 parser
->scope
= qualifying_scope
;
5978 parser
->object_scope
= NULL_TREE
;
5979 parser
->qualifying_scope
= NULL_TREE
;
5981 = cp_parser_class_name (parser
,
5982 /*typename_keyword_p=*/false,
5983 /*template_keyword_p=*/false,
5985 /*check_dependency=*/false,
5986 /*class_head_p=*/false,
5988 if (cp_parser_parse_definitely (parser
))
5991 /* In "p->S::~T", look in the scope given by "*p" as well. */
5992 else if (!done
&& object_scope
)
5994 cp_parser_parse_tentatively (parser
);
5995 parser
->scope
= object_scope
;
5996 parser
->object_scope
= NULL_TREE
;
5997 parser
->qualifying_scope
= NULL_TREE
;
5999 = cp_parser_class_name (parser
,
6000 /*typename_keyword_p=*/false,
6001 /*template_keyword_p=*/false,
6003 /*check_dependency=*/false,
6004 /*class_head_p=*/false,
6006 if (cp_parser_parse_definitely (parser
))
6009 /* Look in the surrounding context. */
6012 parser
->scope
= NULL_TREE
;
6013 parser
->object_scope
= NULL_TREE
;
6014 parser
->qualifying_scope
= NULL_TREE
;
6015 if (processing_template_decl
)
6016 cp_parser_parse_tentatively (parser
);
6018 = cp_parser_class_name (parser
,
6019 /*typename_keyword_p=*/false,
6020 /*template_keyword_p=*/false,
6022 /*check_dependency=*/false,
6023 /*class_head_p=*/false,
6025 if (processing_template_decl
6026 && ! cp_parser_parse_definitely (parser
))
6028 /* We couldn't find a type with this name. If we're parsing
6029 tentatively, fail and try something else. */
6030 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6032 cp_parser_simulate_error (parser
);
6033 return error_mark_node
;
6035 /* Otherwise, accept it and check for a match at instantiation
6037 type_decl
= cp_parser_identifier (parser
);
6038 if (type_decl
!= error_mark_node
)
6039 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6043 /* If an error occurred, assume that the name of the
6044 destructor is the same as the name of the qualifying
6045 class. That allows us to keep parsing after running
6046 into ill-formed destructor names. */
6047 if (type_decl
== error_mark_node
&& scope
)
6048 return build_nt (BIT_NOT_EXPR
, scope
);
6049 else if (type_decl
== error_mark_node
)
6050 return error_mark_node
;
6052 /* Check that destructor name and scope match. */
6053 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6055 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6056 error_at (token
->location
,
6057 "declaration of %<~%T%> as member of %qT",
6059 cp_parser_simulate_error (parser
);
6060 return error_mark_node
;
6065 A typedef-name that names a class shall not be used as the
6066 identifier in the declarator for a destructor declaration. */
6068 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6069 && !DECL_SELF_REFERENCE_P (type_decl
)
6070 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6071 error_at (token
->location
,
6072 "typedef-name %qD used as destructor declarator",
6075 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6079 if (token
->keyword
== RID_OPERATOR
)
6083 /* This could be a template-id, so we try that first. */
6084 cp_parser_parse_tentatively (parser
);
6085 /* Try a template-id. */
6086 id
= cp_parser_template_id (parser
, template_keyword_p
,
6087 /*check_dependency_p=*/true,
6090 /* If that worked, we're done. */
6091 if (cp_parser_parse_definitely (parser
))
6093 /* We still don't know whether we're looking at an
6094 operator-function-id or a conversion-function-id. */
6095 cp_parser_parse_tentatively (parser
);
6096 /* Try an operator-function-id. */
6097 id
= cp_parser_operator_function_id (parser
);
6098 /* If that didn't work, try a conversion-function-id. */
6099 if (!cp_parser_parse_definitely (parser
))
6100 id
= cp_parser_conversion_function_id (parser
);
6101 else if (UDLIT_OPER_P (id
))
6104 const char *name
= UDLIT_OP_SUFFIX (id
);
6105 if (name
[0] != '_' && !in_system_header_at (input_location
)
6107 warning (OPT_Wliteral_suffix
,
6108 "literal operator suffixes not preceded by %<_%>"
6109 " are reserved for future standardization");
6119 cp_parser_error (parser
, "expected unqualified-id");
6120 return error_mark_node
;
6124 /* Parse an (optional) nested-name-specifier.
6126 nested-name-specifier: [C++98]
6127 class-or-namespace-name :: nested-name-specifier [opt]
6128 class-or-namespace-name :: template nested-name-specifier [opt]
6130 nested-name-specifier: [C++0x]
6133 nested-name-specifier identifier ::
6134 nested-name-specifier template [opt] simple-template-id ::
6136 PARSER->SCOPE should be set appropriately before this function is
6137 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6138 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6141 Sets PARSER->SCOPE to the class (TYPE) or namespace
6142 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6143 it unchanged if there is no nested-name-specifier. Returns the new
6144 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6146 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6147 part of a declaration and/or decl-specifier. */
6150 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6151 bool typename_keyword_p
,
6152 bool check_dependency_p
,
6154 bool is_declaration
,
6155 bool template_keyword_p
/* = false */)
6157 bool success
= false;
6158 cp_token_position start
= 0;
6161 /* Remember where the nested-name-specifier starts. */
6162 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6164 start
= cp_lexer_token_position (parser
->lexer
, false);
6165 push_deferring_access_checks (dk_deferred
);
6172 tree saved_qualifying_scope
;
6174 /* Spot cases that cannot be the beginning of a
6175 nested-name-specifier. */
6176 token
= cp_lexer_peek_token (parser
->lexer
);
6178 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6179 the already parsed nested-name-specifier. */
6180 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6182 /* Grab the nested-name-specifier and continue the loop. */
6183 cp_parser_pre_parsed_nested_name_specifier (parser
);
6184 /* If we originally encountered this nested-name-specifier
6185 with IS_DECLARATION set to false, we will not have
6186 resolved TYPENAME_TYPEs, so we must do so here. */
6188 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6190 new_scope
= resolve_typename_type (parser
->scope
,
6191 /*only_current_p=*/false);
6192 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6193 parser
->scope
= new_scope
;
6199 /* Spot cases that cannot be the beginning of a
6200 nested-name-specifier. On the second and subsequent times
6201 through the loop, we look for the `template' keyword. */
6202 if (success
&& token
->keyword
== RID_TEMPLATE
)
6204 /* A template-id can start a nested-name-specifier. */
6205 else if (token
->type
== CPP_TEMPLATE_ID
)
6207 /* DR 743: decltype can be used in a nested-name-specifier. */
6208 else if (token_is_decltype (token
))
6212 /* If the next token is not an identifier, then it is
6213 definitely not a type-name or namespace-name. */
6214 if (token
->type
!= CPP_NAME
)
6216 /* If the following token is neither a `<' (to begin a
6217 template-id), nor a `::', then we are not looking at a
6218 nested-name-specifier. */
6219 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6221 if (token
->type
== CPP_COLON
6222 && parser
->colon_corrects_to_scope_p
6223 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6225 gcc_rich_location
richloc (token
->location
);
6226 richloc
.add_fixit_replace ("::");
6228 "found %<:%> in nested-name-specifier, "
6230 token
->type
= CPP_SCOPE
;
6233 if (token
->type
!= CPP_SCOPE
6234 && !cp_parser_nth_token_starts_template_argument_list_p
6239 /* The nested-name-specifier is optional, so we parse
6241 cp_parser_parse_tentatively (parser
);
6243 /* Look for the optional `template' keyword, if this isn't the
6244 first time through the loop. */
6246 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6248 /* Save the old scope since the name lookup we are about to do
6249 might destroy it. */
6250 old_scope
= parser
->scope
;
6251 saved_qualifying_scope
= parser
->qualifying_scope
;
6252 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6253 look up names in "X<T>::I" in order to determine that "Y" is
6254 a template. So, if we have a typename at this point, we make
6255 an effort to look through it. */
6257 && !typename_keyword_p
6259 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6260 parser
->scope
= resolve_typename_type (parser
->scope
,
6261 /*only_current_p=*/false);
6262 /* Parse the qualifying entity. */
6264 = cp_parser_qualifying_entity (parser
,
6270 /* Look for the `::' token. */
6271 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6273 /* If we found what we wanted, we keep going; otherwise, we're
6275 if (!cp_parser_parse_definitely (parser
))
6277 bool error_p
= false;
6279 /* Restore the OLD_SCOPE since it was valid before the
6280 failed attempt at finding the last
6281 class-or-namespace-name. */
6282 parser
->scope
= old_scope
;
6283 parser
->qualifying_scope
= saved_qualifying_scope
;
6285 /* If the next token is a decltype, and the one after that is a
6286 `::', then the decltype has failed to resolve to a class or
6287 enumeration type. Give this error even when parsing
6288 tentatively since it can't possibly be valid--and we're going
6289 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6290 won't get another chance.*/
6291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6292 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6295 token
= cp_lexer_consume_token (parser
->lexer
);
6296 error_at (token
->location
, "decltype evaluates to %qT, "
6297 "which is not a class or enumeration type",
6298 token
->u
.tree_check_value
->value
);
6299 parser
->scope
= error_mark_node
;
6303 cp_lexer_consume_token (parser
->lexer
);
6306 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6307 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6309 /* If we have a non-type template-id followed by ::, it can't
6310 possibly be valid. */
6311 token
= cp_lexer_peek_token (parser
->lexer
);
6312 tree tid
= token
->u
.tree_check_value
->value
;
6313 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6314 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6316 tree tmpl
= NULL_TREE
;
6317 if (is_overloaded_fn (tid
))
6319 tree fns
= get_fns (tid
);
6320 if (OVL_SINGLE_P (fns
))
6321 tmpl
= OVL_FIRST (fns
);
6322 error_at (token
->location
, "function template-id %qD "
6323 "in nested-name-specifier", tid
);
6327 /* Variable template. */
6328 tmpl
= TREE_OPERAND (tid
, 0);
6329 gcc_assert (variable_template_p (tmpl
));
6330 error_at (token
->location
, "variable template-id %qD "
6331 "in nested-name-specifier", tid
);
6334 inform (DECL_SOURCE_LOCATION (tmpl
),
6335 "%qD declared here", tmpl
);
6337 parser
->scope
= error_mark_node
;
6341 cp_lexer_consume_token (parser
->lexer
);
6342 cp_lexer_consume_token (parser
->lexer
);
6346 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6348 /* If the next token is an identifier, and the one after
6349 that is a `::', then any valid interpretation would have
6350 found a class-or-namespace-name. */
6351 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6352 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6354 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6357 token
= cp_lexer_consume_token (parser
->lexer
);
6360 if (!token
->error_reported
)
6363 tree ambiguous_decls
;
6365 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6367 /*is_template=*/false,
6368 /*is_namespace=*/false,
6369 /*check_dependency=*/true,
6372 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6373 error_at (token
->location
,
6374 "%qD used without template parameters",
6376 else if (ambiguous_decls
)
6378 // cp_parser_lookup_name has the same diagnostic,
6379 // thus make sure to emit it at most once.
6380 if (cp_parser_uncommitted_to_tentative_parse_p
6383 error_at (token
->location
,
6384 "reference to %qD is ambiguous",
6386 print_candidates (ambiguous_decls
);
6388 decl
= error_mark_node
;
6392 if (cxx_dialect
!= cxx98
)
6393 cp_parser_name_lookup_error
6394 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6397 cp_parser_name_lookup_error
6398 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6402 parser
->scope
= error_mark_node
;
6404 /* Treat this as a successful nested-name-specifier
6409 If the name found is not a class-name (clause
6410 _class_) or namespace-name (_namespace.def_), the
6411 program is ill-formed. */
6414 cp_lexer_consume_token (parser
->lexer
);
6418 /* We've found one valid nested-name-specifier. */
6420 /* Name lookup always gives us a DECL. */
6421 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6422 new_scope
= TREE_TYPE (new_scope
);
6423 /* Uses of "template" must be followed by actual templates. */
6424 if (template_keyword_p
6425 && !(CLASS_TYPE_P (new_scope
)
6426 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6427 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6428 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6429 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6430 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6431 == TEMPLATE_ID_EXPR
)))
6432 permerror (input_location
, TYPE_P (new_scope
)
6433 ? G_("%qT is not a template")
6434 : G_("%qD is not a template"),
6436 /* If it is a class scope, try to complete it; we are about to
6437 be looking up names inside the class. */
6438 if (TYPE_P (new_scope
)
6439 /* Since checking types for dependency can be expensive,
6440 avoid doing it if the type is already complete. */
6441 && !COMPLETE_TYPE_P (new_scope
)
6442 /* Do not try to complete dependent types. */
6443 && !dependent_type_p (new_scope
))
6445 new_scope
= complete_type (new_scope
);
6446 /* If it is a typedef to current class, use the current
6447 class instead, as the typedef won't have any names inside
6449 if (!COMPLETE_TYPE_P (new_scope
)
6450 && currently_open_class (new_scope
))
6451 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6453 /* Make sure we look in the right scope the next time through
6455 parser
->scope
= new_scope
;
6458 /* If parsing tentatively, replace the sequence of tokens that makes
6459 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6460 token. That way, should we re-parse the token stream, we will
6461 not have to repeat the effort required to do the parse, nor will
6462 we issue duplicate error messages. */
6463 if (success
&& start
)
6467 token
= cp_lexer_token_at (parser
->lexer
, start
);
6468 /* Reset the contents of the START token. */
6469 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6470 /* Retrieve any deferred checks. Do not pop this access checks yet
6471 so the memory will not be reclaimed during token replacing below. */
6472 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6473 token
->u
.tree_check_value
->value
= parser
->scope
;
6474 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6475 token
->u
.tree_check_value
->qualifying_scope
=
6476 parser
->qualifying_scope
;
6477 token
->keyword
= RID_MAX
;
6479 /* Purge all subsequent tokens. */
6480 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6484 pop_to_parent_deferring_access_checks ();
6486 return success
? parser
->scope
: NULL_TREE
;
6489 /* Parse a nested-name-specifier. See
6490 cp_parser_nested_name_specifier_opt for details. This function
6491 behaves identically, except that it will an issue an error if no
6492 nested-name-specifier is present. */
6495 cp_parser_nested_name_specifier (cp_parser
*parser
,
6496 bool typename_keyword_p
,
6497 bool check_dependency_p
,
6499 bool is_declaration
)
6503 /* Look for the nested-name-specifier. */
6504 scope
= cp_parser_nested_name_specifier_opt (parser
,
6509 /* If it was not present, issue an error message. */
6512 cp_parser_error (parser
, "expected nested-name-specifier");
6513 parser
->scope
= NULL_TREE
;
6519 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6520 this is either a class-name or a namespace-name (which corresponds
6521 to the class-or-namespace-name production in the grammar). For
6522 C++0x, it can also be a type-name that refers to an enumeration
6523 type or a simple-template-id.
6525 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6526 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6527 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6528 TYPE_P is TRUE iff the next name should be taken as a class-name,
6529 even the same name is declared to be another entity in the same
6532 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6533 specified by the class-or-namespace-name. If neither is found the
6534 ERROR_MARK_NODE is returned. */
6537 cp_parser_qualifying_entity (cp_parser
*parser
,
6538 bool typename_keyword_p
,
6539 bool template_keyword_p
,
6540 bool check_dependency_p
,
6542 bool is_declaration
)
6545 tree saved_qualifying_scope
;
6546 tree saved_object_scope
;
6549 bool successful_parse_p
;
6551 /* DR 743: decltype can appear in a nested-name-specifier. */
6552 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6554 scope
= cp_parser_decltype (parser
);
6555 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6556 && !MAYBE_CLASS_TYPE_P (scope
))
6558 cp_parser_simulate_error (parser
);
6559 return error_mark_node
;
6561 if (TYPE_NAME (scope
))
6562 scope
= TYPE_NAME (scope
);
6566 /* Before we try to parse the class-name, we must save away the
6567 current PARSER->SCOPE since cp_parser_class_name will destroy
6569 saved_scope
= parser
->scope
;
6570 saved_qualifying_scope
= parser
->qualifying_scope
;
6571 saved_object_scope
= parser
->object_scope
;
6572 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6573 there is no need to look for a namespace-name. */
6574 only_class_p
= template_keyword_p
6575 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6577 cp_parser_parse_tentatively (parser
);
6578 scope
= cp_parser_class_name (parser
,
6581 type_p
? class_type
: none_type
,
6583 /*class_head_p=*/false,
6585 /*enum_ok=*/cxx_dialect
> cxx98
);
6586 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6587 /* If that didn't work, try for a namespace-name. */
6588 if (!only_class_p
&& !successful_parse_p
)
6590 /* Restore the saved scope. */
6591 parser
->scope
= saved_scope
;
6592 parser
->qualifying_scope
= saved_qualifying_scope
;
6593 parser
->object_scope
= saved_object_scope
;
6594 /* If we are not looking at an identifier followed by the scope
6595 resolution operator, then this is not part of a
6596 nested-name-specifier. (Note that this function is only used
6597 to parse the components of a nested-name-specifier.) */
6598 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6599 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6600 return error_mark_node
;
6601 scope
= cp_parser_namespace_name (parser
);
6607 /* Return true if we are looking at a compound-literal, false otherwise. */
6610 cp_parser_compound_literal_p (cp_parser
*parser
)
6612 cp_lexer_save_tokens (parser
->lexer
);
6614 /* Skip tokens until the next token is a closing parenthesis.
6615 If we find the closing `)', and the next token is a `{', then
6616 we are looking at a compound-literal. */
6617 bool compound_literal_p
6618 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6619 /*consume_paren=*/true)
6620 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6622 /* Roll back the tokens we skipped. */
6623 cp_lexer_rollback_tokens (parser
->lexer
);
6625 return compound_literal_p
;
6628 /* Return true if EXPR is the integer constant zero or a complex constant
6629 of zero, without any folding, but ignoring location wrappers. */
6632 literal_integer_zerop (const_tree expr
)
6634 STRIP_ANY_LOCATION_WRAPPER (expr
);
6635 return integer_zerop (expr
);
6638 /* Parse a postfix-expression.
6642 postfix-expression [ expression ]
6643 postfix-expression ( expression-list [opt] )
6644 simple-type-specifier ( expression-list [opt] )
6645 typename :: [opt] nested-name-specifier identifier
6646 ( expression-list [opt] )
6647 typename :: [opt] nested-name-specifier template [opt] template-id
6648 ( expression-list [opt] )
6649 postfix-expression . template [opt] id-expression
6650 postfix-expression -> template [opt] id-expression
6651 postfix-expression . pseudo-destructor-name
6652 postfix-expression -> pseudo-destructor-name
6653 postfix-expression ++
6654 postfix-expression --
6655 dynamic_cast < type-id > ( expression )
6656 static_cast < type-id > ( expression )
6657 reinterpret_cast < type-id > ( expression )
6658 const_cast < type-id > ( expression )
6659 typeid ( expression )
6665 ( type-id ) { initializer-list , [opt] }
6667 This extension is a GNU version of the C99 compound-literal
6668 construct. (The C99 grammar uses `type-name' instead of `type-id',
6669 but they are essentially the same concept.)
6671 If ADDRESS_P is true, the postfix expression is the operand of the
6672 `&' operator. CAST_P is true if this expression is the target of a
6675 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6676 class member access expressions [expr.ref].
6678 Returns a representation of the expression. */
6681 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6682 bool member_access_only_p
, bool decltype_p
,
6683 cp_id_kind
* pidk_return
)
6688 cp_id_kind idk
= CP_ID_KIND_NONE
;
6689 cp_expr postfix_expression
= NULL_TREE
;
6690 bool is_member_access
= false;
6692 /* Peek at the next token. */
6693 token
= cp_lexer_peek_token (parser
->lexer
);
6694 loc
= token
->location
;
6695 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6697 /* Some of the productions are determined by keywords. */
6698 keyword
= token
->keyword
;
6708 const char *saved_message
;
6709 bool saved_in_type_id_in_expr_p
;
6711 /* All of these can be handled in the same way from the point
6712 of view of parsing. Begin by consuming the token
6713 identifying the cast. */
6714 cp_lexer_consume_token (parser
->lexer
);
6716 /* New types cannot be defined in the cast. */
6717 saved_message
= parser
->type_definition_forbidden_message
;
6718 parser
->type_definition_forbidden_message
6719 = G_("types may not be defined in casts");
6721 /* Look for the opening `<'. */
6722 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6723 /* Parse the type to which we are casting. */
6724 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6725 parser
->in_type_id_in_expr_p
= true;
6726 type
= cp_parser_type_id (parser
);
6727 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6728 /* Look for the closing `>'. */
6729 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6730 /* Restore the old message. */
6731 parser
->type_definition_forbidden_message
= saved_message
;
6733 bool saved_greater_than_is_operator_p
6734 = parser
->greater_than_is_operator_p
;
6735 parser
->greater_than_is_operator_p
= true;
6737 /* And the expression which is being cast. */
6738 matching_parens parens
;
6739 parens
.require_open (parser
);
6740 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6741 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6743 location_t end_loc
= close_paren
?
6744 close_paren
->location
: UNKNOWN_LOCATION
;
6746 parser
->greater_than_is_operator_p
6747 = saved_greater_than_is_operator_p
;
6749 /* Only type conversions to integral or enumeration types
6750 can be used in constant-expressions. */
6751 if (!cast_valid_in_integral_constant_expression_p (type
)
6752 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6754 postfix_expression
= error_mark_node
;
6762 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6766 = build_static_cast (type
, expression
, tf_warning_or_error
);
6770 = build_reinterpret_cast (type
, expression
,
6771 tf_warning_or_error
);
6775 = build_const_cast (type
, expression
, tf_warning_or_error
);
6781 /* Construct a location e.g. :
6782 reinterpret_cast <int *> (expr)
6783 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6784 ranging from the start of the "*_cast" token to the final closing
6785 paren, with the caret at the start. */
6786 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6787 postfix_expression
.set_location (cp_cast_loc
);
6794 const char *saved_message
;
6795 bool saved_in_type_id_in_expr_p
;
6797 /* Consume the `typeid' token. */
6798 cp_lexer_consume_token (parser
->lexer
);
6799 /* Look for the `(' token. */
6800 matching_parens parens
;
6801 parens
.require_open (parser
);
6802 /* Types cannot be defined in a `typeid' expression. */
6803 saved_message
= parser
->type_definition_forbidden_message
;
6804 parser
->type_definition_forbidden_message
6805 = G_("types may not be defined in a %<typeid%> expression");
6806 /* We can't be sure yet whether we're looking at a type-id or an
6808 cp_parser_parse_tentatively (parser
);
6809 /* Try a type-id first. */
6810 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6811 parser
->in_type_id_in_expr_p
= true;
6812 type
= cp_parser_type_id (parser
);
6813 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6814 /* Look for the `)' token. Otherwise, we can't be sure that
6815 we're not looking at an expression: consider `typeid (int
6816 (3))', for example. */
6817 cp_token
*close_paren
= parens
.require_close (parser
);
6818 /* If all went well, simply lookup the type-id. */
6819 if (cp_parser_parse_definitely (parser
))
6820 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6821 /* Otherwise, fall back to the expression variant. */
6826 /* Look for an expression. */
6827 expression
= cp_parser_expression (parser
, & idk
);
6828 /* Compute its typeid. */
6829 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6830 /* Look for the `)' token. */
6831 close_paren
= parens
.require_close (parser
);
6833 /* Restore the saved message. */
6834 parser
->type_definition_forbidden_message
= saved_message
;
6835 /* `typeid' may not appear in an integral constant expression. */
6836 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6837 postfix_expression
= error_mark_node
;
6839 /* Construct a location e.g. :
6842 ranging from the start of the "typeid" token to the final closing
6843 paren, with the caret at the start. */
6846 location_t typeid_loc
6847 = make_location (start_loc
, start_loc
, close_paren
->location
);
6848 postfix_expression
.set_location (typeid_loc
);
6849 postfix_expression
.maybe_add_location_wrapper ();
6857 /* The syntax permitted here is the same permitted for an
6858 elaborated-type-specifier. */
6859 ++parser
->prevent_constrained_type_specifiers
;
6860 type
= cp_parser_elaborated_type_specifier (parser
,
6861 /*is_friend=*/false,
6862 /*is_declaration=*/false);
6863 --parser
->prevent_constrained_type_specifiers
;
6864 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6869 case RID_BUILTIN_SHUFFLE
:
6870 case RID_BUILTIN_LAUNDER
:
6872 vec
<tree
, va_gc
> *vec
;
6876 cp_lexer_consume_token (parser
->lexer
);
6877 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6878 /*cast_p=*/false, /*allow_expansion_p=*/true,
6879 /*non_constant_p=*/NULL
);
6882 postfix_expression
= error_mark_node
;
6886 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6892 if (vec
->length () == 1)
6894 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6897 error_at (loc
, "wrong number of arguments to "
6898 "%<__builtin_addressof%>");
6899 postfix_expression
= error_mark_node
;
6903 case RID_BUILTIN_LAUNDER
:
6904 if (vec
->length () == 1)
6905 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6906 tf_warning_or_error
);
6909 error_at (loc
, "wrong number of arguments to "
6910 "%<__builtin_launder%>");
6911 postfix_expression
= error_mark_node
;
6915 case RID_BUILTIN_SHUFFLE
:
6916 if (vec
->length () == 2)
6918 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6919 (*vec
)[1], tf_warning_or_error
);
6920 else if (vec
->length () == 3)
6922 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6923 (*vec
)[2], tf_warning_or_error
);
6926 error_at (loc
, "wrong number of arguments to "
6927 "%<__builtin_shuffle%>");
6928 postfix_expression
= error_mark_node
;
6942 /* If the next thing is a simple-type-specifier, we may be
6943 looking at a functional cast. We could also be looking at
6944 an id-expression. So, we try the functional cast, and if
6945 that doesn't work we fall back to the primary-expression. */
6946 cp_parser_parse_tentatively (parser
);
6947 /* Look for the simple-type-specifier. */
6948 ++parser
->prevent_constrained_type_specifiers
;
6949 type
= cp_parser_simple_type_specifier (parser
,
6950 /*decl_specs=*/NULL
,
6951 CP_PARSER_FLAGS_NONE
);
6952 --parser
->prevent_constrained_type_specifiers
;
6953 /* Parse the cast itself. */
6954 if (!cp_parser_error_occurred (parser
))
6956 = cp_parser_functional_cast (parser
, type
);
6957 /* If that worked, we're done. */
6958 if (cp_parser_parse_definitely (parser
))
6961 /* If the functional-cast didn't work out, try a
6962 compound-literal. */
6963 if (cp_parser_allow_gnu_extensions_p (parser
)
6964 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6966 cp_expr initializer
= NULL_TREE
;
6968 cp_parser_parse_tentatively (parser
);
6970 matching_parens parens
;
6971 parens
.consume_open (parser
);
6973 /* Avoid calling cp_parser_type_id pointlessly, see comment
6974 in cp_parser_cast_expression about c++/29234. */
6975 if (!cp_parser_compound_literal_p (parser
))
6976 cp_parser_simulate_error (parser
);
6979 /* Parse the type. */
6980 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6981 parser
->in_type_id_in_expr_p
= true;
6982 type
= cp_parser_type_id (parser
);
6983 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6984 parens
.require_close (parser
);
6987 /* If things aren't going well, there's no need to
6989 if (!cp_parser_error_occurred (parser
))
6991 bool non_constant_p
;
6992 /* Parse the brace-enclosed initializer list. */
6993 initializer
= cp_parser_braced_list (parser
,
6996 /* If that worked, we're definitely looking at a
6997 compound-literal expression. */
6998 if (cp_parser_parse_definitely (parser
))
7000 /* Warn the user that a compound literal is not
7001 allowed in standard C++. */
7002 pedwarn (input_location
, OPT_Wpedantic
,
7003 "ISO C++ forbids compound-literals");
7004 /* For simplicity, we disallow compound literals in
7005 constant-expressions. We could
7006 allow compound literals of integer type, whose
7007 initializer was a constant, in constant
7008 expressions. Permitting that usage, as a further
7009 extension, would not change the meaning of any
7010 currently accepted programs. (Of course, as
7011 compound literals are not part of ISO C++, the
7012 standard has nothing to say.) */
7013 if (cp_parser_non_integral_constant_expression (parser
,
7016 postfix_expression
= error_mark_node
;
7019 /* Form the representation of the compound-literal. */
7021 = finish_compound_literal (type
, initializer
,
7022 tf_warning_or_error
, fcl_c99
);
7023 postfix_expression
.set_location (initializer
.get_location ());
7028 /* It must be a primary-expression. */
7030 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7031 /*template_arg_p=*/false,
7038 /* Note that we don't need to worry about calling build_cplus_new on a
7039 class-valued CALL_EXPR in decltype when it isn't the end of the
7040 postfix-expression; unary_complex_lvalue will take care of that for
7043 /* Keep looping until the postfix-expression is complete. */
7046 if (idk
== CP_ID_KIND_UNQUALIFIED
7047 && identifier_p (postfix_expression
)
7048 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7049 /* It is not a Koenig lookup function call. */
7051 = unqualified_name_lookup_error (postfix_expression
);
7053 /* Peek at the next token. */
7054 token
= cp_lexer_peek_token (parser
->lexer
);
7056 switch (token
->type
)
7058 case CPP_OPEN_SQUARE
:
7059 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7061 cp_parser_error (parser
,
7062 "two consecutive %<[%> shall "
7063 "only introduce an attribute");
7064 return error_mark_node
;
7067 = cp_parser_postfix_open_square_expression (parser
,
7071 postfix_expression
.set_range (start_loc
,
7072 postfix_expression
.get_location ());
7074 idk
= CP_ID_KIND_NONE
;
7075 is_member_access
= false;
7078 case CPP_OPEN_PAREN
:
7079 /* postfix-expression ( expression-list [opt] ) */
7082 bool is_builtin_constant_p
;
7083 bool saved_integral_constant_expression_p
= false;
7084 bool saved_non_integral_constant_expression_p
= false;
7085 tsubst_flags_t complain
= complain_flags (decltype_p
);
7086 vec
<tree
, va_gc
> *args
;
7087 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7089 is_member_access
= false;
7091 is_builtin_constant_p
7092 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7093 if (is_builtin_constant_p
)
7095 /* The whole point of __builtin_constant_p is to allow
7096 non-constant expressions to appear as arguments. */
7097 saved_integral_constant_expression_p
7098 = parser
->integral_constant_expression_p
;
7099 saved_non_integral_constant_expression_p
7100 = parser
->non_integral_constant_expression_p
;
7101 parser
->integral_constant_expression_p
= false;
7103 args
= (cp_parser_parenthesized_expression_list
7105 /*cast_p=*/false, /*allow_expansion_p=*/true,
7106 /*non_constant_p=*/NULL
,
7107 /*close_paren_loc=*/&close_paren_loc
,
7108 /*wrap_locations_p=*/true));
7109 if (is_builtin_constant_p
)
7111 parser
->integral_constant_expression_p
7112 = saved_integral_constant_expression_p
;
7113 parser
->non_integral_constant_expression_p
7114 = saved_non_integral_constant_expression_p
;
7119 postfix_expression
= error_mark_node
;
7123 /* Function calls are not permitted in
7124 constant-expressions. */
7125 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7126 && cp_parser_non_integral_constant_expression (parser
,
7129 postfix_expression
= error_mark_node
;
7130 release_tree_vector (args
);
7135 if (idk
== CP_ID_KIND_UNQUALIFIED
7136 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7138 if (identifier_p (postfix_expression
))
7140 if (!args
->is_empty ())
7143 if (!any_type_dependent_arguments_p (args
))
7145 = perform_koenig_lookup (postfix_expression
, args
,
7150 = unqualified_fn_lookup_error (postfix_expression
);
7152 /* We do not perform argument-dependent lookup if
7153 normal lookup finds a non-function, in accordance
7154 with the expected resolution of DR 218. */
7155 else if (!args
->is_empty ()
7156 && is_overloaded_fn (postfix_expression
))
7158 tree fn
= get_first_fn (postfix_expression
);
7159 fn
= STRIP_TEMPLATE (fn
);
7161 /* Do not do argument dependent lookup if regular
7162 lookup finds a member function or a block-scope
7163 function declaration. [basic.lookup.argdep]/3 */
7164 if (!DECL_FUNCTION_MEMBER_P (fn
)
7165 && !DECL_LOCAL_FUNCTION_P (fn
))
7168 if (!any_type_dependent_arguments_p (args
))
7170 = perform_koenig_lookup (postfix_expression
, args
,
7176 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
7177 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
7178 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
7179 && vec_safe_length (args
) == 3)
7181 tree arg0
= (*args
)[0];
7182 tree arg1
= (*args
)[1];
7183 tree arg2
= (*args
)[2];
7184 int literal_mask
= ((literal_integer_zerop (arg1
) << 1)
7185 | (literal_integer_zerop (arg2
) << 2));
7186 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
7189 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7191 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7192 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7194 if (processing_template_decl
7195 && (type_dependent_object_expression_p (instance
)
7196 || (!BASELINK_P (fn
)
7197 && TREE_CODE (fn
) != FIELD_DECL
)
7198 || type_dependent_expression_p (fn
)
7199 || any_type_dependent_arguments_p (args
)))
7201 maybe_generic_this_capture (instance
, fn
);
7203 = build_min_nt_call_vec (postfix_expression
, args
);
7204 release_tree_vector (args
);
7208 if (BASELINK_P (fn
))
7211 = (build_new_method_call
7212 (instance
, fn
, &args
, NULL_TREE
,
7213 (idk
== CP_ID_KIND_QUALIFIED
7214 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7221 = finish_call_expr (postfix_expression
, &args
,
7222 /*disallow_virtual=*/false,
7226 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7227 || TREE_CODE (postfix_expression
) == MEMBER_REF
7228 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7229 postfix_expression
= (build_offset_ref_call_from_tree
7230 (postfix_expression
, &args
,
7232 else if (idk
== CP_ID_KIND_QUALIFIED
)
7233 /* A call to a static class member, or a namespace-scope
7236 = finish_call_expr (postfix_expression
, &args
,
7237 /*disallow_virtual=*/true,
7241 /* All other function calls. */
7243 = finish_call_expr (postfix_expression
, &args
,
7244 /*disallow_virtual=*/false,
7248 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7250 location_t combined_loc
= make_location (token
->location
,
7253 postfix_expression
.set_location (combined_loc
);
7256 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7257 idk
= CP_ID_KIND_NONE
;
7259 release_tree_vector (args
);
7265 /* postfix-expression . template [opt] id-expression
7266 postfix-expression . pseudo-destructor-name
7267 postfix-expression -> template [opt] id-expression
7268 postfix-expression -> pseudo-destructor-name */
7270 /* Consume the `.' or `->' operator. */
7271 cp_lexer_consume_token (parser
->lexer
);
7274 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7278 is_member_access
= true;
7282 /* postfix-expression ++ */
7283 /* Consume the `++' token. */
7284 cp_lexer_consume_token (parser
->lexer
);
7285 /* Generate a representation for the complete expression. */
7287 = finish_increment_expr (postfix_expression
,
7288 POSTINCREMENT_EXPR
);
7289 /* Increments may not appear in constant-expressions. */
7290 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7291 postfix_expression
= error_mark_node
;
7292 idk
= CP_ID_KIND_NONE
;
7293 is_member_access
= false;
7296 case CPP_MINUS_MINUS
:
7297 /* postfix-expression -- */
7298 /* Consume the `--' token. */
7299 cp_lexer_consume_token (parser
->lexer
);
7300 /* Generate a representation for the complete expression. */
7302 = finish_increment_expr (postfix_expression
,
7303 POSTDECREMENT_EXPR
);
7304 /* Decrements may not appear in constant-expressions. */
7305 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7306 postfix_expression
= error_mark_node
;
7307 idk
= CP_ID_KIND_NONE
;
7308 is_member_access
= false;
7312 if (pidk_return
!= NULL
)
7313 * pidk_return
= idk
;
7314 if (member_access_only_p
)
7315 return is_member_access
7316 ? postfix_expression
7317 : cp_expr (error_mark_node
);
7319 return postfix_expression
;
7323 /* We should never get here. */
7325 return error_mark_node
;
7328 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7329 by cp_parser_builtin_offsetof. We're looking for
7331 postfix-expression [ expression ]
7332 postfix-expression [ braced-init-list ] (C++11)
7334 FOR_OFFSETOF is set if we're being called in that context, which
7335 changes how we deal with integer constant expressions. */
7338 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7339 tree postfix_expression
,
7343 tree index
= NULL_TREE
;
7344 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7345 bool saved_greater_than_is_operator_p
;
7347 /* Consume the `[' token. */
7348 cp_lexer_consume_token (parser
->lexer
);
7350 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7351 parser
->greater_than_is_operator_p
= true;
7353 /* Parse the index expression. */
7354 /* ??? For offsetof, there is a question of what to allow here. If
7355 offsetof is not being used in an integral constant expression context,
7356 then we *could* get the right answer by computing the value at runtime.
7357 If we are in an integral constant expression context, then we might
7358 could accept any constant expression; hard to say without analysis.
7359 Rather than open the barn door too wide right away, allow only integer
7360 constant expressions here. */
7362 index
= cp_parser_constant_expression (parser
);
7365 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7367 bool expr_nonconst_p
;
7368 cp_lexer_set_source_position (parser
->lexer
);
7369 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7370 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7373 index
= cp_parser_expression (parser
);
7376 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7378 /* Look for the closing `]'. */
7379 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7381 /* Build the ARRAY_REF. */
7382 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7385 /* When not doing offsetof, array references are not permitted in
7386 constant-expressions. */
7388 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7389 postfix_expression
= error_mark_node
;
7391 return postfix_expression
;
7394 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7395 dereference of incomplete type, returns true if error_mark_node should
7396 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7397 and *DEPENDENT_P. */
7400 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7403 /* In a template, be permissive by treating an object expression
7404 of incomplete type as dependent (after a pedwarn). */
7405 diagnostic_t kind
= (processing_template_decl
7406 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7408 switch (TREE_CODE (*postfix_expression
))
7411 case REINTERPRET_CAST_EXPR
:
7412 case CONST_CAST_EXPR
:
7413 case STATIC_CAST_EXPR
:
7414 case DYNAMIC_CAST_EXPR
:
7415 case IMPLICIT_CONV_EXPR
:
7416 case VIEW_CONVERT_EXPR
:
7417 case NON_LVALUE_EXPR
:
7421 /* Don't emit any diagnostic for OVERLOADs. */
7425 /* Avoid clobbering e.g. DECLs. */
7426 if (!EXPR_P (*postfix_expression
))
7431 if (kind
== DK_IGNORED
)
7434 location_t exploc
= location_of (*postfix_expression
);
7435 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7436 if (!MAYBE_CLASS_TYPE_P (*scope
))
7438 if (kind
== DK_ERROR
)
7439 *scope
= *postfix_expression
= error_mark_node
;
7440 else if (processing_template_decl
)
7442 *dependent_p
= true;
7443 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7448 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7449 by cp_parser_builtin_offsetof. We're looking for
7451 postfix-expression . template [opt] id-expression
7452 postfix-expression . pseudo-destructor-name
7453 postfix-expression -> template [opt] id-expression
7454 postfix-expression -> pseudo-destructor-name
7456 FOR_OFFSETOF is set if we're being called in that context. That sorta
7457 limits what of the above we'll actually accept, but nevermind.
7458 TOKEN_TYPE is the "." or "->" token, which will already have been
7459 removed from the stream. */
7462 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7463 enum cpp_ttype token_type
,
7464 cp_expr postfix_expression
,
7465 bool for_offsetof
, cp_id_kind
*idk
,
7466 location_t location
)
7470 bool pseudo_destructor_p
;
7471 tree scope
= NULL_TREE
;
7472 location_t start_loc
= postfix_expression
.get_start ();
7474 /* If this is a `->' operator, dereference the pointer. */
7475 if (token_type
== CPP_DEREF
)
7476 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7477 tf_warning_or_error
);
7478 /* Check to see whether or not the expression is type-dependent and
7479 not the current instantiation. */
7480 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7481 /* The identifier following the `->' or `.' is not qualified. */
7482 parser
->scope
= NULL_TREE
;
7483 parser
->qualifying_scope
= NULL_TREE
;
7484 parser
->object_scope
= NULL_TREE
;
7485 *idk
= CP_ID_KIND_NONE
;
7487 /* Enter the scope corresponding to the type of the object
7488 given by the POSTFIX_EXPRESSION. */
7491 scope
= TREE_TYPE (postfix_expression
);
7492 /* According to the standard, no expression should ever have
7493 reference type. Unfortunately, we do not currently match
7494 the standard in this respect in that our internal representation
7495 of an expression may have reference type even when the standard
7496 says it does not. Therefore, we have to manually obtain the
7497 underlying type here. */
7498 scope
= non_reference (scope
);
7499 /* The type of the POSTFIX_EXPRESSION must be complete. */
7500 /* Unlike the object expression in other contexts, *this is not
7501 required to be of complete type for purposes of class member
7502 access (5.2.5) outside the member function body. */
7503 if (postfix_expression
!= current_class_ref
7504 && scope
!= error_mark_node
7505 && !(processing_template_decl
7506 && current_class_type
7507 && (same_type_ignoring_top_level_qualifiers_p
7508 (scope
, current_class_type
))))
7510 scope
= complete_type (scope
);
7511 if (!COMPLETE_TYPE_P (scope
)
7512 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7514 return error_mark_node
;
7519 /* Let the name lookup machinery know that we are processing a
7520 class member access expression. */
7521 parser
->context
->object_type
= scope
;
7522 /* If something went wrong, we want to be able to discern that case,
7523 as opposed to the case where there was no SCOPE due to the type
7524 of expression being dependent. */
7526 scope
= error_mark_node
;
7527 /* If the SCOPE was erroneous, make the various semantic analysis
7528 functions exit quickly -- and without issuing additional error
7530 if (scope
== error_mark_node
)
7531 postfix_expression
= error_mark_node
;
7536 /* Tell cp_parser_lookup_name that there was an object, even though it's
7538 parser
->context
->object_type
= unknown_type_node
;
7540 /* Assume this expression is not a pseudo-destructor access. */
7541 pseudo_destructor_p
= false;
7543 /* If the SCOPE is a scalar type, then, if this is a valid program,
7544 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7545 is type dependent, it can be pseudo-destructor-name or something else.
7546 Try to parse it as pseudo-destructor-name first. */
7547 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7552 cp_parser_parse_tentatively (parser
);
7553 /* Parse the pseudo-destructor-name. */
7555 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7558 && (cp_parser_error_occurred (parser
)
7559 || !SCALAR_TYPE_P (type
)))
7560 cp_parser_abort_tentative_parse (parser
);
7561 else if (cp_parser_parse_definitely (parser
))
7563 pseudo_destructor_p
= true;
7565 = finish_pseudo_destructor_expr (postfix_expression
,
7570 if (!pseudo_destructor_p
)
7572 /* If the SCOPE is not a scalar type, we are looking at an
7573 ordinary class member access expression, rather than a
7574 pseudo-destructor-name. */
7576 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7577 /* Parse the id-expression. */
7578 name
= (cp_parser_id_expression
7580 cp_parser_optional_template_keyword (parser
),
7581 /*check_dependency_p=*/true,
7583 /*declarator_p=*/false,
7584 /*optional_p=*/false));
7585 /* In general, build a SCOPE_REF if the member name is qualified.
7586 However, if the name was not dependent and has already been
7587 resolved; there is no need to build the SCOPE_REF. For example;
7589 struct X { void f(); };
7590 template <typename T> void f(T* t) { t->X::f(); }
7592 Even though "t" is dependent, "X::f" is not and has been resolved
7593 to a BASELINK; there is no need to include scope information. */
7595 /* But we do need to remember that there was an explicit scope for
7596 virtual function calls. */
7598 *idk
= CP_ID_KIND_QUALIFIED
;
7600 /* If the name is a template-id that names a type, we will get a
7601 TYPE_DECL here. That is invalid code. */
7602 if (TREE_CODE (name
) == TYPE_DECL
)
7604 error_at (token
->location
, "invalid use of %qD", name
);
7605 postfix_expression
= error_mark_node
;
7609 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7611 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7613 error_at (token
->location
, "%<%D::%D%> is not a class member",
7614 parser
->scope
, name
);
7615 postfix_expression
= error_mark_node
;
7618 name
= build_qualified_name (/*type=*/NULL_TREE
,
7622 parser
->scope
= NULL_TREE
;
7623 parser
->qualifying_scope
= NULL_TREE
;
7624 parser
->object_scope
= NULL_TREE
;
7626 if (parser
->scope
&& name
&& BASELINK_P (name
))
7627 adjust_result_of_qualified_name_lookup
7628 (name
, parser
->scope
, scope
);
7630 = finish_class_member_access_expr (postfix_expression
, name
,
7632 tf_warning_or_error
);
7633 /* Build a location e.g.:
7636 where the caret is at the deref token, ranging from
7637 the start of postfix_expression to the end of the access expr. */
7639 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7640 location_t combined_loc
7641 = make_location (input_location
, start_loc
, end_loc
);
7642 protected_set_expr_location (postfix_expression
, combined_loc
);
7646 /* We no longer need to look up names in the scope of the object on
7647 the left-hand side of the `.' or `->' operator. */
7648 parser
->context
->object_type
= NULL_TREE
;
7650 /* Outside of offsetof, these operators may not appear in
7651 constant-expressions. */
7653 && (cp_parser_non_integral_constant_expression
7654 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7655 postfix_expression
= error_mark_node
;
7657 return postfix_expression
;
7660 /* Parse a parenthesized expression-list.
7663 assignment-expression
7664 expression-list, assignment-expression
7669 identifier, expression-list
7671 CAST_P is true if this expression is the target of a cast.
7673 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7676 WRAP_LOCATIONS_P is true if expressions within this list for which
7677 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7678 their source locations.
7680 Returns a vector of trees. Each element is a representation of an
7681 assignment-expression. NULL is returned if the ( and or ) are
7682 missing. An empty, but allocated, vector is returned on no
7683 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7684 if we are parsing an attribute list for an attribute that wants a
7685 plain identifier argument, normal_attr for an attribute that wants
7686 an expression, or non_attr if we aren't parsing an attribute list. If
7687 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7688 not all of the expressions in the list were constant.
7689 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7690 will be written to with the location of the closing parenthesis. If
7691 an error occurs, it may or may not be written to. */
7693 static vec
<tree
, va_gc
> *
7694 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7695 int is_attribute_list
,
7697 bool allow_expansion_p
,
7698 bool *non_constant_p
,
7699 location_t
*close_paren_loc
,
7700 bool wrap_locations_p
)
7702 vec
<tree
, va_gc
> *expression_list
;
7703 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7704 tree identifier
= NULL_TREE
;
7705 bool saved_greater_than_is_operator_p
;
7707 /* Assume all the expressions will be constant. */
7709 *non_constant_p
= false;
7711 matching_parens parens
;
7712 if (!parens
.require_open (parser
))
7715 expression_list
= make_tree_vector ();
7717 /* Within a parenthesized expression, a `>' token is always
7718 the greater-than operator. */
7719 saved_greater_than_is_operator_p
7720 = parser
->greater_than_is_operator_p
;
7721 parser
->greater_than_is_operator_p
= true;
7723 cp_expr
expr (NULL_TREE
);
7725 /* Consume expressions until there are no more. */
7726 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7729 /* At the beginning of attribute lists, check to see if the
7730 next token is an identifier. */
7731 if (is_attribute_list
== id_attr
7732 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7736 /* Consume the identifier. */
7737 token
= cp_lexer_consume_token (parser
->lexer
);
7738 /* Save the identifier. */
7739 identifier
= token
->u
.value
;
7743 bool expr_non_constant_p
;
7745 /* Parse the next assignment-expression. */
7746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7748 /* A braced-init-list. */
7749 cp_lexer_set_source_position (parser
->lexer
);
7750 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7751 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7752 if (non_constant_p
&& expr_non_constant_p
)
7753 *non_constant_p
= true;
7755 else if (non_constant_p
)
7757 expr
= (cp_parser_constant_expression
7758 (parser
, /*allow_non_constant_p=*/true,
7759 &expr_non_constant_p
));
7760 if (expr_non_constant_p
)
7761 *non_constant_p
= true;
7764 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7768 expr
= instantiate_non_dependent_expr (expr
);
7770 /* If we have an ellipsis, then this is an expression
7772 if (allow_expansion_p
7773 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7775 /* Consume the `...'. */
7776 cp_lexer_consume_token (parser
->lexer
);
7778 /* Build the argument pack. */
7779 expr
= make_pack_expansion (expr
);
7782 if (wrap_locations_p
)
7783 expr
.maybe_add_location_wrapper ();
7785 /* Add it to the list. We add error_mark_node
7786 expressions to the list, so that we can still tell if
7787 the correct form for a parenthesized expression-list
7788 is found. That gives better errors. */
7789 vec_safe_push (expression_list
, expr
.get_value ());
7791 if (expr
== error_mark_node
)
7795 /* After the first item, attribute lists look the same as
7796 expression lists. */
7797 is_attribute_list
= non_attr
;
7800 /* If the next token isn't a `,', then we are done. */
7801 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7804 /* Otherwise, consume the `,' and keep going. */
7805 cp_lexer_consume_token (parser
->lexer
);
7808 if (close_paren_loc
)
7809 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7811 if (!parens
.require_close (parser
))
7816 /* We try and resync to an unnested comma, as that will give the
7817 user better diagnostics. */
7818 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7819 /*recovering=*/true,
7821 /*consume_paren=*/true);
7826 parser
->greater_than_is_operator_p
7827 = saved_greater_than_is_operator_p
;
7832 parser
->greater_than_is_operator_p
7833 = saved_greater_than_is_operator_p
;
7836 vec_safe_insert (expression_list
, 0, identifier
);
7838 return expression_list
;
7841 /* Parse a pseudo-destructor-name.
7843 pseudo-destructor-name:
7844 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7845 :: [opt] nested-name-specifier template template-id :: ~ type-name
7846 :: [opt] nested-name-specifier [opt] ~ type-name
7848 If either of the first two productions is used, sets *SCOPE to the
7849 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7850 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7851 or ERROR_MARK_NODE if the parse fails. */
7854 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7859 bool nested_name_specifier_p
;
7862 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7863 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7864 && !type_dependent_expression_p (object
))
7866 if (cxx_dialect
< cxx14
)
7867 pedwarn (input_location
, 0,
7868 "%<~auto%> only available with "
7869 "-std=c++14 or -std=gnu++14");
7870 cp_lexer_consume_token (parser
->lexer
);
7871 cp_lexer_consume_token (parser
->lexer
);
7873 *type
= TREE_TYPE (object
);
7877 /* Assume that things will not work out. */
7878 *type
= error_mark_node
;
7880 /* Look for the optional `::' operator. */
7881 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7882 /* Look for the optional nested-name-specifier. */
7883 nested_name_specifier_p
7884 = (cp_parser_nested_name_specifier_opt (parser
,
7885 /*typename_keyword_p=*/false,
7886 /*check_dependency_p=*/true,
7888 /*is_declaration=*/false)
7890 /* Now, if we saw a nested-name-specifier, we might be doing the
7891 second production. */
7892 if (nested_name_specifier_p
7893 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7895 /* Consume the `template' keyword. */
7896 cp_lexer_consume_token (parser
->lexer
);
7897 /* Parse the template-id. */
7898 cp_parser_template_id (parser
,
7899 /*template_keyword_p=*/true,
7900 /*check_dependency_p=*/false,
7902 /*is_declaration=*/true);
7903 /* Look for the `::' token. */
7904 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7906 /* If the next token is not a `~', then there might be some
7907 additional qualification. */
7908 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7910 /* At this point, we're looking for "type-name :: ~". The type-name
7911 must not be a class-name, since this is a pseudo-destructor. So,
7912 it must be either an enum-name, or a typedef-name -- both of which
7913 are just identifiers. So, we peek ahead to check that the "::"
7914 and "~" tokens are present; if they are not, then we can avoid
7915 calling type_name. */
7916 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7917 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7918 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7920 cp_parser_error (parser
, "non-scalar type");
7924 /* Look for the type-name. */
7925 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7926 if (*scope
== error_mark_node
)
7929 /* Look for the `::' token. */
7930 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7935 /* Look for the `~'. */
7936 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7938 /* Once we see the ~, this has to be a pseudo-destructor. */
7939 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7940 cp_parser_commit_to_topmost_tentative_parse (parser
);
7942 /* Look for the type-name again. We are not responsible for
7943 checking that it matches the first type-name. */
7944 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7947 /* Parse a unary-expression.
7953 unary-operator cast-expression
7954 sizeof unary-expression
7956 alignof ( type-id ) [C++0x]
7963 __extension__ cast-expression
7964 __alignof__ unary-expression
7965 __alignof__ ( type-id )
7966 alignof unary-expression [C++0x]
7967 __real__ cast-expression
7968 __imag__ cast-expression
7970 sizeof ( type-id ) { initializer-list , [opt] }
7971 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7972 __alignof__ ( type-id ) { initializer-list , [opt] }
7974 ADDRESS_P is true iff the unary-expression is appearing as the
7975 operand of the `&' operator. CAST_P is true if this expression is
7976 the target of a cast.
7978 Returns a representation of the expression. */
7981 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7982 bool address_p
, bool cast_p
, bool decltype_p
)
7985 enum tree_code unary_operator
;
7987 /* Peek at the next token. */
7988 token
= cp_lexer_peek_token (parser
->lexer
);
7989 /* Some keywords give away the kind of expression. */
7990 if (token
->type
== CPP_KEYWORD
)
7992 enum rid keyword
= token
->keyword
;
8001 location_t start_loc
= token
->location
;
8003 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8004 /* Consume the token. */
8005 cp_lexer_consume_token (parser
->lexer
);
8006 /* Parse the operand. */
8007 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8009 if (TYPE_P (operand
))
8010 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
8013 /* ISO C++ defines alignof only with types, not with
8014 expressions. So pedwarn if alignof is used with a non-
8015 type expression. However, __alignof__ is ok. */
8016 if (id_equal (token
->u
.value
, "alignof"))
8017 pedwarn (token
->location
, OPT_Wpedantic
,
8018 "ISO C++ does not allow %<alignof%> "
8021 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8023 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8024 SIZEOF_EXPR with the original operand. */
8025 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8027 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8029 if (!processing_template_decl
&& TYPE_P (operand
))
8031 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8032 build1 (NOP_EXPR
, operand
,
8034 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8037 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8038 TREE_SIDE_EFFECTS (ret
) = 0;
8039 TREE_READONLY (ret
) = 1;
8043 /* Construct a location e.g. :
8046 with start == caret at the start of the "alignof"/"sizeof"
8047 token, with the endpoint at the final closing paren. */
8048 location_t finish_loc
8049 = cp_lexer_previous_token (parser
->lexer
)->location
;
8050 location_t compound_loc
8051 = make_location (start_loc
, start_loc
, finish_loc
);
8053 cp_expr
ret_expr (ret
);
8054 ret_expr
.set_location (compound_loc
);
8055 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8060 return cp_parser_new_expression (parser
);
8063 return cp_parser_delete_expression (parser
);
8067 /* The saved value of the PEDANTIC flag. */
8071 /* Save away the PEDANTIC flag. */
8072 cp_parser_extension_opt (parser
, &saved_pedantic
);
8073 /* Parse the cast-expression. */
8074 expr
= cp_parser_simple_cast_expression (parser
);
8075 /* Restore the PEDANTIC flag. */
8076 pedantic
= saved_pedantic
;
8086 /* Consume the `__real__' or `__imag__' token. */
8087 cp_lexer_consume_token (parser
->lexer
);
8088 /* Parse the cast-expression. */
8089 expression
= cp_parser_simple_cast_expression (parser
);
8090 /* Create the complete representation. */
8091 return build_x_unary_op (token
->location
,
8092 (keyword
== RID_REALPART
8093 ? REALPART_EXPR
: IMAGPART_EXPR
),
8095 tf_warning_or_error
);
8099 case RID_TRANSACTION_ATOMIC
:
8100 case RID_TRANSACTION_RELAXED
:
8101 return cp_parser_transaction_expression (parser
, keyword
);
8106 const char *saved_message
;
8107 bool saved_integral_constant_expression_p
;
8108 bool saved_non_integral_constant_expression_p
;
8109 bool saved_greater_than_is_operator_p
;
8111 location_t start_loc
= token
->location
;
8113 cp_lexer_consume_token (parser
->lexer
);
8114 matching_parens parens
;
8115 parens
.require_open (parser
);
8117 saved_message
= parser
->type_definition_forbidden_message
;
8118 parser
->type_definition_forbidden_message
8119 = G_("types may not be defined in %<noexcept%> expressions");
8121 saved_integral_constant_expression_p
8122 = parser
->integral_constant_expression_p
;
8123 saved_non_integral_constant_expression_p
8124 = parser
->non_integral_constant_expression_p
;
8125 parser
->integral_constant_expression_p
= false;
8127 saved_greater_than_is_operator_p
8128 = parser
->greater_than_is_operator_p
;
8129 parser
->greater_than_is_operator_p
= true;
8131 ++cp_unevaluated_operand
;
8132 ++c_inhibit_evaluation_warnings
;
8133 ++cp_noexcept_operand
;
8134 expr
= cp_parser_expression (parser
);
8135 --cp_noexcept_operand
;
8136 --c_inhibit_evaluation_warnings
;
8137 --cp_unevaluated_operand
;
8139 parser
->greater_than_is_operator_p
8140 = saved_greater_than_is_operator_p
;
8142 parser
->integral_constant_expression_p
8143 = saved_integral_constant_expression_p
;
8144 parser
->non_integral_constant_expression_p
8145 = saved_non_integral_constant_expression_p
;
8147 parser
->type_definition_forbidden_message
= saved_message
;
8149 location_t finish_loc
8150 = cp_lexer_peek_token (parser
->lexer
)->location
;
8151 parens
.require_close (parser
);
8153 /* Construct a location of the form:
8156 with start == caret, finishing at the close-paren. */
8157 location_t noexcept_loc
8158 = make_location (start_loc
, start_loc
, finish_loc
);
8160 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8169 /* Look for the `:: new' and `:: delete', which also signal the
8170 beginning of a new-expression, or delete-expression,
8171 respectively. If the next token is `::', then it might be one of
8173 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8177 /* See if the token after the `::' is one of the keywords in
8178 which we're interested. */
8179 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8180 /* If it's `new', we have a new-expression. */
8181 if (keyword
== RID_NEW
)
8182 return cp_parser_new_expression (parser
);
8183 /* Similarly, for `delete'. */
8184 else if (keyword
== RID_DELETE
)
8185 return cp_parser_delete_expression (parser
);
8188 /* Look for a unary operator. */
8189 unary_operator
= cp_parser_unary_operator (token
);
8190 /* The `++' and `--' operators can be handled similarly, even though
8191 they are not technically unary-operators in the grammar. */
8192 if (unary_operator
== ERROR_MARK
)
8194 if (token
->type
== CPP_PLUS_PLUS
)
8195 unary_operator
= PREINCREMENT_EXPR
;
8196 else if (token
->type
== CPP_MINUS_MINUS
)
8197 unary_operator
= PREDECREMENT_EXPR
;
8198 /* Handle the GNU address-of-label extension. */
8199 else if (cp_parser_allow_gnu_extensions_p (parser
)
8200 && token
->type
== CPP_AND_AND
)
8204 location_t start_loc
= token
->location
;
8206 /* Consume the '&&' token. */
8207 cp_lexer_consume_token (parser
->lexer
);
8208 /* Look for the identifier. */
8209 location_t finish_loc
8210 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8211 identifier
= cp_parser_identifier (parser
);
8212 /* Construct a location of the form:
8215 with caret==start at the "&&", finish at the end of the label. */
8216 location_t combined_loc
8217 = make_location (start_loc
, start_loc
, finish_loc
);
8218 /* Create an expression representing the address. */
8219 expression
= finish_label_address_expr (identifier
, combined_loc
);
8220 if (cp_parser_non_integral_constant_expression (parser
,
8222 expression
= error_mark_node
;
8226 if (unary_operator
!= ERROR_MARK
)
8228 cp_expr cast_expression
;
8229 cp_expr expression
= error_mark_node
;
8230 non_integral_constant non_constant_p
= NIC_NONE
;
8231 location_t loc
= token
->location
;
8232 tsubst_flags_t complain
= complain_flags (decltype_p
);
8234 /* Consume the operator token. */
8235 token
= cp_lexer_consume_token (parser
->lexer
);
8236 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8238 /* Parse the cast-expression. */
8240 = cp_parser_cast_expression (parser
,
8241 unary_operator
== ADDR_EXPR
,
8247 OP_TOKEN CAST_EXPRESSION
8248 ^~~~~~~~~~~~~~~~~~~~~~~~~
8249 with start==caret at the operator token, and
8250 extending to the end of the cast_expression. */
8251 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8253 /* Now, build an appropriate representation. */
8254 switch (unary_operator
)
8257 non_constant_p
= NIC_STAR
;
8258 expression
= build_x_indirect_ref (loc
, cast_expression
,
8261 /* TODO: build_x_indirect_ref does not always honor the
8262 location, so ensure it is set. */
8263 expression
.set_location (loc
);
8267 non_constant_p
= NIC_ADDR
;
8270 expression
= build_x_unary_op (loc
, unary_operator
,
8273 /* TODO: build_x_unary_op does not always honor the location,
8274 so ensure it is set. */
8275 expression
.set_location (loc
);
8278 case PREINCREMENT_EXPR
:
8279 case PREDECREMENT_EXPR
:
8280 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8281 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8284 /* Immediately fold negation of a constant, unless the constant is 0
8285 (since -0 == 0) or it would overflow. */
8286 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8287 && CONSTANT_CLASS_P (cast_expression
)
8288 && !integer_zerop (cast_expression
)
8289 && !TREE_OVERFLOW (cast_expression
))
8291 tree folded
= fold_build1 (unary_operator
,
8292 TREE_TYPE (cast_expression
),
8294 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8296 expression
= cp_expr (folded
, loc
);
8301 case UNARY_PLUS_EXPR
:
8302 case TRUTH_NOT_EXPR
:
8303 expression
= finish_unary_op_expr (loc
, unary_operator
,
8304 cast_expression
, complain
);
8311 if (non_constant_p
!= NIC_NONE
8312 && cp_parser_non_integral_constant_expression (parser
,
8314 expression
= error_mark_node
;
8319 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8320 /*member_access_only_p=*/false,
8325 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8326 unary-operator, the corresponding tree code is returned. */
8328 static enum tree_code
8329 cp_parser_unary_operator (cp_token
* token
)
8331 switch (token
->type
)
8334 return INDIRECT_REF
;
8340 return UNARY_PLUS_EXPR
;
8346 return TRUTH_NOT_EXPR
;
8349 return BIT_NOT_EXPR
;
8356 /* Parse a new-expression.
8359 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8360 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8362 Returns a representation of the expression. */
8365 cp_parser_new_expression (cp_parser
* parser
)
8367 bool global_scope_p
;
8368 vec
<tree
, va_gc
> *placement
;
8370 vec
<tree
, va_gc
> *initializer
;
8371 tree nelts
= NULL_TREE
;
8374 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8376 /* Look for the optional `::' operator. */
8378 = (cp_parser_global_scope_opt (parser
,
8379 /*current_scope_valid_p=*/false)
8381 /* Look for the `new' operator. */
8382 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8383 /* There's no easy way to tell a new-placement from the
8384 `( type-id )' construct. */
8385 cp_parser_parse_tentatively (parser
);
8386 /* Look for a new-placement. */
8387 placement
= cp_parser_new_placement (parser
);
8388 /* If that didn't work out, there's no new-placement. */
8389 if (!cp_parser_parse_definitely (parser
))
8391 if (placement
!= NULL
)
8392 release_tree_vector (placement
);
8396 /* If the next token is a `(', then we have a parenthesized
8398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8401 const char *saved_message
= parser
->type_definition_forbidden_message
;
8403 /* Consume the `('. */
8404 matching_parens parens
;
8405 parens
.consume_open (parser
);
8407 /* Parse the type-id. */
8408 parser
->type_definition_forbidden_message
8409 = G_("types may not be defined in a new-expression");
8411 type_id_in_expr_sentinel
s (parser
);
8412 type
= cp_parser_type_id (parser
);
8414 parser
->type_definition_forbidden_message
= saved_message
;
8416 /* Look for the closing `)'. */
8417 parens
.require_close (parser
);
8418 token
= cp_lexer_peek_token (parser
->lexer
);
8419 /* There should not be a direct-new-declarator in this production,
8420 but GCC used to allowed this, so we check and emit a sensible error
8421 message for this case. */
8422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8424 error_at (token
->location
,
8425 "array bound forbidden after parenthesized type-id");
8426 inform (token
->location
,
8427 "try removing the parentheses around the type-id");
8428 cp_parser_direct_new_declarator (parser
);
8431 /* Otherwise, there must be a new-type-id. */
8433 type
= cp_parser_new_type_id (parser
, &nelts
);
8435 /* If the next token is a `(' or '{', then we have a new-initializer. */
8436 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8437 if (token
->type
== CPP_OPEN_PAREN
8438 || token
->type
== CPP_OPEN_BRACE
)
8439 initializer
= cp_parser_new_initializer (parser
);
8443 /* A new-expression may not appear in an integral constant
8445 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8446 ret
= error_mark_node
;
8447 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8448 of a new-type-id or type-id of a new-expression, the new-expression shall
8449 contain a new-initializer of the form ( assignment-expression )".
8450 Additionally, consistently with the spirit of DR 1467, we want to accept
8451 'new auto { 2 }' too. */
8452 else if ((ret
= type_uses_auto (type
))
8453 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8454 && (vec_safe_length (initializer
) != 1
8455 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8456 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8458 error_at (token
->location
,
8459 "initialization of new-expression for type %<auto%> "
8460 "requires exactly one element");
8461 ret
= error_mark_node
;
8465 /* Construct a location e.g.:
8468 with caret == start at the start of the "new" token, and the end
8469 at the end of the final token we consumed. */
8470 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8471 location_t end_loc
= get_finish (end_tok
->location
);
8472 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8474 /* Create a representation of the new-expression. */
8475 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8476 tf_warning_or_error
);
8477 protected_set_expr_location (ret
, combined_loc
);
8480 if (placement
!= NULL
)
8481 release_tree_vector (placement
);
8482 if (initializer
!= NULL
)
8483 release_tree_vector (initializer
);
8488 /* Parse a new-placement.
8493 Returns the same representation as for an expression-list. */
8495 static vec
<tree
, va_gc
> *
8496 cp_parser_new_placement (cp_parser
* parser
)
8498 vec
<tree
, va_gc
> *expression_list
;
8500 /* Parse the expression-list. */
8501 expression_list
= (cp_parser_parenthesized_expression_list
8502 (parser
, non_attr
, /*cast_p=*/false,
8503 /*allow_expansion_p=*/true,
8504 /*non_constant_p=*/NULL
));
8506 if (expression_list
&& expression_list
->is_empty ())
8507 error ("expected expression-list or type-id");
8509 return expression_list
;
8512 /* Parse a new-type-id.
8515 type-specifier-seq new-declarator [opt]
8517 Returns the TYPE allocated. If the new-type-id indicates an array
8518 type, *NELTS is set to the number of elements in the last array
8519 bound; the TYPE will not include the last array bound. */
8522 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8524 cp_decl_specifier_seq type_specifier_seq
;
8525 cp_declarator
*new_declarator
;
8526 cp_declarator
*declarator
;
8527 cp_declarator
*outer_declarator
;
8528 const char *saved_message
;
8530 /* The type-specifier sequence must not contain type definitions.
8531 (It cannot contain declarations of new types either, but if they
8532 are not definitions we will catch that because they are not
8534 saved_message
= parser
->type_definition_forbidden_message
;
8535 parser
->type_definition_forbidden_message
8536 = G_("types may not be defined in a new-type-id");
8537 /* Parse the type-specifier-seq. */
8538 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8539 /*is_trailing_return=*/false,
8540 &type_specifier_seq
);
8541 /* Restore the old message. */
8542 parser
->type_definition_forbidden_message
= saved_message
;
8544 if (type_specifier_seq
.type
== error_mark_node
)
8545 return error_mark_node
;
8547 /* Parse the new-declarator. */
8548 new_declarator
= cp_parser_new_declarator_opt (parser
);
8550 /* Determine the number of elements in the last array dimension, if
8553 /* Skip down to the last array dimension. */
8554 declarator
= new_declarator
;
8555 outer_declarator
= NULL
;
8556 while (declarator
&& (declarator
->kind
== cdk_pointer
8557 || declarator
->kind
== cdk_ptrmem
))
8559 outer_declarator
= declarator
;
8560 declarator
= declarator
->declarator
;
8563 && declarator
->kind
== cdk_array
8564 && declarator
->declarator
8565 && declarator
->declarator
->kind
== cdk_array
)
8567 outer_declarator
= declarator
;
8568 declarator
= declarator
->declarator
;
8571 if (declarator
&& declarator
->kind
== cdk_array
)
8573 *nelts
= declarator
->u
.array
.bounds
;
8574 if (*nelts
== error_mark_node
)
8575 *nelts
= integer_one_node
;
8577 if (outer_declarator
)
8578 outer_declarator
->declarator
= declarator
->declarator
;
8580 new_declarator
= NULL
;
8583 return groktypename (&type_specifier_seq
, new_declarator
, false);
8586 /* Parse an (optional) new-declarator.
8589 ptr-operator new-declarator [opt]
8590 direct-new-declarator
8592 Returns the declarator. */
8594 static cp_declarator
*
8595 cp_parser_new_declarator_opt (cp_parser
* parser
)
8597 enum tree_code code
;
8598 tree type
, std_attributes
= NULL_TREE
;
8599 cp_cv_quals cv_quals
;
8601 /* We don't know if there's a ptr-operator next, or not. */
8602 cp_parser_parse_tentatively (parser
);
8603 /* Look for a ptr-operator. */
8604 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8605 /* If that worked, look for more new-declarators. */
8606 if (cp_parser_parse_definitely (parser
))
8608 cp_declarator
*declarator
;
8610 /* Parse another optional declarator. */
8611 declarator
= cp_parser_new_declarator_opt (parser
);
8613 declarator
= cp_parser_make_indirect_declarator
8614 (code
, type
, cv_quals
, declarator
, std_attributes
);
8619 /* If the next token is a `[', there is a direct-new-declarator. */
8620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8621 return cp_parser_direct_new_declarator (parser
);
8626 /* Parse a direct-new-declarator.
8628 direct-new-declarator:
8630 direct-new-declarator [constant-expression]
8634 static cp_declarator
*
8635 cp_parser_direct_new_declarator (cp_parser
* parser
)
8637 cp_declarator
*declarator
= NULL
;
8644 /* Look for the opening `['. */
8645 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8647 token
= cp_lexer_peek_token (parser
->lexer
);
8648 expression
= cp_parser_expression (parser
);
8649 /* The standard requires that the expression have integral
8650 type. DR 74 adds enumeration types. We believe that the
8651 real intent is that these expressions be handled like the
8652 expression in a `switch' condition, which also allows
8653 classes with a single conversion to integral or
8654 enumeration type. */
8655 if (!processing_template_decl
)
8658 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8663 error_at (token
->location
,
8664 "expression in new-declarator must have integral "
8665 "or enumeration type");
8666 expression
= error_mark_node
;
8670 /* Look for the closing `]'. */
8671 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8673 /* Add this bound to the declarator. */
8674 declarator
= make_array_declarator (declarator
, expression
);
8676 /* If the next token is not a `[', then there are no more
8678 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8685 /* Parse a new-initializer.
8688 ( expression-list [opt] )
8691 Returns a representation of the expression-list. */
8693 static vec
<tree
, va_gc
> *
8694 cp_parser_new_initializer (cp_parser
* parser
)
8696 vec
<tree
, va_gc
> *expression_list
;
8698 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8701 bool expr_non_constant_p
;
8702 cp_lexer_set_source_position (parser
->lexer
);
8703 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8704 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8705 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8706 expression_list
= make_tree_vector_single (t
);
8709 expression_list
= (cp_parser_parenthesized_expression_list
8710 (parser
, non_attr
, /*cast_p=*/false,
8711 /*allow_expansion_p=*/true,
8712 /*non_constant_p=*/NULL
));
8714 return expression_list
;
8717 /* Parse a delete-expression.
8720 :: [opt] delete cast-expression
8721 :: [opt] delete [ ] cast-expression
8723 Returns a representation of the expression. */
8726 cp_parser_delete_expression (cp_parser
* parser
)
8728 bool global_scope_p
;
8732 /* Look for the optional `::' operator. */
8734 = (cp_parser_global_scope_opt (parser
,
8735 /*current_scope_valid_p=*/false)
8737 /* Look for the `delete' keyword. */
8738 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8739 /* See if the array syntax is in use. */
8740 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8742 /* Consume the `[' token. */
8743 cp_lexer_consume_token (parser
->lexer
);
8744 /* Look for the `]' token. */
8745 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8746 /* Remember that this is the `[]' construct. */
8752 /* Parse the cast-expression. */
8753 expression
= cp_parser_simple_cast_expression (parser
);
8755 /* A delete-expression may not appear in an integral constant
8757 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8758 return error_mark_node
;
8760 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8761 tf_warning_or_error
);
8764 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8765 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8769 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8771 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8772 switch (token
->type
)
8778 case CPP_CLOSE_SQUARE
:
8779 case CPP_CLOSE_PAREN
:
8780 case CPP_CLOSE_BRACE
:
8781 case CPP_OPEN_BRACE
:
8785 case CPP_DEREF_STAR
:
8793 case CPP_GREATER_EQ
:
8814 case CPP_OPEN_PAREN
:
8815 /* In ((type ()) () the last () isn't a valid cast-expression,
8816 so the whole must be parsed as postfix-expression. */
8817 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8820 case CPP_OPEN_SQUARE
:
8821 /* '[' may start a primary-expression in obj-c++ and in C++11,
8822 as a lambda-expression, eg, '(void)[]{}'. */
8823 if (cxx_dialect
>= cxx11
)
8825 return c_dialect_objc ();
8828 case CPP_MINUS_MINUS
:
8829 /* '++' and '--' may or may not start a cast-expression:
8831 struct T { void operator++(int); };
8832 void f() { (T())++; }
8845 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8846 in the order: const_cast, static_cast, reinterpret_cast.
8848 Don't suggest dynamic_cast.
8850 Return the first legal cast kind found, or NULL otherwise. */
8853 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8857 /* Reuse the parser logic by attempting to build the various kinds of
8858 cast, with "complain" disabled.
8859 Identify the first such cast that is valid. */
8861 /* Don't attempt to run such logic within template processing. */
8862 if (processing_template_decl
)
8865 /* First try const_cast. */
8866 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8867 if (trial
!= error_mark_node
)
8868 return "const_cast";
8870 /* If that fails, try static_cast. */
8871 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8872 if (trial
!= error_mark_node
)
8873 return "static_cast";
8875 /* Finally, try reinterpret_cast. */
8876 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8877 if (trial
!= error_mark_node
)
8878 return "reinterpret_cast";
8880 /* No such cast possible. */
8884 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8885 suggesting how to convert a C-style cast of the form:
8889 to a C++-style cast.
8891 The primary range of RICHLOC is asssumed to be that of the original
8892 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8893 of the parens in the C-style cast. */
8896 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8897 location_t close_paren_loc
, tree orig_expr
,
8900 /* This function is non-trivial, so bail out now if the warning isn't
8901 going to be emitted. */
8902 if (!warn_old_style_cast
)
8905 /* Try to find a legal C++ cast, trying them in order:
8906 const_cast, static_cast, reinterpret_cast. */
8907 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8908 if (!cast_suggestion
)
8911 /* Replace the open paren with "CAST_SUGGESTION<". */
8913 pp_printf (&pp
, "%s<", cast_suggestion
);
8914 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8916 /* Replace the close paren with "> (". */
8917 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8919 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8920 rich_loc
->add_fixit_insert_after (")");
8924 /* Parse a cast-expression.
8928 ( type-id ) cast-expression
8930 ADDRESS_P is true iff the unary-expression is appearing as the
8931 operand of the `&' operator. CAST_P is true if this expression is
8932 the target of a cast.
8934 Returns a representation of the expression. */
8937 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8938 bool decltype_p
, cp_id_kind
* pidk
)
8940 /* If it's a `(', then we might be looking at a cast. */
8941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8943 tree type
= NULL_TREE
;
8944 cp_expr
expr (NULL_TREE
);
8945 int cast_expression
= 0;
8946 const char *saved_message
;
8948 /* There's no way to know yet whether or not this is a cast.
8949 For example, `(int (3))' is a unary-expression, while `(int)
8950 3' is a cast. So, we resort to parsing tentatively. */
8951 cp_parser_parse_tentatively (parser
);
8952 /* Types may not be defined in a cast. */
8953 saved_message
= parser
->type_definition_forbidden_message
;
8954 parser
->type_definition_forbidden_message
8955 = G_("types may not be defined in casts");
8956 /* Consume the `('. */
8957 matching_parens parens
;
8958 cp_token
*open_paren
= parens
.consume_open (parser
);
8959 location_t open_paren_loc
= open_paren
->location
;
8960 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8962 /* A very tricky bit is that `(struct S) { 3 }' is a
8963 compound-literal (which we permit in C++ as an extension).
8964 But, that construct is not a cast-expression -- it is a
8965 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8966 is legal; if the compound-literal were a cast-expression,
8967 you'd need an extra set of parentheses.) But, if we parse
8968 the type-id, and it happens to be a class-specifier, then we
8969 will commit to the parse at that point, because we cannot
8970 undo the action that is done when creating a new class. So,
8971 then we cannot back up and do a postfix-expression.
8973 Another tricky case is the following (c++/29234):
8975 struct S { void operator () (); };
8982 As a type-id we parse the parenthesized S()() as a function
8983 returning a function, groktypename complains and we cannot
8984 back up in this case either.
8986 Therefore, we scan ahead to the closing `)', and check to see
8987 if the tokens after the `)' can start a cast-expression. Otherwise
8988 we are dealing with an unary-expression, a postfix-expression
8991 Yet another tricky case, in C++11, is the following (c++/54891):
8995 The issue is that usually, besides the case of lambda-expressions,
8996 the parenthesized type-id cannot be followed by '[', and, eg, we
8997 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8998 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8999 we don't commit, we try a cast-expression, then an unary-expression.
9001 Save tokens so that we can put them back. */
9002 cp_lexer_save_tokens (parser
->lexer
);
9004 /* We may be looking at a cast-expression. */
9005 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9006 /*consume_paren=*/true))
9008 = cp_parser_tokens_start_cast_expression (parser
);
9010 /* Roll back the tokens we skipped. */
9011 cp_lexer_rollback_tokens (parser
->lexer
);
9012 /* If we aren't looking at a cast-expression, simulate an error so
9013 that the call to cp_parser_error_occurred below returns true. */
9014 if (!cast_expression
)
9015 cp_parser_simulate_error (parser
);
9018 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9019 parser
->in_type_id_in_expr_p
= true;
9020 /* Look for the type-id. */
9021 type
= cp_parser_type_id (parser
);
9022 /* Look for the closing `)'. */
9023 cp_token
*close_paren
= parens
.require_close (parser
);
9025 close_paren_loc
= close_paren
->location
;
9026 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9029 /* Restore the saved message. */
9030 parser
->type_definition_forbidden_message
= saved_message
;
9032 /* At this point this can only be either a cast or a
9033 parenthesized ctor such as `(T ())' that looks like a cast to
9034 function returning T. */
9035 if (!cp_parser_error_occurred (parser
))
9037 /* Only commit if the cast-expression doesn't start with
9038 '++', '--', or '[' in C++11. */
9039 if (cast_expression
> 0)
9040 cp_parser_commit_to_topmost_tentative_parse (parser
);
9042 expr
= cp_parser_cast_expression (parser
,
9043 /*address_p=*/false,
9045 /*decltype_p=*/false,
9048 if (cp_parser_parse_definitely (parser
))
9050 /* Warn about old-style casts, if so requested. */
9051 if (warn_old_style_cast
9052 && !in_system_header_at (input_location
)
9053 && !VOID_TYPE_P (type
)
9054 && current_lang_name
!= lang_name_c
)
9056 gcc_rich_location
rich_loc (input_location
);
9057 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9059 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9060 "use of old-style cast to %q#T", type
);
9063 /* Only type conversions to integral or enumeration types
9064 can be used in constant-expressions. */
9065 if (!cast_valid_in_integral_constant_expression_p (type
)
9066 && cp_parser_non_integral_constant_expression (parser
,
9068 return error_mark_node
;
9070 /* Perform the cast. */
9074 with start==caret at the open paren, extending to the
9076 location_t cast_loc
= make_location (open_paren_loc
,
9078 expr
.get_finish ());
9079 expr
= build_c_cast (cast_loc
, type
, expr
);
9084 cp_parser_abort_tentative_parse (parser
);
9087 /* If we get here, then it's not a cast, so it must be a
9088 unary-expression. */
9089 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9090 cast_p
, decltype_p
);
9093 /* Parse a binary expression of the general form:
9097 pm-expression .* cast-expression
9098 pm-expression ->* cast-expression
9100 multiplicative-expression:
9102 multiplicative-expression * pm-expression
9103 multiplicative-expression / pm-expression
9104 multiplicative-expression % pm-expression
9106 additive-expression:
9107 multiplicative-expression
9108 additive-expression + multiplicative-expression
9109 additive-expression - multiplicative-expression
9113 shift-expression << additive-expression
9114 shift-expression >> additive-expression
9116 relational-expression:
9118 relational-expression < shift-expression
9119 relational-expression > shift-expression
9120 relational-expression <= shift-expression
9121 relational-expression >= shift-expression
9125 relational-expression:
9126 relational-expression <? shift-expression
9127 relational-expression >? shift-expression
9129 equality-expression:
9130 relational-expression
9131 equality-expression == relational-expression
9132 equality-expression != relational-expression
9136 and-expression & equality-expression
9138 exclusive-or-expression:
9140 exclusive-or-expression ^ and-expression
9142 inclusive-or-expression:
9143 exclusive-or-expression
9144 inclusive-or-expression | exclusive-or-expression
9146 logical-and-expression:
9147 inclusive-or-expression
9148 logical-and-expression && inclusive-or-expression
9150 logical-or-expression:
9151 logical-and-expression
9152 logical-or-expression || logical-and-expression
9154 All these are implemented with a single function like:
9157 simple-cast-expression
9158 binary-expression <token> binary-expression
9160 CAST_P is true if this expression is the target of a cast.
9162 The binops_by_token map is used to get the tree codes for each <token> type.
9163 binary-expressions are associated according to a precedence table. */
9165 #define TOKEN_PRECEDENCE(token) \
9166 (((token->type == CPP_GREATER \
9167 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9168 && !parser->greater_than_is_operator_p) \
9169 ? PREC_NOT_OPERATOR \
9170 : binops_by_token[token->type].prec)
9173 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9174 bool no_toplevel_fold_p
,
9176 enum cp_parser_prec prec
,
9179 cp_parser_expression_stack stack
;
9180 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9181 cp_parser_expression_stack_entry current
;
9184 enum tree_code rhs_type
;
9185 enum cp_parser_prec new_prec
, lookahead_prec
;
9188 /* Parse the first expression. */
9189 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9190 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9191 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9192 cast_p
, decltype_p
, pidk
);
9193 current
.prec
= prec
;
9195 if (cp_parser_error_occurred (parser
))
9196 return error_mark_node
;
9200 /* Get an operator token. */
9201 token
= cp_lexer_peek_token (parser
->lexer
);
9203 if (warn_cxx11_compat
9204 && token
->type
== CPP_RSHIFT
9205 && !parser
->greater_than_is_operator_p
)
9207 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9208 "%<>>%> operator is treated"
9209 " as two right angle brackets in C++11"))
9210 inform (token
->location
,
9211 "suggest parentheses around %<>>%> expression");
9214 new_prec
= TOKEN_PRECEDENCE (token
);
9215 if (new_prec
!= PREC_NOT_OPERATOR
9216 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9217 /* This is a fold-expression; handle it later. */
9218 new_prec
= PREC_NOT_OPERATOR
;
9220 /* Popping an entry off the stack means we completed a subexpression:
9221 - either we found a token which is not an operator (`>' where it is not
9222 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9223 will happen repeatedly;
9224 - or, we found an operator which has lower priority. This is the case
9225 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9227 if (new_prec
<= current
.prec
)
9236 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9237 current
.loc
= token
->location
;
9239 /* We used the operator token. */
9240 cp_lexer_consume_token (parser
->lexer
);
9242 /* For "false && x" or "true || x", x will never be executed;
9243 disable warnings while evaluating it. */
9244 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9245 c_inhibit_evaluation_warnings
+=
9246 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9247 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9248 c_inhibit_evaluation_warnings
+=
9249 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9251 /* Extract another operand. It may be the RHS of this expression
9252 or the LHS of a new, higher priority expression. */
9253 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9254 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9255 rhs
= cp_parser_simple_cast_expression (parser
);
9257 /* Get another operator token. Look up its precedence to avoid
9258 building a useless (immediately popped) stack entry for common
9259 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9260 token
= cp_lexer_peek_token (parser
->lexer
);
9261 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9262 if (lookahead_prec
!= PREC_NOT_OPERATOR
9263 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9264 lookahead_prec
= PREC_NOT_OPERATOR
;
9265 if (lookahead_prec
> new_prec
)
9267 /* ... and prepare to parse the RHS of the new, higher priority
9268 expression. Since precedence levels on the stack are
9269 monotonically increasing, we do not have to care about
9274 current
.lhs_type
= rhs_type
;
9275 current
.prec
= new_prec
;
9276 new_prec
= lookahead_prec
;
9280 lookahead_prec
= new_prec
;
9281 /* If the stack is not empty, we have parsed into LHS the right side
9282 (`4' in the example above) of an expression we had suspended.
9283 We can use the information on the stack to recover the LHS (`3')
9284 from the stack together with the tree code (`MULT_EXPR'), and
9285 the precedence of the higher level subexpression
9286 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9287 which will be used to actually build the additive expression. */
9289 rhs_type
= current
.lhs_type
;
9294 /* Undo the disabling of warnings done above. */
9295 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9296 c_inhibit_evaluation_warnings
-=
9297 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9298 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9299 c_inhibit_evaluation_warnings
-=
9300 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9302 if (warn_logical_not_paren
9303 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9304 && current
.lhs_type
== TRUTH_NOT_EXPR
9305 /* Avoid warning for !!x == y. */
9306 && (TREE_CODE (current
.lhs
) != NE_EXPR
9307 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9308 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9309 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9310 /* Avoid warning for !b == y where b is boolean. */
9311 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9312 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9314 /* Avoid warning for !!b == y where b is boolean. */
9315 && (!DECL_P (current
.lhs
)
9316 || TREE_TYPE (current
.lhs
) == NULL_TREE
9317 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9318 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9319 current
.lhs
, maybe_constant_value (rhs
));
9323 location_t combined_loc
= make_location (current
.loc
,
9324 current
.lhs
.get_start (),
9327 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9328 ERROR_MARK for everything that is not a binary expression.
9329 This makes warn_about_parentheses miss some warnings that
9330 involve unary operators. For unary expressions we should
9331 pass the correct tree_code unless the unary expression was
9332 surrounded by parentheses.
9334 if (no_toplevel_fold_p
9335 && lookahead_prec
<= current
.prec
9338 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9339 current
.lhs
= error_mark_node
;
9343 = build_min (current
.tree_type
,
9344 TREE_CODE_CLASS (current
.tree_type
)
9346 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9347 current
.lhs
.get_value (), rhs
.get_value ());
9348 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9353 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9354 current
.lhs
, current
.lhs_type
,
9355 rhs
, rhs_type
, &overload
,
9356 complain_flags (decltype_p
));
9357 /* TODO: build_x_binary_op doesn't always honor the location. */
9358 current
.lhs
.set_location (combined_loc
);
9360 current
.lhs_type
= current
.tree_type
;
9362 /* If the binary operator required the use of an overloaded operator,
9363 then this expression cannot be an integral constant-expression.
9364 An overloaded operator can be used even if both operands are
9365 otherwise permissible in an integral constant-expression if at
9366 least one of the operands is of enumeration type. */
9369 && cp_parser_non_integral_constant_expression (parser
,
9371 return error_mark_node
;
9378 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9379 bool no_toplevel_fold_p
,
9380 enum cp_parser_prec prec
,
9383 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9384 /*decltype*/false, prec
, pidk
);
9387 /* Parse the `? expression : assignment-expression' part of a
9388 conditional-expression. The LOGICAL_OR_EXPR is the
9389 logical-or-expression that started the conditional-expression.
9390 Returns a representation of the entire conditional-expression.
9392 This routine is used by cp_parser_assignment_expression.
9394 ? expression : assignment-expression
9398 ? : assignment-expression */
9401 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9403 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9404 cp_expr assignment_expr
;
9405 struct cp_token
*token
;
9406 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9408 /* Consume the `?' token. */
9409 cp_lexer_consume_token (parser
->lexer
);
9410 token
= cp_lexer_peek_token (parser
->lexer
);
9411 if (cp_parser_allow_gnu_extensions_p (parser
)
9412 && token
->type
== CPP_COLON
)
9414 pedwarn (token
->location
, OPT_Wpedantic
,
9415 "ISO C++ does not allow ?: with omitted middle operand");
9416 /* Implicit true clause. */
9418 c_inhibit_evaluation_warnings
+=
9419 folded_logical_or_expr
== truthvalue_true_node
;
9420 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9424 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9425 parser
->colon_corrects_to_scope_p
= false;
9426 /* Parse the expression. */
9427 c_inhibit_evaluation_warnings
+=
9428 folded_logical_or_expr
== truthvalue_false_node
;
9429 expr
= cp_parser_expression (parser
);
9430 c_inhibit_evaluation_warnings
+=
9431 ((folded_logical_or_expr
== truthvalue_true_node
)
9432 - (folded_logical_or_expr
== truthvalue_false_node
));
9433 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9436 /* The next token should be a `:'. */
9437 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9438 /* Parse the assignment-expression. */
9439 assignment_expr
= cp_parser_assignment_expression (parser
);
9440 c_inhibit_evaluation_warnings
-=
9441 folded_logical_or_expr
== truthvalue_true_node
;
9444 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9445 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9446 with the caret at the "?", ranging from the start of
9447 the logical_or_expr to the end of the assignment_expr. */
9448 loc
= make_location (loc
,
9449 logical_or_expr
.get_start (),
9450 assignment_expr
.get_finish ());
9452 /* Build the conditional-expression. */
9453 return build_x_conditional_expr (loc
, logical_or_expr
,
9456 tf_warning_or_error
);
9459 /* Parse an assignment-expression.
9461 assignment-expression:
9462 conditional-expression
9463 logical-or-expression assignment-operator assignment_expression
9466 CAST_P is true if this expression is the target of a cast.
9467 DECLTYPE_P is true if this expression is the operand of decltype.
9469 Returns a representation for the expression. */
9472 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9473 bool cast_p
, bool decltype_p
)
9477 /* If the next token is the `throw' keyword, then we're looking at
9478 a throw-expression. */
9479 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9480 expr
= cp_parser_throw_expression (parser
);
9481 /* Otherwise, it must be that we are looking at a
9482 logical-or-expression. */
9485 /* Parse the binary expressions (logical-or-expression). */
9486 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9488 PREC_NOT_OPERATOR
, pidk
);
9489 /* If the next token is a `?' then we're actually looking at a
9490 conditional-expression. */
9491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9492 return cp_parser_question_colon_clause (parser
, expr
);
9495 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9497 /* If it's an assignment-operator, we're using the second
9499 enum tree_code assignment_operator
9500 = cp_parser_assignment_operator_opt (parser
);
9501 if (assignment_operator
!= ERROR_MARK
)
9503 bool non_constant_p
;
9505 /* Parse the right-hand side of the assignment. */
9506 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9509 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9510 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9512 /* An assignment may not appear in a
9513 constant-expression. */
9514 if (cp_parser_non_integral_constant_expression (parser
,
9516 return error_mark_node
;
9517 /* Build the assignment expression. Its default
9521 is the location of the '=' token as the
9522 caret, ranging from the start of the lhs to the
9524 loc
= make_location (loc
,
9527 expr
= build_x_modify_expr (loc
, expr
,
9528 assignment_operator
,
9530 complain_flags (decltype_p
));
9531 /* TODO: build_x_modify_expr doesn't honor the location,
9532 so we must set it here. */
9533 expr
.set_location (loc
);
9541 /* Parse an (optional) assignment-operator.
9543 assignment-operator: one of
9544 = *= /= %= += -= >>= <<= &= ^= |=
9548 assignment-operator: one of
9551 If the next token is an assignment operator, the corresponding tree
9552 code is returned, and the token is consumed. For example, for
9553 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9554 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9555 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9556 operator, ERROR_MARK is returned. */
9558 static enum tree_code
9559 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9564 /* Peek at the next token. */
9565 token
= cp_lexer_peek_token (parser
->lexer
);
9567 switch (token
->type
)
9578 op
= TRUNC_DIV_EXPR
;
9582 op
= TRUNC_MOD_EXPR
;
9614 /* Nothing else is an assignment operator. */
9618 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9619 if (op
!= ERROR_MARK
9620 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9623 /* If it was an assignment operator, consume it. */
9624 if (op
!= ERROR_MARK
)
9625 cp_lexer_consume_token (parser
->lexer
);
9630 /* Parse an expression.
9633 assignment-expression
9634 expression , assignment-expression
9636 CAST_P is true if this expression is the target of a cast.
9637 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9638 except possibly parenthesized or on the RHS of a comma (N3276).
9640 Returns a representation of the expression. */
9643 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9644 bool cast_p
, bool decltype_p
)
9646 cp_expr expression
= NULL_TREE
;
9647 location_t loc
= UNKNOWN_LOCATION
;
9651 cp_expr assignment_expression
;
9653 /* Parse the next assignment-expression. */
9654 assignment_expression
9655 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9657 /* We don't create a temporary for a call that is the immediate operand
9658 of decltype or on the RHS of a comma. But when we see a comma, we
9659 need to create a temporary for a call on the LHS. */
9660 if (decltype_p
&& !processing_template_decl
9661 && TREE_CODE (assignment_expression
) == CALL_EXPR
9662 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9663 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9664 assignment_expression
9665 = build_cplus_new (TREE_TYPE (assignment_expression
),
9666 assignment_expression
, tf_warning_or_error
);
9668 /* If this is the first assignment-expression, we can just
9671 expression
= assignment_expression
;
9674 /* Create a location with caret at the comma, ranging
9675 from the start of the LHS to the end of the RHS. */
9676 loc
= make_location (loc
,
9677 expression
.get_start (),
9678 assignment_expression
.get_finish ());
9679 expression
= build_x_compound_expr (loc
, expression
,
9680 assignment_expression
,
9681 complain_flags (decltype_p
));
9682 expression
.set_location (loc
);
9684 /* If the next token is not a comma, or we're in a fold-expression, then
9685 we are done with the expression. */
9686 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9687 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9689 /* Consume the `,'. */
9690 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9691 cp_lexer_consume_token (parser
->lexer
);
9692 /* A comma operator cannot appear in a constant-expression. */
9693 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9694 expression
= error_mark_node
;
9700 /* Parse a constant-expression.
9702 constant-expression:
9703 conditional-expression
9705 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9706 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9707 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9708 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9709 only parse a conditional-expression, otherwise parse an
9710 assignment-expression. See below for rationale. */
9713 cp_parser_constant_expression (cp_parser
* parser
,
9714 bool allow_non_constant_p
,
9715 bool *non_constant_p
,
9718 bool saved_integral_constant_expression_p
;
9719 bool saved_allow_non_integral_constant_expression_p
;
9720 bool saved_non_integral_constant_expression_p
;
9723 /* It might seem that we could simply parse the
9724 conditional-expression, and then check to see if it were
9725 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9726 one that the compiler can figure out is constant, possibly after
9727 doing some simplifications or optimizations. The standard has a
9728 precise definition of constant-expression, and we must honor
9729 that, even though it is somewhat more restrictive.
9735 is not a legal declaration, because `(2, 3)' is not a
9736 constant-expression. The `,' operator is forbidden in a
9737 constant-expression. However, GCC's constant-folding machinery
9738 will fold this operation to an INTEGER_CST for `3'. */
9740 /* Save the old settings. */
9741 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9742 saved_allow_non_integral_constant_expression_p
9743 = parser
->allow_non_integral_constant_expression_p
;
9744 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9745 /* We are now parsing a constant-expression. */
9746 parser
->integral_constant_expression_p
= true;
9747 parser
->allow_non_integral_constant_expression_p
9748 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9749 parser
->non_integral_constant_expression_p
= false;
9750 /* Although the grammar says "conditional-expression", when not STRICT_P,
9751 we parse an "assignment-expression", which also permits
9752 "throw-expression" and the use of assignment operators. In the case
9753 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9754 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9755 actually essential that we look for an assignment-expression.
9756 For example, cp_parser_initializer_clauses uses this function to
9757 determine whether a particular assignment-expression is in fact
9761 /* Parse the binary expressions (logical-or-expression). */
9762 expression
= cp_parser_binary_expression (parser
, false, false, false,
9763 PREC_NOT_OPERATOR
, NULL
);
9764 /* If the next token is a `?' then we're actually looking at
9765 a conditional-expression; otherwise we're done. */
9766 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9767 expression
= cp_parser_question_colon_clause (parser
, expression
);
9770 expression
= cp_parser_assignment_expression (parser
);
9771 /* Restore the old settings. */
9772 parser
->integral_constant_expression_p
9773 = saved_integral_constant_expression_p
;
9774 parser
->allow_non_integral_constant_expression_p
9775 = saved_allow_non_integral_constant_expression_p
;
9776 if (cxx_dialect
>= cxx11
)
9778 /* Require an rvalue constant expression here; that's what our
9779 callers expect. Reference constant expressions are handled
9780 separately in e.g. cp_parser_template_argument. */
9781 tree decay
= expression
;
9782 if (TREE_TYPE (expression
)
9783 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9784 decay
= build_address (expression
);
9785 bool is_const
= potential_rvalue_constant_expression (decay
);
9786 parser
->non_integral_constant_expression_p
= !is_const
;
9787 if (!is_const
&& !allow_non_constant_p
)
9788 require_potential_rvalue_constant_expression (decay
);
9790 if (allow_non_constant_p
)
9791 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9792 parser
->non_integral_constant_expression_p
9793 = saved_non_integral_constant_expression_p
;
9798 /* Parse __builtin_offsetof.
9800 offsetof-expression:
9801 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9803 offsetof-member-designator:
9805 | offsetof-member-designator "." id-expression
9806 | offsetof-member-designator "[" expression "]"
9807 | offsetof-member-designator "->" id-expression */
9810 cp_parser_builtin_offsetof (cp_parser
*parser
)
9812 int save_ice_p
, save_non_ice_p
;
9817 location_t finish_loc
;
9819 /* We're about to accept non-integral-constant things, but will
9820 definitely yield an integral constant expression. Save and
9821 restore these values around our local parsing. */
9822 save_ice_p
= parser
->integral_constant_expression_p
;
9823 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9825 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9827 /* Consume the "__builtin_offsetof" token. */
9828 cp_lexer_consume_token (parser
->lexer
);
9829 /* Consume the opening `('. */
9830 matching_parens parens
;
9831 parens
.require_open (parser
);
9832 /* Parse the type-id. */
9833 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9834 type
= cp_parser_type_id (parser
);
9835 /* Look for the `,'. */
9836 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9837 token
= cp_lexer_peek_token (parser
->lexer
);
9839 /* Build the (type *)null that begins the traditional offsetof macro. */
9841 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9842 tf_warning_or_error
);
9844 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9845 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9846 true, &dummy
, token
->location
);
9849 token
= cp_lexer_peek_token (parser
->lexer
);
9850 switch (token
->type
)
9852 case CPP_OPEN_SQUARE
:
9853 /* offsetof-member-designator "[" expression "]" */
9854 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9859 /* offsetof-member-designator "->" identifier */
9860 expr
= grok_array_decl (token
->location
, expr
,
9861 integer_zero_node
, false);
9865 /* offsetof-member-designator "." identifier */
9866 cp_lexer_consume_token (parser
->lexer
);
9867 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9872 case CPP_CLOSE_PAREN
:
9873 /* Consume the ")" token. */
9874 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9875 cp_lexer_consume_token (parser
->lexer
);
9879 /* Error. We know the following require will fail, but
9880 that gives the proper error message. */
9881 parens
.require_close (parser
);
9882 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9883 expr
= error_mark_node
;
9889 /* Make a location of the form:
9890 __builtin_offsetof (struct s, f)
9891 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9892 with caret at the type-id, ranging from the start of the
9893 "_builtin_offsetof" token to the close paren. */
9894 loc
= make_location (loc
, start_loc
, finish_loc
);
9895 /* The result will be an INTEGER_CST, so we need to explicitly
9896 preserve the location. */
9897 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9900 parser
->integral_constant_expression_p
= save_ice_p
;
9901 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9903 expr
= expr
.maybe_add_location_wrapper ();
9907 /* Parse a trait expression.
9909 Returns a representation of the expression, the underlying type
9910 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9913 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9916 tree type1
, type2
= NULL_TREE
;
9917 bool binary
= false;
9918 bool variadic
= false;
9922 case RID_HAS_NOTHROW_ASSIGN
:
9923 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9925 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9926 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9928 case RID_HAS_NOTHROW_COPY
:
9929 kind
= CPTK_HAS_NOTHROW_COPY
;
9931 case RID_HAS_TRIVIAL_ASSIGN
:
9932 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9934 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9935 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9937 case RID_HAS_TRIVIAL_COPY
:
9938 kind
= CPTK_HAS_TRIVIAL_COPY
;
9940 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9941 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9943 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9944 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9946 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9947 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9949 case RID_IS_ABSTRACT
:
9950 kind
= CPTK_IS_ABSTRACT
;
9952 case RID_IS_AGGREGATE
:
9953 kind
= CPTK_IS_AGGREGATE
;
9955 case RID_IS_BASE_OF
:
9956 kind
= CPTK_IS_BASE_OF
;
9960 kind
= CPTK_IS_CLASS
;
9963 kind
= CPTK_IS_EMPTY
;
9966 kind
= CPTK_IS_ENUM
;
9969 kind
= CPTK_IS_FINAL
;
9971 case RID_IS_LITERAL_TYPE
:
9972 kind
= CPTK_IS_LITERAL_TYPE
;
9977 case RID_IS_POLYMORPHIC
:
9978 kind
= CPTK_IS_POLYMORPHIC
;
9980 case RID_IS_SAME_AS
:
9981 kind
= CPTK_IS_SAME_AS
;
9984 case RID_IS_STD_LAYOUT
:
9985 kind
= CPTK_IS_STD_LAYOUT
;
9987 case RID_IS_TRIVIAL
:
9988 kind
= CPTK_IS_TRIVIAL
;
9990 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9991 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9994 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9995 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9998 case RID_IS_TRIVIALLY_COPYABLE
:
9999 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10002 kind
= CPTK_IS_UNION
;
10004 case RID_UNDERLYING_TYPE
:
10005 kind
= CPTK_UNDERLYING_TYPE
;
10010 case RID_DIRECT_BASES
:
10011 kind
= CPTK_DIRECT_BASES
;
10013 case RID_IS_ASSIGNABLE
:
10014 kind
= CPTK_IS_ASSIGNABLE
;
10017 case RID_IS_CONSTRUCTIBLE
:
10018 kind
= CPTK_IS_CONSTRUCTIBLE
;
10022 gcc_unreachable ();
10025 /* Get location of initial token. */
10026 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10028 /* Consume the token. */
10029 cp_lexer_consume_token (parser
->lexer
);
10031 matching_parens parens
;
10032 parens
.require_open (parser
);
10035 type_id_in_expr_sentinel
s (parser
);
10036 type1
= cp_parser_type_id (parser
);
10039 if (type1
== error_mark_node
)
10040 return error_mark_node
;
10044 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10047 type_id_in_expr_sentinel
s (parser
);
10048 type2
= cp_parser_type_id (parser
);
10051 if (type2
== error_mark_node
)
10052 return error_mark_node
;
10056 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10058 cp_lexer_consume_token (parser
->lexer
);
10059 tree elt
= cp_parser_type_id (parser
);
10060 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10062 cp_lexer_consume_token (parser
->lexer
);
10063 elt
= make_pack_expansion (elt
);
10065 if (elt
== error_mark_node
)
10066 return error_mark_node
;
10067 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10071 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10072 parens
.require_close (parser
);
10074 /* Construct a location of the form:
10075 __is_trivially_copyable(_Tp)
10076 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10077 with start == caret, finishing at the close-paren. */
10078 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10080 /* Complete the trait expression, which may mean either processing
10081 the trait expr now or saving it for template instantiation. */
10084 case CPTK_UNDERLYING_TYPE
:
10085 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10087 return cp_expr (finish_bases (type1
, false), trait_loc
);
10088 case CPTK_DIRECT_BASES
:
10089 return cp_expr (finish_bases (type1
, true), trait_loc
);
10091 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10095 /* Parse a lambda expression.
10098 lambda-introducer lambda-declarator [opt] compound-statement
10100 Returns a representation of the expression. */
10103 cp_parser_lambda_expression (cp_parser
* parser
)
10105 tree lambda_expr
= build_lambda_expr ();
10108 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10109 cp_token_position start
= 0;
10111 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10113 if (cp_unevaluated_operand
)
10115 if (!token
->error_reported
)
10117 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10118 "lambda-expression in unevaluated context");
10119 token
->error_reported
= true;
10123 else if (parser
->in_template_argument_list_p
)
10125 if (!token
->error_reported
)
10127 error_at (token
->location
, "lambda-expression in template-argument");
10128 token
->error_reported
= true;
10133 /* We may be in the middle of deferred access check. Disable
10135 push_deferring_access_checks (dk_no_deferred
);
10137 cp_parser_lambda_introducer (parser
, lambda_expr
);
10139 type
= begin_lambda_type (lambda_expr
);
10140 if (type
== error_mark_node
)
10141 return error_mark_node
;
10143 record_lambda_scope (lambda_expr
);
10145 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10146 determine_visibility (TYPE_NAME (type
));
10148 /* Now that we've started the type, add the capture fields for any
10149 explicit captures. */
10150 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10153 /* Inside the class, surrounding template-parameter-lists do not apply. */
10154 unsigned int saved_num_template_parameter_lists
10155 = parser
->num_template_parameter_lists
;
10156 unsigned char in_statement
= parser
->in_statement
;
10157 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10158 bool fully_implicit_function_template_p
10159 = parser
->fully_implicit_function_template_p
;
10160 tree implicit_template_parms
= parser
->implicit_template_parms
;
10161 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10162 bool auto_is_implicit_function_template_parm_p
10163 = parser
->auto_is_implicit_function_template_parm_p
;
10165 parser
->num_template_parameter_lists
= 0;
10166 parser
->in_statement
= 0;
10167 parser
->in_switch_statement_p
= false;
10168 parser
->fully_implicit_function_template_p
= false;
10169 parser
->implicit_template_parms
= 0;
10170 parser
->implicit_template_scope
= 0;
10171 parser
->auto_is_implicit_function_template_parm_p
= false;
10173 /* By virtue of defining a local class, a lambda expression has access to
10174 the private variables of enclosing classes. */
10176 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10178 if (ok
&& cp_parser_error_occurred (parser
))
10183 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10184 && cp_parser_start_tentative_firewall (parser
))
10186 cp_parser_lambda_body (parser
, lambda_expr
);
10188 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10190 if (cp_parser_skip_to_closing_brace (parser
))
10191 cp_lexer_consume_token (parser
->lexer
);
10194 /* The capture list was built up in reverse order; fix that now. */
10195 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10196 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10199 maybe_add_lambda_conv_op (type
);
10201 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10203 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10204 parser
->in_statement
= in_statement
;
10205 parser
->in_switch_statement_p
= in_switch_statement_p
;
10206 parser
->fully_implicit_function_template_p
10207 = fully_implicit_function_template_p
;
10208 parser
->implicit_template_parms
= implicit_template_parms
;
10209 parser
->implicit_template_scope
= implicit_template_scope
;
10210 parser
->auto_is_implicit_function_template_parm_p
10211 = auto_is_implicit_function_template_parm_p
;
10214 /* This field is only used during parsing of the lambda. */
10215 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10217 /* This lambda shouldn't have any proxies left at this point. */
10218 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10219 /* And now that we're done, push proxies for an enclosing lambda. */
10220 insert_pending_capture_proxies ();
10223 lambda_expr
= build_lambda_object (lambda_expr
);
10225 lambda_expr
= error_mark_node
;
10227 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10229 pop_deferring_access_checks ();
10231 return lambda_expr
;
10234 /* Parse the beginning of a lambda expression.
10237 [ lambda-capture [opt] ]
10239 LAMBDA_EXPR is the current representation of the lambda expression. */
10242 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10244 /* Need commas after the first capture. */
10247 /* Eat the leading `['. */
10248 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10250 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10252 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10253 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10254 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10255 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10257 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10259 cp_lexer_consume_token (parser
->lexer
);
10263 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10265 cp_token
* capture_token
;
10267 tree capture_init_expr
;
10268 cp_id_kind idk
= CP_ID_KIND_NONE
;
10269 bool explicit_init_p
= false;
10271 enum capture_kind_type
10276 enum capture_kind_type capture_kind
= BY_COPY
;
10278 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10280 error ("expected end of capture-list");
10287 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10289 /* Possibly capture `this'. */
10290 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10292 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10293 if (cxx_dialect
< cxx2a
10294 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10295 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10296 "with by-copy capture default");
10297 cp_lexer_consume_token (parser
->lexer
);
10298 add_capture (lambda_expr
,
10299 /*id=*/this_identifier
,
10300 /*initializer=*/finish_this_expr (),
10301 /*by_reference_p=*/true,
10306 /* Possibly capture `*this'. */
10307 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10308 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10310 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10311 if (cxx_dialect
< cxx17
)
10312 pedwarn (loc
, 0, "%<*this%> capture only available with "
10313 "-std=c++17 or -std=gnu++17");
10314 cp_lexer_consume_token (parser
->lexer
);
10315 cp_lexer_consume_token (parser
->lexer
);
10316 add_capture (lambda_expr
,
10317 /*id=*/this_identifier
,
10318 /*initializer=*/finish_this_expr (),
10319 /*by_reference_p=*/false,
10324 /* Remember whether we want to capture as a reference or not. */
10325 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10327 capture_kind
= BY_REFERENCE
;
10328 cp_lexer_consume_token (parser
->lexer
);
10331 /* Get the identifier. */
10332 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10333 capture_id
= cp_parser_identifier (parser
);
10335 if (capture_id
== error_mark_node
)
10336 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10337 delimiters, but I modified this to stop on unnested ']' as well. It
10338 was already changed to stop on unnested '}', so the
10339 "closing_parenthesis" name is no more misleading with my change. */
10341 cp_parser_skip_to_closing_parenthesis (parser
,
10342 /*recovering=*/true,
10344 /*consume_paren=*/true);
10348 /* Find the initializer for this capture. */
10349 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10350 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10351 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10353 bool direct
, non_constant
;
10354 /* An explicit initializer exists. */
10355 if (cxx_dialect
< cxx14
)
10356 pedwarn (input_location
, 0,
10357 "lambda capture initializers "
10358 "only available with -std=c++14 or -std=gnu++14");
10359 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10361 explicit_init_p
= true;
10362 if (capture_init_expr
== NULL_TREE
)
10364 error ("empty initializer for lambda init-capture");
10365 capture_init_expr
= error_mark_node
;
10370 const char* error_msg
;
10372 /* Turn the identifier into an id-expression. */
10374 = cp_parser_lookup_name_simple (parser
, capture_id
,
10375 capture_token
->location
);
10377 if (capture_init_expr
== error_mark_node
)
10379 unqualified_name_lookup_error (capture_id
);
10382 else if (!VAR_P (capture_init_expr
)
10383 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10385 error_at (capture_token
->location
,
10386 "capture of non-variable %qE",
10387 capture_init_expr
);
10388 if (DECL_P (capture_init_expr
))
10389 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10390 "%q#D declared here", capture_init_expr
);
10393 if (VAR_P (capture_init_expr
)
10394 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10396 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10397 "%qD with non-automatic storage duration",
10398 capture_init_expr
))
10399 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10400 "%q#D declared here", capture_init_expr
);
10405 = finish_id_expression
10410 /*integral_constant_expression_p=*/false,
10411 /*allow_non_integral_constant_expression_p=*/false,
10412 /*non_integral_constant_expression_p=*/NULL
,
10413 /*template_p=*/false,
10415 /*address_p=*/false,
10416 /*template_arg_p=*/false,
10418 capture_token
->location
);
10420 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10422 cp_lexer_consume_token (parser
->lexer
);
10423 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10427 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10428 && !explicit_init_p
)
10430 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10431 && capture_kind
== BY_COPY
)
10432 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10433 "of %qD redundant with by-copy capture default",
10435 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10436 && capture_kind
== BY_REFERENCE
)
10437 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10438 "capture of %qD redundant with by-reference capture "
10439 "default", capture_id
);
10442 add_capture (lambda_expr
,
10445 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10448 /* If there is any qualification still in effect, clear it
10449 now; we will be starting fresh with the next capture. */
10450 parser
->scope
= NULL_TREE
;
10451 parser
->qualifying_scope
= NULL_TREE
;
10452 parser
->object_scope
= NULL_TREE
;
10455 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10458 /* Parse the (optional) middle of a lambda expression.
10461 < template-parameter-list [opt] >
10462 ( parameter-declaration-clause [opt] )
10463 attribute-specifier [opt]
10464 decl-specifier-seq [opt]
10465 exception-specification [opt]
10466 lambda-return-type-clause [opt]
10468 LAMBDA_EXPR is the current representation of the lambda expression. */
10471 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10473 /* 5.1.1.4 of the standard says:
10474 If a lambda-expression does not include a lambda-declarator, it is as if
10475 the lambda-declarator were ().
10476 This means an empty parameter list, no attributes, and no exception
10478 tree param_list
= void_list_node
;
10479 tree attributes
= NULL_TREE
;
10480 tree exception_spec
= NULL_TREE
;
10481 tree template_param_list
= NULL_TREE
;
10482 tree tx_qual
= NULL_TREE
;
10483 tree return_type
= NULL_TREE
;
10484 cp_decl_specifier_seq lambda_specs
;
10485 clear_decl_specs (&lambda_specs
);
10487 /* The template-parameter-list is optional, but must begin with
10488 an opening angle if present. */
10489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10491 if (cxx_dialect
< cxx14
)
10492 pedwarn (parser
->lexer
->next_token
->location
, 0,
10493 "lambda templates are only available with "
10494 "-std=c++14 or -std=gnu++14");
10495 else if (cxx_dialect
< cxx2a
)
10496 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10497 "lambda templates are only available with "
10498 "-std=c++2a or -std=gnu++2a");
10500 cp_lexer_consume_token (parser
->lexer
);
10502 template_param_list
= cp_parser_template_parameter_list (parser
);
10504 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10506 /* We just processed one more parameter list. */
10507 ++parser
->num_template_parameter_lists
;
10510 /* The parameter-declaration-clause is optional (unless
10511 template-parameter-list was given), but must begin with an
10512 opening parenthesis if present. */
10513 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10515 matching_parens parens
;
10516 parens
.consume_open (parser
);
10518 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10520 /* Parse parameters. */
10521 param_list
= cp_parser_parameter_declaration_clause (parser
);
10523 /* Default arguments shall not be specified in the
10524 parameter-declaration-clause of a lambda-declarator. */
10525 if (cxx_dialect
< cxx14
)
10526 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10527 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10528 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10529 "default argument specified for lambda parameter");
10531 parens
.require_close (parser
);
10533 attributes
= cp_parser_attributes_opt (parser
);
10535 /* In the decl-specifier-seq of the lambda-declarator, each
10536 decl-specifier shall either be mutable or constexpr. */
10537 int declares_class_or_enum
;
10538 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10539 cp_parser_decl_specifier_seq (parser
,
10540 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10541 &lambda_specs
, &declares_class_or_enum
);
10542 if (lambda_specs
.storage_class
== sc_mutable
)
10544 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10545 if (lambda_specs
.conflicting_specifiers_p
)
10546 error_at (lambda_specs
.locations
[ds_storage_class
],
10547 "duplicate %<mutable%>");
10550 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10552 /* Parse optional exception specification. */
10553 exception_spec
= cp_parser_exception_specification_opt (parser
);
10555 /* Parse optional trailing return type. */
10556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10558 cp_lexer_consume_token (parser
->lexer
);
10559 return_type
= cp_parser_trailing_type_id (parser
);
10562 /* The function parameters must be in scope all the way until after the
10563 trailing-return-type in case of decltype. */
10564 pop_bindings_and_leave_scope ();
10566 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10567 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10569 /* Create the function call operator.
10571 Messing with declarators like this is no uglier than building up the
10572 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10575 cp_decl_specifier_seq return_type_specs
;
10576 cp_declarator
* declarator
;
10581 clear_decl_specs (&return_type_specs
);
10583 return_type_specs
.type
= return_type
;
10585 /* Maybe we will deduce the return type later. */
10586 return_type_specs
.type
= make_auto ();
10588 if (lambda_specs
.locations
[ds_constexpr
])
10590 if (cxx_dialect
>= cxx17
)
10591 return_type_specs
.locations
[ds_constexpr
]
10592 = lambda_specs
.locations
[ds_constexpr
];
10594 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10595 "lambda only available with -std=c++17 or -std=gnu++17");
10598 p
= obstack_alloc (&declarator_obstack
, 0);
10600 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10602 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10603 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10604 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10605 VIRT_SPEC_UNSPECIFIED
,
10609 /*late_return_type=*/NULL_TREE
,
10610 /*requires_clause*/NULL_TREE
);
10611 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10613 fco
= grokmethod (&return_type_specs
,
10616 if (fco
!= error_mark_node
)
10618 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10619 DECL_ARTIFICIAL (fco
) = 1;
10620 /* Give the object parameter a different name. */
10621 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10623 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10625 if (template_param_list
)
10627 fco
= finish_member_template_decl (fco
);
10628 finish_template_decl (template_param_list
);
10629 --parser
->num_template_parameter_lists
;
10631 else if (parser
->fully_implicit_function_template_p
)
10632 fco
= finish_fully_implicit_template (parser
, fco
);
10634 finish_member_declaration (fco
);
10636 obstack_free (&declarator_obstack
, p
);
10638 return (fco
!= error_mark_node
);
10642 /* Parse the body of a lambda expression, which is simply
10646 but which requires special handling.
10647 LAMBDA_EXPR is the current representation of the lambda expression. */
10650 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10652 bool nested
= (current_function_decl
!= NULL_TREE
);
10653 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10654 bool in_function_body
= parser
->in_function_body
;
10657 push_function_context ();
10659 /* Still increment function_depth so that we don't GC in the
10660 middle of an expression. */
10663 vec
<tree
> omp_privatization_save
;
10664 save_omp_privatization_clauses (omp_privatization_save
);
10665 /* Clear this in case we're in the middle of a default argument. */
10666 parser
->local_variables_forbidden_p
= false;
10667 parser
->in_function_body
= true;
10670 local_specialization_stack
s (lss_copy
);
10671 tree fco
= lambda_function (lambda_expr
);
10672 tree body
= start_lambda_function (fco
, lambda_expr
);
10673 matching_braces braces
;
10675 if (braces
.require_open (parser
))
10677 tree compound_stmt
= begin_compound_stmt (0);
10679 /* Originally C++11 required us to peek for 'return expr'; and
10680 process it specially here to deduce the return type. N3638
10681 removed the need for that. */
10683 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10684 cp_parser_label_declaration (parser
);
10685 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10686 braces
.require_close (parser
);
10688 finish_compound_stmt (compound_stmt
);
10691 finish_lambda_function (body
);
10694 restore_omp_privatization_clauses (omp_privatization_save
);
10695 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10696 parser
->in_function_body
= in_function_body
;
10698 pop_function_context();
10703 /* Statements [gram.stmt.stmt] */
10705 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10708 add_debug_begin_stmt (location_t loc
)
10710 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10712 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10713 /* A concept is never expanded normally. */
10716 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10717 SET_EXPR_LOCATION (stmt
, loc
);
10721 /* Parse a statement.
10725 expression-statement
10727 selection-statement
10728 iteration-statement
10730 declaration-statement
10737 attribute-specifier-seq (opt) expression-statement
10738 attribute-specifier-seq (opt) compound-statement
10739 attribute-specifier-seq (opt) selection-statement
10740 attribute-specifier-seq (opt) iteration-statement
10741 attribute-specifier-seq (opt) jump-statement
10742 declaration-statement
10743 attribute-specifier-seq (opt) try-block
10746 expression-statement
10754 IN_COMPOUND is true when the statement is nested inside a
10755 cp_parser_compound_statement; this matters for certain pragmas.
10757 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10758 is a (possibly labeled) if statement which is not enclosed in braces
10759 and has an else clause. This is used to implement -Wparentheses.
10761 CHAIN is a vector of if-else-if conditions. */
10764 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10765 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10766 location_t
*loc_after_labels
)
10768 tree statement
, std_attrs
= NULL_TREE
;
10770 location_t statement_location
, attrs_location
;
10775 /* There is no statement yet. */
10776 statement
= NULL_TREE
;
10778 saved_token_sentinel
saved_tokens (parser
->lexer
);
10779 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10780 if (c_dialect_objc ())
10781 /* In obj-c++, seeing '[[' might be the either the beginning of
10782 c++11 attributes, or a nested objc-message-expression. So
10783 let's parse the c++11 attributes tentatively. */
10784 cp_parser_parse_tentatively (parser
);
10785 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10786 if (c_dialect_objc ())
10788 if (!cp_parser_parse_definitely (parser
))
10789 std_attrs
= NULL_TREE
;
10792 /* Peek at the next token. */
10793 token
= cp_lexer_peek_token (parser
->lexer
);
10794 /* Remember the location of the first token in the statement. */
10795 statement_location
= token
->location
;
10796 add_debug_begin_stmt (statement_location
);
10797 /* If this is a keyword, then that will often determine what kind of
10798 statement we have. */
10799 if (token
->type
== CPP_KEYWORD
)
10801 enum rid keyword
= token
->keyword
;
10807 /* Looks like a labeled-statement with a case label.
10808 Parse the label, and then use tail recursion to parse
10810 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10811 in_compound
= false;
10816 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10822 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10829 statement
= cp_parser_jump_statement (parser
);
10832 /* Objective-C++ exception-handling constructs. */
10835 case RID_AT_FINALLY
:
10836 case RID_AT_SYNCHRONIZED
:
10838 statement
= cp_parser_objc_statement (parser
);
10842 statement
= cp_parser_try_block (parser
);
10845 case RID_NAMESPACE
:
10846 /* This must be a namespace alias definition. */
10847 cp_parser_declaration_statement (parser
);
10850 case RID_TRANSACTION_ATOMIC
:
10851 case RID_TRANSACTION_RELAXED
:
10852 case RID_SYNCHRONIZED
:
10853 case RID_ATOMIC_NOEXCEPT
:
10854 case RID_ATOMIC_CANCEL
:
10855 statement
= cp_parser_transaction (parser
, token
);
10857 case RID_TRANSACTION_CANCEL
:
10858 statement
= cp_parser_transaction_cancel (parser
);
10862 /* It might be a keyword like `int' that can start a
10863 declaration-statement. */
10867 else if (token
->type
== CPP_NAME
)
10869 /* If the next token is a `:', then we are looking at a
10870 labeled-statement. */
10871 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10872 if (token
->type
== CPP_COLON
)
10874 /* Looks like a labeled-statement with an ordinary label.
10875 Parse the label, and then use tail recursion to parse
10878 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10879 in_compound
= false;
10883 /* Anything that starts with a `{' must be a compound-statement. */
10884 else if (token
->type
== CPP_OPEN_BRACE
)
10885 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10886 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10887 a statement all its own. */
10888 else if (token
->type
== CPP_PRAGMA
)
10890 /* Only certain OpenMP pragmas are attached to statements, and thus
10891 are considered statements themselves. All others are not. In
10892 the context of a compound, accept the pragma as a "statement" and
10893 return so that we can check for a close brace. Otherwise we
10894 require a real statement and must go back and read one. */
10896 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10897 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10901 else if (token
->type
== CPP_EOF
)
10903 cp_parser_error (parser
, "expected statement");
10907 /* Everything else must be a declaration-statement or an
10908 expression-statement. Try for the declaration-statement
10909 first, unless we are looking at a `;', in which case we know that
10910 we have an expression-statement. */
10913 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10915 if (std_attrs
!= NULL_TREE
)
10917 /* Attributes should be parsed as part of the the
10918 declaration, so let's un-parse them. */
10919 saved_tokens
.rollback();
10920 std_attrs
= NULL_TREE
;
10923 cp_parser_parse_tentatively (parser
);
10924 /* Try to parse the declaration-statement. */
10925 cp_parser_declaration_statement (parser
);
10926 /* If that worked, we're done. */
10927 if (cp_parser_parse_definitely (parser
))
10930 /* All preceding labels have been parsed at this point. */
10931 if (loc_after_labels
!= NULL
)
10932 *loc_after_labels
= statement_location
;
10934 /* Look for an expression-statement instead. */
10935 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10937 /* Handle [[fallthrough]];. */
10938 if (attribute_fallthrough_p (std_attrs
))
10940 /* The next token after the fallthrough attribute is ';'. */
10941 if (statement
== NULL_TREE
)
10943 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10944 statement
= build_call_expr_internal_loc (statement_location
,
10946 void_type_node
, 0);
10947 finish_expr_stmt (statement
);
10950 warning_at (statement_location
, OPT_Wattributes
,
10951 "%<fallthrough%> attribute not followed by %<;%>");
10952 std_attrs
= NULL_TREE
;
10956 /* Set the line number for the statement. */
10957 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10958 SET_EXPR_LOCATION (statement
, statement_location
);
10960 /* Allow "[[fallthrough]];", but warn otherwise. */
10961 if (std_attrs
!= NULL_TREE
)
10962 warning_at (attrs_location
,
10964 "attributes at the beginning of statement are ignored");
10967 /* Append ATTR to attribute list ATTRS. */
10970 attr_chainon (tree attrs
, tree attr
)
10972 if (attrs
== error_mark_node
)
10973 return error_mark_node
;
10974 if (attr
== error_mark_node
)
10975 return error_mark_node
;
10976 return chainon (attrs
, attr
);
10979 /* Parse the label for a labeled-statement, i.e.
10982 case constant-expression :
10986 case constant-expression ... constant-expression : statement
10988 When a label is parsed without errors, the label is added to the
10989 parse tree by the finish_* functions, so this function doesn't
10990 have to return the label. */
10993 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10996 tree label
= NULL_TREE
;
10997 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10999 /* The next token should be an identifier. */
11000 token
= cp_lexer_peek_token (parser
->lexer
);
11001 if (token
->type
!= CPP_NAME
11002 && token
->type
!= CPP_KEYWORD
)
11004 cp_parser_error (parser
, "expected labeled-statement");
11008 /* Remember whether this case or a user-defined label is allowed to fall
11010 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11012 parser
->colon_corrects_to_scope_p
= false;
11013 switch (token
->keyword
)
11017 tree expr
, expr_hi
;
11018 cp_token
*ellipsis
;
11020 /* Consume the `case' token. */
11021 cp_lexer_consume_token (parser
->lexer
);
11022 /* Parse the constant-expression. */
11023 expr
= cp_parser_constant_expression (parser
);
11024 if (check_for_bare_parameter_packs (expr
))
11025 expr
= error_mark_node
;
11027 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11028 if (ellipsis
->type
== CPP_ELLIPSIS
)
11030 /* Consume the `...' token. */
11031 cp_lexer_consume_token (parser
->lexer
);
11032 expr_hi
= cp_parser_constant_expression (parser
);
11033 if (check_for_bare_parameter_packs (expr_hi
))
11034 expr_hi
= error_mark_node
;
11036 /* We don't need to emit warnings here, as the common code
11037 will do this for us. */
11040 expr_hi
= NULL_TREE
;
11042 if (parser
->in_switch_statement_p
)
11044 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11045 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11046 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11049 error_at (token
->location
,
11050 "case label %qE not within a switch statement",
11056 /* Consume the `default' token. */
11057 cp_lexer_consume_token (parser
->lexer
);
11059 if (parser
->in_switch_statement_p
)
11061 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11062 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11063 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11066 error_at (token
->location
, "case label not within a switch statement");
11070 /* Anything else must be an ordinary label. */
11071 label
= finish_label_stmt (cp_parser_identifier (parser
));
11072 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11073 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11077 /* Require the `:' token. */
11078 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11080 /* An ordinary label may optionally be followed by attributes.
11081 However, this is only permitted if the attributes are then
11082 followed by a semicolon. This is because, for backward
11083 compatibility, when parsing
11084 lab: __attribute__ ((unused)) int i;
11085 we want the attribute to attach to "i", not "lab". */
11086 if (label
!= NULL_TREE
11087 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11090 cp_parser_parse_tentatively (parser
);
11091 attrs
= cp_parser_gnu_attributes_opt (parser
);
11092 if (attrs
== NULL_TREE
11093 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11094 cp_parser_abort_tentative_parse (parser
);
11095 else if (!cp_parser_parse_definitely (parser
))
11098 attributes
= attr_chainon (attributes
, attrs
);
11101 if (attributes
!= NULL_TREE
)
11102 cplus_decl_attributes (&label
, attributes
, 0);
11104 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11107 /* Parse an expression-statement.
11109 expression-statement:
11112 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11113 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11114 indicates whether this expression-statement is part of an
11115 expression statement. */
11118 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11120 tree statement
= NULL_TREE
;
11121 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11122 location_t loc
= token
->location
;
11124 /* There might be attribute fallthrough. */
11125 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11127 /* If the next token is a ';', then there is no expression
11129 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11131 statement
= cp_parser_expression (parser
);
11132 if (statement
== error_mark_node
11133 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11135 cp_parser_skip_to_end_of_block_or_statement (parser
);
11136 return error_mark_node
;
11140 /* Handle [[fallthrough]];. */
11141 if (attribute_fallthrough_p (attr
))
11143 /* The next token after the fallthrough attribute is ';'. */
11144 if (statement
== NULL_TREE
)
11145 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11146 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11147 void_type_node
, 0);
11149 warning_at (loc
, OPT_Wattributes
,
11150 "%<fallthrough%> attribute not followed by %<;%>");
11154 /* Allow "[[fallthrough]];", but warn otherwise. */
11155 if (attr
!= NULL_TREE
)
11156 warning_at (loc
, OPT_Wattributes
,
11157 "attributes at the beginning of statement are ignored");
11159 /* Give a helpful message for "A<T>::type t;" and the like. */
11160 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11161 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11163 if (TREE_CODE (statement
) == SCOPE_REF
)
11164 error_at (token
->location
, "need %<typename%> before %qE because "
11165 "%qT is a dependent scope",
11166 statement
, TREE_OPERAND (statement
, 0));
11167 else if (is_overloaded_fn (statement
)
11168 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11171 tree fn
= get_first_fn (statement
);
11172 error_at (token
->location
,
11173 "%<%T::%D%> names the constructor, not the type",
11174 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11178 /* Consume the final `;'. */
11179 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11181 if (in_statement_expr
11182 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11183 /* This is the final expression statement of a statement
11185 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11186 else if (statement
)
11187 statement
= finish_expr_stmt (statement
);
11192 /* Parse a compound-statement.
11194 compound-statement:
11195 { statement-seq [opt] }
11199 compound-statement:
11200 { label-declaration-seq [opt] statement-seq [opt] }
11202 label-declaration-seq:
11204 label-declaration-seq label-declaration
11206 Returns a tree representing the statement. */
11209 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11210 int bcs_flags
, bool function_body
)
11212 tree compound_stmt
;
11213 matching_braces braces
;
11215 /* Consume the `{'. */
11216 if (!braces
.require_open (parser
))
11217 return error_mark_node
;
11218 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11219 && !function_body
&& cxx_dialect
< cxx14
)
11220 pedwarn (input_location
, OPT_Wpedantic
,
11221 "compound-statement in %<constexpr%> function");
11222 /* Begin the compound-statement. */
11223 compound_stmt
= begin_compound_stmt (bcs_flags
);
11224 /* If the next keyword is `__label__' we have a label declaration. */
11225 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11226 cp_parser_label_declaration (parser
);
11227 /* Parse an (optional) statement-seq. */
11228 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11229 /* Finish the compound-statement. */
11230 finish_compound_stmt (compound_stmt
);
11231 /* Consume the `}'. */
11232 braces
.require_close (parser
);
11234 return compound_stmt
;
11237 /* Parse an (optional) statement-seq.
11241 statement-seq [opt] statement */
11244 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11246 /* Scan statements until there aren't any more. */
11249 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11251 /* If we are looking at a `}', then we have run out of
11252 statements; the same is true if we have reached the end
11253 of file, or have stumbled upon a stray '@end'. */
11254 if (token
->type
== CPP_CLOSE_BRACE
11255 || token
->type
== CPP_EOF
11256 || token
->type
== CPP_PRAGMA_EOL
11257 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11260 /* If we are in a compound statement and find 'else' then
11261 something went wrong. */
11262 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11264 if (parser
->in_statement
& IN_IF_STMT
)
11268 token
= cp_lexer_consume_token (parser
->lexer
);
11269 error_at (token
->location
, "%<else%> without a previous %<if%>");
11273 /* Parse the statement. */
11274 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11278 /* Return true if we're looking at (init; cond), false otherwise. */
11281 cp_parser_init_statement_p (cp_parser
*parser
)
11283 /* Save tokens so that we can put them back. */
11284 cp_lexer_save_tokens (parser
->lexer
);
11286 /* Look for ';' that is not nested in () or {}. */
11287 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11288 /*recovering=*/false,
11290 /*consume_paren=*/false);
11292 /* Roll back the tokens we skipped. */
11293 cp_lexer_rollback_tokens (parser
->lexer
);
11298 /* Parse a selection-statement.
11300 selection-statement:
11301 if ( init-statement [opt] condition ) statement
11302 if ( init-statement [opt] condition ) statement else statement
11303 switch ( init-statement [opt] condition ) statement
11305 Returns the new IF_STMT or SWITCH_STMT.
11307 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11308 is a (possibly labeled) if statement which is not enclosed in
11309 braces and has an else clause. This is used to implement
11312 CHAIN is a vector of if-else-if conditions. This is used to implement
11313 -Wduplicated-cond. */
11316 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11321 token_indent_info guard_tinfo
;
11326 /* Peek at the next token. */
11327 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11328 guard_tinfo
= get_token_indent_info (token
);
11330 /* See what kind of keyword it is. */
11331 keyword
= token
->keyword
;
11341 if (keyword
== RID_IF
11342 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11346 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11347 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11348 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11349 "with -std=c++17 or -std=gnu++17");
11352 /* Look for the `('. */
11353 matching_parens parens
;
11354 if (!parens
.require_open (parser
))
11356 cp_parser_skip_to_end_of_statement (parser
);
11357 return error_mark_node
;
11360 /* Begin the selection-statement. */
11361 if (keyword
== RID_IF
)
11363 statement
= begin_if_stmt ();
11364 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11367 statement
= begin_switch_stmt ();
11369 /* Parse the optional init-statement. */
11370 if (cp_parser_init_statement_p (parser
))
11373 if (cxx_dialect
< cxx17
)
11374 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11375 "init-statement in selection statements only available "
11376 "with -std=c++17 or -std=gnu++17");
11377 cp_parser_init_statement (parser
, &decl
);
11380 /* Parse the condition. */
11381 condition
= cp_parser_condition (parser
);
11382 /* Look for the `)'. */
11383 if (!parens
.require_close (parser
))
11384 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11385 /*consume_paren=*/true);
11387 if (keyword
== RID_IF
)
11390 unsigned char in_statement
;
11392 /* Add the condition. */
11393 condition
= finish_if_stmt_cond (condition
, statement
);
11395 if (warn_duplicated_cond
)
11396 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11399 /* Parse the then-clause. */
11400 in_statement
= parser
->in_statement
;
11401 parser
->in_statement
|= IN_IF_STMT
;
11403 /* Outside a template, the non-selected branch of a constexpr
11404 if is a 'discarded statement', i.e. unevaluated. */
11405 bool was_discarded
= in_discarded_stmt
;
11406 bool discard_then
= (cx
&& !processing_template_decl
11407 && integer_zerop (condition
));
11410 in_discarded_stmt
= true;
11411 ++c_inhibit_evaluation_warnings
;
11414 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11417 parser
->in_statement
= in_statement
;
11419 finish_then_clause (statement
);
11423 THEN_CLAUSE (statement
) = NULL_TREE
;
11424 in_discarded_stmt
= was_discarded
;
11425 --c_inhibit_evaluation_warnings
;
11428 /* If the next token is `else', parse the else-clause. */
11429 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11432 bool discard_else
= (cx
&& !processing_template_decl
11433 && integer_nonzerop (condition
));
11436 in_discarded_stmt
= true;
11437 ++c_inhibit_evaluation_warnings
;
11441 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11442 /* Consume the `else' keyword. */
11443 cp_lexer_consume_token (parser
->lexer
);
11444 if (warn_duplicated_cond
)
11446 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11450 /* We've got "if (COND) else if (COND2)". Start
11451 the condition chain and add COND as the first
11453 chain
= new vec
<tree
> ();
11454 if (!CONSTANT_CLASS_P (condition
)
11455 && !TREE_SIDE_EFFECTS (condition
))
11457 /* Wrap it in a NOP_EXPR so that we can set the
11458 location of the condition. */
11459 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11461 SET_EXPR_LOCATION (e
, token
->location
);
11462 chain
->safe_push (e
);
11465 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11468 /* This is if-else without subsequent if. Zap the
11469 condition chain; we would have already warned at
11475 begin_else_clause (statement
);
11476 /* Parse the else-clause. */
11477 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11478 guard_tinfo
, chain
);
11480 finish_else_clause (statement
);
11482 /* If we are currently parsing a then-clause, then
11483 IF_P will not be NULL. We set it to true to
11484 indicate that this if statement has an else clause.
11485 This may trigger the Wparentheses warning below
11486 when we get back up to the parent if statement. */
11492 ELSE_CLAUSE (statement
) = NULL_TREE
;
11493 in_discarded_stmt
= was_discarded
;
11494 --c_inhibit_evaluation_warnings
;
11499 /* This if statement does not have an else clause. If
11500 NESTED_IF is true, then the then-clause has an if
11501 statement which does have an else clause. We warn
11502 about the potential ambiguity. */
11504 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11505 "suggest explicit braces to avoid ambiguous"
11507 if (warn_duplicated_cond
)
11509 /* We don't need the condition chain anymore. */
11515 /* Now we're all done with the if-statement. */
11516 finish_if_stmt (statement
);
11520 bool in_switch_statement_p
;
11521 unsigned char in_statement
;
11523 /* Add the condition. */
11524 finish_switch_cond (condition
, statement
);
11526 /* Parse the body of the switch-statement. */
11527 in_switch_statement_p
= parser
->in_switch_statement_p
;
11528 in_statement
= parser
->in_statement
;
11529 parser
->in_switch_statement_p
= true;
11530 parser
->in_statement
|= IN_SWITCH_STMT
;
11531 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11533 parser
->in_switch_statement_p
= in_switch_statement_p
;
11534 parser
->in_statement
= in_statement
;
11536 /* Now we're all done with the switch-statement. */
11537 finish_switch_stmt (statement
);
11545 cp_parser_error (parser
, "expected selection-statement");
11546 return error_mark_node
;
11550 /* Parse a condition.
11554 type-specifier-seq declarator = initializer-clause
11555 type-specifier-seq declarator braced-init-list
11560 type-specifier-seq declarator asm-specification [opt]
11561 attributes [opt] = assignment-expression
11563 Returns the expression that should be tested. */
11566 cp_parser_condition (cp_parser
* parser
)
11568 cp_decl_specifier_seq type_specifiers
;
11569 const char *saved_message
;
11570 int declares_class_or_enum
;
11572 /* Try the declaration first. */
11573 cp_parser_parse_tentatively (parser
);
11574 /* New types are not allowed in the type-specifier-seq for a
11576 saved_message
= parser
->type_definition_forbidden_message
;
11577 parser
->type_definition_forbidden_message
11578 = G_("types may not be defined in conditions");
11579 /* Parse the type-specifier-seq. */
11580 cp_parser_decl_specifier_seq (parser
,
11581 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11583 &declares_class_or_enum
);
11584 /* Restore the saved message. */
11585 parser
->type_definition_forbidden_message
= saved_message
;
11586 /* If all is well, we might be looking at a declaration. */
11587 if (!cp_parser_error_occurred (parser
))
11590 tree asm_specification
;
11592 cp_declarator
*declarator
;
11593 tree initializer
= NULL_TREE
;
11595 /* Parse the declarator. */
11596 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11597 /*ctor_dtor_or_conv_p=*/NULL
,
11598 /*parenthesized_p=*/NULL
,
11599 /*member_p=*/false,
11600 /*friend_p=*/false);
11601 /* Parse the attributes. */
11602 attributes
= cp_parser_attributes_opt (parser
);
11603 /* Parse the asm-specification. */
11604 asm_specification
= cp_parser_asm_specification_opt (parser
);
11605 /* If the next token is not an `=' or '{', then we might still be
11606 looking at an expression. For example:
11610 looks like a decl-specifier-seq and a declarator -- but then
11611 there is no `=', so this is an expression. */
11612 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11613 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11614 cp_parser_simulate_error (parser
);
11616 /* If we did see an `=' or '{', then we are looking at a declaration
11618 if (cp_parser_parse_definitely (parser
))
11621 bool non_constant_p
;
11622 int flags
= LOOKUP_ONLYCONVERTING
;
11624 /* Create the declaration. */
11625 decl
= start_decl (declarator
, &type_specifiers
,
11626 /*initialized_p=*/true,
11627 attributes
, /*prefix_attributes=*/NULL_TREE
,
11630 /* Parse the initializer. */
11631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11633 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11634 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11639 /* Consume the `='. */
11640 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11641 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11643 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11644 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11646 /* Process the initializer. */
11647 cp_finish_decl (decl
,
11648 initializer
, !non_constant_p
,
11653 pop_scope (pushed_scope
);
11655 return convert_from_reference (decl
);
11658 /* If we didn't even get past the declarator successfully, we are
11659 definitely not looking at a declaration. */
11661 cp_parser_abort_tentative_parse (parser
);
11663 /* Otherwise, we are looking at an expression. */
11664 return cp_parser_expression (parser
);
11667 /* Parses a for-statement or range-for-statement until the closing ')',
11671 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11673 tree init
, scope
, decl
;
11676 /* Begin the for-statement. */
11677 scope
= begin_for_scope (&init
);
11679 /* Parse the initialization. */
11680 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11683 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11685 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11689 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11690 unsigned short unroll
)
11692 /* Normal for loop */
11693 tree condition
= NULL_TREE
;
11694 tree expression
= NULL_TREE
;
11697 stmt
= begin_for_stmt (scope
, init
);
11698 /* The init-statement has already been parsed in
11699 cp_parser_init_statement, so no work is needed here. */
11700 finish_init_stmt (stmt
);
11702 /* If there's a condition, process it. */
11703 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11704 condition
= cp_parser_condition (parser
);
11707 cp_parser_error (parser
, "missing loop condition in loop with "
11708 "%<GCC ivdep%> pragma");
11709 condition
= error_mark_node
;
11713 cp_parser_error (parser
, "missing loop condition in loop with "
11714 "%<GCC unroll%> pragma");
11715 condition
= error_mark_node
;
11717 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11718 /* Look for the `;'. */
11719 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11721 /* If there's an expression, process it. */
11722 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11723 expression
= cp_parser_expression (parser
);
11724 finish_for_expr (expression
, stmt
);
11729 /* Tries to parse a range-based for-statement:
11732 decl-specifier-seq declarator : expression
11734 The decl-specifier-seq declarator and the `:' are already parsed by
11735 cp_parser_init_statement. If processing_template_decl it returns a
11736 newly created RANGE_FOR_STMT; if not, it is converted to a
11737 regular FOR_STMT. */
11740 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11741 bool ivdep
, unsigned short unroll
)
11743 tree stmt
, range_expr
;
11744 auto_vec
<cxx_binding
*, 16> bindings
;
11745 auto_vec
<tree
, 16> names
;
11746 tree decomp_first_name
= NULL_TREE
;
11747 unsigned int decomp_cnt
= 0;
11749 /* Get the range declaration momentarily out of the way so that
11750 the range expression doesn't clash with it. */
11751 if (range_decl
!= error_mark_node
)
11753 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11755 tree v
= DECL_VALUE_EXPR (range_decl
);
11756 /* For decomposition declaration get all of the corresponding
11757 declarations out of the way. */
11758 if (TREE_CODE (v
) == ARRAY_REF
11759 && VAR_P (TREE_OPERAND (v
, 0))
11760 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11762 tree d
= range_decl
;
11763 range_decl
= TREE_OPERAND (v
, 0);
11764 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11765 decomp_first_name
= d
;
11766 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11768 tree name
= DECL_NAME (d
);
11769 names
.safe_push (name
);
11770 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11771 IDENTIFIER_BINDING (name
)
11772 = IDENTIFIER_BINDING (name
)->previous
;
11776 if (names
.is_empty ())
11778 tree name
= DECL_NAME (range_decl
);
11779 names
.safe_push (name
);
11780 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11781 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11785 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11787 bool expr_non_constant_p
;
11788 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11791 range_expr
= cp_parser_expression (parser
);
11793 /* Put the range declaration(s) back into scope. */
11794 for (unsigned int i
= 0; i
< names
.length (); i
++)
11796 cxx_binding
*binding
= bindings
[i
];
11797 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11798 IDENTIFIER_BINDING (names
[i
]) = binding
;
11801 /* If in template, STMT is converted to a normal for-statement
11802 at instantiation. If not, it is done just ahead. */
11803 if (processing_template_decl
)
11805 if (check_for_bare_parameter_packs (range_expr
))
11806 range_expr
= error_mark_node
;
11807 stmt
= begin_range_for_stmt (scope
, init
);
11809 RANGE_FOR_IVDEP (stmt
) = 1;
11811 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11812 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11813 if (!type_dependent_expression_p (range_expr
)
11814 /* do_auto_deduction doesn't mess with template init-lists. */
11815 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11816 do_range_for_auto_deduction (range_decl
, range_expr
);
11820 stmt
= begin_for_stmt (scope
, init
);
11821 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11822 decomp_first_name
, decomp_cnt
, ivdep
,
11828 /* Subroutine of cp_convert_range_for: given the initializer expression,
11829 builds up the range temporary. */
11832 build_range_temp (tree range_expr
)
11834 tree range_type
, range_temp
;
11836 /* Find out the type deduced by the declaration
11837 `auto &&__range = range_expr'. */
11838 range_type
= cp_build_reference_type (make_auto (), true);
11839 range_type
= do_auto_deduction (range_type
, range_expr
,
11840 type_uses_auto (range_type
));
11842 /* Create the __range variable. */
11843 range_temp
= build_decl (input_location
, VAR_DECL
,
11844 get_identifier ("__for_range"), range_type
);
11845 TREE_USED (range_temp
) = 1;
11846 DECL_ARTIFICIAL (range_temp
) = 1;
11851 /* Used by cp_parser_range_for in template context: we aren't going to
11852 do a full conversion yet, but we still need to resolve auto in the
11853 type of the for-range-declaration if present. This is basically
11854 a shortcut version of cp_convert_range_for. */
11857 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11859 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11862 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11863 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11864 iter_type
= (cp_parser_perform_range_for_lookup
11865 (range_temp
, &begin_dummy
, &end_dummy
));
11868 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11870 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11872 tf_warning_or_error
);
11873 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11874 iter_decl
, auto_node
);
11879 /* Converts a range-based for-statement into a normal
11880 for-statement, as per the definition.
11882 for (RANGE_DECL : RANGE_EXPR)
11885 should be equivalent to:
11888 auto &&__range = RANGE_EXPR;
11889 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11893 RANGE_DECL = *__begin;
11898 If RANGE_EXPR is an array:
11899 BEGIN_EXPR = __range
11900 END_EXPR = __range + ARRAY_SIZE(__range)
11901 Else if RANGE_EXPR has a member 'begin' or 'end':
11902 BEGIN_EXPR = __range.begin()
11903 END_EXPR = __range.end()
11905 BEGIN_EXPR = begin(__range)
11906 END_EXPR = end(__range);
11908 If __range has a member 'begin' but not 'end', or vice versa, we must
11909 still use the second alternative (it will surely fail, however).
11910 When calling begin()/end() in the third alternative we must use
11911 argument dependent lookup, but always considering 'std' as an associated
11915 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11916 tree decomp_first_name
, unsigned int decomp_cnt
,
11917 bool ivdep
, unsigned short unroll
)
11920 tree iter_type
, begin_expr
, end_expr
;
11921 tree condition
, expression
;
11923 range_expr
= mark_lvalue_use (range_expr
);
11925 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11926 /* If an error happened previously do nothing or else a lot of
11927 unhelpful errors would be issued. */
11928 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11933 if (VAR_P (range_expr
)
11934 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11935 /* Can't bind a reference to an array of runtime bound. */
11936 range_temp
= range_expr
;
11939 range_temp
= build_range_temp (range_expr
);
11940 pushdecl (range_temp
);
11941 cp_finish_decl (range_temp
, range_expr
,
11942 /*is_constant_init*/false, NULL_TREE
,
11943 LOOKUP_ONLYCONVERTING
);
11944 range_temp
= convert_from_reference (range_temp
);
11946 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11947 &begin_expr
, &end_expr
);
11950 /* The new for initialization statement. */
11951 begin
= build_decl (input_location
, VAR_DECL
,
11952 get_identifier ("__for_begin"), iter_type
);
11953 TREE_USED (begin
) = 1;
11954 DECL_ARTIFICIAL (begin
) = 1;
11956 cp_finish_decl (begin
, begin_expr
,
11957 /*is_constant_init*/false, NULL_TREE
,
11958 LOOKUP_ONLYCONVERTING
);
11960 if (cxx_dialect
>= cxx17
)
11961 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11962 end
= build_decl (input_location
, VAR_DECL
,
11963 get_identifier ("__for_end"), iter_type
);
11964 TREE_USED (end
) = 1;
11965 DECL_ARTIFICIAL (end
) = 1;
11967 cp_finish_decl (end
, end_expr
,
11968 /*is_constant_init*/false, NULL_TREE
,
11969 LOOKUP_ONLYCONVERTING
);
11971 finish_init_stmt (statement
);
11973 /* The new for condition. */
11974 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11977 NULL
, tf_warning_or_error
);
11978 finish_for_cond (condition
, statement
, ivdep
, unroll
);
11980 /* The new increment expression. */
11981 expression
= finish_unary_op_expr (input_location
,
11982 PREINCREMENT_EXPR
, begin
,
11983 tf_warning_or_error
);
11984 finish_for_expr (expression
, statement
);
11986 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11987 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11989 /* The declaration is initialized with *__begin inside the loop body. */
11990 cp_finish_decl (range_decl
,
11991 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
11992 tf_warning_or_error
),
11993 /*is_constant_init*/false, NULL_TREE
,
11994 LOOKUP_ONLYCONVERTING
);
11995 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11996 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12001 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12002 We need to solve both at the same time because the method used
12003 depends on the existence of members begin or end.
12004 Returns the type deduced for the iterator expression. */
12007 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12009 if (error_operand_p (range
))
12011 *begin
= *end
= error_mark_node
;
12012 return error_mark_node
;
12015 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12017 error ("range-based %<for%> expression of type %qT "
12018 "has incomplete type", TREE_TYPE (range
));
12019 *begin
= *end
= error_mark_node
;
12020 return error_mark_node
;
12022 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12024 /* If RANGE is an array, we will use pointer arithmetic. */
12025 *begin
= decay_conversion (range
, tf_warning_or_error
);
12026 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12028 array_type_nelts_top (TREE_TYPE (range
)),
12030 return TREE_TYPE (*begin
);
12034 /* If it is not an array, we must do a bit of magic. */
12035 tree id_begin
, id_end
;
12036 tree member_begin
, member_end
;
12038 *begin
= *end
= error_mark_node
;
12040 id_begin
= get_identifier ("begin");
12041 id_end
= get_identifier ("end");
12042 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12043 /*protect=*/2, /*want_type=*/false,
12044 tf_warning_or_error
);
12045 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12046 /*protect=*/2, /*want_type=*/false,
12047 tf_warning_or_error
);
12049 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
12051 /* Use the member functions. */
12052 if (member_begin
!= NULL_TREE
)
12053 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12055 error ("range-based %<for%> expression of type %qT has an "
12056 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
12058 if (member_end
!= NULL_TREE
)
12059 *end
= cp_parser_range_for_member_function (range
, id_end
);
12061 error ("range-based %<for%> expression of type %qT has a "
12062 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
12066 /* Use global functions with ADL. */
12067 vec
<tree
, va_gc
> *vec
;
12068 vec
= make_tree_vector ();
12070 vec_safe_push (vec
, range
);
12072 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12073 tf_warning_or_error
);
12074 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12075 tf_warning_or_error
);
12076 member_end
= perform_koenig_lookup (id_end
, vec
,
12077 tf_warning_or_error
);
12078 *end
= finish_call_expr (member_end
, &vec
, false, true,
12079 tf_warning_or_error
);
12081 release_tree_vector (vec
);
12084 /* Last common checks. */
12085 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12087 /* If one of the expressions is an error do no more checks. */
12088 *begin
= *end
= error_mark_node
;
12089 return error_mark_node
;
12091 else if (type_dependent_expression_p (*begin
)
12092 || type_dependent_expression_p (*end
))
12093 /* Can happen, when, eg, in a template context, Koenig lookup
12094 can't resolve begin/end (c++/58503). */
12098 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12099 /* The unqualified type of the __begin and __end temporaries should
12100 be the same, as required by the multiple auto declaration. */
12101 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12103 if (cxx_dialect
>= cxx17
12104 && (build_x_binary_op (input_location
, NE_EXPR
,
12105 *begin
, ERROR_MARK
,
12108 != error_mark_node
))
12109 /* P0184R0 allows __begin and __end to have different types,
12110 but make sure they are comparable so we can give a better
12113 error ("inconsistent begin/end types in range-based %<for%> "
12114 "statement: %qT and %qT",
12115 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12122 /* Helper function for cp_parser_perform_range_for_lookup.
12123 Builds a tree for RANGE.IDENTIFIER(). */
12126 cp_parser_range_for_member_function (tree range
, tree identifier
)
12129 vec
<tree
, va_gc
> *vec
;
12131 member
= finish_class_member_access_expr (range
, identifier
,
12132 false, tf_warning_or_error
);
12133 if (member
== error_mark_node
)
12134 return error_mark_node
;
12136 vec
= make_tree_vector ();
12137 res
= finish_call_expr (member
, &vec
,
12138 /*disallow_virtual=*/false,
12139 /*koenig_p=*/false,
12140 tf_warning_or_error
);
12141 release_tree_vector (vec
);
12145 /* Parse an iteration-statement.
12147 iteration-statement:
12148 while ( condition ) statement
12149 do statement while ( expression ) ;
12150 for ( init-statement condition [opt] ; expression [opt] )
12153 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12156 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12157 unsigned short unroll
)
12162 unsigned char in_statement
;
12163 token_indent_info guard_tinfo
;
12165 /* Peek at the next token. */
12166 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12168 return error_mark_node
;
12170 guard_tinfo
= get_token_indent_info (token
);
12172 /* Remember whether or not we are already within an iteration
12174 in_statement
= parser
->in_statement
;
12176 /* See what kind of keyword it is. */
12177 keyword
= token
->keyword
;
12184 /* Begin the while-statement. */
12185 statement
= begin_while_stmt ();
12186 /* Look for the `('. */
12187 matching_parens parens
;
12188 parens
.require_open (parser
);
12189 /* Parse the condition. */
12190 condition
= cp_parser_condition (parser
);
12191 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12192 /* Look for the `)'. */
12193 parens
.require_close (parser
);
12194 /* Parse the dependent statement. */
12195 parser
->in_statement
= IN_ITERATION_STMT
;
12196 bool prev
= note_iteration_stmt_body_start ();
12197 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12198 note_iteration_stmt_body_end (prev
);
12199 parser
->in_statement
= in_statement
;
12200 /* We're done with the while-statement. */
12201 finish_while_stmt (statement
);
12209 /* Begin the do-statement. */
12210 statement
= begin_do_stmt ();
12211 /* Parse the body of the do-statement. */
12212 parser
->in_statement
= IN_ITERATION_STMT
;
12213 bool prev
= note_iteration_stmt_body_start ();
12214 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12215 note_iteration_stmt_body_end (prev
);
12216 parser
->in_statement
= in_statement
;
12217 finish_do_body (statement
);
12218 /* Look for the `while' keyword. */
12219 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12220 /* Look for the `('. */
12221 matching_parens parens
;
12222 parens
.require_open (parser
);
12223 /* Parse the expression. */
12224 expression
= cp_parser_expression (parser
);
12225 /* We're done with the do-statement. */
12226 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12227 /* Look for the `)'. */
12228 parens
.require_close (parser
);
12229 /* Look for the `;'. */
12230 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12236 /* Look for the `('. */
12237 matching_parens parens
;
12238 parens
.require_open (parser
);
12240 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12242 /* Look for the `)'. */
12243 parens
.require_close (parser
);
12245 /* Parse the body of the for-statement. */
12246 parser
->in_statement
= IN_ITERATION_STMT
;
12247 bool prev
= note_iteration_stmt_body_start ();
12248 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12249 note_iteration_stmt_body_end (prev
);
12250 parser
->in_statement
= in_statement
;
12252 /* We're done with the for-statement. */
12253 finish_for_stmt (statement
);
12258 cp_parser_error (parser
, "expected iteration-statement");
12259 statement
= error_mark_node
;
12266 /* Parse a init-statement or the declarator of a range-based-for.
12267 Returns true if a range-based-for declaration is seen.
12270 expression-statement
12271 simple-declaration */
12274 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
12276 /* If the next token is a `;', then we have an empty
12277 expression-statement. Grammatically, this is also a
12278 simple-declaration, but an invalid one, because it does not
12279 declare anything. Therefore, if we did not handle this case
12280 specially, we would issue an error message about an invalid
12282 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12284 bool is_range_for
= false;
12285 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12287 /* A colon is used in range-based for. */
12288 parser
->colon_corrects_to_scope_p
= false;
12290 /* We're going to speculatively look for a declaration, falling back
12291 to an expression, if necessary. */
12292 cp_parser_parse_tentatively (parser
);
12293 /* Parse the declaration. */
12294 cp_parser_simple_declaration (parser
,
12295 /*function_definition_allowed_p=*/false,
12297 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12298 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12300 /* It is a range-for, consume the ':' */
12301 cp_lexer_consume_token (parser
->lexer
);
12302 is_range_for
= true;
12303 if (cxx_dialect
< cxx11
)
12305 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12306 "range-based %<for%> loops only available with "
12307 "-std=c++11 or -std=gnu++11");
12308 *decl
= error_mark_node
;
12312 /* The ';' is not consumed yet because we told
12313 cp_parser_simple_declaration not to. */
12314 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12316 if (cp_parser_parse_definitely (parser
))
12317 return is_range_for
;
12318 /* If the tentative parse failed, then we shall need to look for an
12319 expression-statement. */
12321 /* If we are here, it is an expression-statement. */
12322 cp_parser_expression_statement (parser
, NULL_TREE
);
12326 /* Parse a jump-statement.
12331 return expression [opt] ;
12332 return braced-init-list ;
12338 goto * expression ;
12340 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12343 cp_parser_jump_statement (cp_parser
* parser
)
12345 tree statement
= error_mark_node
;
12348 unsigned char in_statement
;
12350 /* Peek at the next token. */
12351 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12353 return error_mark_node
;
12355 /* See what kind of keyword it is. */
12356 keyword
= token
->keyword
;
12360 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12361 switch (in_statement
)
12364 error_at (token
->location
, "break statement not within loop or switch");
12367 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12368 || in_statement
== IN_ITERATION_STMT
);
12369 statement
= finish_break_stmt ();
12370 if (in_statement
== IN_ITERATION_STMT
)
12371 break_maybe_infinite_loop ();
12374 error_at (token
->location
, "invalid exit from OpenMP structured block");
12377 error_at (token
->location
, "break statement used with OpenMP for loop");
12380 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12384 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12387 error_at (token
->location
, "continue statement not within a loop");
12389 /* Fall through. */
12390 case IN_ITERATION_STMT
:
12392 statement
= finish_continue_stmt ();
12395 error_at (token
->location
, "invalid exit from OpenMP structured block");
12398 gcc_unreachable ();
12400 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12406 bool expr_non_constant_p
;
12408 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12410 cp_lexer_set_source_position (parser
->lexer
);
12411 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12412 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12414 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12415 expr
= cp_parser_expression (parser
);
12417 /* If the next token is a `;', then there is no
12420 /* Build the return-statement. */
12421 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12422 /* Don't deduce from a discarded return statement. */;
12424 statement
= finish_return_stmt (expr
);
12425 /* Look for the final `;'. */
12426 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12431 if (parser
->in_function_body
12432 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12434 error ("%<goto%> in %<constexpr%> function");
12435 cp_function_chain
->invalid_constexpr
= true;
12438 /* Create the goto-statement. */
12439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12441 /* Issue a warning about this use of a GNU extension. */
12442 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12443 /* Consume the '*' token. */
12444 cp_lexer_consume_token (parser
->lexer
);
12445 /* Parse the dependent expression. */
12446 finish_goto_stmt (cp_parser_expression (parser
));
12449 finish_goto_stmt (cp_parser_identifier (parser
));
12450 /* Look for the final `;'. */
12451 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12455 cp_parser_error (parser
, "expected jump-statement");
12462 /* Parse a declaration-statement.
12464 declaration-statement:
12465 block-declaration */
12468 cp_parser_declaration_statement (cp_parser
* parser
)
12472 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12473 p
= obstack_alloc (&declarator_obstack
, 0);
12475 /* Parse the block-declaration. */
12476 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12478 /* Free any declarators allocated. */
12479 obstack_free (&declarator_obstack
, p
);
12482 /* Some dependent statements (like `if (cond) statement'), are
12483 implicitly in their own scope. In other words, if the statement is
12484 a single statement (as opposed to a compound-statement), it is
12485 none-the-less treated as if it were enclosed in braces. Any
12486 declarations appearing in the dependent statement are out of scope
12487 after control passes that point. This function parses a statement,
12488 but ensures that is in its own scope, even if it is not a
12489 compound-statement.
12491 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12492 is a (possibly labeled) if statement which is not enclosed in
12493 braces and has an else clause. This is used to implement
12496 CHAIN is a vector of if-else-if conditions. This is used to implement
12499 Returns the new statement. */
12502 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12503 const token_indent_info
&guard_tinfo
,
12507 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12508 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12509 token_indent_info body_tinfo
12510 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12515 /* Mark if () ; with a special NOP_EXPR. */
12516 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12518 cp_lexer_consume_token (parser
->lexer
);
12519 statement
= add_stmt (build_empty_stmt (body_loc
));
12521 if (guard_tinfo
.keyword
== RID_IF
12522 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12523 warning_at (body_loc
, OPT_Wempty_body
,
12524 "suggest braces around empty body in an %<if%> statement");
12525 else if (guard_tinfo
.keyword
== RID_ELSE
)
12526 warning_at (body_loc
, OPT_Wempty_body
,
12527 "suggest braces around empty body in an %<else%> statement");
12529 /* if a compound is opened, we simply parse the statement directly. */
12530 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12531 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12532 /* If the token is not a `{', then we must take special action. */
12535 /* Create a compound-statement. */
12536 statement
= begin_compound_stmt (0);
12537 /* Parse the dependent-statement. */
12538 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12539 &body_loc_after_labels
);
12540 /* Finish the dummy compound-statement. */
12541 finish_compound_stmt (statement
);
12544 token_indent_info next_tinfo
12545 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12546 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12548 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12549 && next_tinfo
.type
!= CPP_SEMICOLON
)
12550 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12551 guard_tinfo
.location
, guard_tinfo
.keyword
);
12553 /* Return the statement. */
12557 /* For some dependent statements (like `while (cond) statement'), we
12558 have already created a scope. Therefore, even if the dependent
12559 statement is a compound-statement, we do not want to create another
12563 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12564 const token_indent_info
&guard_tinfo
)
12566 /* If the token is a `{', then we must take special action. */
12567 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12569 token_indent_info body_tinfo
12570 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12571 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12573 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12574 &loc_after_labels
);
12575 token_indent_info next_tinfo
12576 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12577 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12579 if (loc_after_labels
!= UNKNOWN_LOCATION
12580 && next_tinfo
.type
!= CPP_SEMICOLON
)
12581 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12582 guard_tinfo
.location
,
12583 guard_tinfo
.keyword
);
12587 /* Avoid calling cp_parser_compound_statement, so that we
12588 don't create a new scope. Do everything else by hand. */
12589 matching_braces braces
;
12590 braces
.require_open (parser
);
12591 /* If the next keyword is `__label__' we have a label declaration. */
12592 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12593 cp_parser_label_declaration (parser
);
12594 /* Parse an (optional) statement-seq. */
12595 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12596 braces
.require_close (parser
);
12600 /* Declarations [gram.dcl.dcl] */
12602 /* Parse an optional declaration-sequence.
12606 declaration-seq declaration */
12609 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12615 token
= cp_lexer_peek_token (parser
->lexer
);
12617 if (token
->type
== CPP_CLOSE_BRACE
12618 || token
->type
== CPP_EOF
12619 || token
->type
== CPP_PRAGMA_EOL
)
12622 if (token
->type
== CPP_SEMICOLON
)
12624 /* A declaration consisting of a single semicolon is
12625 invalid. Allow it unless we're being pedantic. */
12626 cp_lexer_consume_token (parser
->lexer
);
12627 if (!in_system_header_at (input_location
))
12628 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12632 /* If we're entering or exiting a region that's implicitly
12633 extern "C", modify the lang context appropriately. */
12634 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12636 push_lang_context (lang_name_c
);
12637 parser
->implicit_extern_c
= true;
12639 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12641 pop_lang_context ();
12642 parser
->implicit_extern_c
= false;
12645 if (token
->type
== CPP_PRAGMA
)
12647 /* A top-level declaration can consist solely of a #pragma.
12648 A nested declaration cannot, so this is done here and not
12649 in cp_parser_declaration. (A #pragma at block scope is
12650 handled in cp_parser_statement.) */
12651 cp_parser_pragma (parser
, pragma_external
, NULL
);
12655 /* Parse the declaration itself. */
12656 cp_parser_declaration (parser
);
12660 /* Parse a declaration.
12664 function-definition
12665 template-declaration
12666 explicit-instantiation
12667 explicit-specialization
12668 linkage-specification
12669 namespace-definition
12677 __extension__ declaration */
12680 cp_parser_declaration (cp_parser
* parser
)
12684 int saved_pedantic
;
12686 tree attributes
= NULL_TREE
;
12688 /* Check for the `__extension__' keyword. */
12689 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12691 /* Parse the qualified declaration. */
12692 cp_parser_declaration (parser
);
12693 /* Restore the PEDANTIC flag. */
12694 pedantic
= saved_pedantic
;
12699 /* Try to figure out what kind of declaration is present. */
12700 token1
= *cp_lexer_peek_token (parser
->lexer
);
12702 if (token1
.type
!= CPP_EOF
)
12703 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12706 token2
.type
= CPP_EOF
;
12707 token2
.keyword
= RID_MAX
;
12710 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12711 p
= obstack_alloc (&declarator_obstack
, 0);
12713 /* If the next token is `extern' and the following token is a string
12714 literal, then we have a linkage specification. */
12715 if (token1
.keyword
== RID_EXTERN
12716 && cp_parser_is_pure_string_literal (&token2
))
12717 cp_parser_linkage_specification (parser
);
12718 /* If the next token is `template', then we have either a template
12719 declaration, an explicit instantiation, or an explicit
12721 else if (token1
.keyword
== RID_TEMPLATE
)
12723 /* `template <>' indicates a template specialization. */
12724 if (token2
.type
== CPP_LESS
12725 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12726 cp_parser_explicit_specialization (parser
);
12727 /* `template <' indicates a template declaration. */
12728 else if (token2
.type
== CPP_LESS
)
12729 cp_parser_template_declaration (parser
, /*member_p=*/false);
12730 /* Anything else must be an explicit instantiation. */
12732 cp_parser_explicit_instantiation (parser
);
12734 /* If the next token is `export', then we have a template
12736 else if (token1
.keyword
== RID_EXPORT
)
12737 cp_parser_template_declaration (parser
, /*member_p=*/false);
12738 /* If the next token is `extern', 'static' or 'inline' and the one
12739 after that is `template', we have a GNU extended explicit
12740 instantiation directive. */
12741 else if (cp_parser_allow_gnu_extensions_p (parser
)
12742 && (token1
.keyword
== RID_EXTERN
12743 || token1
.keyword
== RID_STATIC
12744 || token1
.keyword
== RID_INLINE
)
12745 && token2
.keyword
== RID_TEMPLATE
)
12746 cp_parser_explicit_instantiation (parser
);
12747 /* If the next token is `namespace', check for a named or unnamed
12748 namespace definition. */
12749 else if (token1
.keyword
== RID_NAMESPACE
12750 && (/* A named namespace definition. */
12751 (token2
.type
== CPP_NAME
12752 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12754 || (token2
.type
== CPP_OPEN_SQUARE
12755 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12756 == CPP_OPEN_SQUARE
)
12757 /* An unnamed namespace definition. */
12758 || token2
.type
== CPP_OPEN_BRACE
12759 || token2
.keyword
== RID_ATTRIBUTE
))
12760 cp_parser_namespace_definition (parser
);
12761 /* An inline (associated) namespace definition. */
12762 else if (token1
.keyword
== RID_INLINE
12763 && token2
.keyword
== RID_NAMESPACE
)
12764 cp_parser_namespace_definition (parser
);
12765 /* Objective-C++ declaration/definition. */
12766 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12767 cp_parser_objc_declaration (parser
, NULL_TREE
);
12768 else if (c_dialect_objc ()
12769 && token1
.keyword
== RID_ATTRIBUTE
12770 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12771 cp_parser_objc_declaration (parser
, attributes
);
12772 /* At this point we may have a template declared by a concept
12774 else if (flag_concepts
12775 && cp_parser_template_declaration_after_export (parser
,
12776 /*member_p=*/false))
12779 /* Try to parse a block-declaration, or a function-definition. */
12780 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12782 /* Free any declarators allocated. */
12783 obstack_free (&declarator_obstack
, p
);
12786 /* Parse a block-declaration.
12791 namespace-alias-definition
12798 __extension__ block-declaration
12803 static_assert-declaration
12805 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12806 part of a declaration-statement. */
12809 cp_parser_block_declaration (cp_parser
*parser
,
12813 int saved_pedantic
;
12815 /* Check for the `__extension__' keyword. */
12816 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12818 /* Parse the qualified declaration. */
12819 cp_parser_block_declaration (parser
, statement_p
);
12820 /* Restore the PEDANTIC flag. */
12821 pedantic
= saved_pedantic
;
12826 /* Peek at the next token to figure out which kind of declaration is
12828 token1
= cp_lexer_peek_token (parser
->lexer
);
12830 /* If the next keyword is `asm', we have an asm-definition. */
12831 if (token1
->keyword
== RID_ASM
)
12834 cp_parser_commit_to_tentative_parse (parser
);
12835 cp_parser_asm_definition (parser
);
12837 /* If the next keyword is `namespace', we have a
12838 namespace-alias-definition. */
12839 else if (token1
->keyword
== RID_NAMESPACE
)
12840 cp_parser_namespace_alias_definition (parser
);
12841 /* If the next keyword is `using', we have a
12842 using-declaration, a using-directive, or an alias-declaration. */
12843 else if (token1
->keyword
== RID_USING
)
12848 cp_parser_commit_to_tentative_parse (parser
);
12849 /* If the token after `using' is `namespace', then we have a
12850 using-directive. */
12851 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12852 if (token2
->keyword
== RID_NAMESPACE
)
12853 cp_parser_using_directive (parser
);
12854 /* If the second token after 'using' is '=', then we have an
12855 alias-declaration. */
12856 else if (cxx_dialect
>= cxx11
12857 && token2
->type
== CPP_NAME
12858 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12859 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12860 cp_parser_alias_declaration (parser
);
12861 /* Otherwise, it's a using-declaration. */
12863 cp_parser_using_declaration (parser
,
12864 /*access_declaration_p=*/false);
12866 /* If the next keyword is `__label__' we have a misplaced label
12868 else if (token1
->keyword
== RID_LABEL
)
12870 cp_lexer_consume_token (parser
->lexer
);
12871 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12872 cp_parser_skip_to_end_of_statement (parser
);
12873 /* If the next token is now a `;', consume it. */
12874 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12875 cp_lexer_consume_token (parser
->lexer
);
12877 /* If the next token is `static_assert' we have a static assertion. */
12878 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12879 cp_parser_static_assert (parser
, /*member_p=*/false);
12880 /* Anything else must be a simple-declaration. */
12882 cp_parser_simple_declaration (parser
, !statement_p
,
12883 /*maybe_range_for_decl*/NULL
);
12886 /* Parse a simple-declaration.
12888 simple-declaration:
12889 decl-specifier-seq [opt] init-declarator-list [opt] ;
12890 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12891 brace-or-equal-initializer ;
12893 init-declarator-list:
12895 init-declarator-list , init-declarator
12897 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12898 function-definition as a simple-declaration.
12900 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12901 parsed declaration if it is an uninitialized single declarator not followed
12902 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12903 if present, will not be consumed. */
12906 cp_parser_simple_declaration (cp_parser
* parser
,
12907 bool function_definition_allowed_p
,
12908 tree
*maybe_range_for_decl
)
12910 cp_decl_specifier_seq decl_specifiers
;
12911 int declares_class_or_enum
;
12912 bool saw_declarator
;
12913 location_t comma_loc
= UNKNOWN_LOCATION
;
12914 location_t init_loc
= UNKNOWN_LOCATION
;
12916 if (maybe_range_for_decl
)
12917 *maybe_range_for_decl
= NULL_TREE
;
12919 /* Defer access checks until we know what is being declared; the
12920 checks for names appearing in the decl-specifier-seq should be
12921 done as if we were in the scope of the thing being declared. */
12922 push_deferring_access_checks (dk_deferred
);
12924 /* Parse the decl-specifier-seq. We have to keep track of whether
12925 or not the decl-specifier-seq declares a named class or
12926 enumeration type, since that is the only case in which the
12927 init-declarator-list is allowed to be empty.
12931 In a simple-declaration, the optional init-declarator-list can be
12932 omitted only when declaring a class or enumeration, that is when
12933 the decl-specifier-seq contains either a class-specifier, an
12934 elaborated-type-specifier, or an enum-specifier. */
12935 cp_parser_decl_specifier_seq (parser
,
12936 CP_PARSER_FLAGS_OPTIONAL
,
12938 &declares_class_or_enum
);
12939 /* We no longer need to defer access checks. */
12940 stop_deferring_access_checks ();
12942 /* In a block scope, a valid declaration must always have a
12943 decl-specifier-seq. By not trying to parse declarators, we can
12944 resolve the declaration/expression ambiguity more quickly. */
12945 if (!function_definition_allowed_p
12946 && !decl_specifiers
.any_specifiers_p
)
12948 cp_parser_error (parser
, "expected declaration");
12952 /* If the next two tokens are both identifiers, the code is
12953 erroneous. The usual cause of this situation is code like:
12957 where "T" should name a type -- but does not. */
12958 if (!decl_specifiers
.any_type_specifiers_p
12959 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12961 /* If parsing tentatively, we should commit; we really are
12962 looking at a declaration. */
12963 cp_parser_commit_to_tentative_parse (parser
);
12968 /* If we have seen at least one decl-specifier, and the next token
12969 is not a parenthesis, then we must be looking at a declaration.
12970 (After "int (" we might be looking at a functional cast.) */
12971 if (decl_specifiers
.any_specifiers_p
12972 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12973 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12974 && !cp_parser_error_occurred (parser
))
12975 cp_parser_commit_to_tentative_parse (parser
);
12977 /* Look for C++17 decomposition declaration. */
12978 for (size_t n
= 1; ; n
++)
12979 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
12980 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
12982 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
12983 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
12984 && decl_specifiers
.any_specifiers_p
)
12987 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
12988 maybe_range_for_decl
,
12991 /* The next token should be either a `,' or a `;'. */
12992 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12993 /* If it's a `;', we are done. */
12994 if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12996 /* Anything else is an error. */
12999 /* If we have already issued an error message we don't need
13000 to issue another one. */
13001 if ((decl
!= error_mark_node
13002 && DECL_INITIAL (decl
) != error_mark_node
)
13003 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13004 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13005 /* Skip tokens until we reach the end of the statement. */
13006 cp_parser_skip_to_end_of_statement (parser
);
13007 /* If the next token is now a `;', consume it. */
13008 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13009 cp_lexer_consume_token (parser
->lexer
);
13017 bool auto_specifier_p
;
13018 /* NULL_TREE if both variable and function declaration are allowed,
13019 error_mark_node if function declaration are not allowed and
13020 a FUNCTION_DECL that should be diagnosed if it is followed by
13021 variable declarations. */
13022 tree auto_function_declaration
;
13024 last_type
= NULL_TREE
;
13026 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13027 auto_function_declaration
= NULL_TREE
;
13029 /* Keep going until we hit the `;' at the end of the simple
13031 saw_declarator
= false;
13032 while (cp_lexer_next_token_is_not (parser
->lexer
,
13036 bool function_definition_p
;
13038 tree auto_result
= NULL_TREE
;
13040 if (saw_declarator
)
13042 /* If we are processing next declarator, comma is expected */
13043 token
= cp_lexer_peek_token (parser
->lexer
);
13044 gcc_assert (token
->type
== CPP_COMMA
);
13045 cp_lexer_consume_token (parser
->lexer
);
13046 if (maybe_range_for_decl
)
13048 *maybe_range_for_decl
= error_mark_node
;
13049 if (comma_loc
== UNKNOWN_LOCATION
)
13050 comma_loc
= token
->location
;
13054 saw_declarator
= true;
13056 /* Parse the init-declarator. */
13057 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13059 function_definition_allowed_p
,
13060 /*member_p=*/false,
13061 declares_class_or_enum
,
13062 &function_definition_p
,
13063 maybe_range_for_decl
,
13066 /* If an error occurred while parsing tentatively, exit quickly.
13067 (That usually happens when in the body of a function; each
13068 statement is treated as a declaration-statement until proven
13070 if (cp_parser_error_occurred (parser
))
13073 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13075 /* If the init-declarator-list contains more than one
13076 init-declarator, they shall all form declarations of
13078 if (auto_function_declaration
== NULL_TREE
)
13079 auto_function_declaration
13080 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13081 else if (TREE_CODE (decl
) == FUNCTION_DECL
13082 || auto_function_declaration
!= error_mark_node
)
13084 error_at (decl_specifiers
.locations
[ds_type_spec
],
13085 "non-variable %qD in declaration with more than one "
13086 "declarator with placeholder type",
13087 TREE_CODE (decl
) == FUNCTION_DECL
13088 ? decl
: auto_function_declaration
);
13089 auto_function_declaration
= error_mark_node
;
13094 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13097 && last_type
!= error_mark_node
13098 && !same_type_p (auto_result
, last_type
))
13100 /* If the list of declarators contains more than one declarator,
13101 the type of each declared variable is determined as described
13102 above. If the type deduced for the template parameter U is not
13103 the same in each deduction, the program is ill-formed. */
13104 error_at (decl_specifiers
.locations
[ds_type_spec
],
13105 "inconsistent deduction for %qT: %qT and then %qT",
13106 decl_specifiers
.type
, last_type
, auto_result
);
13107 last_type
= error_mark_node
;
13110 last_type
= auto_result
;
13113 /* Handle function definitions specially. */
13114 if (function_definition_p
)
13116 /* If the next token is a `,', then we are probably
13117 processing something like:
13121 which is erroneous. */
13122 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13124 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13125 error_at (token
->location
,
13127 " declarations and function-definitions is forbidden");
13129 /* Otherwise, we're done with the list of declarators. */
13132 pop_deferring_access_checks ();
13136 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13137 *maybe_range_for_decl
= decl
;
13138 /* The next token should be either a `,' or a `;'. */
13139 token
= cp_lexer_peek_token (parser
->lexer
);
13140 /* If it's a `,', there are more declarators to come. */
13141 if (token
->type
== CPP_COMMA
)
13142 /* will be consumed next time around */;
13143 /* If it's a `;', we are done. */
13144 else if (token
->type
== CPP_SEMICOLON
)
13146 else if (maybe_range_for_decl
)
13148 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13149 permerror (decl_specifiers
.locations
[ds_type_spec
],
13150 "types may not be defined in a for-range-declaration");
13153 /* Anything else is an error. */
13156 /* If we have already issued an error message we don't need
13157 to issue another one. */
13158 if ((decl
!= error_mark_node
13159 && DECL_INITIAL (decl
) != error_mark_node
)
13160 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13161 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13162 /* Skip tokens until we reach the end of the statement. */
13163 cp_parser_skip_to_end_of_statement (parser
);
13164 /* If the next token is now a `;', consume it. */
13165 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13166 cp_lexer_consume_token (parser
->lexer
);
13169 /* After the first time around, a function-definition is not
13170 allowed -- even if it was OK at first. For example:
13175 function_definition_allowed_p
= false;
13178 /* Issue an error message if no declarators are present, and the
13179 decl-specifier-seq does not itself declare a class or
13180 enumeration: [dcl.dcl]/3. */
13181 if (!saw_declarator
)
13183 if (cp_parser_declares_only_class_p (parser
))
13185 if (!declares_class_or_enum
13186 && decl_specifiers
.type
13187 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13188 /* Ensure an error is issued anyway when finish_decltype_type,
13189 called via cp_parser_decl_specifier_seq, returns a class or
13190 an enumeration (c++/51786). */
13191 decl_specifiers
.type
= NULL_TREE
;
13192 shadow_tag (&decl_specifiers
);
13194 /* Perform any deferred access checks. */
13195 perform_deferred_access_checks (tf_warning_or_error
);
13198 /* Consume the `;'. */
13200 if (!maybe_range_for_decl
)
13201 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13202 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13204 if (init_loc
!= UNKNOWN_LOCATION
)
13205 error_at (init_loc
, "initializer in range-based %<for%> loop");
13206 if (comma_loc
!= UNKNOWN_LOCATION
)
13207 error_at (comma_loc
,
13208 "multiple declarations in range-based %<for%> loop");
13212 pop_deferring_access_checks ();
13215 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13216 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13220 cp_parser_decomposition_declaration (cp_parser
*parser
,
13221 cp_decl_specifier_seq
*decl_specifiers
,
13222 tree
*maybe_range_for_decl
,
13223 location_t
*init_loc
)
13225 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13226 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13227 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13229 /* Parse the identifier-list. */
13230 auto_vec
<cp_expr
, 10> v
;
13231 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13234 cp_expr e
= cp_parser_identifier (parser
);
13235 if (e
.get_value () == error_mark_node
)
13238 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13240 cp_lexer_consume_token (parser
->lexer
);
13243 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13244 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13246 end_loc
= UNKNOWN_LOCATION
;
13247 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13250 cp_lexer_consume_token (parser
->lexer
);
13253 cp_parser_skip_to_end_of_statement (parser
);
13254 return error_mark_node
;
13258 if (cxx_dialect
< cxx17
)
13259 pedwarn (loc
, 0, "structured bindings only available with "
13260 "-std=c++17 or -std=gnu++17");
13263 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13264 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13265 declarator
->id_loc
= loc
;
13266 if (ref_qual
!= REF_QUAL_NONE
)
13267 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13268 ref_qual
== REF_QUAL_RVALUE
,
13270 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13271 NULL_TREE
, decl_specifiers
->attributes
,
13273 tree orig_decl
= decl
;
13277 cp_decl_specifier_seq decl_specs
;
13278 clear_decl_specs (&decl_specs
);
13279 decl_specs
.type
= make_auto ();
13281 FOR_EACH_VEC_ELT (v
, i
, e
)
13284 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13286 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13287 declarator
->id_loc
= e
.get_location ();
13288 tree elt_pushed_scope
;
13289 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13290 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13291 if (decl2
== error_mark_node
)
13292 decl
= error_mark_node
;
13293 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13295 /* Ensure we've diagnosed redeclaration if we aren't creating
13297 gcc_assert (errorcount
);
13298 decl
= error_mark_node
;
13302 if (elt_pushed_scope
)
13303 pop_scope (elt_pushed_scope
);
13308 error_at (loc
, "empty structured binding declaration");
13309 decl
= error_mark_node
;
13312 if (maybe_range_for_decl
== NULL
13313 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13315 bool non_constant_p
= false, is_direct_init
= false;
13316 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13317 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13319 if (initializer
== NULL_TREE
13320 || (TREE_CODE (initializer
) == TREE_LIST
13321 && TREE_CHAIN (initializer
))
13323 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13324 && CONSTRUCTOR_NELTS (initializer
) != 1))
13326 error_at (loc
, "invalid initializer for structured binding "
13328 initializer
= error_mark_node
;
13331 if (decl
!= error_mark_node
)
13333 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13334 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13335 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13336 cp_finish_decomp (decl
, prev
, v
.length ());
13339 else if (decl
!= error_mark_node
)
13341 *maybe_range_for_decl
= prev
;
13342 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13343 the underlying DECL. */
13344 cp_finish_decomp (decl
, prev
, v
.length ());
13348 pop_scope (pushed_scope
);
13350 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13352 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13353 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13359 /* Parse a decl-specifier-seq.
13361 decl-specifier-seq:
13362 decl-specifier-seq [opt] decl-specifier
13363 decl-specifier attribute-specifier-seq [opt] (C++11)
13366 storage-class-specifier
13377 Concepts Extension:
13382 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13384 The parser flags FLAGS is used to control type-specifier parsing.
13386 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13389 1: one of the decl-specifiers is an elaborated-type-specifier
13390 (i.e., a type declaration)
13391 2: one of the decl-specifiers is an enum-specifier or a
13392 class-specifier (i.e., a type definition)
13397 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13398 cp_parser_flags flags
,
13399 cp_decl_specifier_seq
*decl_specs
,
13400 int* declares_class_or_enum
)
13402 bool constructor_possible_p
= !parser
->in_declarator_p
;
13403 bool found_decl_spec
= false;
13404 cp_token
*start_token
= NULL
;
13407 /* Clear DECL_SPECS. */
13408 clear_decl_specs (decl_specs
);
13410 /* Assume no class or enumeration type is declared. */
13411 *declares_class_or_enum
= 0;
13413 /* Keep reading specifiers until there are no more to read. */
13416 bool constructor_p
;
13420 /* Peek at the next token. */
13421 token
= cp_lexer_peek_token (parser
->lexer
);
13423 /* Save the first token of the decl spec list for error
13426 start_token
= token
;
13427 /* Handle attributes. */
13428 if (cp_next_tokens_can_be_attribute_p (parser
))
13430 /* Parse the attributes. */
13431 tree attrs
= cp_parser_attributes_opt (parser
);
13433 /* In a sequence of declaration specifiers, c++11 attributes
13434 appertain to the type that precede them. In that case
13437 The attribute-specifier-seq affects the type only for
13438 the declaration it appears in, not other declarations
13439 involving the same type.
13441 But for now let's force the user to position the
13442 attribute either at the beginning of the declaration or
13443 after the declarator-id, which would clearly mean that it
13444 applies to the declarator. */
13445 if (cxx11_attribute_p (attrs
))
13447 if (!found_decl_spec
)
13448 /* The c++11 attribute is at the beginning of the
13449 declaration. It appertains to the entity being
13453 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13455 /* This is an attribute following a
13456 class-specifier. */
13457 if (decl_specs
->type_definition_p
)
13458 warn_misplaced_attr_for_class_type (token
->location
,
13464 decl_specs
->std_attributes
13465 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13466 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13467 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13473 decl_specs
->attributes
13474 = attr_chainon (decl_specs
->attributes
, attrs
);
13475 if (decl_specs
->locations
[ds_attribute
] == 0)
13476 decl_specs
->locations
[ds_attribute
] = token
->location
;
13479 /* Assume we will find a decl-specifier keyword. */
13480 found_decl_spec
= true;
13481 /* If the next token is an appropriate keyword, we can simply
13482 add it to the list. */
13483 switch (token
->keyword
)
13489 if (!at_class_scope_p ())
13491 gcc_rich_location
richloc (token
->location
);
13492 richloc
.add_fixit_remove ();
13493 error_at (&richloc
, "%<friend%> used outside of class");
13494 cp_lexer_purge_token (parser
->lexer
);
13499 /* Consume the token. */
13500 cp_lexer_consume_token (parser
->lexer
);
13504 case RID_CONSTEXPR
:
13506 cp_lexer_consume_token (parser
->lexer
);
13511 cp_lexer_consume_token (parser
->lexer
);
13514 /* function-specifier:
13521 cp_parser_function_specifier_opt (parser
, decl_specs
);
13528 /* Consume the token. */
13529 cp_lexer_consume_token (parser
->lexer
);
13530 /* A constructor declarator cannot appear in a typedef. */
13531 constructor_possible_p
= false;
13532 /* The "typedef" keyword can only occur in a declaration; we
13533 may as well commit at this point. */
13534 cp_parser_commit_to_tentative_parse (parser
);
13536 if (decl_specs
->storage_class
!= sc_none
)
13537 decl_specs
->conflicting_specifiers_p
= true;
13540 /* storage-class-specifier:
13550 if (cxx_dialect
== cxx98
)
13552 /* Consume the token. */
13553 cp_lexer_consume_token (parser
->lexer
);
13555 /* Complain about `auto' as a storage specifier, if
13556 we're complaining about C++0x compatibility. */
13557 gcc_rich_location
richloc (token
->location
);
13558 richloc
.add_fixit_remove ();
13559 warning_at (&richloc
, OPT_Wc__11_compat
,
13560 "%<auto%> changes meaning in C++11; "
13561 "please remove it");
13563 /* Set the storage class anyway. */
13564 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13568 /* C++0x auto type-specifier. */
13569 found_decl_spec
= false;
13576 /* Consume the token. */
13577 cp_lexer_consume_token (parser
->lexer
);
13578 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13582 /* Consume the token. */
13584 cp_lexer_consume_token (parser
->lexer
);
13588 /* We did not yet find a decl-specifier yet. */
13589 found_decl_spec
= false;
13593 if (found_decl_spec
13594 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13595 && token
->keyword
!= RID_CONSTEXPR
)
13596 error ("decl-specifier invalid in condition");
13598 if (found_decl_spec
13599 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13600 && token
->keyword
!= RID_MUTABLE
13601 && token
->keyword
!= RID_CONSTEXPR
)
13602 error_at (token
->location
, "%qD invalid in lambda",
13603 ridpointers
[token
->keyword
]);
13606 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13608 /* Constructors are a special case. The `S' in `S()' is not a
13609 decl-specifier; it is the beginning of the declarator. */
13611 = (!found_decl_spec
13612 && constructor_possible_p
13613 && (cp_parser_constructor_declarator_p
13614 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13616 /* If we don't have a DECL_SPEC yet, then we must be looking at
13617 a type-specifier. */
13618 if (!found_decl_spec
&& !constructor_p
)
13620 int decl_spec_declares_class_or_enum
;
13621 bool is_cv_qualifier
;
13625 = cp_parser_type_specifier (parser
, flags
,
13627 /*is_declaration=*/true,
13628 &decl_spec_declares_class_or_enum
,
13630 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13632 /* If this type-specifier referenced a user-defined type
13633 (a typedef, class-name, etc.), then we can't allow any
13634 more such type-specifiers henceforth.
13638 The longest sequence of decl-specifiers that could
13639 possibly be a type name is taken as the
13640 decl-specifier-seq of a declaration. The sequence shall
13641 be self-consistent as described below.
13645 As a general rule, at most one type-specifier is allowed
13646 in the complete decl-specifier-seq of a declaration. The
13647 only exceptions are the following:
13649 -- const or volatile can be combined with any other
13652 -- signed or unsigned can be combined with char, long,
13660 void g (const int Pc);
13662 Here, Pc is *not* part of the decl-specifier seq; it's
13663 the declarator. Therefore, once we see a type-specifier
13664 (other than a cv-qualifier), we forbid any additional
13665 user-defined types. We *do* still allow things like `int
13666 int' to be considered a decl-specifier-seq, and issue the
13667 error message later. */
13668 if (type_spec
&& !is_cv_qualifier
)
13669 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13670 /* A constructor declarator cannot follow a type-specifier. */
13673 constructor_possible_p
= false;
13674 found_decl_spec
= true;
13675 if (!is_cv_qualifier
)
13676 decl_specs
->any_type_specifiers_p
= true;
13680 /* If we still do not have a DECL_SPEC, then there are no more
13681 decl-specifiers. */
13682 if (!found_decl_spec
)
13685 decl_specs
->any_specifiers_p
= true;
13686 /* After we see one decl-specifier, further decl-specifiers are
13687 always optional. */
13688 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13691 /* Don't allow a friend specifier with a class definition. */
13692 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13693 && (*declares_class_or_enum
& 2))
13694 error_at (decl_specs
->locations
[ds_friend
],
13695 "class definition may not be declared a friend");
13698 /* Parse an (optional) storage-class-specifier.
13700 storage-class-specifier:
13709 storage-class-specifier:
13712 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13715 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13717 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13720 if (cxx_dialect
!= cxx98
)
13722 /* Fall through for C++98. */
13723 gcc_fallthrough ();
13730 /* Consume the token. */
13731 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13738 /* Parse an (optional) function-specifier.
13740 function-specifier:
13745 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13746 Updates DECL_SPECS, if it is non-NULL. */
13749 cp_parser_function_specifier_opt (cp_parser
* parser
,
13750 cp_decl_specifier_seq
*decl_specs
)
13752 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13753 switch (token
->keyword
)
13756 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13760 /* 14.5.2.3 [temp.mem]
13762 A member function template shall not be virtual. */
13763 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13764 && current_class_type
)
13765 error_at (token
->location
, "templates may not be %<virtual%>");
13767 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13771 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13778 /* Consume the token. */
13779 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13782 /* Parse a linkage-specification.
13784 linkage-specification:
13785 extern string-literal { declaration-seq [opt] }
13786 extern string-literal declaration */
13789 cp_parser_linkage_specification (cp_parser
* parser
)
13793 /* Look for the `extern' keyword. */
13794 cp_token
*extern_token
13795 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13797 /* Look for the string-literal. */
13798 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13799 linkage
= cp_parser_string_literal (parser
, false, false);
13801 /* Transform the literal into an identifier. If the literal is a
13802 wide-character string, or contains embedded NULs, then we can't
13803 handle it as the user wants. */
13804 if (strlen (TREE_STRING_POINTER (linkage
))
13805 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13807 cp_parser_error (parser
, "invalid linkage-specification");
13808 /* Assume C++ linkage. */
13809 linkage
= lang_name_cplusplus
;
13812 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13814 /* We're now using the new linkage. */
13815 push_lang_context (linkage
);
13817 /* Preserve the location of the the innermost linkage specification,
13818 tracking the locations of nested specifications via a local. */
13819 location_t saved_location
13820 = parser
->innermost_linkage_specification_location
;
13821 /* Construct a location ranging from the start of the "extern" to
13822 the end of the string-literal, with the caret at the start, e.g.:
13826 parser
->innermost_linkage_specification_location
13827 = make_location (extern_token
->location
,
13828 extern_token
->location
,
13829 get_finish (string_token
->location
));
13831 /* If the next token is a `{', then we're using the first
13833 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13835 cp_ensure_no_omp_declare_simd (parser
);
13836 cp_ensure_no_oacc_routine (parser
);
13838 /* Consume the `{' token. */
13839 matching_braces braces
;
13840 braces
.consume_open (parser
)->location
;
13841 /* Parse the declarations. */
13842 cp_parser_declaration_seq_opt (parser
);
13843 /* Look for the closing `}'. */
13844 braces
.require_close (parser
);
13846 /* Otherwise, there's just one declaration. */
13849 bool saved_in_unbraced_linkage_specification_p
;
13851 saved_in_unbraced_linkage_specification_p
13852 = parser
->in_unbraced_linkage_specification_p
;
13853 parser
->in_unbraced_linkage_specification_p
= true;
13854 cp_parser_declaration (parser
);
13855 parser
->in_unbraced_linkage_specification_p
13856 = saved_in_unbraced_linkage_specification_p
;
13859 /* We're done with the linkage-specification. */
13860 pop_lang_context ();
13862 /* Restore location of parent linkage specification, if any. */
13863 parser
->innermost_linkage_specification_location
= saved_location
;
13866 /* Parse a static_assert-declaration.
13868 static_assert-declaration:
13869 static_assert ( constant-expression , string-literal ) ;
13870 static_assert ( constant-expression ) ; (C++17)
13872 If MEMBER_P, this static_assert is a class member. */
13875 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13878 location_t token_loc
;
13882 /* Peek at the `static_assert' token so we can keep track of exactly
13883 where the static assertion started. */
13884 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13886 /* Look for the `static_assert' keyword. */
13887 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13891 /* We know we are in a static assertion; commit to any tentative
13893 if (cp_parser_parsing_tentatively (parser
))
13894 cp_parser_commit_to_tentative_parse (parser
);
13896 /* Parse the `(' starting the static assertion condition. */
13897 matching_parens parens
;
13898 parens
.require_open (parser
);
13900 /* Parse the constant-expression. Allow a non-constant expression
13901 here in order to give better diagnostics in finish_static_assert. */
13903 cp_parser_constant_expression (parser
,
13904 /*allow_non_constant_p=*/true,
13905 /*non_constant_p=*/&dummy
);
13907 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13909 if (cxx_dialect
< cxx17
)
13910 pedwarn (input_location
, OPT_Wpedantic
,
13911 "static_assert without a message "
13912 "only available with -std=c++17 or -std=gnu++17");
13914 cp_lexer_consume_token (parser
->lexer
);
13915 message
= build_string (1, "");
13916 TREE_TYPE (message
) = char_array_type_node
;
13917 fix_string_type (message
);
13921 /* Parse the separating `,'. */
13922 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13924 /* Parse the string-literal message. */
13925 message
= cp_parser_string_literal (parser
,
13926 /*translate=*/false,
13929 /* A `)' completes the static assertion. */
13930 if (!parens
.require_close (parser
))
13931 cp_parser_skip_to_closing_parenthesis (parser
,
13932 /*recovering=*/true,
13933 /*or_comma=*/false,
13934 /*consume_paren=*/true);
13937 /* A semicolon terminates the declaration. */
13938 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13940 /* Get the location for the static assertion. Use that of the
13941 condition if available, otherwise, use that of the "static_assert"
13943 location_t assert_loc
= condition
.get_location ();
13944 if (assert_loc
== UNKNOWN_LOCATION
)
13945 assert_loc
= token_loc
;
13947 /* Complete the static assertion, which may mean either processing
13948 the static assert now or saving it for template instantiation. */
13949 finish_static_assert (condition
, message
, assert_loc
, member_p
);
13952 /* Parse the expression in decltype ( expression ). */
13955 cp_parser_decltype_expr (cp_parser
*parser
,
13956 bool &id_expression_or_member_access_p
)
13958 cp_token
*id_expr_start_token
;
13961 /* Since we're going to preserve any side-effects from this parse, set up a
13962 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13963 in the expression. */
13964 tentative_firewall
firewall (parser
);
13966 /* First, try parsing an id-expression. */
13967 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13968 cp_parser_parse_tentatively (parser
);
13969 expr
= cp_parser_id_expression (parser
,
13970 /*template_keyword_p=*/false,
13971 /*check_dependency_p=*/true,
13972 /*template_p=*/NULL
,
13973 /*declarator_p=*/false,
13974 /*optional_p=*/false);
13976 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13978 bool non_integral_constant_expression_p
= false;
13979 tree id_expression
= expr
;
13981 const char *error_msg
;
13983 if (identifier_p (expr
))
13984 /* Lookup the name we got back from the id-expression. */
13985 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13986 id_expr_start_token
->location
);
13988 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
13989 /* A template without args is not a complete id-expression. */
13990 expr
= error_mark_node
;
13993 && expr
!= error_mark_node
13994 && TREE_CODE (expr
) != TYPE_DECL
13995 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13996 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13997 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13999 /* Complete lookup of the id-expression. */
14000 expr
= (finish_id_expression
14001 (id_expression
, expr
, parser
->scope
, &idk
,
14002 /*integral_constant_expression_p=*/false,
14003 /*allow_non_integral_constant_expression_p=*/true,
14004 &non_integral_constant_expression_p
,
14005 /*template_p=*/false,
14007 /*address_p=*/false,
14008 /*template_arg_p=*/false,
14010 id_expr_start_token
->location
));
14012 if (expr
== error_mark_node
)
14013 /* We found an id-expression, but it was something that we
14014 should not have found. This is an error, not something
14015 we can recover from, so note that we found an
14016 id-expression and we'll recover as gracefully as
14018 id_expression_or_member_access_p
= true;
14022 && expr
!= error_mark_node
14023 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14024 /* We have an id-expression. */
14025 id_expression_or_member_access_p
= true;
14028 if (!id_expression_or_member_access_p
)
14030 /* Abort the id-expression parse. */
14031 cp_parser_abort_tentative_parse (parser
);
14033 /* Parsing tentatively, again. */
14034 cp_parser_parse_tentatively (parser
);
14036 /* Parse a class member access. */
14037 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14038 /*cast_p=*/false, /*decltype*/true,
14039 /*member_access_only_p=*/true, NULL
);
14042 && expr
!= error_mark_node
14043 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14044 /* We have an id-expression. */
14045 id_expression_or_member_access_p
= true;
14048 if (id_expression_or_member_access_p
)
14049 /* We have parsed the complete id-expression or member access. */
14050 cp_parser_parse_definitely (parser
);
14053 /* Abort our attempt to parse an id-expression or member access
14055 cp_parser_abort_tentative_parse (parser
);
14057 /* Commit to the tentative_firewall so we get syntax errors. */
14058 cp_parser_commit_to_tentative_parse (parser
);
14060 /* Parse a full expression. */
14061 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14062 /*decltype_p=*/true);
14068 /* Parse a `decltype' type. Returns the type.
14070 simple-type-specifier:
14071 decltype ( expression )
14073 decltype ( auto ) */
14076 cp_parser_decltype (cp_parser
*parser
)
14078 bool id_expression_or_member_access_p
= false;
14079 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14081 if (start_token
->type
== CPP_DECLTYPE
)
14083 /* Already parsed. */
14084 cp_lexer_consume_token (parser
->lexer
);
14085 return saved_checks_value (start_token
->u
.tree_check_value
);
14088 /* Look for the `decltype' token. */
14089 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14090 return error_mark_node
;
14092 /* Parse the opening `('. */
14093 matching_parens parens
;
14094 if (!parens
.require_open (parser
))
14095 return error_mark_node
;
14097 push_deferring_access_checks (dk_deferred
);
14099 tree expr
= NULL_TREE
;
14101 if (cxx_dialect
>= cxx14
14102 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14103 /* decltype (auto) */
14104 cp_lexer_consume_token (parser
->lexer
);
14107 /* decltype (expression) */
14109 /* Types cannot be defined in a `decltype' expression. Save away the
14110 old message and set the new one. */
14111 const char *saved_message
= parser
->type_definition_forbidden_message
;
14112 parser
->type_definition_forbidden_message
14113 = G_("types may not be defined in %<decltype%> expressions");
14115 /* The restrictions on constant-expressions do not apply inside
14116 decltype expressions. */
14117 bool saved_integral_constant_expression_p
14118 = parser
->integral_constant_expression_p
;
14119 bool saved_non_integral_constant_expression_p
14120 = parser
->non_integral_constant_expression_p
;
14121 parser
->integral_constant_expression_p
= false;
14123 /* Within a parenthesized expression, a `>' token is always
14124 the greater-than operator. */
14125 bool saved_greater_than_is_operator_p
14126 = parser
->greater_than_is_operator_p
;
14127 parser
->greater_than_is_operator_p
= true;
14129 /* Do not actually evaluate the expression. */
14130 ++cp_unevaluated_operand
;
14132 /* Do not warn about problems with the expression. */
14133 ++c_inhibit_evaluation_warnings
;
14135 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14137 /* Go back to evaluating expressions. */
14138 --cp_unevaluated_operand
;
14139 --c_inhibit_evaluation_warnings
;
14141 /* The `>' token might be the end of a template-id or
14142 template-parameter-list now. */
14143 parser
->greater_than_is_operator_p
14144 = saved_greater_than_is_operator_p
;
14146 /* Restore the old message and the integral constant expression
14148 parser
->type_definition_forbidden_message
= saved_message
;
14149 parser
->integral_constant_expression_p
14150 = saved_integral_constant_expression_p
;
14151 parser
->non_integral_constant_expression_p
14152 = saved_non_integral_constant_expression_p
;
14155 /* Parse to the closing `)'. */
14156 if (!parens
.require_close (parser
))
14158 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14159 /*consume_paren=*/true);
14160 pop_deferring_access_checks ();
14161 return error_mark_node
;
14167 expr
= make_decltype_auto ();
14168 AUTO_IS_DECLTYPE (expr
) = true;
14171 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14172 tf_warning_or_error
);
14174 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14176 start_token
->type
= CPP_DECLTYPE
;
14177 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14178 start_token
->u
.tree_check_value
->value
= expr
;
14179 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14180 start_token
->keyword
= RID_MAX
;
14181 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14183 pop_to_parent_deferring_access_checks ();
14188 /* Special member functions [gram.special] */
14190 /* Parse a conversion-function-id.
14192 conversion-function-id:
14193 operator conversion-type-id
14195 Returns an IDENTIFIER_NODE representing the operator. */
14198 cp_parser_conversion_function_id (cp_parser
* parser
)
14202 tree saved_qualifying_scope
;
14203 tree saved_object_scope
;
14204 tree pushed_scope
= NULL_TREE
;
14206 /* Look for the `operator' token. */
14207 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14208 return error_mark_node
;
14209 /* When we parse the conversion-type-id, the current scope will be
14210 reset. However, we need that information in able to look up the
14211 conversion function later, so we save it here. */
14212 saved_scope
= parser
->scope
;
14213 saved_qualifying_scope
= parser
->qualifying_scope
;
14214 saved_object_scope
= parser
->object_scope
;
14215 /* We must enter the scope of the class so that the names of
14216 entities declared within the class are available in the
14217 conversion-type-id. For example, consider:
14224 S::operator I() { ... }
14226 In order to see that `I' is a type-name in the definition, we
14227 must be in the scope of `S'. */
14229 pushed_scope
= push_scope (saved_scope
);
14230 /* Parse the conversion-type-id. */
14231 type
= cp_parser_conversion_type_id (parser
);
14232 /* Leave the scope of the class, if any. */
14234 pop_scope (pushed_scope
);
14235 /* Restore the saved scope. */
14236 parser
->scope
= saved_scope
;
14237 parser
->qualifying_scope
= saved_qualifying_scope
;
14238 parser
->object_scope
= saved_object_scope
;
14239 /* If the TYPE is invalid, indicate failure. */
14240 if (type
== error_mark_node
)
14241 return error_mark_node
;
14242 return make_conv_op_name (type
);
14245 /* Parse a conversion-type-id:
14247 conversion-type-id:
14248 type-specifier-seq conversion-declarator [opt]
14250 Returns the TYPE specified. */
14253 cp_parser_conversion_type_id (cp_parser
* parser
)
14256 cp_decl_specifier_seq type_specifiers
;
14257 cp_declarator
*declarator
;
14258 tree type_specified
;
14259 const char *saved_message
;
14261 /* Parse the attributes. */
14262 attributes
= cp_parser_attributes_opt (parser
);
14264 saved_message
= parser
->type_definition_forbidden_message
;
14265 parser
->type_definition_forbidden_message
14266 = G_("types may not be defined in a conversion-type-id");
14268 /* Parse the type-specifiers. */
14269 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14270 /*is_trailing_return=*/false,
14273 parser
->type_definition_forbidden_message
= saved_message
;
14275 /* If that didn't work, stop. */
14276 if (type_specifiers
.type
== error_mark_node
)
14277 return error_mark_node
;
14278 /* Parse the conversion-declarator. */
14279 declarator
= cp_parser_conversion_declarator_opt (parser
);
14281 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14282 /*initialized=*/0, &attributes
);
14284 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14286 /* Don't give this error when parsing tentatively. This happens to
14287 work because we always parse this definitively once. */
14288 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14289 && type_uses_auto (type_specified
))
14291 if (cxx_dialect
< cxx14
)
14293 error ("invalid use of %<auto%> in conversion operator");
14294 return error_mark_node
;
14296 else if (template_parm_scope_p ())
14297 warning (0, "use of %<auto%> in member template "
14298 "conversion operator can never be deduced");
14301 return type_specified
;
14304 /* Parse an (optional) conversion-declarator.
14306 conversion-declarator:
14307 ptr-operator conversion-declarator [opt]
14311 static cp_declarator
*
14312 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14314 enum tree_code code
;
14315 tree class_type
, std_attributes
= NULL_TREE
;
14316 cp_cv_quals cv_quals
;
14318 /* We don't know if there's a ptr-operator next, or not. */
14319 cp_parser_parse_tentatively (parser
);
14320 /* Try the ptr-operator. */
14321 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14323 /* If it worked, look for more conversion-declarators. */
14324 if (cp_parser_parse_definitely (parser
))
14326 cp_declarator
*declarator
;
14328 /* Parse another optional declarator. */
14329 declarator
= cp_parser_conversion_declarator_opt (parser
);
14331 declarator
= cp_parser_make_indirect_declarator
14332 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14340 /* Parse an (optional) ctor-initializer.
14343 : mem-initializer-list */
14346 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14348 /* If the next token is not a `:', then there is no
14349 ctor-initializer. */
14350 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14352 /* Do default initialization of any bases and members. */
14353 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14354 finish_mem_initializers (NULL_TREE
);
14358 /* Consume the `:' token. */
14359 cp_lexer_consume_token (parser
->lexer
);
14360 /* And the mem-initializer-list. */
14361 cp_parser_mem_initializer_list (parser
);
14364 /* Parse a mem-initializer-list.
14366 mem-initializer-list:
14367 mem-initializer ... [opt]
14368 mem-initializer ... [opt] , mem-initializer-list */
14371 cp_parser_mem_initializer_list (cp_parser
* parser
)
14373 tree mem_initializer_list
= NULL_TREE
;
14374 tree target_ctor
= error_mark_node
;
14375 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14377 /* Let the semantic analysis code know that we are starting the
14378 mem-initializer-list. */
14379 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14380 error_at (token
->location
,
14381 "only constructors take member initializers");
14383 /* Loop through the list. */
14386 tree mem_initializer
;
14388 token
= cp_lexer_peek_token (parser
->lexer
);
14389 /* Parse the mem-initializer. */
14390 mem_initializer
= cp_parser_mem_initializer (parser
);
14391 /* If the next token is a `...', we're expanding member initializers. */
14392 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14394 /* Consume the `...'. */
14395 cp_lexer_consume_token (parser
->lexer
);
14397 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14398 can be expanded but members cannot. */
14399 if (mem_initializer
!= error_mark_node
14400 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14402 error_at (token
->location
,
14403 "cannot expand initializer for member %qD",
14404 TREE_PURPOSE (mem_initializer
));
14405 mem_initializer
= error_mark_node
;
14408 /* Construct the pack expansion type. */
14409 if (mem_initializer
!= error_mark_node
)
14410 mem_initializer
= make_pack_expansion (mem_initializer
);
14412 if (target_ctor
!= error_mark_node
14413 && mem_initializer
!= error_mark_node
)
14415 error ("mem-initializer for %qD follows constructor delegation",
14416 TREE_PURPOSE (mem_initializer
));
14417 mem_initializer
= error_mark_node
;
14419 /* Look for a target constructor. */
14420 if (mem_initializer
!= error_mark_node
14421 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14422 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14424 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14425 if (mem_initializer_list
)
14427 error ("constructor delegation follows mem-initializer for %qD",
14428 TREE_PURPOSE (mem_initializer_list
));
14429 mem_initializer
= error_mark_node
;
14431 target_ctor
= mem_initializer
;
14433 /* Add it to the list, unless it was erroneous. */
14434 if (mem_initializer
!= error_mark_node
)
14436 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14437 mem_initializer_list
= mem_initializer
;
14439 /* If the next token is not a `,', we're done. */
14440 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14442 /* Consume the `,' token. */
14443 cp_lexer_consume_token (parser
->lexer
);
14446 /* Perform semantic analysis. */
14447 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14448 finish_mem_initializers (mem_initializer_list
);
14451 /* Parse a mem-initializer.
14454 mem-initializer-id ( expression-list [opt] )
14455 mem-initializer-id braced-init-list
14460 ( expression-list [opt] )
14462 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14463 class) or FIELD_DECL (for a non-static data member) to initialize;
14464 the TREE_VALUE is the expression-list. An empty initialization
14465 list is represented by void_list_node. */
14468 cp_parser_mem_initializer (cp_parser
* parser
)
14470 tree mem_initializer_id
;
14471 tree expression_list
;
14473 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14475 /* Find out what is being initialized. */
14476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14478 permerror (token
->location
,
14479 "anachronistic old-style base class initializer");
14480 mem_initializer_id
= NULL_TREE
;
14484 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14485 if (mem_initializer_id
== error_mark_node
)
14486 return mem_initializer_id
;
14488 member
= expand_member_init (mem_initializer_id
);
14489 if (member
&& !DECL_P (member
))
14490 in_base_initializer
= 1;
14492 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14494 bool expr_non_constant_p
;
14495 cp_lexer_set_source_position (parser
->lexer
);
14496 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14497 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14498 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14499 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14503 vec
<tree
, va_gc
> *vec
;
14504 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14506 /*allow_expansion_p=*/true,
14507 /*non_constant_p=*/NULL
);
14509 return error_mark_node
;
14510 expression_list
= build_tree_list_vec (vec
);
14511 release_tree_vector (vec
);
14514 if (expression_list
== error_mark_node
)
14515 return error_mark_node
;
14516 if (!expression_list
)
14517 expression_list
= void_type_node
;
14519 in_base_initializer
= 0;
14521 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14524 /* Parse a mem-initializer-id.
14526 mem-initializer-id:
14527 :: [opt] nested-name-specifier [opt] class-name
14528 decltype-specifier (C++11)
14531 Returns a TYPE indicating the class to be initialized for the first
14532 production (and the second in C++11). Returns an IDENTIFIER_NODE
14533 indicating the data member to be initialized for the last production. */
14536 cp_parser_mem_initializer_id (cp_parser
* parser
)
14538 bool global_scope_p
;
14539 bool nested_name_specifier_p
;
14540 bool template_p
= false;
14543 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14545 /* `typename' is not allowed in this context ([temp.res]). */
14546 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14548 error_at (token
->location
,
14549 "keyword %<typename%> not allowed in this context (a qualified "
14550 "member initializer is implicitly a type)");
14551 cp_lexer_consume_token (parser
->lexer
);
14553 /* Look for the optional `::' operator. */
14555 = (cp_parser_global_scope_opt (parser
,
14556 /*current_scope_valid_p=*/false)
14558 /* Look for the optional nested-name-specifier. The simplest way to
14563 The keyword `typename' is not permitted in a base-specifier or
14564 mem-initializer; in these contexts a qualified name that
14565 depends on a template-parameter is implicitly assumed to be a
14568 is to assume that we have seen the `typename' keyword at this
14570 nested_name_specifier_p
14571 = (cp_parser_nested_name_specifier_opt (parser
,
14572 /*typename_keyword_p=*/true,
14573 /*check_dependency_p=*/true,
14575 /*is_declaration=*/true)
14577 if (nested_name_specifier_p
)
14578 template_p
= cp_parser_optional_template_keyword (parser
);
14579 /* If there is a `::' operator or a nested-name-specifier, then we
14580 are definitely looking for a class-name. */
14581 if (global_scope_p
|| nested_name_specifier_p
)
14582 return cp_parser_class_name (parser
,
14583 /*typename_keyword_p=*/true,
14584 /*template_keyword_p=*/template_p
,
14586 /*check_dependency_p=*/true,
14587 /*class_head_p=*/false,
14588 /*is_declaration=*/true);
14589 /* Otherwise, we could also be looking for an ordinary identifier. */
14590 cp_parser_parse_tentatively (parser
);
14591 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14592 /* Try a decltype-specifier. */
14593 id
= cp_parser_decltype (parser
);
14595 /* Otherwise, try a class-name. */
14596 id
= cp_parser_class_name (parser
,
14597 /*typename_keyword_p=*/true,
14598 /*template_keyword_p=*/false,
14600 /*check_dependency_p=*/true,
14601 /*class_head_p=*/false,
14602 /*is_declaration=*/true);
14603 /* If we found one, we're done. */
14604 if (cp_parser_parse_definitely (parser
))
14606 /* Otherwise, look for an ordinary identifier. */
14607 return cp_parser_identifier (parser
);
14610 /* Overloading [gram.over] */
14612 /* Parse an operator-function-id.
14614 operator-function-id:
14617 Returns an IDENTIFIER_NODE for the operator which is a
14618 human-readable spelling of the identifier, e.g., `operator +'. */
14621 cp_parser_operator_function_id (cp_parser
* parser
)
14623 /* Look for the `operator' keyword. */
14624 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14625 return error_mark_node
;
14626 /* And then the name of the operator itself. */
14627 return cp_parser_operator (parser
);
14630 /* Return an identifier node for a user-defined literal operator.
14631 The suffix identifier is chained to the operator name identifier. */
14634 cp_literal_operator_id (const char* name
)
14637 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14638 + strlen (name
) + 10);
14639 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14640 identifier
= get_identifier (buffer
);
14645 /* Parse an operator.
14648 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14649 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14650 || ++ -- , ->* -> () []
14657 Returns an IDENTIFIER_NODE for the operator which is a
14658 human-readable spelling of the identifier, e.g., `operator +'. */
14661 cp_parser_operator (cp_parser
* parser
)
14663 tree id
= NULL_TREE
;
14667 /* Peek at the next token. */
14668 token
= cp_lexer_peek_token (parser
->lexer
);
14670 location_t start_loc
= token
->location
;
14672 /* Figure out which operator we have. */
14673 enum tree_code op
= ERROR_MARK
;
14674 bool assop
= false;
14675 bool consumed
= false;
14676 switch (token
->type
)
14680 /* The keyword should be either `new' or `delete'. */
14681 if (token
->keyword
== RID_NEW
)
14683 else if (token
->keyword
== RID_DELETE
)
14688 /* Consume the `new' or `delete' token. */
14689 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14691 /* Peek at the next token. */
14692 token
= cp_lexer_peek_token (parser
->lexer
);
14693 /* If it's a `[' token then this is the array variant of the
14695 if (token
->type
== CPP_OPEN_SQUARE
)
14697 /* Consume the `[' token. */
14698 cp_lexer_consume_token (parser
->lexer
);
14699 /* Look for the `]' token. */
14700 if (cp_token
*close_token
14701 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14702 end_loc
= close_token
->location
;
14703 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14705 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14723 op
= TRUNC_DIV_EXPR
;
14727 op
= TRUNC_MOD_EXPR
;
14747 op
= TRUTH_NOT_EXPR
;
14780 op
= TRUNC_DIV_EXPR
;
14785 op
= TRUNC_MOD_EXPR
;
14811 case CPP_LSHIFT_EQ
:
14816 case CPP_RSHIFT_EQ
:
14833 case CPP_GREATER_EQ
:
14838 op
= TRUTH_ANDIF_EXPR
;
14842 op
= TRUTH_ORIF_EXPR
;
14845 case CPP_PLUS_PLUS
:
14846 op
= POSTINCREMENT_EXPR
;
14849 case CPP_MINUS_MINUS
:
14850 op
= PREDECREMENT_EXPR
;
14854 op
= COMPOUND_EXPR
;
14857 case CPP_DEREF_STAR
:
14862 op
= COMPONENT_REF
;
14865 case CPP_OPEN_PAREN
:
14867 /* Consume the `('. */
14868 matching_parens parens
;
14869 parens
.consume_open (parser
);
14870 /* Look for the matching `)'. */
14871 parens
.require_close (parser
);
14877 case CPP_OPEN_SQUARE
:
14878 /* Consume the `['. */
14879 cp_lexer_consume_token (parser
->lexer
);
14880 /* Look for the matching `]'. */
14881 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14886 case CPP_UTF8STRING
:
14887 case CPP_UTF8STRING_USERDEF
:
14894 case CPP_STRING_USERDEF
:
14895 case CPP_WSTRING_USERDEF
:
14896 case CPP_STRING16_USERDEF
:
14897 case CPP_STRING32_USERDEF
:
14899 tree str
, string_tree
;
14902 if (cxx_dialect
== cxx98
)
14903 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14905 /* Consume the string. */
14906 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14907 /*wide_ok=*/true, /*lookup_udlit=*/false);
14908 if (str
== error_mark_node
)
14909 return error_mark_node
;
14910 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14912 string_tree
= USERDEF_LITERAL_VALUE (str
);
14913 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14918 /* Look for the suffix identifier. */
14919 token
= cp_lexer_peek_token (parser
->lexer
);
14920 if (token
->type
== CPP_NAME
)
14921 id
= cp_parser_identifier (parser
);
14922 else if (token
->type
== CPP_KEYWORD
)
14924 error ("unexpected keyword;"
14925 " remove space between quotes and suffix identifier");
14926 return error_mark_node
;
14930 error ("expected suffix identifier");
14931 return error_mark_node
;
14934 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14935 (TREE_TYPE (TREE_TYPE (string_tree
))));
14936 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14939 error ("expected empty string after %<operator%> keyword");
14940 return error_mark_node
;
14942 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14945 error ("invalid encoding prefix in literal operator");
14946 return error_mark_node
;
14948 if (id
!= error_mark_node
)
14950 const char *name
= IDENTIFIER_POINTER (id
);
14951 id
= cp_literal_operator_id (name
);
14957 /* Anything else is an error. */
14961 /* If we have selected an identifier, we need to consume the
14963 if (op
!= ERROR_MARK
)
14965 id
= ovl_op_identifier (assop
, op
);
14967 cp_lexer_consume_token (parser
->lexer
);
14969 /* Otherwise, no valid operator name was present. */
14972 cp_parser_error (parser
, "expected operator");
14973 id
= error_mark_node
;
14976 return cp_expr (id
, start_loc
);
14979 /* Parse a template-declaration.
14981 template-declaration:
14982 export [opt] template < template-parameter-list > declaration
14984 If MEMBER_P is TRUE, this template-declaration occurs within a
14987 The grammar rule given by the standard isn't correct. What
14988 is really meant is:
14990 template-declaration:
14991 export [opt] template-parameter-list-seq
14992 decl-specifier-seq [opt] init-declarator [opt] ;
14993 export [opt] template-parameter-list-seq
14994 function-definition
14996 template-parameter-list-seq:
14997 template-parameter-list-seq [opt]
14998 template < template-parameter-list >
15000 Concept Extensions:
15002 template-parameter-list-seq:
15003 template < template-parameter-list > requires-clause [opt]
15006 requires logical-or-expression */
15009 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15011 /* Check for `export'. */
15012 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15014 /* Consume the `export' token. */
15015 cp_lexer_consume_token (parser
->lexer
);
15016 /* Warn that we do not support `export'. */
15017 warning (0, "keyword %<export%> not implemented, and will be ignored");
15020 cp_parser_template_declaration_after_export (parser
, member_p
);
15023 /* Parse a template-parameter-list.
15025 template-parameter-list:
15027 template-parameter-list , template-parameter
15029 Returns a TREE_LIST. Each node represents a template parameter.
15030 The nodes are connected via their TREE_CHAINs. */
15033 cp_parser_template_parameter_list (cp_parser
* parser
)
15035 tree parameter_list
= NULL_TREE
;
15037 begin_template_parm_list ();
15039 /* The loop below parses the template parms. We first need to know
15040 the total number of template parms to be able to compute proper
15041 canonical types of each dependent type. So after the loop, when
15042 we know the total number of template parms,
15043 end_template_parm_list computes the proper canonical types and
15044 fixes up the dependent types accordingly. */
15049 bool is_parameter_pack
;
15050 location_t parm_loc
;
15052 /* Parse the template-parameter. */
15053 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15054 parameter
= cp_parser_template_parameter (parser
,
15056 &is_parameter_pack
);
15057 /* Add it to the list. */
15058 if (parameter
!= error_mark_node
)
15059 parameter_list
= process_template_parm (parameter_list
,
15063 is_parameter_pack
);
15066 tree err_parm
= build_tree_list (parameter
, parameter
);
15067 parameter_list
= chainon (parameter_list
, err_parm
);
15070 /* If the next token is not a `,', we're done. */
15071 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15073 /* Otherwise, consume the `,' token. */
15074 cp_lexer_consume_token (parser
->lexer
);
15077 return end_template_parm_list (parameter_list
);
15080 /* Parse a introduction-list.
15083 introduced-parameter
15084 introduction-list , introduced-parameter
15086 introduced-parameter:
15087 ...[opt] identifier
15089 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15090 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15091 WILDCARD_DECL will also have DECL_NAME set and token location in
15092 DECL_SOURCE_LOCATION. */
15095 cp_parser_introduction_list (cp_parser
*parser
)
15097 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15101 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15103 cp_lexer_consume_token (parser
->lexer
);
15105 /* Build placeholder. */
15106 tree parm
= build_nt (WILDCARD_DECL
);
15107 DECL_SOURCE_LOCATION (parm
)
15108 = cp_lexer_peek_token (parser
->lexer
)->location
;
15109 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15110 WILDCARD_PACK_P (parm
) = is_pack
;
15111 vec_safe_push (introduction_vec
, parm
);
15113 /* If the next token is not a `,', we're done. */
15114 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15116 /* Otherwise, consume the `,' token. */
15117 cp_lexer_consume_token (parser
->lexer
);
15120 /* Convert the vec into a TREE_VEC. */
15121 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15124 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15125 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15127 release_tree_vector (introduction_vec
);
15128 return introduction_list
;
15131 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15132 is an abstract declarator. */
15134 static inline cp_declarator
*
15135 get_id_declarator (cp_declarator
*declarator
)
15137 cp_declarator
*d
= declarator
;
15138 while (d
&& d
->kind
!= cdk_id
)
15143 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15144 is an abstract declarator. */
15147 get_unqualified_id (cp_declarator
*declarator
)
15149 declarator
= get_id_declarator (declarator
);
15151 return declarator
->u
.id
.unqualified_name
;
15156 /* Returns true if DECL represents a constrained-parameter. */
15159 is_constrained_parameter (tree decl
)
15162 && TREE_CODE (decl
) == TYPE_DECL
15163 && CONSTRAINED_PARM_CONCEPT (decl
)
15164 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15167 /* Returns true if PARM declares a constrained-parameter. */
15170 is_constrained_parameter (cp_parameter_declarator
*parm
)
15172 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15175 /* Check that the type parameter is only a declarator-id, and that its
15176 type is not cv-qualified. */
15179 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15180 cp_parameter_declarator
*parm
)
15182 if (!parm
->declarator
)
15185 if (parm
->declarator
->kind
!= cdk_id
)
15187 cp_parser_error (parser
, "invalid constrained type parameter");
15191 /* Don't allow cv-qualified type parameters. */
15192 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15193 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15195 cp_parser_error (parser
, "cv-qualified type parameter");
15202 /* Finish parsing/processing a template type parameter and checking
15203 various restrictions. */
15206 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15208 cp_parameter_declarator
* parmdecl
)
15210 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15211 return finish_template_type_parm (class_type_node
, id
);
15213 return error_mark_node
;
15217 finish_constrained_template_template_parm (tree proto
, tree id
)
15219 /* FIXME: This should probably be copied, and we may need to adjust
15220 the template parameter depths. */
15221 tree saved_parms
= current_template_parms
;
15222 begin_template_parm_list ();
15223 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15224 end_template_parm_list ();
15226 tree parm
= finish_template_template_parm (class_type_node
, id
);
15227 current_template_parms
= saved_parms
;
15232 /* Finish parsing/processing a template template parameter by borrowing
15233 the template parameter list from the prototype parameter. */
15236 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15239 cp_parameter_declarator
*parmdecl
)
15241 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15242 return error_mark_node
;
15243 return finish_constrained_template_template_parm (proto
, id
);
15246 /* Create a new non-type template parameter from the given PARM
15250 constrained_non_type_template_parm (bool *is_non_type
,
15251 cp_parameter_declarator
*parm
)
15253 *is_non_type
= true;
15254 cp_declarator
*decl
= parm
->declarator
;
15255 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15256 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15257 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15260 /* Build a constrained template parameter based on the PARMDECL
15261 declarator. The type of PARMDECL is the constrained type, which
15262 refers to the prototype template parameter that ultimately
15263 specifies the type of the declared parameter. */
15266 finish_constrained_parameter (cp_parser
*parser
,
15267 cp_parameter_declarator
*parmdecl
,
15269 bool *is_parameter_pack
)
15271 tree decl
= parmdecl
->decl_specifiers
.type
;
15272 tree id
= get_unqualified_id (parmdecl
->declarator
);
15273 tree def
= parmdecl
->default_argument
;
15274 tree proto
= DECL_INITIAL (decl
);
15276 /* A template parameter constrained by a variadic concept shall also
15277 be declared as a template parameter pack. */
15278 bool is_variadic
= template_parameter_pack_p (proto
);
15279 if (is_variadic
&& !*is_parameter_pack
)
15280 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15282 /* Build the parameter. Return an error if the declarator was invalid. */
15284 if (TREE_CODE (proto
) == TYPE_DECL
)
15285 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15286 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15287 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15290 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15291 if (parm
== error_mark_node
)
15292 return error_mark_node
;
15294 /* Finish the parameter decl and create a node attaching the
15295 default argument and constraint. */
15296 parm
= build_tree_list (def
, parm
);
15297 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15302 /* Returns true if the parsed type actually represents the declaration
15303 of a type template-parameter. */
15306 declares_constrained_type_template_parameter (tree type
)
15308 return (is_constrained_parameter (type
)
15309 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15313 /* Returns true if the parsed type actually represents the declaration of
15314 a template template-parameter. */
15317 declares_constrained_template_template_parameter (tree type
)
15319 return (is_constrained_parameter (type
)
15320 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15323 /* Parse a default argument for a type template-parameter.
15324 Note that diagnostics are handled in cp_parser_template_parameter. */
15327 cp_parser_default_type_template_argument (cp_parser
*parser
)
15329 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15331 /* Consume the `=' token. */
15332 cp_lexer_consume_token (parser
->lexer
);
15334 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15336 /* Parse the default-argument. */
15337 push_deferring_access_checks (dk_no_deferred
);
15338 tree default_argument
= cp_parser_type_id (parser
);
15339 pop_deferring_access_checks ();
15341 if (flag_concepts
&& type_uses_auto (default_argument
))
15343 error_at (token
->location
,
15344 "invalid use of %<auto%> in default template argument");
15345 return error_mark_node
;
15348 return default_argument
;
15351 /* Parse a default argument for a template template-parameter. */
15354 cp_parser_default_template_template_argument (cp_parser
*parser
)
15356 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15360 /* Consume the `='. */
15361 cp_lexer_consume_token (parser
->lexer
);
15362 /* Parse the id-expression. */
15363 push_deferring_access_checks (dk_no_deferred
);
15364 /* save token before parsing the id-expression, for error
15366 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15367 tree default_argument
15368 = cp_parser_id_expression (parser
,
15369 /*template_keyword_p=*/false,
15370 /*check_dependency_p=*/true,
15371 /*template_p=*/&is_template
,
15372 /*declarator_p=*/false,
15373 /*optional_p=*/false);
15374 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15375 /* If the id-expression was a template-id that refers to
15376 a template-class, we already have the declaration here,
15377 so no further lookup is needed. */
15380 /* Look up the name. */
15382 = cp_parser_lookup_name (parser
, default_argument
,
15384 /*is_template=*/is_template
,
15385 /*is_namespace=*/false,
15386 /*check_dependency=*/true,
15387 /*ambiguous_decls=*/NULL
,
15389 /* See if the default argument is valid. */
15390 default_argument
= check_template_template_default_arg (default_argument
);
15391 pop_deferring_access_checks ();
15392 return default_argument
;
15395 /* Parse a template-parameter.
15397 template-parameter:
15399 parameter-declaration
15401 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15402 the parameter. The TREE_PURPOSE is the default value, if any.
15403 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15404 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15405 set to true iff this parameter is a parameter pack. */
15408 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15409 bool *is_parameter_pack
)
15412 cp_parameter_declarator
*parameter_declarator
;
15415 /* Assume it is a type parameter or a template parameter. */
15416 *is_non_type
= false;
15417 /* Assume it not a parameter pack. */
15418 *is_parameter_pack
= false;
15419 /* Peek at the next token. */
15420 token
= cp_lexer_peek_token (parser
->lexer
);
15421 /* If it is `template', we have a type-parameter. */
15422 if (token
->keyword
== RID_TEMPLATE
)
15423 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15424 /* If it is `class' or `typename' we do not know yet whether it is a
15425 type parameter or a non-type parameter. Consider:
15427 template <typename T, typename T::X X> ...
15431 template <class C, class D*> ...
15433 Here, the first parameter is a type parameter, and the second is
15434 a non-type parameter. We can tell by looking at the token after
15435 the identifier -- if it is a `,', `=', or `>' then we have a type
15437 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15439 /* Peek at the token after `class' or `typename'. */
15440 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15441 /* If it's an ellipsis, we have a template type parameter
15443 if (token
->type
== CPP_ELLIPSIS
)
15444 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15445 /* If it's an identifier, skip it. */
15446 if (token
->type
== CPP_NAME
)
15447 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15448 /* Now, see if the token looks like the end of a template
15450 if (token
->type
== CPP_COMMA
15451 || token
->type
== CPP_EQ
15452 || token
->type
== CPP_GREATER
)
15453 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15456 /* Otherwise, it is a non-type parameter or a constrained parameter.
15460 When parsing a default template-argument for a non-type
15461 template-parameter, the first non-nested `>' is taken as the end
15462 of the template parameter-list rather than a greater-than
15464 parameter_declarator
15465 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15466 /*parenthesized_p=*/NULL
);
15468 if (!parameter_declarator
)
15469 return error_mark_node
;
15471 /* If the parameter declaration is marked as a parameter pack, set
15472 *IS_PARAMETER_PACK to notify the caller. */
15473 if (parameter_declarator
->template_parameter_pack_p
)
15474 *is_parameter_pack
= true;
15476 if (parameter_declarator
->default_argument
)
15478 /* Can happen in some cases of erroneous input (c++/34892). */
15479 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15480 /* Consume the `...' for better error recovery. */
15481 cp_lexer_consume_token (parser
->lexer
);
15484 // The parameter may have been constrained.
15485 if (is_constrained_parameter (parameter_declarator
))
15486 return finish_constrained_parameter (parser
,
15487 parameter_declarator
,
15489 is_parameter_pack
);
15491 // Now we're sure that the parameter is a non-type parameter.
15492 *is_non_type
= true;
15494 parm
= grokdeclarator (parameter_declarator
->declarator
,
15495 ¶meter_declarator
->decl_specifiers
,
15496 TPARM
, /*initialized=*/0,
15497 /*attrlist=*/NULL
);
15498 if (parm
== error_mark_node
)
15499 return error_mark_node
;
15501 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15504 /* Parse a type-parameter.
15507 class identifier [opt]
15508 class identifier [opt] = type-id
15509 typename identifier [opt]
15510 typename identifier [opt] = type-id
15511 template < template-parameter-list > class identifier [opt]
15512 template < template-parameter-list > class identifier [opt]
15515 GNU Extension (variadic templates):
15518 class ... identifier [opt]
15519 typename ... identifier [opt]
15521 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15522 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15523 the declaration of the parameter.
15525 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15528 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15533 /* Look for a keyword to tell us what kind of parameter this is. */
15534 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15536 return error_mark_node
;
15538 switch (token
->keyword
)
15544 tree default_argument
;
15546 /* If the next token is an ellipsis, we have a template
15548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15550 /* Consume the `...' token. */
15551 cp_lexer_consume_token (parser
->lexer
);
15552 maybe_warn_variadic_templates ();
15554 *is_parameter_pack
= true;
15557 /* If the next token is an identifier, then it names the
15559 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15560 identifier
= cp_parser_identifier (parser
);
15562 identifier
= NULL_TREE
;
15564 /* Create the parameter. */
15565 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15567 /* If the next token is an `=', we have a default argument. */
15568 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15571 = cp_parser_default_type_template_argument (parser
);
15573 /* Template parameter packs cannot have default
15575 if (*is_parameter_pack
)
15578 error_at (token
->location
,
15579 "template parameter pack %qD cannot have a "
15580 "default argument", identifier
);
15582 error_at (token
->location
,
15583 "template parameter packs cannot have "
15584 "default arguments");
15585 default_argument
= NULL_TREE
;
15587 else if (check_for_bare_parameter_packs (default_argument
))
15588 default_argument
= error_mark_node
;
15591 default_argument
= NULL_TREE
;
15593 /* Create the combined representation of the parameter and the
15594 default argument. */
15595 parameter
= build_tree_list (default_argument
, parameter
);
15602 tree default_argument
;
15604 /* Look for the `<'. */
15605 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15606 /* Parse the template-parameter-list. */
15607 cp_parser_template_parameter_list (parser
);
15608 /* Look for the `>'. */
15609 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15611 // If template requirements are present, parse them.
15614 tree reqs
= get_shorthand_constraints (current_template_parms
);
15615 if (tree r
= cp_parser_requires_clause_opt (parser
))
15616 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15617 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15620 /* Look for the `class' or 'typename' keywords. */
15621 cp_parser_type_parameter_key (parser
);
15622 /* If the next token is an ellipsis, we have a template
15624 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15626 /* Consume the `...' token. */
15627 cp_lexer_consume_token (parser
->lexer
);
15628 maybe_warn_variadic_templates ();
15630 *is_parameter_pack
= true;
15632 /* If the next token is an `=', then there is a
15633 default-argument. If the next token is a `>', we are at
15634 the end of the parameter-list. If the next token is a `,',
15635 then we are at the end of this parameter. */
15636 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15637 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15638 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15640 identifier
= cp_parser_identifier (parser
);
15641 /* Treat invalid names as if the parameter were nameless. */
15642 if (identifier
== error_mark_node
)
15643 identifier
= NULL_TREE
;
15646 identifier
= NULL_TREE
;
15648 /* Create the template parameter. */
15649 parameter
= finish_template_template_parm (class_type_node
,
15652 /* If the next token is an `=', then there is a
15653 default-argument. */
15654 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15657 = cp_parser_default_template_template_argument (parser
);
15659 /* Template parameter packs cannot have default
15661 if (*is_parameter_pack
)
15664 error_at (token
->location
,
15665 "template parameter pack %qD cannot "
15666 "have a default argument",
15669 error_at (token
->location
, "template parameter packs cannot "
15670 "have default arguments");
15671 default_argument
= NULL_TREE
;
15675 default_argument
= NULL_TREE
;
15677 /* Create the combined representation of the parameter and the
15678 default argument. */
15679 parameter
= build_tree_list (default_argument
, parameter
);
15684 gcc_unreachable ();
15691 /* Parse a template-id.
15694 template-name < template-argument-list [opt] >
15696 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15697 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15698 returned. Otherwise, if the template-name names a function, or set
15699 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15700 names a class, returns a TYPE_DECL for the specialization.
15702 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15703 uninstantiated templates. */
15706 cp_parser_template_id (cp_parser
*parser
,
15707 bool template_keyword_p
,
15708 bool check_dependency_p
,
15709 enum tag_types tag_type
,
15710 bool is_declaration
)
15715 cp_token_position start_of_id
= 0;
15716 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15717 bool is_identifier
;
15719 /* If the next token corresponds to a template-id, there is no need
15721 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15722 if (token
->type
== CPP_TEMPLATE_ID
)
15724 cp_lexer_consume_token (parser
->lexer
);
15725 return saved_checks_value (token
->u
.tree_check_value
);
15728 /* Avoid performing name lookup if there is no possibility of
15729 finding a template-id. */
15730 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15731 || (token
->type
== CPP_NAME
15732 && !cp_parser_nth_token_starts_template_argument_list_p
15735 cp_parser_error (parser
, "expected template-id");
15736 return error_mark_node
;
15739 /* Remember where the template-id starts. */
15740 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15741 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15743 push_deferring_access_checks (dk_deferred
);
15745 /* Parse the template-name. */
15746 is_identifier
= false;
15747 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15748 check_dependency_p
,
15752 if (templ
== error_mark_node
|| is_identifier
)
15754 pop_deferring_access_checks ();
15758 /* Since we're going to preserve any side-effects from this parse, set up a
15759 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15760 in the template arguments. */
15761 tentative_firewall
firewall (parser
);
15763 /* If we find the sequence `[:' after a template-name, it's probably
15764 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15765 parse correctly the argument list. */
15766 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15767 == CPP_OPEN_SQUARE
)
15768 && next_token
->flags
& DIGRAPH
15769 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15771 && !(next_token_2
->flags
& PREV_WHITE
))
15773 cp_parser_parse_tentatively (parser
);
15774 /* Change `:' into `::'. */
15775 next_token_2
->type
= CPP_SCOPE
;
15776 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15778 cp_lexer_consume_token (parser
->lexer
);
15780 /* Parse the arguments. */
15781 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15782 if (!cp_parser_parse_definitely (parser
))
15784 /* If we couldn't parse an argument list, then we revert our changes
15785 and return simply an error. Maybe this is not a template-id
15787 next_token_2
->type
= CPP_COLON
;
15788 cp_parser_error (parser
, "expected %<<%>");
15789 pop_deferring_access_checks ();
15790 return error_mark_node
;
15792 /* Otherwise, emit an error about the invalid digraph, but continue
15793 parsing because we got our argument list. */
15794 if (permerror (next_token
->location
,
15795 "%<<::%> cannot begin a template-argument list"))
15797 static bool hint
= false;
15798 inform (next_token
->location
,
15799 "%<<:%> is an alternate spelling for %<[%>."
15800 " Insert whitespace between %<<%> and %<::%>");
15801 if (!hint
&& !flag_permissive
)
15803 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15804 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15805 "accept your code)");
15812 /* Look for the `<' that starts the template-argument-list. */
15813 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15815 pop_deferring_access_checks ();
15816 return error_mark_node
;
15818 /* Parse the arguments. */
15819 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15822 /* Set the location to be of the form:
15823 template-name < template-argument-list [opt] >
15824 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15825 with caret == start at the start of the template-name,
15826 ranging until the closing '>'. */
15827 location_t finish_loc
15828 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15829 location_t combined_loc
15830 = make_location (token
->location
, token
->location
, finish_loc
);
15832 /* Build a representation of the specialization. */
15833 if (identifier_p (templ
))
15834 template_id
= build_min_nt_loc (combined_loc
,
15837 else if (DECL_TYPE_TEMPLATE_P (templ
)
15838 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15840 bool entering_scope
;
15841 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15842 template (rather than some instantiation thereof) only if
15843 is not nested within some other construct. For example, in
15844 "template <typename T> void f(T) { A<T>::", A<T> is just an
15845 instantiation of A. */
15846 entering_scope
= (template_parm_scope_p ()
15847 && cp_lexer_next_token_is (parser
->lexer
,
15850 = finish_template_type (templ
, arguments
, entering_scope
);
15852 /* A template-like identifier may be a partial concept id. */
15853 else if (flag_concepts
15854 && (template_id
= (cp_parser_maybe_partial_concept_id
15855 (parser
, templ
, arguments
))))
15856 return template_id
;
15857 else if (variable_template_p (templ
))
15859 template_id
= lookup_template_variable (templ
, arguments
);
15860 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15861 SET_EXPR_LOCATION (template_id
, combined_loc
);
15865 /* If it's not a class-template or a template-template, it should be
15866 a function-template. */
15867 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15868 || TREE_CODE (templ
) == OVERLOAD
15869 || BASELINK_P (templ
)));
15871 template_id
= lookup_template_function (templ
, arguments
);
15872 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15873 SET_EXPR_LOCATION (template_id
, combined_loc
);
15876 /* If parsing tentatively, replace the sequence of tokens that makes
15877 up the template-id with a CPP_TEMPLATE_ID token. That way,
15878 should we re-parse the token stream, we will not have to repeat
15879 the effort required to do the parse, nor will we issue duplicate
15880 error messages about problems during instantiation of the
15883 /* Don't do this if we had a parse error in a declarator; re-parsing
15884 might succeed if a name changes meaning (60361). */
15885 && !(cp_parser_error_occurred (parser
)
15886 && cp_parser_parsing_tentatively (parser
)
15887 && parser
->in_declarator_p
))
15889 /* Reset the contents of the START_OF_ID token. */
15890 token
->type
= CPP_TEMPLATE_ID
;
15891 token
->location
= combined_loc
;
15893 /* We must mark the lookup as kept, so we don't throw it away on
15894 the first parse. */
15895 if (is_overloaded_fn (template_id
))
15896 lookup_keep (get_fns (template_id
), true);
15898 /* Retrieve any deferred checks. Do not pop this access checks yet
15899 so the memory will not be reclaimed during token replacing below. */
15900 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15901 token
->u
.tree_check_value
->value
= template_id
;
15902 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15903 token
->keyword
= RID_MAX
;
15905 /* Purge all subsequent tokens. */
15906 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15908 /* ??? Can we actually assume that, if template_id ==
15909 error_mark_node, we will have issued a diagnostic to the
15910 user, as opposed to simply marking the tentative parse as
15912 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15913 error_at (token
->location
, "parse error in template argument list");
15916 pop_to_parent_deferring_access_checks ();
15917 return template_id
;
15920 /* Parse a template-name.
15925 The standard should actually say:
15929 operator-function-id
15931 A defect report has been filed about this issue.
15933 A conversion-function-id cannot be a template name because they cannot
15934 be part of a template-id. In fact, looking at this code:
15936 a.operator K<int>()
15938 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15939 It is impossible to call a templated conversion-function-id with an
15940 explicit argument list, since the only allowed template parameter is
15941 the type to which it is converting.
15943 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15944 `template' keyword, in a construction like:
15948 In that case `f' is taken to be a template-name, even though there
15949 is no way of knowing for sure.
15951 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15952 name refers to a set of overloaded functions, at least one of which
15953 is a template, or an IDENTIFIER_NODE with the name of the template,
15954 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15955 names are looked up inside uninstantiated templates. */
15958 cp_parser_template_name (cp_parser
* parser
,
15959 bool template_keyword_p
,
15960 bool check_dependency_p
,
15961 bool is_declaration
,
15962 enum tag_types tag_type
,
15963 bool *is_identifier
)
15967 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15969 /* If the next token is `operator', then we have either an
15970 operator-function-id or a conversion-function-id. */
15971 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15973 /* We don't know whether we're looking at an
15974 operator-function-id or a conversion-function-id. */
15975 cp_parser_parse_tentatively (parser
);
15976 /* Try an operator-function-id. */
15977 identifier
= cp_parser_operator_function_id (parser
);
15978 /* If that didn't work, try a conversion-function-id. */
15979 if (!cp_parser_parse_definitely (parser
))
15981 cp_parser_error (parser
, "expected template-name");
15982 return error_mark_node
;
15985 /* Look for the identifier. */
15987 identifier
= cp_parser_identifier (parser
);
15989 /* If we didn't find an identifier, we don't have a template-id. */
15990 if (identifier
== error_mark_node
)
15991 return error_mark_node
;
15993 /* If the name immediately followed the `template' keyword, then it
15994 is a template-name. However, if the next token is not `<', then
15995 we do not treat it as a template-name, since it is not being used
15996 as part of a template-id. This enables us to handle constructs
15999 template <typename T> struct S { S(); };
16000 template <typename T> S<T>::S();
16002 correctly. We would treat `S' as a template -- if it were `S<T>'
16003 -- but we do not if there is no `<'. */
16005 if (processing_template_decl
16006 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16008 /* In a declaration, in a dependent context, we pretend that the
16009 "template" keyword was present in order to improve error
16010 recovery. For example, given:
16012 template <typename T> void f(T::X<int>);
16014 we want to treat "X<int>" as a template-id. */
16016 && !template_keyword_p
16017 && parser
->scope
&& TYPE_P (parser
->scope
)
16018 && check_dependency_p
16019 && dependent_scope_p (parser
->scope
)
16020 /* Do not do this for dtors (or ctors), since they never
16021 need the template keyword before their name. */
16022 && !constructor_name_p (identifier
, parser
->scope
))
16024 cp_token_position start
= 0;
16026 /* Explain what went wrong. */
16027 error_at (token
->location
, "non-template %qD used as template",
16029 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16030 parser
->scope
, identifier
);
16031 /* If parsing tentatively, find the location of the "<" token. */
16032 if (cp_parser_simulate_error (parser
))
16033 start
= cp_lexer_token_position (parser
->lexer
, true);
16034 /* Parse the template arguments so that we can issue error
16035 messages about them. */
16036 cp_lexer_consume_token (parser
->lexer
);
16037 cp_parser_enclosed_template_argument_list (parser
);
16038 /* Skip tokens until we find a good place from which to
16039 continue parsing. */
16040 cp_parser_skip_to_closing_parenthesis (parser
,
16041 /*recovering=*/true,
16043 /*consume_paren=*/false);
16044 /* If parsing tentatively, permanently remove the
16045 template argument list. That will prevent duplicate
16046 error messages from being issued about the missing
16047 "template" keyword. */
16049 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16051 *is_identifier
= true;
16052 parser
->context
->object_type
= NULL_TREE
;
16056 /* If the "template" keyword is present, then there is generally
16057 no point in doing name-lookup, so we just return IDENTIFIER.
16058 But, if the qualifying scope is non-dependent then we can
16059 (and must) do name-lookup normally. */
16060 if (template_keyword_p
)
16062 tree scope
= (parser
->scope
? parser
->scope
16063 : parser
->context
->object_type
);
16064 if (scope
&& TYPE_P (scope
)
16065 && (!CLASS_TYPE_P (scope
)
16066 || (check_dependency_p
&& dependent_type_p (scope
))))
16068 /* We're optimizing away the call to cp_parser_lookup_name, but
16069 we still need to do this. */
16070 parser
->context
->object_type
= NULL_TREE
;
16076 /* Look up the name. */
16077 decl
= cp_parser_lookup_name (parser
, identifier
,
16079 /*is_template=*/true,
16080 /*is_namespace=*/false,
16081 check_dependency_p
,
16082 /*ambiguous_decls=*/NULL
,
16085 decl
= strip_using_decl (decl
);
16087 /* If DECL is a template, then the name was a template-name. */
16088 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16090 if (TREE_DEPRECATED (decl
)
16091 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16092 warn_deprecated_use (decl
, NULL_TREE
);
16096 /* The standard does not explicitly indicate whether a name that
16097 names a set of overloaded declarations, some of which are
16098 templates, is a template-name. However, such a name should
16099 be a template-name; otherwise, there is no way to form a
16100 template-id for the overloaded templates. */
16101 bool found
= false;
16103 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16104 !found
&& iter
; ++iter
)
16105 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16110 /* The name does not name a template. */
16111 cp_parser_error (parser
, "expected template-name");
16112 return error_mark_node
;
16116 /* If DECL is dependent, and refers to a function, then just return
16117 its name; we will look it up again during template instantiation. */
16118 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16120 tree scope
= ovl_scope (decl
);
16121 if (TYPE_P (scope
) && dependent_type_p (scope
))
16128 /* Parse a template-argument-list.
16130 template-argument-list:
16131 template-argument ... [opt]
16132 template-argument-list , template-argument ... [opt]
16134 Returns a TREE_VEC containing the arguments. */
16137 cp_parser_template_argument_list (cp_parser
* parser
)
16139 tree fixed_args
[10];
16140 unsigned n_args
= 0;
16141 unsigned alloced
= 10;
16142 tree
*arg_ary
= fixed_args
;
16144 bool saved_in_template_argument_list_p
;
16146 bool saved_non_ice_p
;
16148 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16149 parser
->in_template_argument_list_p
= true;
16150 /* Even if the template-id appears in an integral
16151 constant-expression, the contents of the argument list do
16153 saved_ice_p
= parser
->integral_constant_expression_p
;
16154 parser
->integral_constant_expression_p
= false;
16155 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16156 parser
->non_integral_constant_expression_p
= false;
16158 /* Parse the arguments. */
16164 /* Consume the comma. */
16165 cp_lexer_consume_token (parser
->lexer
);
16167 /* Parse the template-argument. */
16168 argument
= cp_parser_template_argument (parser
);
16170 /* If the next token is an ellipsis, we're expanding a template
16172 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16174 if (argument
== error_mark_node
)
16176 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16177 error_at (token
->location
,
16178 "expected parameter pack before %<...%>");
16180 /* Consume the `...' token. */
16181 cp_lexer_consume_token (parser
->lexer
);
16183 /* Make the argument into a TYPE_PACK_EXPANSION or
16184 EXPR_PACK_EXPANSION. */
16185 argument
= make_pack_expansion (argument
);
16188 if (n_args
== alloced
)
16192 if (arg_ary
== fixed_args
)
16194 arg_ary
= XNEWVEC (tree
, alloced
);
16195 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16198 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16200 arg_ary
[n_args
++] = argument
;
16202 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16204 vec
= make_tree_vec (n_args
);
16207 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16209 if (arg_ary
!= fixed_args
)
16211 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16212 parser
->integral_constant_expression_p
= saved_ice_p
;
16213 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16215 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16219 /* Parse a template-argument.
16222 assignment-expression
16226 The representation is that of an assignment-expression, type-id, or
16227 id-expression -- except that the qualified id-expression is
16228 evaluated, so that the value returned is either a DECL or an
16231 Although the standard says "assignment-expression", it forbids
16232 throw-expressions or assignments in the template argument.
16233 Therefore, we use "conditional-expression" instead. */
16236 cp_parser_template_argument (cp_parser
* parser
)
16241 bool maybe_type_id
= false;
16242 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16243 location_t loc
= 0;
16246 /* There's really no way to know what we're looking at, so we just
16247 try each alternative in order.
16251 In a template-argument, an ambiguity between a type-id and an
16252 expression is resolved to a type-id, regardless of the form of
16253 the corresponding template-parameter.
16255 Therefore, we try a type-id first. */
16256 cp_parser_parse_tentatively (parser
);
16257 argument
= cp_parser_template_type_arg (parser
);
16258 /* If there was no error parsing the type-id but the next token is a
16259 '>>', our behavior depends on which dialect of C++ we're
16260 parsing. In C++98, we probably found a typo for '> >'. But there
16261 are type-id which are also valid expressions. For instance:
16263 struct X { int operator >> (int); };
16264 template <int V> struct Foo {};
16267 Here 'X()' is a valid type-id of a function type, but the user just
16268 wanted to write the expression "X() >> 5". Thus, we remember that we
16269 found a valid type-id, but we still try to parse the argument as an
16270 expression to see what happens.
16272 In C++0x, the '>>' will be considered two separate '>'
16274 if (!cp_parser_error_occurred (parser
)
16275 && cxx_dialect
== cxx98
16276 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16278 maybe_type_id
= true;
16279 cp_parser_abort_tentative_parse (parser
);
16283 /* If the next token isn't a `,' or a `>', then this argument wasn't
16284 really finished. This means that the argument is not a valid
16286 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16287 cp_parser_error (parser
, "expected template-argument");
16288 /* If that worked, we're done. */
16289 if (cp_parser_parse_definitely (parser
))
16292 /* We're still not sure what the argument will be. */
16293 cp_parser_parse_tentatively (parser
);
16294 /* Try a template. */
16295 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16296 argument
= cp_parser_id_expression (parser
,
16297 /*template_keyword_p=*/false,
16298 /*check_dependency_p=*/true,
16300 /*declarator_p=*/false,
16301 /*optional_p=*/false);
16302 /* If the next token isn't a `,' or a `>', then this argument wasn't
16303 really finished. */
16304 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16305 cp_parser_error (parser
, "expected template-argument");
16306 if (!cp_parser_error_occurred (parser
))
16308 /* Figure out what is being referred to. If the id-expression
16309 was for a class template specialization, then we will have a
16310 TYPE_DECL at this point. There is no need to do name lookup
16311 at this point in that case. */
16312 if (TREE_CODE (argument
) != TYPE_DECL
)
16313 argument
= cp_parser_lookup_name (parser
, argument
,
16315 /*is_template=*/template_p
,
16316 /*is_namespace=*/false,
16317 /*check_dependency=*/true,
16318 /*ambiguous_decls=*/NULL
,
16319 argument_start_token
->location
);
16320 /* Handle a constrained-type-specifier for a non-type template
16322 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16324 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16325 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16326 cp_parser_error (parser
, "expected template-name");
16328 if (cp_parser_parse_definitely (parser
))
16330 if (TREE_DEPRECATED (argument
))
16331 warn_deprecated_use (argument
, NULL_TREE
);
16334 /* It must be a non-type argument. In C++17 any constant-expression is
16336 if (cxx_dialect
> cxx14
)
16339 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16341 -- an integral constant-expression of integral or enumeration
16344 -- the name of a non-type template-parameter; or
16346 -- the name of an object or function with external linkage...
16348 -- the address of an object or function with external linkage...
16350 -- a pointer to member... */
16351 /* Look for a non-type template parameter. */
16352 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16354 cp_parser_parse_tentatively (parser
);
16355 argument
= cp_parser_primary_expression (parser
,
16356 /*address_p=*/false,
16358 /*template_arg_p=*/true,
16360 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16361 || !cp_parser_next_token_ends_template_argument_p (parser
))
16362 cp_parser_simulate_error (parser
);
16363 if (cp_parser_parse_definitely (parser
))
16367 /* If the next token is "&", the argument must be the address of an
16368 object or function with external linkage. */
16369 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16372 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16373 cp_lexer_consume_token (parser
->lexer
);
16375 /* See if we might have an id-expression. */
16376 token
= cp_lexer_peek_token (parser
->lexer
);
16377 if (token
->type
== CPP_NAME
16378 || token
->keyword
== RID_OPERATOR
16379 || token
->type
== CPP_SCOPE
16380 || token
->type
== CPP_TEMPLATE_ID
16381 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16383 cp_parser_parse_tentatively (parser
);
16384 argument
= cp_parser_primary_expression (parser
,
16387 /*template_arg_p=*/true,
16389 if (cp_parser_error_occurred (parser
)
16390 || !cp_parser_next_token_ends_template_argument_p (parser
))
16391 cp_parser_abort_tentative_parse (parser
);
16396 if (INDIRECT_REF_P (argument
))
16398 /* Strip the dereference temporarily. */
16399 gcc_assert (REFERENCE_REF_P (argument
));
16400 argument
= TREE_OPERAND (argument
, 0);
16403 /* If we're in a template, we represent a qualified-id referring
16404 to a static data member as a SCOPE_REF even if the scope isn't
16405 dependent so that we can check access control later. */
16407 if (TREE_CODE (probe
) == SCOPE_REF
)
16408 probe
= TREE_OPERAND (probe
, 1);
16411 /* A variable without external linkage might still be a
16412 valid constant-expression, so no error is issued here
16413 if the external-linkage check fails. */
16414 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16415 cp_parser_simulate_error (parser
);
16417 else if (is_overloaded_fn (argument
))
16418 /* All overloaded functions are allowed; if the external
16419 linkage test does not pass, an error will be issued
16423 && (TREE_CODE (argument
) == OFFSET_REF
16424 || TREE_CODE (argument
) == SCOPE_REF
))
16425 /* A pointer-to-member. */
16427 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16430 cp_parser_simulate_error (parser
);
16432 if (cp_parser_parse_definitely (parser
))
16435 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16436 tf_warning_or_error
);
16438 argument
= convert_from_reference (argument
);
16443 /* If the argument started with "&", there are no other valid
16444 alternatives at this point. */
16447 cp_parser_error (parser
, "invalid non-type template argument");
16448 return error_mark_node
;
16452 /* If the argument wasn't successfully parsed as a type-id followed
16453 by '>>', the argument can only be a constant expression now.
16454 Otherwise, we try parsing the constant-expression tentatively,
16455 because the argument could really be a type-id. */
16457 cp_parser_parse_tentatively (parser
);
16459 if (cxx_dialect
<= cxx14
)
16460 argument
= cp_parser_constant_expression (parser
);
16463 /* With C++17 generalized non-type template arguments we need to handle
16464 lvalue constant expressions, too. */
16465 argument
= cp_parser_assignment_expression (parser
);
16466 require_potential_constant_expression (argument
);
16469 if (!maybe_type_id
)
16471 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16472 cp_parser_error (parser
, "expected template-argument");
16473 if (cp_parser_parse_definitely (parser
))
16475 /* We did our best to parse the argument as a non type-id, but that
16476 was the only alternative that matched (albeit with a '>' after
16477 it). We can assume it's just a typo from the user, and a
16478 diagnostic will then be issued. */
16479 return cp_parser_template_type_arg (parser
);
16482 /* Parse an explicit-instantiation.
16484 explicit-instantiation:
16485 template declaration
16487 Although the standard says `declaration', what it really means is:
16489 explicit-instantiation:
16490 template decl-specifier-seq [opt] declarator [opt] ;
16492 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16493 supposed to be allowed. A defect report has been filed about this
16498 explicit-instantiation:
16499 storage-class-specifier template
16500 decl-specifier-seq [opt] declarator [opt] ;
16501 function-specifier template
16502 decl-specifier-seq [opt] declarator [opt] ; */
16505 cp_parser_explicit_instantiation (cp_parser
* parser
)
16507 int declares_class_or_enum
;
16508 cp_decl_specifier_seq decl_specifiers
;
16509 tree extension_specifier
= NULL_TREE
;
16511 timevar_push (TV_TEMPLATE_INST
);
16513 /* Look for an (optional) storage-class-specifier or
16514 function-specifier. */
16515 if (cp_parser_allow_gnu_extensions_p (parser
))
16517 extension_specifier
16518 = cp_parser_storage_class_specifier_opt (parser
);
16519 if (!extension_specifier
)
16520 extension_specifier
16521 = cp_parser_function_specifier_opt (parser
,
16522 /*decl_specs=*/NULL
);
16525 /* Look for the `template' keyword. */
16526 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16527 /* Let the front end know that we are processing an explicit
16529 begin_explicit_instantiation ();
16530 /* [temp.explicit] says that we are supposed to ignore access
16531 control while processing explicit instantiation directives. */
16532 push_deferring_access_checks (dk_no_check
);
16533 /* Parse a decl-specifier-seq. */
16534 cp_parser_decl_specifier_seq (parser
,
16535 CP_PARSER_FLAGS_OPTIONAL
,
16537 &declares_class_or_enum
);
16538 /* If there was exactly one decl-specifier, and it declared a class,
16539 and there's no declarator, then we have an explicit type
16541 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16545 type
= check_tag_decl (&decl_specifiers
,
16546 /*explicit_type_instantiation_p=*/true);
16547 /* Turn access control back on for names used during
16548 template instantiation. */
16549 pop_deferring_access_checks ();
16551 do_type_instantiation (type
, extension_specifier
,
16552 /*complain=*/tf_error
);
16556 cp_declarator
*declarator
;
16559 /* Parse the declarator. */
16561 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16562 /*ctor_dtor_or_conv_p=*/NULL
,
16563 /*parenthesized_p=*/NULL
,
16564 /*member_p=*/false,
16565 /*friend_p=*/false);
16566 if (declares_class_or_enum
& 2)
16567 cp_parser_check_for_definition_in_return_type (declarator
,
16568 decl_specifiers
.type
,
16569 decl_specifiers
.locations
[ds_type_spec
]);
16570 if (declarator
!= cp_error_declarator
)
16572 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16573 permerror (decl_specifiers
.locations
[ds_inline
],
16574 "explicit instantiation shall not use"
16575 " %<inline%> specifier");
16576 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16577 permerror (decl_specifiers
.locations
[ds_constexpr
],
16578 "explicit instantiation shall not use"
16579 " %<constexpr%> specifier");
16581 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16582 NORMAL
, 0, &decl_specifiers
.attributes
);
16583 /* Turn access control back on for names used during
16584 template instantiation. */
16585 pop_deferring_access_checks ();
16586 /* Do the explicit instantiation. */
16587 do_decl_instantiation (decl
, extension_specifier
);
16591 pop_deferring_access_checks ();
16592 /* Skip the body of the explicit instantiation. */
16593 cp_parser_skip_to_end_of_statement (parser
);
16596 /* We're done with the instantiation. */
16597 end_explicit_instantiation ();
16599 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16601 timevar_pop (TV_TEMPLATE_INST
);
16604 /* Parse an explicit-specialization.
16606 explicit-specialization:
16607 template < > declaration
16609 Although the standard says `declaration', what it really means is:
16611 explicit-specialization:
16612 template <> decl-specifier [opt] init-declarator [opt] ;
16613 template <> function-definition
16614 template <> explicit-specialization
16615 template <> template-declaration */
16618 cp_parser_explicit_specialization (cp_parser
* parser
)
16620 bool need_lang_pop
;
16621 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16623 /* Look for the `template' keyword. */
16624 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16625 /* Look for the `<'. */
16626 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16627 /* Look for the `>'. */
16628 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16629 /* We have processed another parameter list. */
16630 ++parser
->num_template_parameter_lists
;
16633 A template ... explicit specialization ... shall not have C
16635 if (current_lang_name
== lang_name_c
)
16637 error_at (token
->location
, "template specialization with C linkage");
16638 maybe_show_extern_c_location ();
16639 /* Give it C++ linkage to avoid confusing other parts of the
16641 push_lang_context (lang_name_cplusplus
);
16642 need_lang_pop
= true;
16645 need_lang_pop
= false;
16646 /* Let the front end know that we are beginning a specialization. */
16647 if (!begin_specialization ())
16649 end_specialization ();
16653 /* If the next keyword is `template', we need to figure out whether
16654 or not we're looking a template-declaration. */
16655 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16657 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16658 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16659 cp_parser_template_declaration_after_export (parser
,
16660 /*member_p=*/false);
16662 cp_parser_explicit_specialization (parser
);
16665 /* Parse the dependent declaration. */
16666 cp_parser_single_declaration (parser
,
16668 /*member_p=*/false,
16669 /*explicit_specialization_p=*/true,
16670 /*friend_p=*/NULL
);
16671 /* We're done with the specialization. */
16672 end_specialization ();
16673 /* For the erroneous case of a template with C linkage, we pushed an
16674 implicit C++ linkage scope; exit that scope now. */
16676 pop_lang_context ();
16677 /* We're done with this parameter list. */
16678 --parser
->num_template_parameter_lists
;
16681 /* Parse a type-specifier.
16684 simple-type-specifier
16687 elaborated-type-specifier
16695 Returns a representation of the type-specifier. For a
16696 class-specifier, enum-specifier, or elaborated-type-specifier, a
16697 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16699 The parser flags FLAGS is used to control type-specifier parsing.
16701 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16702 in a decl-specifier-seq.
16704 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16705 class-specifier, enum-specifier, or elaborated-type-specifier, then
16706 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16707 if a type is declared; 2 if it is defined. Otherwise, it is set to
16710 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16711 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16712 is set to FALSE. */
16715 cp_parser_type_specifier (cp_parser
* parser
,
16716 cp_parser_flags flags
,
16717 cp_decl_specifier_seq
*decl_specs
,
16718 bool is_declaration
,
16719 int* declares_class_or_enum
,
16720 bool* is_cv_qualifier
)
16722 tree type_spec
= NULL_TREE
;
16725 cp_decl_spec ds
= ds_last
;
16727 /* Assume this type-specifier does not declare a new type. */
16728 if (declares_class_or_enum
)
16729 *declares_class_or_enum
= 0;
16730 /* And that it does not specify a cv-qualifier. */
16731 if (is_cv_qualifier
)
16732 *is_cv_qualifier
= false;
16733 /* Peek at the next token. */
16734 token
= cp_lexer_peek_token (parser
->lexer
);
16736 /* If we're looking at a keyword, we can use that to guide the
16737 production we choose. */
16738 keyword
= token
->keyword
;
16742 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16743 goto elaborated_type_specifier
;
16745 /* Look for the enum-specifier. */
16746 type_spec
= cp_parser_enum_specifier (parser
);
16747 /* If that worked, we're done. */
16750 if (declares_class_or_enum
)
16751 *declares_class_or_enum
= 2;
16753 cp_parser_set_decl_spec_type (decl_specs
,
16756 /*type_definition_p=*/true);
16760 goto elaborated_type_specifier
;
16762 /* Any of these indicate either a class-specifier, or an
16763 elaborated-type-specifier. */
16767 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16768 goto elaborated_type_specifier
;
16770 /* Parse tentatively so that we can back up if we don't find a
16771 class-specifier. */
16772 cp_parser_parse_tentatively (parser
);
16773 /* Look for the class-specifier. */
16774 type_spec
= cp_parser_class_specifier (parser
);
16775 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16776 /* If that worked, we're done. */
16777 if (cp_parser_parse_definitely (parser
))
16779 if (declares_class_or_enum
)
16780 *declares_class_or_enum
= 2;
16782 cp_parser_set_decl_spec_type (decl_specs
,
16785 /*type_definition_p=*/true);
16789 /* Fall through. */
16790 elaborated_type_specifier
:
16791 /* We're declaring (not defining) a class or enum. */
16792 if (declares_class_or_enum
)
16793 *declares_class_or_enum
= 1;
16795 /* Fall through. */
16797 /* Look for an elaborated-type-specifier. */
16799 = (cp_parser_elaborated_type_specifier
16801 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16804 cp_parser_set_decl_spec_type (decl_specs
,
16807 /*type_definition_p=*/false);
16812 if (is_cv_qualifier
)
16813 *is_cv_qualifier
= true;
16818 if (is_cv_qualifier
)
16819 *is_cv_qualifier
= true;
16824 if (is_cv_qualifier
)
16825 *is_cv_qualifier
= true;
16829 /* The `__complex__' keyword is a GNU extension. */
16837 /* Handle simple keywords. */
16842 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16843 decl_specs
->any_specifiers_p
= true;
16845 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16848 /* If we do not already have a type-specifier, assume we are looking
16849 at a simple-type-specifier. */
16850 type_spec
= cp_parser_simple_type_specifier (parser
,
16854 /* If we didn't find a type-specifier, and a type-specifier was not
16855 optional in this context, issue an error message. */
16856 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16858 cp_parser_error (parser
, "expected type specifier");
16859 return error_mark_node
;
16865 /* Parse a simple-type-specifier.
16867 simple-type-specifier:
16868 :: [opt] nested-name-specifier [opt] type-name
16869 :: [opt] nested-name-specifier template template-id
16884 simple-type-specifier:
16886 decltype ( expression )
16889 __underlying_type ( type-id )
16893 nested-name-specifier(opt) template-name
16897 simple-type-specifier:
16899 __typeof__ unary-expression
16900 __typeof__ ( type-id )
16901 __typeof__ ( type-id ) { initializer-list , [opt] }
16903 Concepts Extension:
16905 simple-type-specifier:
16906 constrained-type-specifier
16908 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16909 appropriately updated. */
16912 cp_parser_simple_type_specifier (cp_parser
* parser
,
16913 cp_decl_specifier_seq
*decl_specs
,
16914 cp_parser_flags flags
)
16916 tree type
= NULL_TREE
;
16920 /* Peek at the next token. */
16921 token
= cp_lexer_peek_token (parser
->lexer
);
16923 /* If we're looking at a keyword, things are easy. */
16924 switch (token
->keyword
)
16928 decl_specs
->explicit_char_p
= true;
16929 type
= char_type_node
;
16932 type
= char16_type_node
;
16935 type
= char32_type_node
;
16938 type
= wchar_type_node
;
16941 type
= boolean_type_node
;
16944 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16945 type
= short_integer_type_node
;
16949 decl_specs
->explicit_int_p
= true;
16950 type
= integer_type_node
;
16956 idx
= token
->keyword
- RID_INT_N_0
;
16957 if (! int_n_enabled_p
[idx
])
16961 decl_specs
->explicit_intN_p
= true;
16962 decl_specs
->int_n_idx
= idx
;
16964 type
= int_n_trees
[idx
].signed_type
;
16968 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16969 type
= long_integer_type_node
;
16972 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16973 type
= integer_type_node
;
16976 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16977 type
= unsigned_type_node
;
16980 type
= float_type_node
;
16983 type
= double_type_node
;
16986 type
= void_type_node
;
16990 maybe_warn_cpp0x (CPP0X_AUTO
);
16991 if (parser
->auto_is_implicit_function_template_parm_p
)
16993 /* The 'auto' might be the placeholder return type for a function decl
16994 with trailing return type. */
16995 bool have_trailing_return_fn_decl
= false;
16997 cp_parser_parse_tentatively (parser
);
16998 cp_lexer_consume_token (parser
->lexer
);
16999 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17000 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17001 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17002 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17004 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17006 cp_lexer_consume_token (parser
->lexer
);
17007 cp_parser_skip_to_closing_parenthesis (parser
,
17008 /*recovering*/false,
17010 /*consume_paren*/true);
17014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17016 have_trailing_return_fn_decl
= true;
17020 cp_lexer_consume_token (parser
->lexer
);
17022 cp_parser_abort_tentative_parse (parser
);
17024 if (have_trailing_return_fn_decl
)
17026 type
= make_auto ();
17030 if (cxx_dialect
>= cxx14
)
17032 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17033 type
= TREE_TYPE (type
);
17036 type
= error_mark_node
;
17038 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17040 if (cxx_dialect
< cxx14
)
17041 error_at (token
->location
,
17042 "use of %<auto%> in lambda parameter declaration "
17043 "only available with "
17044 "-std=c++14 or -std=gnu++14");
17046 else if (cxx_dialect
< cxx14
)
17047 error_at (token
->location
,
17048 "use of %<auto%> in parameter declaration "
17049 "only available with "
17050 "-std=c++14 or -std=gnu++14");
17051 else if (!flag_concepts
)
17052 pedwarn (token
->location
, 0,
17053 "use of %<auto%> in parameter declaration "
17054 "only available with -fconcepts");
17057 type
= make_auto ();
17061 /* Since DR 743, decltype can either be a simple-type-specifier by
17062 itself or begin a nested-name-specifier. Parsing it will replace
17063 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17064 handling below decide what to do. */
17065 cp_parser_decltype (parser
);
17066 cp_lexer_set_token_position (parser
->lexer
, token
);
17070 /* Consume the `typeof' token. */
17071 cp_lexer_consume_token (parser
->lexer
);
17072 /* Parse the operand to `typeof'. */
17073 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17074 /* If it is not already a TYPE, take its type. */
17075 if (!TYPE_P (type
))
17076 type
= finish_typeof (type
);
17079 cp_parser_set_decl_spec_type (decl_specs
, type
,
17081 /*type_definition_p=*/false);
17085 case RID_UNDERLYING_TYPE
:
17086 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17088 cp_parser_set_decl_spec_type (decl_specs
, type
,
17090 /*type_definition_p=*/false);
17095 case RID_DIRECT_BASES
:
17096 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17098 cp_parser_set_decl_spec_type (decl_specs
, type
,
17100 /*type_definition_p=*/false);
17106 /* If token is an already-parsed decltype not followed by ::,
17107 it's a simple-type-specifier. */
17108 if (token
->type
== CPP_DECLTYPE
17109 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17111 type
= saved_checks_value (token
->u
.tree_check_value
);
17114 cp_parser_set_decl_spec_type (decl_specs
, type
,
17116 /*type_definition_p=*/false);
17117 /* Remember that we are handling a decltype in order to
17118 implement the resolution of DR 1510 when the argument
17119 isn't instantiation dependent. */
17120 decl_specs
->decltype_p
= true;
17122 cp_lexer_consume_token (parser
->lexer
);
17126 /* If the type-specifier was for a built-in type, we're done. */
17129 /* Record the type. */
17131 && (token
->keyword
!= RID_SIGNED
17132 && token
->keyword
!= RID_UNSIGNED
17133 && token
->keyword
!= RID_SHORT
17134 && token
->keyword
!= RID_LONG
))
17135 cp_parser_set_decl_spec_type (decl_specs
,
17138 /*type_definition_p=*/false);
17140 decl_specs
->any_specifiers_p
= true;
17142 /* Consume the token. */
17143 cp_lexer_consume_token (parser
->lexer
);
17145 if (type
== error_mark_node
)
17146 return error_mark_node
;
17148 /* There is no valid C++ program where a non-template type is
17149 followed by a "<". That usually indicates that the user thought
17150 that the type was a template. */
17151 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17154 return TYPE_NAME (type
);
17157 /* The type-specifier must be a user-defined type. */
17158 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17163 /* Don't gobble tokens or issue error messages if this is an
17164 optional type-specifier. */
17165 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17166 cp_parser_parse_tentatively (parser
);
17168 token
= cp_lexer_peek_token (parser
->lexer
);
17170 /* Look for the optional `::' operator. */
17172 = (cp_parser_global_scope_opt (parser
,
17173 /*current_scope_valid_p=*/false)
17175 /* Look for the nested-name specifier. */
17177 = (cp_parser_nested_name_specifier_opt (parser
,
17178 /*typename_keyword_p=*/false,
17179 /*check_dependency_p=*/true,
17181 /*is_declaration=*/false)
17183 /* If we have seen a nested-name-specifier, and the next token
17184 is `template', then we are using the template-id production. */
17186 && cp_parser_optional_template_keyword (parser
))
17188 /* Look for the template-id. */
17189 type
= cp_parser_template_id (parser
,
17190 /*template_keyword_p=*/true,
17191 /*check_dependency_p=*/true,
17193 /*is_declaration=*/false);
17194 /* If the template-id did not name a type, we are out of
17196 if (TREE_CODE (type
) != TYPE_DECL
)
17198 cp_parser_error (parser
, "expected template-id for type");
17202 /* Otherwise, look for a type-name. */
17204 type
= cp_parser_type_name (parser
);
17205 /* Keep track of all name-lookups performed in class scopes. */
17209 && TREE_CODE (type
) == TYPE_DECL
17210 && identifier_p (DECL_NAME (type
)))
17211 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17212 /* If it didn't work out, we don't have a TYPE. */
17213 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17214 && !cp_parser_parse_definitely (parser
))
17216 if (!type
&& cxx_dialect
>= cxx17
)
17218 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17219 cp_parser_parse_tentatively (parser
);
17221 cp_parser_global_scope_opt (parser
,
17222 /*current_scope_valid_p=*/false);
17223 cp_parser_nested_name_specifier_opt (parser
,
17224 /*typename_keyword_p=*/false,
17225 /*check_dependency_p=*/true,
17227 /*is_declaration=*/false);
17228 tree name
= cp_parser_identifier (parser
);
17229 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17230 && parser
->scope
!= error_mark_node
)
17232 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17234 /*is_template=*/false,
17235 /*is_namespace=*/false,
17236 /*check_dependency=*/true,
17237 /*ambiguous_decls=*/NULL
,
17239 if (tmpl
&& tmpl
!= error_mark_node
17240 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17241 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17242 type
= make_template_placeholder (tmpl
);
17245 type
= error_mark_node
;
17246 if (!cp_parser_simulate_error (parser
))
17247 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17248 NLE_TYPE
, token
->location
);
17252 type
= error_mark_node
;
17254 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17255 && !cp_parser_parse_definitely (parser
))
17258 if (type
&& decl_specs
)
17259 cp_parser_set_decl_spec_type (decl_specs
, type
,
17261 /*type_definition_p=*/false);
17264 /* If we didn't get a type-name, issue an error message. */
17265 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17267 cp_parser_error (parser
, "expected type-name");
17268 return error_mark_node
;
17271 if (type
&& type
!= error_mark_node
)
17273 /* See if TYPE is an Objective-C type, and if so, parse and
17274 accept any protocol references following it. Do this before
17275 the cp_parser_check_for_invalid_template_id() call, because
17276 Objective-C types can be followed by '<...>' which would
17277 enclose protocol names rather than template arguments, and so
17278 everything is fine. */
17279 if (c_dialect_objc () && !parser
->scope
17280 && (objc_is_id (type
) || objc_is_class_name (type
)))
17282 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17283 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17285 /* Clobber the "unqualified" type previously entered into
17286 DECL_SPECS with the new, improved protocol-qualified version. */
17288 decl_specs
->type
= qual_type
;
17293 /* There is no valid C++ program where a non-template type is
17294 followed by a "<". That usually indicates that the user
17295 thought that the type was a template. */
17296 cp_parser_check_for_invalid_template_id (parser
, type
,
17304 /* Parse a type-name.
17310 simple-template-id [in c++0x]
17327 Returns a TYPE_DECL for the type. */
17330 cp_parser_type_name (cp_parser
* parser
)
17332 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17337 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17341 /* We can't know yet whether it is a class-name or not. */
17342 cp_parser_parse_tentatively (parser
);
17343 /* Try a class-name. */
17344 type_decl
= cp_parser_class_name (parser
,
17345 typename_keyword_p
,
17346 /*template_keyword_p=*/false,
17348 /*check_dependency_p=*/true,
17349 /*class_head_p=*/false,
17350 /*is_declaration=*/false);
17351 /* If it's not a class-name, keep looking. */
17352 if (!cp_parser_parse_definitely (parser
))
17354 if (cxx_dialect
< cxx11
)
17355 /* It must be a typedef-name or an enum-name. */
17356 return cp_parser_nonclass_name (parser
);
17358 cp_parser_parse_tentatively (parser
);
17359 /* It is either a simple-template-id representing an
17360 instantiation of an alias template... */
17361 type_decl
= cp_parser_template_id (parser
,
17362 /*template_keyword_p=*/false,
17363 /*check_dependency_p=*/true,
17365 /*is_declaration=*/false);
17366 /* Note that this must be an instantiation of an alias template
17367 because [temp.names]/6 says:
17369 A template-id that names an alias template specialization
17372 Whereas [temp.names]/7 says:
17374 A simple-template-id that names a class template
17375 specialization is a class-name.
17377 With concepts, this could also be a partial-concept-id that
17378 declares a non-type template parameter. */
17379 if (type_decl
!= NULL_TREE
17380 && TREE_CODE (type_decl
) == TYPE_DECL
17381 && TYPE_DECL_ALIAS_P (type_decl
))
17382 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17383 else if (is_constrained_parameter (type_decl
))
17384 /* Don't do anything. */ ;
17386 cp_parser_simulate_error (parser
);
17388 if (!cp_parser_parse_definitely (parser
))
17389 /* ... Or a typedef-name or an enum-name. */
17390 return cp_parser_nonclass_name (parser
);
17396 /* Check if DECL and ARGS can form a constrained-type-specifier.
17397 If ARGS is non-null, we try to form a concept check of the
17398 form DECL<?, ARGS> where ? is a wildcard that matches any
17399 kind of template argument. If ARGS is NULL, then we try to
17400 form a concept check of the form DECL<?>. */
17403 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17404 tree decl
, tree args
)
17406 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17408 /* If we a constrained-type-specifier cannot be deduced. */
17409 if (parser
->prevent_constrained_type_specifiers
)
17412 /* A constrained type specifier can only be found in an
17413 overload set or as a reference to a template declaration.
17415 FIXME: This might be masking a bug. It's possible that
17416 that the deduction below is causing template specializations
17417 to be formed with the wildcard as an argument. */
17418 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17421 /* Try to build a call expression that evaluates the
17422 concept. This can fail if the overload set refers
17423 only to non-templates. */
17424 tree placeholder
= build_nt (WILDCARD_DECL
);
17425 tree check
= build_concept_check (decl
, placeholder
, args
);
17426 if (check
== error_mark_node
)
17429 /* Deduce the checked constraint and the prototype parameter.
17431 FIXME: In certain cases, failure to deduce should be a
17432 diagnosable error. */
17435 if (!deduce_constrained_parameter (check
, conc
, proto
))
17438 /* In template parameter scope, this results in a constrained
17439 parameter. Return a descriptor of that parm. */
17440 if (processing_template_parmlist
)
17441 return build_constrained_parameter (conc
, proto
, args
);
17443 /* In a parameter-declaration-clause, constrained-type
17444 specifiers result in invented template parameters. */
17445 if (parser
->auto_is_implicit_function_template_parm_p
)
17447 tree x
= build_constrained_parameter (conc
, proto
, args
);
17448 return synthesize_implicit_template_parm (parser
, x
);
17452 /* Otherwise, we're in a context where the constrained
17453 type name is deduced and the constraint applies
17454 after deduction. */
17455 return make_constrained_auto (conc
, args
);
17461 /* If DECL refers to a concept, return a TYPE_DECL representing
17462 the result of using the constrained type specifier in the
17463 current context. DECL refers to a concept if
17465 - it is an overload set containing a function concept taking a single
17468 - it is a variable concept taking a single type argument. */
17471 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17474 && (TREE_CODE (decl
) == OVERLOAD
17475 || BASELINK_P (decl
)
17476 || variable_concept_p (decl
)))
17477 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17482 /* Check if DECL and ARGS form a partial-concept-id. If so,
17483 assign ID to the resulting constrained placeholder.
17485 Returns true if the partial-concept-id designates a placeholder
17486 and false otherwise. Note that *id is set to NULL_TREE in
17490 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17492 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17495 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17507 Returns a TYPE_DECL for the type. */
17510 cp_parser_nonclass_name (cp_parser
* parser
)
17515 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17516 identifier
= cp_parser_identifier (parser
);
17517 if (identifier
== error_mark_node
)
17518 return error_mark_node
;
17520 /* Look up the type-name. */
17521 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17523 type_decl
= strip_using_decl (type_decl
);
17525 /* If we found an overload set, then it may refer to a concept-name. */
17526 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17529 if (TREE_CODE (type_decl
) != TYPE_DECL
17530 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17532 /* See if this is an Objective-C type. */
17533 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17534 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17536 type_decl
= TYPE_NAME (type
);
17539 /* Issue an error if we did not find a type-name. */
17540 if (TREE_CODE (type_decl
) != TYPE_DECL
17541 /* In Objective-C, we have the complication that class names are
17542 normally type names and start declarations (eg, the
17543 "NSObject" in "NSObject *object;"), but can be used in an
17544 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17545 is an expression. So, a classname followed by a dot is not a
17546 valid type-name. */
17547 || (objc_is_class_name (TREE_TYPE (type_decl
))
17548 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17550 if (!cp_parser_simulate_error (parser
))
17551 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17552 NLE_TYPE
, token
->location
);
17553 return error_mark_node
;
17555 /* Remember that the name was used in the definition of the
17556 current class so that we can check later to see if the
17557 meaning would have been different after the class was
17558 entirely defined. */
17559 else if (type_decl
!= error_mark_node
17561 maybe_note_name_used_in_class (identifier
, type_decl
);
17566 /* Parse an elaborated-type-specifier. Note that the grammar given
17567 here incorporates the resolution to DR68.
17569 elaborated-type-specifier:
17570 class-key :: [opt] nested-name-specifier [opt] identifier
17571 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17572 enum-key :: [opt] nested-name-specifier [opt] identifier
17573 typename :: [opt] nested-name-specifier identifier
17574 typename :: [opt] nested-name-specifier template [opt]
17579 elaborated-type-specifier:
17580 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17581 class-key attributes :: [opt] nested-name-specifier [opt]
17582 template [opt] template-id
17583 enum attributes :: [opt] nested-name-specifier [opt] identifier
17585 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17586 declared `friend'. If IS_DECLARATION is TRUE, then this
17587 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17588 something is being declared.
17590 Returns the TYPE specified. */
17593 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17595 bool is_declaration
)
17597 enum tag_types tag_type
;
17599 tree type
= NULL_TREE
;
17600 tree attributes
= NULL_TREE
;
17602 cp_token
*token
= NULL
;
17604 /* See if we're looking at the `enum' keyword. */
17605 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17607 /* Consume the `enum' token. */
17608 cp_lexer_consume_token (parser
->lexer
);
17609 /* Remember that it's an enumeration type. */
17610 tag_type
= enum_type
;
17611 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17612 enums) is used here. */
17613 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17614 if (cp_parser_is_keyword (token
, RID_CLASS
)
17615 || cp_parser_is_keyword (token
, RID_STRUCT
))
17617 gcc_rich_location
richloc (token
->location
);
17618 richloc
.add_range (input_location
, false);
17619 richloc
.add_fixit_remove ();
17620 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17621 "a scoped enum must not use the %qD keyword",
17623 /* Consume the `struct' or `class' and parse it anyway. */
17624 cp_lexer_consume_token (parser
->lexer
);
17626 /* Parse the attributes. */
17627 attributes
= cp_parser_attributes_opt (parser
);
17629 /* Or, it might be `typename'. */
17630 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17633 /* Consume the `typename' token. */
17634 cp_lexer_consume_token (parser
->lexer
);
17635 /* Remember that it's a `typename' type. */
17636 tag_type
= typename_type
;
17638 /* Otherwise it must be a class-key. */
17641 tag_type
= cp_parser_class_key (parser
);
17642 if (tag_type
== none_type
)
17643 return error_mark_node
;
17644 /* Parse the attributes. */
17645 attributes
= cp_parser_attributes_opt (parser
);
17648 /* Look for the `::' operator. */
17649 globalscope
= cp_parser_global_scope_opt (parser
,
17650 /*current_scope_valid_p=*/false);
17651 /* Look for the nested-name-specifier. */
17652 tree nested_name_specifier
;
17653 if (tag_type
== typename_type
&& !globalscope
)
17655 nested_name_specifier
17656 = cp_parser_nested_name_specifier (parser
,
17657 /*typename_keyword_p=*/true,
17658 /*check_dependency_p=*/true,
17661 if (!nested_name_specifier
)
17662 return error_mark_node
;
17665 /* Even though `typename' is not present, the proposed resolution
17666 to Core Issue 180 says that in `class A<T>::B', `B' should be
17667 considered a type-name, even if `A<T>' is dependent. */
17668 nested_name_specifier
17669 = cp_parser_nested_name_specifier_opt (parser
,
17670 /*typename_keyword_p=*/true,
17671 /*check_dependency_p=*/true,
17674 /* For everything but enumeration types, consider a template-id.
17675 For an enumeration type, consider only a plain identifier. */
17676 if (tag_type
!= enum_type
)
17678 bool template_p
= false;
17681 /* Allow the `template' keyword. */
17682 template_p
= cp_parser_optional_template_keyword (parser
);
17683 /* If we didn't see `template', we don't know if there's a
17684 template-id or not. */
17686 cp_parser_parse_tentatively (parser
);
17687 /* Parse the template-id. */
17688 token
= cp_lexer_peek_token (parser
->lexer
);
17689 decl
= cp_parser_template_id (parser
, template_p
,
17690 /*check_dependency_p=*/true,
17693 /* If we didn't find a template-id, look for an ordinary
17695 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17697 /* We can get here when cp_parser_template_id, called by
17698 cp_parser_class_name with tag_type == none_type, succeeds
17699 and caches a BASELINK. Then, when called again here,
17700 instead of failing and returning an error_mark_node
17701 returns it (see template/typename17.C in C++11).
17702 ??? Could we diagnose this earlier? */
17703 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17705 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17706 type
= error_mark_node
;
17708 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17709 in effect, then we must assume that, upon instantiation, the
17710 template will correspond to a class. */
17711 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17712 && tag_type
== typename_type
)
17713 type
= make_typename_type (parser
->scope
, decl
,
17715 /*complain=*/tf_error
);
17716 /* If the `typename' keyword is in effect and DECL is not a type
17717 decl, then type is non existent. */
17718 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17720 else if (TREE_CODE (decl
) == TYPE_DECL
)
17722 type
= check_elaborated_type_specifier (tag_type
, decl
,
17723 /*allow_template_p=*/true);
17725 /* If the next token is a semicolon, this must be a specialization,
17726 instantiation, or friend declaration. Check the scope while we
17727 still know whether or not we had a nested-name-specifier. */
17728 if (type
!= error_mark_node
17729 && !nested_name_specifier
&& !is_friend
17730 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17731 check_unqualified_spec_or_inst (type
, token
->location
);
17733 else if (decl
== error_mark_node
)
17734 type
= error_mark_node
;
17739 token
= cp_lexer_peek_token (parser
->lexer
);
17740 identifier
= cp_parser_identifier (parser
);
17742 if (identifier
== error_mark_node
)
17744 parser
->scope
= NULL_TREE
;
17745 return error_mark_node
;
17748 /* For a `typename', we needn't call xref_tag. */
17749 if (tag_type
== typename_type
17750 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17751 return cp_parser_make_typename_type (parser
, identifier
,
17754 /* Template parameter lists apply only if we are not within a
17755 function parameter list. */
17756 bool template_parm_lists_apply
17757 = parser
->num_template_parameter_lists
;
17758 if (template_parm_lists_apply
)
17759 for (cp_binding_level
*s
= current_binding_level
;
17760 s
&& s
->kind
!= sk_template_parms
;
17761 s
= s
->level_chain
)
17762 if (s
->kind
== sk_function_parms
)
17763 template_parm_lists_apply
= false;
17765 /* Look up a qualified name in the usual way. */
17769 tree ambiguous_decls
;
17771 decl
= cp_parser_lookup_name (parser
, identifier
,
17773 /*is_template=*/false,
17774 /*is_namespace=*/false,
17775 /*check_dependency=*/true,
17779 /* If the lookup was ambiguous, an error will already have been
17781 if (ambiguous_decls
)
17782 return error_mark_node
;
17784 /* If we are parsing friend declaration, DECL may be a
17785 TEMPLATE_DECL tree node here. However, we need to check
17786 whether this TEMPLATE_DECL results in valid code. Consider
17787 the following example:
17790 template <class T> class C {};
17793 template <class T> friend class N::C; // #1, valid code
17795 template <class T> class Y {
17796 friend class N::C; // #2, invalid code
17799 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17800 name lookup of `N::C'. We see that friend declaration must
17801 be template for the code to be valid. Note that
17802 processing_template_decl does not work here since it is
17803 always 1 for the above two cases. */
17805 decl
= (cp_parser_maybe_treat_template_as_class
17806 (decl
, /*tag_name_p=*/is_friend
17807 && template_parm_lists_apply
));
17809 if (TREE_CODE (decl
) != TYPE_DECL
)
17811 cp_parser_diagnose_invalid_type_name (parser
,
17814 return error_mark_node
;
17817 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17819 bool allow_template
= (template_parm_lists_apply
17820 || DECL_SELF_REFERENCE_P (decl
));
17821 type
= check_elaborated_type_specifier (tag_type
, decl
,
17824 if (type
== error_mark_node
)
17825 return error_mark_node
;
17828 /* Forward declarations of nested types, such as
17833 are invalid unless all components preceding the final '::'
17834 are complete. If all enclosing types are complete, these
17835 declarations become merely pointless.
17837 Invalid forward declarations of nested types are errors
17838 caught elsewhere in parsing. Those that are pointless arrive
17841 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17842 && !is_friend
&& !processing_explicit_instantiation
)
17843 warning (0, "declaration %qD does not declare anything", decl
);
17845 type
= TREE_TYPE (decl
);
17849 /* An elaborated-type-specifier sometimes introduces a new type and
17850 sometimes names an existing type. Normally, the rule is that it
17851 introduces a new type only if there is not an existing type of
17852 the same name already in scope. For example, given:
17855 void f() { struct S s; }
17857 the `struct S' in the body of `f' is the same `struct S' as in
17858 the global scope; the existing definition is used. However, if
17859 there were no global declaration, this would introduce a new
17860 local class named `S'.
17862 An exception to this rule applies to the following code:
17864 namespace N { struct S; }
17866 Here, the elaborated-type-specifier names a new type
17867 unconditionally; even if there is already an `S' in the
17868 containing scope this declaration names a new type.
17869 This exception only applies if the elaborated-type-specifier
17870 forms the complete declaration:
17874 A declaration consisting solely of `class-key identifier ;' is
17875 either a redeclaration of the name in the current scope or a
17876 forward declaration of the identifier as a class name. It
17877 introduces the name into the current scope.
17879 We are in this situation precisely when the next token is a `;'.
17881 An exception to the exception is that a `friend' declaration does
17882 *not* name a new type; i.e., given:
17884 struct S { friend struct T; };
17886 `T' is not a new type in the scope of `S'.
17888 Also, `new struct S' or `sizeof (struct S)' never results in the
17889 definition of a new type; a new type can only be declared in a
17890 declaration context. */
17896 /* Friends have special name lookup rules. */
17897 ts
= ts_within_enclosing_non_class
;
17898 else if (is_declaration
17899 && cp_lexer_next_token_is (parser
->lexer
,
17901 /* This is a `class-key identifier ;' */
17907 (template_parm_lists_apply
17908 && (cp_parser_next_token_starts_class_definition_p (parser
)
17909 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17910 /* An unqualified name was used to reference this type, so
17911 there were no qualifying templates. */
17912 if (template_parm_lists_apply
17913 && !cp_parser_check_template_parameters (parser
,
17914 /*num_templates=*/0,
17916 /*declarator=*/NULL
))
17917 return error_mark_node
;
17918 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17922 if (type
== error_mark_node
)
17923 return error_mark_node
;
17925 /* Allow attributes on forward declarations of classes. */
17928 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17929 warning (OPT_Wattributes
,
17930 "attributes ignored on uninstantiated type");
17931 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17932 && ! processing_explicit_instantiation
)
17933 warning (OPT_Wattributes
,
17934 "attributes ignored on template instantiation");
17935 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17936 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17938 warning (OPT_Wattributes
,
17939 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17942 if (tag_type
!= enum_type
)
17944 /* Indicate whether this class was declared as a `class' or as a
17946 if (CLASS_TYPE_P (type
))
17947 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17948 cp_parser_check_class_key (tag_type
, type
);
17951 /* A "<" cannot follow an elaborated type specifier. If that
17952 happens, the user was probably trying to form a template-id. */
17953 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
17959 /* Parse an enum-specifier.
17962 enum-head { enumerator-list [opt] }
17963 enum-head { enumerator-list , } [C++0x]
17966 enum-key identifier [opt] enum-base [opt]
17967 enum-key nested-name-specifier identifier enum-base [opt]
17972 enum struct [C++0x]
17975 : type-specifier-seq
17977 opaque-enum-specifier:
17978 enum-key identifier enum-base [opt] ;
17981 enum-key attributes[opt] identifier [opt] enum-base [opt]
17982 { enumerator-list [opt] }attributes[opt]
17983 enum-key attributes[opt] identifier [opt] enum-base [opt]
17984 { enumerator-list, }attributes[opt] [C++0x]
17986 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17987 if the token stream isn't an enum-specifier after all. */
17990 cp_parser_enum_specifier (cp_parser
* parser
)
17993 tree type
= NULL_TREE
;
17995 tree nested_name_specifier
= NULL_TREE
;
17997 bool scoped_enum_p
= false;
17998 bool has_underlying_type
= false;
17999 bool nested_being_defined
= false;
18000 bool new_value_list
= false;
18001 bool is_new_type
= false;
18002 bool is_unnamed
= false;
18003 tree underlying_type
= NULL_TREE
;
18004 cp_token
*type_start_token
= NULL
;
18005 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18007 parser
->colon_corrects_to_scope_p
= false;
18009 /* Parse tentatively so that we can back up if we don't find a
18011 cp_parser_parse_tentatively (parser
);
18013 /* Caller guarantees that the current token is 'enum', an identifier
18014 possibly follows, and the token after that is an opening brace.
18015 If we don't have an identifier, fabricate an anonymous name for
18016 the enumeration being defined. */
18017 cp_lexer_consume_token (parser
->lexer
);
18019 /* Parse the "class" or "struct", which indicates a scoped
18020 enumeration type in C++0x. */
18021 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18022 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18024 if (cxx_dialect
< cxx11
)
18025 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18027 /* Consume the `struct' or `class' token. */
18028 cp_lexer_consume_token (parser
->lexer
);
18030 scoped_enum_p
= true;
18033 attributes
= cp_parser_attributes_opt (parser
);
18035 /* Clear the qualification. */
18036 parser
->scope
= NULL_TREE
;
18037 parser
->qualifying_scope
= NULL_TREE
;
18038 parser
->object_scope
= NULL_TREE
;
18040 /* Figure out in what scope the declaration is being placed. */
18041 prev_scope
= current_scope ();
18043 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18045 push_deferring_access_checks (dk_no_check
);
18046 nested_name_specifier
18047 = cp_parser_nested_name_specifier_opt (parser
,
18048 /*typename_keyword_p=*/true,
18049 /*check_dependency_p=*/false,
18051 /*is_declaration=*/false);
18053 if (nested_name_specifier
)
18057 identifier
= cp_parser_identifier (parser
);
18058 name
= cp_parser_lookup_name (parser
, identifier
,
18060 /*is_template=*/false,
18061 /*is_namespace=*/false,
18062 /*check_dependency=*/true,
18063 /*ambiguous_decls=*/NULL
,
18065 if (name
&& name
!= error_mark_node
)
18067 type
= TREE_TYPE (name
);
18068 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18070 /* Are template enums allowed in ISO? */
18071 if (template_parm_scope_p ())
18072 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18073 "%qD is an enumeration template", name
);
18074 /* ignore a typename reference, for it will be solved by name
18079 else if (nested_name_specifier
== error_mark_node
)
18080 /* We already issued an error. */;
18083 error_at (type_start_token
->location
,
18084 "%qD does not name an enumeration in %qT",
18085 identifier
, nested_name_specifier
);
18086 nested_name_specifier
= error_mark_node
;
18091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18092 identifier
= cp_parser_identifier (parser
);
18095 identifier
= make_anon_name ();
18098 error_at (type_start_token
->location
,
18099 "unnamed scoped enum is not allowed");
18102 pop_deferring_access_checks ();
18104 /* Check for the `:' that denotes a specified underlying type in C++0x.
18105 Note that a ':' could also indicate a bitfield width, however. */
18106 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18108 cp_decl_specifier_seq type_specifiers
;
18110 /* Consume the `:'. */
18111 cp_lexer_consume_token (parser
->lexer
);
18113 /* Parse the type-specifier-seq. */
18114 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18115 /*is_trailing_return=*/false,
18118 /* At this point this is surely not elaborated type specifier. */
18119 if (!cp_parser_parse_definitely (parser
))
18122 if (cxx_dialect
< cxx11
)
18123 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18125 has_underlying_type
= true;
18127 /* If that didn't work, stop. */
18128 if (type_specifiers
.type
!= error_mark_node
)
18130 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18131 /*initialized=*/0, NULL
);
18132 if (underlying_type
== error_mark_node
18133 || check_for_bare_parameter_packs (underlying_type
))
18134 underlying_type
= NULL_TREE
;
18138 /* Look for the `{' but don't consume it yet. */
18139 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18141 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18143 cp_parser_error (parser
, "expected %<{%>");
18144 if (has_underlying_type
)
18150 /* An opaque-enum-specifier must have a ';' here. */
18151 if ((scoped_enum_p
|| underlying_type
)
18152 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18154 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18155 if (has_underlying_type
)
18163 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18166 if (nested_name_specifier
)
18168 if (CLASS_TYPE_P (nested_name_specifier
))
18170 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18171 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18172 push_scope (nested_name_specifier
);
18174 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18176 push_nested_namespace (nested_name_specifier
);
18180 /* Issue an error message if type-definitions are forbidden here. */
18181 if (!cp_parser_check_type_definition (parser
))
18182 type
= error_mark_node
;
18184 /* Create the new type. We do this before consuming the opening
18185 brace so the enum will be recorded as being on the line of its
18186 tag (or the 'enum' keyword, if there is no tag). */
18187 type
= start_enum (identifier
, type
, underlying_type
,
18188 attributes
, scoped_enum_p
, &is_new_type
);
18190 /* If the next token is not '{' it is an opaque-enum-specifier or an
18191 elaborated-type-specifier. */
18192 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18194 timevar_push (TV_PARSE_ENUM
);
18195 if (nested_name_specifier
18196 && nested_name_specifier
!= error_mark_node
)
18198 /* The following catches invalid code such as:
18199 enum class S<int>::E { A, B, C }; */
18200 if (!processing_specialization
18201 && CLASS_TYPE_P (nested_name_specifier
)
18202 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18203 error_at (type_start_token
->location
, "cannot add an enumerator "
18204 "list to a template instantiation");
18206 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18208 error_at (type_start_token
->location
,
18209 "%<%T::%E%> has not been declared",
18210 TYPE_CONTEXT (nested_name_specifier
),
18211 nested_name_specifier
);
18212 type
= error_mark_node
;
18214 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18215 && !CLASS_TYPE_P (nested_name_specifier
))
18217 error_at (type_start_token
->location
, "nested name specifier "
18218 "%qT for enum declaration does not name a class "
18219 "or namespace", nested_name_specifier
);
18220 type
= error_mark_node
;
18222 /* If that scope does not contain the scope in which the
18223 class was originally declared, the program is invalid. */
18224 else if (prev_scope
&& !is_ancestor (prev_scope
,
18225 nested_name_specifier
))
18227 if (at_namespace_scope_p ())
18228 error_at (type_start_token
->location
,
18229 "declaration of %qD in namespace %qD which does not "
18231 type
, prev_scope
, nested_name_specifier
);
18233 error_at (type_start_token
->location
,
18234 "declaration of %qD in %qD which does not "
18236 type
, prev_scope
, nested_name_specifier
);
18237 type
= error_mark_node
;
18239 /* If that scope is the scope where the declaration is being placed
18240 the program is invalid. */
18241 else if (CLASS_TYPE_P (nested_name_specifier
)
18242 && CLASS_TYPE_P (prev_scope
)
18243 && same_type_p (nested_name_specifier
, prev_scope
))
18245 permerror (type_start_token
->location
,
18246 "extra qualification not allowed");
18247 nested_name_specifier
= NULL_TREE
;
18252 begin_scope (sk_scoped_enum
, type
);
18254 /* Consume the opening brace. */
18255 matching_braces braces
;
18256 braces
.consume_open (parser
);
18258 if (type
== error_mark_node
)
18259 ; /* Nothing to add */
18260 else if (OPAQUE_ENUM_P (type
)
18261 || (cxx_dialect
> cxx98
&& processing_specialization
))
18263 new_value_list
= true;
18264 SET_OPAQUE_ENUM_P (type
, false);
18265 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18269 error_at (type_start_token
->location
,
18270 "multiple definition of %q#T", type
);
18271 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18272 "previous definition here");
18273 type
= error_mark_node
;
18276 if (type
== error_mark_node
)
18277 cp_parser_skip_to_end_of_block_or_statement (parser
);
18278 /* If the next token is not '}', then there are some enumerators. */
18279 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18281 if (is_unnamed
&& !scoped_enum_p
)
18282 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18283 "ISO C++ forbids empty unnamed enum");
18286 cp_parser_enumerator_list (parser
, type
);
18288 /* Consume the final '}'. */
18289 braces
.require_close (parser
);
18293 timevar_pop (TV_PARSE_ENUM
);
18297 /* If a ';' follows, then it is an opaque-enum-specifier
18298 and additional restrictions apply. */
18299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18302 error_at (type_start_token
->location
,
18303 "opaque-enum-specifier without name");
18304 else if (nested_name_specifier
)
18305 error_at (type_start_token
->location
,
18306 "opaque-enum-specifier must use a simple identifier");
18310 /* Look for trailing attributes to apply to this enumeration, and
18311 apply them if appropriate. */
18312 if (cp_parser_allow_gnu_extensions_p (parser
))
18314 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18315 cplus_decl_attributes (&type
,
18317 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18320 /* Finish up the enumeration. */
18321 if (type
!= error_mark_node
)
18323 if (new_value_list
)
18324 finish_enum_value_list (type
);
18326 finish_enum (type
);
18329 if (nested_name_specifier
)
18331 if (CLASS_TYPE_P (nested_name_specifier
))
18333 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18334 pop_scope (nested_name_specifier
);
18336 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18338 pop_nested_namespace (nested_name_specifier
);
18342 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18346 /* Parse an enumerator-list. The enumerators all have the indicated
18350 enumerator-definition
18351 enumerator-list , enumerator-definition */
18354 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18358 /* Parse an enumerator-definition. */
18359 cp_parser_enumerator_definition (parser
, type
);
18361 /* If the next token is not a ',', we've reached the end of
18363 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18365 /* Otherwise, consume the `,' and keep going. */
18366 cp_lexer_consume_token (parser
->lexer
);
18367 /* If the next token is a `}', there is a trailing comma. */
18368 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18370 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18371 pedwarn (input_location
, OPT_Wpedantic
,
18372 "comma at end of enumerator list");
18378 /* Parse an enumerator-definition. The enumerator has the indicated
18381 enumerator-definition:
18383 enumerator = constant-expression
18390 enumerator-definition:
18391 enumerator attributes [opt]
18392 enumerator attributes [opt] = constant-expression */
18395 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18401 /* Save the input location because we are interested in the location
18402 of the identifier and not the location of the explicit value. */
18403 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18405 /* Look for the identifier. */
18406 identifier
= cp_parser_identifier (parser
);
18407 if (identifier
== error_mark_node
)
18410 /* Parse any specified attributes. */
18411 tree attrs
= cp_parser_attributes_opt (parser
);
18413 /* If the next token is an '=', then there is an explicit value. */
18414 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18416 /* Consume the `=' token. */
18417 cp_lexer_consume_token (parser
->lexer
);
18418 /* Parse the value. */
18419 value
= cp_parser_constant_expression (parser
);
18424 /* If we are processing a template, make sure the initializer of the
18425 enumerator doesn't contain any bare template parameter pack. */
18426 if (check_for_bare_parameter_packs (value
))
18427 value
= error_mark_node
;
18429 /* Create the enumerator. */
18430 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18433 /* Parse a namespace-name.
18436 original-namespace-name
18439 Returns the NAMESPACE_DECL for the namespace. */
18442 cp_parser_namespace_name (cp_parser
* parser
)
18445 tree namespace_decl
;
18447 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18449 /* Get the name of the namespace. */
18450 identifier
= cp_parser_identifier (parser
);
18451 if (identifier
== error_mark_node
)
18452 return error_mark_node
;
18454 /* Look up the identifier in the currently active scope. Look only
18455 for namespaces, due to:
18457 [basic.lookup.udir]
18459 When looking up a namespace-name in a using-directive or alias
18460 definition, only namespace names are considered.
18464 [basic.lookup.qual]
18466 During the lookup of a name preceding the :: scope resolution
18467 operator, object, function, and enumerator names are ignored.
18469 (Note that cp_parser_qualifying_entity only calls this
18470 function if the token after the name is the scope resolution
18472 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18474 /*is_template=*/false,
18475 /*is_namespace=*/true,
18476 /*check_dependency=*/true,
18477 /*ambiguous_decls=*/NULL
,
18479 /* If it's not a namespace, issue an error. */
18480 if (namespace_decl
== error_mark_node
18481 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18483 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18485 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18486 if (namespace_decl
== error_mark_node
18487 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18488 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18491 cp_parser_error (parser
, "expected namespace-name");
18492 namespace_decl
= error_mark_node
;
18495 return namespace_decl
;
18498 /* Parse a namespace-definition.
18500 namespace-definition:
18501 named-namespace-definition
18502 unnamed-namespace-definition
18504 named-namespace-definition:
18505 original-namespace-definition
18506 extension-namespace-definition
18508 original-namespace-definition:
18509 namespace identifier { namespace-body }
18511 extension-namespace-definition:
18512 namespace original-namespace-name { namespace-body }
18514 unnamed-namespace-definition:
18515 namespace { namespace-body } */
18518 cp_parser_namespace_definition (cp_parser
* parser
)
18521 int nested_definition_count
= 0;
18523 cp_ensure_no_omp_declare_simd (parser
);
18524 cp_ensure_no_oacc_routine (parser
);
18526 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18530 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18531 cp_lexer_consume_token (parser
->lexer
);
18534 /* Look for the `namespace' keyword. */
18536 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18538 /* Parse any specified attributes before the identifier. */
18539 tree attribs
= cp_parser_attributes_opt (parser
);
18543 identifier
= NULL_TREE
;
18545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18547 identifier
= cp_parser_identifier (parser
);
18549 /* Parse any attributes specified after the identifier. */
18550 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18553 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18556 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18557 pedwarn (input_location
, OPT_Wpedantic
,
18558 "nested namespace definitions only available with "
18559 "-std=c++17 or -std=gnu++17");
18561 /* Nested namespace names can create new namespaces (unlike
18562 other qualified-ids). */
18563 if (int count
= identifier
? push_namespace (identifier
) : 0)
18564 nested_definition_count
+= count
;
18566 cp_parser_error (parser
, "nested namespace name required");
18567 cp_lexer_consume_token (parser
->lexer
);
18570 if (nested_definition_count
&& !identifier
)
18571 cp_parser_error (parser
, "namespace name required");
18573 if (nested_definition_count
&& attribs
)
18574 error_at (token
->location
,
18575 "a nested namespace definition cannot have attributes");
18576 if (nested_definition_count
&& is_inline
)
18577 error_at (token
->location
,
18578 "a nested namespace definition cannot be inline");
18580 /* Start the namespace. */
18581 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18583 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18585 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18587 /* Look for the `{' to validate starting the namespace. */
18588 matching_braces braces
;
18589 if (braces
.require_open (parser
))
18591 /* Parse the body of the namespace. */
18592 cp_parser_namespace_body (parser
);
18594 /* Look for the final `}'. */
18595 braces
.require_close (parser
);
18598 if (has_visibility
)
18599 pop_visibility (1);
18601 /* Pop the nested namespace definitions. */
18602 while (nested_definition_count
--)
18606 /* Parse a namespace-body.
18609 declaration-seq [opt] */
18612 cp_parser_namespace_body (cp_parser
* parser
)
18614 cp_parser_declaration_seq_opt (parser
);
18617 /* Parse a namespace-alias-definition.
18619 namespace-alias-definition:
18620 namespace identifier = qualified-namespace-specifier ; */
18623 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18626 tree namespace_specifier
;
18628 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18630 /* Look for the `namespace' keyword. */
18631 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18632 /* Look for the identifier. */
18633 identifier
= cp_parser_identifier (parser
);
18634 if (identifier
== error_mark_node
)
18636 /* Look for the `=' token. */
18637 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18638 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18640 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18641 /* Skip the definition. */
18642 cp_lexer_consume_token (parser
->lexer
);
18643 if (cp_parser_skip_to_closing_brace (parser
))
18644 cp_lexer_consume_token (parser
->lexer
);
18647 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18648 /* Look for the qualified-namespace-specifier. */
18649 namespace_specifier
18650 = cp_parser_qualified_namespace_specifier (parser
);
18651 /* Look for the `;' token. */
18652 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18654 /* Register the alias in the symbol table. */
18655 do_namespace_alias (identifier
, namespace_specifier
);
18658 /* Parse a qualified-namespace-specifier.
18660 qualified-namespace-specifier:
18661 :: [opt] nested-name-specifier [opt] namespace-name
18663 Returns a NAMESPACE_DECL corresponding to the specified
18667 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18669 /* Look for the optional `::'. */
18670 cp_parser_global_scope_opt (parser
,
18671 /*current_scope_valid_p=*/false);
18673 /* Look for the optional nested-name-specifier. */
18674 cp_parser_nested_name_specifier_opt (parser
,
18675 /*typename_keyword_p=*/false,
18676 /*check_dependency_p=*/true,
18678 /*is_declaration=*/true);
18680 return cp_parser_namespace_name (parser
);
18683 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18684 access declaration.
18687 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18688 using :: unqualified-id ;
18690 access-declaration:
18696 cp_parser_using_declaration (cp_parser
* parser
,
18697 bool access_declaration_p
)
18700 bool typename_p
= false;
18701 bool global_scope_p
;
18705 int oldcount
= errorcount
;
18706 cp_token
*diag_token
= NULL
;
18708 if (access_declaration_p
)
18710 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18711 cp_parser_parse_tentatively (parser
);
18715 /* Look for the `using' keyword. */
18716 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18719 /* Peek at the next token. */
18720 token
= cp_lexer_peek_token (parser
->lexer
);
18721 /* See if it's `typename'. */
18722 if (token
->keyword
== RID_TYPENAME
)
18724 /* Remember that we've seen it. */
18726 /* Consume the `typename' token. */
18727 cp_lexer_consume_token (parser
->lexer
);
18731 /* Look for the optional global scope qualification. */
18733 = (cp_parser_global_scope_opt (parser
,
18734 /*current_scope_valid_p=*/false)
18737 /* If we saw `typename', or didn't see `::', then there must be a
18738 nested-name-specifier present. */
18739 if (typename_p
|| !global_scope_p
)
18741 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18742 /*check_dependency_p=*/true,
18744 /*is_declaration=*/true);
18745 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18747 cp_parser_skip_to_end_of_block_or_statement (parser
);
18751 /* Otherwise, we could be in either of the two productions. In that
18752 case, treat the nested-name-specifier as optional. */
18754 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18755 /*typename_keyword_p=*/false,
18756 /*check_dependency_p=*/true,
18758 /*is_declaration=*/true);
18760 qscope
= global_namespace
;
18761 else if (UNSCOPED_ENUM_P (qscope
))
18762 qscope
= CP_TYPE_CONTEXT (qscope
);
18764 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18765 /* Something has already gone wrong; there's no need to parse
18766 further. Since an error has occurred, the return value of
18767 cp_parser_parse_definitely will be false, as required. */
18768 return cp_parser_parse_definitely (parser
);
18770 token
= cp_lexer_peek_token (parser
->lexer
);
18771 /* Parse the unqualified-id. */
18772 identifier
= cp_parser_unqualified_id (parser
,
18773 /*template_keyword_p=*/false,
18774 /*check_dependency_p=*/true,
18775 /*declarator_p=*/true,
18776 /*optional_p=*/false);
18778 if (access_declaration_p
)
18780 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18781 cp_parser_simulate_error (parser
);
18782 if (!cp_parser_parse_definitely (parser
))
18785 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18787 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18788 if (cxx_dialect
< cxx17
18789 && !in_system_header_at (ell
->location
))
18790 pedwarn (ell
->location
, 0,
18791 "pack expansion in using-declaration only available "
18792 "with -std=c++17 or -std=gnu++17");
18793 qscope
= make_pack_expansion (qscope
);
18796 /* The function we call to handle a using-declaration is different
18797 depending on what scope we are in. */
18798 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18800 else if (!identifier_p (identifier
)
18801 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18802 /* [namespace.udecl]
18804 A using declaration shall not name a template-id. */
18805 error_at (token
->location
,
18806 "a template-id may not appear in a using-declaration");
18809 if (at_class_scope_p ())
18811 /* Create the USING_DECL. */
18812 decl
= do_class_using_decl (qscope
, identifier
);
18814 if (decl
&& typename_p
)
18815 USING_DECL_TYPENAME_P (decl
) = 1;
18817 if (check_for_bare_parameter_packs (decl
))
18819 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18823 /* Add it to the list of members in this class. */
18824 finish_member_declaration (decl
);
18828 decl
= cp_parser_lookup_name_simple (parser
,
18831 if (decl
== error_mark_node
)
18832 cp_parser_name_lookup_error (parser
, identifier
,
18835 else if (check_for_bare_parameter_packs (decl
))
18837 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18840 else if (!at_namespace_scope_p ())
18841 finish_local_using_decl (decl
, qscope
, identifier
);
18843 finish_namespace_using_decl (decl
, qscope
, identifier
);
18847 if (!access_declaration_p
18848 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18850 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18851 if (cxx_dialect
< cxx17
)
18852 pedwarn (comma
->location
, 0,
18853 "comma-separated list in using-declaration only available "
18854 "with -std=c++17 or -std=gnu++17");
18858 /* Look for the final `;'. */
18859 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18861 if (access_declaration_p
&& errorcount
== oldcount
)
18862 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18863 "access declarations are deprecated "
18864 "in favour of using-declarations; "
18865 "suggestion: add the %<using%> keyword");
18870 /* Parse an alias-declaration.
18873 using identifier attribute-specifier-seq [opt] = type-id */
18876 cp_parser_alias_declaration (cp_parser
* parser
)
18878 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18879 location_t id_location
;
18880 cp_declarator
*declarator
;
18881 cp_decl_specifier_seq decl_specs
;
18883 const char *saved_message
= NULL
;
18885 /* Look for the `using' keyword. */
18886 cp_token
*using_token
18887 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18888 if (using_token
== NULL
)
18889 return error_mark_node
;
18891 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18892 id
= cp_parser_identifier (parser
);
18893 if (id
== error_mark_node
)
18894 return error_mark_node
;
18896 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18897 attributes
= cp_parser_attributes_opt (parser
);
18898 if (attributes
== error_mark_node
)
18899 return error_mark_node
;
18901 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18903 if (cp_parser_error_occurred (parser
))
18904 return error_mark_node
;
18906 cp_parser_commit_to_tentative_parse (parser
);
18908 /* Now we are going to parse the type-id of the declaration. */
18913 "A type-specifier-seq shall not define a class or enumeration
18914 unless it appears in the type-id of an alias-declaration (7.1.3) that
18915 is not the declaration of a template-declaration."
18917 In other words, if we currently are in an alias template, the
18918 type-id should not define a type.
18920 So let's set parser->type_definition_forbidden_message in that
18921 case; cp_parser_check_type_definition (called by
18922 cp_parser_class_specifier) will then emit an error if a type is
18923 defined in the type-id. */
18924 if (parser
->num_template_parameter_lists
)
18926 saved_message
= parser
->type_definition_forbidden_message
;
18927 parser
->type_definition_forbidden_message
=
18928 G_("types may not be defined in alias template declarations");
18931 type
= cp_parser_type_id (parser
);
18933 /* Restore the error message if need be. */
18934 if (parser
->num_template_parameter_lists
)
18935 parser
->type_definition_forbidden_message
= saved_message
;
18937 if (type
== error_mark_node
18938 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18940 cp_parser_skip_to_end_of_block_or_statement (parser
);
18941 return error_mark_node
;
18944 /* A typedef-name can also be introduced by an alias-declaration. The
18945 identifier following the using keyword becomes a typedef-name. It has
18946 the same semantics as if it were introduced by the typedef
18947 specifier. In particular, it does not define a new type and it shall
18948 not appear in the type-id. */
18950 clear_decl_specs (&decl_specs
);
18951 decl_specs
.type
= type
;
18952 if (attributes
!= NULL_TREE
)
18954 decl_specs
.attributes
= attributes
;
18955 set_and_check_decl_spec_loc (&decl_specs
,
18959 set_and_check_decl_spec_loc (&decl_specs
,
18962 set_and_check_decl_spec_loc (&decl_specs
,
18966 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
18967 declarator
->id_loc
= id_location
;
18969 member_p
= at_class_scope_p ();
18971 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
18972 NULL_TREE
, attributes
);
18974 decl
= start_decl (declarator
, &decl_specs
, 0,
18975 attributes
, NULL_TREE
, &pushed_scope
);
18976 if (decl
== error_mark_node
)
18979 // Attach constraints to the alias declaration.
18980 if (flag_concepts
&& current_template_parms
)
18982 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
18983 tree constr
= build_constraints (reqs
, NULL_TREE
);
18984 set_constraints (decl
, constr
);
18987 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
18990 pop_scope (pushed_scope
);
18992 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18993 added into the symbol table; otherwise, return the TYPE_DECL. */
18994 if (DECL_LANG_SPECIFIC (decl
)
18995 && DECL_TEMPLATE_INFO (decl
)
18996 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
18998 decl
= DECL_TI_TEMPLATE (decl
);
19000 check_member_template (decl
);
19006 /* Parse a using-directive.
19009 using namespace :: [opt] nested-name-specifier [opt]
19010 namespace-name ; */
19013 cp_parser_using_directive (cp_parser
* parser
)
19015 tree namespace_decl
;
19018 /* Look for the `using' keyword. */
19019 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19020 /* And the `namespace' keyword. */
19021 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19022 /* Look for the optional `::' operator. */
19023 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19024 /* And the optional nested-name-specifier. */
19025 cp_parser_nested_name_specifier_opt (parser
,
19026 /*typename_keyword_p=*/false,
19027 /*check_dependency_p=*/true,
19029 /*is_declaration=*/true);
19030 /* Get the namespace being used. */
19031 namespace_decl
= cp_parser_namespace_name (parser
);
19032 /* And any specified attributes. */
19033 attribs
= cp_parser_attributes_opt (parser
);
19035 /* Update the symbol table. */
19036 if (namespace_bindings_p ())
19037 finish_namespace_using_directive (namespace_decl
, attribs
);
19039 finish_local_using_directive (namespace_decl
, attribs
);
19041 /* Look for the final `;'. */
19042 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19045 /* Parse an asm-definition.
19048 asm ( string-literal ) ;
19053 asm volatile [opt] ( string-literal ) ;
19054 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19055 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19056 : asm-operand-list [opt] ) ;
19057 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19058 : asm-operand-list [opt]
19059 : asm-clobber-list [opt] ) ;
19060 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19061 : asm-clobber-list [opt]
19062 : asm-goto-list ) ; */
19065 cp_parser_asm_definition (cp_parser
* parser
)
19068 tree outputs
= NULL_TREE
;
19069 tree inputs
= NULL_TREE
;
19070 tree clobbers
= NULL_TREE
;
19071 tree labels
= NULL_TREE
;
19073 bool volatile_p
= false;
19074 bool extended_p
= false;
19075 bool invalid_inputs_p
= false;
19076 bool invalid_outputs_p
= false;
19077 bool goto_p
= false;
19078 required_token missing
= RT_NONE
;
19080 /* Look for the `asm' keyword. */
19081 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19083 if (parser
->in_function_body
19084 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19086 error ("%<asm%> in %<constexpr%> function");
19087 cp_function_chain
->invalid_constexpr
= true;
19090 /* See if the next token is `volatile'. */
19091 if (cp_parser_allow_gnu_extensions_p (parser
)
19092 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19094 /* Remember that we saw the `volatile' keyword. */
19096 /* Consume the token. */
19097 cp_lexer_consume_token (parser
->lexer
);
19099 if (cp_parser_allow_gnu_extensions_p (parser
)
19100 && parser
->in_function_body
19101 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19103 /* Remember that we saw the `goto' keyword. */
19105 /* Consume the token. */
19106 cp_lexer_consume_token (parser
->lexer
);
19108 /* Look for the opening `('. */
19109 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19111 /* Look for the string. */
19112 string
= cp_parser_string_literal (parser
, false, false);
19113 if (string
== error_mark_node
)
19115 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19116 /*consume_paren=*/true);
19120 /* If we're allowing GNU extensions, check for the extended assembly
19121 syntax. Unfortunately, the `:' tokens need not be separated by
19122 a space in C, and so, for compatibility, we tolerate that here
19123 too. Doing that means that we have to treat the `::' operator as
19125 if (cp_parser_allow_gnu_extensions_p (parser
)
19126 && parser
->in_function_body
19127 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19128 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19130 bool inputs_p
= false;
19131 bool clobbers_p
= false;
19132 bool labels_p
= false;
19134 /* The extended syntax was used. */
19137 /* Look for outputs. */
19138 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19140 /* Consume the `:'. */
19141 cp_lexer_consume_token (parser
->lexer
);
19142 /* Parse the output-operands. */
19143 if (cp_lexer_next_token_is_not (parser
->lexer
,
19145 && cp_lexer_next_token_is_not (parser
->lexer
,
19147 && cp_lexer_next_token_is_not (parser
->lexer
,
19151 outputs
= cp_parser_asm_operand_list (parser
);
19152 if (outputs
== error_mark_node
)
19153 invalid_outputs_p
= true;
19156 /* If the next token is `::', there are no outputs, and the
19157 next token is the beginning of the inputs. */
19158 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19159 /* The inputs are coming next. */
19162 /* Look for inputs. */
19164 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19166 /* Consume the `:' or `::'. */
19167 cp_lexer_consume_token (parser
->lexer
);
19168 /* Parse the output-operands. */
19169 if (cp_lexer_next_token_is_not (parser
->lexer
,
19171 && cp_lexer_next_token_is_not (parser
->lexer
,
19173 && cp_lexer_next_token_is_not (parser
->lexer
,
19176 inputs
= cp_parser_asm_operand_list (parser
);
19177 if (inputs
== error_mark_node
)
19178 invalid_inputs_p
= true;
19181 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19182 /* The clobbers are coming next. */
19185 /* Look for clobbers. */
19187 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19190 /* Consume the `:' or `::'. */
19191 cp_lexer_consume_token (parser
->lexer
);
19192 /* Parse the clobbers. */
19193 if (cp_lexer_next_token_is_not (parser
->lexer
,
19195 && cp_lexer_next_token_is_not (parser
->lexer
,
19197 clobbers
= cp_parser_asm_clobber_list (parser
);
19200 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19201 /* The labels are coming next. */
19204 /* Look for labels. */
19206 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19209 /* Consume the `:' or `::'. */
19210 cp_lexer_consume_token (parser
->lexer
);
19211 /* Parse the labels. */
19212 labels
= cp_parser_asm_label_list (parser
);
19215 if (goto_p
&& !labels_p
)
19216 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19219 missing
= RT_COLON_SCOPE
;
19221 /* Look for the closing `)'. */
19222 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19223 missing
? missing
: RT_CLOSE_PAREN
))
19224 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19225 /*consume_paren=*/true);
19226 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19228 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19230 /* Create the ASM_EXPR. */
19231 if (parser
->in_function_body
)
19233 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19234 inputs
, clobbers
, labels
);
19235 /* If the extended syntax was not used, mark the ASM_EXPR. */
19238 tree temp
= asm_stmt
;
19239 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19240 temp
= TREE_OPERAND (temp
, 0);
19242 ASM_INPUT_P (temp
) = 1;
19246 symtab
->finalize_toplevel_asm (string
);
19250 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19251 type that comes from the decl-specifier-seq. */
19254 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19256 for (cp_declarator
*d
= declarator
; d
;)
19266 if (TYPE_PTRMEMFUNC_P (type
))
19267 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19268 type
= TREE_TYPE (type
);
19276 /* Declarators [gram.dcl.decl] */
19278 /* Parse an init-declarator.
19281 declarator initializer [opt]
19286 declarator asm-specification [opt] attributes [opt] initializer [opt]
19288 function-definition:
19289 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19291 decl-specifier-seq [opt] declarator function-try-block
19295 function-definition:
19296 __extension__ function-definition
19300 function-definition:
19301 decl-specifier-seq [opt] declarator function-transaction-block
19303 The DECL_SPECIFIERS apply to this declarator. Returns a
19304 representation of the entity declared. If MEMBER_P is TRUE, then
19305 this declarator appears in a class scope. The new DECL created by
19306 this declarator is returned.
19308 The CHECKS are access checks that should be performed once we know
19309 what entity is being declared (and, therefore, what classes have
19312 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19313 for a function-definition here as well. If the declarator is a
19314 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19315 be TRUE upon return. By that point, the function-definition will
19316 have been completely parsed.
19318 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19321 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19322 parsed declaration if it is an uninitialized single declarator not followed
19323 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19324 if present, will not be consumed. If returned, this declarator will be
19325 created with SD_INITIALIZED but will not call cp_finish_decl.
19327 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19328 and there is an initializer, the pointed location_t is set to the
19329 location of the '=' or `(', or '{' in C++11 token introducing the
19333 cp_parser_init_declarator (cp_parser
* parser
,
19334 cp_decl_specifier_seq
*decl_specifiers
,
19335 vec
<deferred_access_check
, va_gc
> *checks
,
19336 bool function_definition_allowed_p
,
19338 int declares_class_or_enum
,
19339 bool* function_definition_p
,
19340 tree
* maybe_range_for_decl
,
19341 location_t
* init_loc
,
19344 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19345 *attributes_start_token
= NULL
;
19346 cp_declarator
*declarator
;
19347 tree prefix_attributes
;
19348 tree attributes
= NULL
;
19349 tree asm_specification
;
19351 tree decl
= NULL_TREE
;
19353 int is_initialized
;
19354 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19355 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19357 enum cpp_ttype initialization_kind
;
19358 bool is_direct_init
= false;
19359 bool is_non_constant_init
;
19360 int ctor_dtor_or_conv_p
;
19361 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19362 tree pushed_scope
= NULL_TREE
;
19363 bool range_for_decl_p
= false;
19364 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19365 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19367 /* Gather the attributes that were provided with the
19368 decl-specifiers. */
19369 prefix_attributes
= decl_specifiers
->attributes
;
19371 /* Assume that this is not the declarator for a function
19373 if (function_definition_p
)
19374 *function_definition_p
= false;
19376 /* Default arguments are only permitted for function parameters. */
19377 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19378 parser
->default_arg_ok_p
= false;
19380 /* Defer access checks while parsing the declarator; we cannot know
19381 what names are accessible until we know what is being
19383 resume_deferring_access_checks ();
19385 token
= cp_lexer_peek_token (parser
->lexer
);
19387 /* Parse the declarator. */
19389 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19390 &ctor_dtor_or_conv_p
,
19391 /*parenthesized_p=*/NULL
,
19392 member_p
, friend_p
);
19393 /* Gather up the deferred checks. */
19394 stop_deferring_access_checks ();
19396 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19398 /* If the DECLARATOR was erroneous, there's no need to go
19400 if (declarator
== cp_error_declarator
)
19401 return error_mark_node
;
19403 /* Check that the number of template-parameter-lists is OK. */
19404 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19406 return error_mark_node
;
19408 if (declares_class_or_enum
& 2)
19409 cp_parser_check_for_definition_in_return_type (declarator
,
19410 decl_specifiers
->type
,
19411 decl_specifiers
->locations
[ds_type_spec
]);
19413 /* Figure out what scope the entity declared by the DECLARATOR is
19414 located in. `grokdeclarator' sometimes changes the scope, so
19415 we compute it now. */
19416 scope
= get_scope_of_declarator (declarator
);
19418 /* Perform any lookups in the declared type which were thought to be
19419 dependent, but are not in the scope of the declarator. */
19420 decl_specifiers
->type
19421 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19423 /* If we're allowing GNU extensions, look for an
19424 asm-specification. */
19425 if (cp_parser_allow_gnu_extensions_p (parser
))
19427 /* Look for an asm-specification. */
19428 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19429 asm_specification
= cp_parser_asm_specification_opt (parser
);
19432 asm_specification
= NULL_TREE
;
19434 /* Look for attributes. */
19435 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19436 attributes
= cp_parser_attributes_opt (parser
);
19438 /* Peek at the next token. */
19439 token
= cp_lexer_peek_token (parser
->lexer
);
19441 bool bogus_implicit_tmpl
= false;
19443 if (function_declarator_p (declarator
))
19445 /* Handle C++17 deduction guides. */
19446 if (!decl_specifiers
->type
19447 && ctor_dtor_or_conv_p
<= 0
19448 && cxx_dialect
>= cxx17
)
19450 cp_declarator
*id
= get_id_declarator (declarator
);
19451 tree name
= id
->u
.id
.unqualified_name
;
19452 parser
->scope
= id
->u
.id
.qualifying_scope
;
19453 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19455 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19456 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19458 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19459 id
->u
.id
.sfk
= sfk_deduction_guide
;
19460 ctor_dtor_or_conv_p
= 1;
19464 /* Check to see if the token indicates the start of a
19465 function-definition. */
19466 if (cp_parser_token_starts_function_definition_p (token
))
19468 if (!function_definition_allowed_p
)
19470 /* If a function-definition should not appear here, issue an
19472 cp_parser_error (parser
,
19473 "a function-definition is not allowed here");
19474 return error_mark_node
;
19477 location_t func_brace_location
19478 = cp_lexer_peek_token (parser
->lexer
)->location
;
19480 /* Neither attributes nor an asm-specification are allowed
19481 on a function-definition. */
19482 if (asm_specification
)
19483 error_at (asm_spec_start_token
->location
,
19484 "an asm-specification is not allowed "
19485 "on a function-definition");
19487 error_at (attributes_start_token
->location
,
19488 "attributes are not allowed "
19489 "on a function-definition");
19490 /* This is a function-definition. */
19491 *function_definition_p
= true;
19493 /* Parse the function definition. */
19495 decl
= cp_parser_save_member_function_body (parser
,
19498 prefix_attributes
);
19501 (cp_parser_function_definition_from_specifiers_and_declarator
19502 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19504 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19506 /* This is where the prologue starts... */
19507 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19508 = func_brace_location
;
19514 else if (parser
->fully_implicit_function_template_p
)
19516 /* A non-template declaration involving a function parameter list
19517 containing an implicit template parameter will be made into a
19518 template. If the resulting declaration is not going to be an
19519 actual function then finish the template scope here to prevent it.
19520 An error message will be issued once we have a decl to talk about.
19522 FIXME probably we should do type deduction rather than create an
19523 implicit template, but the standard currently doesn't allow it. */
19524 bogus_implicit_tmpl
= true;
19525 finish_fully_implicit_template (parser
, NULL_TREE
);
19530 Only in function declarations for constructors, destructors, type
19531 conversions, and deduction guides can the decl-specifier-seq be omitted.
19533 We explicitly postpone this check past the point where we handle
19534 function-definitions because we tolerate function-definitions
19535 that are missing their return types in some modes. */
19536 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19538 cp_parser_error (parser
,
19539 "expected constructor, destructor, or type conversion");
19540 return error_mark_node
;
19543 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19544 if (token
->type
== CPP_EQ
19545 || token
->type
== CPP_OPEN_PAREN
19546 || token
->type
== CPP_OPEN_BRACE
)
19548 is_initialized
= SD_INITIALIZED
;
19549 initialization_kind
= token
->type
;
19550 if (maybe_range_for_decl
)
19551 *maybe_range_for_decl
= error_mark_node
;
19552 tmp_init_loc
= token
->location
;
19553 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19554 *init_loc
= tmp_init_loc
;
19556 if (token
->type
== CPP_EQ
19557 && function_declarator_p (declarator
))
19559 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19560 if (t2
->keyword
== RID_DEFAULT
)
19561 is_initialized
= SD_DEFAULTED
;
19562 else if (t2
->keyword
== RID_DELETE
)
19563 is_initialized
= SD_DELETED
;
19568 /* If the init-declarator isn't initialized and isn't followed by a
19569 `,' or `;', it's not a valid init-declarator. */
19570 if (token
->type
!= CPP_COMMA
19571 && token
->type
!= CPP_SEMICOLON
)
19573 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19574 range_for_decl_p
= true;
19577 if (!maybe_range_for_decl
)
19578 cp_parser_error (parser
, "expected initializer");
19579 return error_mark_node
;
19582 is_initialized
= SD_UNINITIALIZED
;
19583 initialization_kind
= CPP_EOF
;
19586 /* Because start_decl has side-effects, we should only call it if we
19587 know we're going ahead. By this point, we know that we cannot
19588 possibly be looking at any other construct. */
19589 cp_parser_commit_to_tentative_parse (parser
);
19591 /* Enter the newly declared entry in the symbol table. If we're
19592 processing a declaration in a class-specifier, we wait until
19593 after processing the initializer. */
19596 if (parser
->in_unbraced_linkage_specification_p
)
19597 decl_specifiers
->storage_class
= sc_extern
;
19598 decl
= start_decl (declarator
, decl_specifiers
,
19599 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19600 attributes
, prefix_attributes
, &pushed_scope
);
19601 cp_finalize_omp_declare_simd (parser
, decl
);
19602 cp_finalize_oacc_routine (parser
, decl
, false);
19603 /* Adjust location of decl if declarator->id_loc is more appropriate:
19604 set, and decl wasn't merged with another decl, in which case its
19605 location would be different from input_location, and more accurate. */
19607 && declarator
->id_loc
!= UNKNOWN_LOCATION
19608 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19609 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19612 /* Enter the SCOPE. That way unqualified names appearing in the
19613 initializer will be looked up in SCOPE. */
19614 pushed_scope
= push_scope (scope
);
19616 /* Perform deferred access control checks, now that we know in which
19617 SCOPE the declared entity resides. */
19618 if (!member_p
&& decl
)
19620 tree saved_current_function_decl
= NULL_TREE
;
19622 /* If the entity being declared is a function, pretend that we
19623 are in its scope. If it is a `friend', it may have access to
19624 things that would not otherwise be accessible. */
19625 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19627 saved_current_function_decl
= current_function_decl
;
19628 current_function_decl
= decl
;
19631 /* Perform access checks for template parameters. */
19632 cp_parser_perform_template_parameter_access_checks (checks
);
19634 /* Perform the access control checks for the declarator and the
19635 decl-specifiers. */
19636 perform_deferred_access_checks (tf_warning_or_error
);
19638 /* Restore the saved value. */
19639 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19640 current_function_decl
= saved_current_function_decl
;
19643 /* Parse the initializer. */
19644 initializer
= NULL_TREE
;
19645 is_direct_init
= false;
19646 is_non_constant_init
= true;
19647 if (is_initialized
)
19649 if (function_declarator_p (declarator
))
19651 if (initialization_kind
== CPP_EQ
)
19652 initializer
= cp_parser_pure_specifier (parser
);
19655 /* If the declaration was erroneous, we don't really
19656 know what the user intended, so just silently
19657 consume the initializer. */
19658 if (decl
!= error_mark_node
)
19659 error_at (tmp_init_loc
, "initializer provided for function");
19660 cp_parser_skip_to_closing_parenthesis (parser
,
19661 /*recovering=*/true,
19662 /*or_comma=*/false,
19663 /*consume_paren=*/true);
19668 /* We want to record the extra mangling scope for in-class
19669 initializers of class members and initializers of static data
19670 member templates. The former involves deferring
19671 parsing of the initializer until end of class as with default
19672 arguments. So right here we only handle the latter. */
19673 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19674 start_lambda_scope (decl
);
19675 initializer
= cp_parser_initializer (parser
,
19677 &is_non_constant_init
);
19678 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19679 finish_lambda_scope ();
19680 if (initializer
== error_mark_node
)
19681 cp_parser_skip_to_end_of_statement (parser
);
19685 /* The old parser allows attributes to appear after a parenthesized
19686 initializer. Mark Mitchell proposed removing this functionality
19687 on the GCC mailing lists on 2002-08-13. This parser accepts the
19688 attributes -- but ignores them. Made a permerror in GCC 8. */
19689 if (cp_parser_allow_gnu_extensions_p (parser
)
19690 && initialization_kind
== CPP_OPEN_PAREN
19691 && cp_parser_attributes_opt (parser
)
19692 && permerror (input_location
,
19693 "attributes after parenthesized initializer ignored"))
19696 if (flag_permissive
&& !hint
)
19699 inform (input_location
,
19700 "this flexibility is deprecated and will be removed");
19704 /* And now complain about a non-function implicit template. */
19705 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19706 error_at (DECL_SOURCE_LOCATION (decl
),
19707 "non-function %qD declared as implicit template", decl
);
19709 /* For an in-class declaration, use `grokfield' to create the
19715 pop_scope (pushed_scope
);
19716 pushed_scope
= NULL_TREE
;
19718 decl
= grokfield (declarator
, decl_specifiers
,
19719 initializer
, !is_non_constant_init
,
19720 /*asmspec=*/NULL_TREE
,
19721 attr_chainon (attributes
, prefix_attributes
));
19722 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19723 cp_parser_save_default_args (parser
, decl
);
19724 cp_finalize_omp_declare_simd (parser
, decl
);
19725 cp_finalize_oacc_routine (parser
, decl
, false);
19728 /* Finish processing the declaration. But, skip member
19730 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19732 cp_finish_decl (decl
,
19733 initializer
, !is_non_constant_init
,
19735 /* If the initializer is in parentheses, then this is
19736 a direct-initialization, which means that an
19737 `explicit' constructor is OK. Otherwise, an
19738 `explicit' constructor cannot be used. */
19739 ((is_direct_init
|| !is_initialized
)
19740 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19742 else if ((cxx_dialect
!= cxx98
) && friend_p
19743 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19744 /* Core issue #226 (C++0x only): A default template-argument
19745 shall not be specified in a friend class template
19747 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19748 /*is_partial=*/false, /*is_friend_decl=*/1);
19750 if (!friend_p
&& pushed_scope
)
19751 pop_scope (pushed_scope
);
19753 if (function_declarator_p (declarator
)
19754 && parser
->fully_implicit_function_template_p
)
19757 decl
= finish_fully_implicit_template (parser
, decl
);
19759 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19762 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19763 && type_uses_auto (decl_specifiers
->type
))
19764 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19769 /* Parse a declarator.
19773 ptr-operator declarator
19775 abstract-declarator:
19776 ptr-operator abstract-declarator [opt]
19777 direct-abstract-declarator
19782 attributes [opt] direct-declarator
19783 attributes [opt] ptr-operator declarator
19785 abstract-declarator:
19786 attributes [opt] ptr-operator abstract-declarator [opt]
19787 attributes [opt] direct-abstract-declarator
19789 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19790 detect constructors, destructors, deduction guides, or conversion operators.
19791 It is set to -1 if the declarator is a name, and +1 if it is a
19792 function. Otherwise it is set to zero. Usually you just want to
19793 test for >0, but internally the negative value is used.
19795 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19796 a decl-specifier-seq unless it declares a constructor, destructor,
19797 or conversion. It might seem that we could check this condition in
19798 semantic analysis, rather than parsing, but that makes it difficult
19799 to handle something like `f()'. We want to notice that there are
19800 no decl-specifiers, and therefore realize that this is an
19801 expression, not a declaration.)
19803 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19804 the declarator is a direct-declarator of the form "(...)".
19806 MEMBER_P is true iff this declarator is a member-declarator.
19808 FRIEND_P is true iff this declarator is a friend. */
19810 static cp_declarator
*
19811 cp_parser_declarator (cp_parser
* parser
,
19812 cp_parser_declarator_kind dcl_kind
,
19813 int* ctor_dtor_or_conv_p
,
19814 bool* parenthesized_p
,
19815 bool member_p
, bool friend_p
)
19817 cp_declarator
*declarator
;
19818 enum tree_code code
;
19819 cp_cv_quals cv_quals
;
19821 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19823 /* Assume this is not a constructor, destructor, or type-conversion
19825 if (ctor_dtor_or_conv_p
)
19826 *ctor_dtor_or_conv_p
= 0;
19828 if (cp_parser_allow_gnu_extensions_p (parser
))
19829 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19831 /* Check for the ptr-operator production. */
19832 cp_parser_parse_tentatively (parser
);
19833 /* Parse the ptr-operator. */
19834 code
= cp_parser_ptr_operator (parser
,
19839 /* If that worked, then we have a ptr-operator. */
19840 if (cp_parser_parse_definitely (parser
))
19842 /* If a ptr-operator was found, then this declarator was not
19844 if (parenthesized_p
)
19845 *parenthesized_p
= true;
19846 /* The dependent declarator is optional if we are parsing an
19847 abstract-declarator. */
19848 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19849 cp_parser_parse_tentatively (parser
);
19851 /* Parse the dependent declarator. */
19852 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19853 /*ctor_dtor_or_conv_p=*/NULL
,
19854 /*parenthesized_p=*/NULL
,
19855 /*member_p=*/false,
19858 /* If we are parsing an abstract-declarator, we must handle the
19859 case where the dependent declarator is absent. */
19860 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19861 && !cp_parser_parse_definitely (parser
))
19864 declarator
= cp_parser_make_indirect_declarator
19865 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19867 /* Everything else is a direct-declarator. */
19870 if (parenthesized_p
)
19871 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19873 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19874 ctor_dtor_or_conv_p
,
19875 member_p
, friend_p
);
19878 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19879 declarator
->attributes
= gnu_attributes
;
19883 /* Parse a direct-declarator or direct-abstract-declarator.
19887 direct-declarator ( parameter-declaration-clause )
19888 cv-qualifier-seq [opt]
19889 ref-qualifier [opt]
19890 exception-specification [opt]
19891 direct-declarator [ constant-expression [opt] ]
19894 direct-abstract-declarator:
19895 direct-abstract-declarator [opt]
19896 ( parameter-declaration-clause )
19897 cv-qualifier-seq [opt]
19898 ref-qualifier [opt]
19899 exception-specification [opt]
19900 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19901 ( abstract-declarator )
19903 Returns a representation of the declarator. DCL_KIND is
19904 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19905 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19906 we are parsing a direct-declarator. It is
19907 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19908 of ambiguity we prefer an abstract declarator, as per
19909 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19910 as for cp_parser_declarator. */
19912 static cp_declarator
*
19913 cp_parser_direct_declarator (cp_parser
* parser
,
19914 cp_parser_declarator_kind dcl_kind
,
19915 int* ctor_dtor_or_conv_p
,
19916 bool member_p
, bool friend_p
)
19919 cp_declarator
*declarator
= NULL
;
19920 tree scope
= NULL_TREE
;
19921 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19922 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19924 tree pushed_scope
= NULL_TREE
;
19925 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
19929 /* Peek at the next token. */
19930 token
= cp_lexer_peek_token (parser
->lexer
);
19931 if (token
->type
== CPP_OPEN_PAREN
)
19933 /* This is either a parameter-declaration-clause, or a
19934 parenthesized declarator. When we know we are parsing a
19935 named declarator, it must be a parenthesized declarator
19936 if FIRST is true. For instance, `(int)' is a
19937 parameter-declaration-clause, with an omitted
19938 direct-abstract-declarator. But `((*))', is a
19939 parenthesized abstract declarator. Finally, when T is a
19940 template parameter `(T)' is a
19941 parameter-declaration-clause, and not a parenthesized
19944 We first try and parse a parameter-declaration-clause,
19945 and then try a nested declarator (if FIRST is true).
19947 It is not an error for it not to be a
19948 parameter-declaration-clause, even when FIRST is
19954 The first is the declaration of a function while the
19955 second is the definition of a variable, including its
19958 Having seen only the parenthesis, we cannot know which of
19959 these two alternatives should be selected. Even more
19960 complex are examples like:
19965 The former is a function-declaration; the latter is a
19966 variable initialization.
19968 Thus again, we try a parameter-declaration-clause, and if
19969 that fails, we back out and return. */
19971 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19974 bool is_declarator
= false;
19978 /* In a member-declarator, the only valid interpretation
19979 of a parenthesis is the start of a
19980 parameter-declaration-clause. (It is invalid to
19981 initialize a static data member with a parenthesized
19982 initializer; only the "=" form of initialization is
19985 cp_parser_parse_tentatively (parser
);
19987 /* Consume the `('. */
19988 matching_parens parens
;
19989 parens
.consume_open (parser
);
19992 /* If this is going to be an abstract declarator, we're
19993 in a declarator and we can't have default args. */
19994 parser
->default_arg_ok_p
= false;
19995 parser
->in_declarator_p
= true;
19998 begin_scope (sk_function_parms
, NULL_TREE
);
20000 /* Parse the parameter-declaration-clause. */
20001 params
= cp_parser_parameter_declaration_clause (parser
);
20003 /* Consume the `)'. */
20004 parens
.require_close (parser
);
20006 /* If all went well, parse the cv-qualifier-seq,
20007 ref-qualifier and the exception-specification. */
20008 if (member_p
|| cp_parser_parse_definitely (parser
))
20010 cp_cv_quals cv_quals
;
20011 cp_virt_specifiers virt_specifiers
;
20012 cp_ref_qualifier ref_qual
;
20013 tree exception_specification
;
20016 bool memfn
= (member_p
|| (pushed_scope
20017 && CLASS_TYPE_P (pushed_scope
)));
20019 is_declarator
= true;
20021 if (ctor_dtor_or_conv_p
)
20022 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20025 /* Parse the cv-qualifier-seq. */
20026 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20027 /* Parse the ref-qualifier. */
20028 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20029 /* Parse the tx-qualifier. */
20030 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20031 /* And the exception-specification. */
20032 exception_specification
20033 = cp_parser_exception_specification_opt (parser
);
20035 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20037 /* In here, we handle cases where attribute is used after
20038 the function declaration. For example:
20039 void func (int x) __attribute__((vector(..))); */
20040 tree gnu_attrs
= NULL_TREE
;
20041 tree requires_clause
= NULL_TREE
;
20042 late_return
= (cp_parser_late_return_type_opt
20043 (parser
, declarator
, requires_clause
,
20044 memfn
? cv_quals
: -1));
20046 /* Parse the virt-specifier-seq. */
20047 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20049 /* Create the function-declarator. */
20050 declarator
= make_call_declarator (declarator
,
20056 exception_specification
,
20059 declarator
->std_attributes
= attrs
;
20060 declarator
->attributes
= gnu_attrs
;
20061 /* Any subsequent parameter lists are to do with
20062 return type, so are not those of the declared
20064 parser
->default_arg_ok_p
= false;
20067 /* Remove the function parms from scope. */
20068 pop_bindings_and_leave_scope ();
20071 /* Repeat the main loop. */
20075 /* If this is the first, we can try a parenthesized
20079 bool saved_in_type_id_in_expr_p
;
20081 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20082 parser
->in_declarator_p
= saved_in_declarator_p
;
20084 open_paren
= token
;
20085 /* Consume the `('. */
20086 matching_parens parens
;
20087 parens
.consume_open (parser
);
20088 /* Parse the nested declarator. */
20089 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20090 parser
->in_type_id_in_expr_p
= true;
20092 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20093 /*parenthesized_p=*/NULL
,
20094 member_p
, friend_p
);
20095 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20097 /* Expect a `)'. */
20098 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20099 if (!parens
.require_close (parser
))
20100 declarator
= cp_error_declarator
;
20101 if (declarator
== cp_error_declarator
)
20104 goto handle_declarator
;
20106 /* Otherwise, we must be done. */
20110 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20111 && token
->type
== CPP_OPEN_SQUARE
20112 && !cp_next_tokens_can_be_attribute_p (parser
))
20114 /* Parse an array-declarator. */
20115 tree bounds
, attrs
;
20117 if (ctor_dtor_or_conv_p
)
20118 *ctor_dtor_or_conv_p
= 0;
20122 parser
->default_arg_ok_p
= false;
20123 parser
->in_declarator_p
= true;
20124 /* Consume the `['. */
20125 cp_lexer_consume_token (parser
->lexer
);
20126 /* Peek at the next token. */
20127 token
= cp_lexer_peek_token (parser
->lexer
);
20128 /* If the next token is `]', then there is no
20129 constant-expression. */
20130 if (token
->type
!= CPP_CLOSE_SQUARE
)
20132 bool non_constant_p
;
20134 = cp_parser_constant_expression (parser
,
20135 /*allow_non_constant=*/true,
20137 if (!non_constant_p
)
20139 else if (error_operand_p (bounds
))
20140 /* Already gave an error. */;
20141 else if (!parser
->in_function_body
20142 || current_binding_level
->kind
== sk_function_parms
)
20144 /* Normally, the array bound must be an integral constant
20145 expression. However, as an extension, we allow VLAs
20146 in function scopes as long as they aren't part of a
20147 parameter declaration. */
20148 cp_parser_error (parser
,
20149 "array bound is not an integer constant");
20150 bounds
= error_mark_node
;
20152 else if (processing_template_decl
20153 && !type_dependent_expression_p (bounds
))
20155 /* Remember this wasn't a constant-expression. */
20156 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20157 TREE_SIDE_EFFECTS (bounds
) = 1;
20161 bounds
= NULL_TREE
;
20162 /* Look for the closing `]'. */
20163 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20165 declarator
= cp_error_declarator
;
20169 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20170 declarator
= make_array_declarator (declarator
, bounds
);
20171 declarator
->std_attributes
= attrs
;
20173 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20176 tree qualifying_scope
;
20177 tree unqualified_name
;
20179 special_function_kind sfk
;
20181 bool pack_expansion_p
= false;
20182 cp_token
*declarator_id_start_token
;
20184 /* Parse a declarator-id */
20185 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20188 cp_parser_parse_tentatively (parser
);
20190 /* If we see an ellipsis, we should be looking at a
20192 if (token
->type
== CPP_ELLIPSIS
)
20194 /* Consume the `...' */
20195 cp_lexer_consume_token (parser
->lexer
);
20197 pack_expansion_p
= true;
20201 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20203 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20204 qualifying_scope
= parser
->scope
;
20209 if (!unqualified_name
&& pack_expansion_p
)
20211 /* Check whether an error occurred. */
20212 okay
= !cp_parser_error_occurred (parser
);
20214 /* We already consumed the ellipsis to mark a
20215 parameter pack, but we have no way to report it,
20216 so abort the tentative parse. We will be exiting
20217 immediately anyway. */
20218 cp_parser_abort_tentative_parse (parser
);
20221 okay
= cp_parser_parse_definitely (parser
);
20224 unqualified_name
= error_mark_node
;
20225 else if (unqualified_name
20226 && (qualifying_scope
20227 || (!identifier_p (unqualified_name
))))
20229 cp_parser_error (parser
, "expected unqualified-id");
20230 unqualified_name
= error_mark_node
;
20234 if (!unqualified_name
)
20236 if (unqualified_name
== error_mark_node
)
20238 declarator
= cp_error_declarator
;
20239 pack_expansion_p
= false;
20240 declarator
->parameter_pack_p
= false;
20244 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20246 if (qualifying_scope
&& at_namespace_scope_p ()
20247 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20249 /* In the declaration of a member of a template class
20250 outside of the class itself, the SCOPE will sometimes
20251 be a TYPENAME_TYPE. For example, given:
20253 template <typename T>
20254 int S<T>::R::i = 3;
20256 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20257 this context, we must resolve S<T>::R to an ordinary
20258 type, rather than a typename type.
20260 The reason we normally avoid resolving TYPENAME_TYPEs
20261 is that a specialization of `S' might render
20262 `S<T>::R' not a type. However, if `S' is
20263 specialized, then this `i' will not be used, so there
20264 is no harm in resolving the types here. */
20267 /* Resolve the TYPENAME_TYPE. */
20268 type
= resolve_typename_type (qualifying_scope
,
20269 /*only_current_p=*/false);
20270 /* If that failed, the declarator is invalid. */
20271 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20273 if (typedef_variant_p (type
))
20274 error_at (declarator_id_start_token
->location
,
20275 "cannot define member of dependent typedef "
20278 error_at (declarator_id_start_token
->location
,
20279 "%<%T::%E%> is not a type",
20280 TYPE_CONTEXT (qualifying_scope
),
20281 TYPE_IDENTIFIER (qualifying_scope
));
20283 qualifying_scope
= type
;
20288 if (unqualified_name
)
20292 if (qualifying_scope
20293 && CLASS_TYPE_P (qualifying_scope
))
20294 class_type
= qualifying_scope
;
20296 class_type
= current_class_type
;
20298 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20300 tree name_type
= TREE_TYPE (unqualified_name
);
20302 if (!class_type
|| !same_type_p (name_type
, class_type
))
20304 /* We do not attempt to print the declarator
20305 here because we do not have enough
20306 information about its original syntactic
20308 cp_parser_error (parser
, "invalid declarator");
20309 declarator
= cp_error_declarator
;
20312 else if (qualifying_scope
20313 && CLASSTYPE_USE_TEMPLATE (name_type
))
20315 error_at (declarator_id_start_token
->location
,
20316 "invalid use of constructor as a template");
20317 inform (declarator_id_start_token
->location
,
20318 "use %<%T::%D%> instead of %<%T::%D%> to "
20319 "name the constructor in a qualified name",
20321 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20322 class_type
, name_type
);
20323 declarator
= cp_error_declarator
;
20326 unqualified_name
= constructor_name (class_type
);
20331 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20332 sfk
= sfk_destructor
;
20333 else if (identifier_p (unqualified_name
)
20334 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20335 sfk
= sfk_conversion
;
20336 else if (/* There's no way to declare a constructor
20337 for an unnamed type, even if the type
20338 got a name for linkage purposes. */
20339 !TYPE_WAS_UNNAMED (class_type
)
20340 /* Handle correctly (c++/19200):
20354 friend void N::S();
20356 && (!friend_p
|| class_type
== qualifying_scope
)
20357 && constructor_name_p (unqualified_name
,
20359 sfk
= sfk_constructor
;
20360 else if (is_overloaded_fn (unqualified_name
)
20361 && DECL_CONSTRUCTOR_P (get_first_fn
20362 (unqualified_name
)))
20363 sfk
= sfk_constructor
;
20365 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20366 *ctor_dtor_or_conv_p
= -1;
20369 declarator
= make_id_declarator (qualifying_scope
,
20372 declarator
->std_attributes
= attrs
;
20373 declarator
->id_loc
= token
->location
;
20374 declarator
->parameter_pack_p
= pack_expansion_p
;
20376 if (pack_expansion_p
)
20377 maybe_warn_variadic_templates ();
20380 handle_declarator
:;
20381 scope
= get_scope_of_declarator (declarator
);
20384 /* Any names that appear after the declarator-id for a
20385 member are looked up in the containing scope. */
20386 if (at_function_scope_p ())
20388 /* But declarations with qualified-ids can't appear in a
20390 cp_parser_error (parser
, "qualified-id in declaration");
20391 declarator
= cp_error_declarator
;
20394 pushed_scope
= push_scope (scope
);
20396 parser
->in_declarator_p
= true;
20397 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20398 || (declarator
&& declarator
->kind
== cdk_id
))
20399 /* Default args are only allowed on function
20401 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20403 parser
->default_arg_ok_p
= false;
20412 /* For an abstract declarator, we might wind up with nothing at this
20413 point. That's an error; the declarator is not optional. */
20415 cp_parser_error (parser
, "expected declarator");
20416 else if (open_paren
)
20418 /* Record overly parenthesized declarator so we can give a
20419 diagnostic about confusing decl/expr disambiguation. */
20420 if (declarator
->kind
== cdk_array
)
20422 /* If the open and close parens are on different lines, this
20423 is probably a formatting thing, so ignore. */
20424 expanded_location open
= expand_location (open_paren
->location
);
20425 expanded_location close
= expand_location (close_paren
->location
);
20426 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20430 declarator
->parenthesized
= open_paren
->location
;
20433 /* If we entered a scope, we must exit it now. */
20435 pop_scope (pushed_scope
);
20437 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20438 parser
->in_declarator_p
= saved_in_declarator_p
;
20443 /* Parse a ptr-operator.
20446 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20447 * cv-qualifier-seq [opt]
20449 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20450 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20455 & cv-qualifier-seq [opt]
20457 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20458 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20459 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20460 filled in with the TYPE containing the member. *CV_QUALS is
20461 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20462 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20463 Note that the tree codes returned by this function have nothing
20464 to do with the types of trees that will be eventually be created
20465 to represent the pointer or reference type being parsed. They are
20466 just constants with suggestive names. */
20467 static enum tree_code
20468 cp_parser_ptr_operator (cp_parser
* parser
,
20470 cp_cv_quals
*cv_quals
,
20473 enum tree_code code
= ERROR_MARK
;
20475 tree attrs
= NULL_TREE
;
20477 /* Assume that it's not a pointer-to-member. */
20479 /* And that there are no cv-qualifiers. */
20480 *cv_quals
= TYPE_UNQUALIFIED
;
20482 /* Peek at the next token. */
20483 token
= cp_lexer_peek_token (parser
->lexer
);
20485 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20486 if (token
->type
== CPP_MULT
)
20487 code
= INDIRECT_REF
;
20488 else if (token
->type
== CPP_AND
)
20490 else if ((cxx_dialect
!= cxx98
) &&
20491 token
->type
== CPP_AND_AND
) /* C++0x only */
20492 code
= NON_LVALUE_EXPR
;
20494 if (code
!= ERROR_MARK
)
20496 /* Consume the `*', `&' or `&&'. */
20497 cp_lexer_consume_token (parser
->lexer
);
20499 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20500 `&', if we are allowing GNU extensions. (The only qualifier
20501 that can legally appear after `&' is `restrict', but that is
20502 enforced during semantic analysis. */
20503 if (code
== INDIRECT_REF
20504 || cp_parser_allow_gnu_extensions_p (parser
))
20505 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20507 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20508 if (attributes
!= NULL
)
20509 *attributes
= attrs
;
20513 /* Try the pointer-to-member case. */
20514 cp_parser_parse_tentatively (parser
);
20515 /* Look for the optional `::' operator. */
20516 cp_parser_global_scope_opt (parser
,
20517 /*current_scope_valid_p=*/false);
20518 /* Look for the nested-name specifier. */
20519 token
= cp_lexer_peek_token (parser
->lexer
);
20520 cp_parser_nested_name_specifier (parser
,
20521 /*typename_keyword_p=*/false,
20522 /*check_dependency_p=*/true,
20524 /*is_declaration=*/false);
20525 /* If we found it, and the next token is a `*', then we are
20526 indeed looking at a pointer-to-member operator. */
20527 if (!cp_parser_error_occurred (parser
)
20528 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20530 /* Indicate that the `*' operator was used. */
20531 code
= INDIRECT_REF
;
20533 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20534 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20535 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20536 error_at (token
->location
, "cannot form pointer to member of "
20537 "non-class %q#T", parser
->scope
);
20540 /* The type of which the member is a member is given by the
20542 *type
= parser
->scope
;
20543 /* The next name will not be qualified. */
20544 parser
->scope
= NULL_TREE
;
20545 parser
->qualifying_scope
= NULL_TREE
;
20546 parser
->object_scope
= NULL_TREE
;
20547 /* Look for optional c++11 attributes. */
20548 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20549 if (attributes
!= NULL
)
20550 *attributes
= attrs
;
20551 /* Look for the optional cv-qualifier-seq. */
20552 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20555 /* If that didn't work we don't have a ptr-operator. */
20556 if (!cp_parser_parse_definitely (parser
))
20557 cp_parser_error (parser
, "expected ptr-operator");
20563 /* Parse an (optional) cv-qualifier-seq.
20566 cv-qualifier cv-qualifier-seq [opt]
20577 Returns a bitmask representing the cv-qualifiers. */
20580 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20582 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20587 cp_cv_quals cv_qualifier
;
20589 /* Peek at the next token. */
20590 token
= cp_lexer_peek_token (parser
->lexer
);
20591 /* See if it's a cv-qualifier. */
20592 switch (token
->keyword
)
20595 cv_qualifier
= TYPE_QUAL_CONST
;
20599 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20603 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20607 cv_qualifier
= TYPE_UNQUALIFIED
;
20614 if (cv_quals
& cv_qualifier
)
20616 gcc_rich_location
richloc (token
->location
);
20617 richloc
.add_fixit_remove ();
20618 error_at (&richloc
, "duplicate cv-qualifier");
20619 cp_lexer_purge_token (parser
->lexer
);
20623 cp_lexer_consume_token (parser
->lexer
);
20624 cv_quals
|= cv_qualifier
;
20631 /* Parse an (optional) ref-qualifier
20637 Returns cp_ref_qualifier representing ref-qualifier. */
20639 static cp_ref_qualifier
20640 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20642 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20644 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20645 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20650 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20651 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20653 switch (token
->type
)
20656 curr_ref_qual
= REF_QUAL_LVALUE
;
20660 curr_ref_qual
= REF_QUAL_RVALUE
;
20664 curr_ref_qual
= REF_QUAL_NONE
;
20668 if (!curr_ref_qual
)
20672 error_at (token
->location
, "multiple ref-qualifiers");
20673 cp_lexer_purge_token (parser
->lexer
);
20677 ref_qual
= curr_ref_qual
;
20678 cp_lexer_consume_token (parser
->lexer
);
20685 /* Parse an optional tx-qualifier.
20689 transaction_safe_dynamic */
20692 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20694 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20695 if (token
->type
== CPP_NAME
)
20697 tree name
= token
->u
.value
;
20698 const char *p
= IDENTIFIER_POINTER (name
);
20699 const int len
= strlen ("transaction_safe");
20700 if (!strncmp (p
, "transaction_safe", len
))
20704 || !strcmp (p
, "_dynamic"))
20706 cp_lexer_consume_token (parser
->lexer
);
20709 error ("%qE requires %<-fgnu-tm%>", name
);
20720 /* Parse an (optional) virt-specifier-seq.
20722 virt-specifier-seq:
20723 virt-specifier virt-specifier-seq [opt]
20729 Returns a bitmask representing the virt-specifiers. */
20731 static cp_virt_specifiers
20732 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20734 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20739 cp_virt_specifiers virt_specifier
;
20741 /* Peek at the next token. */
20742 token
= cp_lexer_peek_token (parser
->lexer
);
20743 /* See if it's a virt-specifier-qualifier. */
20744 if (token
->type
!= CPP_NAME
)
20746 if (id_equal (token
->u
.value
, "override"))
20748 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20749 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20751 else if (id_equal (token
->u
.value
, "final"))
20753 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20754 virt_specifier
= VIRT_SPEC_FINAL
;
20756 else if (id_equal (token
->u
.value
, "__final"))
20758 virt_specifier
= VIRT_SPEC_FINAL
;
20763 if (virt_specifiers
& virt_specifier
)
20765 gcc_rich_location
richloc (token
->location
);
20766 richloc
.add_fixit_remove ();
20767 error_at (&richloc
, "duplicate virt-specifier");
20768 cp_lexer_purge_token (parser
->lexer
);
20772 cp_lexer_consume_token (parser
->lexer
);
20773 virt_specifiers
|= virt_specifier
;
20776 return virt_specifiers
;
20779 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20780 is in scope even though it isn't real. */
20783 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20787 if (current_class_ptr
)
20789 /* We don't clear this between NSDMIs. Is it already what we want? */
20790 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20791 if (DECL_P (current_class_ptr
)
20792 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20793 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20794 && cp_type_quals (type
) == quals
)
20798 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20799 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20800 current_class_ptr
= NULL_TREE
;
20802 = cp_build_fold_indirect_ref (this_parm
);
20803 current_class_ptr
= this_parm
;
20806 /* Return true iff our current scope is a non-static data member
20810 parsing_nsdmi (void)
20812 /* We recognize NSDMI context by the context-less 'this' pointer set up
20813 by the function above. */
20814 if (current_class_ptr
20815 && TREE_CODE (current_class_ptr
) == PARM_DECL
20816 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20821 /* Parse a late-specified return type, if any. This is not a separate
20822 non-terminal, but part of a function declarator, which looks like
20824 -> trailing-type-specifier-seq abstract-declarator(opt)
20826 Returns the type indicated by the type-id.
20828 In addition to this, parse any queued up #pragma omp declare simd
20829 clauses, and #pragma acc routine clauses.
20831 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20835 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20836 tree
& requires_clause
, cp_cv_quals quals
)
20839 tree type
= NULL_TREE
;
20840 bool declare_simd_p
= (parser
->omp_declare_simd
20842 && declarator
->kind
== cdk_id
);
20844 bool oacc_routine_p
= (parser
->oacc_routine
20846 && declarator
->kind
== cdk_id
);
20848 /* Peek at the next token. */
20849 token
= cp_lexer_peek_token (parser
->lexer
);
20850 /* A late-specified return type is indicated by an initial '->'. */
20851 if (token
->type
!= CPP_DEREF
20852 && token
->keyword
!= RID_REQUIRES
20853 && !(token
->type
== CPP_NAME
20854 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20855 && !(declare_simd_p
|| oacc_routine_p
))
20858 tree save_ccp
= current_class_ptr
;
20859 tree save_ccr
= current_class_ref
;
20862 /* DR 1207: 'this' is in scope in the trailing return type. */
20863 inject_this_parameter (current_class_type
, quals
);
20866 if (token
->type
== CPP_DEREF
)
20868 /* Consume the ->. */
20869 cp_lexer_consume_token (parser
->lexer
);
20871 type
= cp_parser_trailing_type_id (parser
);
20874 /* Function declarations may be followed by a trailing
20875 requires-clause. */
20876 requires_clause
= cp_parser_requires_clause_opt (parser
);
20878 if (declare_simd_p
)
20879 declarator
->attributes
20880 = cp_parser_late_parsing_omp_declare_simd (parser
,
20881 declarator
->attributes
);
20882 if (oacc_routine_p
)
20883 declarator
->attributes
20884 = cp_parser_late_parsing_oacc_routine (parser
,
20885 declarator
->attributes
);
20889 current_class_ptr
= save_ccp
;
20890 current_class_ref
= save_ccr
;
20896 /* Parse a declarator-id.
20900 :: [opt] nested-name-specifier [opt] type-name
20902 In the `id-expression' case, the value returned is as for
20903 cp_parser_id_expression if the id-expression was an unqualified-id.
20904 If the id-expression was a qualified-id, then a SCOPE_REF is
20905 returned. The first operand is the scope (either a NAMESPACE_DECL
20906 or TREE_TYPE), but the second is still just a representation of an
20910 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20913 /* The expression must be an id-expression. Assume that qualified
20914 names are the names of types so that:
20917 int S<T>::R::i = 3;
20919 will work; we must treat `S<T>::R' as the name of a type.
20920 Similarly, assume that qualified names are templates, where
20924 int S<T>::R<T>::i = 3;
20927 id
= cp_parser_id_expression (parser
,
20928 /*template_keyword_p=*/false,
20929 /*check_dependency_p=*/false,
20930 /*template_p=*/NULL
,
20931 /*declarator_p=*/true,
20933 if (id
&& BASELINK_P (id
))
20934 id
= BASELINK_FUNCTIONS (id
);
20938 /* Parse a type-id.
20941 type-specifier-seq abstract-declarator [opt]
20943 Returns the TYPE specified. */
20946 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
20947 bool is_trailing_return
)
20949 cp_decl_specifier_seq type_specifier_seq
;
20950 cp_declarator
*abstract_declarator
;
20952 /* Parse the type-specifier-seq. */
20953 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
20954 is_trailing_return
,
20955 &type_specifier_seq
);
20956 if (is_template_arg
&& type_specifier_seq
.type
20957 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
20958 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
20959 /* A bare template name as a template argument is a template template
20960 argument, not a placeholder, so fail parsing it as a type argument. */
20962 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
20963 cp_parser_simulate_error (parser
);
20964 return error_mark_node
;
20966 if (type_specifier_seq
.type
== error_mark_node
)
20967 return error_mark_node
;
20969 /* There might or might not be an abstract declarator. */
20970 cp_parser_parse_tentatively (parser
);
20971 /* Look for the declarator. */
20972 abstract_declarator
20973 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
20974 /*parenthesized_p=*/NULL
,
20975 /*member_p=*/false,
20976 /*friend_p=*/false);
20977 /* Check to see if there really was a declarator. */
20978 if (!cp_parser_parse_definitely (parser
))
20979 abstract_declarator
= NULL
;
20981 if (type_specifier_seq
.type
20982 /* The concepts TS allows 'auto' as a type-id. */
20983 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
20984 /* None of the valid uses of 'auto' in C++14 involve the type-id
20985 nonterminal, but it is valid in a trailing-return-type. */
20986 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
20987 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
20989 /* A type-id with type 'auto' is only ok if the abstract declarator
20990 is a function declarator with a late-specified return type.
20992 A type-id with 'auto' is also valid in a trailing-return-type
20993 in a compound-requirement. */
20994 if (abstract_declarator
20995 && abstract_declarator
->kind
== cdk_function
20996 && abstract_declarator
->u
.function
.late_return_type
)
20998 else if (parser
->in_result_type_constraint_p
)
21002 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21003 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21005 error_at (loc
, "missing template arguments after %qT",
21007 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21011 error_at (loc
, "invalid use of %qT", auto_node
);
21012 return error_mark_node
;
21016 return groktypename (&type_specifier_seq
, abstract_declarator
,
21021 cp_parser_type_id (cp_parser
*parser
)
21023 return cp_parser_type_id_1 (parser
, false, false);
21027 cp_parser_template_type_arg (cp_parser
*parser
)
21030 const char *saved_message
= parser
->type_definition_forbidden_message
;
21031 parser
->type_definition_forbidden_message
21032 = G_("types may not be defined in template arguments");
21033 r
= cp_parser_type_id_1 (parser
, true, false);
21034 parser
->type_definition_forbidden_message
= saved_message
;
21035 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21037 error ("invalid use of %<auto%> in template argument");
21038 r
= error_mark_node
;
21044 cp_parser_trailing_type_id (cp_parser
*parser
)
21046 return cp_parser_type_id_1 (parser
, false, true);
21049 /* Parse a type-specifier-seq.
21051 type-specifier-seq:
21052 type-specifier type-specifier-seq [opt]
21056 type-specifier-seq:
21057 attributes type-specifier-seq [opt]
21059 If IS_DECLARATION is true, we are at the start of a "condition" or
21060 exception-declaration, so we might be followed by a declarator-id.
21062 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21063 i.e. we've just seen "->".
21065 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21068 cp_parser_type_specifier_seq (cp_parser
* parser
,
21069 bool is_declaration
,
21070 bool is_trailing_return
,
21071 cp_decl_specifier_seq
*type_specifier_seq
)
21073 bool seen_type_specifier
= false;
21074 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21075 cp_token
*start_token
= NULL
;
21077 /* Clear the TYPE_SPECIFIER_SEQ. */
21078 clear_decl_specs (type_specifier_seq
);
21080 /* In the context of a trailing return type, enum E { } is an
21081 elaborated-type-specifier followed by a function-body, not an
21083 if (is_trailing_return
)
21084 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21086 /* Parse the type-specifiers and attributes. */
21089 tree type_specifier
;
21090 bool is_cv_qualifier
;
21092 /* Check for attributes first. */
21093 if (cp_next_tokens_can_be_attribute_p (parser
))
21095 type_specifier_seq
->attributes
21096 = attr_chainon (type_specifier_seq
->attributes
,
21097 cp_parser_attributes_opt (parser
));
21101 /* record the token of the beginning of the type specifier seq,
21102 for error reporting purposes*/
21104 start_token
= cp_lexer_peek_token (parser
->lexer
);
21106 /* Look for the type-specifier. */
21107 type_specifier
= cp_parser_type_specifier (parser
,
21109 type_specifier_seq
,
21110 /*is_declaration=*/false,
21113 if (!type_specifier
)
21115 /* If the first type-specifier could not be found, this is not a
21116 type-specifier-seq at all. */
21117 if (!seen_type_specifier
)
21119 /* Set in_declarator_p to avoid skipping to the semicolon. */
21120 int in_decl
= parser
->in_declarator_p
;
21121 parser
->in_declarator_p
= true;
21123 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21124 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21125 cp_parser_error (parser
, "expected type-specifier");
21127 parser
->in_declarator_p
= in_decl
;
21129 type_specifier_seq
->type
= error_mark_node
;
21132 /* If subsequent type-specifiers could not be found, the
21133 type-specifier-seq is complete. */
21137 seen_type_specifier
= true;
21138 /* The standard says that a condition can be:
21140 type-specifier-seq declarator = assignment-expression
21147 we should treat the "S" as a declarator, not as a
21148 type-specifier. The standard doesn't say that explicitly for
21149 type-specifier-seq, but it does say that for
21150 decl-specifier-seq in an ordinary declaration. Perhaps it
21151 would be clearer just to allow a decl-specifier-seq here, and
21152 then add a semantic restriction that if any decl-specifiers
21153 that are not type-specifiers appear, the program is invalid. */
21154 if (is_declaration
&& !is_cv_qualifier
)
21155 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21159 /* Return whether the function currently being declared has an associated
21160 template parameter list. */
21163 function_being_declared_is_template_p (cp_parser
* parser
)
21165 if (!current_template_parms
|| processing_template_parmlist
)
21168 if (parser
->implicit_template_scope
)
21171 if (at_class_scope_p ()
21172 && TYPE_BEING_DEFINED (current_class_type
))
21173 return parser
->num_template_parameter_lists
!= 0;
21175 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21176 (current_class_type
));
21179 /* Parse a parameter-declaration-clause.
21181 parameter-declaration-clause:
21182 parameter-declaration-list [opt] ... [opt]
21183 parameter-declaration-list , ...
21185 Returns a representation for the parameter declarations. A return
21186 value of NULL indicates a parameter-declaration-clause consisting
21187 only of an ellipsis. */
21190 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21199 int auto_is_implicit_function_template_parm_p
;
21201 parser
->auto_is_implicit_function_template_parm_p
21202 = auto_is_implicit_function_template_parm_p
;
21204 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
21208 if (!processing_specialization
21209 && !processing_template_parmlist
21210 && !processing_explicit_instantiation
21211 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21212 actual function or a random abstract declarator. */
21213 && parser
->default_arg_ok_p
)
21214 if (!current_function_decl
21215 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21216 parser
->auto_is_implicit_function_template_parm_p
= true;
21218 /* Peek at the next token. */
21219 token
= cp_lexer_peek_token (parser
->lexer
);
21220 /* Check for trivial parameter-declaration-clauses. */
21221 if (token
->type
== CPP_ELLIPSIS
)
21223 /* Consume the `...' token. */
21224 cp_lexer_consume_token (parser
->lexer
);
21227 else if (token
->type
== CPP_CLOSE_PAREN
)
21228 /* There are no parameters. */
21230 #ifndef NO_IMPLICIT_EXTERN_C
21231 if (in_system_header_at (input_location
)
21232 && current_class_type
== NULL
21233 && current_lang_name
== lang_name_c
)
21237 return void_list_node
;
21239 /* Check for `(void)', too, which is a special case. */
21240 else if (token
->keyword
== RID_VOID
21241 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21242 == CPP_CLOSE_PAREN
))
21244 /* Consume the `void' token. */
21245 cp_lexer_consume_token (parser
->lexer
);
21246 /* There are no parameters. */
21247 return void_list_node
;
21250 /* Parse the parameter-declaration-list. */
21251 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
21252 /* If a parse error occurred while parsing the
21253 parameter-declaration-list, then the entire
21254 parameter-declaration-clause is erroneous. */
21258 /* Peek at the next token. */
21259 token
= cp_lexer_peek_token (parser
->lexer
);
21260 /* If it's a `,', the clause should terminate with an ellipsis. */
21261 if (token
->type
== CPP_COMMA
)
21263 /* Consume the `,'. */
21264 cp_lexer_consume_token (parser
->lexer
);
21265 /* Expect an ellipsis. */
21267 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21269 /* It might also be `...' if the optional trailing `,' was
21271 else if (token
->type
== CPP_ELLIPSIS
)
21273 /* Consume the `...' token. */
21274 cp_lexer_consume_token (parser
->lexer
);
21275 /* And remember that we saw it. */
21279 ellipsis_p
= false;
21281 /* Finish the parameter list. */
21283 parameters
= chainon (parameters
, void_list_node
);
21288 /* Parse a parameter-declaration-list.
21290 parameter-declaration-list:
21291 parameter-declaration
21292 parameter-declaration-list , parameter-declaration
21294 Returns a representation of the parameter-declaration-list, as for
21295 cp_parser_parameter_declaration_clause. However, the
21296 `void_list_node' is never appended to the list. Upon return,
21297 *IS_ERROR will be true iff an error occurred. */
21300 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
21302 tree parameters
= NULL_TREE
;
21303 tree
*tail
= ¶meters
;
21304 bool saved_in_unbraced_linkage_specification_p
;
21307 /* Assume all will go well. */
21309 /* The special considerations that apply to a function within an
21310 unbraced linkage specifications do not apply to the parameters
21311 to the function. */
21312 saved_in_unbraced_linkage_specification_p
21313 = parser
->in_unbraced_linkage_specification_p
;
21314 parser
->in_unbraced_linkage_specification_p
= false;
21316 /* Look for more parameters. */
21319 cp_parameter_declarator
*parameter
;
21320 tree decl
= error_mark_node
;
21321 bool parenthesized_p
= false;
21322 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
21323 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21324 (current_template_parms
)) : 0);
21326 /* Parse the parameter. */
21328 = cp_parser_parameter_declaration (parser
,
21329 /*template_parm_p=*/false,
21332 /* We don't know yet if the enclosing context is deprecated, so wait
21333 and warn in grokparms if appropriate. */
21334 deprecated_state
= DEPRECATED_SUPPRESS
;
21338 /* If a function parameter pack was specified and an implicit template
21339 parameter was introduced during cp_parser_parameter_declaration,
21340 change any implicit parameters introduced into packs. */
21341 if (parser
->implicit_template_parms
21342 && parameter
->declarator
21343 && parameter
->declarator
->parameter_pack_p
)
21345 int latest_template_parm_idx
= TREE_VEC_LENGTH
21346 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21348 if (latest_template_parm_idx
!= template_parm_idx
)
21349 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
21350 (parameter
->decl_specifiers
.type
,
21351 template_parm_idx
, latest_template_parm_idx
);
21354 decl
= grokdeclarator (parameter
->declarator
,
21355 ¶meter
->decl_specifiers
,
21357 parameter
->default_argument
!= NULL_TREE
,
21358 ¶meter
->decl_specifiers
.attributes
);
21359 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21360 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21363 deprecated_state
= DEPRECATED_NORMAL
;
21365 /* If a parse error occurred parsing the parameter declaration,
21366 then the entire parameter-declaration-list is erroneous. */
21367 if (decl
== error_mark_node
)
21370 parameters
= error_mark_node
;
21374 if (parameter
->decl_specifiers
.attributes
)
21375 cplus_decl_attributes (&decl
,
21376 parameter
->decl_specifiers
.attributes
,
21378 if (DECL_NAME (decl
))
21379 decl
= pushdecl (decl
);
21381 if (decl
!= error_mark_node
)
21383 retrofit_lang_decl (decl
);
21384 DECL_PARM_INDEX (decl
) = ++index
;
21385 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21388 /* Add the new parameter to the list. */
21389 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21390 tail
= &TREE_CHAIN (*tail
);
21392 /* Peek at the next token. */
21393 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21394 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21395 /* These are for Objective-C++ */
21396 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21397 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21398 /* The parameter-declaration-list is complete. */
21400 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21404 /* Peek at the next token. */
21405 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21406 /* If it's an ellipsis, then the list is complete. */
21407 if (token
->type
== CPP_ELLIPSIS
)
21409 /* Otherwise, there must be more parameters. Consume the
21411 cp_lexer_consume_token (parser
->lexer
);
21412 /* When parsing something like:
21414 int i(float f, double d)
21416 we can tell after seeing the declaration for "f" that we
21417 are not looking at an initialization of a variable "i",
21418 but rather at the declaration of a function "i".
21420 Due to the fact that the parsing of template arguments
21421 (as specified to a template-id) requires backtracking we
21422 cannot use this technique when inside a template argument
21424 if (!parser
->in_template_argument_list_p
21425 && !parser
->in_type_id_in_expr_p
21426 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21427 /* However, a parameter-declaration of the form
21428 "float(f)" (which is a valid declaration of a
21429 parameter "f") can also be interpreted as an
21430 expression (the conversion of "f" to "float"). */
21431 && !parenthesized_p
)
21432 cp_parser_commit_to_tentative_parse (parser
);
21436 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21437 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21438 cp_parser_skip_to_closing_parenthesis (parser
,
21439 /*recovering=*/true,
21440 /*or_comma=*/false,
21441 /*consume_paren=*/false);
21446 parser
->in_unbraced_linkage_specification_p
21447 = saved_in_unbraced_linkage_specification_p
;
21449 /* Reset implicit_template_scope if we are about to leave the function
21450 parameter list that introduced it. Note that for out-of-line member
21451 definitions, there will be one or more class scopes before we get to
21452 the template parameter scope. */
21454 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21455 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21457 while (maybe_its
->kind
== sk_class
)
21458 maybe_its
= maybe_its
->level_chain
;
21459 if (maybe_its
== its
)
21461 parser
->implicit_template_parms
= 0;
21462 parser
->implicit_template_scope
= 0;
21469 /* Parse a parameter declaration.
21471 parameter-declaration:
21472 decl-specifier-seq ... [opt] declarator
21473 decl-specifier-seq declarator = assignment-expression
21474 decl-specifier-seq ... [opt] abstract-declarator [opt]
21475 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21477 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21478 declares a template parameter. (In that case, a non-nested `>'
21479 token encountered during the parsing of the assignment-expression
21480 is not interpreted as a greater-than operator.)
21482 Returns a representation of the parameter, or NULL if an error
21483 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21484 true iff the declarator is of the form "(p)". */
21486 static cp_parameter_declarator
*
21487 cp_parser_parameter_declaration (cp_parser
*parser
,
21488 bool template_parm_p
,
21489 bool *parenthesized_p
)
21491 int declares_class_or_enum
;
21492 cp_decl_specifier_seq decl_specifiers
;
21493 cp_declarator
*declarator
;
21494 tree default_argument
;
21495 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21496 const char *saved_message
;
21497 bool template_parameter_pack_p
= false;
21499 /* In a template parameter, `>' is not an operator.
21503 When parsing a default template-argument for a non-type
21504 template-parameter, the first non-nested `>' is taken as the end
21505 of the template parameter-list rather than a greater-than
21508 /* Type definitions may not appear in parameter types. */
21509 saved_message
= parser
->type_definition_forbidden_message
;
21510 parser
->type_definition_forbidden_message
21511 = G_("types may not be defined in parameter types");
21513 /* Parse the declaration-specifiers. */
21514 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21515 cp_parser_decl_specifier_seq (parser
,
21516 CP_PARSER_FLAGS_NONE
,
21518 &declares_class_or_enum
);
21520 /* Complain about missing 'typename' or other invalid type names. */
21521 if (!decl_specifiers
.any_type_specifiers_p
21522 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21523 decl_specifiers
.type
= error_mark_node
;
21525 /* If an error occurred, there's no reason to attempt to parse the
21526 rest of the declaration. */
21527 if (cp_parser_error_occurred (parser
))
21529 parser
->type_definition_forbidden_message
= saved_message
;
21533 /* Peek at the next token. */
21534 token
= cp_lexer_peek_token (parser
->lexer
);
21536 /* If the next token is a `)', `,', `=', `>', or `...', then there
21537 is no declarator. However, when variadic templates are enabled,
21538 there may be a declarator following `...'. */
21539 if (token
->type
== CPP_CLOSE_PAREN
21540 || token
->type
== CPP_COMMA
21541 || token
->type
== CPP_EQ
21542 || token
->type
== CPP_GREATER
)
21545 if (parenthesized_p
)
21546 *parenthesized_p
= false;
21548 /* Otherwise, there should be a declarator. */
21551 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21552 parser
->default_arg_ok_p
= false;
21554 /* After seeing a decl-specifier-seq, if the next token is not a
21555 "(", there is no possibility that the code is a valid
21556 expression. Therefore, if parsing tentatively, we commit at
21558 if (!parser
->in_template_argument_list_p
21559 /* In an expression context, having seen:
21563 we cannot be sure whether we are looking at a
21564 function-type (taking a "char" as a parameter) or a cast
21565 of some object of type "char" to "int". */
21566 && !parser
->in_type_id_in_expr_p
21567 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21568 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21569 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21570 cp_parser_commit_to_tentative_parse (parser
);
21571 /* Parse the declarator. */
21572 declarator_token_start
= token
;
21573 declarator
= cp_parser_declarator (parser
,
21574 CP_PARSER_DECLARATOR_EITHER
,
21575 /*ctor_dtor_or_conv_p=*/NULL
,
21577 /*member_p=*/false,
21578 /*friend_p=*/false);
21579 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21580 /* After the declarator, allow more attributes. */
21581 decl_specifiers
.attributes
21582 = attr_chainon (decl_specifiers
.attributes
,
21583 cp_parser_attributes_opt (parser
));
21585 /* If the declarator is a template parameter pack, remember that and
21586 clear the flag in the declarator itself so we don't get errors
21587 from grokdeclarator. */
21588 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21590 declarator
->parameter_pack_p
= false;
21591 template_parameter_pack_p
= true;
21595 /* If the next token is an ellipsis, and we have not seen a declarator
21596 name, and if either the type of the declarator contains parameter
21597 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21598 for, eg, abbreviated integral type names), then we actually have a
21599 parameter pack expansion expression. Otherwise, leave the ellipsis
21600 for a C-style variadic function. */
21601 token
= cp_lexer_peek_token (parser
->lexer
);
21602 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21604 tree type
= decl_specifiers
.type
;
21606 if (type
&& DECL_P (type
))
21607 type
= TREE_TYPE (type
);
21610 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21611 && (template_parm_p
|| uses_parameter_packs (type
)))
21612 || (!type
&& template_parm_p
))
21613 && declarator_can_be_parameter_pack (declarator
))
21615 /* Consume the `...'. */
21616 cp_lexer_consume_token (parser
->lexer
);
21617 maybe_warn_variadic_templates ();
21619 /* Build a pack expansion type */
21620 if (template_parm_p
)
21621 template_parameter_pack_p
= true;
21622 else if (declarator
)
21623 declarator
->parameter_pack_p
= true;
21625 decl_specifiers
.type
= make_pack_expansion (type
);
21629 /* The restriction on defining new types applies only to the type
21630 of the parameter, not to the default argument. */
21631 parser
->type_definition_forbidden_message
= saved_message
;
21633 /* If the next token is `=', then process a default argument. */
21634 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21636 tree type
= decl_specifiers
.type
;
21637 token
= cp_lexer_peek_token (parser
->lexer
);
21638 /* If we are defining a class, then the tokens that make up the
21639 default argument must be saved and processed later. */
21640 if (!template_parm_p
&& at_class_scope_p ()
21641 && TYPE_BEING_DEFINED (current_class_type
)
21642 && !LAMBDA_TYPE_P (current_class_type
))
21643 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21645 // A constrained-type-specifier may declare a type template-parameter.
21646 else if (declares_constrained_type_template_parameter (type
))
21648 = cp_parser_default_type_template_argument (parser
);
21650 // A constrained-type-specifier may declare a template-template-parameter.
21651 else if (declares_constrained_template_template_parameter (type
))
21653 = cp_parser_default_template_template_argument (parser
);
21655 /* Outside of a class definition, we can just parse the
21656 assignment-expression. */
21659 = cp_parser_default_argument (parser
, template_parm_p
);
21661 if (!parser
->default_arg_ok_p
)
21663 permerror (token
->location
,
21664 "default arguments are only "
21665 "permitted for function parameters");
21667 else if ((declarator
&& declarator
->parameter_pack_p
)
21668 || template_parameter_pack_p
21669 || (decl_specifiers
.type
21670 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21672 /* Find the name of the parameter pack. */
21673 cp_declarator
*id_declarator
= declarator
;
21674 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21675 id_declarator
= id_declarator
->declarator
;
21677 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21678 error_at (declarator_token_start
->location
,
21680 ? G_("template parameter pack %qD "
21681 "cannot have a default argument")
21682 : G_("parameter pack %qD cannot have "
21683 "a default argument"),
21684 id_declarator
->u
.id
.unqualified_name
);
21686 error_at (declarator_token_start
->location
,
21688 ? G_("template parameter pack cannot have "
21689 "a default argument")
21690 : G_("parameter pack cannot have a "
21691 "default argument"));
21693 default_argument
= NULL_TREE
;
21697 default_argument
= NULL_TREE
;
21699 /* Generate a location for the parameter, ranging from the start of the
21700 initial token to the end of the final token (using input_location for
21701 the latter, set up by cp_lexer_set_source_position_from_token when
21704 If we have a identifier, then use it for the caret location, e.g.
21706 extern int callee (int one, int (*two)(int, int), float three);
21707 ~~~~~~^~~~~~~~~~~~~~
21709 otherwise, reuse the start location for the caret location e.g.:
21711 extern int callee (int one, int (*)(int, int), float three);
21715 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21716 ? declarator
->id_loc
21717 : decl_spec_token_start
->location
);
21718 location_t param_loc
= make_location (caret_loc
,
21719 decl_spec_token_start
->location
,
21722 return make_parameter_declarator (&decl_specifiers
,
21726 template_parameter_pack_p
);
21729 /* Parse a default argument and return it.
21731 TEMPLATE_PARM_P is true if this is a default argument for a
21732 non-type template parameter. */
21734 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21736 tree default_argument
= NULL_TREE
;
21737 bool saved_greater_than_is_operator_p
;
21738 bool saved_local_variables_forbidden_p
;
21739 bool non_constant_p
, is_direct_init
;
21741 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21743 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21744 parser
->greater_than_is_operator_p
= !template_parm_p
;
21745 /* Local variable names (and the `this' keyword) may not
21746 appear in a default argument. */
21747 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21748 parser
->local_variables_forbidden_p
= true;
21749 /* Parse the assignment-expression. */
21750 if (template_parm_p
)
21751 push_deferring_access_checks (dk_no_deferred
);
21752 tree saved_class_ptr
= NULL_TREE
;
21753 tree saved_class_ref
= NULL_TREE
;
21754 /* The "this" pointer is not valid in a default argument. */
21757 saved_class_ptr
= current_class_ptr
;
21758 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21759 saved_class_ref
= current_class_ref
;
21760 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21763 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21764 /* Restore the "this" pointer. */
21767 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21768 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21770 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21771 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21772 if (template_parm_p
)
21773 pop_deferring_access_checks ();
21774 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21775 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21777 return default_argument
;
21780 /* Parse a function-body.
21783 compound_statement */
21786 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21788 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21789 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21793 /* Parse a ctor-initializer-opt followed by a function-body. Return
21794 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21795 is true we are parsing a function-try-block. */
21798 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21799 bool in_function_try_block
)
21802 const bool check_body_p
=
21803 DECL_CONSTRUCTOR_P (current_function_decl
)
21804 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21807 /* Begin the function body. */
21808 body
= begin_function_body ();
21809 /* Parse the optional ctor-initializer. */
21810 cp_parser_ctor_initializer_opt (parser
);
21812 /* If we're parsing a constexpr constructor definition, we need
21813 to check that the constructor body is indeed empty. However,
21814 before we get to cp_parser_function_body lot of junk has been
21815 generated, so we can't just check that we have an empty block.
21816 Rather we take a snapshot of the outermost block, and check whether
21817 cp_parser_function_body changed its state. */
21820 list
= cur_stmt_list
;
21821 if (STATEMENT_LIST_TAIL (list
))
21822 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21824 /* Parse the function-body. */
21825 cp_parser_function_body (parser
, in_function_try_block
);
21827 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21828 /* Finish the function body. */
21829 finish_function_body (body
);
21832 /* Parse an initializer.
21835 = initializer-clause
21836 ( expression-list )
21838 Returns an expression representing the initializer. If no
21839 initializer is present, NULL_TREE is returned.
21841 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21842 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21843 set to TRUE if there is no initializer present. If there is an
21844 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21845 is set to true; otherwise it is set to false. */
21848 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21849 bool* non_constant_p
)
21854 /* Peek at the next token. */
21855 token
= cp_lexer_peek_token (parser
->lexer
);
21857 /* Let our caller know whether or not this initializer was
21859 *is_direct_init
= (token
->type
!= CPP_EQ
);
21860 /* Assume that the initializer is constant. */
21861 *non_constant_p
= false;
21863 if (token
->type
== CPP_EQ
)
21865 /* Consume the `='. */
21866 cp_lexer_consume_token (parser
->lexer
);
21867 /* Parse the initializer-clause. */
21868 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21870 else if (token
->type
== CPP_OPEN_PAREN
)
21872 vec
<tree
, va_gc
> *vec
;
21873 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21875 /*allow_expansion_p=*/true,
21878 return error_mark_node
;
21879 init
= build_tree_list_vec (vec
);
21880 release_tree_vector (vec
);
21882 else if (token
->type
== CPP_OPEN_BRACE
)
21884 cp_lexer_set_source_position (parser
->lexer
);
21885 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21886 init
= cp_parser_braced_list (parser
, non_constant_p
);
21887 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21891 /* Anything else is an error. */
21892 cp_parser_error (parser
, "expected initializer");
21893 init
= error_mark_node
;
21896 if (check_for_bare_parameter_packs (init
))
21897 init
= error_mark_node
;
21902 /* Parse an initializer-clause.
21904 initializer-clause:
21905 assignment-expression
21908 Returns an expression representing the initializer.
21910 If the `assignment-expression' production is used the value
21911 returned is simply a representation for the expression.
21913 Otherwise, calls cp_parser_braced_list. */
21916 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21918 cp_expr initializer
;
21920 /* Assume the expression is constant. */
21921 *non_constant_p
= false;
21923 /* If it is not a `{', then we are looking at an
21924 assignment-expression. */
21925 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21928 = cp_parser_constant_expression (parser
,
21929 /*allow_non_constant_p=*/true,
21933 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21935 return initializer
;
21938 /* Parse a brace-enclosed initializer list.
21941 { initializer-list , [opt] }
21942 { designated-initializer-list , [opt] }
21945 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21946 the elements of the initializer-list (or NULL, if the last
21947 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21948 NULL_TREE. There is no way to detect whether or not the optional
21949 trailing `,' was provided. NON_CONSTANT_P is as for
21950 cp_parser_initializer. */
21953 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
21956 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21958 /* Consume the `{' token. */
21959 matching_braces braces
;
21960 braces
.require_open (parser
);
21961 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21962 initializer
= make_node (CONSTRUCTOR
);
21963 /* If it's not a `}', then there is a non-trivial initializer. */
21964 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
21966 /* Parse the initializer list. */
21967 CONSTRUCTOR_ELTS (initializer
)
21968 = cp_parser_initializer_list (parser
, non_constant_p
);
21969 /* A trailing `,' token is allowed. */
21970 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21971 cp_lexer_consume_token (parser
->lexer
);
21974 *non_constant_p
= false;
21975 /* Now, there should be a trailing `}'. */
21976 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21977 braces
.require_close (parser
);
21978 TREE_TYPE (initializer
) = init_list_type_node
;
21980 cp_expr
result (initializer
);
21981 /* Build a location of the form:
21984 with caret==start at the open brace, finish at the close brace. */
21985 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
21986 result
.set_location (combined_loc
);
21990 /* Consume tokens up to, and including, the next non-nested closing `]'.
21991 Returns true iff we found a closing `]'. */
21994 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
21996 unsigned square_depth
= 0;
22000 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22002 switch (token
->type
)
22005 case CPP_PRAGMA_EOL
:
22006 /* If we've run out of tokens, then there is no closing `]'. */
22009 case CPP_OPEN_SQUARE
:
22013 case CPP_CLOSE_SQUARE
:
22014 if (!square_depth
--)
22016 cp_lexer_consume_token (parser
->lexer
);
22025 /* Consume the token. */
22026 cp_lexer_consume_token (parser
->lexer
);
22030 /* Return true if we are looking at an array-designator, false otherwise. */
22033 cp_parser_array_designator_p (cp_parser
*parser
)
22035 /* Consume the `['. */
22036 cp_lexer_consume_token (parser
->lexer
);
22038 cp_lexer_save_tokens (parser
->lexer
);
22040 /* Skip tokens until the next token is a closing square bracket.
22041 If we find the closing `]', and the next token is a `=', then
22042 we are looking at an array designator. */
22043 bool array_designator_p
22044 = (cp_parser_skip_to_closing_square_bracket (parser
)
22045 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22047 /* Roll back the tokens we skipped. */
22048 cp_lexer_rollback_tokens (parser
->lexer
);
22050 return array_designator_p
;
22053 /* Parse an initializer-list.
22056 initializer-clause ... [opt]
22057 initializer-list , initializer-clause ... [opt]
22061 designated-initializer-list:
22062 designated-initializer-clause
22063 designated-initializer-list , designated-initializer-clause
22065 designated-initializer-clause:
22066 designator brace-or-equal-initializer
22074 designation initializer-clause ...[opt]
22075 initializer-list , designation initializer-clause ...[opt]
22080 [ constant-expression ] =
22082 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22083 for the initializer. If the INDEX of the elt is non-NULL, it is the
22084 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22085 as for cp_parser_initializer. */
22087 static vec
<constructor_elt
, va_gc
> *
22088 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22090 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22091 bool first_p
= true;
22092 tree first_designator
= NULL_TREE
;
22094 /* Assume all of the expressions are constant. */
22095 *non_constant_p
= false;
22097 /* Parse the rest of the list. */
22103 bool clause_non_constant_p
;
22104 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22106 /* Handle the C++2A syntax, '. id ='. */
22107 if ((cxx_dialect
>= cxx2a
22108 || cp_parser_allow_gnu_extensions_p (parser
))
22109 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22110 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22111 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22112 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22113 == CPP_OPEN_BRACE
)))
22115 if (cxx_dialect
< cxx2a
)
22116 pedwarn (loc
, OPT_Wpedantic
,
22117 "C++ designated initializers only available with "
22118 "-std=c++2a or -std=gnu++2a");
22119 /* Consume the `.'. */
22120 cp_lexer_consume_token (parser
->lexer
);
22121 /* Consume the identifier. */
22122 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22123 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22124 /* Consume the `='. */
22125 cp_lexer_consume_token (parser
->lexer
);
22127 /* Also, if the next token is an identifier and the following one is a
22128 colon, we are looking at the GNU designated-initializer
22130 else if (cp_parser_allow_gnu_extensions_p (parser
)
22131 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22132 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22135 /* Warn the user that they are using an extension. */
22136 pedwarn (loc
, OPT_Wpedantic
,
22137 "ISO C++ does not allow GNU designated initializers");
22138 /* Consume the identifier. */
22139 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22140 /* Consume the `:'. */
22141 cp_lexer_consume_token (parser
->lexer
);
22143 /* Also handle C99 array designators, '[ const ] ='. */
22144 else if (cp_parser_allow_gnu_extensions_p (parser
)
22145 && !c_dialect_objc ()
22146 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22148 /* In C++11, [ could start a lambda-introducer. */
22149 bool non_const
= false;
22151 cp_parser_parse_tentatively (parser
);
22153 if (!cp_parser_array_designator_p (parser
))
22155 cp_parser_simulate_error (parser
);
22156 designator
= NULL_TREE
;
22160 designator
= cp_parser_constant_expression (parser
, true,
22162 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22163 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22166 if (!cp_parser_parse_definitely (parser
))
22167 designator
= NULL_TREE
;
22169 && (!require_potential_rvalue_constant_expression
22171 designator
= NULL_TREE
;
22173 /* Warn the user that they are using an extension. */
22174 pedwarn (loc
, OPT_Wpedantic
,
22175 "ISO C++ does not allow C99 designated initializers");
22178 designator
= NULL_TREE
;
22182 first_designator
= designator
;
22185 else if (cxx_dialect
>= cxx2a
22186 && first_designator
!= error_mark_node
22187 && (!first_designator
!= !designator
))
22189 error_at (loc
, "either all initializer clauses should be designated "
22190 "or none of them should be");
22191 first_designator
= error_mark_node
;
22193 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22194 first_designator
= designator
;
22196 /* Parse the initializer. */
22197 initializer
= cp_parser_initializer_clause (parser
,
22198 &clause_non_constant_p
);
22199 /* If any clause is non-constant, so is the entire initializer. */
22200 if (clause_non_constant_p
)
22201 *non_constant_p
= true;
22203 /* If we have an ellipsis, this is an initializer pack
22205 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22207 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22209 /* Consume the `...'. */
22210 cp_lexer_consume_token (parser
->lexer
);
22212 if (designator
&& cxx_dialect
>= cxx2a
)
22214 "%<...%> not allowed in designated initializer list");
22216 /* Turn the initializer into an initializer expansion. */
22217 initializer
= make_pack_expansion (initializer
);
22220 /* Add it to the vector. */
22221 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22223 /* If the next token is not a comma, we have reached the end of
22225 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22228 /* Peek at the next token. */
22229 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22230 /* If the next token is a `}', then we're still done. An
22231 initializer-clause can have a trailing `,' after the
22232 initializer-list and before the closing `}'. */
22233 if (token
->type
== CPP_CLOSE_BRACE
)
22236 /* Consume the `,' token. */
22237 cp_lexer_consume_token (parser
->lexer
);
22240 /* The same identifier shall not appear in multiple designators
22241 of a designated-initializer-list. */
22242 if (first_designator
)
22245 tree designator
, val
;
22246 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22247 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22249 if (IDENTIFIER_MARKED (designator
))
22251 error_at (EXPR_LOC_OR_LOC (val
, input_location
),
22252 "%<.%s%> designator used multiple times in "
22253 "the same initializer list",
22254 IDENTIFIER_POINTER (designator
));
22255 (*v
)[i
].index
= NULL_TREE
;
22258 IDENTIFIER_MARKED (designator
) = 1;
22260 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22261 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22262 IDENTIFIER_MARKED (designator
) = 0;
22268 /* Classes [gram.class] */
22270 /* Parse a class-name.
22276 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22277 to indicate that names looked up in dependent types should be
22278 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22279 keyword has been used to indicate that the name that appears next
22280 is a template. TAG_TYPE indicates the explicit tag given before
22281 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22282 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22283 is the class being defined in a class-head. If ENUM_OK is TRUE,
22284 enum-names are also accepted.
22286 Returns the TYPE_DECL representing the class. */
22289 cp_parser_class_name (cp_parser
*parser
,
22290 bool typename_keyword_p
,
22291 bool template_keyword_p
,
22292 enum tag_types tag_type
,
22293 bool check_dependency_p
,
22295 bool is_declaration
,
22302 tree identifier
= NULL_TREE
;
22304 /* All class-names start with an identifier. */
22305 token
= cp_lexer_peek_token (parser
->lexer
);
22306 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22308 cp_parser_error (parser
, "expected class-name");
22309 return error_mark_node
;
22312 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22313 to a template-id, so we save it here. */
22314 scope
= parser
->scope
;
22315 if (scope
== error_mark_node
)
22316 return error_mark_node
;
22318 /* Any name names a type if we're following the `typename' keyword
22319 in a qualified name where the enclosing scope is type-dependent. */
22320 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22321 && dependent_type_p (scope
));
22322 /* Handle the common case (an identifier, but not a template-id)
22324 if (token
->type
== CPP_NAME
22325 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22327 cp_token
*identifier_token
;
22330 /* Look for the identifier. */
22331 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22332 ambiguous_p
= identifier_token
->error_reported
;
22333 identifier
= cp_parser_identifier (parser
);
22334 /* If the next token isn't an identifier, we are certainly not
22335 looking at a class-name. */
22336 if (identifier
== error_mark_node
)
22337 decl
= error_mark_node
;
22338 /* If we know this is a type-name, there's no need to look it
22340 else if (typename_p
)
22344 tree ambiguous_decls
;
22345 /* If we already know that this lookup is ambiguous, then
22346 we've already issued an error message; there's no reason
22350 cp_parser_simulate_error (parser
);
22351 return error_mark_node
;
22353 /* If the next token is a `::', then the name must be a type
22356 [basic.lookup.qual]
22358 During the lookup for a name preceding the :: scope
22359 resolution operator, object, function, and enumerator
22360 names are ignored. */
22361 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22362 tag_type
= scope_type
;
22363 /* Look up the name. */
22364 decl
= cp_parser_lookup_name (parser
, identifier
,
22366 /*is_template=*/false,
22367 /*is_namespace=*/false,
22368 check_dependency_p
,
22370 identifier_token
->location
);
22371 if (ambiguous_decls
)
22373 if (cp_parser_parsing_tentatively (parser
))
22374 cp_parser_simulate_error (parser
);
22375 return error_mark_node
;
22381 /* Try a template-id. */
22382 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22383 check_dependency_p
,
22386 if (decl
== error_mark_node
)
22387 return error_mark_node
;
22390 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22392 /* If this is a typename, create a TYPENAME_TYPE. */
22393 if (typename_p
&& decl
!= error_mark_node
)
22395 decl
= make_typename_type (scope
, decl
, typename_type
,
22396 /*complain=*/tf_error
);
22397 if (decl
!= error_mark_node
)
22398 decl
= TYPE_NAME (decl
);
22401 decl
= strip_using_decl (decl
);
22403 /* Check to see that it is really the name of a class. */
22404 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22405 && identifier_p (TREE_OPERAND (decl
, 0))
22406 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22407 /* Situations like this:
22409 template <typename T> struct A {
22410 typename T::template X<int>::I i;
22413 are problematic. Is `T::template X<int>' a class-name? The
22414 standard does not seem to be definitive, but there is no other
22415 valid interpretation of the following `::'. Therefore, those
22416 names are considered class-names. */
22418 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22419 if (decl
!= error_mark_node
)
22420 decl
= TYPE_NAME (decl
);
22422 else if (TREE_CODE (decl
) != TYPE_DECL
22423 || TREE_TYPE (decl
) == error_mark_node
22424 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22425 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22426 /* In Objective-C 2.0, a classname followed by '.' starts a
22427 dot-syntax expression, and it's not a type-name. */
22428 || (c_dialect_objc ()
22429 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22430 && objc_is_class_name (decl
)))
22431 decl
= error_mark_node
;
22433 if (decl
== error_mark_node
)
22434 cp_parser_error (parser
, "expected class-name");
22435 else if (identifier
&& !parser
->scope
)
22436 maybe_note_name_used_in_class (identifier
, decl
);
22441 /* Parse a class-specifier.
22444 class-head { member-specification [opt] }
22446 Returns the TREE_TYPE representing the class. */
22449 cp_parser_class_specifier_1 (cp_parser
* parser
)
22452 tree attributes
= NULL_TREE
;
22453 bool nested_name_specifier_p
;
22454 unsigned saved_num_template_parameter_lists
;
22455 bool saved_in_function_body
;
22456 unsigned char in_statement
;
22457 bool in_switch_statement_p
;
22458 bool saved_in_unbraced_linkage_specification_p
;
22459 tree old_scope
= NULL_TREE
;
22460 tree scope
= NULL_TREE
;
22461 cp_token
*closing_brace
;
22463 push_deferring_access_checks (dk_no_deferred
);
22465 /* Parse the class-head. */
22466 type
= cp_parser_class_head (parser
,
22467 &nested_name_specifier_p
);
22468 /* If the class-head was a semantic disaster, skip the entire body
22472 cp_parser_skip_to_end_of_block_or_statement (parser
);
22473 pop_deferring_access_checks ();
22474 return error_mark_node
;
22477 /* Look for the `{'. */
22478 matching_braces braces
;
22479 if (!braces
.require_open (parser
))
22481 pop_deferring_access_checks ();
22482 return error_mark_node
;
22485 cp_ensure_no_omp_declare_simd (parser
);
22486 cp_ensure_no_oacc_routine (parser
);
22488 /* Issue an error message if type-definitions are forbidden here. */
22489 cp_parser_check_type_definition (parser
);
22490 /* Remember that we are defining one more class. */
22491 ++parser
->num_classes_being_defined
;
22492 /* Inside the class, surrounding template-parameter-lists do not
22494 saved_num_template_parameter_lists
22495 = parser
->num_template_parameter_lists
;
22496 parser
->num_template_parameter_lists
= 0;
22497 /* We are not in a function body. */
22498 saved_in_function_body
= parser
->in_function_body
;
22499 parser
->in_function_body
= false;
22500 /* Or in a loop. */
22501 in_statement
= parser
->in_statement
;
22502 parser
->in_statement
= 0;
22503 /* Or in a switch. */
22504 in_switch_statement_p
= parser
->in_switch_statement_p
;
22505 parser
->in_switch_statement_p
= false;
22506 /* We are not immediately inside an extern "lang" block. */
22507 saved_in_unbraced_linkage_specification_p
22508 = parser
->in_unbraced_linkage_specification_p
;
22509 parser
->in_unbraced_linkage_specification_p
= false;
22511 // Associate constraints with the type.
22513 type
= associate_classtype_constraints (type
);
22515 /* Start the class. */
22516 if (nested_name_specifier_p
)
22518 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22519 old_scope
= push_inner_scope (scope
);
22521 type
= begin_class_definition (type
);
22523 if (type
== error_mark_node
)
22524 /* If the type is erroneous, skip the entire body of the class. */
22525 cp_parser_skip_to_closing_brace (parser
);
22527 /* Parse the member-specification. */
22528 cp_parser_member_specification_opt (parser
);
22530 /* Look for the trailing `}'. */
22531 closing_brace
= braces
.require_close (parser
);
22532 /* Look for trailing attributes to apply to this class. */
22533 if (cp_parser_allow_gnu_extensions_p (parser
))
22534 attributes
= cp_parser_gnu_attributes_opt (parser
);
22535 if (type
!= error_mark_node
)
22536 type
= finish_struct (type
, attributes
);
22537 if (nested_name_specifier_p
)
22538 pop_inner_scope (old_scope
, scope
);
22540 /* We've finished a type definition. Check for the common syntax
22541 error of forgetting a semicolon after the definition. We need to
22542 be careful, as we can't just check for not-a-semicolon and be done
22543 with it; the user might have typed:
22545 class X { } c = ...;
22546 class X { } *p = ...;
22548 and so forth. Instead, enumerate all the possible tokens that
22549 might follow this production; if we don't see one of them, then
22550 complain and silently insert the semicolon. */
22552 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22553 bool want_semicolon
= true;
22555 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22556 /* Don't try to parse c++11 attributes here. As per the
22557 grammar, that should be a task for
22558 cp_parser_decl_specifier_seq. */
22559 want_semicolon
= false;
22561 switch (token
->type
)
22564 case CPP_SEMICOLON
:
22567 case CPP_OPEN_PAREN
:
22568 case CPP_CLOSE_PAREN
:
22570 want_semicolon
= false;
22573 /* While it's legal for type qualifiers and storage class
22574 specifiers to follow type definitions in the grammar, only
22575 compiler testsuites contain code like that. Assume that if
22576 we see such code, then what we're really seeing is a case
22580 const <type> var = ...;
22585 static <type> func (...) ...
22587 i.e. the qualifier or specifier applies to the next
22588 declaration. To do so, however, we need to look ahead one
22589 more token to see if *that* token is a type specifier.
22591 This code could be improved to handle:
22594 static const <type> var = ...; */
22596 if (keyword_is_decl_specifier (token
->keyword
))
22598 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22600 /* Handling user-defined types here would be nice, but very
22603 = (lookahead
->type
== CPP_KEYWORD
22604 && keyword_begins_type_specifier (lookahead
->keyword
));
22611 /* If we don't have a type, then something is very wrong and we
22612 shouldn't try to do anything clever. Likewise for not seeing the
22614 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22616 /* Locate the closing brace. */
22617 cp_token_position prev
22618 = cp_lexer_previous_token_position (parser
->lexer
);
22619 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22620 location_t loc
= prev_token
->location
;
22622 /* We want to suggest insertion of a ';' immediately *after* the
22623 closing brace, so, if we can, offset the location by 1 column. */
22624 location_t next_loc
= loc
;
22625 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22626 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22628 rich_location
richloc (line_table
, next_loc
);
22630 /* If we successfully offset the location, suggest the fix-it. */
22631 if (next_loc
!= loc
)
22632 richloc
.add_fixit_insert_before (next_loc
, ";");
22634 if (CLASSTYPE_DECLARED_CLASS (type
))
22635 error_at (&richloc
,
22636 "expected %<;%> after class definition");
22637 else if (TREE_CODE (type
) == RECORD_TYPE
)
22638 error_at (&richloc
,
22639 "expected %<;%> after struct definition");
22640 else if (TREE_CODE (type
) == UNION_TYPE
)
22641 error_at (&richloc
,
22642 "expected %<;%> after union definition");
22644 gcc_unreachable ();
22646 /* Unget one token and smash it to look as though we encountered
22647 a semicolon in the input stream. */
22648 cp_lexer_set_token_position (parser
->lexer
, prev
);
22649 token
= cp_lexer_peek_token (parser
->lexer
);
22650 token
->type
= CPP_SEMICOLON
;
22651 token
->keyword
= RID_MAX
;
22655 /* If this class is not itself within the scope of another class,
22656 then we need to parse the bodies of all of the queued function
22657 definitions. Note that the queued functions defined in a class
22658 are not always processed immediately following the
22659 class-specifier for that class. Consider:
22662 struct B { void f() { sizeof (A); } };
22665 If `f' were processed before the processing of `A' were
22666 completed, there would be no way to compute the size of `A'.
22667 Note that the nesting we are interested in here is lexical --
22668 not the semantic nesting given by TYPE_CONTEXT. In particular,
22671 struct A { struct B; };
22672 struct A::B { void f() { } };
22674 there is no need to delay the parsing of `A::B::f'. */
22675 if (--parser
->num_classes_being_defined
== 0)
22678 tree class_type
= NULL_TREE
;
22679 tree pushed_scope
= NULL_TREE
;
22681 cp_default_arg_entry
*e
;
22682 tree save_ccp
, save_ccr
;
22684 if (any_erroneous_template_args_p (type
))
22686 /* Skip default arguments, NSDMIs, etc, in order to improve
22687 error recovery (c++/71169, c++/71832). */
22688 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22689 vec_safe_truncate (unparsed_nsdmis
, 0);
22690 vec_safe_truncate (unparsed_classes
, 0);
22691 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22694 /* In a first pass, parse default arguments to the functions.
22695 Then, in a second pass, parse the bodies of the functions.
22696 This two-phased approach handles cases like:
22704 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22707 /* If there are default arguments that have not yet been processed,
22708 take care of them now. */
22709 if (class_type
!= e
->class_type
)
22712 pop_scope (pushed_scope
);
22713 class_type
= e
->class_type
;
22714 pushed_scope
= push_scope (class_type
);
22716 /* Make sure that any template parameters are in scope. */
22717 maybe_begin_member_template_processing (decl
);
22718 /* Parse the default argument expressions. */
22719 cp_parser_late_parsing_default_args (parser
, decl
);
22720 /* Remove any template parameters from the symbol table. */
22721 maybe_end_member_template_processing ();
22723 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22724 /* Now parse any NSDMIs. */
22725 save_ccp
= current_class_ptr
;
22726 save_ccr
= current_class_ref
;
22727 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22729 if (class_type
!= DECL_CONTEXT (decl
))
22732 pop_scope (pushed_scope
);
22733 class_type
= DECL_CONTEXT (decl
);
22734 pushed_scope
= push_scope (class_type
);
22736 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22737 cp_parser_late_parsing_nsdmi (parser
, decl
);
22739 vec_safe_truncate (unparsed_nsdmis
, 0);
22740 current_class_ptr
= save_ccp
;
22741 current_class_ref
= save_ccr
;
22743 pop_scope (pushed_scope
);
22745 /* Now do some post-NSDMI bookkeeping. */
22746 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22747 after_nsdmi_defaulted_late_checks (class_type
);
22748 vec_safe_truncate (unparsed_classes
, 0);
22749 after_nsdmi_defaulted_late_checks (type
);
22751 /* Now parse the body of the functions. */
22754 /* OpenMP UDRs need to be parsed before all other functions. */
22755 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22756 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22757 cp_parser_late_parsing_for_member (parser
, decl
);
22758 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22759 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22760 cp_parser_late_parsing_for_member (parser
, decl
);
22763 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22764 cp_parser_late_parsing_for_member (parser
, decl
);
22765 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22768 vec_safe_push (unparsed_classes
, type
);
22770 /* Put back any saved access checks. */
22771 pop_deferring_access_checks ();
22773 /* Restore saved state. */
22774 parser
->in_switch_statement_p
= in_switch_statement_p
;
22775 parser
->in_statement
= in_statement
;
22776 parser
->in_function_body
= saved_in_function_body
;
22777 parser
->num_template_parameter_lists
22778 = saved_num_template_parameter_lists
;
22779 parser
->in_unbraced_linkage_specification_p
22780 = saved_in_unbraced_linkage_specification_p
;
22786 cp_parser_class_specifier (cp_parser
* parser
)
22789 timevar_push (TV_PARSE_STRUCT
);
22790 ret
= cp_parser_class_specifier_1 (parser
);
22791 timevar_pop (TV_PARSE_STRUCT
);
22795 /* Parse a class-head.
22798 class-key identifier [opt] base-clause [opt]
22799 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22800 class-key nested-name-specifier [opt] template-id
22803 class-virt-specifier:
22807 class-key attributes identifier [opt] base-clause [opt]
22808 class-key attributes nested-name-specifier identifier base-clause [opt]
22809 class-key attributes nested-name-specifier [opt] template-id
22812 Upon return BASES is initialized to the list of base classes (or
22813 NULL, if there are none) in the same form returned by
22814 cp_parser_base_clause.
22816 Returns the TYPE of the indicated class. Sets
22817 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22818 involving a nested-name-specifier was used, and FALSE otherwise.
22820 Returns error_mark_node if this is not a class-head.
22822 Returns NULL_TREE if the class-head is syntactically valid, but
22823 semantically invalid in a way that means we should skip the entire
22824 body of the class. */
22827 cp_parser_class_head (cp_parser
* parser
,
22828 bool* nested_name_specifier_p
)
22830 tree nested_name_specifier
;
22831 enum tag_types class_key
;
22832 tree id
= NULL_TREE
;
22833 tree type
= NULL_TREE
;
22836 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22837 bool template_id_p
= false;
22838 bool qualified_p
= false;
22839 bool invalid_nested_name_p
= false;
22840 bool invalid_explicit_specialization_p
= false;
22841 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22842 tree pushed_scope
= NULL_TREE
;
22843 unsigned num_templates
;
22844 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22845 /* Assume no nested-name-specifier will be present. */
22846 *nested_name_specifier_p
= false;
22847 /* Assume no template parameter lists will be used in defining the
22850 parser
->colon_corrects_to_scope_p
= false;
22852 /* Look for the class-key. */
22853 class_key
= cp_parser_class_key (parser
);
22854 if (class_key
== none_type
)
22855 return error_mark_node
;
22857 location_t class_head_start_location
= input_location
;
22859 /* Parse the attributes. */
22860 attributes
= cp_parser_attributes_opt (parser
);
22862 /* If the next token is `::', that is invalid -- but sometimes
22863 people do try to write:
22867 Handle this gracefully by accepting the extra qualifier, and then
22868 issuing an error about it later if this really is a
22869 class-head. If it turns out just to be an elaborated type
22870 specifier, remain silent. */
22871 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22872 qualified_p
= true;
22874 push_deferring_access_checks (dk_no_check
);
22876 /* Determine the name of the class. Begin by looking for an
22877 optional nested-name-specifier. */
22878 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22879 nested_name_specifier
22880 = cp_parser_nested_name_specifier_opt (parser
,
22881 /*typename_keyword_p=*/false,
22882 /*check_dependency_p=*/false,
22884 /*is_declaration=*/false);
22885 /* If there was a nested-name-specifier, then there *must* be an
22888 cp_token
*bad_template_keyword
= NULL
;
22890 if (nested_name_specifier
)
22892 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22893 /* Although the grammar says `identifier', it really means
22894 `class-name' or `template-name'. You are only allowed to
22895 define a class that has already been declared with this
22898 The proposed resolution for Core Issue 180 says that wherever
22899 you see `class T::X' you should treat `X' as a type-name.
22901 It is OK to define an inaccessible class; for example:
22903 class A { class B; };
22906 We do not know if we will see a class-name, or a
22907 template-name. We look for a class-name first, in case the
22908 class-name is a template-id; if we looked for the
22909 template-name first we would stop after the template-name. */
22910 cp_parser_parse_tentatively (parser
);
22911 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22912 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
22913 type
= cp_parser_class_name (parser
,
22914 /*typename_keyword_p=*/false,
22915 /*template_keyword_p=*/false,
22917 /*check_dependency_p=*/false,
22918 /*class_head_p=*/true,
22919 /*is_declaration=*/false);
22920 /* If that didn't work, ignore the nested-name-specifier. */
22921 if (!cp_parser_parse_definitely (parser
))
22923 invalid_nested_name_p
= true;
22924 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22925 id
= cp_parser_identifier (parser
);
22926 if (id
== error_mark_node
)
22929 /* If we could not find a corresponding TYPE, treat this
22930 declaration like an unqualified declaration. */
22931 if (type
== error_mark_node
)
22932 nested_name_specifier
= NULL_TREE
;
22933 /* Otherwise, count the number of templates used in TYPE and its
22934 containing scopes. */
22939 for (scope
= TREE_TYPE (type
);
22940 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22941 scope
= get_containing_scope (scope
))
22943 && CLASS_TYPE_P (scope
)
22944 && CLASSTYPE_TEMPLATE_INFO (scope
)
22945 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22946 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22947 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22951 /* Otherwise, the identifier is optional. */
22954 /* We don't know whether what comes next is a template-id,
22955 an identifier, or nothing at all. */
22956 cp_parser_parse_tentatively (parser
);
22957 /* Check for a template-id. */
22958 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22959 id
= cp_parser_template_id (parser
,
22960 /*template_keyword_p=*/false,
22961 /*check_dependency_p=*/true,
22963 /*is_declaration=*/true);
22964 /* If that didn't work, it could still be an identifier. */
22965 if (!cp_parser_parse_definitely (parser
))
22967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22969 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22970 id
= cp_parser_identifier (parser
);
22977 template_id_p
= true;
22982 pop_deferring_access_checks ();
22986 cp_parser_check_for_invalid_template_id (parser
, id
,
22988 type_start_token
->location
);
22990 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22992 /* If it's not a `:' or a `{' then we can't really be looking at a
22993 class-head, since a class-head only appears as part of a
22994 class-specifier. We have to detect this situation before calling
22995 xref_tag, since that has irreversible side-effects. */
22996 if (!cp_parser_next_token_starts_class_definition_p (parser
))
22998 cp_parser_error (parser
, "expected %<{%> or %<:%>");
22999 type
= error_mark_node
;
23003 /* At this point, we're going ahead with the class-specifier, even
23004 if some other problem occurs. */
23005 cp_parser_commit_to_tentative_parse (parser
);
23006 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23008 cp_parser_error (parser
,
23009 "cannot specify %<override%> for a class");
23010 type
= error_mark_node
;
23013 /* Issue the error about the overly-qualified name now. */
23016 cp_parser_error (parser
,
23017 "global qualification of class name is invalid");
23018 type
= error_mark_node
;
23021 else if (invalid_nested_name_p
)
23023 cp_parser_error (parser
,
23024 "qualified name does not name a class");
23025 type
= error_mark_node
;
23028 else if (nested_name_specifier
)
23032 if (bad_template_keyword
)
23033 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23034 keyword template shall not appear at the top level. */
23035 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23036 "keyword %<template%> not allowed in class-head-name");
23038 /* Reject typedef-names in class heads. */
23039 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23041 error_at (type_start_token
->location
,
23042 "invalid class name in declaration of %qD",
23048 /* Figure out in what scope the declaration is being placed. */
23049 scope
= current_scope ();
23050 /* If that scope does not contain the scope in which the
23051 class was originally declared, the program is invalid. */
23052 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23054 if (at_namespace_scope_p ())
23055 error_at (type_start_token
->location
,
23056 "declaration of %qD in namespace %qD which does not "
23058 type
, scope
, nested_name_specifier
);
23060 error_at (type_start_token
->location
,
23061 "declaration of %qD in %qD which does not enclose %qD",
23062 type
, scope
, nested_name_specifier
);
23068 A declarator-id shall not be qualified except for the
23069 definition of a ... nested class outside of its class
23070 ... [or] the definition or explicit instantiation of a
23071 class member of a namespace outside of its namespace. */
23072 if (scope
== nested_name_specifier
)
23074 permerror (nested_name_specifier_token_start
->location
,
23075 "extra qualification not allowed");
23076 nested_name_specifier
= NULL_TREE
;
23080 /* An explicit-specialization must be preceded by "template <>". If
23081 it is not, try to recover gracefully. */
23082 if (at_namespace_scope_p ()
23083 && parser
->num_template_parameter_lists
== 0
23084 && !processing_template_parmlist
23087 /* Build a location of this form:
23088 struct typename <ARGS>
23089 ^~~~~~~~~~~~~~~~~~~~~~
23090 with caret==start at the start token, and
23091 finishing at the end of the type. */
23092 location_t reported_loc
23093 = make_location (class_head_start_location
,
23094 class_head_start_location
,
23095 get_finish (type_start_token
->location
));
23096 rich_location
richloc (line_table
, reported_loc
);
23097 richloc
.add_fixit_insert_before (class_head_start_location
,
23099 error_at (&richloc
,
23100 "an explicit specialization must be preceded by"
23101 " %<template <>%>");
23102 invalid_explicit_specialization_p
= true;
23103 /* Take the same action that would have been taken by
23104 cp_parser_explicit_specialization. */
23105 ++parser
->num_template_parameter_lists
;
23106 begin_specialization ();
23108 /* There must be no "return" statements between this point and the
23109 end of this function; set "type "to the correct return value and
23110 use "goto done;" to return. */
23111 /* Make sure that the right number of template parameters were
23113 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23114 type_start_token
->location
,
23115 /*declarator=*/NULL
))
23117 /* If something went wrong, there is no point in even trying to
23118 process the class-definition. */
23123 /* Look up the type. */
23126 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23127 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23128 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23130 error_at (type_start_token
->location
,
23131 "function template %qD redeclared as a class template", id
);
23132 type
= error_mark_node
;
23136 type
= TREE_TYPE (id
);
23137 type
= maybe_process_partial_specialization (type
);
23139 /* Check the scope while we still know whether or not we had a
23140 nested-name-specifier. */
23141 if (type
!= error_mark_node
)
23142 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23144 if (nested_name_specifier
)
23145 pushed_scope
= push_scope (nested_name_specifier
);
23147 else if (nested_name_specifier
)
23153 template <typename T> struct S { struct T };
23154 template <typename T> struct S<T>::T { };
23156 we will get a TYPENAME_TYPE when processing the definition of
23157 `S::T'. We need to resolve it to the actual type before we
23158 try to define it. */
23159 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23161 class_type
= resolve_typename_type (TREE_TYPE (type
),
23162 /*only_current_p=*/false);
23163 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23164 type
= TYPE_NAME (class_type
);
23167 cp_parser_error (parser
, "could not resolve typename type");
23168 type
= error_mark_node
;
23172 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23173 == error_mark_node
)
23179 class_type
= current_class_type
;
23180 /* Enter the scope indicated by the nested-name-specifier. */
23181 pushed_scope
= push_scope (nested_name_specifier
);
23182 /* Get the canonical version of this type. */
23183 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23184 /* Call push_template_decl if it seems like we should be defining a
23185 template either from the template headers or the type we're
23186 defining, so that we diagnose both extra and missing headers. */
23187 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23188 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23189 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23191 type
= push_template_decl (type
);
23192 if (type
== error_mark_node
)
23199 type
= TREE_TYPE (type
);
23200 *nested_name_specifier_p
= true;
23202 else /* The name is not a nested name. */
23204 /* If the class was unnamed, create a dummy name. */
23206 id
= make_anon_name ();
23207 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23208 ? ts_within_enclosing_non_class
23210 type
= xref_tag (class_key
, id
, tag_scope
,
23211 parser
->num_template_parameter_lists
);
23214 /* Indicate whether this class was declared as a `class' or as a
23216 if (TREE_CODE (type
) == RECORD_TYPE
)
23217 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23218 cp_parser_check_class_key (class_key
, type
);
23220 /* If this type was already complete, and we see another definition,
23221 that's an error. */
23222 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23224 error_at (type_start_token
->location
, "redefinition of %q#T",
23226 inform (location_of (type
), "previous definition of %q#T",
23231 else if (type
== error_mark_node
)
23236 /* Apply attributes now, before any use of the class as a template
23237 argument in its base list. */
23238 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23239 fixup_attribute_variants (type
);
23242 /* We will have entered the scope containing the class; the names of
23243 base classes should be looked up in that context. For example:
23245 struct A { struct B {}; struct C; };
23246 struct A::C : B {};
23250 /* Get the list of base-classes, if there is one. */
23251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23253 /* PR59482: enter the class scope so that base-specifiers are looked
23257 bases
= cp_parser_base_clause (parser
);
23258 /* PR59482: get out of the previously pushed class scope so that the
23259 subsequent pops pop the right thing. */
23266 /* If we're really defining a class, process the base classes.
23267 If they're invalid, fail. */
23268 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23269 xref_basetypes (type
, bases
);
23272 /* Leave the scope given by the nested-name-specifier. We will
23273 enter the class scope itself while processing the members. */
23275 pop_scope (pushed_scope
);
23277 if (invalid_explicit_specialization_p
)
23279 end_specialization ();
23280 --parser
->num_template_parameter_lists
;
23284 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23285 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23286 CLASSTYPE_FINAL (type
) = 1;
23288 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23292 /* Parse a class-key.
23299 Returns the kind of class-key specified, or none_type to indicate
23302 static enum tag_types
23303 cp_parser_class_key (cp_parser
* parser
)
23306 enum tag_types tag_type
;
23308 /* Look for the class-key. */
23309 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23313 /* Check to see if the TOKEN is a class-key. */
23314 tag_type
= cp_parser_token_is_class_key (token
);
23316 cp_parser_error (parser
, "expected class-key");
23320 /* Parse a type-parameter-key.
23322 type-parameter-key:
23328 cp_parser_type_parameter_key (cp_parser
* parser
)
23330 /* Look for the type-parameter-key. */
23331 enum tag_types tag_type
= none_type
;
23332 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23333 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23335 cp_lexer_consume_token (parser
->lexer
);
23336 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23337 /* typename is not allowed in a template template parameter
23338 by the standard until C++17. */
23339 pedwarn (token
->location
, OPT_Wpedantic
,
23340 "ISO C++ forbids typename key in template template parameter;"
23341 " use -std=c++17 or -std=gnu++17");
23344 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23349 /* Parse an (optional) member-specification.
23351 member-specification:
23352 member-declaration member-specification [opt]
23353 access-specifier : member-specification [opt] */
23356 cp_parser_member_specification_opt (cp_parser
* parser
)
23363 /* Peek at the next token. */
23364 token
= cp_lexer_peek_token (parser
->lexer
);
23365 /* If it's a `}', or EOF then we've seen all the members. */
23366 if (token
->type
== CPP_CLOSE_BRACE
23367 || token
->type
== CPP_EOF
23368 || token
->type
== CPP_PRAGMA_EOL
)
23371 /* See if this token is a keyword. */
23372 keyword
= token
->keyword
;
23376 case RID_PROTECTED
:
23378 /* Consume the access-specifier. */
23379 cp_lexer_consume_token (parser
->lexer
);
23380 /* Remember which access-specifier is active. */
23381 current_access_specifier
= token
->u
.value
;
23382 /* Look for the `:'. */
23383 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23387 /* Accept #pragmas at class scope. */
23388 if (token
->type
== CPP_PRAGMA
)
23390 cp_parser_pragma (parser
, pragma_member
, NULL
);
23394 /* Otherwise, the next construction must be a
23395 member-declaration. */
23396 cp_parser_member_declaration (parser
);
23401 /* Parse a member-declaration.
23403 member-declaration:
23404 decl-specifier-seq [opt] member-declarator-list [opt] ;
23405 function-definition ; [opt]
23406 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23408 template-declaration
23411 member-declarator-list:
23413 member-declarator-list , member-declarator
23416 declarator pure-specifier [opt]
23417 declarator constant-initializer [opt]
23418 identifier [opt] : constant-expression
23422 member-declaration:
23423 __extension__ member-declaration
23426 declarator attributes [opt] pure-specifier [opt]
23427 declarator attributes [opt] constant-initializer [opt]
23428 identifier [opt] attributes [opt] : constant-expression
23432 member-declaration:
23433 static_assert-declaration */
23436 cp_parser_member_declaration (cp_parser
* parser
)
23438 cp_decl_specifier_seq decl_specifiers
;
23439 tree prefix_attributes
;
23441 int declares_class_or_enum
;
23443 cp_token
*token
= NULL
;
23444 cp_token
*decl_spec_token_start
= NULL
;
23445 cp_token
*initializer_token_start
= NULL
;
23446 int saved_pedantic
;
23447 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23449 /* Check for the `__extension__' keyword. */
23450 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23453 cp_parser_member_declaration (parser
);
23454 /* Restore the old value of the PEDANTIC flag. */
23455 pedantic
= saved_pedantic
;
23460 /* Check for a template-declaration. */
23461 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23463 /* An explicit specialization here is an error condition, and we
23464 expect the specialization handler to detect and report this. */
23465 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23466 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23467 cp_parser_explicit_specialization (parser
);
23469 cp_parser_template_declaration (parser
, /*member_p=*/true);
23473 /* Check for a template introduction. */
23474 else if (cp_parser_template_declaration_after_export (parser
, true))
23477 /* Check for a using-declaration. */
23478 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23480 if (cxx_dialect
< cxx11
)
23482 /* Parse the using-declaration. */
23483 cp_parser_using_declaration (parser
,
23484 /*access_declaration_p=*/false);
23490 bool alias_decl_expected
;
23491 cp_parser_parse_tentatively (parser
);
23492 decl
= cp_parser_alias_declaration (parser
);
23493 /* Note that if we actually see the '=' token after the
23494 identifier, cp_parser_alias_declaration commits the
23495 tentative parse. In that case, we really expect an
23496 alias-declaration. Otherwise, we expect a using
23498 alias_decl_expected
=
23499 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23500 cp_parser_parse_definitely (parser
);
23502 if (alias_decl_expected
)
23503 finish_member_declaration (decl
);
23505 cp_parser_using_declaration (parser
,
23506 /*access_declaration_p=*/false);
23511 /* Check for @defs. */
23512 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23515 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23516 ivar
= ivar_chains
;
23520 ivar
= TREE_CHAIN (member
);
23521 TREE_CHAIN (member
) = NULL_TREE
;
23522 finish_member_declaration (member
);
23527 /* If the next token is `static_assert' we have a static assertion. */
23528 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23530 cp_parser_static_assert (parser
, /*member_p=*/true);
23534 parser
->colon_corrects_to_scope_p
= false;
23536 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23539 /* Parse the decl-specifier-seq. */
23540 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23541 cp_parser_decl_specifier_seq (parser
,
23542 CP_PARSER_FLAGS_OPTIONAL
,
23544 &declares_class_or_enum
);
23545 /* Check for an invalid type-name. */
23546 if (!decl_specifiers
.any_type_specifiers_p
23547 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23549 /* If there is no declarator, then the decl-specifier-seq should
23551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23553 /* If there was no decl-specifier-seq, and the next token is a
23554 `;', then we have something like:
23560 Each member-declaration shall declare at least one member
23561 name of the class. */
23562 if (!decl_specifiers
.any_specifiers_p
)
23564 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23565 if (!in_system_header_at (token
->location
))
23567 gcc_rich_location
richloc (token
->location
);
23568 richloc
.add_fixit_remove ();
23569 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23576 /* See if this declaration is a friend. */
23577 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23578 /* If there were decl-specifiers, check to see if there was
23579 a class-declaration. */
23580 type
= check_tag_decl (&decl_specifiers
,
23581 /*explicit_type_instantiation_p=*/false);
23582 /* Nested classes have already been added to the class, but
23583 a `friend' needs to be explicitly registered. */
23586 /* If the `friend' keyword was present, the friend must
23587 be introduced with a class-key. */
23588 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23589 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23590 "in C++03 a class-key must be used "
23591 "when declaring a friend");
23594 template <typename T> struct A {
23595 friend struct A<T>::B;
23598 A<T>::B will be represented by a TYPENAME_TYPE, and
23599 therefore not recognized by check_tag_decl. */
23602 type
= decl_specifiers
.type
;
23603 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23604 type
= TREE_TYPE (type
);
23606 if (!type
|| !TYPE_P (type
))
23607 error_at (decl_spec_token_start
->location
,
23608 "friend declaration does not name a class or "
23611 make_friend_class (current_class_type
, type
,
23612 /*complain=*/true);
23614 /* If there is no TYPE, an error message will already have
23616 else if (!type
|| type
== error_mark_node
)
23618 /* An anonymous aggregate has to be handled specially; such
23619 a declaration really declares a data member (with a
23620 particular type), as opposed to a nested class. */
23621 else if (ANON_AGGR_TYPE_P (type
))
23624 if (decl_specifiers
.storage_class
!= sc_none
)
23625 error_at (decl_spec_token_start
->location
,
23626 "a storage class on an anonymous aggregate "
23627 "in class scope is not allowed");
23629 /* Remove constructors and such from TYPE, now that we
23630 know it is an anonymous aggregate. */
23631 fixup_anonymous_aggr (type
);
23632 /* And make the corresponding data member. */
23633 decl
= build_decl (decl_spec_token_start
->location
,
23634 FIELD_DECL
, NULL_TREE
, type
);
23635 /* Add it to the class. */
23636 finish_member_declaration (decl
);
23639 cp_parser_check_access_in_redeclaration
23641 decl_spec_token_start
->location
);
23646 bool assume_semicolon
= false;
23648 /* Clear attributes from the decl_specifiers but keep them
23649 around as prefix attributes that apply them to the entity
23651 prefix_attributes
= decl_specifiers
.attributes
;
23652 decl_specifiers
.attributes
= NULL_TREE
;
23654 /* See if these declarations will be friends. */
23655 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23657 /* Keep going until we hit the `;' at the end of the
23659 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23661 tree attributes
= NULL_TREE
;
23662 tree first_attribute
;
23664 bool named_bitfld
= false;
23666 /* Peek at the next token. */
23667 token
= cp_lexer_peek_token (parser
->lexer
);
23669 /* The following code wants to know early if it is a bit-field
23670 or some other declaration. Attributes can appear before
23671 the `:' token. Skip over them without consuming any tokens
23672 to peek if they are followed by `:'. */
23673 if (cp_next_tokens_can_be_attribute_p (parser
)
23674 || (token
->type
== CPP_NAME
23675 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23676 && (named_bitfld
= true)))
23679 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23680 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23683 /* Check for a bitfield declaration. */
23684 if (token
->type
== CPP_COLON
23685 || (token
->type
== CPP_NAME
23686 && token
== cp_lexer_peek_token (parser
->lexer
)
23687 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23688 && (named_bitfld
= true)))
23692 tree late_attributes
= NULL_TREE
;
23695 identifier
= cp_parser_identifier (parser
);
23697 identifier
= NULL_TREE
;
23699 /* Look for attributes that apply to the bitfield. */
23700 attributes
= cp_parser_attributes_opt (parser
);
23702 /* Consume the `:' token. */
23703 cp_lexer_consume_token (parser
->lexer
);
23705 /* Get the width of the bitfield. */
23706 width
= cp_parser_constant_expression (parser
, false, NULL
,
23707 cxx_dialect
>= cxx11
);
23709 /* In C++2A and as extension for C++11 and above we allow
23710 default member initializers for bit-fields. */
23711 initializer
= NULL_TREE
;
23712 if (cxx_dialect
>= cxx11
23713 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23714 || cp_lexer_next_token_is (parser
->lexer
,
23718 = cp_lexer_peek_token (parser
->lexer
)->location
;
23719 if (cxx_dialect
< cxx2a
23720 && !in_system_header_at (loc
)
23721 && identifier
!= NULL_TREE
)
23723 "default member initializers for bit-fields "
23724 "only available with -std=c++2a or "
23727 initializer
= cp_parser_save_nsdmi (parser
);
23728 if (identifier
== NULL_TREE
)
23730 error_at (loc
, "default member initializer for "
23731 "unnamed bit-field");
23732 initializer
= NULL_TREE
;
23737 /* Look for attributes that apply to the bitfield after
23738 the `:' token and width. This is where GCC used to
23739 parse attributes in the past, pedwarn if there is
23740 a std attribute. */
23741 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23742 pedwarn (input_location
, OPT_Wpedantic
,
23743 "ISO C++ allows bit-field attributes only "
23744 "before the %<:%> token");
23746 late_attributes
= cp_parser_attributes_opt (parser
);
23749 attributes
= attr_chainon (attributes
, late_attributes
);
23751 /* Remember which attributes are prefix attributes and
23753 first_attribute
= attributes
;
23754 /* Combine the attributes. */
23755 attributes
= attr_chainon (prefix_attributes
, attributes
);
23757 /* Create the bitfield declaration. */
23758 decl
= grokbitfield (identifier
23759 ? make_id_declarator (NULL_TREE
,
23764 width
, initializer
,
23769 cp_declarator
*declarator
;
23770 tree asm_specification
;
23771 int ctor_dtor_or_conv_p
;
23773 /* Parse the declarator. */
23775 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23776 &ctor_dtor_or_conv_p
,
23777 /*parenthesized_p=*/NULL
,
23781 /* If something went wrong parsing the declarator, make sure
23782 that we at least consume some tokens. */
23783 if (declarator
== cp_error_declarator
)
23785 /* Skip to the end of the statement. */
23786 cp_parser_skip_to_end_of_statement (parser
);
23787 /* If the next token is not a semicolon, that is
23788 probably because we just skipped over the body of
23789 a function. So, we consume a semicolon if
23790 present, but do not issue an error message if it
23792 if (cp_lexer_next_token_is (parser
->lexer
,
23794 cp_lexer_consume_token (parser
->lexer
);
23798 if (declares_class_or_enum
& 2)
23799 cp_parser_check_for_definition_in_return_type
23800 (declarator
, decl_specifiers
.type
,
23801 decl_specifiers
.locations
[ds_type_spec
]);
23803 /* Look for an asm-specification. */
23804 asm_specification
= cp_parser_asm_specification_opt (parser
);
23805 /* Look for attributes that apply to the declaration. */
23806 attributes
= cp_parser_attributes_opt (parser
);
23807 /* Remember which attributes are prefix attributes and
23809 first_attribute
= attributes
;
23810 /* Combine the attributes. */
23811 attributes
= attr_chainon (prefix_attributes
, attributes
);
23813 /* If it's an `=', then we have a constant-initializer or a
23814 pure-specifier. It is not correct to parse the
23815 initializer before registering the member declaration
23816 since the member declaration should be in scope while
23817 its initializer is processed. However, the rest of the
23818 front end does not yet provide an interface that allows
23819 us to handle this correctly. */
23820 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23824 A pure-specifier shall be used only in the declaration of
23825 a virtual function.
23827 A member-declarator can contain a constant-initializer
23828 only if it declares a static member of integral or
23831 Therefore, if the DECLARATOR is for a function, we look
23832 for a pure-specifier; otherwise, we look for a
23833 constant-initializer. When we call `grokfield', it will
23834 perform more stringent semantics checks. */
23835 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23836 if (function_declarator_p (declarator
)
23837 || (decl_specifiers
.type
23838 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23839 && declarator
->kind
== cdk_id
23840 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23841 == FUNCTION_TYPE
)))
23842 initializer
= cp_parser_pure_specifier (parser
);
23843 else if (decl_specifiers
.storage_class
!= sc_static
)
23844 initializer
= cp_parser_save_nsdmi (parser
);
23845 else if (cxx_dialect
>= cxx11
)
23848 /* Don't require a constant rvalue in C++11, since we
23849 might want a reference constant. We'll enforce
23850 constancy later. */
23851 cp_lexer_consume_token (parser
->lexer
);
23852 /* Parse the initializer. */
23853 initializer
= cp_parser_initializer_clause (parser
,
23857 /* Parse the initializer. */
23858 initializer
= cp_parser_constant_initializer (parser
);
23860 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23861 && !function_declarator_p (declarator
))
23864 if (decl_specifiers
.storage_class
!= sc_static
)
23865 initializer
= cp_parser_save_nsdmi (parser
);
23867 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23869 /* Otherwise, there is no initializer. */
23871 initializer
= NULL_TREE
;
23873 /* See if we are probably looking at a function
23874 definition. We are certainly not looking at a
23875 member-declarator. Calling `grokfield' has
23876 side-effects, so we must not do it unless we are sure
23877 that we are looking at a member-declarator. */
23878 if (cp_parser_token_starts_function_definition_p
23879 (cp_lexer_peek_token (parser
->lexer
)))
23881 /* The grammar does not allow a pure-specifier to be
23882 used when a member function is defined. (It is
23883 possible that this fact is an oversight in the
23884 standard, since a pure function may be defined
23885 outside of the class-specifier. */
23886 if (initializer
&& initializer_token_start
)
23887 error_at (initializer_token_start
->location
,
23888 "pure-specifier on function-definition");
23889 decl
= cp_parser_save_member_function_body (parser
,
23893 if (parser
->fully_implicit_function_template_p
)
23894 decl
= finish_fully_implicit_template (parser
, decl
);
23895 /* If the member was not a friend, declare it here. */
23897 finish_member_declaration (decl
);
23898 /* Peek at the next token. */
23899 token
= cp_lexer_peek_token (parser
->lexer
);
23900 /* If the next token is a semicolon, consume it. */
23901 if (token
->type
== CPP_SEMICOLON
)
23903 location_t semicolon_loc
23904 = cp_lexer_consume_token (parser
->lexer
)->location
;
23905 gcc_rich_location
richloc (semicolon_loc
);
23906 richloc
.add_fixit_remove ();
23907 warning_at (&richloc
, OPT_Wextra_semi
,
23908 "extra %<;%> after in-class "
23909 "function definition");
23914 if (declarator
->kind
== cdk_function
)
23915 declarator
->id_loc
= token
->location
;
23916 /* Create the declaration. */
23917 decl
= grokfield (declarator
, &decl_specifiers
,
23918 initializer
, /*init_const_expr_p=*/true,
23919 asm_specification
, attributes
);
23920 if (parser
->fully_implicit_function_template_p
)
23923 finish_fully_implicit_template (parser
, 0);
23925 decl
= finish_fully_implicit_template (parser
, decl
);
23929 cp_finalize_omp_declare_simd (parser
, decl
);
23930 cp_finalize_oacc_routine (parser
, decl
, false);
23932 /* Reset PREFIX_ATTRIBUTES. */
23933 if (attributes
!= error_mark_node
)
23935 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23936 attributes
= TREE_CHAIN (attributes
);
23938 TREE_CHAIN (attributes
) = NULL_TREE
;
23941 /* If there is any qualification still in effect, clear it
23942 now; we will be starting fresh with the next declarator. */
23943 parser
->scope
= NULL_TREE
;
23944 parser
->qualifying_scope
= NULL_TREE
;
23945 parser
->object_scope
= NULL_TREE
;
23946 /* If it's a `,', then there are more declarators. */
23947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23949 cp_lexer_consume_token (parser
->lexer
);
23950 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23952 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23953 gcc_rich_location
richloc (token
->location
);
23954 richloc
.add_fixit_remove ();
23955 error_at (&richloc
, "stray %<,%> at end of "
23956 "member declaration");
23959 /* If the next token isn't a `;', then we have a parse error. */
23960 else if (cp_lexer_next_token_is_not (parser
->lexer
,
23963 /* The next token might be a ways away from where the
23964 actual semicolon is missing. Find the previous token
23965 and use that for our error position. */
23966 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23967 gcc_rich_location
richloc (token
->location
);
23968 richloc
.add_fixit_insert_after (";");
23969 error_at (&richloc
, "expected %<;%> at end of "
23970 "member declaration");
23972 /* Assume that the user meant to provide a semicolon. If
23973 we were to cp_parser_skip_to_end_of_statement, we might
23974 skip to a semicolon inside a member function definition
23975 and issue nonsensical error messages. */
23976 assume_semicolon
= true;
23981 /* Add DECL to the list of members. */
23983 /* Explicitly include, eg, NSDMIs, for better error
23984 recovery (c++/58650). */
23985 || !DECL_DECLARES_FUNCTION_P (decl
))
23986 finish_member_declaration (decl
);
23988 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23989 cp_parser_save_default_args (parser
, decl
);
23990 else if (TREE_CODE (decl
) == FIELD_DECL
23991 && DECL_INITIAL (decl
))
23992 /* Add DECL to the queue of NSDMI to be parsed later. */
23993 vec_safe_push (unparsed_nsdmis
, decl
);
23996 if (assume_semicolon
)
24001 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24003 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24006 /* Parse a pure-specifier.
24011 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24012 Otherwise, ERROR_MARK_NODE is returned. */
24015 cp_parser_pure_specifier (cp_parser
* parser
)
24019 /* Look for the `=' token. */
24020 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24021 return error_mark_node
;
24022 /* Look for the `0' token. */
24023 token
= cp_lexer_peek_token (parser
->lexer
);
24025 if (token
->type
== CPP_EOF
24026 || token
->type
== CPP_PRAGMA_EOL
)
24027 return error_mark_node
;
24029 cp_lexer_consume_token (parser
->lexer
);
24031 /* Accept = default or = delete in c++0x mode. */
24032 if (token
->keyword
== RID_DEFAULT
24033 || token
->keyword
== RID_DELETE
)
24035 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24036 return token
->u
.value
;
24039 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24040 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24042 cp_parser_error (parser
,
24043 "invalid pure specifier (only %<= 0%> is allowed)");
24044 cp_parser_skip_to_end_of_statement (parser
);
24045 return error_mark_node
;
24047 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24049 error_at (token
->location
, "templates may not be %<virtual%>");
24050 return error_mark_node
;
24053 return integer_zero_node
;
24056 /* Parse a constant-initializer.
24058 constant-initializer:
24059 = constant-expression
24061 Returns a representation of the constant-expression. */
24064 cp_parser_constant_initializer (cp_parser
* parser
)
24066 /* Look for the `=' token. */
24067 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24068 return error_mark_node
;
24070 /* It is invalid to write:
24072 struct S { static const int i = { 7 }; };
24075 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24077 cp_parser_error (parser
,
24078 "a brace-enclosed initializer is not allowed here");
24079 /* Consume the opening brace. */
24080 matching_braces braces
;
24081 braces
.consume_open (parser
);
24082 /* Skip the initializer. */
24083 cp_parser_skip_to_closing_brace (parser
);
24084 /* Look for the trailing `}'. */
24085 braces
.require_close (parser
);
24087 return error_mark_node
;
24090 return cp_parser_constant_expression (parser
);
24093 /* Derived classes [gram.class.derived] */
24095 /* Parse a base-clause.
24098 : base-specifier-list
24100 base-specifier-list:
24101 base-specifier ... [opt]
24102 base-specifier-list , base-specifier ... [opt]
24104 Returns a TREE_LIST representing the base-classes, in the order in
24105 which they were declared. The representation of each node is as
24106 described by cp_parser_base_specifier.
24108 In the case that no bases are specified, this function will return
24109 NULL_TREE, not ERROR_MARK_NODE. */
24112 cp_parser_base_clause (cp_parser
* parser
)
24114 tree bases
= NULL_TREE
;
24116 /* Look for the `:' that begins the list. */
24117 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24119 /* Scan the base-specifier-list. */
24124 bool pack_expansion_p
= false;
24126 /* Look for the base-specifier. */
24127 base
= cp_parser_base_specifier (parser
);
24128 /* Look for the (optional) ellipsis. */
24129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24131 /* Consume the `...'. */
24132 cp_lexer_consume_token (parser
->lexer
);
24134 pack_expansion_p
= true;
24137 /* Add BASE to the front of the list. */
24138 if (base
&& base
!= error_mark_node
)
24140 if (pack_expansion_p
)
24141 /* Make this a pack expansion type. */
24142 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24144 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24146 TREE_CHAIN (base
) = bases
;
24150 /* Peek at the next token. */
24151 token
= cp_lexer_peek_token (parser
->lexer
);
24152 /* If it's not a comma, then the list is complete. */
24153 if (token
->type
!= CPP_COMMA
)
24155 /* Consume the `,'. */
24156 cp_lexer_consume_token (parser
->lexer
);
24159 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24160 base class had a qualified name. However, the next name that
24161 appears is certainly not qualified. */
24162 parser
->scope
= NULL_TREE
;
24163 parser
->qualifying_scope
= NULL_TREE
;
24164 parser
->object_scope
= NULL_TREE
;
24166 return nreverse (bases
);
24169 /* Parse a base-specifier.
24172 :: [opt] nested-name-specifier [opt] class-name
24173 virtual access-specifier [opt] :: [opt] nested-name-specifier
24175 access-specifier virtual [opt] :: [opt] nested-name-specifier
24178 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24179 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24180 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24181 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24184 cp_parser_base_specifier (cp_parser
* parser
)
24188 bool virtual_p
= false;
24189 bool duplicate_virtual_error_issued_p
= false;
24190 bool duplicate_access_error_issued_p
= false;
24191 bool class_scope_p
, template_p
;
24192 tree access
= access_default_node
;
24195 /* Process the optional `virtual' and `access-specifier'. */
24198 /* Peek at the next token. */
24199 token
= cp_lexer_peek_token (parser
->lexer
);
24200 /* Process `virtual'. */
24201 switch (token
->keyword
)
24204 /* If `virtual' appears more than once, issue an error. */
24205 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24207 cp_parser_error (parser
,
24208 "%<virtual%> specified more than once in base-specifier");
24209 duplicate_virtual_error_issued_p
= true;
24214 /* Consume the `virtual' token. */
24215 cp_lexer_consume_token (parser
->lexer
);
24220 case RID_PROTECTED
:
24222 /* If more than one access specifier appears, issue an
24224 if (access
!= access_default_node
24225 && !duplicate_access_error_issued_p
)
24227 cp_parser_error (parser
,
24228 "more than one access specifier in base-specifier");
24229 duplicate_access_error_issued_p
= true;
24232 access
= ridpointers
[(int) token
->keyword
];
24234 /* Consume the access-specifier. */
24235 cp_lexer_consume_token (parser
->lexer
);
24244 /* It is not uncommon to see programs mechanically, erroneously, use
24245 the 'typename' keyword to denote (dependent) qualified types
24246 as base classes. */
24247 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24249 token
= cp_lexer_peek_token (parser
->lexer
);
24250 if (!processing_template_decl
)
24251 error_at (token
->location
,
24252 "keyword %<typename%> not allowed outside of templates");
24254 error_at (token
->location
,
24255 "keyword %<typename%> not allowed in this context "
24256 "(the base class is implicitly a type)");
24257 cp_lexer_consume_token (parser
->lexer
);
24260 /* Look for the optional `::' operator. */
24261 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24262 /* Look for the nested-name-specifier. The simplest way to
24267 The keyword `typename' is not permitted in a base-specifier or
24268 mem-initializer; in these contexts a qualified name that
24269 depends on a template-parameter is implicitly assumed to be a
24272 is to pretend that we have seen the `typename' keyword at this
24274 cp_parser_nested_name_specifier_opt (parser
,
24275 /*typename_keyword_p=*/true,
24276 /*check_dependency_p=*/true,
24278 /*is_declaration=*/true);
24279 /* If the base class is given by a qualified name, assume that names
24280 we see are type names or templates, as appropriate. */
24281 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24282 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24285 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24286 /* DR 950 allows decltype as a base-specifier. */
24287 type
= cp_parser_decltype (parser
);
24290 /* Otherwise, look for the class-name. */
24291 type
= cp_parser_class_name (parser
,
24295 /*check_dependency_p=*/true,
24296 /*class_head_p=*/false,
24297 /*is_declaration=*/true);
24298 type
= TREE_TYPE (type
);
24301 if (type
== error_mark_node
)
24302 return error_mark_node
;
24304 return finish_base_specifier (type
, access
, virtual_p
);
24307 /* Exception handling [gram.exception] */
24309 /* Parse an (optional) noexcept-specification.
24311 noexcept-specification:
24312 noexcept ( constant-expression ) [opt]
24314 If no noexcept-specification is present, returns NULL_TREE.
24315 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24316 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24317 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24318 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24319 in which case a boolean condition is returned instead. */
24322 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24323 bool require_constexpr
,
24324 bool* consumed_expr
,
24328 const char *saved_message
;
24330 /* Peek at the next token. */
24331 token
= cp_lexer_peek_token (parser
->lexer
);
24333 /* Is it a noexcept-specification? */
24334 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24337 cp_lexer_consume_token (parser
->lexer
);
24339 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24341 matching_parens parens
;
24342 parens
.consume_open (parser
);
24344 if (require_constexpr
)
24346 /* Types may not be defined in an exception-specification. */
24347 saved_message
= parser
->type_definition_forbidden_message
;
24348 parser
->type_definition_forbidden_message
24349 = G_("types may not be defined in an exception-specification");
24351 expr
= cp_parser_constant_expression (parser
);
24353 /* Restore the saved message. */
24354 parser
->type_definition_forbidden_message
= saved_message
;
24358 expr
= cp_parser_expression (parser
);
24359 *consumed_expr
= true;
24362 parens
.require_close (parser
);
24366 expr
= boolean_true_node
;
24367 if (!require_constexpr
)
24368 *consumed_expr
= false;
24371 /* We cannot build a noexcept-spec right away because this will check
24372 that expr is a constexpr. */
24374 return build_noexcept_spec (expr
, tf_warning_or_error
);
24382 /* Parse an (optional) exception-specification.
24384 exception-specification:
24385 throw ( type-id-list [opt] )
24387 Returns a TREE_LIST representing the exception-specification. The
24388 TREE_VALUE of each node is a type. */
24391 cp_parser_exception_specification_opt (cp_parser
* parser
)
24395 const char *saved_message
;
24397 /* Peek at the next token. */
24398 token
= cp_lexer_peek_token (parser
->lexer
);
24400 /* Is it a noexcept-specification? */
24401 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24403 if (type_id_list
!= NULL_TREE
)
24404 return type_id_list
;
24406 /* If it's not `throw', then there's no exception-specification. */
24407 if (!cp_parser_is_keyword (token
, RID_THROW
))
24410 location_t loc
= token
->location
;
24412 /* Consume the `throw'. */
24413 cp_lexer_consume_token (parser
->lexer
);
24415 /* Look for the `('. */
24416 matching_parens parens
;
24417 parens
.require_open (parser
);
24419 /* Peek at the next token. */
24420 token
= cp_lexer_peek_token (parser
->lexer
);
24421 /* If it's not a `)', then there is a type-id-list. */
24422 if (token
->type
!= CPP_CLOSE_PAREN
)
24424 /* Types may not be defined in an exception-specification. */
24425 saved_message
= parser
->type_definition_forbidden_message
;
24426 parser
->type_definition_forbidden_message
24427 = G_("types may not be defined in an exception-specification");
24428 /* Parse the type-id-list. */
24429 type_id_list
= cp_parser_type_id_list (parser
);
24430 /* Restore the saved message. */
24431 parser
->type_definition_forbidden_message
= saved_message
;
24433 if (cxx_dialect
>= cxx17
)
24435 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24437 type_id_list
= NULL_TREE
;
24439 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24440 warning_at (loc
, OPT_Wdeprecated
,
24441 "dynamic exception specifications are deprecated in "
24444 /* In C++17, throw() is equivalent to noexcept (true). throw()
24445 is deprecated in C++11 and above as well, but is still widely used,
24446 so don't warn about it yet. */
24447 else if (cxx_dialect
>= cxx17
)
24448 type_id_list
= noexcept_true_spec
;
24450 type_id_list
= empty_except_spec
;
24452 /* Look for the `)'. */
24453 parens
.require_close (parser
);
24455 return type_id_list
;
24458 /* Parse an (optional) type-id-list.
24462 type-id-list , type-id ... [opt]
24464 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24465 in the order that the types were presented. */
24468 cp_parser_type_id_list (cp_parser
* parser
)
24470 tree types
= NULL_TREE
;
24477 token
= cp_lexer_peek_token (parser
->lexer
);
24479 /* Get the next type-id. */
24480 type
= cp_parser_type_id (parser
);
24481 /* Check for invalid 'auto'. */
24482 if (flag_concepts
&& type_uses_auto (type
))
24484 error_at (token
->location
,
24485 "invalid use of %<auto%> in exception-specification");
24486 type
= error_mark_node
;
24488 /* Parse the optional ellipsis. */
24489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24491 /* Consume the `...'. */
24492 cp_lexer_consume_token (parser
->lexer
);
24494 /* Turn the type into a pack expansion expression. */
24495 type
= make_pack_expansion (type
);
24497 /* Add it to the list. */
24498 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24499 /* Peek at the next token. */
24500 token
= cp_lexer_peek_token (parser
->lexer
);
24501 /* If it is not a `,', we are done. */
24502 if (token
->type
!= CPP_COMMA
)
24504 /* Consume the `,'. */
24505 cp_lexer_consume_token (parser
->lexer
);
24508 return nreverse (types
);
24511 /* Parse a try-block.
24514 try compound-statement handler-seq */
24517 cp_parser_try_block (cp_parser
* parser
)
24521 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24522 if (parser
->in_function_body
24523 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24524 error ("%<try%> in %<constexpr%> function");
24526 try_block
= begin_try_block ();
24527 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24528 finish_try_block (try_block
);
24529 cp_parser_handler_seq (parser
);
24530 finish_handler_sequence (try_block
);
24535 /* Parse a function-try-block.
24537 function-try-block:
24538 try ctor-initializer [opt] function-body handler-seq */
24541 cp_parser_function_try_block (cp_parser
* parser
)
24543 tree compound_stmt
;
24546 /* Look for the `try' keyword. */
24547 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24549 /* Let the rest of the front end know where we are. */
24550 try_block
= begin_function_try_block (&compound_stmt
);
24551 /* Parse the function-body. */
24552 cp_parser_ctor_initializer_opt_and_function_body
24553 (parser
, /*in_function_try_block=*/true);
24554 /* We're done with the `try' part. */
24555 finish_function_try_block (try_block
);
24556 /* Parse the handlers. */
24557 cp_parser_handler_seq (parser
);
24558 /* We're done with the handlers. */
24559 finish_function_handler_sequence (try_block
, compound_stmt
);
24562 /* Parse a handler-seq.
24565 handler handler-seq [opt] */
24568 cp_parser_handler_seq (cp_parser
* parser
)
24574 /* Parse the handler. */
24575 cp_parser_handler (parser
);
24576 /* Peek at the next token. */
24577 token
= cp_lexer_peek_token (parser
->lexer
);
24578 /* If it's not `catch' then there are no more handlers. */
24579 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24584 /* Parse a handler.
24587 catch ( exception-declaration ) compound-statement */
24590 cp_parser_handler (cp_parser
* parser
)
24595 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24596 handler
= begin_handler ();
24597 matching_parens parens
;
24598 parens
.require_open (parser
);
24599 declaration
= cp_parser_exception_declaration (parser
);
24600 finish_handler_parms (declaration
, handler
);
24601 parens
.require_close (parser
);
24602 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24603 finish_handler (handler
);
24606 /* Parse an exception-declaration.
24608 exception-declaration:
24609 type-specifier-seq declarator
24610 type-specifier-seq abstract-declarator
24614 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24615 ellipsis variant is used. */
24618 cp_parser_exception_declaration (cp_parser
* parser
)
24620 cp_decl_specifier_seq type_specifiers
;
24621 cp_declarator
*declarator
;
24622 const char *saved_message
;
24624 /* If it's an ellipsis, it's easy to handle. */
24625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24627 /* Consume the `...' token. */
24628 cp_lexer_consume_token (parser
->lexer
);
24632 /* Types may not be defined in exception-declarations. */
24633 saved_message
= parser
->type_definition_forbidden_message
;
24634 parser
->type_definition_forbidden_message
24635 = G_("types may not be defined in exception-declarations");
24637 /* Parse the type-specifier-seq. */
24638 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24639 /*is_trailing_return=*/false,
24641 /* If it's a `)', then there is no declarator. */
24642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24645 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24646 /*ctor_dtor_or_conv_p=*/NULL
,
24647 /*parenthesized_p=*/NULL
,
24648 /*member_p=*/false,
24649 /*friend_p=*/false);
24651 /* Restore the saved message. */
24652 parser
->type_definition_forbidden_message
= saved_message
;
24654 if (!type_specifiers
.any_specifiers_p
)
24655 return error_mark_node
;
24657 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24660 /* Parse a throw-expression.
24663 throw assignment-expression [opt]
24665 Returns a THROW_EXPR representing the throw-expression. */
24668 cp_parser_throw_expression (cp_parser
* parser
)
24673 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24674 token
= cp_lexer_peek_token (parser
->lexer
);
24675 /* Figure out whether or not there is an assignment-expression
24676 following the "throw" keyword. */
24677 if (token
->type
== CPP_COMMA
24678 || token
->type
== CPP_SEMICOLON
24679 || token
->type
== CPP_CLOSE_PAREN
24680 || token
->type
== CPP_CLOSE_SQUARE
24681 || token
->type
== CPP_CLOSE_BRACE
24682 || token
->type
== CPP_COLON
)
24683 expression
= NULL_TREE
;
24685 expression
= cp_parser_assignment_expression (parser
);
24687 return build_throw (expression
);
24690 /* GNU Extensions */
24692 /* Parse an (optional) asm-specification.
24695 asm ( string-literal )
24697 If the asm-specification is present, returns a STRING_CST
24698 corresponding to the string-literal. Otherwise, returns
24702 cp_parser_asm_specification_opt (cp_parser
* parser
)
24705 tree asm_specification
;
24707 /* Peek at the next token. */
24708 token
= cp_lexer_peek_token (parser
->lexer
);
24709 /* If the next token isn't the `asm' keyword, then there's no
24710 asm-specification. */
24711 if (!cp_parser_is_keyword (token
, RID_ASM
))
24714 /* Consume the `asm' token. */
24715 cp_lexer_consume_token (parser
->lexer
);
24716 /* Look for the `('. */
24717 matching_parens parens
;
24718 parens
.require_open (parser
);
24720 /* Look for the string-literal. */
24721 asm_specification
= cp_parser_string_literal (parser
, false, false);
24723 /* Look for the `)'. */
24724 parens
.require_close (parser
);
24726 return asm_specification
;
24729 /* Parse an asm-operand-list.
24733 asm-operand-list , asm-operand
24736 string-literal ( expression )
24737 [ string-literal ] string-literal ( expression )
24739 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24740 each node is the expression. The TREE_PURPOSE is itself a
24741 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24742 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24743 is a STRING_CST for the string literal before the parenthesis. Returns
24744 ERROR_MARK_NODE if any of the operands are invalid. */
24747 cp_parser_asm_operand_list (cp_parser
* parser
)
24749 tree asm_operands
= NULL_TREE
;
24750 bool invalid_operands
= false;
24754 tree string_literal
;
24758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24760 /* Consume the `[' token. */
24761 cp_lexer_consume_token (parser
->lexer
);
24762 /* Read the operand name. */
24763 name
= cp_parser_identifier (parser
);
24764 if (name
!= error_mark_node
)
24765 name
= build_string (IDENTIFIER_LENGTH (name
),
24766 IDENTIFIER_POINTER (name
));
24767 /* Look for the closing `]'. */
24768 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24772 /* Look for the string-literal. */
24773 string_literal
= cp_parser_string_literal (parser
, false, false);
24775 /* Look for the `('. */
24776 matching_parens parens
;
24777 parens
.require_open (parser
);
24778 /* Parse the expression. */
24779 expression
= cp_parser_expression (parser
);
24780 /* Look for the `)'. */
24781 parens
.require_close (parser
);
24783 if (name
== error_mark_node
24784 || string_literal
== error_mark_node
24785 || expression
== error_mark_node
)
24786 invalid_operands
= true;
24788 /* Add this operand to the list. */
24789 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24792 /* If the next token is not a `,', there are no more
24794 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24796 /* Consume the `,'. */
24797 cp_lexer_consume_token (parser
->lexer
);
24800 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24803 /* Parse an asm-clobber-list.
24807 asm-clobber-list , string-literal
24809 Returns a TREE_LIST, indicating the clobbers in the order that they
24810 appeared. The TREE_VALUE of each node is a STRING_CST. */
24813 cp_parser_asm_clobber_list (cp_parser
* parser
)
24815 tree clobbers
= NULL_TREE
;
24819 tree string_literal
;
24821 /* Look for the string literal. */
24822 string_literal
= cp_parser_string_literal (parser
, false, false);
24823 /* Add it to the list. */
24824 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24825 /* If the next token is not a `,', then the list is
24827 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24829 /* Consume the `,' token. */
24830 cp_lexer_consume_token (parser
->lexer
);
24836 /* Parse an asm-label-list.
24840 asm-label-list , identifier
24842 Returns a TREE_LIST, indicating the labels in the order that they
24843 appeared. The TREE_VALUE of each node is a label. */
24846 cp_parser_asm_label_list (cp_parser
* parser
)
24848 tree labels
= NULL_TREE
;
24852 tree identifier
, label
, name
;
24854 /* Look for the identifier. */
24855 identifier
= cp_parser_identifier (parser
);
24856 if (!error_operand_p (identifier
))
24858 label
= lookup_label (identifier
);
24859 if (TREE_CODE (label
) == LABEL_DECL
)
24861 TREE_USED (label
) = 1;
24862 check_goto (label
);
24863 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24864 IDENTIFIER_POINTER (identifier
));
24865 labels
= tree_cons (name
, label
, labels
);
24868 /* If the next token is not a `,', then the list is
24870 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24872 /* Consume the `,' token. */
24873 cp_lexer_consume_token (parser
->lexer
);
24876 return nreverse (labels
);
24879 /* Return TRUE iff the next tokens in the stream are possibly the
24880 beginning of a GNU extension attribute. */
24883 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24885 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24888 /* Return TRUE iff the next tokens in the stream are possibly the
24889 beginning of a standard C++-11 attribute specifier. */
24892 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24894 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24897 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24898 beginning of a standard C++-11 attribute specifier. */
24901 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24903 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24905 return (cxx_dialect
>= cxx11
24906 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24907 || (token
->type
== CPP_OPEN_SQUARE
24908 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24909 && token
->type
== CPP_OPEN_SQUARE
)));
24912 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24913 beginning of a GNU extension attribute. */
24916 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24918 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24920 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24923 /* Return true iff the next tokens can be the beginning of either a
24924 GNU attribute list, or a standard C++11 attribute sequence. */
24927 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24929 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24930 || cp_next_tokens_can_be_std_attribute_p (parser
));
24933 /* Return true iff the next Nth tokens can be the beginning of either
24934 a GNU attribute list, or a standard C++11 attribute sequence. */
24937 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24939 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24940 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24943 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24944 of GNU attributes, or return NULL. */
24947 cp_parser_attributes_opt (cp_parser
*parser
)
24949 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24950 return cp_parser_gnu_attributes_opt (parser
);
24951 return cp_parser_std_attribute_spec_seq (parser
);
24954 /* Parse an (optional) series of attributes.
24957 attributes attribute
24960 __attribute__ (( attribute-list [opt] ))
24962 The return value is as for cp_parser_gnu_attribute_list. */
24965 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
24967 tree attributes
= NULL_TREE
;
24972 tree attribute_list
;
24975 /* Peek at the next token. */
24976 token
= cp_lexer_peek_token (parser
->lexer
);
24977 /* If it's not `__attribute__', then we're done. */
24978 if (token
->keyword
!= RID_ATTRIBUTE
)
24981 /* Consume the `__attribute__' keyword. */
24982 cp_lexer_consume_token (parser
->lexer
);
24983 /* Look for the two `(' tokens. */
24984 matching_parens outer_parens
;
24985 outer_parens
.require_open (parser
);
24986 matching_parens inner_parens
;
24987 inner_parens
.require_open (parser
);
24989 /* Peek at the next token. */
24990 token
= cp_lexer_peek_token (parser
->lexer
);
24991 if (token
->type
!= CPP_CLOSE_PAREN
)
24992 /* Parse the attribute-list. */
24993 attribute_list
= cp_parser_gnu_attribute_list (parser
);
24995 /* If the next token is a `)', then there is no attribute
24997 attribute_list
= NULL
;
24999 /* Look for the two `)' tokens. */
25000 if (!inner_parens
.require_close (parser
))
25002 if (!outer_parens
.require_close (parser
))
25005 cp_parser_skip_to_end_of_statement (parser
);
25007 /* Add these new attributes to the list. */
25008 attributes
= attr_chainon (attributes
, attribute_list
);
25014 /* Parse a GNU attribute-list.
25018 attribute-list , attribute
25022 identifier ( identifier )
25023 identifier ( identifier , expression-list )
25024 identifier ( expression-list )
25026 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25027 to an attribute. The TREE_PURPOSE of each node is the identifier
25028 indicating which attribute is in use. The TREE_VALUE represents
25029 the arguments, if any. */
25032 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25034 tree attribute_list
= NULL_TREE
;
25035 bool save_translate_strings_p
= parser
->translate_strings_p
;
25037 parser
->translate_strings_p
= false;
25044 /* Look for the identifier. We also allow keywords here; for
25045 example `__attribute__ ((const))' is legal. */
25046 token
= cp_lexer_peek_token (parser
->lexer
);
25047 if (token
->type
== CPP_NAME
25048 || token
->type
== CPP_KEYWORD
)
25050 tree arguments
= NULL_TREE
;
25052 /* Consume the token, but save it since we need it for the
25053 SIMD enabled function parsing. */
25054 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25056 /* Save away the identifier that indicates which attribute
25058 identifier
= (token
->type
== CPP_KEYWORD
)
25059 /* For keywords, use the canonical spelling, not the
25060 parsed identifier. */
25061 ? ridpointers
[(int) token
->keyword
]
25062 : id_token
->u
.value
;
25064 identifier
= canonicalize_attr_name (identifier
);
25065 attribute
= build_tree_list (identifier
, NULL_TREE
);
25067 /* Peek at the next token. */
25068 token
= cp_lexer_peek_token (parser
->lexer
);
25069 /* If it's an `(', then parse the attribute arguments. */
25070 if (token
->type
== CPP_OPEN_PAREN
)
25072 vec
<tree
, va_gc
> *vec
;
25073 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25074 ? id_attr
: normal_attr
);
25075 vec
= cp_parser_parenthesized_expression_list
25076 (parser
, attr_flag
, /*cast_p=*/false,
25077 /*allow_expansion_p=*/false,
25078 /*non_constant_p=*/NULL
);
25080 arguments
= error_mark_node
;
25083 arguments
= build_tree_list_vec (vec
);
25084 release_tree_vector (vec
);
25086 /* Save the arguments away. */
25087 TREE_VALUE (attribute
) = arguments
;
25090 if (arguments
!= error_mark_node
)
25092 /* Add this attribute to the list. */
25093 TREE_CHAIN (attribute
) = attribute_list
;
25094 attribute_list
= attribute
;
25097 token
= cp_lexer_peek_token (parser
->lexer
);
25099 /* Now, look for more attributes. If the next token isn't a
25100 `,', we're done. */
25101 if (token
->type
!= CPP_COMMA
)
25104 /* Consume the comma and keep going. */
25105 cp_lexer_consume_token (parser
->lexer
);
25107 parser
->translate_strings_p
= save_translate_strings_p
;
25109 /* We built up the list in reverse order. */
25110 return nreverse (attribute_list
);
25113 /* Parse a standard C++11 attribute.
25115 The returned representation is a TREE_LIST which TREE_PURPOSE is
25116 the scoped name of the attribute, and the TREE_VALUE is its
25119 Note that the scoped name of the attribute is itself a TREE_LIST
25120 which TREE_PURPOSE is the namespace of the attribute, and
25121 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25122 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25123 and which TREE_PURPOSE is directly the attribute name.
25125 Clients of the attribute code should use get_attribute_namespace
25126 and get_attribute_name to get the actual namespace and name of
25127 attributes, regardless of their being GNU or C++11 attributes.
25130 attribute-token attribute-argument-clause [opt]
25134 attribute-scoped-token
25136 attribute-scoped-token:
25137 attribute-namespace :: identifier
25139 attribute-namespace:
25142 attribute-argument-clause:
25143 ( balanced-token-seq )
25145 balanced-token-seq:
25146 balanced-token [opt]
25147 balanced-token-seq balanced-token
25150 ( balanced-token-seq )
25151 [ balanced-token-seq ]
25152 { balanced-token-seq }. */
25155 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25157 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25160 /* First, parse name of the attribute, a.k.a attribute-token. */
25162 token
= cp_lexer_peek_token (parser
->lexer
);
25163 if (token
->type
== CPP_NAME
)
25164 attr_id
= token
->u
.value
;
25165 else if (token
->type
== CPP_KEYWORD
)
25166 attr_id
= ridpointers
[(int) token
->keyword
];
25167 else if (token
->flags
& NAMED_OP
)
25168 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25170 if (attr_id
== NULL_TREE
)
25173 cp_lexer_consume_token (parser
->lexer
);
25175 token
= cp_lexer_peek_token (parser
->lexer
);
25176 if (token
->type
== CPP_SCOPE
)
25178 /* We are seeing a scoped attribute token. */
25180 cp_lexer_consume_token (parser
->lexer
);
25182 error_at (token
->location
, "attribute using prefix used together "
25183 "with scoped attribute token");
25186 token
= cp_lexer_consume_token (parser
->lexer
);
25187 if (token
->type
== CPP_NAME
)
25188 attr_id
= token
->u
.value
;
25189 else if (token
->type
== CPP_KEYWORD
)
25190 attr_id
= ridpointers
[(int) token
->keyword
];
25191 else if (token
->flags
& NAMED_OP
)
25192 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25195 error_at (token
->location
,
25196 "expected an identifier for the attribute name");
25197 return error_mark_node
;
25200 attr_id
= canonicalize_attr_name (attr_id
);
25201 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25203 token
= cp_lexer_peek_token (parser
->lexer
);
25206 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25210 attr_id
= canonicalize_attr_name (attr_id
);
25211 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25213 /* C++11 noreturn attribute is equivalent to GNU's. */
25214 if (is_attribute_p ("noreturn", attr_id
))
25215 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25216 /* C++14 deprecated attribute is equivalent to GNU's. */
25217 else if (is_attribute_p ("deprecated", attr_id
))
25218 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25219 /* C++17 fallthrough attribute is equivalent to GNU's. */
25220 else if (is_attribute_p ("fallthrough", attr_id
))
25221 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25222 /* Transactional Memory TS optimize_for_synchronized attribute is
25223 equivalent to GNU transaction_callable. */
25224 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25225 TREE_PURPOSE (attribute
)
25226 = get_identifier ("transaction_callable");
25227 /* Transactional Memory attributes are GNU attributes. */
25228 else if (tm_attr_to_mask (attr_id
))
25229 TREE_PURPOSE (attribute
) = attr_id
;
25232 /* Now parse the optional argument clause of the attribute. */
25234 if (token
->type
!= CPP_OPEN_PAREN
)
25238 vec
<tree
, va_gc
> *vec
;
25239 int attr_flag
= normal_attr
;
25241 if (attr_ns
== get_identifier ("gnu")
25242 && attribute_takes_identifier_p (attr_id
))
25243 /* A GNU attribute that takes an identifier in parameter. */
25244 attr_flag
= id_attr
;
25246 vec
= cp_parser_parenthesized_expression_list
25247 (parser
, attr_flag
, /*cast_p=*/false,
25248 /*allow_expansion_p=*/true,
25249 /*non_constant_p=*/NULL
);
25251 arguments
= error_mark_node
;
25254 arguments
= build_tree_list_vec (vec
);
25255 release_tree_vector (vec
);
25258 if (arguments
== error_mark_node
)
25259 attribute
= error_mark_node
;
25261 TREE_VALUE (attribute
) = arguments
;
25267 /* Check that the attribute ATTRIBUTE appears at most once in the
25268 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25269 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25270 isn't implemented yet in GCC. */
25273 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25277 tree name
= get_attribute_name (attribute
);
25278 if (is_attribute_p ("noreturn", name
)
25279 && lookup_attribute ("noreturn", attributes
))
25280 error ("attribute %<noreturn%> can appear at most once "
25281 "in an attribute-list");
25282 else if (is_attribute_p ("deprecated", name
)
25283 && lookup_attribute ("deprecated", attributes
))
25284 error ("attribute %<deprecated%> can appear at most once "
25285 "in an attribute-list");
25289 /* Parse a list of standard C++-11 attributes.
25293 attribute-list , attribute[opt]
25295 attribute-list , attribute ...
25299 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25301 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25302 cp_token
*token
= NULL
;
25306 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25307 if (attribute
== error_mark_node
)
25309 if (attribute
!= NULL_TREE
)
25311 cp_parser_check_std_attribute (attributes
, attribute
);
25312 TREE_CHAIN (attribute
) = attributes
;
25313 attributes
= attribute
;
25315 token
= cp_lexer_peek_token (parser
->lexer
);
25316 if (token
->type
== CPP_ELLIPSIS
)
25318 cp_lexer_consume_token (parser
->lexer
);
25319 if (attribute
== NULL_TREE
)
25320 error_at (token
->location
,
25321 "expected attribute before %<...%>");
25324 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25325 if (pack
== error_mark_node
)
25326 return error_mark_node
;
25327 TREE_VALUE (attribute
) = pack
;
25329 token
= cp_lexer_peek_token (parser
->lexer
);
25331 if (token
->type
!= CPP_COMMA
)
25333 cp_lexer_consume_token (parser
->lexer
);
25335 attributes
= nreverse (attributes
);
25339 /* Parse a standard C++-11 attribute specifier.
25341 attribute-specifier:
25342 [ [ attribute-using-prefix [opt] attribute-list ] ]
25343 alignment-specifier
25345 attribute-using-prefix:
25346 using attribute-namespace :
25348 alignment-specifier:
25349 alignas ( type-id ... [opt] )
25350 alignas ( alignment-expression ... [opt] ). */
25353 cp_parser_std_attribute_spec (cp_parser
*parser
)
25355 tree attributes
= NULL_TREE
;
25356 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25358 if (token
->type
== CPP_OPEN_SQUARE
25359 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25361 tree attr_ns
= NULL_TREE
;
25363 cp_lexer_consume_token (parser
->lexer
);
25364 cp_lexer_consume_token (parser
->lexer
);
25366 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25368 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25369 if (token
->type
== CPP_NAME
)
25370 attr_ns
= token
->u
.value
;
25371 else if (token
->type
== CPP_KEYWORD
)
25372 attr_ns
= ridpointers
[(int) token
->keyword
];
25373 else if (token
->flags
& NAMED_OP
)
25374 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25377 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25379 if (cxx_dialect
< cxx17
25380 && !in_system_header_at (input_location
))
25381 pedwarn (input_location
, 0,
25382 "attribute using prefix only available "
25383 "with -std=c++17 or -std=gnu++17");
25385 cp_lexer_consume_token (parser
->lexer
);
25386 cp_lexer_consume_token (parser
->lexer
);
25387 cp_lexer_consume_token (parser
->lexer
);
25390 attr_ns
= NULL_TREE
;
25393 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25395 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25396 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25397 cp_parser_skip_to_end_of_statement (parser
);
25399 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25400 when we are sure that we have actually parsed them. */
25401 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25407 /* Look for an alignment-specifier. */
25409 token
= cp_lexer_peek_token (parser
->lexer
);
25411 if (token
->type
!= CPP_KEYWORD
25412 || token
->keyword
!= RID_ALIGNAS
)
25415 cp_lexer_consume_token (parser
->lexer
);
25416 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25418 matching_parens parens
;
25419 if (!parens
.require_open (parser
))
25420 return error_mark_node
;
25422 cp_parser_parse_tentatively (parser
);
25423 alignas_expr
= cp_parser_type_id (parser
);
25425 if (!cp_parser_parse_definitely (parser
))
25427 alignas_expr
= cp_parser_assignment_expression (parser
);
25428 if (alignas_expr
== error_mark_node
)
25429 cp_parser_skip_to_end_of_statement (parser
);
25430 if (alignas_expr
== NULL_TREE
25431 || alignas_expr
== error_mark_node
)
25432 return alignas_expr
;
25435 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25436 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25438 /* Handle alignas (pack...). */
25439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25441 cp_lexer_consume_token (parser
->lexer
);
25442 alignas_expr
= make_pack_expansion (alignas_expr
);
25445 /* Something went wrong, so don't build the attribute. */
25446 if (alignas_expr
== error_mark_node
)
25447 return error_mark_node
;
25449 if (!parens
.require_close (parser
))
25450 return error_mark_node
;
25452 /* Build the C++-11 representation of an 'aligned'
25455 build_tree_list (build_tree_list (get_identifier ("gnu"),
25456 get_identifier ("aligned")),
25463 /* Parse a standard C++-11 attribute-specifier-seq.
25465 attribute-specifier-seq:
25466 attribute-specifier-seq [opt] attribute-specifier
25470 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25472 tree attr_specs
= NULL_TREE
;
25473 tree attr_last
= NULL_TREE
;
25477 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25478 if (attr_spec
== NULL_TREE
)
25480 if (attr_spec
== error_mark_node
)
25481 return error_mark_node
;
25484 TREE_CHAIN (attr_last
) = attr_spec
;
25486 attr_specs
= attr_last
= attr_spec
;
25487 attr_last
= tree_last (attr_last
);
25493 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25494 return index of the first token after balanced-token, or N on failure. */
25497 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25500 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25502 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25505 case CPP_PRAGMA_EOL
:
25506 /* Ran out of tokens. */
25508 case CPP_OPEN_PAREN
:
25511 case CPP_OPEN_BRACE
:
25514 case CPP_OPEN_SQUARE
:
25517 case CPP_CLOSE_PAREN
:
25520 case CPP_CLOSE_BRACE
:
25523 case CPP_CLOSE_SQUARE
:
25529 while (nparens
|| nbraces
|| nsquares
);
25533 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25534 return index of the first token after the GNU attribute tokens, or N on
25538 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25542 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25543 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25544 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25547 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25550 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25557 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25558 next token), return index of the first token after the standard C++11
25559 attribute tokens, or N on failure. */
25562 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25566 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25567 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25569 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25572 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25576 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25577 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25579 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25590 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25591 as the next token), return index of the first token after the attribute
25592 tokens, or N on failure. */
25595 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25597 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25598 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25599 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25602 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25603 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25604 current value of the PEDANTIC flag, regardless of whether or not
25605 the `__extension__' keyword is present. The caller is responsible
25606 for restoring the value of the PEDANTIC flag. */
25609 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25611 /* Save the old value of the PEDANTIC flag. */
25612 *saved_pedantic
= pedantic
;
25614 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25616 /* Consume the `__extension__' token. */
25617 cp_lexer_consume_token (parser
->lexer
);
25618 /* We're not being pedantic while the `__extension__' keyword is
25628 /* Parse a label declaration.
25631 __label__ label-declarator-seq ;
25633 label-declarator-seq:
25634 identifier , label-declarator-seq
25638 cp_parser_label_declaration (cp_parser
* parser
)
25640 /* Look for the `__label__' keyword. */
25641 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25647 /* Look for an identifier. */
25648 identifier
= cp_parser_identifier (parser
);
25649 /* If we failed, stop. */
25650 if (identifier
== error_mark_node
)
25652 /* Declare it as a label. */
25653 finish_label_decl (identifier
);
25654 /* If the next token is a `;', stop. */
25655 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25657 /* Look for the `,' separating the label declarations. */
25658 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25661 /* Look for the final `;'. */
25662 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25665 // -------------------------------------------------------------------------- //
25668 // Parse a requires clause.
25670 // requires-clause:
25671 // 'requires' logical-or-expression
25673 // The required logical-or-expression must be a constant expression. Note
25674 // that we don't check that the expression is constepxr here. We defer until
25675 // we analyze constraints and then, we only check atomic constraints.
25677 cp_parser_requires_clause (cp_parser
*parser
)
25679 // Parse the requires clause so that it is not automatically folded.
25680 ++processing_template_decl
;
25681 tree expr
= cp_parser_binary_expression (parser
, false, false,
25682 PREC_NOT_OPERATOR
, NULL
);
25683 if (check_for_bare_parameter_packs (expr
))
25684 expr
= error_mark_node
;
25685 --processing_template_decl
;
25689 // Optionally parse a requires clause:
25691 cp_parser_requires_clause_opt (cp_parser
*parser
)
25693 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25694 if (tok
->keyword
!= RID_REQUIRES
)
25696 if (!flag_concepts
&& tok
->type
== CPP_NAME
25697 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25699 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25700 "%<requires%> only available with -fconcepts");
25701 /* Parse and discard the requires-clause. */
25702 cp_lexer_consume_token (parser
->lexer
);
25703 cp_parser_requires_clause (parser
);
25707 cp_lexer_consume_token (parser
->lexer
);
25708 return cp_parser_requires_clause (parser
);
25712 /*---------------------------------------------------------------------------
25713 Requires expressions
25714 ---------------------------------------------------------------------------*/
25716 /* Parse a requires expression
25718 requirement-expression:
25719 'requires' requirement-parameter-list [opt] requirement-body */
25721 cp_parser_requires_expression (cp_parser
*parser
)
25723 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25724 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25726 /* A requires-expression shall appear only within a concept
25727 definition or a requires-clause.
25729 TODO: Implement this diagnostic correctly. */
25730 if (!processing_template_decl
)
25732 error_at (loc
, "a requires expression cannot appear outside a template");
25733 cp_parser_skip_to_end_of_statement (parser
);
25734 return error_mark_node
;
25739 /* Local parameters are delared as variables within the scope
25740 of the expression. They are not visible past the end of
25741 the expression. Expressions within the requires-expression
25742 are unevaluated. */
25743 struct scope_sentinel
25747 ++cp_unevaluated_operand
;
25748 begin_scope (sk_block
, NULL_TREE
);
25753 pop_bindings_and_leave_scope ();
25754 --cp_unevaluated_operand
;
25758 /* Parse the optional parameter list. */
25759 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25761 parms
= cp_parser_requirement_parameter_list (parser
);
25762 if (parms
== error_mark_node
)
25763 return error_mark_node
;
25768 /* Parse the requirement body. */
25769 reqs
= cp_parser_requirement_body (parser
);
25770 if (reqs
== error_mark_node
)
25771 return error_mark_node
;
25774 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25776 grokparms (parms
, &parms
);
25777 return finish_requires_expr (parms
, reqs
);
25780 /* Parse a parameterized requirement.
25782 requirement-parameter-list:
25783 '(' parameter-declaration-clause ')' */
25785 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25787 matching_parens parens
;
25788 if (!parens
.require_open (parser
))
25789 return error_mark_node
;
25791 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25793 if (!parens
.require_close (parser
))
25794 return error_mark_node
;
25799 /* Parse the body of a requirement.
25802 '{' requirement-list '}' */
25804 cp_parser_requirement_body (cp_parser
*parser
)
25806 matching_braces braces
;
25807 if (!braces
.require_open (parser
))
25808 return error_mark_node
;
25810 tree reqs
= cp_parser_requirement_list (parser
);
25812 if (!braces
.require_close (parser
))
25813 return error_mark_node
;
25818 /* Parse a list of requirements.
25822 requirement-list ';' requirement[opt] */
25824 cp_parser_requirement_list (cp_parser
*parser
)
25826 tree result
= NULL_TREE
;
25829 tree req
= cp_parser_requirement (parser
);
25830 if (req
== error_mark_node
)
25831 return error_mark_node
;
25833 result
= tree_cons (NULL_TREE
, req
, result
);
25835 /* If we see a semi-colon, consume it. */
25836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25837 cp_lexer_consume_token (parser
->lexer
);
25839 /* Stop processing at the end of the list. */
25840 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25844 /* Reverse the order of requirements so they are analyzed in
25845 declaration order. */
25846 return nreverse (result
);
25849 /* Parse a syntactic requirement or type requirement.
25853 compound-requirement
25855 nested-requirement */
25857 cp_parser_requirement (cp_parser
*parser
)
25859 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25860 return cp_parser_compound_requirement (parser
);
25861 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25862 return cp_parser_type_requirement (parser
);
25863 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25864 return cp_parser_nested_requirement (parser
);
25866 return cp_parser_simple_requirement (parser
);
25869 /* Parse a simple requirement.
25871 simple-requirement:
25874 cp_parser_simple_requirement (cp_parser
*parser
)
25876 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25877 if (!expr
|| expr
== error_mark_node
)
25878 return error_mark_node
;
25880 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25881 return error_mark_node
;
25883 return finish_simple_requirement (expr
);
25886 /* Parse a type requirement
25889 nested-name-specifier [opt] required-type-name ';'
25891 required-type-name:
25893 'template' [opt] simple-template-id */
25895 cp_parser_type_requirement (cp_parser
*parser
)
25897 cp_lexer_consume_token (parser
->lexer
);
25899 // Save the scope before parsing name specifiers.
25900 tree saved_scope
= parser
->scope
;
25901 tree saved_object_scope
= parser
->object_scope
;
25902 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25903 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25904 cp_parser_nested_name_specifier_opt (parser
,
25905 /*typename_keyword_p=*/true,
25906 /*check_dependency_p=*/false,
25908 /*is_declaration=*/false);
25911 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25913 cp_lexer_consume_token (parser
->lexer
);
25914 type
= cp_parser_template_id (parser
,
25915 /*template_keyword_p=*/true,
25916 /*check_dependency=*/false,
25917 /*tag_type=*/none_type
,
25918 /*is_declaration=*/false);
25919 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25920 /*complain=*/tf_error
);
25923 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25925 if (TREE_CODE (type
) == TYPE_DECL
)
25926 type
= TREE_TYPE (type
);
25928 parser
->scope
= saved_scope
;
25929 parser
->object_scope
= saved_object_scope
;
25930 parser
->qualifying_scope
= saved_qualifying_scope
;
25932 if (type
== error_mark_node
)
25933 cp_parser_skip_to_end_of_statement (parser
);
25935 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25936 return error_mark_node
;
25937 if (type
== error_mark_node
)
25938 return error_mark_node
;
25940 return finish_type_requirement (type
);
25943 /* Parse a compound requirement
25945 compound-requirement:
25946 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25948 cp_parser_compound_requirement (cp_parser
*parser
)
25950 /* Parse an expression enclosed in '{ }'s. */
25951 matching_braces braces
;
25952 if (!braces
.require_open (parser
))
25953 return error_mark_node
;
25955 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25956 if (!expr
|| expr
== error_mark_node
)
25957 return error_mark_node
;
25959 if (!braces
.require_close (parser
))
25960 return error_mark_node
;
25962 /* Parse the optional noexcept. */
25963 bool noexcept_p
= false;
25964 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25966 cp_lexer_consume_token (parser
->lexer
);
25970 /* Parse the optional trailing return type. */
25971 tree type
= NULL_TREE
;
25972 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25974 cp_lexer_consume_token (parser
->lexer
);
25975 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25976 parser
->in_result_type_constraint_p
= true;
25977 type
= cp_parser_trailing_type_id (parser
);
25978 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25979 if (type
== error_mark_node
)
25980 return error_mark_node
;
25983 return finish_compound_requirement (expr
, type
, noexcept_p
);
25986 /* Parse a nested requirement. This is the same as a requires clause.
25988 nested-requirement:
25991 cp_parser_nested_requirement (cp_parser
*parser
)
25993 cp_lexer_consume_token (parser
->lexer
);
25994 tree req
= cp_parser_requires_clause (parser
);
25995 if (req
== error_mark_node
)
25996 return error_mark_node
;
25997 return finish_nested_requirement (req
);
26000 /* Support Functions */
26002 /* Return the appropriate prefer_type argument for lookup_name_real based on
26003 tag_type and template_mem_access. */
26006 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26008 /* DR 141: When looking in the current enclosing context for a template-name
26009 after -> or ., only consider class templates. */
26010 if (template_mem_access
)
26014 case none_type
: return 0; // No preference.
26015 case scope_type
: return 1; // Type or namespace.
26016 default: return 2; // Type only.
26020 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26021 NAME should have one of the representations used for an
26022 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26023 is returned. If PARSER->SCOPE is a dependent type, then a
26024 SCOPE_REF is returned.
26026 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26027 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26028 was formed. Abstractly, such entities should not be passed to this
26029 function, because they do not need to be looked up, but it is
26030 simpler to check for this special case here, rather than at the
26033 In cases not explicitly covered above, this function returns a
26034 DECL, OVERLOAD, or baselink representing the result of the lookup.
26035 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26038 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26039 (e.g., "struct") that was used. In that case bindings that do not
26040 refer to types are ignored.
26042 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26045 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26048 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26051 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26052 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26053 NULL_TREE otherwise. */
26056 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26057 enum tag_types tag_type
,
26060 bool check_dependency
,
26061 tree
*ambiguous_decls
,
26062 location_t name_location
)
26065 tree object_type
= parser
->context
->object_type
;
26067 /* Assume that the lookup will be unambiguous. */
26068 if (ambiguous_decls
)
26069 *ambiguous_decls
= NULL_TREE
;
26071 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26072 no longer valid. Note that if we are parsing tentatively, and
26073 the parse fails, OBJECT_TYPE will be automatically restored. */
26074 parser
->context
->object_type
= NULL_TREE
;
26076 if (name
== error_mark_node
)
26077 return error_mark_node
;
26079 /* A template-id has already been resolved; there is no lookup to
26081 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26083 if (BASELINK_P (name
))
26085 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26086 == TEMPLATE_ID_EXPR
);
26090 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26091 it should already have been checked to make sure that the name
26092 used matches the type being destroyed. */
26093 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26097 /* Figure out to which type this destructor applies. */
26099 type
= parser
->scope
;
26100 else if (object_type
)
26101 type
= object_type
;
26103 type
= current_class_type
;
26104 /* If that's not a class type, there is no destructor. */
26105 if (!type
|| !CLASS_TYPE_P (type
))
26106 return error_mark_node
;
26108 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26109 lazily_declare_fn (sfk_destructor
, type
);
26111 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26114 return error_mark_node
;
26117 /* By this point, the NAME should be an ordinary identifier. If
26118 the id-expression was a qualified name, the qualifying scope is
26119 stored in PARSER->SCOPE at this point. */
26120 gcc_assert (identifier_p (name
));
26122 /* Perform the lookup. */
26127 if (parser
->scope
== error_mark_node
)
26128 return error_mark_node
;
26130 /* If the SCOPE is dependent, the lookup must be deferred until
26131 the template is instantiated -- unless we are explicitly
26132 looking up names in uninstantiated templates. Even then, we
26133 cannot look up the name if the scope is not a class type; it
26134 might, for example, be a template type parameter. */
26135 dependent_p
= (TYPE_P (parser
->scope
)
26136 && dependent_scope_p (parser
->scope
));
26137 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26139 /* Defer lookup. */
26140 decl
= error_mark_node
;
26143 tree pushed_scope
= NULL_TREE
;
26145 /* If PARSER->SCOPE is a dependent type, then it must be a
26146 class type, and we must not be checking dependencies;
26147 otherwise, we would have processed this lookup above. So
26148 that PARSER->SCOPE is not considered a dependent base by
26149 lookup_member, we must enter the scope here. */
26151 pushed_scope
= push_scope (parser
->scope
);
26153 /* If the PARSER->SCOPE is a template specialization, it
26154 may be instantiated during name lookup. In that case,
26155 errors may be issued. Even if we rollback the current
26156 tentative parse, those errors are valid. */
26157 decl
= lookup_qualified_name (parser
->scope
, name
,
26158 prefer_type_arg (tag_type
),
26159 /*complain=*/true);
26161 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26162 lookup result and the nested-name-specifier nominates a class C:
26163 * if the name specified after the nested-name-specifier, when
26164 looked up in C, is the injected-class-name of C (Clause 9), or
26165 * if the name specified after the nested-name-specifier is the
26166 same as the identifier or the simple-template-id's template-
26167 name in the last component of the nested-name-specifier,
26168 the name is instead considered to name the constructor of
26169 class C. [ Note: for example, the constructor is not an
26170 acceptable lookup result in an elaborated-type-specifier so
26171 the constructor would not be used in place of the
26172 injected-class-name. --end note ] Such a constructor name
26173 shall be used only in the declarator-id of a declaration that
26174 names a constructor or in a using-declaration. */
26175 if (tag_type
== none_type
26176 && DECL_SELF_REFERENCE_P (decl
)
26177 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26178 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26179 prefer_type_arg (tag_type
),
26180 /*complain=*/true);
26182 /* If we have a single function from a using decl, pull it out. */
26183 if (TREE_CODE (decl
) == OVERLOAD
26184 && !really_overloaded_fn (decl
))
26185 decl
= OVL_FUNCTION (decl
);
26188 pop_scope (pushed_scope
);
26191 /* If the scope is a dependent type and either we deferred lookup or
26192 we did lookup but didn't find the name, rememeber the name. */
26193 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26194 && dependent_type_p (parser
->scope
))
26200 /* The resolution to Core Issue 180 says that `struct
26201 A::B' should be considered a type-name, even if `A'
26203 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26204 /*complain=*/tf_error
);
26205 if (type
!= error_mark_node
)
26206 decl
= TYPE_NAME (type
);
26208 else if (is_template
26209 && (cp_parser_next_token_ends_template_argument_p (parser
)
26210 || cp_lexer_next_token_is (parser
->lexer
,
26212 decl
= make_unbound_class_template (parser
->scope
,
26214 /*complain=*/tf_error
);
26216 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26217 parser
->scope
, name
,
26220 parser
->qualifying_scope
= parser
->scope
;
26221 parser
->object_scope
= NULL_TREE
;
26223 else if (object_type
)
26225 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26226 OBJECT_TYPE is not a class. */
26227 if (CLASS_TYPE_P (object_type
))
26228 /* If the OBJECT_TYPE is a template specialization, it may
26229 be instantiated during name lookup. In that case, errors
26230 may be issued. Even if we rollback the current tentative
26231 parse, those errors are valid. */
26232 decl
= lookup_member (object_type
,
26235 prefer_type_arg (tag_type
),
26236 tf_warning_or_error
);
26241 /* Look it up in the enclosing context. DR 141: When looking for a
26242 template-name after -> or ., only consider class templates. */
26243 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26245 /*block_p=*/true, is_namespace
, 0);
26246 if (object_type
== unknown_type_node
)
26247 /* The object is type-dependent, so we can't look anything up; we used
26248 this to get the DR 141 behavior. */
26249 object_type
= NULL_TREE
;
26250 parser
->object_scope
= object_type
;
26251 parser
->qualifying_scope
= NULL_TREE
;
26255 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26257 /*block_p=*/true, is_namespace
, 0);
26258 parser
->qualifying_scope
= NULL_TREE
;
26259 parser
->object_scope
= NULL_TREE
;
26262 /* If the lookup failed, let our caller know. */
26263 if (!decl
|| decl
== error_mark_node
)
26264 return error_mark_node
;
26266 /* Pull out the template from an injected-class-name (or multiple). */
26268 decl
= maybe_get_template_decl_from_type_decl (decl
);
26270 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26271 if (TREE_CODE (decl
) == TREE_LIST
)
26273 if (ambiguous_decls
)
26274 *ambiguous_decls
= decl
;
26275 /* The error message we have to print is too complicated for
26276 cp_parser_error, so we incorporate its actions directly. */
26277 if (!cp_parser_simulate_error (parser
))
26279 error_at (name_location
, "reference to %qD is ambiguous",
26281 print_candidates (decl
);
26283 return error_mark_node
;
26286 gcc_assert (DECL_P (decl
)
26287 || TREE_CODE (decl
) == OVERLOAD
26288 || TREE_CODE (decl
) == SCOPE_REF
26289 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26290 || BASELINK_P (decl
));
26292 /* If we have resolved the name of a member declaration, check to
26293 see if the declaration is accessible. When the name resolves to
26294 set of overloaded functions, accessibility is checked when
26295 overload resolution is done.
26297 During an explicit instantiation, access is not checked at all,
26298 as per [temp.explicit]. */
26300 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26302 maybe_record_typedef_use (decl
);
26304 return cp_expr (decl
, name_location
);
26307 /* Like cp_parser_lookup_name, but for use in the typical case where
26308 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26309 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26312 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26314 return cp_parser_lookup_name (parser
, name
,
26316 /*is_template=*/false,
26317 /*is_namespace=*/false,
26318 /*check_dependency=*/true,
26319 /*ambiguous_decls=*/NULL
,
26323 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26324 the current context, return the TYPE_DECL. If TAG_NAME_P is
26325 true, the DECL indicates the class being defined in a class-head,
26326 or declared in an elaborated-type-specifier.
26328 Otherwise, return DECL. */
26331 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26333 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26334 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26337 template <typename T> struct B;
26340 template <typename T> struct A::B {};
26342 Similarly, in an elaborated-type-specifier:
26344 namespace N { struct X{}; }
26347 template <typename T> friend struct N::X;
26350 However, if the DECL refers to a class type, and we are in
26351 the scope of the class, then the name lookup automatically
26352 finds the TYPE_DECL created by build_self_reference rather
26353 than a TEMPLATE_DECL. For example, in:
26355 template <class T> struct S {
26359 there is no need to handle such case. */
26361 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26362 return DECL_TEMPLATE_RESULT (decl
);
26367 /* If too many, or too few, template-parameter lists apply to the
26368 declarator, issue an error message. Returns TRUE if all went well,
26369 and FALSE otherwise. */
26372 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26373 cp_declarator
*declarator
,
26374 location_t declarator_location
)
26376 switch (declarator
->kind
)
26380 unsigned num_templates
= 0;
26381 tree scope
= declarator
->u
.id
.qualifying_scope
;
26384 num_templates
= num_template_headers_for_class (scope
);
26385 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26386 == TEMPLATE_ID_EXPR
)
26387 /* If the DECLARATOR has the form `X<y>' then it uses one
26388 additional level of template parameters. */
26391 return cp_parser_check_template_parameters
26392 (parser
, num_templates
, declarator_location
, declarator
);
26398 case cdk_reference
:
26400 return (cp_parser_check_declarator_template_parameters
26401 (parser
, declarator
->declarator
, declarator_location
));
26408 gcc_unreachable ();
26413 /* NUM_TEMPLATES were used in the current declaration. If that is
26414 invalid, return FALSE and issue an error messages. Otherwise,
26415 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26416 declarator and we can print more accurate diagnostics. */
26419 cp_parser_check_template_parameters (cp_parser
* parser
,
26420 unsigned num_templates
,
26421 location_t location
,
26422 cp_declarator
*declarator
)
26424 /* If there are the same number of template classes and parameter
26425 lists, that's OK. */
26426 if (parser
->num_template_parameter_lists
== num_templates
)
26428 /* If there are more, but only one more, then we are referring to a
26429 member template. That's OK too. */
26430 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
26432 /* If there are more template classes than parameter lists, we have
26435 template <class T> void S<T>::R<T>::f (); */
26436 if (parser
->num_template_parameter_lists
< num_templates
)
26438 if (declarator
&& !current_function_decl
)
26439 error_at (location
, "specializing member %<%T::%E%> "
26440 "requires %<template<>%> syntax",
26441 declarator
->u
.id
.qualifying_scope
,
26442 declarator
->u
.id
.unqualified_name
);
26443 else if (declarator
)
26444 error_at (location
, "invalid declaration of %<%T::%E%>",
26445 declarator
->u
.id
.qualifying_scope
,
26446 declarator
->u
.id
.unqualified_name
);
26448 error_at (location
, "too few template-parameter-lists");
26451 /* Otherwise, there are too many template parameter lists. We have
26454 template <class T> template <class U> void S::f(); */
26455 error_at (location
, "too many template-parameter-lists");
26459 /* Parse an optional `::' token indicating that the following name is
26460 from the global namespace. If so, PARSER->SCOPE is set to the
26461 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26462 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26463 Returns the new value of PARSER->SCOPE, if the `::' token is
26464 present, and NULL_TREE otherwise. */
26467 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26471 /* Peek at the next token. */
26472 token
= cp_lexer_peek_token (parser
->lexer
);
26473 /* If we're looking at a `::' token then we're starting from the
26474 global namespace, not our current location. */
26475 if (token
->type
== CPP_SCOPE
)
26477 /* Consume the `::' token. */
26478 cp_lexer_consume_token (parser
->lexer
);
26479 /* Set the SCOPE so that we know where to start the lookup. */
26480 parser
->scope
= global_namespace
;
26481 parser
->qualifying_scope
= global_namespace
;
26482 parser
->object_scope
= NULL_TREE
;
26484 return parser
->scope
;
26486 else if (!current_scope_valid_p
)
26488 parser
->scope
= NULL_TREE
;
26489 parser
->qualifying_scope
= NULL_TREE
;
26490 parser
->object_scope
= NULL_TREE
;
26496 /* Returns TRUE if the upcoming token sequence is the start of a
26497 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26498 declarator is preceded by the `friend' specifier. */
26501 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26503 bool constructor_p
;
26504 bool outside_class_specifier_p
;
26505 tree nested_name_specifier
;
26506 cp_token
*next_token
;
26508 /* The common case is that this is not a constructor declarator, so
26509 try to avoid doing lots of work if at all possible. It's not
26510 valid declare a constructor at function scope. */
26511 if (parser
->in_function_body
)
26513 /* And only certain tokens can begin a constructor declarator. */
26514 next_token
= cp_lexer_peek_token (parser
->lexer
);
26515 if (next_token
->type
!= CPP_NAME
26516 && next_token
->type
!= CPP_SCOPE
26517 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26518 && next_token
->type
!= CPP_TEMPLATE_ID
)
26521 /* Parse tentatively; we are going to roll back all of the tokens
26523 cp_parser_parse_tentatively (parser
);
26524 /* Assume that we are looking at a constructor declarator. */
26525 constructor_p
= true;
26527 /* Look for the optional `::' operator. */
26528 cp_parser_global_scope_opt (parser
,
26529 /*current_scope_valid_p=*/false);
26530 /* Look for the nested-name-specifier. */
26531 nested_name_specifier
26532 = (cp_parser_nested_name_specifier_opt (parser
,
26533 /*typename_keyword_p=*/false,
26534 /*check_dependency_p=*/false,
26536 /*is_declaration=*/false));
26538 outside_class_specifier_p
= (!at_class_scope_p ()
26539 || !TYPE_BEING_DEFINED (current_class_type
)
26542 /* Outside of a class-specifier, there must be a
26543 nested-name-specifier. Except in C++17 mode, where we
26544 might be declaring a guiding declaration. */
26545 if (!nested_name_specifier
&& outside_class_specifier_p
26546 && cxx_dialect
< cxx17
)
26547 constructor_p
= false;
26548 else if (nested_name_specifier
== error_mark_node
)
26549 constructor_p
= false;
26551 /* If we have a class scope, this is easy; DR 147 says that S::S always
26552 names the constructor, and no other qualified name could. */
26553 if (constructor_p
&& nested_name_specifier
26554 && CLASS_TYPE_P (nested_name_specifier
))
26556 tree id
= cp_parser_unqualified_id (parser
,
26557 /*template_keyword_p=*/false,
26558 /*check_dependency_p=*/false,
26559 /*declarator_p=*/true,
26560 /*optional_p=*/false);
26561 if (is_overloaded_fn (id
))
26562 id
= DECL_NAME (get_first_fn (id
));
26563 if (!constructor_name_p (id
, nested_name_specifier
))
26564 constructor_p
= false;
26566 /* If we still think that this might be a constructor-declarator,
26567 look for a class-name. */
26568 else if (constructor_p
)
26572 template <typename T> struct S {
26576 we must recognize that the nested `S' names a class. */
26577 if (cxx_dialect
>= cxx17
)
26578 cp_parser_parse_tentatively (parser
);
26581 type_decl
= cp_parser_class_name (parser
,
26582 /*typename_keyword_p=*/false,
26583 /*template_keyword_p=*/false,
26585 /*check_dependency_p=*/false,
26586 /*class_head_p=*/false,
26587 /*is_declaration=*/false);
26589 if (cxx_dialect
>= cxx17
26590 && !cp_parser_parse_definitely (parser
))
26592 type_decl
= NULL_TREE
;
26593 tree tmpl
= cp_parser_template_name (parser
,
26594 /*template_keyword*/false,
26595 /*check_dependency_p*/false,
26596 /*is_declaration*/false,
26598 /*is_identifier*/NULL
);
26599 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26600 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26601 /* It's a deduction guide, return true. */;
26603 cp_parser_simulate_error (parser
);
26606 /* If there was no class-name, then this is not a constructor.
26607 Otherwise, if we are in a class-specifier and we aren't
26608 handling a friend declaration, check that its type matches
26609 current_class_type (c++/38313). Note: error_mark_node
26610 is left alone for error recovery purposes. */
26611 constructor_p
= (!cp_parser_error_occurred (parser
)
26612 && (outside_class_specifier_p
26613 || type_decl
== NULL_TREE
26614 || type_decl
== error_mark_node
26615 || same_type_p (current_class_type
,
26616 TREE_TYPE (type_decl
))));
26618 /* If we're still considering a constructor, we have to see a `(',
26619 to begin the parameter-declaration-clause, followed by either a
26620 `)', an `...', or a decl-specifier. We need to check for a
26621 type-specifier to avoid being fooled into thinking that:
26625 is a constructor. (It is actually a function named `f' that
26626 takes one parameter (of type `int') and returns a value of type
26629 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26630 constructor_p
= false;
26633 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26634 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26635 /* A parameter declaration begins with a decl-specifier,
26636 which is either the "attribute" keyword, a storage class
26637 specifier, or (usually) a type-specifier. */
26638 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26641 tree pushed_scope
= NULL_TREE
;
26642 unsigned saved_num_template_parameter_lists
;
26644 /* Names appearing in the type-specifier should be looked up
26645 in the scope of the class. */
26646 if (current_class_type
)
26648 else if (type_decl
)
26650 type
= TREE_TYPE (type_decl
);
26651 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26653 type
= resolve_typename_type (type
,
26654 /*only_current_p=*/false);
26655 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26657 cp_parser_abort_tentative_parse (parser
);
26661 pushed_scope
= push_scope (type
);
26664 /* Inside the constructor parameter list, surrounding
26665 template-parameter-lists do not apply. */
26666 saved_num_template_parameter_lists
26667 = parser
->num_template_parameter_lists
;
26668 parser
->num_template_parameter_lists
= 0;
26670 /* Look for the type-specifier. */
26671 cp_parser_type_specifier (parser
,
26672 CP_PARSER_FLAGS_NONE
,
26673 /*decl_specs=*/NULL
,
26674 /*is_declarator=*/true,
26675 /*declares_class_or_enum=*/NULL
,
26676 /*is_cv_qualifier=*/NULL
);
26678 parser
->num_template_parameter_lists
26679 = saved_num_template_parameter_lists
;
26681 /* Leave the scope of the class. */
26683 pop_scope (pushed_scope
);
26685 constructor_p
= !cp_parser_error_occurred (parser
);
26689 /* We did not really want to consume any tokens. */
26690 cp_parser_abort_tentative_parse (parser
);
26692 return constructor_p
;
26695 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26696 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26697 they must be performed once we are in the scope of the function.
26699 Returns the function defined. */
26702 cp_parser_function_definition_from_specifiers_and_declarator
26703 (cp_parser
* parser
,
26704 cp_decl_specifier_seq
*decl_specifiers
,
26706 const cp_declarator
*declarator
)
26711 /* Begin the function-definition. */
26712 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26714 /* The things we're about to see are not directly qualified by any
26715 template headers we've seen thus far. */
26716 reset_specialization ();
26718 /* If there were names looked up in the decl-specifier-seq that we
26719 did not check, check them now. We must wait until we are in the
26720 scope of the function to perform the checks, since the function
26721 might be a friend. */
26722 perform_deferred_access_checks (tf_warning_or_error
);
26726 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26727 parser
->omp_declare_simd
= NULL
;
26728 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26729 parser
->oacc_routine
= NULL
;
26734 /* Skip the entire function. */
26735 cp_parser_skip_to_end_of_block_or_statement (parser
);
26736 fn
= error_mark_node
;
26738 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26740 /* Seen already, skip it. An error message has already been output. */
26741 cp_parser_skip_to_end_of_block_or_statement (parser
);
26742 fn
= current_function_decl
;
26743 current_function_decl
= NULL_TREE
;
26744 /* If this is a function from a class, pop the nested class. */
26745 if (current_class_name
)
26746 pop_nested_class ();
26751 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26752 tv
= TV_PARSE_INLINE
;
26754 tv
= TV_PARSE_FUNC
;
26756 fn
= cp_parser_function_definition_after_declarator (parser
,
26757 /*inline_p=*/false);
26764 /* Parse the part of a function-definition that follows the
26765 declarator. INLINE_P is TRUE iff this function is an inline
26766 function defined within a class-specifier.
26768 Returns the function defined. */
26771 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26775 bool saved_in_unbraced_linkage_specification_p
;
26776 bool saved_in_function_body
;
26777 unsigned saved_num_template_parameter_lists
;
26779 bool fully_implicit_function_template_p
26780 = parser
->fully_implicit_function_template_p
;
26781 parser
->fully_implicit_function_template_p
= false;
26782 tree implicit_template_parms
26783 = parser
->implicit_template_parms
;
26784 parser
->implicit_template_parms
= 0;
26785 cp_binding_level
* implicit_template_scope
26786 = parser
->implicit_template_scope
;
26787 parser
->implicit_template_scope
= 0;
26789 saved_in_function_body
= parser
->in_function_body
;
26790 parser
->in_function_body
= true;
26791 /* If the next token is `return', then the code may be trying to
26792 make use of the "named return value" extension that G++ used to
26794 token
= cp_lexer_peek_token (parser
->lexer
);
26795 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26797 /* Consume the `return' keyword. */
26798 cp_lexer_consume_token (parser
->lexer
);
26799 /* Look for the identifier that indicates what value is to be
26801 cp_parser_identifier (parser
);
26802 /* Issue an error message. */
26803 error_at (token
->location
,
26804 "named return values are no longer supported");
26805 /* Skip tokens until we reach the start of the function body. */
26808 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26809 if (token
->type
== CPP_OPEN_BRACE
26810 || token
->type
== CPP_EOF
26811 || token
->type
== CPP_PRAGMA_EOL
)
26813 cp_lexer_consume_token (parser
->lexer
);
26816 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26817 anything declared inside `f'. */
26818 saved_in_unbraced_linkage_specification_p
26819 = parser
->in_unbraced_linkage_specification_p
;
26820 parser
->in_unbraced_linkage_specification_p
= false;
26821 /* Inside the function, surrounding template-parameter-lists do not
26823 saved_num_template_parameter_lists
26824 = parser
->num_template_parameter_lists
;
26825 parser
->num_template_parameter_lists
= 0;
26827 /* If the next token is `try', `__transaction_atomic', or
26828 `__transaction_relaxed`, then we are looking at either function-try-block
26829 or function-transaction-block. Note that all of these include the
26831 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26832 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26833 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26834 RID_TRANSACTION_RELAXED
))
26835 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26836 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26837 cp_parser_function_try_block (parser
);
26839 cp_parser_ctor_initializer_opt_and_function_body
26840 (parser
, /*in_function_try_block=*/false);
26842 /* Finish the function. */
26843 fn
= finish_function (inline_p
);
26844 /* Generate code for it, if necessary. */
26845 expand_or_defer_fn (fn
);
26846 /* Restore the saved values. */
26847 parser
->in_unbraced_linkage_specification_p
26848 = saved_in_unbraced_linkage_specification_p
;
26849 parser
->num_template_parameter_lists
26850 = saved_num_template_parameter_lists
;
26851 parser
->in_function_body
= saved_in_function_body
;
26853 parser
->fully_implicit_function_template_p
26854 = fully_implicit_function_template_p
;
26855 parser
->implicit_template_parms
26856 = implicit_template_parms
;
26857 parser
->implicit_template_scope
26858 = implicit_template_scope
;
26860 if (parser
->fully_implicit_function_template_p
)
26861 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26866 /* Parse a template-declaration body (following argument list). */
26869 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26870 tree parameter_list
,
26873 tree decl
= NULL_TREE
;
26874 bool friend_p
= false;
26876 /* We just processed one more parameter list. */
26877 ++parser
->num_template_parameter_lists
;
26879 /* Get the deferred access checks from the parameter list. These
26880 will be checked once we know what is being declared, as for a
26881 member template the checks must be performed in the scope of the
26882 class containing the member. */
26883 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26885 /* Tentatively parse for a new template parameter list, which can either be
26886 the template keyword or a template introduction. */
26887 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26889 else if (cxx_dialect
>= cxx11
26890 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26891 decl
= cp_parser_alias_declaration (parser
);
26894 /* There are no access checks when parsing a template, as we do not
26895 know if a specialization will be a friend. */
26896 push_deferring_access_checks (dk_no_check
);
26897 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26898 decl
= cp_parser_single_declaration (parser
,
26901 /*explicit_specialization_p=*/false,
26903 pop_deferring_access_checks ();
26905 /* If this is a member template declaration, let the front
26907 if (member_p
&& !friend_p
&& decl
)
26909 if (TREE_CODE (decl
) == TYPE_DECL
)
26910 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26912 decl
= finish_member_template_decl (decl
);
26914 else if (friend_p
&& decl
26915 && DECL_DECLARES_TYPE_P (decl
))
26916 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26917 /*complain=*/true);
26919 /* We are done with the current parameter list. */
26920 --parser
->num_template_parameter_lists
;
26922 pop_deferring_access_checks ();
26925 finish_template_decl (parameter_list
);
26927 /* Check the template arguments for a literal operator template. */
26929 && DECL_DECLARES_FUNCTION_P (decl
)
26930 && UDLIT_OPER_P (DECL_NAME (decl
)))
26933 if (parameter_list
== NULL_TREE
)
26937 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26938 if (num_parms
== 1)
26940 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26941 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26942 if (TREE_TYPE (parm
) != char_type_node
26943 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26946 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26948 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26949 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26950 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26951 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26952 if (parm
== error_mark_node
26953 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26954 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26962 if (cxx_dialect
>= cxx14
)
26963 error ("literal operator template %qD has invalid parameter list."
26964 " Expected non-type template argument pack <char...>"
26965 " or <typename CharT, CharT...>",
26968 error ("literal operator template %qD has invalid parameter list."
26969 " Expected non-type template argument pack <char...>",
26974 /* Register member declarations. */
26975 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26976 finish_member_declaration (decl
);
26977 /* If DECL is a function template, we must return to parse it later.
26978 (Even though there is no definition, there might be default
26979 arguments that need handling.) */
26980 if (member_p
&& decl
26981 && DECL_DECLARES_FUNCTION_P (decl
))
26982 vec_safe_push (unparsed_funs_with_definitions
, decl
);
26985 /* Parse a template introduction header for a template-declaration. Returns
26986 false if tentative parse fails. */
26989 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
26991 cp_parser_parse_tentatively (parser
);
26993 tree saved_scope
= parser
->scope
;
26994 tree saved_object_scope
= parser
->object_scope
;
26995 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26997 /* Look for the optional `::' operator. */
26998 cp_parser_global_scope_opt (parser
,
26999 /*current_scope_valid_p=*/false);
27000 /* Look for the nested-name-specifier. */
27001 cp_parser_nested_name_specifier_opt (parser
,
27002 /*typename_keyword_p=*/false,
27003 /*check_dependency_p=*/true,
27005 /*is_declaration=*/false);
27007 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27008 tree concept_name
= cp_parser_identifier (parser
);
27010 /* Look up the concept for which we will be matching
27011 template parameters. */
27012 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27014 parser
->scope
= saved_scope
;
27015 parser
->object_scope
= saved_object_scope
;
27016 parser
->qualifying_scope
= saved_qualifying_scope
;
27018 if (concept_name
== error_mark_node
)
27019 cp_parser_simulate_error (parser
);
27021 /* Look for opening brace for introduction. */
27022 matching_braces braces
;
27023 braces
.require_open (parser
);
27025 if (!cp_parser_parse_definitely (parser
))
27028 push_deferring_access_checks (dk_deferred
);
27030 /* Build vector of placeholder parameters and grab
27031 matching identifiers. */
27032 tree introduction_list
= cp_parser_introduction_list (parser
);
27034 /* The introduction-list shall not be empty. */
27035 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27038 error ("empty introduction-list");
27042 /* Look for closing brace for introduction. */
27043 if (!braces
.require_close (parser
))
27046 if (tmpl_decl
== error_mark_node
)
27048 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27053 /* Build and associate the constraint. */
27054 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27055 if (parms
&& parms
!= error_mark_node
)
27057 cp_parser_template_declaration_after_parameters (parser
, parms
,
27062 error_at (token
->location
, "no matching concept for template-introduction");
27066 /* Parse a normal template-declaration following the template keyword. */
27069 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27071 tree parameter_list
;
27072 bool need_lang_pop
;
27073 location_t location
= input_location
;
27075 /* Look for the `<' token. */
27076 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27078 if (at_class_scope_p () && current_function_decl
)
27080 /* 14.5.2.2 [temp.mem]
27082 A local class shall not have member templates. */
27083 error_at (location
,
27084 "invalid declaration of member template in local class");
27085 cp_parser_skip_to_end_of_block_or_statement (parser
);
27090 A template ... shall not have C linkage. */
27091 if (current_lang_name
== lang_name_c
)
27093 error_at (location
, "template with C linkage");
27094 maybe_show_extern_c_location ();
27095 /* Give it C++ linkage to avoid confusing other parts of the
27097 push_lang_context (lang_name_cplusplus
);
27098 need_lang_pop
= true;
27101 need_lang_pop
= false;
27103 /* We cannot perform access checks on the template parameter
27104 declarations until we know what is being declared, just as we
27105 cannot check the decl-specifier list. */
27106 push_deferring_access_checks (dk_deferred
);
27108 /* If the next token is `>', then we have an invalid
27109 specialization. Rather than complain about an invalid template
27110 parameter, issue an error message here. */
27111 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27113 cp_parser_error (parser
, "invalid explicit specialization");
27114 begin_specialization ();
27115 parameter_list
= NULL_TREE
;
27119 /* Parse the template parameters. */
27120 parameter_list
= cp_parser_template_parameter_list (parser
);
27123 /* Look for the `>'. */
27124 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27126 /* Manage template requirements */
27129 tree reqs
= get_shorthand_constraints (current_template_parms
);
27130 if (tree r
= cp_parser_requires_clause_opt (parser
))
27131 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27132 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27135 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27138 /* For the erroneous case of a template with C linkage, we pushed an
27139 implicit C++ linkage scope; exit that scope now. */
27141 pop_lang_context ();
27144 /* Parse a template-declaration, assuming that the `export' (and
27145 `extern') keywords, if present, has already been scanned. MEMBER_P
27146 is as for cp_parser_template_declaration. */
27149 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27151 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27153 cp_lexer_consume_token (parser
->lexer
);
27154 cp_parser_explicit_template_declaration (parser
, member_p
);
27157 else if (flag_concepts
)
27158 return cp_parser_template_introduction (parser
, member_p
);
27163 /* Perform the deferred access checks from a template-parameter-list.
27164 CHECKS is a TREE_LIST of access checks, as returned by
27165 get_deferred_access_checks. */
27168 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27170 ++processing_template_parmlist
;
27171 perform_access_checks (checks
, tf_warning_or_error
);
27172 --processing_template_parmlist
;
27175 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27176 `function-definition' sequence that follows a template header.
27177 If MEMBER_P is true, this declaration appears in a class scope.
27179 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27180 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27183 cp_parser_single_declaration (cp_parser
* parser
,
27184 vec
<deferred_access_check
, va_gc
> *checks
,
27186 bool explicit_specialization_p
,
27189 int declares_class_or_enum
;
27190 tree decl
= NULL_TREE
;
27191 cp_decl_specifier_seq decl_specifiers
;
27192 bool function_definition_p
= false;
27193 cp_token
*decl_spec_token_start
;
27195 /* This function is only used when processing a template
27197 gcc_assert (innermost_scope_kind () == sk_template_parms
27198 || innermost_scope_kind () == sk_template_spec
);
27200 /* Defer access checks until we know what is being declared. */
27201 push_deferring_access_checks (dk_deferred
);
27203 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27205 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27206 cp_parser_decl_specifier_seq (parser
,
27207 CP_PARSER_FLAGS_OPTIONAL
,
27209 &declares_class_or_enum
);
27211 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27213 /* There are no template typedefs. */
27214 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27216 error_at (decl_spec_token_start
->location
,
27217 "template declaration of %<typedef%>");
27218 decl
= error_mark_node
;
27221 /* Gather up the access checks that occurred the
27222 decl-specifier-seq. */
27223 stop_deferring_access_checks ();
27225 /* Check for the declaration of a template class. */
27226 if (declares_class_or_enum
)
27228 if (cp_parser_declares_only_class_p (parser
)
27229 || (declares_class_or_enum
& 2))
27231 // If this is a declaration, but not a definition, associate
27232 // any constraints with the type declaration. Constraints
27233 // are associated with definitions in cp_parser_class_specifier.
27234 if (declares_class_or_enum
== 1)
27235 associate_classtype_constraints (decl_specifiers
.type
);
27237 decl
= shadow_tag (&decl_specifiers
);
27242 friend template <typename T> struct A<T>::B;
27245 A<T>::B will be represented by a TYPENAME_TYPE, and
27246 therefore not recognized by shadow_tag. */
27247 if (friend_p
&& *friend_p
27249 && decl_specifiers
.type
27250 && TYPE_P (decl_specifiers
.type
))
27251 decl
= decl_specifiers
.type
;
27253 if (decl
&& decl
!= error_mark_node
)
27254 decl
= TYPE_NAME (decl
);
27256 decl
= error_mark_node
;
27258 /* Perform access checks for template parameters. */
27259 cp_parser_perform_template_parameter_access_checks (checks
);
27261 /* Give a helpful diagnostic for
27262 template <class T> struct A { } a;
27263 if we aren't already recovering from an error. */
27264 if (!cp_parser_declares_only_class_p (parser
)
27267 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27268 "a class template declaration must not declare "
27270 cp_parser_skip_to_end_of_block_or_statement (parser
);
27276 /* Complain about missing 'typename' or other invalid type names. */
27277 if (!decl_specifiers
.any_type_specifiers_p
27278 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27280 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27281 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27282 the rest of this declaration. */
27283 decl
= error_mark_node
;
27287 /* If it's not a template class, try for a template function. If
27288 the next token is a `;', then this declaration does not declare
27289 anything. But, if there were errors in the decl-specifiers, then
27290 the error might well have come from an attempted class-specifier.
27291 In that case, there's no need to warn about a missing declarator. */
27293 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27294 || decl_specifiers
.type
!= error_mark_node
))
27296 decl
= cp_parser_init_declarator (parser
,
27299 /*function_definition_allowed_p=*/true,
27301 declares_class_or_enum
,
27302 &function_definition_p
,
27305 /* 7.1.1-1 [dcl.stc]
27307 A storage-class-specifier shall not be specified in an explicit
27308 specialization... */
27310 && explicit_specialization_p
27311 && decl_specifiers
.storage_class
!= sc_none
)
27313 error_at (decl_spec_token_start
->location
,
27314 "explicit template specialization cannot have a storage class");
27315 decl
= error_mark_node
;
27318 if (decl
&& VAR_P (decl
))
27319 check_template_variable (decl
);
27322 /* Look for a trailing `;' after the declaration. */
27323 if (!function_definition_p
27324 && (decl
== error_mark_node
27325 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27326 cp_parser_skip_to_end_of_block_or_statement (parser
);
27329 pop_deferring_access_checks ();
27331 /* Clear any current qualification; whatever comes next is the start
27332 of something new. */
27333 parser
->scope
= NULL_TREE
;
27334 parser
->qualifying_scope
= NULL_TREE
;
27335 parser
->object_scope
= NULL_TREE
;
27340 /* Parse a cast-expression that is not the operand of a unary "&". */
27343 cp_parser_simple_cast_expression (cp_parser
*parser
)
27345 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27346 /*cast_p=*/false, /*decltype*/false, NULL
);
27349 /* Parse a functional cast to TYPE. Returns an expression
27350 representing the cast. */
27353 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27355 vec
<tree
, va_gc
> *vec
;
27356 tree expression_list
;
27360 location_t start_loc
= input_location
;
27363 type
= error_mark_node
;
27365 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27367 cp_lexer_set_source_position (parser
->lexer
);
27368 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27369 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27370 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27371 if (TREE_CODE (type
) == TYPE_DECL
)
27372 type
= TREE_TYPE (type
);
27374 cast
= finish_compound_literal (type
, expression_list
,
27375 tf_warning_or_error
, fcl_functional
);
27376 /* Create a location of the form:
27379 with caret == start at the start of the type name,
27380 finishing at the closing brace. */
27381 location_t finish_loc
27382 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27383 location_t combined_loc
= make_location (start_loc
, start_loc
,
27385 cast
.set_location (combined_loc
);
27390 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27392 /*allow_expansion_p=*/true,
27393 /*non_constant_p=*/NULL
);
27395 expression_list
= error_mark_node
;
27398 expression_list
= build_tree_list_vec (vec
);
27399 release_tree_vector (vec
);
27402 cast
= build_functional_cast (type
, expression_list
,
27403 tf_warning_or_error
);
27404 /* [expr.const]/1: In an integral constant expression "only type
27405 conversions to integral or enumeration type can be used". */
27406 if (TREE_CODE (type
) == TYPE_DECL
)
27407 type
= TREE_TYPE (type
);
27408 if (cast
!= error_mark_node
27409 && !cast_valid_in_integral_constant_expression_p (type
)
27410 && cp_parser_non_integral_constant_expression (parser
,
27412 return error_mark_node
;
27414 /* Create a location of the form:
27417 with caret == start at the start of the type name,
27418 finishing at the closing paren. */
27419 location_t finish_loc
27420 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27421 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27422 cast
.set_location (combined_loc
);
27426 /* Save the tokens that make up the body of a member function defined
27427 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27428 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27429 specifiers applied to the declaration. Returns the FUNCTION_DECL
27430 for the member function. */
27433 cp_parser_save_member_function_body (cp_parser
* parser
,
27434 cp_decl_specifier_seq
*decl_specifiers
,
27435 cp_declarator
*declarator
,
27441 bool function_try_block
= false;
27443 /* Create the FUNCTION_DECL. */
27444 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27445 cp_finalize_omp_declare_simd (parser
, fn
);
27446 cp_finalize_oacc_routine (parser
, fn
, true);
27447 /* If something went badly wrong, bail out now. */
27448 if (fn
== error_mark_node
)
27450 /* If there's a function-body, skip it. */
27451 if (cp_parser_token_starts_function_definition_p
27452 (cp_lexer_peek_token (parser
->lexer
)))
27453 cp_parser_skip_to_end_of_block_or_statement (parser
);
27454 return error_mark_node
;
27457 /* Remember it, if there default args to post process. */
27458 cp_parser_save_default_args (parser
, fn
);
27460 /* Save away the tokens that make up the body of the
27462 first
= parser
->lexer
->next_token
;
27464 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27465 cp_lexer_consume_token (parser
->lexer
);
27466 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27467 RID_TRANSACTION_ATOMIC
))
27469 cp_lexer_consume_token (parser
->lexer
);
27470 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27472 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27473 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27474 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27475 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27476 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27478 cp_lexer_consume_token (parser
->lexer
);
27479 cp_lexer_consume_token (parser
->lexer
);
27480 cp_lexer_consume_token (parser
->lexer
);
27481 cp_lexer_consume_token (parser
->lexer
);
27482 cp_lexer_consume_token (parser
->lexer
);
27485 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27486 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27488 cp_lexer_consume_token (parser
->lexer
);
27489 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27494 /* Handle function try blocks. */
27495 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27497 cp_lexer_consume_token (parser
->lexer
);
27498 function_try_block
= true;
27500 /* We can have braced-init-list mem-initializers before the fn body. */
27501 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27503 cp_lexer_consume_token (parser
->lexer
);
27504 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27506 /* cache_group will stop after an un-nested { } pair, too. */
27507 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27510 /* variadic mem-inits have ... after the ')'. */
27511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27512 cp_lexer_consume_token (parser
->lexer
);
27515 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27516 /* Handle function try blocks. */
27517 if (function_try_block
)
27518 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27519 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27520 last
= parser
->lexer
->next_token
;
27522 /* Save away the inline definition; we will process it when the
27523 class is complete. */
27524 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27525 DECL_PENDING_INLINE_P (fn
) = 1;
27527 /* We need to know that this was defined in the class, so that
27528 friend templates are handled correctly. */
27529 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27531 /* Add FN to the queue of functions to be parsed later. */
27532 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27537 /* Save the tokens that make up the in-class initializer for a non-static
27538 data member. Returns a DEFAULT_ARG. */
27541 cp_parser_save_nsdmi (cp_parser
* parser
)
27543 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27546 /* Parse a template-argument-list, as well as the trailing ">" (but
27547 not the opening "<"). See cp_parser_template_argument_list for the
27551 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27555 tree saved_qualifying_scope
;
27556 tree saved_object_scope
;
27557 bool saved_greater_than_is_operator_p
;
27558 int saved_unevaluated_operand
;
27559 int saved_inhibit_evaluation_warnings
;
27563 When parsing a template-id, the first non-nested `>' is taken as
27564 the end of the template-argument-list rather than a greater-than
27566 saved_greater_than_is_operator_p
27567 = parser
->greater_than_is_operator_p
;
27568 parser
->greater_than_is_operator_p
= false;
27569 /* Parsing the argument list may modify SCOPE, so we save it
27571 saved_scope
= parser
->scope
;
27572 saved_qualifying_scope
= parser
->qualifying_scope
;
27573 saved_object_scope
= parser
->object_scope
;
27574 /* We need to evaluate the template arguments, even though this
27575 template-id may be nested within a "sizeof". */
27576 saved_unevaluated_operand
= cp_unevaluated_operand
;
27577 cp_unevaluated_operand
= 0;
27578 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27579 c_inhibit_evaluation_warnings
= 0;
27580 /* Parse the template-argument-list itself. */
27581 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27582 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27583 arguments
= NULL_TREE
;
27585 arguments
= cp_parser_template_argument_list (parser
);
27586 /* Look for the `>' that ends the template-argument-list. If we find
27587 a '>>' instead, it's probably just a typo. */
27588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27590 if (cxx_dialect
!= cxx98
)
27592 /* In C++0x, a `>>' in a template argument list or cast
27593 expression is considered to be two separate `>'
27594 tokens. So, change the current token to a `>', but don't
27595 consume it: it will be consumed later when the outer
27596 template argument list (or cast expression) is parsed.
27597 Note that this replacement of `>' for `>>' is necessary
27598 even if we are parsing tentatively: in the tentative
27599 case, after calling
27600 cp_parser_enclosed_template_argument_list we will always
27601 throw away all of the template arguments and the first
27602 closing `>', either because the template argument list
27603 was erroneous or because we are replacing those tokens
27604 with a CPP_TEMPLATE_ID token. The second `>' (which will
27605 not have been thrown away) is needed either to close an
27606 outer template argument list or to complete a new-style
27608 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27609 token
->type
= CPP_GREATER
;
27611 else if (!saved_greater_than_is_operator_p
)
27613 /* If we're in a nested template argument list, the '>>' has
27614 to be a typo for '> >'. We emit the error message, but we
27615 continue parsing and we push a '>' as next token, so that
27616 the argument list will be parsed correctly. Note that the
27617 global source location is still on the token before the
27618 '>>', so we need to say explicitly where we want it. */
27619 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27620 gcc_rich_location
richloc (token
->location
);
27621 richloc
.add_fixit_replace ("> >");
27622 error_at (&richloc
, "%<>>%> should be %<> >%> "
27623 "within a nested template argument list");
27625 token
->type
= CPP_GREATER
;
27629 /* If this is not a nested template argument list, the '>>'
27630 is a typo for '>'. Emit an error message and continue.
27631 Same deal about the token location, but here we can get it
27632 right by consuming the '>>' before issuing the diagnostic. */
27633 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27634 error_at (token
->location
,
27635 "spurious %<>>%>, use %<>%> to terminate "
27636 "a template argument list");
27640 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27641 /* The `>' token might be a greater-than operator again now. */
27642 parser
->greater_than_is_operator_p
27643 = saved_greater_than_is_operator_p
;
27644 /* Restore the SAVED_SCOPE. */
27645 parser
->scope
= saved_scope
;
27646 parser
->qualifying_scope
= saved_qualifying_scope
;
27647 parser
->object_scope
= saved_object_scope
;
27648 cp_unevaluated_operand
= saved_unevaluated_operand
;
27649 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27654 /* MEMBER_FUNCTION is a member function, or a friend. If default
27655 arguments, or the body of the function have not yet been parsed,
27659 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27661 timevar_push (TV_PARSE_INMETH
);
27662 /* If this member is a template, get the underlying
27664 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27665 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27667 /* There should not be any class definitions in progress at this
27668 point; the bodies of members are only parsed outside of all class
27670 gcc_assert (parser
->num_classes_being_defined
== 0);
27671 /* While we're parsing the member functions we might encounter more
27672 classes. We want to handle them right away, but we don't want
27673 them getting mixed up with functions that are currently in the
27675 push_unparsed_function_queues (parser
);
27677 /* Make sure that any template parameters are in scope. */
27678 maybe_begin_member_template_processing (member_function
);
27680 /* If the body of the function has not yet been parsed, parse it
27682 if (DECL_PENDING_INLINE_P (member_function
))
27684 tree function_scope
;
27685 cp_token_cache
*tokens
;
27687 /* The function is no longer pending; we are processing it. */
27688 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27689 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27690 DECL_PENDING_INLINE_P (member_function
) = 0;
27692 /* If this is a local class, enter the scope of the containing
27694 function_scope
= current_function_decl
;
27695 if (function_scope
)
27696 push_function_context ();
27698 /* Push the body of the function onto the lexer stack. */
27699 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27701 /* Let the front end know that we going to be defining this
27703 start_preparsed_function (member_function
, NULL_TREE
,
27704 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27706 /* Don't do access checking if it is a templated function. */
27707 if (processing_template_decl
)
27708 push_deferring_access_checks (dk_no_check
);
27710 /* #pragma omp declare reduction needs special parsing. */
27711 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27713 parser
->lexer
->in_pragma
= true;
27714 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27715 finish_function (/*inline_p=*/true);
27716 cp_check_omp_declare_reduction (member_function
);
27719 /* Now, parse the body of the function. */
27720 cp_parser_function_definition_after_declarator (parser
,
27721 /*inline_p=*/true);
27723 if (processing_template_decl
)
27724 pop_deferring_access_checks ();
27726 /* Leave the scope of the containing function. */
27727 if (function_scope
)
27728 pop_function_context ();
27729 cp_parser_pop_lexer (parser
);
27732 /* Remove any template parameters from the symbol table. */
27733 maybe_end_member_template_processing ();
27735 /* Restore the queue. */
27736 pop_unparsed_function_queues (parser
);
27737 timevar_pop (TV_PARSE_INMETH
);
27740 /* If DECL contains any default args, remember it on the unparsed
27741 functions queue. */
27744 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27748 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27750 probe
= TREE_CHAIN (probe
))
27751 if (TREE_PURPOSE (probe
))
27753 cp_default_arg_entry entry
= {current_class_type
, decl
};
27754 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27759 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27760 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27761 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27762 from the parameter-type-list. */
27765 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27766 tree default_arg
, tree parmtype
)
27768 cp_token_cache
*tokens
;
27772 if (default_arg
== error_mark_node
)
27773 return error_mark_node
;
27775 /* Push the saved tokens for the default argument onto the parser's
27777 tokens
= DEFARG_TOKENS (default_arg
);
27778 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27780 start_lambda_scope (decl
);
27782 /* Parse the default argument. */
27783 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27784 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27785 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27787 finish_lambda_scope ();
27789 if (parsed_arg
== error_mark_node
)
27790 cp_parser_skip_to_end_of_statement (parser
);
27792 if (!processing_template_decl
)
27794 /* In a non-template class, check conversions now. In a template,
27795 we'll wait and instantiate these as needed. */
27796 if (TREE_CODE (decl
) == PARM_DECL
)
27797 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27798 tf_warning_or_error
);
27799 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27800 parsed_arg
= error_mark_node
;
27802 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27805 /* If the token stream has not been completely used up, then
27806 there was extra junk after the end of the default
27808 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27810 if (TREE_CODE (decl
) == PARM_DECL
)
27811 cp_parser_error (parser
, "expected %<,%>");
27813 cp_parser_error (parser
, "expected %<;%>");
27816 /* Revert to the main lexer. */
27817 cp_parser_pop_lexer (parser
);
27822 /* FIELD is a non-static data member with an initializer which we saved for
27823 later; parse it now. */
27826 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27830 maybe_begin_member_template_processing (field
);
27832 push_unparsed_function_queues (parser
);
27833 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27834 DECL_INITIAL (field
),
27836 pop_unparsed_function_queues (parser
);
27838 maybe_end_member_template_processing ();
27840 DECL_INITIAL (field
) = def
;
27843 /* FN is a FUNCTION_DECL which may contains a parameter with an
27844 unparsed DEFAULT_ARG. Parse the default args now. This function
27845 assumes that the current scope is the scope in which the default
27846 argument should be processed. */
27849 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27851 bool saved_local_variables_forbidden_p
;
27852 tree parm
, parmdecl
;
27854 /* While we're parsing the default args, we might (due to the
27855 statement expression extension) encounter more classes. We want
27856 to handle them right away, but we don't want them getting mixed
27857 up with default args that are currently in the queue. */
27858 push_unparsed_function_queues (parser
);
27860 /* Local variable names (and the `this' keyword) may not appear
27861 in a default argument. */
27862 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27863 parser
->local_variables_forbidden_p
= true;
27865 push_defarg_context (fn
);
27867 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27868 parmdecl
= DECL_ARGUMENTS (fn
);
27869 parm
&& parm
!= void_list_node
;
27870 parm
= TREE_CHAIN (parm
),
27871 parmdecl
= DECL_CHAIN (parmdecl
))
27873 tree default_arg
= TREE_PURPOSE (parm
);
27875 vec
<tree
, va_gc
> *insts
;
27882 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27883 /* This can happen for a friend declaration for a function
27884 already declared with default arguments. */
27888 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27890 TREE_VALUE (parm
));
27891 TREE_PURPOSE (parm
) = parsed_arg
;
27893 /* Update any instantiations we've already created. */
27894 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27895 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27896 TREE_PURPOSE (copy
) = parsed_arg
;
27899 pop_defarg_context ();
27901 /* Make sure no default arg is missing. */
27902 check_default_args (fn
);
27904 /* Restore the state of local_variables_forbidden_p. */
27905 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27907 /* Restore the queue. */
27908 pop_unparsed_function_queues (parser
);
27911 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27913 sizeof ... ( identifier )
27915 where the 'sizeof' token has already been consumed. */
27918 cp_parser_sizeof_pack (cp_parser
*parser
)
27920 /* Consume the `...'. */
27921 cp_lexer_consume_token (parser
->lexer
);
27922 maybe_warn_variadic_templates ();
27924 matching_parens parens
;
27925 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27927 parens
.consume_open (parser
);
27929 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27930 "%<sizeof...%> argument must be surrounded by parentheses");
27932 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27933 tree name
= cp_parser_identifier (parser
);
27934 if (name
== error_mark_node
)
27935 return error_mark_node
;
27936 /* The name is not qualified. */
27937 parser
->scope
= NULL_TREE
;
27938 parser
->qualifying_scope
= NULL_TREE
;
27939 parser
->object_scope
= NULL_TREE
;
27940 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27941 if (expr
== error_mark_node
)
27942 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27944 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27945 expr
= TREE_TYPE (expr
);
27946 else if (TREE_CODE (expr
) == CONST_DECL
)
27947 expr
= DECL_INITIAL (expr
);
27948 expr
= make_pack_expansion (expr
);
27949 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27952 parens
.require_close (parser
);
27957 /* Parse the operand of `sizeof' (or a similar operator). Returns
27958 either a TYPE or an expression, depending on the form of the
27959 input. The KEYWORD indicates which kind of expression we have
27963 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27965 tree expr
= NULL_TREE
;
27966 const char *saved_message
;
27968 bool saved_integral_constant_expression_p
;
27969 bool saved_non_integral_constant_expression_p
;
27971 /* If it's a `...', then we are computing the length of a parameter
27973 if (keyword
== RID_SIZEOF
27974 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27975 return cp_parser_sizeof_pack (parser
);
27977 /* Types cannot be defined in a `sizeof' expression. Save away the
27979 saved_message
= parser
->type_definition_forbidden_message
;
27980 /* And create the new one. */
27981 tmp
= concat ("types may not be defined in %<",
27982 IDENTIFIER_POINTER (ridpointers
[keyword
]),
27983 "%> expressions", NULL
);
27984 parser
->type_definition_forbidden_message
= tmp
;
27986 /* The restrictions on constant-expressions do not apply inside
27987 sizeof expressions. */
27988 saved_integral_constant_expression_p
27989 = parser
->integral_constant_expression_p
;
27990 saved_non_integral_constant_expression_p
27991 = parser
->non_integral_constant_expression_p
;
27992 parser
->integral_constant_expression_p
= false;
27994 /* Do not actually evaluate the expression. */
27995 ++cp_unevaluated_operand
;
27996 ++c_inhibit_evaluation_warnings
;
27997 /* If it's a `(', then we might be looking at the type-id
27999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28001 tree type
= NULL_TREE
;
28003 /* We can't be sure yet whether we're looking at a type-id or an
28005 cp_parser_parse_tentatively (parser
);
28007 matching_parens parens
;
28008 parens
.consume_open (parser
);
28010 /* Note: as a GNU Extension, compound literals are considered
28011 postfix-expressions as they are in C99, so they are valid
28012 arguments to sizeof. See comment in cp_parser_cast_expression
28014 if (cp_parser_compound_literal_p (parser
))
28015 cp_parser_simulate_error (parser
);
28018 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28019 parser
->in_type_id_in_expr_p
= true;
28020 /* Look for the type-id. */
28021 type
= cp_parser_type_id (parser
);
28022 /* Look for the closing `)'. */
28023 parens
.require_close (parser
);
28024 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28027 /* If all went well, then we're done. */
28028 if (cp_parser_parse_definitely (parser
))
28030 cp_decl_specifier_seq decl_specs
;
28032 /* Build a trivial decl-specifier-seq. */
28033 clear_decl_specs (&decl_specs
);
28034 decl_specs
.type
= type
;
28036 /* Call grokdeclarator to figure out what type this is. */
28037 expr
= grokdeclarator (NULL
,
28041 /*attrlist=*/NULL
);
28045 /* If the type-id production did not work out, then we must be
28046 looking at the unary-expression production. */
28048 expr
= cp_parser_unary_expression (parser
);
28050 /* Go back to evaluating expressions. */
28051 --cp_unevaluated_operand
;
28052 --c_inhibit_evaluation_warnings
;
28054 /* Free the message we created. */
28056 /* And restore the old one. */
28057 parser
->type_definition_forbidden_message
= saved_message
;
28058 parser
->integral_constant_expression_p
28059 = saved_integral_constant_expression_p
;
28060 parser
->non_integral_constant_expression_p
28061 = saved_non_integral_constant_expression_p
;
28066 /* If the current declaration has no declarator, return true. */
28069 cp_parser_declares_only_class_p (cp_parser
*parser
)
28071 /* If the next token is a `;' or a `,' then there is no
28073 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28074 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28077 /* Update the DECL_SPECS to reflect the storage class indicated by
28081 cp_parser_set_storage_class (cp_parser
*parser
,
28082 cp_decl_specifier_seq
*decl_specs
,
28086 cp_storage_class storage_class
;
28088 if (parser
->in_unbraced_linkage_specification_p
)
28090 error_at (token
->location
, "invalid use of %qD in linkage specification",
28091 ridpointers
[keyword
]);
28094 else if (decl_specs
->storage_class
!= sc_none
)
28096 decl_specs
->conflicting_specifiers_p
= true;
28100 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28101 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28102 && decl_specs
->gnu_thread_keyword_p
)
28104 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28105 "%<__thread%> before %qD", ridpointers
[keyword
]);
28111 storage_class
= sc_auto
;
28114 storage_class
= sc_register
;
28117 storage_class
= sc_static
;
28120 storage_class
= sc_extern
;
28123 storage_class
= sc_mutable
;
28126 gcc_unreachable ();
28128 decl_specs
->storage_class
= storage_class
;
28129 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28131 /* A storage class specifier cannot be applied alongside a typedef
28132 specifier. If there is a typedef specifier present then set
28133 conflicting_specifiers_p which will trigger an error later
28134 on in grokdeclarator. */
28135 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28136 decl_specs
->conflicting_specifiers_p
= true;
28139 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28140 is true, the type is a class or enum definition. */
28143 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28146 bool type_definition_p
)
28148 decl_specs
->any_specifiers_p
= true;
28150 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28151 (with, for example, in "typedef int wchar_t;") we remember that
28152 this is what happened. In system headers, we ignore these
28153 declarations so that G++ can work with system headers that are not
28155 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28156 && !type_definition_p
28157 && (type_spec
== boolean_type_node
28158 || type_spec
== char16_type_node
28159 || type_spec
== char32_type_node
28160 || type_spec
== wchar_type_node
)
28161 && (decl_specs
->type
28162 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28163 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28164 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28165 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28167 decl_specs
->redefined_builtin_type
= type_spec
;
28168 set_and_check_decl_spec_loc (decl_specs
,
28169 ds_redefined_builtin_type_spec
,
28171 if (!decl_specs
->type
)
28173 decl_specs
->type
= type_spec
;
28174 decl_specs
->type_definition_p
= false;
28175 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28178 else if (decl_specs
->type
)
28179 decl_specs
->multiple_types_p
= true;
28182 decl_specs
->type
= type_spec
;
28183 decl_specs
->type_definition_p
= type_definition_p
;
28184 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28185 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28189 /* True iff TOKEN is the GNU keyword __thread. */
28192 token_is__thread (cp_token
*token
)
28194 gcc_assert (token
->keyword
== RID_THREAD
);
28195 return id_equal (token
->u
.value
, "__thread");
28198 /* Set the location for a declarator specifier and check if it is
28201 DECL_SPECS is the sequence of declarator specifiers onto which to
28204 DS is the single declarator specifier to set which location is to
28205 be set onto the existing sequence of declarators.
28207 LOCATION is the location for the declarator specifier to
28211 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28212 cp_decl_spec ds
, cp_token
*token
)
28214 gcc_assert (ds
< ds_last
);
28216 if (decl_specs
== NULL
)
28219 source_location location
= token
->location
;
28221 if (decl_specs
->locations
[ds
] == 0)
28223 decl_specs
->locations
[ds
] = location
;
28224 if (ds
== ds_thread
)
28225 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28231 if (decl_specs
->locations
[ds_long_long
] != 0)
28232 error_at (location
,
28233 "%<long long long%> is too long for GCC");
28236 decl_specs
->locations
[ds_long_long
] = location
;
28237 pedwarn_cxx98 (location
,
28239 "ISO C++ 1998 does not support %<long long%>");
28242 else if (ds
== ds_thread
)
28244 bool gnu
= token_is__thread (token
);
28245 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28246 error_at (location
,
28247 "both %<__thread%> and %<thread_local%> specified");
28250 gcc_rich_location
richloc (location
);
28251 richloc
.add_fixit_remove ();
28252 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28257 static const char *const decl_spec_names
[] = {
28274 gcc_rich_location
richloc (location
);
28275 richloc
.add_fixit_remove ();
28276 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28281 /* Return true iff the declarator specifier DS is present in the
28282 sequence of declarator specifiers DECL_SPECS. */
28285 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28288 gcc_assert (ds
< ds_last
);
28290 if (decl_specs
== NULL
)
28293 return decl_specs
->locations
[ds
] != 0;
28296 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28297 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28300 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28302 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28305 /* Issue an error message indicating that TOKEN_DESC was expected.
28306 If KEYWORD is true, it indicated this function is called by
28307 cp_parser_require_keword and the required token can only be
28308 a indicated keyword.
28310 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28311 within any error as the location of an "opening" token matching
28312 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28313 RT_CLOSE_PAREN). */
28316 cp_parser_required_error (cp_parser
*parser
,
28317 required_token token_desc
,
28319 location_t matching_location
)
28321 if (cp_parser_simulate_error (parser
))
28324 const char *gmsgid
= NULL
;
28325 switch (token_desc
)
28328 gmsgid
= G_("expected %<new%>");
28331 gmsgid
= G_("expected %<delete%>");
28334 gmsgid
= G_("expected %<return%>");
28337 gmsgid
= G_("expected %<while%>");
28340 gmsgid
= G_("expected %<extern%>");
28342 case RT_STATIC_ASSERT
:
28343 gmsgid
= G_("expected %<static_assert%>");
28346 gmsgid
= G_("expected %<decltype%>");
28349 gmsgid
= G_("expected %<operator%>");
28352 gmsgid
= G_("expected %<class%>");
28355 gmsgid
= G_("expected %<template%>");
28358 gmsgid
= G_("expected %<namespace%>");
28361 gmsgid
= G_("expected %<using%>");
28364 gmsgid
= G_("expected %<asm%>");
28367 gmsgid
= G_("expected %<try%>");
28370 gmsgid
= G_("expected %<catch%>");
28373 gmsgid
= G_("expected %<throw%>");
28376 gmsgid
= G_("expected %<__label__%>");
28379 gmsgid
= G_("expected %<@try%>");
28381 case RT_AT_SYNCHRONIZED
:
28382 gmsgid
= G_("expected %<@synchronized%>");
28385 gmsgid
= G_("expected %<@throw%>");
28387 case RT_TRANSACTION_ATOMIC
:
28388 gmsgid
= G_("expected %<__transaction_atomic%>");
28390 case RT_TRANSACTION_RELAXED
:
28391 gmsgid
= G_("expected %<__transaction_relaxed%>");
28397 if (!gmsgid
&& !keyword
)
28399 switch (token_desc
)
28402 gmsgid
= G_("expected %<;%>");
28404 case RT_OPEN_PAREN
:
28405 gmsgid
= G_("expected %<(%>");
28407 case RT_CLOSE_BRACE
:
28408 gmsgid
= G_("expected %<}%>");
28410 case RT_OPEN_BRACE
:
28411 gmsgid
= G_("expected %<{%>");
28413 case RT_CLOSE_SQUARE
:
28414 gmsgid
= G_("expected %<]%>");
28416 case RT_OPEN_SQUARE
:
28417 gmsgid
= G_("expected %<[%>");
28420 gmsgid
= G_("expected %<,%>");
28423 gmsgid
= G_("expected %<::%>");
28426 gmsgid
= G_("expected %<<%>");
28429 gmsgid
= G_("expected %<>%>");
28432 gmsgid
= G_("expected %<=%>");
28435 gmsgid
= G_("expected %<...%>");
28438 gmsgid
= G_("expected %<*%>");
28441 gmsgid
= G_("expected %<~%>");
28444 gmsgid
= G_("expected %<:%>");
28446 case RT_COLON_SCOPE
:
28447 gmsgid
= G_("expected %<:%> or %<::%>");
28449 case RT_CLOSE_PAREN
:
28450 gmsgid
= G_("expected %<)%>");
28452 case RT_COMMA_CLOSE_PAREN
:
28453 gmsgid
= G_("expected %<,%> or %<)%>");
28455 case RT_PRAGMA_EOL
:
28456 gmsgid
= G_("expected end of line");
28459 gmsgid
= G_("expected identifier");
28462 gmsgid
= G_("expected selection-statement");
28465 gmsgid
= G_("expected iteration-statement");
28468 gmsgid
= G_("expected jump-statement");
28471 gmsgid
= G_("expected class-key");
28473 case RT_CLASS_TYPENAME_TEMPLATE
:
28474 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28477 gcc_unreachable ();
28482 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28486 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28487 issue an error message indicating that TOKEN_DESC was expected.
28489 Returns the token consumed, if the token had the appropriate type.
28490 Otherwise, returns NULL.
28492 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28493 within any error as the location of an "opening" token matching
28494 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28495 RT_CLOSE_PAREN). */
28498 cp_parser_require (cp_parser
* parser
,
28499 enum cpp_ttype type
,
28500 required_token token_desc
,
28501 location_t matching_location
)
28503 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28504 return cp_lexer_consume_token (parser
->lexer
);
28507 /* Output the MESSAGE -- unless we're parsing tentatively. */
28508 if (!cp_parser_simulate_error (parser
))
28509 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28510 matching_location
);
28515 /* An error message is produced if the next token is not '>'.
28516 All further tokens are skipped until the desired token is
28517 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28520 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28522 /* Current level of '< ... >'. */
28523 unsigned level
= 0;
28524 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28525 unsigned nesting_depth
= 0;
28527 /* Are we ready, yet? If not, issue error message. */
28528 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28531 /* Skip tokens until the desired token is found. */
28534 /* Peek at the next token. */
28535 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28538 if (!nesting_depth
)
28543 if (cxx_dialect
== cxx98
)
28544 /* C++0x views the `>>' operator as two `>' tokens, but
28547 else if (!nesting_depth
&& level
-- == 0)
28549 /* We've hit a `>>' where the first `>' closes the
28550 template argument list, and the second `>' is
28551 spurious. Just consume the `>>' and stop; we've
28552 already produced at least one error. */
28553 cp_lexer_consume_token (parser
->lexer
);
28556 /* Fall through for C++0x, so we handle the second `>' in
28558 gcc_fallthrough ();
28561 if (!nesting_depth
&& level
-- == 0)
28563 /* We've reached the token we want, consume it and stop. */
28564 cp_lexer_consume_token (parser
->lexer
);
28569 case CPP_OPEN_PAREN
:
28570 case CPP_OPEN_SQUARE
:
28574 case CPP_CLOSE_PAREN
:
28575 case CPP_CLOSE_SQUARE
:
28576 if (nesting_depth
-- == 0)
28581 case CPP_PRAGMA_EOL
:
28582 case CPP_SEMICOLON
:
28583 case CPP_OPEN_BRACE
:
28584 case CPP_CLOSE_BRACE
:
28585 /* The '>' was probably forgotten, don't look further. */
28592 /* Consume this token. */
28593 cp_lexer_consume_token (parser
->lexer
);
28597 /* If the next token is the indicated keyword, consume it. Otherwise,
28598 issue an error message indicating that TOKEN_DESC was expected.
28600 Returns the token consumed, if the token had the appropriate type.
28601 Otherwise, returns NULL. */
28604 cp_parser_require_keyword (cp_parser
* parser
,
28606 required_token token_desc
)
28608 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28610 if (token
&& token
->keyword
!= keyword
)
28612 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28620 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28621 function-definition. */
28624 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28626 return (/* An ordinary function-body begins with an `{'. */
28627 token
->type
== CPP_OPEN_BRACE
28628 /* A ctor-initializer begins with a `:'. */
28629 || token
->type
== CPP_COLON
28630 /* A function-try-block begins with `try'. */
28631 || token
->keyword
== RID_TRY
28632 /* A function-transaction-block begins with `__transaction_atomic'
28633 or `__transaction_relaxed'. */
28634 || token
->keyword
== RID_TRANSACTION_ATOMIC
28635 || token
->keyword
== RID_TRANSACTION_RELAXED
28636 /* The named return value extension begins with `return'. */
28637 || token
->keyword
== RID_RETURN
);
28640 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28644 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28648 token
= cp_lexer_peek_token (parser
->lexer
);
28649 return (token
->type
== CPP_OPEN_BRACE
28650 || (token
->type
== CPP_COLON
28651 && !parser
->colon_doesnt_start_class_def_p
));
28654 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28655 C++0x) ending a template-argument. */
28658 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28662 token
= cp_lexer_peek_token (parser
->lexer
);
28663 return (token
->type
== CPP_COMMA
28664 || token
->type
== CPP_GREATER
28665 || token
->type
== CPP_ELLIPSIS
28666 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28669 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28670 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28673 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28678 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28679 if (token
->type
== CPP_LESS
)
28681 /* Check for the sequence `<::' in the original code. It would be lexed as
28682 `[:', where `[' is a digraph, and there is no whitespace before
28684 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28687 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28688 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28694 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28695 or none_type otherwise. */
28697 static enum tag_types
28698 cp_parser_token_is_class_key (cp_token
* token
)
28700 switch (token
->keyword
)
28705 return record_type
;
28714 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28715 or none_type otherwise or if the token is null. */
28717 static enum tag_types
28718 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28723 switch (token
->keyword
)
28728 return typename_type
;
28735 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28738 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28740 if (type
== error_mark_node
)
28742 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28744 if (permerror (input_location
, "%qs tag used in naming %q#T",
28745 class_key
== union_type
? "union"
28746 : class_key
== record_type
? "struct" : "class",
28748 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28749 "%q#T was previously declared here", type
);
28753 /* Issue an error message if DECL is redeclared with different
28754 access than its original declaration [class.access.spec/3].
28755 This applies to nested classes, nested class templates and
28756 enumerations [class.mem/1]. */
28759 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28762 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28763 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28766 if ((TREE_PRIVATE (decl
)
28767 != (current_access_specifier
== access_private_node
))
28768 || (TREE_PROTECTED (decl
)
28769 != (current_access_specifier
== access_protected_node
)))
28770 error_at (location
, "%qD redeclared with different access", decl
);
28773 /* Look for the `template' keyword, as a syntactic disambiguator.
28774 Return TRUE iff it is present, in which case it will be
28778 cp_parser_optional_template_keyword (cp_parser
*parser
)
28780 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28782 /* In C++98 the `template' keyword can only be used within templates;
28783 outside templates the parser can always figure out what is a
28784 template and what is not. In C++11, per the resolution of DR 468,
28785 `template' is allowed in cases where it is not strictly necessary. */
28786 if (!processing_template_decl
28787 && pedantic
&& cxx_dialect
== cxx98
)
28789 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28790 pedwarn (token
->location
, OPT_Wpedantic
,
28791 "in C++98 %<template%> (as a disambiguator) is only "
28792 "allowed within templates");
28793 /* If this part of the token stream is rescanned, the same
28794 error message would be generated. So, we purge the token
28795 from the stream. */
28796 cp_lexer_purge_token (parser
->lexer
);
28801 /* Consume the `template' keyword. */
28802 cp_lexer_consume_token (parser
->lexer
);
28809 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28810 set PARSER->SCOPE, and perform other related actions. */
28813 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28815 struct tree_check
*check_value
;
28817 /* Get the stored value. */
28818 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28819 /* Set the scope from the stored value. */
28820 parser
->scope
= saved_checks_value (check_value
);
28821 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28822 parser
->object_scope
= NULL_TREE
;
28825 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28826 encounter the end of a block before what we were looking for. */
28829 cp_parser_cache_group (cp_parser
*parser
,
28830 enum cpp_ttype end
,
28835 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28837 /* Abort a parenthesized expression if we encounter a semicolon. */
28838 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28839 && token
->type
== CPP_SEMICOLON
)
28841 /* If we've reached the end of the file, stop. */
28842 if (token
->type
== CPP_EOF
28843 || (end
!= CPP_PRAGMA_EOL
28844 && token
->type
== CPP_PRAGMA_EOL
))
28846 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28847 /* We've hit the end of an enclosing block, so there's been some
28848 kind of syntax error. */
28851 /* Consume the token. */
28852 cp_lexer_consume_token (parser
->lexer
);
28853 /* See if it starts a new group. */
28854 if (token
->type
== CPP_OPEN_BRACE
)
28856 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28857 /* In theory this should probably check end == '}', but
28858 cp_parser_save_member_function_body needs it to exit
28859 after either '}' or ')' when called with ')'. */
28863 else if (token
->type
== CPP_OPEN_PAREN
)
28865 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28866 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28869 else if (token
->type
== CPP_PRAGMA
)
28870 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28871 else if (token
->type
== end
)
28876 /* Like above, for caching a default argument or NSDMI. Both of these are
28877 terminated by a non-nested comma, but it can be unclear whether or not a
28878 comma is nested in a template argument list unless we do more parsing.
28879 In order to handle this ambiguity, when we encounter a ',' after a '<'
28880 we try to parse what follows as a parameter-declaration-list (in the
28881 case of a default argument) or a member-declarator (in the case of an
28882 NSDMI). If that succeeds, then we stop caching. */
28885 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28887 unsigned depth
= 0;
28888 int maybe_template_id
= 0;
28889 cp_token
*first_token
;
28891 tree default_argument
;
28893 /* Add tokens until we have processed the entire default
28894 argument. We add the range [first_token, token). */
28895 first_token
= cp_lexer_peek_token (parser
->lexer
);
28896 if (first_token
->type
== CPP_OPEN_BRACE
)
28898 /* For list-initialization, this is straightforward. */
28899 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28900 token
= cp_lexer_peek_token (parser
->lexer
);
28906 /* Peek at the next token. */
28907 token
= cp_lexer_peek_token (parser
->lexer
);
28908 /* What we do depends on what token we have. */
28909 switch (token
->type
)
28911 /* In valid code, a default argument must be
28912 immediately followed by a `,' `)', or `...'. */
28914 if (depth
== 0 && maybe_template_id
)
28916 /* If we've seen a '<', we might be in a
28917 template-argument-list. Until Core issue 325 is
28918 resolved, we don't know how this situation ought
28919 to be handled, so try to DTRT. We check whether
28920 what comes after the comma is a valid parameter
28921 declaration list. If it is, then the comma ends
28922 the default argument; otherwise the default
28923 argument continues. */
28924 bool error
= false;
28927 /* Set ITALP so cp_parser_parameter_declaration_list
28928 doesn't decide to commit to this parse. */
28929 bool saved_italp
= parser
->in_template_argument_list_p
;
28930 parser
->in_template_argument_list_p
= true;
28932 cp_parser_parse_tentatively (parser
);
28936 /* Parse declarators until we reach a non-comma or
28937 somthing that cannot be an initializer.
28938 Just checking whether we're looking at a single
28939 declarator is insufficient. Consider:
28940 int var = tuple<T,U>::x;
28941 The template parameter 'U' looks exactly like a
28945 int ctor_dtor_or_conv_p
;
28946 cp_lexer_consume_token (parser
->lexer
);
28947 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28948 &ctor_dtor_or_conv_p
,
28949 /*parenthesized_p=*/NULL
,
28951 /*friend_p=*/false);
28952 peek
= cp_lexer_peek_token (parser
->lexer
);
28953 if (cp_parser_error_occurred (parser
))
28956 while (peek
->type
== CPP_COMMA
);
28957 /* If we met an '=' or ';' then the original comma
28958 was the end of the NSDMI. Otherwise assume
28959 we're still in the NSDMI. */
28960 error
= (peek
->type
!= CPP_EQ
28961 && peek
->type
!= CPP_SEMICOLON
);
28965 cp_lexer_consume_token (parser
->lexer
);
28966 begin_scope (sk_function_parms
, NULL_TREE
);
28967 cp_parser_parameter_declaration_list (parser
, &error
);
28968 pop_bindings_and_leave_scope ();
28970 if (!cp_parser_error_occurred (parser
) && !error
)
28972 cp_parser_abort_tentative_parse (parser
);
28974 parser
->in_template_argument_list_p
= saved_italp
;
28978 case CPP_CLOSE_PAREN
:
28980 /* If we run into a non-nested `;', `}', or `]',
28981 then the code is invalid -- but the default
28982 argument is certainly over. */
28983 case CPP_SEMICOLON
:
28984 case CPP_CLOSE_BRACE
:
28985 case CPP_CLOSE_SQUARE
:
28987 /* Handle correctly int n = sizeof ... ( p ); */
28988 && token
->type
!= CPP_ELLIPSIS
)
28990 /* Update DEPTH, if necessary. */
28991 else if (token
->type
== CPP_CLOSE_PAREN
28992 || token
->type
== CPP_CLOSE_BRACE
28993 || token
->type
== CPP_CLOSE_SQUARE
)
28997 case CPP_OPEN_PAREN
:
28998 case CPP_OPEN_SQUARE
:
28999 case CPP_OPEN_BRACE
:
29005 /* This might be the comparison operator, or it might
29006 start a template argument list. */
29007 ++maybe_template_id
;
29011 if (cxx_dialect
== cxx98
)
29013 /* Fall through for C++0x, which treats the `>>'
29014 operator like two `>' tokens in certain
29016 gcc_fallthrough ();
29021 /* This might be an operator, or it might close a
29022 template argument list. But if a previous '<'
29023 started a template argument list, this will have
29024 closed it, so we can't be in one anymore. */
29025 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29026 if (maybe_template_id
< 0)
29027 maybe_template_id
= 0;
29031 /* If we run out of tokens, issue an error message. */
29033 case CPP_PRAGMA_EOL
:
29034 error_at (token
->location
, "file ends in default argument");
29035 return error_mark_node
;
29039 /* In these cases, we should look for template-ids.
29040 For example, if the default argument is
29041 `X<int, double>()', we need to do name lookup to
29042 figure out whether or not `X' is a template; if
29043 so, the `,' does not end the default argument.
29045 That is not yet done. */
29052 /* If we've reached the end, stop. */
29056 /* Add the token to the token block. */
29057 token
= cp_lexer_consume_token (parser
->lexer
);
29060 /* Create a DEFAULT_ARG to represent the unparsed default
29062 default_argument
= make_node (DEFAULT_ARG
);
29063 DEFARG_TOKENS (default_argument
)
29064 = cp_token_cache_new (first_token
, token
);
29065 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29067 return default_argument
;
29070 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29073 defarg_location (tree default_argument
)
29075 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29076 location_t start
= tokens
->first
->location
;
29077 location_t end
= tokens
->last
->location
;
29078 return make_location (start
, start
, end
);
29081 /* Begin parsing tentatively. We always save tokens while parsing
29082 tentatively so that if the tentative parsing fails we can restore the
29086 cp_parser_parse_tentatively (cp_parser
* parser
)
29088 /* Enter a new parsing context. */
29089 parser
->context
= cp_parser_context_new (parser
->context
);
29090 /* Begin saving tokens. */
29091 cp_lexer_save_tokens (parser
->lexer
);
29092 /* In order to avoid repetitive access control error messages,
29093 access checks are queued up until we are no longer parsing
29095 push_deferring_access_checks (dk_deferred
);
29098 /* Commit to the currently active tentative parse. */
29101 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29103 cp_parser_context
*context
;
29106 /* Mark all of the levels as committed. */
29107 lexer
= parser
->lexer
;
29108 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29110 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29112 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29113 while (!cp_lexer_saving_tokens (lexer
))
29114 lexer
= lexer
->next
;
29115 cp_lexer_commit_tokens (lexer
);
29119 /* Commit to the topmost currently active tentative parse.
29121 Note that this function shouldn't be called when there are
29122 irreversible side-effects while in a tentative state. For
29123 example, we shouldn't create a permanent entry in the symbol
29124 table, or issue an error message that might not apply if the
29125 tentative parse is aborted. */
29128 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29130 cp_parser_context
*context
= parser
->context
;
29131 cp_lexer
*lexer
= parser
->lexer
;
29135 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29137 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29139 while (!cp_lexer_saving_tokens (lexer
))
29140 lexer
= lexer
->next
;
29141 cp_lexer_commit_tokens (lexer
);
29145 /* Abort the currently active tentative parse. All consumed tokens
29146 will be rolled back, and no diagnostics will be issued. */
29149 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29151 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29152 || errorcount
> 0);
29153 cp_parser_simulate_error (parser
);
29154 /* Now, pretend that we want to see if the construct was
29155 successfully parsed. */
29156 cp_parser_parse_definitely (parser
);
29159 /* Stop parsing tentatively. If a parse error has occurred, restore the
29160 token stream. Otherwise, commit to the tokens we have consumed.
29161 Returns true if no error occurred; false otherwise. */
29164 cp_parser_parse_definitely (cp_parser
* parser
)
29166 bool error_occurred
;
29167 cp_parser_context
*context
;
29169 /* Remember whether or not an error occurred, since we are about to
29170 destroy that information. */
29171 error_occurred
= cp_parser_error_occurred (parser
);
29172 /* Remove the topmost context from the stack. */
29173 context
= parser
->context
;
29174 parser
->context
= context
->next
;
29175 /* If no parse errors occurred, commit to the tentative parse. */
29176 if (!error_occurred
)
29178 /* Commit to the tokens read tentatively, unless that was
29180 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29181 cp_lexer_commit_tokens (parser
->lexer
);
29183 pop_to_parent_deferring_access_checks ();
29185 /* Otherwise, if errors occurred, roll back our state so that things
29186 are just as they were before we began the tentative parse. */
29189 cp_lexer_rollback_tokens (parser
->lexer
);
29190 pop_deferring_access_checks ();
29192 /* Add the context to the front of the free list. */
29193 context
->next
= cp_parser_context_free_list
;
29194 cp_parser_context_free_list
= context
;
29196 return !error_occurred
;
29199 /* Returns true if we are parsing tentatively and are not committed to
29200 this tentative parse. */
29203 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29205 return (cp_parser_parsing_tentatively (parser
)
29206 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29209 /* Returns nonzero iff an error has occurred during the most recent
29210 tentative parse. */
29213 cp_parser_error_occurred (cp_parser
* parser
)
29215 return (cp_parser_parsing_tentatively (parser
)
29216 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29219 /* Returns nonzero if GNU extensions are allowed. */
29222 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29224 return parser
->allow_gnu_extensions_p
;
29227 /* Objective-C++ Productions */
29230 /* Parse an Objective-C expression, which feeds into a primary-expression
29234 objc-message-expression
29235 objc-string-literal
29236 objc-encode-expression
29237 objc-protocol-expression
29238 objc-selector-expression
29240 Returns a tree representation of the expression. */
29243 cp_parser_objc_expression (cp_parser
* parser
)
29245 /* Try to figure out what kind of declaration is present. */
29246 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29250 case CPP_OPEN_SQUARE
:
29251 return cp_parser_objc_message_expression (parser
);
29253 case CPP_OBJC_STRING
:
29254 kwd
= cp_lexer_consume_token (parser
->lexer
);
29255 return objc_build_string_object (kwd
->u
.value
);
29258 switch (kwd
->keyword
)
29260 case RID_AT_ENCODE
:
29261 return cp_parser_objc_encode_expression (parser
);
29263 case RID_AT_PROTOCOL
:
29264 return cp_parser_objc_protocol_expression (parser
);
29266 case RID_AT_SELECTOR
:
29267 return cp_parser_objc_selector_expression (parser
);
29274 error_at (kwd
->location
,
29275 "misplaced %<@%D%> Objective-C++ construct",
29277 cp_parser_skip_to_end_of_block_or_statement (parser
);
29280 return error_mark_node
;
29283 /* Parse an Objective-C message expression.
29285 objc-message-expression:
29286 [ objc-message-receiver objc-message-args ]
29288 Returns a representation of an Objective-C message. */
29291 cp_parser_objc_message_expression (cp_parser
* parser
)
29293 tree receiver
, messageargs
;
29295 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29296 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29297 receiver
= cp_parser_objc_message_receiver (parser
);
29298 messageargs
= cp_parser_objc_message_args (parser
);
29299 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29300 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29302 tree result
= objc_build_message_expr (receiver
, messageargs
);
29304 /* Construct a location e.g.
29307 ranging from the '[' to the ']', with the caret at the start. */
29308 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29309 protected_set_expr_location (result
, combined_loc
);
29314 /* Parse an objc-message-receiver.
29316 objc-message-receiver:
29318 simple-type-specifier
29320 Returns a representation of the type or expression. */
29323 cp_parser_objc_message_receiver (cp_parser
* parser
)
29327 /* An Objective-C message receiver may be either (1) a type
29328 or (2) an expression. */
29329 cp_parser_parse_tentatively (parser
);
29330 rcv
= cp_parser_expression (parser
);
29332 /* If that worked out, fine. */
29333 if (cp_parser_parse_definitely (parser
))
29336 cp_parser_parse_tentatively (parser
);
29337 rcv
= cp_parser_simple_type_specifier (parser
,
29338 /*decl_specs=*/NULL
,
29339 CP_PARSER_FLAGS_NONE
);
29341 if (cp_parser_parse_definitely (parser
))
29342 return objc_get_class_reference (rcv
);
29344 cp_parser_error (parser
, "objective-c++ message receiver expected");
29345 return error_mark_node
;
29348 /* Parse the arguments and selectors comprising an Objective-C message.
29353 objc-selector-args , objc-comma-args
29355 objc-selector-args:
29356 objc-selector [opt] : assignment-expression
29357 objc-selector-args objc-selector [opt] : assignment-expression
29360 assignment-expression
29361 objc-comma-args , assignment-expression
29363 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29364 selector arguments and TREE_VALUE containing a list of comma
29368 cp_parser_objc_message_args (cp_parser
* parser
)
29370 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29371 bool maybe_unary_selector_p
= true;
29372 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29374 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29376 tree selector
= NULL_TREE
, arg
;
29378 if (token
->type
!= CPP_COLON
)
29379 selector
= cp_parser_objc_selector (parser
);
29381 /* Detect if we have a unary selector. */
29382 if (maybe_unary_selector_p
29383 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29384 return build_tree_list (selector
, NULL_TREE
);
29386 maybe_unary_selector_p
= false;
29387 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29388 arg
= cp_parser_assignment_expression (parser
);
29391 = chainon (sel_args
,
29392 build_tree_list (selector
, arg
));
29394 token
= cp_lexer_peek_token (parser
->lexer
);
29397 /* Handle non-selector arguments, if any. */
29398 while (token
->type
== CPP_COMMA
)
29402 cp_lexer_consume_token (parser
->lexer
);
29403 arg
= cp_parser_assignment_expression (parser
);
29406 = chainon (addl_args
,
29407 build_tree_list (NULL_TREE
, arg
));
29409 token
= cp_lexer_peek_token (parser
->lexer
);
29412 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29414 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29415 return build_tree_list (error_mark_node
, error_mark_node
);
29418 return build_tree_list (sel_args
, addl_args
);
29421 /* Parse an Objective-C encode expression.
29423 objc-encode-expression:
29424 @encode objc-typename
29426 Returns an encoded representation of the type argument. */
29429 cp_parser_objc_encode_expression (cp_parser
* parser
)
29433 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29435 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29436 matching_parens parens
;
29437 parens
.require_open (parser
);
29438 token
= cp_lexer_peek_token (parser
->lexer
);
29439 type
= complete_type (cp_parser_type_id (parser
));
29440 parens
.require_close (parser
);
29444 error_at (token
->location
,
29445 "%<@encode%> must specify a type as an argument");
29446 return error_mark_node
;
29449 /* This happens if we find @encode(T) (where T is a template
29450 typename or something dependent on a template typename) when
29451 parsing a template. In that case, we can't compile it
29452 immediately, but we rather create an AT_ENCODE_EXPR which will
29453 need to be instantiated when the template is used.
29455 if (dependent_type_p (type
))
29457 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29458 TREE_READONLY (value
) = 1;
29463 /* Build a location of the form:
29466 with caret==start at the @ token, finishing at the close paren. */
29467 location_t combined_loc
29468 = make_location (start_loc
, start_loc
,
29469 cp_lexer_previous_token (parser
->lexer
)->location
);
29471 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29474 /* Parse an Objective-C @defs expression. */
29477 cp_parser_objc_defs_expression (cp_parser
*parser
)
29481 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29482 matching_parens parens
;
29483 parens
.require_open (parser
);
29484 name
= cp_parser_identifier (parser
);
29485 parens
.require_close (parser
);
29487 return objc_get_class_ivars (name
);
29490 /* Parse an Objective-C protocol expression.
29492 objc-protocol-expression:
29493 @protocol ( identifier )
29495 Returns a representation of the protocol expression. */
29498 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29501 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29503 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29504 matching_parens parens
;
29505 parens
.require_open (parser
);
29506 proto
= cp_parser_identifier (parser
);
29507 parens
.require_close (parser
);
29509 /* Build a location of the form:
29512 with caret==start at the @ token, finishing at the close paren. */
29513 location_t combined_loc
29514 = make_location (start_loc
, start_loc
,
29515 cp_lexer_previous_token (parser
->lexer
)->location
);
29516 tree result
= objc_build_protocol_expr (proto
);
29517 protected_set_expr_location (result
, combined_loc
);
29521 /* Parse an Objective-C selector expression.
29523 objc-selector-expression:
29524 @selector ( objc-method-signature )
29526 objc-method-signature:
29532 objc-selector-seq objc-selector :
29534 Returns a representation of the method selector. */
29537 cp_parser_objc_selector_expression (cp_parser
* parser
)
29539 tree sel_seq
= NULL_TREE
;
29540 bool maybe_unary_selector_p
= true;
29542 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29544 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29545 matching_parens parens
;
29546 parens
.require_open (parser
);
29547 token
= cp_lexer_peek_token (parser
->lexer
);
29549 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29550 || token
->type
== CPP_SCOPE
)
29552 tree selector
= NULL_TREE
;
29554 if (token
->type
!= CPP_COLON
29555 || token
->type
== CPP_SCOPE
)
29556 selector
= cp_parser_objc_selector (parser
);
29558 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29559 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29561 /* Detect if we have a unary selector. */
29562 if (maybe_unary_selector_p
)
29564 sel_seq
= selector
;
29565 goto finish_selector
;
29569 cp_parser_error (parser
, "expected %<:%>");
29572 maybe_unary_selector_p
= false;
29573 token
= cp_lexer_consume_token (parser
->lexer
);
29575 if (token
->type
== CPP_SCOPE
)
29578 = chainon (sel_seq
,
29579 build_tree_list (selector
, NULL_TREE
));
29581 = chainon (sel_seq
,
29582 build_tree_list (NULL_TREE
, NULL_TREE
));
29586 = chainon (sel_seq
,
29587 build_tree_list (selector
, NULL_TREE
));
29589 token
= cp_lexer_peek_token (parser
->lexer
);
29593 parens
.require_close (parser
);
29596 /* Build a location of the form:
29599 with caret==start at the @ token, finishing at the close paren. */
29600 location_t combined_loc
29601 = make_location (loc
, loc
,
29602 cp_lexer_previous_token (parser
->lexer
)->location
);
29603 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29604 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29605 protected_set_expr_location (result
, combined_loc
);
29609 /* Parse a list of identifiers.
29611 objc-identifier-list:
29613 objc-identifier-list , identifier
29615 Returns a TREE_LIST of identifier nodes. */
29618 cp_parser_objc_identifier_list (cp_parser
* parser
)
29624 identifier
= cp_parser_identifier (parser
);
29625 if (identifier
== error_mark_node
)
29626 return error_mark_node
;
29628 list
= build_tree_list (NULL_TREE
, identifier
);
29629 sep
= cp_lexer_peek_token (parser
->lexer
);
29631 while (sep
->type
== CPP_COMMA
)
29633 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29634 identifier
= cp_parser_identifier (parser
);
29635 if (identifier
== error_mark_node
)
29638 list
= chainon (list
, build_tree_list (NULL_TREE
,
29640 sep
= cp_lexer_peek_token (parser
->lexer
);
29646 /* Parse an Objective-C alias declaration.
29648 objc-alias-declaration:
29649 @compatibility_alias identifier identifier ;
29651 This function registers the alias mapping with the Objective-C front end.
29652 It returns nothing. */
29655 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29659 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29660 alias
= cp_parser_identifier (parser
);
29661 orig
= cp_parser_identifier (parser
);
29662 objc_declare_alias (alias
, orig
);
29663 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29666 /* Parse an Objective-C class forward-declaration.
29668 objc-class-declaration:
29669 @class objc-identifier-list ;
29671 The function registers the forward declarations with the Objective-C
29672 front end. It returns nothing. */
29675 cp_parser_objc_class_declaration (cp_parser
* parser
)
29677 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29682 id
= cp_parser_identifier (parser
);
29683 if (id
== error_mark_node
)
29686 objc_declare_class (id
);
29688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29689 cp_lexer_consume_token (parser
->lexer
);
29693 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29696 /* Parse a list of Objective-C protocol references.
29698 objc-protocol-refs-opt:
29699 objc-protocol-refs [opt]
29701 objc-protocol-refs:
29702 < objc-identifier-list >
29704 Returns a TREE_LIST of identifiers, if any. */
29707 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29709 tree protorefs
= NULL_TREE
;
29711 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29713 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29714 protorefs
= cp_parser_objc_identifier_list (parser
);
29715 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29721 /* Parse a Objective-C visibility specification. */
29724 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29726 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29728 switch (vis
->keyword
)
29730 case RID_AT_PRIVATE
:
29731 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29733 case RID_AT_PROTECTED
:
29734 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29736 case RID_AT_PUBLIC
:
29737 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29739 case RID_AT_PACKAGE
:
29740 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29746 /* Eat '@private'/'@protected'/'@public'. */
29747 cp_lexer_consume_token (parser
->lexer
);
29750 /* Parse an Objective-C method type. Return 'true' if it is a class
29751 (+) method, and 'false' if it is an instance (-) method. */
29754 cp_parser_objc_method_type (cp_parser
* parser
)
29756 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29762 /* Parse an Objective-C protocol qualifier. */
29765 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29767 tree quals
= NULL_TREE
, node
;
29768 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29770 node
= token
->u
.value
;
29772 while (node
&& identifier_p (node
)
29773 && (node
== ridpointers
[(int) RID_IN
]
29774 || node
== ridpointers
[(int) RID_OUT
]
29775 || node
== ridpointers
[(int) RID_INOUT
]
29776 || node
== ridpointers
[(int) RID_BYCOPY
]
29777 || node
== ridpointers
[(int) RID_BYREF
]
29778 || node
== ridpointers
[(int) RID_ONEWAY
]))
29780 quals
= tree_cons (NULL_TREE
, node
, quals
);
29781 cp_lexer_consume_token (parser
->lexer
);
29782 token
= cp_lexer_peek_token (parser
->lexer
);
29783 node
= token
->u
.value
;
29789 /* Parse an Objective-C typename. */
29792 cp_parser_objc_typename (cp_parser
* parser
)
29794 tree type_name
= NULL_TREE
;
29796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29798 tree proto_quals
, cp_type
= NULL_TREE
;
29800 matching_parens parens
;
29801 parens
.consume_open (parser
); /* Eat '('. */
29802 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29804 /* An ObjC type name may consist of just protocol qualifiers, in which
29805 case the type shall default to 'id'. */
29806 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29808 cp_type
= cp_parser_type_id (parser
);
29810 /* If the type could not be parsed, an error has already
29811 been produced. For error recovery, behave as if it had
29812 not been specified, which will use the default type
29814 if (cp_type
== error_mark_node
)
29816 cp_type
= NULL_TREE
;
29817 /* We need to skip to the closing parenthesis as
29818 cp_parser_type_id() does not seem to do it for
29820 cp_parser_skip_to_closing_parenthesis (parser
,
29821 /*recovering=*/true,
29822 /*or_comma=*/false,
29823 /*consume_paren=*/false);
29827 parens
.require_close (parser
);
29828 type_name
= build_tree_list (proto_quals
, cp_type
);
29834 /* Check to see if TYPE refers to an Objective-C selector name. */
29837 cp_parser_objc_selector_p (enum cpp_ttype type
)
29839 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29840 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29841 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29842 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29843 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29846 /* Parse an Objective-C selector. */
29849 cp_parser_objc_selector (cp_parser
* parser
)
29851 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29853 if (!cp_parser_objc_selector_p (token
->type
))
29855 error_at (token
->location
, "invalid Objective-C++ selector name");
29856 return error_mark_node
;
29859 /* C++ operator names are allowed to appear in ObjC selectors. */
29860 switch (token
->type
)
29862 case CPP_AND_AND
: return get_identifier ("and");
29863 case CPP_AND_EQ
: return get_identifier ("and_eq");
29864 case CPP_AND
: return get_identifier ("bitand");
29865 case CPP_OR
: return get_identifier ("bitor");
29866 case CPP_COMPL
: return get_identifier ("compl");
29867 case CPP_NOT
: return get_identifier ("not");
29868 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29869 case CPP_OR_OR
: return get_identifier ("or");
29870 case CPP_OR_EQ
: return get_identifier ("or_eq");
29871 case CPP_XOR
: return get_identifier ("xor");
29872 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29873 default: return token
->u
.value
;
29877 /* Parse an Objective-C params list. */
29880 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29882 tree params
= NULL_TREE
;
29883 bool maybe_unary_selector_p
= true;
29884 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29886 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29888 tree selector
= NULL_TREE
, type_name
, identifier
;
29889 tree parm_attr
= NULL_TREE
;
29891 if (token
->keyword
== RID_ATTRIBUTE
)
29894 if (token
->type
!= CPP_COLON
)
29895 selector
= cp_parser_objc_selector (parser
);
29897 /* Detect if we have a unary selector. */
29898 if (maybe_unary_selector_p
29899 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29901 params
= selector
; /* Might be followed by attributes. */
29905 maybe_unary_selector_p
= false;
29906 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29908 /* Something went quite wrong. There should be a colon
29909 here, but there is not. Stop parsing parameters. */
29912 type_name
= cp_parser_objc_typename (parser
);
29913 /* New ObjC allows attributes on parameters too. */
29914 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29915 parm_attr
= cp_parser_attributes_opt (parser
);
29916 identifier
= cp_parser_identifier (parser
);
29920 objc_build_keyword_decl (selector
,
29925 token
= cp_lexer_peek_token (parser
->lexer
);
29928 if (params
== NULL_TREE
)
29930 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29931 return error_mark_node
;
29934 /* We allow tail attributes for the method. */
29935 if (token
->keyword
== RID_ATTRIBUTE
)
29937 *attributes
= cp_parser_attributes_opt (parser
);
29938 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29939 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29941 cp_parser_error (parser
,
29942 "method attributes must be specified at the end");
29943 return error_mark_node
;
29946 if (params
== NULL_TREE
)
29948 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29949 return error_mark_node
;
29954 /* Parse the non-keyword Objective-C params. */
29957 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29960 tree params
= make_node (TREE_LIST
);
29961 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29962 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29964 while (token
->type
== CPP_COMMA
)
29966 cp_parameter_declarator
*parmdecl
;
29969 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29970 token
= cp_lexer_peek_token (parser
->lexer
);
29972 if (token
->type
== CPP_ELLIPSIS
)
29974 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29976 token
= cp_lexer_peek_token (parser
->lexer
);
29980 /* TODO: parse attributes for tail parameters. */
29981 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
29982 parm
= grokdeclarator (parmdecl
->declarator
,
29983 &parmdecl
->decl_specifiers
,
29984 PARM
, /*initialized=*/0,
29985 /*attrlist=*/NULL
);
29987 chainon (params
, build_tree_list (NULL_TREE
, parm
));
29988 token
= cp_lexer_peek_token (parser
->lexer
);
29991 /* We allow tail attributes for the method. */
29992 if (token
->keyword
== RID_ATTRIBUTE
)
29994 if (*attributes
== NULL_TREE
)
29996 *attributes
= cp_parser_attributes_opt (parser
);
29997 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29998 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30002 /* We have an error, but parse the attributes, so that we can
30004 *attributes
= cp_parser_attributes_opt (parser
);
30006 cp_parser_error (parser
,
30007 "method attributes must be specified at the end");
30008 return error_mark_node
;
30014 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30017 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30019 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30021 /* If the next token is `extern' and the following token is a string
30022 literal, then we have a linkage specification. */
30023 if (token
->keyword
== RID_EXTERN
30024 && cp_parser_is_pure_string_literal
30025 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30026 cp_parser_linkage_specification (parser
);
30027 /* Handle #pragma, if any. */
30028 else if (token
->type
== CPP_PRAGMA
)
30029 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30030 /* Allow stray semicolons. */
30031 else if (token
->type
== CPP_SEMICOLON
)
30032 cp_lexer_consume_token (parser
->lexer
);
30033 /* Mark methods as optional or required, when building protocols. */
30034 else if (token
->keyword
== RID_AT_OPTIONAL
)
30036 cp_lexer_consume_token (parser
->lexer
);
30037 objc_set_method_opt (true);
30039 else if (token
->keyword
== RID_AT_REQUIRED
)
30041 cp_lexer_consume_token (parser
->lexer
);
30042 objc_set_method_opt (false);
30044 else if (token
->keyword
== RID_NAMESPACE
)
30045 cp_parser_namespace_definition (parser
);
30046 /* Other stray characters must generate errors. */
30047 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30049 cp_lexer_consume_token (parser
->lexer
);
30050 error ("stray %qs between Objective-C++ methods",
30051 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30053 /* Finally, try to parse a block-declaration, or a function-definition. */
30055 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30058 /* Parse a method signature. */
30061 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30063 tree rettype
, kwdparms
, optparms
;
30064 bool ellipsis
= false;
30065 bool is_class_method
;
30067 is_class_method
= cp_parser_objc_method_type (parser
);
30068 rettype
= cp_parser_objc_typename (parser
);
30069 *attributes
= NULL_TREE
;
30070 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30071 if (kwdparms
== error_mark_node
)
30072 return error_mark_node
;
30073 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30074 if (optparms
== error_mark_node
)
30075 return error_mark_node
;
30077 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30081 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30084 cp_lexer_save_tokens (parser
->lexer
);
30085 tattr
= cp_parser_attributes_opt (parser
);
30086 gcc_assert (tattr
) ;
30088 /* If the attributes are followed by a method introducer, this is not allowed.
30089 Dump the attributes and flag the situation. */
30090 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30091 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30094 /* Otherwise, the attributes introduce some interstitial code, possibly so
30095 rewind to allow that check. */
30096 cp_lexer_rollback_tokens (parser
->lexer
);
30100 /* Parse an Objective-C method prototype list. */
30103 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30105 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30107 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30109 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30111 tree attributes
, sig
;
30112 bool is_class_method
;
30113 if (token
->type
== CPP_PLUS
)
30114 is_class_method
= true;
30116 is_class_method
= false;
30117 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30118 if (sig
== error_mark_node
)
30120 cp_parser_skip_to_end_of_block_or_statement (parser
);
30121 token
= cp_lexer_peek_token (parser
->lexer
);
30124 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30125 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30127 else if (token
->keyword
== RID_AT_PROPERTY
)
30128 cp_parser_objc_at_property_declaration (parser
);
30129 else if (token
->keyword
== RID_ATTRIBUTE
30130 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30131 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30133 "prefix attributes are ignored for methods");
30135 /* Allow for interspersed non-ObjC++ code. */
30136 cp_parser_objc_interstitial_code (parser
);
30138 token
= cp_lexer_peek_token (parser
->lexer
);
30141 if (token
->type
!= CPP_EOF
)
30142 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30144 cp_parser_error (parser
, "expected %<@end%>");
30146 objc_finish_interface ();
30149 /* Parse an Objective-C method definition list. */
30152 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30154 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30156 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30160 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30163 tree sig
, attribute
;
30164 bool is_class_method
;
30165 if (token
->type
== CPP_PLUS
)
30166 is_class_method
= true;
30168 is_class_method
= false;
30169 push_deferring_access_checks (dk_deferred
);
30170 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30171 if (sig
== error_mark_node
)
30173 cp_parser_skip_to_end_of_block_or_statement (parser
);
30174 token
= cp_lexer_peek_token (parser
->lexer
);
30177 objc_start_method_definition (is_class_method
, sig
, attribute
,
30180 /* For historical reasons, we accept an optional semicolon. */
30181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30182 cp_lexer_consume_token (parser
->lexer
);
30184 ptk
= cp_lexer_peek_token (parser
->lexer
);
30185 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30186 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30188 perform_deferred_access_checks (tf_warning_or_error
);
30189 stop_deferring_access_checks ();
30190 meth
= cp_parser_function_definition_after_declarator (parser
,
30192 pop_deferring_access_checks ();
30193 objc_finish_method_definition (meth
);
30196 /* The following case will be removed once @synthesize is
30197 completely implemented. */
30198 else if (token
->keyword
== RID_AT_PROPERTY
)
30199 cp_parser_objc_at_property_declaration (parser
);
30200 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30201 cp_parser_objc_at_synthesize_declaration (parser
);
30202 else if (token
->keyword
== RID_AT_DYNAMIC
)
30203 cp_parser_objc_at_dynamic_declaration (parser
);
30204 else if (token
->keyword
== RID_ATTRIBUTE
30205 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30206 warning_at (token
->location
, OPT_Wattributes
,
30207 "prefix attributes are ignored for methods");
30209 /* Allow for interspersed non-ObjC++ code. */
30210 cp_parser_objc_interstitial_code (parser
);
30212 token
= cp_lexer_peek_token (parser
->lexer
);
30215 if (token
->type
!= CPP_EOF
)
30216 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30218 cp_parser_error (parser
, "expected %<@end%>");
30220 objc_finish_implementation ();
30223 /* Parse Objective-C ivars. */
30226 cp_parser_objc_class_ivars (cp_parser
* parser
)
30228 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30230 if (token
->type
!= CPP_OPEN_BRACE
)
30231 return; /* No ivars specified. */
30233 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30234 token
= cp_lexer_peek_token (parser
->lexer
);
30236 while (token
->type
!= CPP_CLOSE_BRACE
30237 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30239 cp_decl_specifier_seq declspecs
;
30240 int decl_class_or_enum_p
;
30241 tree prefix_attributes
;
30243 cp_parser_objc_visibility_spec (parser
);
30245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30248 cp_parser_decl_specifier_seq (parser
,
30249 CP_PARSER_FLAGS_OPTIONAL
,
30251 &decl_class_or_enum_p
);
30253 /* auto, register, static, extern, mutable. */
30254 if (declspecs
.storage_class
!= sc_none
)
30256 cp_parser_error (parser
, "invalid type for instance variable");
30257 declspecs
.storage_class
= sc_none
;
30260 /* thread_local. */
30261 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30263 cp_parser_error (parser
, "invalid type for instance variable");
30264 declspecs
.locations
[ds_thread
] = 0;
30268 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30270 cp_parser_error (parser
, "invalid type for instance variable");
30271 declspecs
.locations
[ds_typedef
] = 0;
30274 prefix_attributes
= declspecs
.attributes
;
30275 declspecs
.attributes
= NULL_TREE
;
30277 /* Keep going until we hit the `;' at the end of the
30279 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30281 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30282 cp_declarator
*declarator
= NULL
;
30283 int ctor_dtor_or_conv_p
;
30285 /* Check for a (possibly unnamed) bitfield declaration. */
30286 token
= cp_lexer_peek_token (parser
->lexer
);
30287 if (token
->type
== CPP_COLON
)
30290 if (token
->type
== CPP_NAME
30291 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30294 /* Get the name of the bitfield. */
30295 declarator
= make_id_declarator (NULL_TREE
,
30296 cp_parser_identifier (parser
),
30300 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30301 /* Get the width of the bitfield. */
30303 = cp_parser_constant_expression (parser
);
30307 /* Parse the declarator. */
30309 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30310 &ctor_dtor_or_conv_p
,
30311 /*parenthesized_p=*/NULL
,
30312 /*member_p=*/false,
30313 /*friend_p=*/false);
30316 /* Look for attributes that apply to the ivar. */
30317 attributes
= cp_parser_attributes_opt (parser
);
30318 /* Remember which attributes are prefix attributes and
30320 first_attribute
= attributes
;
30321 /* Combine the attributes. */
30322 attributes
= attr_chainon (prefix_attributes
, attributes
);
30325 /* Create the bitfield declaration. */
30326 decl
= grokbitfield (declarator
, &declspecs
,
30327 width
, NULL_TREE
, attributes
);
30329 decl
= grokfield (declarator
, &declspecs
,
30330 NULL_TREE
, /*init_const_expr_p=*/false,
30331 NULL_TREE
, attributes
);
30333 /* Add the instance variable. */
30334 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30335 objc_add_instance_variable (decl
);
30337 /* Reset PREFIX_ATTRIBUTES. */
30338 if (attributes
!= error_mark_node
)
30340 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30341 attributes
= TREE_CHAIN (attributes
);
30343 TREE_CHAIN (attributes
) = NULL_TREE
;
30346 token
= cp_lexer_peek_token (parser
->lexer
);
30348 if (token
->type
== CPP_COMMA
)
30350 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30356 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30357 token
= cp_lexer_peek_token (parser
->lexer
);
30360 if (token
->keyword
== RID_AT_END
)
30361 cp_parser_error (parser
, "expected %<}%>");
30363 /* Do not consume the RID_AT_END, so it will be read again as terminating
30364 the @interface of @implementation. */
30365 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30366 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30368 /* For historical reasons, we accept an optional semicolon. */
30369 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30370 cp_lexer_consume_token (parser
->lexer
);
30373 /* Parse an Objective-C protocol declaration. */
30376 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30378 tree proto
, protorefs
;
30381 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30382 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30384 tok
= cp_lexer_peek_token (parser
->lexer
);
30385 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30386 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30390 /* See if we have a forward declaration or a definition. */
30391 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30393 /* Try a forward declaration first. */
30394 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30400 id
= cp_parser_identifier (parser
);
30401 if (id
== error_mark_node
)
30404 objc_declare_protocol (id
, attributes
);
30406 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30407 cp_lexer_consume_token (parser
->lexer
);
30411 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30414 /* Ok, we got a full-fledged definition (or at least should). */
30417 proto
= cp_parser_identifier (parser
);
30418 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30419 objc_start_protocol (proto
, protorefs
, attributes
);
30420 cp_parser_objc_method_prototype_list (parser
);
30424 /* Parse an Objective-C superclass or category. */
30427 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30430 tree
*categ
, bool *is_class_extension
)
30432 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30434 *super
= *categ
= NULL_TREE
;
30435 *is_class_extension
= false;
30436 if (next
->type
== CPP_COLON
)
30438 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30439 *super
= cp_parser_identifier (parser
);
30441 else if (next
->type
== CPP_OPEN_PAREN
)
30443 matching_parens parens
;
30444 parens
.consume_open (parser
); /* Eat '('. */
30446 /* If there is no category name, and this is an @interface, we
30447 have a class extension. */
30448 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30450 *categ
= NULL_TREE
;
30451 *is_class_extension
= true;
30454 *categ
= cp_parser_identifier (parser
);
30456 parens
.require_close (parser
);
30460 /* Parse an Objective-C class interface. */
30463 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30465 tree name
, super
, categ
, protos
;
30466 bool is_class_extension
;
30468 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30469 name
= cp_parser_identifier (parser
);
30470 if (name
== error_mark_node
)
30472 /* It's hard to recover because even if valid @interface stuff
30473 is to follow, we can't compile it (or validate it) if we
30474 don't even know which class it refers to. Let's assume this
30475 was a stray '@interface' token in the stream and skip it.
30479 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30480 &is_class_extension
);
30481 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30483 /* We have either a class or a category on our hands. */
30484 if (categ
|| is_class_extension
)
30485 objc_start_category_interface (name
, categ
, protos
, attributes
);
30488 objc_start_class_interface (name
, super
, protos
, attributes
);
30489 /* Handle instance variable declarations, if any. */
30490 cp_parser_objc_class_ivars (parser
);
30491 objc_continue_interface ();
30494 cp_parser_objc_method_prototype_list (parser
);
30497 /* Parse an Objective-C class implementation. */
30500 cp_parser_objc_class_implementation (cp_parser
* parser
)
30502 tree name
, super
, categ
;
30503 bool is_class_extension
;
30505 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30506 name
= cp_parser_identifier (parser
);
30507 if (name
== error_mark_node
)
30509 /* It's hard to recover because even if valid @implementation
30510 stuff is to follow, we can't compile it (or validate it) if
30511 we don't even know which class it refers to. Let's assume
30512 this was a stray '@implementation' token in the stream and
30517 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30518 &is_class_extension
);
30520 /* We have either a class or a category on our hands. */
30522 objc_start_category_implementation (name
, categ
);
30525 objc_start_class_implementation (name
, super
);
30526 /* Handle instance variable declarations, if any. */
30527 cp_parser_objc_class_ivars (parser
);
30528 objc_continue_implementation ();
30531 cp_parser_objc_method_definition_list (parser
);
30534 /* Consume the @end token and finish off the implementation. */
30537 cp_parser_objc_end_implementation (cp_parser
* parser
)
30539 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30540 objc_finish_implementation ();
30543 /* Parse an Objective-C declaration. */
30546 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30548 /* Try to figure out what kind of declaration is present. */
30549 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30552 switch (kwd
->keyword
)
30557 error_at (kwd
->location
, "attributes may not be specified before"
30558 " the %<@%D%> Objective-C++ keyword",
30562 case RID_AT_IMPLEMENTATION
:
30563 warning_at (kwd
->location
, OPT_Wattributes
,
30564 "prefix attributes are ignored before %<@%D%>",
30571 switch (kwd
->keyword
)
30574 cp_parser_objc_alias_declaration (parser
);
30577 cp_parser_objc_class_declaration (parser
);
30579 case RID_AT_PROTOCOL
:
30580 cp_parser_objc_protocol_declaration (parser
, attributes
);
30582 case RID_AT_INTERFACE
:
30583 cp_parser_objc_class_interface (parser
, attributes
);
30585 case RID_AT_IMPLEMENTATION
:
30586 cp_parser_objc_class_implementation (parser
);
30589 cp_parser_objc_end_implementation (parser
);
30592 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30594 cp_parser_skip_to_end_of_block_or_statement (parser
);
30598 /* Parse an Objective-C try-catch-finally statement.
30600 objc-try-catch-finally-stmt:
30601 @try compound-statement objc-catch-clause-seq [opt]
30602 objc-finally-clause [opt]
30604 objc-catch-clause-seq:
30605 objc-catch-clause objc-catch-clause-seq [opt]
30608 @catch ( objc-exception-declaration ) compound-statement
30610 objc-finally-clause:
30611 @finally compound-statement
30613 objc-exception-declaration:
30614 parameter-declaration
30617 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30621 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30622 for C. Keep them in sync. */
30625 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30627 location_t location
;
30630 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30631 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30632 objc_maybe_warn_exceptions (location
);
30633 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30634 node, lest it get absorbed into the surrounding block. */
30635 stmt
= push_stmt_list ();
30636 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30637 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30639 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30641 cp_parameter_declarator
*parm
;
30642 tree parameter_declaration
= error_mark_node
;
30643 bool seen_open_paren
= false;
30644 matching_parens parens
;
30646 cp_lexer_consume_token (parser
->lexer
);
30647 if (parens
.require_open (parser
))
30648 seen_open_paren
= true;
30649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30651 /* We have "@catch (...)" (where the '...' are literally
30652 what is in the code). Skip the '...'.
30653 parameter_declaration is set to NULL_TREE, and
30654 objc_being_catch_clauses() knows that that means
30656 cp_lexer_consume_token (parser
->lexer
);
30657 parameter_declaration
= NULL_TREE
;
30661 /* We have "@catch (NSException *exception)" or something
30662 like that. Parse the parameter declaration. */
30663 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30665 parameter_declaration
= error_mark_node
;
30667 parameter_declaration
= grokdeclarator (parm
->declarator
,
30668 &parm
->decl_specifiers
,
30669 PARM
, /*initialized=*/0,
30670 /*attrlist=*/NULL
);
30672 if (seen_open_paren
)
30673 parens
.require_close (parser
);
30676 /* If there was no open parenthesis, we are recovering from
30677 an error, and we are trying to figure out what mistake
30678 the user has made. */
30680 /* If there is an immediate closing parenthesis, the user
30681 probably forgot the opening one (ie, they typed "@catch
30682 NSException *e)". Parse the closing parenthesis and keep
30684 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30685 cp_lexer_consume_token (parser
->lexer
);
30687 /* If these is no immediate closing parenthesis, the user
30688 probably doesn't know that parenthesis are required at
30689 all (ie, they typed "@catch NSException *e"). So, just
30690 forget about the closing parenthesis and keep going. */
30692 objc_begin_catch_clause (parameter_declaration
);
30693 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30694 objc_finish_catch_clause ();
30696 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30698 cp_lexer_consume_token (parser
->lexer
);
30699 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30700 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30701 node, lest it get absorbed into the surrounding block. */
30702 stmt
= push_stmt_list ();
30703 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30704 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30707 return objc_finish_try_stmt ();
30710 /* Parse an Objective-C synchronized statement.
30712 objc-synchronized-stmt:
30713 @synchronized ( expression ) compound-statement
30715 Returns NULL_TREE. */
30718 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30720 location_t location
;
30723 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30725 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30726 objc_maybe_warn_exceptions (location
);
30727 matching_parens parens
;
30728 parens
.require_open (parser
);
30729 lock
= cp_parser_expression (parser
);
30730 parens
.require_close (parser
);
30732 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30733 node, lest it get absorbed into the surrounding block. */
30734 stmt
= push_stmt_list ();
30735 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30737 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30740 /* Parse an Objective-C throw statement.
30743 @throw assignment-expression [opt] ;
30745 Returns a constructed '@throw' statement. */
30748 cp_parser_objc_throw_statement (cp_parser
*parser
)
30750 tree expr
= NULL_TREE
;
30751 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30753 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30755 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30756 expr
= cp_parser_expression (parser
);
30758 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30760 return objc_build_throw_stmt (loc
, expr
);
30763 /* Parse an Objective-C statement. */
30766 cp_parser_objc_statement (cp_parser
* parser
)
30768 /* Try to figure out what kind of declaration is present. */
30769 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30771 switch (kwd
->keyword
)
30774 return cp_parser_objc_try_catch_finally_statement (parser
);
30775 case RID_AT_SYNCHRONIZED
:
30776 return cp_parser_objc_synchronized_statement (parser
);
30778 return cp_parser_objc_throw_statement (parser
);
30780 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30782 cp_parser_skip_to_end_of_block_or_statement (parser
);
30785 return error_mark_node
;
30788 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30789 look ahead to see if an objc keyword follows the attributes. This
30790 is to detect the use of prefix attributes on ObjC @interface and
30794 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30796 cp_lexer_save_tokens (parser
->lexer
);
30797 *attrib
= cp_parser_attributes_opt (parser
);
30798 gcc_assert (*attrib
);
30799 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30801 cp_lexer_commit_tokens (parser
->lexer
);
30804 cp_lexer_rollback_tokens (parser
->lexer
);
30808 /* This routine is a minimal replacement for
30809 c_parser_struct_declaration () used when parsing the list of
30810 types/names or ObjC++ properties. For example, when parsing the
30813 @property (readonly) int a, b, c;
30815 this function is responsible for parsing "int a, int b, int c" and
30816 returning the declarations as CHAIN of DECLs.
30818 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30819 similar parsing. */
30821 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30823 tree decls
= NULL_TREE
;
30824 cp_decl_specifier_seq declspecs
;
30825 int decl_class_or_enum_p
;
30826 tree prefix_attributes
;
30828 cp_parser_decl_specifier_seq (parser
,
30829 CP_PARSER_FLAGS_NONE
,
30831 &decl_class_or_enum_p
);
30833 if (declspecs
.type
== error_mark_node
)
30834 return error_mark_node
;
30836 /* auto, register, static, extern, mutable. */
30837 if (declspecs
.storage_class
!= sc_none
)
30839 cp_parser_error (parser
, "invalid type for property");
30840 declspecs
.storage_class
= sc_none
;
30843 /* thread_local. */
30844 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30846 cp_parser_error (parser
, "invalid type for property");
30847 declspecs
.locations
[ds_thread
] = 0;
30851 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30853 cp_parser_error (parser
, "invalid type for property");
30854 declspecs
.locations
[ds_typedef
] = 0;
30857 prefix_attributes
= declspecs
.attributes
;
30858 declspecs
.attributes
= NULL_TREE
;
30860 /* Keep going until we hit the `;' at the end of the declaration. */
30861 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30863 tree attributes
, first_attribute
, decl
;
30864 cp_declarator
*declarator
;
30867 /* Parse the declarator. */
30868 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30869 NULL
, NULL
, false, false);
30871 /* Look for attributes that apply to the ivar. */
30872 attributes
= cp_parser_attributes_opt (parser
);
30873 /* Remember which attributes are prefix attributes and
30875 first_attribute
= attributes
;
30876 /* Combine the attributes. */
30877 attributes
= attr_chainon (prefix_attributes
, attributes
);
30879 decl
= grokfield (declarator
, &declspecs
,
30880 NULL_TREE
, /*init_const_expr_p=*/false,
30881 NULL_TREE
, attributes
);
30883 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30884 return error_mark_node
;
30886 /* Reset PREFIX_ATTRIBUTES. */
30887 if (attributes
!= error_mark_node
)
30889 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30890 attributes
= TREE_CHAIN (attributes
);
30892 TREE_CHAIN (attributes
) = NULL_TREE
;
30895 DECL_CHAIN (decl
) = decls
;
30898 token
= cp_lexer_peek_token (parser
->lexer
);
30899 if (token
->type
== CPP_COMMA
)
30901 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30910 /* Parse an Objective-C @property declaration. The syntax is:
30912 objc-property-declaration:
30913 '@property' objc-property-attributes[opt] struct-declaration ;
30915 objc-property-attributes:
30916 '(' objc-property-attribute-list ')'
30918 objc-property-attribute-list:
30919 objc-property-attribute
30920 objc-property-attribute-list, objc-property-attribute
30922 objc-property-attribute
30923 'getter' = identifier
30924 'setter' = identifier
30933 @property NSString *name;
30934 @property (readonly) id object;
30935 @property (retain, nonatomic, getter=getTheName) id name;
30936 @property int a, b, c;
30938 PS: This function is identical to
30939 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30941 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30943 /* The following variables hold the attributes of the properties as
30944 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30945 seen. When we see an attribute, we set them to 'true' (if they
30946 are boolean properties) or to the identifier (if they have an
30947 argument, ie, for getter and setter). Note that here we only
30948 parse the list of attributes, check the syntax and accumulate the
30949 attributes that we find. objc_add_property_declaration() will
30950 then process the information. */
30951 bool property_assign
= false;
30952 bool property_copy
= false;
30953 tree property_getter_ident
= NULL_TREE
;
30954 bool property_nonatomic
= false;
30955 bool property_readonly
= false;
30956 bool property_readwrite
= false;
30957 bool property_retain
= false;
30958 tree property_setter_ident
= NULL_TREE
;
30960 /* 'properties' is the list of properties that we read. Usually a
30961 single one, but maybe more (eg, in "@property int a, b, c;" there
30966 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30968 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30970 /* Parse the optional attribute list... */
30971 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30974 matching_parens parens
;
30975 parens
.consume_open (parser
);
30979 bool syntax_error
= false;
30980 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30983 if (token
->type
!= CPP_NAME
)
30985 cp_parser_error (parser
, "expected identifier");
30988 keyword
= C_RID_CODE (token
->u
.value
);
30989 cp_lexer_consume_token (parser
->lexer
);
30992 case RID_ASSIGN
: property_assign
= true; break;
30993 case RID_COPY
: property_copy
= true; break;
30994 case RID_NONATOMIC
: property_nonatomic
= true; break;
30995 case RID_READONLY
: property_readonly
= true; break;
30996 case RID_READWRITE
: property_readwrite
= true; break;
30997 case RID_RETAIN
: property_retain
= true; break;
31001 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31003 if (keyword
== RID_GETTER
)
31004 cp_parser_error (parser
,
31005 "missing %<=%> (after %<getter%> attribute)");
31007 cp_parser_error (parser
,
31008 "missing %<=%> (after %<setter%> attribute)");
31009 syntax_error
= true;
31012 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31013 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31015 cp_parser_error (parser
, "expected identifier");
31016 syntax_error
= true;
31019 if (keyword
== RID_SETTER
)
31021 if (property_setter_ident
!= NULL_TREE
)
31023 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31024 cp_lexer_consume_token (parser
->lexer
);
31027 property_setter_ident
= cp_parser_objc_selector (parser
);
31028 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31029 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31031 cp_lexer_consume_token (parser
->lexer
);
31035 if (property_getter_ident
!= NULL_TREE
)
31037 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31038 cp_lexer_consume_token (parser
->lexer
);
31041 property_getter_ident
= cp_parser_objc_selector (parser
);
31045 cp_parser_error (parser
, "unknown property attribute");
31046 syntax_error
= true;
31053 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31054 cp_lexer_consume_token (parser
->lexer
);
31059 /* FIXME: "@property (setter, assign);" will generate a spurious
31060 "error: expected ‘)’ before ‘,’ token". This is because
31061 cp_parser_require, unlike the C counterpart, will produce an
31062 error even if we are in error recovery. */
31063 if (!parens
.require_close (parser
))
31065 cp_parser_skip_to_closing_parenthesis (parser
,
31066 /*recovering=*/true,
31067 /*or_comma=*/false,
31068 /*consume_paren=*/true);
31072 /* ... and the property declaration(s). */
31073 properties
= cp_parser_objc_struct_declaration (parser
);
31075 if (properties
== error_mark_node
)
31077 cp_parser_skip_to_end_of_statement (parser
);
31078 /* If the next token is now a `;', consume it. */
31079 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31080 cp_lexer_consume_token (parser
->lexer
);
31084 if (properties
== NULL_TREE
)
31085 cp_parser_error (parser
, "expected identifier");
31088 /* Comma-separated properties are chained together in
31089 reverse order; add them one by one. */
31090 properties
= nreverse (properties
);
31092 for (; properties
; properties
= TREE_CHAIN (properties
))
31093 objc_add_property_declaration (loc
, copy_node (properties
),
31094 property_readonly
, property_readwrite
,
31095 property_assign
, property_retain
,
31096 property_copy
, property_nonatomic
,
31097 property_getter_ident
, property_setter_ident
);
31100 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31103 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31105 objc-synthesize-declaration:
31106 @synthesize objc-synthesize-identifier-list ;
31108 objc-synthesize-identifier-list:
31109 objc-synthesize-identifier
31110 objc-synthesize-identifier-list, objc-synthesize-identifier
31112 objc-synthesize-identifier
31114 identifier = identifier
31117 @synthesize MyProperty;
31118 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31120 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31121 for C. Keep them in sync.
31124 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31126 tree list
= NULL_TREE
;
31128 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31130 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31133 tree property
, ivar
;
31134 property
= cp_parser_identifier (parser
);
31135 if (property
== error_mark_node
)
31137 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31140 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31142 cp_lexer_consume_token (parser
->lexer
);
31143 ivar
= cp_parser_identifier (parser
);
31144 if (ivar
== error_mark_node
)
31146 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31152 list
= chainon (list
, build_tree_list (ivar
, property
));
31153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31154 cp_lexer_consume_token (parser
->lexer
);
31158 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31159 objc_add_synthesize_declaration (loc
, list
);
31162 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31164 objc-dynamic-declaration:
31165 @dynamic identifier-list ;
31168 @dynamic MyProperty;
31169 @dynamic MyProperty, AnotherProperty;
31171 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31172 for C. Keep them in sync.
31175 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31177 tree list
= NULL_TREE
;
31179 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31181 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31185 property
= cp_parser_identifier (parser
);
31186 if (property
== error_mark_node
)
31188 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31191 list
= chainon (list
, build_tree_list (NULL
, property
));
31192 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31193 cp_lexer_consume_token (parser
->lexer
);
31197 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31198 objc_add_dynamic_declaration (loc
, list
);
31202 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31204 /* Returns name of the next clause.
31205 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31206 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31207 returned and the token is consumed. */
31209 static pragma_omp_clause
31210 cp_parser_omp_clause_name (cp_parser
*parser
)
31212 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31214 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31215 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31216 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31217 result
= PRAGMA_OMP_CLAUSE_IF
;
31218 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31219 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31220 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31221 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31222 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31223 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31224 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31225 result
= PRAGMA_OMP_CLAUSE_FOR
;
31226 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31228 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31229 const char *p
= IDENTIFIER_POINTER (id
);
31234 if (!strcmp ("aligned", p
))
31235 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31236 else if (!strcmp ("async", p
))
31237 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31240 if (!strcmp ("collapse", p
))
31241 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31242 else if (!strcmp ("copy", p
))
31243 result
= PRAGMA_OACC_CLAUSE_COPY
;
31244 else if (!strcmp ("copyin", p
))
31245 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31246 else if (!strcmp ("copyout", p
))
31247 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31248 else if (!strcmp ("copyprivate", p
))
31249 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31250 else if (!strcmp ("create", p
))
31251 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31254 if (!strcmp ("defaultmap", p
))
31255 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31256 else if (!strcmp ("depend", p
))
31257 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31258 else if (!strcmp ("device", p
))
31259 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31260 else if (!strcmp ("deviceptr", p
))
31261 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31262 else if (!strcmp ("device_resident", p
))
31263 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31264 else if (!strcmp ("dist_schedule", p
))
31265 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31268 if (!strcmp ("final", p
))
31269 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31270 else if (!strcmp ("firstprivate", p
))
31271 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31272 else if (!strcmp ("from", p
))
31273 result
= PRAGMA_OMP_CLAUSE_FROM
;
31276 if (!strcmp ("gang", p
))
31277 result
= PRAGMA_OACC_CLAUSE_GANG
;
31278 else if (!strcmp ("grainsize", p
))
31279 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31282 if (!strcmp ("hint", p
))
31283 result
= PRAGMA_OMP_CLAUSE_HINT
;
31284 else if (!strcmp ("host", p
))
31285 result
= PRAGMA_OACC_CLAUSE_HOST
;
31288 if (!strcmp ("inbranch", p
))
31289 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31290 else if (!strcmp ("independent", p
))
31291 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31292 else if (!strcmp ("is_device_ptr", p
))
31293 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31296 if (!strcmp ("lastprivate", p
))
31297 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31298 else if (!strcmp ("linear", p
))
31299 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31300 else if (!strcmp ("link", p
))
31301 result
= PRAGMA_OMP_CLAUSE_LINK
;
31304 if (!strcmp ("map", p
))
31305 result
= PRAGMA_OMP_CLAUSE_MAP
;
31306 else if (!strcmp ("mergeable", p
))
31307 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31310 if (!strcmp ("nogroup", p
))
31311 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31312 else if (!strcmp ("notinbranch", p
))
31313 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31314 else if (!strcmp ("nowait", p
))
31315 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31316 else if (!strcmp ("num_gangs", p
))
31317 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31318 else if (!strcmp ("num_tasks", p
))
31319 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31320 else if (!strcmp ("num_teams", p
))
31321 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31322 else if (!strcmp ("num_threads", p
))
31323 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31324 else if (!strcmp ("num_workers", p
))
31325 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31328 if (!strcmp ("ordered", p
))
31329 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31332 if (!strcmp ("parallel", p
))
31333 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31334 else if (!strcmp ("present", p
))
31335 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31336 else if (!strcmp ("present_or_copy", p
)
31337 || !strcmp ("pcopy", p
))
31338 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31339 else if (!strcmp ("present_or_copyin", p
)
31340 || !strcmp ("pcopyin", p
))
31341 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31342 else if (!strcmp ("present_or_copyout", p
)
31343 || !strcmp ("pcopyout", p
))
31344 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31345 else if (!strcmp ("present_or_create", p
)
31346 || !strcmp ("pcreate", p
))
31347 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31348 else if (!strcmp ("priority", p
))
31349 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31350 else if (!strcmp ("proc_bind", p
))
31351 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31354 if (!strcmp ("reduction", p
))
31355 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31358 if (!strcmp ("safelen", p
))
31359 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31360 else if (!strcmp ("schedule", p
))
31361 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31362 else if (!strcmp ("sections", p
))
31363 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31364 else if (!strcmp ("self", p
))
31365 result
= PRAGMA_OACC_CLAUSE_SELF
;
31366 else if (!strcmp ("seq", p
))
31367 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31368 else if (!strcmp ("shared", p
))
31369 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31370 else if (!strcmp ("simd", p
))
31371 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31372 else if (!strcmp ("simdlen", p
))
31373 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31376 if (!strcmp ("taskgroup", p
))
31377 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31378 else if (!strcmp ("thread_limit", p
))
31379 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31380 else if (!strcmp ("threads", p
))
31381 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31382 else if (!strcmp ("tile", p
))
31383 result
= PRAGMA_OACC_CLAUSE_TILE
;
31384 else if (!strcmp ("to", p
))
31385 result
= PRAGMA_OMP_CLAUSE_TO
;
31388 if (!strcmp ("uniform", p
))
31389 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31390 else if (!strcmp ("untied", p
))
31391 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31392 else if (!strcmp ("use_device", p
))
31393 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31394 else if (!strcmp ("use_device_ptr", p
))
31395 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31398 if (!strcmp ("vector", p
))
31399 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31400 else if (!strcmp ("vector_length", p
))
31401 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31404 if (!strcmp ("wait", p
))
31405 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31406 else if (!strcmp ("worker", p
))
31407 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31412 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31413 cp_lexer_consume_token (parser
->lexer
);
31418 /* Validate that a clause of the given type does not already exist. */
31421 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31422 const char *name
, location_t location
)
31426 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31427 if (OMP_CLAUSE_CODE (c
) == code
)
31429 error_at (location
, "too many %qs clauses", name
);
31437 variable-list , identifier
31439 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31440 colon). An opening parenthesis will have been consumed by the caller.
31442 If KIND is nonzero, create the appropriate node and install the decl
31443 in OMP_CLAUSE_DECL and add the node to the head of the list.
31445 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31446 return the list created.
31448 COLON can be NULL if only closing parenthesis should end the list,
31449 or pointer to bool which will receive false if the list is terminated
31450 by closing parenthesis or true if the list is terminated by colon. */
31453 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31454 tree list
, bool *colon
)
31457 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31460 parser
->colon_corrects_to_scope_p
= false;
31467 token
= cp_lexer_peek_token (parser
->lexer
);
31469 && current_class_ptr
31470 && cp_parser_is_keyword (token
, RID_THIS
))
31472 decl
= finish_this_expr ();
31473 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31474 || CONVERT_EXPR_P (decl
))
31475 decl
= TREE_OPERAND (decl
, 0);
31476 cp_lexer_consume_token (parser
->lexer
);
31480 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31481 /*check_dependency_p=*/true,
31482 /*template_p=*/NULL
,
31483 /*declarator_p=*/false,
31484 /*optional_p=*/false);
31485 if (name
== error_mark_node
)
31488 if (identifier_p (name
))
31489 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31492 if (decl
== error_mark_node
)
31493 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31496 if (decl
== error_mark_node
)
31498 else if (kind
!= 0)
31502 case OMP_CLAUSE__CACHE_
:
31503 /* The OpenACC cache directive explicitly only allows "array
31504 elements or subarrays". */
31505 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31507 error_at (token
->location
, "expected %<[%>");
31508 decl
= error_mark_node
;
31512 case OMP_CLAUSE_MAP
:
31513 case OMP_CLAUSE_FROM
:
31514 case OMP_CLAUSE_TO
:
31515 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31518 = cp_lexer_peek_token (parser
->lexer
)->location
;
31519 cp_id_kind idk
= CP_ID_KIND_NONE
;
31520 cp_lexer_consume_token (parser
->lexer
);
31521 decl
= convert_from_reference (decl
);
31523 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31528 case OMP_CLAUSE_DEPEND
:
31529 case OMP_CLAUSE_REDUCTION
:
31530 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31532 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31534 parser
->colon_corrects_to_scope_p
= false;
31535 cp_lexer_consume_token (parser
->lexer
);
31536 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31537 low_bound
= cp_parser_expression (parser
);
31539 parser
->colon_corrects_to_scope_p
31540 = saved_colon_corrects_to_scope_p
;
31541 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31542 length
= integer_one_node
;
31545 /* Look for `:'. */
31546 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31548 if (!cp_lexer_next_token_is (parser
->lexer
,
31550 length
= cp_parser_expression (parser
);
31552 /* Look for the closing `]'. */
31553 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31557 decl
= tree_cons (low_bound
, length
, decl
);
31564 tree u
= build_omp_clause (token
->location
, kind
);
31565 OMP_CLAUSE_DECL (u
) = decl
;
31566 OMP_CLAUSE_CHAIN (u
) = list
;
31570 list
= tree_cons (decl
, NULL_TREE
, list
);
31573 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31575 cp_lexer_consume_token (parser
->lexer
);
31579 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31581 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31584 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31588 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31592 /* Try to resync to an unnested comma. Copied from
31593 cp_parser_parenthesized_expression_list. */
31596 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31597 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31598 /*recovering=*/true,
31600 /*consume_paren=*/true);
31608 /* Similarly, but expect leading and trailing parenthesis. This is a very
31609 common case for omp clauses. */
31612 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31614 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31615 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31620 copy ( variable-list )
31621 copyin ( variable-list )
31622 copyout ( variable-list )
31623 create ( variable-list )
31624 delete ( variable-list )
31625 present ( variable-list )
31626 present_or_copy ( variable-list )
31627 pcopy ( variable-list )
31628 present_or_copyin ( variable-list )
31629 pcopyin ( variable-list )
31630 present_or_copyout ( variable-list )
31631 pcopyout ( variable-list )
31632 present_or_create ( variable-list )
31633 pcreate ( variable-list ) */
31636 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31639 enum gomp_map_kind kind
;
31642 case PRAGMA_OACC_CLAUSE_COPY
:
31643 kind
= GOMP_MAP_FORCE_TOFROM
;
31645 case PRAGMA_OACC_CLAUSE_COPYIN
:
31646 kind
= GOMP_MAP_FORCE_TO
;
31648 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31649 kind
= GOMP_MAP_FORCE_FROM
;
31651 case PRAGMA_OACC_CLAUSE_CREATE
:
31652 kind
= GOMP_MAP_FORCE_ALLOC
;
31654 case PRAGMA_OACC_CLAUSE_DELETE
:
31655 kind
= GOMP_MAP_DELETE
;
31657 case PRAGMA_OACC_CLAUSE_DEVICE
:
31658 kind
= GOMP_MAP_FORCE_TO
;
31660 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31661 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31663 case PRAGMA_OACC_CLAUSE_HOST
:
31664 case PRAGMA_OACC_CLAUSE_SELF
:
31665 kind
= GOMP_MAP_FORCE_FROM
;
31667 case PRAGMA_OACC_CLAUSE_LINK
:
31668 kind
= GOMP_MAP_LINK
;
31670 case PRAGMA_OACC_CLAUSE_PRESENT
:
31671 kind
= GOMP_MAP_FORCE_PRESENT
;
31673 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31674 kind
= GOMP_MAP_TOFROM
;
31676 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31677 kind
= GOMP_MAP_TO
;
31679 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31680 kind
= GOMP_MAP_FROM
;
31682 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31683 kind
= GOMP_MAP_ALLOC
;
31686 gcc_unreachable ();
31689 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31691 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31692 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31698 deviceptr ( variable-list ) */
31701 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31703 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31706 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31707 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31708 variable-list must only allow for pointer variables. */
31709 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31710 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31712 tree v
= TREE_PURPOSE (t
);
31713 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31714 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31715 OMP_CLAUSE_DECL (u
) = v
;
31716 OMP_CLAUSE_CHAIN (u
) = list
;
31730 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31731 enum omp_clause_code code
,
31732 tree list
, location_t location
)
31734 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31735 tree c
= build_omp_clause (location
, code
);
31736 OMP_CLAUSE_CHAIN (c
) = list
;
31741 num_gangs ( expression )
31742 num_workers ( expression )
31743 vector_length ( expression ) */
31746 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31747 const char *str
, tree list
)
31749 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31751 matching_parens parens
;
31752 if (!parens
.require_open (parser
))
31755 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31757 if (t
== error_mark_node
31758 || !parens
.require_close (parser
))
31760 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31761 /*or_comma=*/false,
31762 /*consume_paren=*/true);
31766 check_no_duplicate_clause (list
, code
, str
, loc
);
31768 tree c
= build_omp_clause (loc
, code
);
31769 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31770 OMP_CLAUSE_CHAIN (c
) = list
;
31776 gang [( gang-arg-list )]
31777 worker [( [num:] int-expr )]
31778 vector [( [length:] int-expr )]
31780 where gang-arg is one of:
31785 and size-expr may be:
31792 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31793 const char *str
, tree list
)
31795 const char *id
= "num";
31796 cp_lexer
*lexer
= parser
->lexer
;
31797 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31798 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31800 if (kind
== OMP_CLAUSE_VECTOR
)
31803 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31805 matching_parens parens
;
31806 parens
.consume_open (parser
);
31810 cp_token
*next
= cp_lexer_peek_token (lexer
);
31813 /* Gang static argument. */
31814 if (kind
== OMP_CLAUSE_GANG
31815 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31817 cp_lexer_consume_token (lexer
);
31819 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31820 goto cleanup_error
;
31823 if (ops
[idx
] != NULL
)
31825 cp_parser_error (parser
, "too many %<static%> arguments");
31826 goto cleanup_error
;
31829 /* Check for the '*' argument. */
31830 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31831 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31832 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31835 cp_lexer_consume_token (lexer
);
31836 ops
[idx
] = integer_minus_one_node
;
31838 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31840 cp_lexer_consume_token (lexer
);
31846 /* Worker num: argument and vector length: arguments. */
31847 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31848 && id_equal (next
->u
.value
, id
)
31849 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31851 cp_lexer_consume_token (lexer
); /* id */
31852 cp_lexer_consume_token (lexer
); /* ':' */
31855 /* Now collect the actual argument. */
31856 if (ops
[idx
] != NULL_TREE
)
31858 cp_parser_error (parser
, "unexpected argument");
31859 goto cleanup_error
;
31862 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31864 if (expr
== error_mark_node
)
31865 goto cleanup_error
;
31867 mark_exp_read (expr
);
31870 if (kind
== OMP_CLAUSE_GANG
31871 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31873 cp_lexer_consume_token (lexer
);
31880 if (!parens
.require_close (parser
))
31881 goto cleanup_error
;
31884 check_no_duplicate_clause (list
, kind
, str
, loc
);
31886 c
= build_omp_clause (loc
, kind
);
31889 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31891 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31892 OMP_CLAUSE_CHAIN (c
) = list
;
31897 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31902 tile ( size-expr-list ) */
31905 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31907 tree c
, expr
= error_mark_node
;
31908 tree tile
= NULL_TREE
;
31910 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31911 so, but the spec authors never considered such a case and have
31912 differing opinions on what it might mean, including 'not
31914 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31915 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
31918 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31923 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
31926 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31927 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31928 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31930 cp_lexer_consume_token (parser
->lexer
);
31931 expr
= integer_zero_node
;
31934 expr
= cp_parser_constant_expression (parser
);
31936 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31938 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31940 /* Consume the trailing ')'. */
31941 cp_lexer_consume_token (parser
->lexer
);
31943 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31944 tile
= nreverse (tile
);
31945 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31946 OMP_CLAUSE_CHAIN (c
) = list
;
31951 Parse wait clause or directive parameters. */
31954 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31956 vec
<tree
, va_gc
> *args
;
31959 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31961 /*allow_expansion_p=*/true,
31962 /*non_constant_p=*/NULL
);
31964 if (args
== NULL
|| args
->length () == 0)
31966 cp_parser_error (parser
, "expected integer expression before ')'");
31968 release_tree_vector (args
);
31972 args_tree
= build_tree_list_vec (args
);
31974 release_tree_vector (args
);
31976 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31978 tree targ
= TREE_VALUE (t
);
31980 if (targ
!= error_mark_node
)
31982 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
31983 error ("%<wait%> expression must be integral");
31986 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
31988 targ
= mark_rvalue_use (targ
);
31989 OMP_CLAUSE_DECL (c
) = targ
;
31990 OMP_CLAUSE_CHAIN (c
) = list
;
32000 wait ( int-expr-list ) */
32003 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32005 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32007 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32010 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32016 collapse ( constant-expression ) */
32019 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32025 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32026 matching_parens parens
;
32027 if (!parens
.require_open (parser
))
32030 num
= cp_parser_constant_expression (parser
);
32032 if (!parens
.require_close (parser
))
32033 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32034 /*or_comma=*/false,
32035 /*consume_paren=*/true);
32037 if (num
== error_mark_node
)
32039 num
= fold_non_dependent_expr (num
);
32040 if (!tree_fits_shwi_p (num
)
32041 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32042 || (n
= tree_to_shwi (num
)) <= 0
32045 error_at (loc
, "collapse argument needs positive constant integer expression");
32049 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32050 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32051 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32052 OMP_CLAUSE_CHAIN (c
) = list
;
32053 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32059 default ( none | shared )
32062 default ( none | present ) */
32065 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32066 location_t location
, bool is_oacc
)
32068 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32071 matching_parens parens
;
32072 if (!parens
.require_open (parser
))
32074 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32076 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32077 const char *p
= IDENTIFIER_POINTER (id
);
32082 if (strcmp ("none", p
) != 0)
32084 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32088 if (strcmp ("present", p
) != 0 || !is_oacc
)
32090 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32094 if (strcmp ("shared", p
) != 0 || is_oacc
)
32096 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32103 cp_lexer_consume_token (parser
->lexer
);
32109 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32111 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32114 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32115 || !parens
.require_close (parser
))
32116 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32117 /*or_comma=*/false,
32118 /*consume_paren=*/true);
32120 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32123 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32124 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32125 OMP_CLAUSE_CHAIN (c
) = list
;
32126 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32132 final ( expression ) */
32135 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32139 matching_parens parens
;
32140 if (!parens
.require_open (parser
))
32143 t
= cp_parser_condition (parser
);
32145 if (t
== error_mark_node
32146 || !parens
.require_close (parser
))
32147 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32148 /*or_comma=*/false,
32149 /*consume_paren=*/true);
32151 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32153 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32154 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32155 OMP_CLAUSE_CHAIN (c
) = list
;
32164 if ( directive-name-modifier : expression )
32166 directive-name-modifier:
32167 parallel | task | taskloop | target data | target | target update
32168 | target enter data | target exit data */
32171 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32175 enum tree_code if_modifier
= ERROR_MARK
;
32177 matching_parens parens
;
32178 if (!parens
.require_open (parser
))
32181 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32183 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32184 const char *p
= IDENTIFIER_POINTER (id
);
32187 if (strcmp ("parallel", p
) == 0)
32188 if_modifier
= OMP_PARALLEL
;
32189 else if (strcmp ("task", p
) == 0)
32190 if_modifier
= OMP_TASK
;
32191 else if (strcmp ("taskloop", p
) == 0)
32192 if_modifier
= OMP_TASKLOOP
;
32193 else if (strcmp ("target", p
) == 0)
32195 if_modifier
= OMP_TARGET
;
32196 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32198 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32199 p
= IDENTIFIER_POINTER (id
);
32200 if (strcmp ("data", p
) == 0)
32201 if_modifier
= OMP_TARGET_DATA
;
32202 else if (strcmp ("update", p
) == 0)
32203 if_modifier
= OMP_TARGET_UPDATE
;
32204 else if (strcmp ("enter", p
) == 0)
32205 if_modifier
= OMP_TARGET_ENTER_DATA
;
32206 else if (strcmp ("exit", p
) == 0)
32207 if_modifier
= OMP_TARGET_EXIT_DATA
;
32208 if (if_modifier
!= OMP_TARGET
)
32213 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32214 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32216 if_modifier
= ERROR_MARK
;
32218 if (if_modifier
== OMP_TARGET_ENTER_DATA
32219 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32221 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32223 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32224 p
= IDENTIFIER_POINTER (id
);
32225 if (strcmp ("data", p
) == 0)
32231 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32232 error_at (loc
, "expected %<data%>");
32233 if_modifier
= ERROR_MARK
;
32238 if (if_modifier
!= ERROR_MARK
)
32240 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32243 cp_lexer_consume_token (parser
->lexer
);
32250 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32251 error_at (loc
, "expected %<:%>");
32253 if_modifier
= ERROR_MARK
;
32258 t
= cp_parser_condition (parser
);
32260 if (t
== error_mark_node
32261 || !parens
.require_close (parser
))
32262 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32263 /*or_comma=*/false,
32264 /*consume_paren=*/true);
32266 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32267 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32269 if (if_modifier
!= ERROR_MARK
32270 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32272 const char *p
= NULL
;
32273 switch (if_modifier
)
32275 case OMP_PARALLEL
: p
= "parallel"; break;
32276 case OMP_TASK
: p
= "task"; break;
32277 case OMP_TASKLOOP
: p
= "taskloop"; break;
32278 case OMP_TARGET_DATA
: p
= "target data"; break;
32279 case OMP_TARGET
: p
= "target"; break;
32280 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32281 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32282 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32283 default: gcc_unreachable ();
32285 error_at (location
, "too many %<if%> clauses with %qs modifier",
32289 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32292 error_at (location
, "too many %<if%> clauses");
32294 error_at (location
, "too many %<if%> clauses without modifier");
32297 else if (if_modifier
== ERROR_MARK
32298 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32300 error_at (location
, "if any %<if%> clause has modifier, then all "
32301 "%<if%> clauses have to use modifier");
32306 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32307 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32308 OMP_CLAUSE_IF_EXPR (c
) = t
;
32309 OMP_CLAUSE_CHAIN (c
) = list
;
32318 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32319 tree list
, location_t location
)
32323 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32326 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32327 OMP_CLAUSE_CHAIN (c
) = list
;
32335 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32336 tree list
, location_t location
)
32340 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32342 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32343 OMP_CLAUSE_CHAIN (c
) = list
;
32348 num_threads ( expression ) */
32351 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32352 location_t location
)
32356 matching_parens parens
;
32357 if (!parens
.require_open (parser
))
32360 t
= cp_parser_expression (parser
);
32362 if (t
== error_mark_node
32363 || !parens
.require_close (parser
))
32364 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32365 /*or_comma=*/false,
32366 /*consume_paren=*/true);
32368 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32369 "num_threads", location
);
32371 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32372 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32373 OMP_CLAUSE_CHAIN (c
) = list
;
32379 num_tasks ( expression ) */
32382 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32383 location_t location
)
32387 matching_parens parens
;
32388 if (!parens
.require_open (parser
))
32391 t
= cp_parser_expression (parser
);
32393 if (t
== error_mark_node
32394 || !parens
.require_close (parser
))
32395 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32396 /*or_comma=*/false,
32397 /*consume_paren=*/true);
32399 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32400 "num_tasks", location
);
32402 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32403 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32404 OMP_CLAUSE_CHAIN (c
) = list
;
32410 grainsize ( expression ) */
32413 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32414 location_t location
)
32418 matching_parens parens
;
32419 if (!parens
.require_open (parser
))
32422 t
= cp_parser_expression (parser
);
32424 if (t
== error_mark_node
32425 || !parens
.require_close (parser
))
32426 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32427 /*or_comma=*/false,
32428 /*consume_paren=*/true);
32430 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32431 "grainsize", location
);
32433 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32434 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32435 OMP_CLAUSE_CHAIN (c
) = list
;
32441 priority ( expression ) */
32444 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32445 location_t location
)
32449 matching_parens parens
;
32450 if (!parens
.require_open (parser
))
32453 t
= cp_parser_expression (parser
);
32455 if (t
== error_mark_node
32456 || !parens
.require_close (parser
))
32457 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32458 /*or_comma=*/false,
32459 /*consume_paren=*/true);
32461 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32462 "priority", location
);
32464 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32465 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32466 OMP_CLAUSE_CHAIN (c
) = list
;
32472 hint ( expression ) */
32475 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32476 location_t location
)
32480 matching_parens parens
;
32481 if (!parens
.require_open (parser
))
32484 t
= cp_parser_expression (parser
);
32486 if (t
== error_mark_node
32487 || !parens
.require_close (parser
))
32488 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32489 /*or_comma=*/false,
32490 /*consume_paren=*/true);
32492 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32494 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32495 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32496 OMP_CLAUSE_CHAIN (c
) = list
;
32502 defaultmap ( tofrom : scalar ) */
32505 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32506 location_t location
)
32511 matching_parens parens
;
32512 if (!parens
.require_open (parser
))
32515 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32517 cp_parser_error (parser
, "expected %<tofrom%>");
32520 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32521 p
= IDENTIFIER_POINTER (id
);
32522 if (strcmp (p
, "tofrom") != 0)
32524 cp_parser_error (parser
, "expected %<tofrom%>");
32527 cp_lexer_consume_token (parser
->lexer
);
32528 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32531 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32533 cp_parser_error (parser
, "expected %<scalar%>");
32536 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32537 p
= IDENTIFIER_POINTER (id
);
32538 if (strcmp (p
, "scalar") != 0)
32540 cp_parser_error (parser
, "expected %<scalar%>");
32543 cp_lexer_consume_token (parser
->lexer
);
32544 if (!parens
.require_close (parser
))
32547 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32550 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32551 OMP_CLAUSE_CHAIN (c
) = list
;
32555 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32556 /*or_comma=*/false,
32557 /*consume_paren=*/true);
32565 ordered ( constant-expression ) */
32568 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32569 tree list
, location_t location
)
32571 tree c
, num
= NULL_TREE
;
32574 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32575 "ordered", location
);
32577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32579 matching_parens parens
;
32580 parens
.consume_open (parser
);
32582 num
= cp_parser_constant_expression (parser
);
32584 if (!parens
.require_close (parser
))
32585 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32586 /*or_comma=*/false,
32587 /*consume_paren=*/true);
32589 if (num
== error_mark_node
)
32591 num
= fold_non_dependent_expr (num
);
32592 if (!tree_fits_shwi_p (num
)
32593 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32594 || (n
= tree_to_shwi (num
)) <= 0
32597 error_at (location
,
32598 "ordered argument needs positive constant integer "
32604 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32605 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32606 OMP_CLAUSE_CHAIN (c
) = list
;
32611 reduction ( reduction-operator : variable-list )
32613 reduction-operator:
32614 One of: + * - & ^ | && ||
32618 reduction-operator:
32619 One of: + * - & ^ | && || min max
32623 reduction-operator:
32624 One of: + * - & ^ | && ||
32628 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32630 enum tree_code code
= ERROR_MARK
;
32631 tree nlist
, c
, id
= NULL_TREE
;
32633 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32636 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32638 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32639 case CPP_MULT
: code
= MULT_EXPR
; break;
32640 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32641 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32642 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32643 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32644 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32645 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32649 if (code
!= ERROR_MARK
)
32650 cp_lexer_consume_token (parser
->lexer
);
32653 bool saved_colon_corrects_to_scope_p
;
32654 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32655 parser
->colon_corrects_to_scope_p
= false;
32656 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32657 /*check_dependency_p=*/true,
32658 /*template_p=*/NULL
,
32659 /*declarator_p=*/false,
32660 /*optional_p=*/false);
32661 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32662 if (identifier_p (id
))
32664 const char *p
= IDENTIFIER_POINTER (id
);
32666 if (strcmp (p
, "min") == 0)
32668 else if (strcmp (p
, "max") == 0)
32670 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32672 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32674 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32676 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32677 code
= BIT_AND_EXPR
;
32678 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32679 code
= BIT_IOR_EXPR
;
32680 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32681 code
= BIT_XOR_EXPR
;
32682 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32683 code
= TRUTH_ANDIF_EXPR
;
32684 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32685 code
= TRUTH_ORIF_EXPR
;
32686 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32687 tree scope
= parser
->scope
;
32689 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32690 parser
->scope
= NULL_TREE
;
32691 parser
->qualifying_scope
= NULL_TREE
;
32692 parser
->object_scope
= NULL_TREE
;
32696 error ("invalid reduction-identifier");
32698 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32699 /*or_comma=*/false,
32700 /*consume_paren=*/true);
32705 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32708 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32710 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32712 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32713 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32720 schedule ( schedule-kind )
32721 schedule ( schedule-kind , expression )
32724 static | dynamic | guided | runtime | auto
32727 schedule ( schedule-modifier : schedule-kind )
32728 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32736 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32739 int modifiers
= 0, nmodifiers
= 0;
32741 matching_parens parens
;
32742 if (!parens
.require_open (parser
))
32745 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32747 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32749 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32750 const char *p
= IDENTIFIER_POINTER (id
);
32751 if (strcmp ("simd", p
) == 0)
32752 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32753 else if (strcmp ("monotonic", p
) == 0)
32754 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32755 else if (strcmp ("nonmonotonic", p
) == 0)
32756 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32759 cp_lexer_consume_token (parser
->lexer
);
32760 if (nmodifiers
++ == 0
32761 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32762 cp_lexer_consume_token (parser
->lexer
);
32765 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32770 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32772 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32773 const char *p
= IDENTIFIER_POINTER (id
);
32778 if (strcmp ("dynamic", p
) != 0)
32780 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32784 if (strcmp ("guided", p
) != 0)
32786 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32790 if (strcmp ("runtime", p
) != 0)
32792 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32799 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32800 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32801 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32802 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32805 cp_lexer_consume_token (parser
->lexer
);
32807 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32808 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32809 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32810 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32812 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32817 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32820 cp_lexer_consume_token (parser
->lexer
);
32822 token
= cp_lexer_peek_token (parser
->lexer
);
32823 t
= cp_parser_assignment_expression (parser
);
32825 if (t
== error_mark_node
)
32827 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32828 error_at (token
->location
, "schedule %<runtime%> does not take "
32829 "a %<chunk_size%> parameter");
32830 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32831 error_at (token
->location
, "schedule %<auto%> does not take "
32832 "a %<chunk_size%> parameter");
32834 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32836 if (!parens
.require_close (parser
))
32839 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32842 OMP_CLAUSE_SCHEDULE_KIND (c
)
32843 = (enum omp_clause_schedule_kind
)
32844 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32846 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32847 OMP_CLAUSE_CHAIN (c
) = list
;
32851 cp_parser_error (parser
, "invalid schedule kind");
32853 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32854 /*or_comma=*/false,
32855 /*consume_paren=*/true);
32863 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32864 tree list
, location_t location
)
32868 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32870 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32871 OMP_CLAUSE_CHAIN (c
) = list
;
32880 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32881 tree list
, location_t location
)
32883 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32884 tree c
= build_omp_clause (location
, code
);
32885 OMP_CLAUSE_CHAIN (c
) = list
;
32896 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32897 enum omp_clause_code code
,
32898 tree list
, location_t location
)
32900 tree c
= build_omp_clause (location
, code
);
32901 OMP_CLAUSE_CHAIN (c
) = list
;
32909 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32910 tree list
, location_t location
)
32912 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32913 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32914 OMP_CLAUSE_CHAIN (c
) = list
;
32923 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32924 enum omp_clause_code code
,
32925 tree list
, location_t location
)
32927 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32928 tree c
= build_omp_clause (location
, code
);
32929 OMP_CLAUSE_CHAIN (c
) = list
;
32934 num_teams ( expression ) */
32937 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32938 location_t location
)
32942 matching_parens parens
;
32943 if (!parens
.require_open (parser
))
32946 t
= cp_parser_expression (parser
);
32948 if (t
== error_mark_node
32949 || !parens
.require_close (parser
))
32950 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32951 /*or_comma=*/false,
32952 /*consume_paren=*/true);
32954 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32955 "num_teams", location
);
32957 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32958 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32959 OMP_CLAUSE_CHAIN (c
) = list
;
32965 thread_limit ( expression ) */
32968 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32969 location_t location
)
32973 matching_parens parens
;
32974 if (!parens
.require_open (parser
))
32977 t
= cp_parser_expression (parser
);
32979 if (t
== error_mark_node
32980 || !parens
.require_close (parser
))
32981 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32982 /*or_comma=*/false,
32983 /*consume_paren=*/true);
32985 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
32986 "thread_limit", location
);
32988 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
32989 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
32990 OMP_CLAUSE_CHAIN (c
) = list
;
32996 aligned ( variable-list )
32997 aligned ( variable-list : constant-expression ) */
33000 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33002 tree nlist
, c
, alignment
= NULL_TREE
;
33005 matching_parens parens
;
33006 if (!parens
.require_open (parser
))
33009 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33014 alignment
= cp_parser_constant_expression (parser
);
33016 if (!parens
.require_close (parser
))
33017 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33018 /*or_comma=*/false,
33019 /*consume_paren=*/true);
33021 if (alignment
== error_mark_node
)
33022 alignment
= NULL_TREE
;
33025 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33026 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33032 linear ( variable-list )
33033 linear ( variable-list : expression )
33036 linear ( modifier ( variable-list ) )
33037 linear ( modifier ( variable-list ) : expression ) */
33040 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33043 tree nlist
, c
, step
= integer_one_node
;
33045 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33047 matching_parens parens
;
33048 if (!parens
.require_open (parser
))
33051 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33053 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33054 const char *p
= IDENTIFIER_POINTER (id
);
33056 if (strcmp ("ref", p
) == 0)
33057 kind
= OMP_CLAUSE_LINEAR_REF
;
33058 else if (strcmp ("val", p
) == 0)
33059 kind
= OMP_CLAUSE_LINEAR_VAL
;
33060 else if (strcmp ("uval", p
) == 0)
33061 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33062 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33063 cp_lexer_consume_token (parser
->lexer
);
33065 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33068 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33069 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33073 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33074 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33076 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33077 else if (!parens
.require_close (parser
))
33078 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33079 /*or_comma=*/false,
33080 /*consume_paren=*/true);
33087 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33088 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33090 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33091 cp_parser_parse_tentatively (parser
);
33092 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33093 /*check_dependency_p=*/true,
33094 /*template_p=*/NULL
,
33095 /*declarator_p=*/false,
33096 /*optional_p=*/false);
33097 if (step
!= error_mark_node
)
33098 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33099 if (step
== error_mark_node
)
33102 cp_parser_abort_tentative_parse (parser
);
33104 else if (!cp_parser_parse_definitely (parser
))
33108 step
= cp_parser_expression (parser
);
33110 if (!parens
.require_close (parser
))
33111 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33112 /*or_comma=*/false,
33113 /*consume_paren=*/true);
33115 if (step
== error_mark_node
)
33119 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33121 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33122 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33129 safelen ( constant-expression ) */
33132 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33133 location_t location
)
33137 matching_parens parens
;
33138 if (!parens
.require_open (parser
))
33141 t
= cp_parser_constant_expression (parser
);
33143 if (t
== error_mark_node
33144 || !parens
.require_close (parser
))
33145 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33146 /*or_comma=*/false,
33147 /*consume_paren=*/true);
33149 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33151 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33152 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33153 OMP_CLAUSE_CHAIN (c
) = list
;
33159 simdlen ( constant-expression ) */
33162 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33163 location_t location
)
33167 matching_parens parens
;
33168 if (!parens
.require_open (parser
))
33171 t
= cp_parser_constant_expression (parser
);
33173 if (t
== error_mark_node
33174 || !parens
.require_close (parser
))
33175 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33176 /*or_comma=*/false,
33177 /*consume_paren=*/true);
33179 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33181 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33182 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33183 OMP_CLAUSE_CHAIN (c
) = list
;
33190 identifier [+/- integer]
33191 vec , identifier [+/- integer]
33195 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33200 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33202 cp_parser_error (parser
, "expected identifier");
33206 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33208 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33209 tree t
, identifier
= cp_parser_identifier (parser
);
33210 tree addend
= NULL
;
33212 if (identifier
== error_mark_node
)
33213 t
= error_mark_node
;
33216 t
= cp_parser_lookup_name_simple
33217 (parser
, identifier
,
33218 cp_lexer_peek_token (parser
->lexer
)->location
);
33219 if (t
== error_mark_node
)
33220 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33225 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33227 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33229 addend
= integer_zero_node
;
33230 goto add_to_vector
;
33232 cp_lexer_consume_token (parser
->lexer
);
33234 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33236 cp_parser_error (parser
, "expected integer");
33240 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33241 if (TREE_CODE (addend
) != INTEGER_CST
)
33243 cp_parser_error (parser
, "expected integer");
33246 cp_lexer_consume_token (parser
->lexer
);
33249 if (t
!= error_mark_node
)
33251 vec
= tree_cons (addend
, t
, vec
);
33253 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33256 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33259 cp_lexer_consume_token (parser
->lexer
);
33262 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33264 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33265 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33266 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33267 OMP_CLAUSE_CHAIN (u
) = list
;
33274 depend ( depend-kind : variable-list )
33282 depend ( sink : vec ) */
33285 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33288 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33290 matching_parens parens
;
33291 if (!parens
.require_open (parser
))
33294 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33296 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33297 const char *p
= IDENTIFIER_POINTER (id
);
33299 if (strcmp ("in", p
) == 0)
33300 kind
= OMP_CLAUSE_DEPEND_IN
;
33301 else if (strcmp ("inout", p
) == 0)
33302 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33303 else if (strcmp ("out", p
) == 0)
33304 kind
= OMP_CLAUSE_DEPEND_OUT
;
33305 else if (strcmp ("source", p
) == 0)
33306 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33307 else if (strcmp ("sink", p
) == 0)
33308 kind
= OMP_CLAUSE_DEPEND_SINK
;
33315 cp_lexer_consume_token (parser
->lexer
);
33317 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33319 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33320 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33321 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33322 OMP_CLAUSE_CHAIN (c
) = list
;
33323 if (!parens
.require_close (parser
))
33324 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33325 /*or_comma=*/false,
33326 /*consume_paren=*/true);
33330 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33333 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33334 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33337 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33340 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33341 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33346 cp_parser_error (parser
, "invalid depend kind");
33348 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33349 /*or_comma=*/false,
33350 /*consume_paren=*/true);
33355 map ( map-kind : variable-list )
33356 map ( variable-list )
33359 alloc | to | from | tofrom
33363 alloc | to | from | tofrom | release | delete
33365 map ( always [,] map-kind: variable-list ) */
33368 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33371 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33372 bool always
= false;
33374 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33377 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33379 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33380 const char *p
= IDENTIFIER_POINTER (id
);
33382 if (strcmp ("always", p
) == 0)
33385 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33387 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33388 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33390 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33394 cp_lexer_consume_token (parser
->lexer
);
33396 cp_lexer_consume_token (parser
->lexer
);
33401 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33402 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33404 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33405 const char *p
= IDENTIFIER_POINTER (id
);
33407 if (strcmp ("alloc", p
) == 0)
33408 kind
= GOMP_MAP_ALLOC
;
33409 else if (strcmp ("to", p
) == 0)
33410 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33411 else if (strcmp ("from", p
) == 0)
33412 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33413 else if (strcmp ("tofrom", p
) == 0)
33414 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33415 else if (strcmp ("release", p
) == 0)
33416 kind
= GOMP_MAP_RELEASE
;
33419 cp_parser_error (parser
, "invalid map kind");
33420 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33421 /*or_comma=*/false,
33422 /*consume_paren=*/true);
33425 cp_lexer_consume_token (parser
->lexer
);
33426 cp_lexer_consume_token (parser
->lexer
);
33428 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33429 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33431 kind
= GOMP_MAP_DELETE
;
33432 cp_lexer_consume_token (parser
->lexer
);
33433 cp_lexer_consume_token (parser
->lexer
);
33436 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33439 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33440 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33446 device ( expression ) */
33449 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33450 location_t location
)
33454 matching_parens parens
;
33455 if (!parens
.require_open (parser
))
33458 t
= cp_parser_expression (parser
);
33460 if (t
== error_mark_node
33461 || !parens
.require_close (parser
))
33462 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33463 /*or_comma=*/false,
33464 /*consume_paren=*/true);
33466 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33467 "device", location
);
33469 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33470 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33471 OMP_CLAUSE_CHAIN (c
) = list
;
33477 dist_schedule ( static )
33478 dist_schedule ( static , expression ) */
33481 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33482 location_t location
)
33486 matching_parens parens
;
33487 if (!parens
.require_open (parser
))
33490 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33492 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33494 cp_lexer_consume_token (parser
->lexer
);
33496 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33498 cp_lexer_consume_token (parser
->lexer
);
33500 t
= cp_parser_assignment_expression (parser
);
33502 if (t
== error_mark_node
)
33504 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33506 if (!parens
.require_close (parser
))
33509 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33512 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33514 OMP_CLAUSE_CHAIN (c
) = list
;
33518 cp_parser_error (parser
, "invalid dist_schedule kind");
33520 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33521 /*or_comma=*/false,
33522 /*consume_paren=*/true);
33527 proc_bind ( proc-bind-kind )
33530 master | close | spread */
33533 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33534 location_t location
)
33537 enum omp_clause_proc_bind_kind kind
;
33539 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33542 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33544 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33545 const char *p
= IDENTIFIER_POINTER (id
);
33547 if (strcmp ("master", p
) == 0)
33548 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33549 else if (strcmp ("close", p
) == 0)
33550 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33551 else if (strcmp ("spread", p
) == 0)
33552 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33559 cp_lexer_consume_token (parser
->lexer
);
33560 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33563 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33564 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33566 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33567 OMP_CLAUSE_CHAIN (c
) = list
;
33571 cp_parser_error (parser
, "invalid depend kind");
33573 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33574 /*or_comma=*/false,
33575 /*consume_paren=*/true);
33580 async [( int-expr )] */
33583 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33586 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33588 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33590 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33592 matching_parens parens
;
33593 parens
.consume_open (parser
);
33595 t
= cp_parser_expression (parser
);
33596 if (t
== error_mark_node
33597 || !parens
.require_close (parser
))
33598 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33599 /*or_comma=*/false,
33600 /*consume_paren=*/true);
33603 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33605 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33606 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33607 OMP_CLAUSE_CHAIN (c
) = list
;
33613 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33614 is a bitmask in MASK. Return the list of clauses found. */
33617 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33618 const char *where
, cp_token
*pragma_tok
,
33619 bool finish_p
= true)
33621 tree clauses
= NULL
;
33624 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33627 pragma_omp_clause c_kind
;
33628 omp_clause_code code
;
33629 const char *c_name
;
33630 tree prev
= clauses
;
33632 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33633 cp_lexer_consume_token (parser
->lexer
);
33635 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33636 c_kind
= cp_parser_omp_clause_name (parser
);
33640 case PRAGMA_OACC_CLAUSE_ASYNC
:
33641 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33644 case PRAGMA_OACC_CLAUSE_AUTO
:
33645 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33649 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33650 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33651 c_name
= "collapse";
33653 case PRAGMA_OACC_CLAUSE_COPY
:
33654 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33657 case PRAGMA_OACC_CLAUSE_COPYIN
:
33658 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33661 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33662 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33663 c_name
= "copyout";
33665 case PRAGMA_OACC_CLAUSE_CREATE
:
33666 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33669 case PRAGMA_OACC_CLAUSE_DELETE
:
33670 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33673 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33674 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33675 c_name
= "default";
33677 case PRAGMA_OACC_CLAUSE_DEVICE
:
33678 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33681 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33682 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33683 c_name
= "deviceptr";
33685 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33686 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33687 c_name
= "device_resident";
33689 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33690 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33692 c_name
= "firstprivate";
33694 case PRAGMA_OACC_CLAUSE_GANG
:
33696 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33699 case PRAGMA_OACC_CLAUSE_HOST
:
33700 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33703 case PRAGMA_OACC_CLAUSE_IF
:
33704 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33707 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33708 clauses
= cp_parser_oacc_simple_clause (parser
,
33709 OMP_CLAUSE_INDEPENDENT
,
33711 c_name
= "independent";
33713 case PRAGMA_OACC_CLAUSE_LINK
:
33714 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33717 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33718 code
= OMP_CLAUSE_NUM_GANGS
;
33719 c_name
= "num_gangs";
33720 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33723 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33724 c_name
= "num_workers";
33725 code
= OMP_CLAUSE_NUM_WORKERS
;
33726 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33729 case PRAGMA_OACC_CLAUSE_PRESENT
:
33730 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33731 c_name
= "present";
33733 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33734 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33735 c_name
= "present_or_copy";
33737 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33738 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33739 c_name
= "present_or_copyin";
33741 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33742 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33743 c_name
= "present_or_copyout";
33745 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33746 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33747 c_name
= "present_or_create";
33749 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33750 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33752 c_name
= "private";
33754 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33755 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33756 c_name
= "reduction";
33758 case PRAGMA_OACC_CLAUSE_SELF
:
33759 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33762 case PRAGMA_OACC_CLAUSE_SEQ
:
33763 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33767 case PRAGMA_OACC_CLAUSE_TILE
:
33768 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33771 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33772 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33774 c_name
= "use_device";
33776 case PRAGMA_OACC_CLAUSE_VECTOR
:
33778 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33781 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33782 c_name
= "vector_length";
33783 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33784 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33787 case PRAGMA_OACC_CLAUSE_WAIT
:
33788 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33791 case PRAGMA_OACC_CLAUSE_WORKER
:
33793 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33797 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33803 if (((mask
>> c_kind
) & 1) == 0)
33805 /* Remove the invalid clause(s) from the list to avoid
33806 confusing the rest of the compiler. */
33808 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33813 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33816 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33821 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33822 is a bitmask in MASK. Return the list of clauses found; the result
33823 of clause default goes in *pdefault. */
33826 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33827 const char *where
, cp_token
*pragma_tok
,
33828 bool finish_p
= true)
33830 tree clauses
= NULL
;
33832 cp_token
*token
= NULL
;
33834 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33836 pragma_omp_clause c_kind
;
33837 const char *c_name
;
33838 tree prev
= clauses
;
33840 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33841 cp_lexer_consume_token (parser
->lexer
);
33843 token
= cp_lexer_peek_token (parser
->lexer
);
33844 c_kind
= cp_parser_omp_clause_name (parser
);
33848 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33849 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33851 c_name
= "collapse";
33853 case PRAGMA_OMP_CLAUSE_COPYIN
:
33854 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33857 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33858 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33860 c_name
= "copyprivate";
33862 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33863 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33864 token
->location
, false);
33865 c_name
= "default";
33867 case PRAGMA_OMP_CLAUSE_FINAL
:
33868 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33871 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33872 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33874 c_name
= "firstprivate";
33876 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33877 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33879 c_name
= "grainsize";
33881 case PRAGMA_OMP_CLAUSE_HINT
:
33882 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33886 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33887 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33889 c_name
= "defaultmap";
33891 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33892 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33894 c_name
= "use_device_ptr";
33896 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33897 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33899 c_name
= "is_device_ptr";
33901 case PRAGMA_OMP_CLAUSE_IF
:
33902 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33906 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33907 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33909 c_name
= "lastprivate";
33911 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33912 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33914 c_name
= "mergeable";
33916 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33917 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33920 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33921 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33923 c_name
= "num_tasks";
33925 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33926 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33928 c_name
= "num_threads";
33930 case PRAGMA_OMP_CLAUSE_ORDERED
:
33931 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33933 c_name
= "ordered";
33935 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33936 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33938 c_name
= "priority";
33940 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33941 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33943 c_name
= "private";
33945 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33946 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33947 c_name
= "reduction";
33949 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33950 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33952 c_name
= "schedule";
33954 case PRAGMA_OMP_CLAUSE_SHARED
:
33955 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33959 case PRAGMA_OMP_CLAUSE_UNTIED
:
33960 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33964 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33965 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33966 clauses
, token
->location
);
33967 c_name
= "inbranch";
33969 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33970 clauses
= cp_parser_omp_clause_branch (parser
,
33971 OMP_CLAUSE_NOTINBRANCH
,
33972 clauses
, token
->location
);
33973 c_name
= "notinbranch";
33975 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33976 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33977 clauses
, token
->location
);
33978 c_name
= "parallel";
33982 error_at (token
->location
, "%qs must be the first clause of %qs",
33987 case PRAGMA_OMP_CLAUSE_FOR
:
33988 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
33989 clauses
, token
->location
);
33992 goto clause_not_first
;
33994 case PRAGMA_OMP_CLAUSE_SECTIONS
:
33995 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
33996 clauses
, token
->location
);
33997 c_name
= "sections";
33999 goto clause_not_first
;
34001 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34002 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34003 clauses
, token
->location
);
34004 c_name
= "taskgroup";
34006 goto clause_not_first
;
34008 case PRAGMA_OMP_CLAUSE_LINK
:
34009 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34012 case PRAGMA_OMP_CLAUSE_TO
:
34013 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34014 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34017 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34020 case PRAGMA_OMP_CLAUSE_FROM
:
34021 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34024 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34025 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34027 c_name
= "uniform";
34029 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34030 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34032 c_name
= "num_teams";
34034 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34035 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34037 c_name
= "thread_limit";
34039 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34040 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34041 c_name
= "aligned";
34043 case PRAGMA_OMP_CLAUSE_LINEAR
:
34045 bool declare_simd
= false;
34046 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34047 declare_simd
= true;
34048 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34052 case PRAGMA_OMP_CLAUSE_DEPEND
:
34053 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34057 case PRAGMA_OMP_CLAUSE_MAP
:
34058 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34061 case PRAGMA_OMP_CLAUSE_DEVICE
:
34062 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34066 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34067 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34069 c_name
= "dist_schedule";
34071 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34072 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34074 c_name
= "proc_bind";
34076 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34077 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34079 c_name
= "safelen";
34081 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34082 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34084 c_name
= "simdlen";
34086 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34087 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34089 c_name
= "nogroup";
34091 case PRAGMA_OMP_CLAUSE_THREADS
:
34093 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34094 clauses
, token
->location
);
34095 c_name
= "threads";
34097 case PRAGMA_OMP_CLAUSE_SIMD
:
34099 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34100 clauses
, token
->location
);
34104 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34110 if (((mask
>> c_kind
) & 1) == 0)
34112 /* Remove the invalid clause(s) from the list to avoid
34113 confusing the rest of the compiler. */
34115 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34119 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34122 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34123 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34125 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34134 In practice, we're also interested in adding the statement to an
34135 outer node. So it is convenient if we work around the fact that
34136 cp_parser_statement calls add_stmt. */
34139 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34141 unsigned save
= parser
->in_statement
;
34143 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34144 This preserves the "not within loop or switch" style error messages
34145 for nonsense cases like
34151 if (parser
->in_statement
)
34152 parser
->in_statement
= IN_OMP_BLOCK
;
34158 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34160 parser
->in_statement
= save
;
34164 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34166 tree stmt
= begin_omp_structured_block ();
34167 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34169 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34171 cp_parser_end_omp_structured_block (parser
, save
);
34172 return finish_omp_structured_block (stmt
);
34176 # pragma omp atomic new-line
34180 x binop= expr | x++ | ++x | x-- | --x
34182 +, *, -, /, &, ^, |, <<, >>
34184 where x is an lvalue expression with scalar type.
34187 # pragma omp atomic new-line
34190 # pragma omp atomic read new-line
34193 # pragma omp atomic write new-line
34196 # pragma omp atomic update new-line
34199 # pragma omp atomic capture new-line
34202 # pragma omp atomic capture new-line
34210 expression-stmt | x = x binop expr
34212 v = expression-stmt
34214 { v = x; update-stmt; } | { update-stmt; v = x; }
34218 expression-stmt | x = x binop expr | x = expr binop x
34222 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34224 where x and v are lvalue expressions with scalar type. */
34227 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34229 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34230 tree rhs1
= NULL_TREE
, orig_lhs
;
34231 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34232 bool structured_block
= false;
34233 bool seq_cst
= false;
34235 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34237 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34238 const char *p
= IDENTIFIER_POINTER (id
);
34240 if (!strcmp (p
, "seq_cst"))
34243 cp_lexer_consume_token (parser
->lexer
);
34244 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34245 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34246 cp_lexer_consume_token (parser
->lexer
);
34249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34251 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34252 const char *p
= IDENTIFIER_POINTER (id
);
34254 if (!strcmp (p
, "read"))
34255 code
= OMP_ATOMIC_READ
;
34256 else if (!strcmp (p
, "write"))
34258 else if (!strcmp (p
, "update"))
34260 else if (!strcmp (p
, "capture"))
34261 code
= OMP_ATOMIC_CAPTURE_NEW
;
34265 cp_lexer_consume_token (parser
->lexer
);
34269 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34270 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34271 cp_lexer_consume_token (parser
->lexer
);
34273 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34275 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34276 const char *p
= IDENTIFIER_POINTER (id
);
34278 if (!strcmp (p
, "seq_cst"))
34281 cp_lexer_consume_token (parser
->lexer
);
34285 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34289 case OMP_ATOMIC_READ
:
34290 case NOP_EXPR
: /* atomic write */
34291 v
= cp_parser_unary_expression (parser
);
34292 if (v
== error_mark_node
)
34294 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34296 if (code
== NOP_EXPR
)
34297 lhs
= cp_parser_expression (parser
);
34299 lhs
= cp_parser_unary_expression (parser
);
34300 if (lhs
== error_mark_node
)
34302 if (code
== NOP_EXPR
)
34304 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34312 case OMP_ATOMIC_CAPTURE_NEW
:
34313 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34315 cp_lexer_consume_token (parser
->lexer
);
34316 structured_block
= true;
34320 v
= cp_parser_unary_expression (parser
);
34321 if (v
== error_mark_node
)
34323 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34331 lhs
= cp_parser_unary_expression (parser
);
34333 switch (TREE_CODE (lhs
))
34338 case POSTINCREMENT_EXPR
:
34339 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34340 code
= OMP_ATOMIC_CAPTURE_OLD
;
34342 case PREINCREMENT_EXPR
:
34343 lhs
= TREE_OPERAND (lhs
, 0);
34344 opcode
= PLUS_EXPR
;
34345 rhs
= integer_one_node
;
34348 case POSTDECREMENT_EXPR
:
34349 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34350 code
= OMP_ATOMIC_CAPTURE_OLD
;
34352 case PREDECREMENT_EXPR
:
34353 lhs
= TREE_OPERAND (lhs
, 0);
34354 opcode
= MINUS_EXPR
;
34355 rhs
= integer_one_node
;
34358 case COMPOUND_EXPR
:
34359 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34360 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34361 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34362 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34363 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34364 (TREE_OPERAND (lhs
, 1), 0), 0)))
34366 /* Undo effects of boolean_increment for post {in,de}crement. */
34367 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34370 if (TREE_CODE (lhs
) == MODIFY_EXPR
34371 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34373 /* Undo effects of boolean_increment. */
34374 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34376 /* This is pre or post increment. */
34377 rhs
= TREE_OPERAND (lhs
, 1);
34378 lhs
= TREE_OPERAND (lhs
, 0);
34380 if (code
== OMP_ATOMIC_CAPTURE_NEW
34381 && !structured_block
34382 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34383 code
= OMP_ATOMIC_CAPTURE_OLD
;
34389 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34392 opcode
= MULT_EXPR
;
34395 opcode
= TRUNC_DIV_EXPR
;
34398 opcode
= PLUS_EXPR
;
34401 opcode
= MINUS_EXPR
;
34403 case CPP_LSHIFT_EQ
:
34404 opcode
= LSHIFT_EXPR
;
34406 case CPP_RSHIFT_EQ
:
34407 opcode
= RSHIFT_EXPR
;
34410 opcode
= BIT_AND_EXPR
;
34413 opcode
= BIT_IOR_EXPR
;
34416 opcode
= BIT_XOR_EXPR
;
34419 enum cp_parser_prec oprec
;
34421 cp_lexer_consume_token (parser
->lexer
);
34422 cp_parser_parse_tentatively (parser
);
34423 rhs1
= cp_parser_simple_cast_expression (parser
);
34424 if (rhs1
== error_mark_node
)
34426 cp_parser_abort_tentative_parse (parser
);
34427 cp_parser_simple_cast_expression (parser
);
34430 token
= cp_lexer_peek_token (parser
->lexer
);
34431 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34433 cp_parser_abort_tentative_parse (parser
);
34434 cp_parser_parse_tentatively (parser
);
34435 rhs
= cp_parser_binary_expression (parser
, false, true,
34436 PREC_NOT_OPERATOR
, NULL
);
34437 if (rhs
== error_mark_node
)
34439 cp_parser_abort_tentative_parse (parser
);
34440 cp_parser_binary_expression (parser
, false, true,
34441 PREC_NOT_OPERATOR
, NULL
);
34444 switch (TREE_CODE (rhs
))
34447 case TRUNC_DIV_EXPR
:
34456 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34458 if (cp_parser_parse_definitely (parser
))
34460 opcode
= TREE_CODE (rhs
);
34461 rhs1
= TREE_OPERAND (rhs
, 0);
34462 rhs
= TREE_OPERAND (rhs
, 1);
34472 cp_parser_abort_tentative_parse (parser
);
34473 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34475 rhs
= cp_parser_expression (parser
);
34476 if (rhs
== error_mark_node
)
34482 cp_parser_error (parser
,
34483 "invalid form of %<#pragma omp atomic%>");
34486 if (!cp_parser_parse_definitely (parser
))
34488 switch (token
->type
)
34490 case CPP_SEMICOLON
:
34491 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34493 code
= OMP_ATOMIC_CAPTURE_OLD
;
34498 cp_lexer_consume_token (parser
->lexer
);
34501 else if (structured_block
)
34508 cp_parser_error (parser
,
34509 "invalid form of %<#pragma omp atomic%>");
34512 opcode
= MULT_EXPR
;
34515 opcode
= TRUNC_DIV_EXPR
;
34518 opcode
= PLUS_EXPR
;
34521 opcode
= MINUS_EXPR
;
34524 opcode
= LSHIFT_EXPR
;
34527 opcode
= RSHIFT_EXPR
;
34530 opcode
= BIT_AND_EXPR
;
34533 opcode
= BIT_IOR_EXPR
;
34536 opcode
= BIT_XOR_EXPR
;
34539 cp_parser_error (parser
,
34540 "invalid operator for %<#pragma omp atomic%>");
34543 oprec
= TOKEN_PRECEDENCE (token
);
34544 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34545 if (commutative_tree_code (opcode
))
34546 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34547 cp_lexer_consume_token (parser
->lexer
);
34548 rhs
= cp_parser_binary_expression (parser
, false, false,
34550 if (rhs
== error_mark_node
)
34555 cp_parser_error (parser
,
34556 "invalid operator for %<#pragma omp atomic%>");
34559 cp_lexer_consume_token (parser
->lexer
);
34561 rhs
= cp_parser_expression (parser
);
34562 if (rhs
== error_mark_node
)
34567 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34569 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34571 v
= cp_parser_unary_expression (parser
);
34572 if (v
== error_mark_node
)
34574 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34576 lhs1
= cp_parser_unary_expression (parser
);
34577 if (lhs1
== error_mark_node
)
34580 if (structured_block
)
34582 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34583 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34586 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34587 if (!structured_block
)
34588 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34592 cp_parser_skip_to_end_of_block_or_statement (parser
);
34593 if (structured_block
)
34595 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34596 cp_lexer_consume_token (parser
->lexer
);
34597 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34599 cp_parser_skip_to_end_of_block_or_statement (parser
);
34600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34601 cp_lexer_consume_token (parser
->lexer
);
34608 # pragma omp barrier new-line */
34611 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34613 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34614 finish_omp_barrier ();
34618 # pragma omp critical [(name)] new-line
34622 # pragma omp critical [(name) [hint(expression)]] new-line
34623 structured-block */
34625 #define OMP_CRITICAL_CLAUSE_MASK \
34626 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34629 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34631 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34633 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34635 matching_parens parens
;
34636 parens
.consume_open (parser
);
34638 name
= cp_parser_identifier (parser
);
34640 if (name
== error_mark_node
34641 || !parens
.require_close (parser
))
34642 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34643 /*or_comma=*/false,
34644 /*consume_paren=*/true);
34645 if (name
== error_mark_node
)
34648 clauses
= cp_parser_omp_all_clauses (parser
,
34649 OMP_CRITICAL_CLAUSE_MASK
,
34650 "#pragma omp critical", pragma_tok
);
34653 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34655 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34656 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34660 # pragma omp flush flush-vars[opt] new-line
34663 ( variable-list ) */
34666 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34669 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34670 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34672 finish_omp_flush ();
34675 /* Helper function, to parse omp for increment expression. */
34678 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34680 tree cond
= cp_parser_binary_expression (parser
, false, true,
34681 PREC_NOT_OPERATOR
, NULL
);
34682 if (cond
== error_mark_node
34683 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34685 cp_parser_skip_to_end_of_statement (parser
);
34686 return error_mark_node
;
34689 switch (TREE_CODE (cond
))
34697 /* Fall through: OpenMP disallows NE_EXPR. */
34698 gcc_fallthrough ();
34700 return error_mark_node
;
34703 /* If decl is an iterator, preserve LHS and RHS of the relational
34704 expr until finish_omp_for. */
34706 && (type_dependent_expression_p (decl
)
34707 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34710 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34712 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34713 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34714 /*overload=*/NULL
, tf_warning_or_error
);
34717 /* Helper function, to parse omp for increment expression. */
34720 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34722 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34728 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34730 op
= (token
->type
== CPP_PLUS_PLUS
34731 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34732 cp_lexer_consume_token (parser
->lexer
);
34733 lhs
= cp_parser_simple_cast_expression (parser
);
34735 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34736 return error_mark_node
;
34737 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34740 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34742 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34743 return error_mark_node
;
34745 token
= cp_lexer_peek_token (parser
->lexer
);
34746 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34748 op
= (token
->type
== CPP_PLUS_PLUS
34749 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34750 cp_lexer_consume_token (parser
->lexer
);
34751 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34754 op
= cp_parser_assignment_operator_opt (parser
);
34755 if (op
== ERROR_MARK
)
34756 return error_mark_node
;
34758 if (op
!= NOP_EXPR
)
34760 rhs
= cp_parser_assignment_expression (parser
);
34761 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34762 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34765 lhs
= cp_parser_binary_expression (parser
, false, false,
34766 PREC_ADDITIVE_EXPRESSION
, NULL
);
34767 token
= cp_lexer_peek_token (parser
->lexer
);
34768 decl_first
= (lhs
== decl
34769 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34772 if (token
->type
!= CPP_PLUS
34773 && token
->type
!= CPP_MINUS
)
34774 return error_mark_node
;
34778 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34779 cp_lexer_consume_token (parser
->lexer
);
34780 rhs
= cp_parser_binary_expression (parser
, false, false,
34781 PREC_ADDITIVE_EXPRESSION
, NULL
);
34782 token
= cp_lexer_peek_token (parser
->lexer
);
34783 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34785 if (lhs
== NULL_TREE
)
34787 if (op
== PLUS_EXPR
)
34790 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34791 tf_warning_or_error
);
34794 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34795 ERROR_MARK
, NULL
, tf_warning_or_error
);
34798 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34803 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34804 || op
== MINUS_EXPR
)
34805 return error_mark_node
;
34806 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34809 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34811 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34814 /* Parse the initialization statement of an OpenMP for loop.
34816 Return true if the resulting construct should have an
34817 OMP_CLAUSE_PRIVATE added to it. */
34820 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34821 tree
&this_pre_body
,
34822 vec
<tree
, va_gc
> *for_block
,
34828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34831 tree add_private_clause
= NULL_TREE
;
34833 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34837 integer-type var = lb
34838 random-access-iterator-type var = lb
34839 pointer-type var = lb
34841 cp_decl_specifier_seq type_specifiers
;
34843 /* First, try to parse as an initialized declaration. See
34844 cp_parser_condition, from whence the bulk of this is copied. */
34846 cp_parser_parse_tentatively (parser
);
34847 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34848 /*is_trailing_return=*/false,
34850 if (cp_parser_parse_definitely (parser
))
34852 /* If parsing a type specifier seq succeeded, then this
34853 MUST be a initialized declaration. */
34854 tree asm_specification
, attributes
;
34855 cp_declarator
*declarator
;
34857 declarator
= cp_parser_declarator (parser
,
34858 CP_PARSER_DECLARATOR_NAMED
,
34859 /*ctor_dtor_or_conv_p=*/NULL
,
34860 /*parenthesized_p=*/NULL
,
34861 /*member_p=*/false,
34862 /*friend_p=*/false);
34863 attributes
= cp_parser_attributes_opt (parser
);
34864 asm_specification
= cp_parser_asm_specification_opt (parser
);
34866 if (declarator
== cp_error_declarator
)
34867 cp_parser_skip_to_end_of_statement (parser
);
34871 tree pushed_scope
, auto_node
;
34873 decl
= start_decl (declarator
, &type_specifiers
,
34874 SD_INITIALIZED
, attributes
,
34875 /*prefix_attributes=*/NULL_TREE
,
34878 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34879 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34881 if (cp_lexer_next_token_is (parser
->lexer
,
34883 error ("parenthesized initialization is not allowed in "
34884 "OpenMP %<for%> loop");
34886 /* Trigger an error. */
34887 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34889 init
= error_mark_node
;
34890 cp_parser_skip_to_end_of_statement (parser
);
34892 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34893 || type_dependent_expression_p (decl
)
34896 bool is_direct_init
, is_non_constant_init
;
34898 init
= cp_parser_initializer (parser
,
34900 &is_non_constant_init
);
34905 = do_auto_deduction (TREE_TYPE (decl
), init
,
34908 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34909 && !type_dependent_expression_p (decl
))
34913 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34915 LOOKUP_ONLYCONVERTING
);
34917 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34919 vec_safe_push (for_block
, this_pre_body
);
34924 init
= pop_stmt_list (this_pre_body
);
34925 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34927 tree_stmt_iterator i
= tsi_start (init
);
34928 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34929 while (!tsi_end_p (i
))
34931 tree t
= tsi_stmt (i
);
34932 if (TREE_CODE (t
) == DECL_EXPR
34933 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34936 vec_safe_push (for_block
, t
);
34941 if (tsi_one_before_end_p (i
))
34943 tree t
= tsi_stmt (i
);
34945 free_stmt_list (init
);
34950 this_pre_body
= NULL_TREE
;
34955 cp_lexer_consume_token (parser
->lexer
);
34956 init
= cp_parser_assignment_expression (parser
);
34959 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34960 init
= error_mark_node
;
34962 cp_finish_decl (decl
, NULL_TREE
,
34963 /*init_const_expr_p=*/false,
34965 LOOKUP_ONLYCONVERTING
);
34969 pop_scope (pushed_scope
);
34975 /* If parsing a type specifier sequence failed, then
34976 this MUST be a simple expression. */
34977 cp_parser_parse_tentatively (parser
);
34978 decl
= cp_parser_primary_expression (parser
, false, false,
34980 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
34981 if (!cp_parser_error_occurred (parser
)
34983 && (TREE_CODE (decl
) == COMPONENT_REF
34984 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
34986 cp_parser_abort_tentative_parse (parser
);
34987 cp_parser_parse_tentatively (parser
);
34988 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34989 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
34990 /*check_dependency_p=*/true,
34991 /*template_p=*/NULL
,
34992 /*declarator_p=*/false,
34993 /*optional_p=*/false);
34994 if (name
!= error_mark_node
34995 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
34997 decl
= cp_parser_lookup_name_simple (parser
, name
,
34999 if (TREE_CODE (decl
) == FIELD_DECL
)
35000 add_private_clause
= omp_privatize_field (decl
, false);
35002 cp_parser_abort_tentative_parse (parser
);
35003 cp_parser_parse_tentatively (parser
);
35004 decl
= cp_parser_primary_expression (parser
, false, false,
35007 if (!cp_parser_error_occurred (parser
)
35010 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35014 cp_parser_parse_definitely (parser
);
35015 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35016 rhs
= cp_parser_assignment_expression (parser
);
35018 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35021 tf_warning_or_error
));
35022 if (!add_private_clause
)
35023 add_private_clause
= decl
;
35028 cp_parser_abort_tentative_parse (parser
);
35029 init
= cp_parser_expression (parser
);
35032 if (TREE_CODE (init
) == MODIFY_EXPR
35033 || TREE_CODE (init
) == MODOP_EXPR
)
35034 real_decl
= TREE_OPERAND (init
, 0);
35038 return add_private_clause
;
35041 /* Parse the restricted form of the for statement allowed by OpenMP. */
35044 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35045 tree
*cclauses
, bool *if_p
)
35047 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35048 tree real_decl
, initv
, condv
, incrv
, declv
;
35049 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35050 location_t loc_first
;
35051 bool collapse_err
= false;
35052 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35053 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35054 auto_vec
<tree
, 4> orig_inits
;
35055 bool tiling
= false;
35057 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35058 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35059 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35060 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35063 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35065 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35066 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35069 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35072 if (ordered
&& ordered
< collapse
)
35074 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35075 "%<ordered%> clause parameter is less than %<collapse%>");
35076 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35077 = build_int_cst (NULL_TREE
, collapse
);
35078 ordered
= collapse
;
35082 for (tree
*pc
= &clauses
; *pc
; )
35083 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35085 error_at (OMP_CLAUSE_LOCATION (*pc
),
35086 "%<linear%> clause may not be specified together "
35087 "with %<ordered%> clause with a parameter");
35088 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35091 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35094 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35095 count
= ordered
? ordered
: collapse
;
35097 declv
= make_tree_vec (count
);
35098 initv
= make_tree_vec (count
);
35099 condv
= make_tree_vec (count
);
35100 incrv
= make_tree_vec (count
);
35102 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35104 for (i
= 0; i
< count
; i
++)
35106 int bracecount
= 0;
35107 tree add_private_clause
= NULL_TREE
;
35110 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35113 cp_parser_error (parser
, "for statement expected");
35116 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35118 matching_parens parens
;
35119 if (!parens
.require_open (parser
))
35122 init
= orig_init
= decl
= real_decl
= NULL
;
35123 this_pre_body
= push_stmt_list ();
35126 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35127 init
, orig_init
, decl
, real_decl
);
35129 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35132 this_pre_body
= pop_stmt_list (this_pre_body
);
35136 pre_body
= push_stmt_list ();
35138 add_stmt (this_pre_body
);
35139 pre_body
= pop_stmt_list (pre_body
);
35142 pre_body
= this_pre_body
;
35147 if (cclauses
!= NULL
35148 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35149 && real_decl
!= NULL_TREE
)
35152 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35153 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35154 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35156 error_at (loc
, "iteration variable %qD"
35157 " should not be firstprivate", real_decl
);
35158 *c
= OMP_CLAUSE_CHAIN (*c
);
35160 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35161 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35163 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35165 *c
= OMP_CLAUSE_CHAIN (*c
);
35166 if (code
== OMP_SIMD
)
35168 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35169 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35173 OMP_CLAUSE_CHAIN (l
) = clauses
;
35176 add_private_clause
= NULL_TREE
;
35180 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35181 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35182 add_private_clause
= NULL_TREE
;
35183 c
= &OMP_CLAUSE_CHAIN (*c
);
35187 if (add_private_clause
)
35190 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35192 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35193 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35194 && OMP_CLAUSE_DECL (c
) == decl
)
35196 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35197 && OMP_CLAUSE_DECL (c
) == decl
)
35198 error_at (loc
, "iteration variable %qD "
35199 "should not be firstprivate",
35201 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35202 && OMP_CLAUSE_DECL (c
) == decl
)
35203 error_at (loc
, "iteration variable %qD should not be reduction",
35208 if (code
!= OMP_SIMD
)
35209 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35210 else if (collapse
== 1)
35211 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35213 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35214 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35215 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35218 OMP_CLAUSE_CHAIN (c
) = clauses
;
35220 /* For linear, signal that we need to fill up
35221 the so far unknown linear step. */
35222 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35223 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35229 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35230 cond
= cp_parser_omp_for_cond (parser
, decl
);
35231 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35234 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35236 /* If decl is an iterator, preserve the operator on decl
35237 until finish_omp_for. */
35239 && ((processing_template_decl
35240 && (TREE_TYPE (real_decl
) == NULL_TREE
35241 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
35242 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35243 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35245 incr
= cp_parser_expression (parser
);
35246 if (!EXPR_HAS_LOCATION (incr
))
35247 protected_set_expr_location (incr
, input_location
);
35250 if (!parens
.require_close (parser
))
35251 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35252 /*or_comma=*/false,
35253 /*consume_paren=*/true);
35255 TREE_VEC_ELT (declv
, i
) = decl
;
35256 TREE_VEC_ELT (initv
, i
) = init
;
35257 TREE_VEC_ELT (condv
, i
) = cond
;
35258 TREE_VEC_ELT (incrv
, i
) = incr
;
35261 orig_inits
.safe_grow_cleared (i
+ 1);
35262 orig_inits
[i
] = orig_init
;
35265 if (i
== count
- 1)
35268 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35269 in between the collapsed for loops to be still considered perfectly
35270 nested. Hopefully the final version clarifies this.
35271 For now handle (multiple) {'s and empty statements. */
35272 cp_parser_parse_tentatively (parser
);
35275 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35277 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35279 cp_lexer_consume_token (parser
->lexer
);
35282 else if (bracecount
35283 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35284 cp_lexer_consume_token (parser
->lexer
);
35287 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35288 error_at (loc
, "not enough for loops to collapse");
35289 collapse_err
= true;
35290 cp_parser_abort_tentative_parse (parser
);
35298 cp_parser_parse_definitely (parser
);
35299 nbraces
+= bracecount
;
35306 /* Note that we saved the original contents of this flag when we entered
35307 the structured block, and so we don't need to re-save it here. */
35308 parser
->in_statement
= IN_OMP_FOR
;
35310 /* Note that the grammar doesn't call for a structured block here,
35311 though the loop as a whole is a structured block. */
35312 body
= push_stmt_list ();
35313 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35314 body
= pop_stmt_list (body
);
35316 if (declv
== NULL_TREE
)
35319 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35320 body
, pre_body
, &orig_inits
, clauses
);
35324 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35326 cp_lexer_consume_token (parser
->lexer
);
35329 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35330 cp_lexer_consume_token (parser
->lexer
);
35335 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35336 "collapsed loops not perfectly nested");
35338 collapse_err
= true;
35339 cp_parser_statement_seq_opt (parser
, NULL
);
35340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35345 while (!for_block
->is_empty ())
35347 tree t
= for_block
->pop ();
35348 if (TREE_CODE (t
) == STATEMENT_LIST
)
35349 add_stmt (pop_stmt_list (t
));
35353 release_tree_vector (for_block
);
35358 /* Helper function for OpenMP parsing, split clauses and call
35359 finish_omp_clauses on each of the set of clauses afterwards. */
35362 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35363 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35366 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35367 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35369 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35373 #pragma omp simd simd-clause[optseq] new-line
35376 #define OMP_SIMD_CLAUSE_MASK \
35377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35387 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35388 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35391 tree clauses
, sb
, ret
;
35393 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35395 strcat (p_name
, " simd");
35396 mask
|= OMP_SIMD_CLAUSE_MASK
;
35398 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35402 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35403 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35404 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35405 OMP_CLAUSE_ORDERED
);
35406 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35408 error_at (OMP_CLAUSE_LOCATION (c
),
35409 "%<ordered%> clause with parameter may not be specified "
35410 "on %qs construct", p_name
);
35411 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35415 sb
= begin_omp_structured_block ();
35416 save
= cp_parser_begin_omp_structured_block (parser
);
35418 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35420 cp_parser_end_omp_structured_block (parser
, save
);
35421 add_stmt (finish_omp_structured_block (sb
));
35427 #pragma omp for for-clause[optseq] new-line
35431 #pragma omp for simd for-simd-clause[optseq] new-line
35434 #define OMP_FOR_CLAUSE_MASK \
35435 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35446 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35447 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35450 tree clauses
, sb
, ret
;
35452 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35454 strcat (p_name
, " for");
35455 mask
|= OMP_FOR_CLAUSE_MASK
;
35456 /* parallel for{, simd} disallows nowait clause, but for
35457 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35458 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35459 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35460 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35461 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35462 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35464 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35466 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35467 const char *p
= IDENTIFIER_POINTER (id
);
35469 if (strcmp (p
, "simd") == 0)
35471 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35472 if (cclauses
== NULL
)
35473 cclauses
= cclauses_buf
;
35475 cp_lexer_consume_token (parser
->lexer
);
35476 if (!flag_openmp
) /* flag_openmp_simd */
35477 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35479 sb
= begin_omp_structured_block ();
35480 save
= cp_parser_begin_omp_structured_block (parser
);
35481 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35483 cp_parser_end_omp_structured_block (parser
, save
);
35484 tree body
= finish_omp_structured_block (sb
);
35487 ret
= make_node (OMP_FOR
);
35488 TREE_TYPE (ret
) = void_type_node
;
35489 OMP_FOR_BODY (ret
) = body
;
35490 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35491 SET_EXPR_LOCATION (ret
, loc
);
35496 if (!flag_openmp
) /* flag_openmp_simd */
35498 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35502 /* Composite distribute parallel for disallows linear clause. */
35503 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35504 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35506 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35510 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35511 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35514 sb
= begin_omp_structured_block ();
35515 save
= cp_parser_begin_omp_structured_block (parser
);
35517 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35519 cp_parser_end_omp_structured_block (parser
, save
);
35520 add_stmt (finish_omp_structured_block (sb
));
35526 # pragma omp master new-line
35527 structured-block */
35530 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35532 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35533 return c_finish_omp_master (input_location
,
35534 cp_parser_omp_structured_block (parser
, if_p
));
35538 # pragma omp ordered new-line
35542 # pragma omp ordered ordered-clauses new-line
35543 structured-block */
35545 #define OMP_ORDERED_CLAUSE_MASK \
35546 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35549 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35550 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35553 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35554 enum pragma_context context
, bool *if_p
)
35556 location_t loc
= pragma_tok
->location
;
35558 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35560 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35561 const char *p
= IDENTIFIER_POINTER (id
);
35563 if (strcmp (p
, "depend") == 0)
35565 if (!flag_openmp
) /* flag_openmp_simd */
35567 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35570 if (context
== pragma_stmt
)
35572 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35573 "%<depend%> clause may only be used in compound "
35575 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35579 = cp_parser_omp_all_clauses (parser
,
35580 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35581 "#pragma omp ordered", pragma_tok
);
35582 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35588 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35589 "#pragma omp ordered", pragma_tok
);
35591 if (!flag_openmp
/* flag_openmp_simd */
35592 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35595 c_finish_omp_ordered (loc
, clauses
,
35596 cp_parser_omp_structured_block (parser
, if_p
));
35603 { section-sequence }
35606 section-directive[opt] structured-block
35607 section-sequence section-directive structured-block */
35610 cp_parser_omp_sections_scope (cp_parser
*parser
)
35612 tree stmt
, substmt
;
35613 bool error_suppress
= false;
35616 matching_braces braces
;
35617 if (!braces
.require_open (parser
))
35620 stmt
= push_stmt_list ();
35622 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35623 != PRAGMA_OMP_SECTION
)
35625 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35626 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35627 add_stmt (substmt
);
35632 tok
= cp_lexer_peek_token (parser
->lexer
);
35633 if (tok
->type
== CPP_CLOSE_BRACE
)
35635 if (tok
->type
== CPP_EOF
)
35638 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35640 cp_lexer_consume_token (parser
->lexer
);
35641 cp_parser_require_pragma_eol (parser
, tok
);
35642 error_suppress
= false;
35644 else if (!error_suppress
)
35646 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35647 error_suppress
= true;
35650 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35651 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35652 add_stmt (substmt
);
35654 braces
.require_close (parser
);
35656 substmt
= pop_stmt_list (stmt
);
35658 stmt
= make_node (OMP_SECTIONS
);
35659 TREE_TYPE (stmt
) = void_type_node
;
35660 OMP_SECTIONS_BODY (stmt
) = substmt
;
35667 # pragma omp sections sections-clause[optseq] newline
35670 #define OMP_SECTIONS_CLAUSE_MASK \
35671 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35678 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35679 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35682 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35684 strcat (p_name
, " sections");
35685 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35687 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35689 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35693 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35694 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35697 ret
= cp_parser_omp_sections_scope (parser
);
35699 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35705 # pragma omp parallel parallel-clause[optseq] new-line
35707 # pragma omp parallel for parallel-for-clause[optseq] new-line
35709 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35713 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35714 structured-block */
35716 #define OMP_PARALLEL_CLAUSE_MASK \
35717 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35728 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35729 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35732 tree stmt
, clauses
, block
;
35734 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35736 strcat (p_name
, " parallel");
35737 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35738 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35739 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35740 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35741 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35743 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35745 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35746 if (cclauses
== NULL
)
35747 cclauses
= cclauses_buf
;
35749 cp_lexer_consume_token (parser
->lexer
);
35750 if (!flag_openmp
) /* flag_openmp_simd */
35751 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35753 block
= begin_omp_parallel ();
35754 save
= cp_parser_begin_omp_structured_block (parser
);
35755 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35757 cp_parser_end_omp_structured_block (parser
, save
);
35758 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35760 if (ret
== NULL_TREE
)
35762 OMP_PARALLEL_COMBINED (stmt
) = 1;
35765 /* When combined with distribute, parallel has to be followed by for.
35766 #pragma omp target parallel is allowed though. */
35768 && (mask
& (OMP_CLAUSE_MASK_1
35769 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35771 error_at (loc
, "expected %<for%> after %qs", p_name
);
35772 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35775 else if (!flag_openmp
) /* flag_openmp_simd */
35777 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35780 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35782 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35783 const char *p
= IDENTIFIER_POINTER (id
);
35784 if (strcmp (p
, "sections") == 0)
35786 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35787 cclauses
= cclauses_buf
;
35789 cp_lexer_consume_token (parser
->lexer
);
35790 block
= begin_omp_parallel ();
35791 save
= cp_parser_begin_omp_structured_block (parser
);
35792 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35793 cp_parser_end_omp_structured_block (parser
, save
);
35794 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35796 OMP_PARALLEL_COMBINED (stmt
) = 1;
35801 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35805 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35806 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35809 block
= begin_omp_parallel ();
35810 save
= cp_parser_begin_omp_structured_block (parser
);
35811 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35812 cp_parser_end_omp_structured_block (parser
, save
);
35813 stmt
= finish_omp_parallel (clauses
, block
);
35818 # pragma omp single single-clause[optseq] new-line
35819 structured-block */
35821 #define OMP_SINGLE_CLAUSE_MASK \
35822 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35828 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35830 tree stmt
= make_node (OMP_SINGLE
);
35831 TREE_TYPE (stmt
) = void_type_node
;
35833 OMP_SINGLE_CLAUSES (stmt
)
35834 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35835 "#pragma omp single", pragma_tok
);
35836 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35838 return add_stmt (stmt
);
35842 # pragma omp task task-clause[optseq] new-line
35843 structured-block */
35845 #define OMP_TASK_CLAUSE_MASK \
35846 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35858 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35860 tree clauses
, block
;
35863 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35864 "#pragma omp task", pragma_tok
);
35865 block
= begin_omp_task ();
35866 save
= cp_parser_begin_omp_structured_block (parser
);
35867 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35868 cp_parser_end_omp_structured_block (parser
, save
);
35869 return finish_omp_task (clauses
, block
);
35873 # pragma omp taskwait new-line */
35876 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35878 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35879 finish_omp_taskwait ();
35883 # pragma omp taskyield new-line */
35886 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35888 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35889 finish_omp_taskyield ();
35893 # pragma omp taskgroup new-line
35894 structured-block */
35897 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35899 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35900 return c_finish_omp_taskgroup (input_location
,
35901 cp_parser_omp_structured_block (parser
,
35907 # pragma omp threadprivate (variable-list) */
35910 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35914 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35915 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35917 finish_omp_threadprivate (vars
);
35921 # pragma omp cancel cancel-clause[optseq] new-line */
35923 #define OMP_CANCEL_CLAUSE_MASK \
35924 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35931 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35933 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35934 "#pragma omp cancel", pragma_tok
);
35935 finish_omp_cancel (clauses
);
35939 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35941 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35942 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35948 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35949 enum pragma_context context
)
35952 bool point_seen
= false;
35954 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35956 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35957 const char *p
= IDENTIFIER_POINTER (id
);
35959 if (strcmp (p
, "point") == 0)
35961 cp_lexer_consume_token (parser
->lexer
);
35967 cp_parser_error (parser
, "expected %<point%>");
35968 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35972 if (context
!= pragma_compound
)
35974 if (context
== pragma_stmt
)
35975 error_at (pragma_tok
->location
,
35976 "%<#pragma %s%> may only be used in compound statements",
35977 "omp cancellation point");
35979 cp_parser_error (parser
, "expected declaration specifiers");
35980 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35984 clauses
= cp_parser_omp_all_clauses (parser
,
35985 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
35986 "#pragma omp cancellation point",
35988 finish_omp_cancellation_point (clauses
);
35992 #pragma omp distribute distribute-clause[optseq] new-line
35995 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35996 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36003 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36004 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36007 tree clauses
, sb
, ret
;
36009 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36011 strcat (p_name
, " distribute");
36012 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36016 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36017 const char *p
= IDENTIFIER_POINTER (id
);
36019 bool parallel
= false;
36021 if (strcmp (p
, "simd") == 0)
36024 parallel
= strcmp (p
, "parallel") == 0;
36025 if (parallel
|| simd
)
36027 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36028 if (cclauses
== NULL
)
36029 cclauses
= cclauses_buf
;
36030 cp_lexer_consume_token (parser
->lexer
);
36031 if (!flag_openmp
) /* flag_openmp_simd */
36034 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36037 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36040 sb
= begin_omp_structured_block ();
36041 save
= cp_parser_begin_omp_structured_block (parser
);
36043 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36046 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36048 cp_parser_end_omp_structured_block (parser
, save
);
36049 tree body
= finish_omp_structured_block (sb
);
36052 ret
= make_node (OMP_DISTRIBUTE
);
36053 TREE_TYPE (ret
) = void_type_node
;
36054 OMP_FOR_BODY (ret
) = body
;
36055 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36056 SET_EXPR_LOCATION (ret
, loc
);
36061 if (!flag_openmp
) /* flag_openmp_simd */
36063 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36067 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36071 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36072 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36075 sb
= begin_omp_structured_block ();
36076 save
= cp_parser_begin_omp_structured_block (parser
);
36078 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36080 cp_parser_end_omp_structured_block (parser
, save
);
36081 add_stmt (finish_omp_structured_block (sb
));
36087 # pragma omp teams teams-clause[optseq] new-line
36088 structured-block */
36090 #define OMP_TEAMS_CLAUSE_MASK \
36091 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36100 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36101 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36104 tree clauses
, sb
, ret
;
36106 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36108 strcat (p_name
, " teams");
36109 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36111 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36113 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36114 const char *p
= IDENTIFIER_POINTER (id
);
36115 if (strcmp (p
, "distribute") == 0)
36117 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36118 if (cclauses
== NULL
)
36119 cclauses
= cclauses_buf
;
36121 cp_lexer_consume_token (parser
->lexer
);
36122 if (!flag_openmp
) /* flag_openmp_simd */
36123 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36125 sb
= begin_omp_structured_block ();
36126 save
= cp_parser_begin_omp_structured_block (parser
);
36127 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36129 cp_parser_end_omp_structured_block (parser
, save
);
36130 tree body
= finish_omp_structured_block (sb
);
36133 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36134 ret
= make_node (OMP_TEAMS
);
36135 TREE_TYPE (ret
) = void_type_node
;
36136 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36137 OMP_TEAMS_BODY (ret
) = body
;
36138 OMP_TEAMS_COMBINED (ret
) = 1;
36139 SET_EXPR_LOCATION (ret
, loc
);
36140 return add_stmt (ret
);
36143 if (!flag_openmp
) /* flag_openmp_simd */
36145 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36149 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36153 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36154 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36157 tree stmt
= make_node (OMP_TEAMS
);
36158 TREE_TYPE (stmt
) = void_type_node
;
36159 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36160 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36161 SET_EXPR_LOCATION (stmt
, loc
);
36163 return add_stmt (stmt
);
36167 # pragma omp target data target-data-clause[optseq] new-line
36168 structured-block */
36170 #define OMP_TARGET_DATA_CLAUSE_MASK \
36171 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36177 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36180 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36181 "#pragma omp target data", pragma_tok
);
36183 for (tree
*pc
= &clauses
; *pc
;)
36185 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36186 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36189 case GOMP_MAP_ALWAYS_TO
:
36190 case GOMP_MAP_FROM
:
36191 case GOMP_MAP_ALWAYS_FROM
:
36192 case GOMP_MAP_TOFROM
:
36193 case GOMP_MAP_ALWAYS_TOFROM
:
36194 case GOMP_MAP_ALLOC
:
36197 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36198 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36199 case GOMP_MAP_ALWAYS_POINTER
:
36203 error_at (OMP_CLAUSE_LOCATION (*pc
),
36204 "%<#pragma omp target data%> with map-type other "
36205 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36206 "on %<map%> clause");
36207 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36210 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36216 error_at (pragma_tok
->location
,
36217 "%<#pragma omp target data%> must contain at least "
36218 "one %<map%> clause");
36222 tree stmt
= make_node (OMP_TARGET_DATA
);
36223 TREE_TYPE (stmt
) = void_type_node
;
36224 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36226 keep_next_level (true);
36227 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36229 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36230 return add_stmt (stmt
);
36234 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36235 structured-block */
36237 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36238 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36245 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36246 enum pragma_context context
)
36248 bool data_seen
= false;
36249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36251 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36252 const char *p
= IDENTIFIER_POINTER (id
);
36254 if (strcmp (p
, "data") == 0)
36256 cp_lexer_consume_token (parser
->lexer
);
36262 cp_parser_error (parser
, "expected %<data%>");
36263 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36267 if (context
== pragma_stmt
)
36269 error_at (pragma_tok
->location
,
36270 "%<#pragma %s%> may only be used in compound statements",
36271 "omp target enter data");
36272 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36277 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36278 "#pragma omp target enter data", pragma_tok
);
36280 for (tree
*pc
= &clauses
; *pc
;)
36282 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36283 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36286 case GOMP_MAP_ALWAYS_TO
:
36287 case GOMP_MAP_ALLOC
:
36290 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36291 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36292 case GOMP_MAP_ALWAYS_POINTER
:
36296 error_at (OMP_CLAUSE_LOCATION (*pc
),
36297 "%<#pragma omp target enter data%> with map-type other "
36298 "than %<to%> or %<alloc%> on %<map%> clause");
36299 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36302 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36308 error_at (pragma_tok
->location
,
36309 "%<#pragma omp target enter data%> must contain at least "
36310 "one %<map%> clause");
36314 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36315 TREE_TYPE (stmt
) = void_type_node
;
36316 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36317 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36318 return add_stmt (stmt
);
36322 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36323 structured-block */
36325 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36326 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36333 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36334 enum pragma_context context
)
36336 bool data_seen
= false;
36337 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36339 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36340 const char *p
= IDENTIFIER_POINTER (id
);
36342 if (strcmp (p
, "data") == 0)
36344 cp_lexer_consume_token (parser
->lexer
);
36350 cp_parser_error (parser
, "expected %<data%>");
36351 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36355 if (context
== pragma_stmt
)
36357 error_at (pragma_tok
->location
,
36358 "%<#pragma %s%> may only be used in compound statements",
36359 "omp target exit data");
36360 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36365 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36366 "#pragma omp target exit data", pragma_tok
);
36368 for (tree
*pc
= &clauses
; *pc
;)
36370 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36371 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36373 case GOMP_MAP_FROM
:
36374 case GOMP_MAP_ALWAYS_FROM
:
36375 case GOMP_MAP_RELEASE
:
36376 case GOMP_MAP_DELETE
:
36379 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36380 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36381 case GOMP_MAP_ALWAYS_POINTER
:
36385 error_at (OMP_CLAUSE_LOCATION (*pc
),
36386 "%<#pragma omp target exit data%> with map-type other "
36387 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36389 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36392 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36398 error_at (pragma_tok
->location
,
36399 "%<#pragma omp target exit data%> must contain at least "
36400 "one %<map%> clause");
36404 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36405 TREE_TYPE (stmt
) = void_type_node
;
36406 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36407 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36408 return add_stmt (stmt
);
36412 # pragma omp target update target-update-clause[optseq] new-line */
36414 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36415 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36423 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36424 enum pragma_context context
)
36426 if (context
== pragma_stmt
)
36428 error_at (pragma_tok
->location
,
36429 "%<#pragma %s%> may only be used in compound statements",
36430 "omp target update");
36431 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36436 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36437 "#pragma omp target update", pragma_tok
);
36438 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36439 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36441 error_at (pragma_tok
->location
,
36442 "%<#pragma omp target update%> must contain at least one "
36443 "%<from%> or %<to%> clauses");
36447 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36448 TREE_TYPE (stmt
) = void_type_node
;
36449 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36450 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36456 # pragma omp target target-clause[optseq] new-line
36457 structured-block */
36459 #define OMP_TARGET_CLAUSE_MASK \
36460 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36471 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36472 enum pragma_context context
, bool *if_p
)
36474 tree
*pc
= NULL
, stmt
;
36476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36478 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36479 const char *p
= IDENTIFIER_POINTER (id
);
36480 enum tree_code ccode
= ERROR_MARK
;
36482 if (strcmp (p
, "teams") == 0)
36484 else if (strcmp (p
, "parallel") == 0)
36485 ccode
= OMP_PARALLEL
;
36486 else if (strcmp (p
, "simd") == 0)
36488 if (ccode
!= ERROR_MARK
)
36490 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36491 char p_name
[sizeof ("#pragma omp target teams distribute "
36492 "parallel for simd")];
36494 cp_lexer_consume_token (parser
->lexer
);
36495 strcpy (p_name
, "#pragma omp target");
36496 if (!flag_openmp
) /* flag_openmp_simd */
36502 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36503 OMP_TARGET_CLAUSE_MASK
,
36507 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36508 OMP_TARGET_CLAUSE_MASK
,
36512 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36513 OMP_TARGET_CLAUSE_MASK
,
36517 gcc_unreachable ();
36519 return stmt
!= NULL_TREE
;
36521 keep_next_level (true);
36522 tree sb
= begin_omp_structured_block (), ret
;
36523 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36527 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36528 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36532 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36533 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36537 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36538 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36542 gcc_unreachable ();
36544 cp_parser_end_omp_structured_block (parser
, save
);
36545 tree body
= finish_omp_structured_block (sb
);
36546 if (ret
== NULL_TREE
)
36548 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36550 /* For combined target teams, ensure the num_teams and
36551 thread_limit clause expressions are evaluated on the host,
36552 before entering the target construct. */
36554 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36555 c
; c
= OMP_CLAUSE_CHAIN (c
))
36556 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36557 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36558 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36560 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36561 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36562 if (expr
== error_mark_node
)
36564 tree tmp
= TARGET_EXPR_SLOT (expr
);
36566 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36567 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36568 OMP_CLAUSE_FIRSTPRIVATE
);
36569 OMP_CLAUSE_DECL (tc
) = tmp
;
36570 OMP_CLAUSE_CHAIN (tc
)
36571 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36572 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36575 tree stmt
= make_node (OMP_TARGET
);
36576 TREE_TYPE (stmt
) = void_type_node
;
36577 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36578 OMP_TARGET_BODY (stmt
) = body
;
36579 OMP_TARGET_COMBINED (stmt
) = 1;
36580 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36582 pc
= &OMP_TARGET_CLAUSES (stmt
);
36583 goto check_clauses
;
36585 else if (!flag_openmp
) /* flag_openmp_simd */
36587 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36590 else if (strcmp (p
, "data") == 0)
36592 cp_lexer_consume_token (parser
->lexer
);
36593 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36596 else if (strcmp (p
, "enter") == 0)
36598 cp_lexer_consume_token (parser
->lexer
);
36599 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36602 else if (strcmp (p
, "exit") == 0)
36604 cp_lexer_consume_token (parser
->lexer
);
36605 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36608 else if (strcmp (p
, "update") == 0)
36610 cp_lexer_consume_token (parser
->lexer
);
36611 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36614 if (!flag_openmp
) /* flag_openmp_simd */
36616 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36620 stmt
= make_node (OMP_TARGET
);
36621 TREE_TYPE (stmt
) = void_type_node
;
36623 OMP_TARGET_CLAUSES (stmt
)
36624 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36625 "#pragma omp target", pragma_tok
);
36626 pc
= &OMP_TARGET_CLAUSES (stmt
);
36627 keep_next_level (true);
36628 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36630 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36636 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36637 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36640 case GOMP_MAP_ALWAYS_TO
:
36641 case GOMP_MAP_FROM
:
36642 case GOMP_MAP_ALWAYS_FROM
:
36643 case GOMP_MAP_TOFROM
:
36644 case GOMP_MAP_ALWAYS_TOFROM
:
36645 case GOMP_MAP_ALLOC
:
36646 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36647 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36648 case GOMP_MAP_ALWAYS_POINTER
:
36651 error_at (OMP_CLAUSE_LOCATION (*pc
),
36652 "%<#pragma omp target%> with map-type other "
36653 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36654 "on %<map%> clause");
36655 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36658 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36664 # pragma acc cache (variable-list) new-line
36668 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36670 tree stmt
, clauses
;
36672 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36673 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36675 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36677 stmt
= make_node (OACC_CACHE
);
36678 TREE_TYPE (stmt
) = void_type_node
;
36679 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36680 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36687 # pragma acc data oacc-data-clause[optseq] new-line
36688 structured-block */
36690 #define OACC_DATA_CLAUSE_MASK \
36691 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36704 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36706 tree stmt
, clauses
, block
;
36709 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36710 "#pragma acc data", pragma_tok
);
36712 block
= begin_omp_parallel ();
36713 save
= cp_parser_begin_omp_structured_block (parser
);
36714 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36715 cp_parser_end_omp_structured_block (parser
, save
);
36716 stmt
= finish_oacc_data (clauses
, block
);
36721 # pragma acc host_data <clauses> new-line
36722 structured-block */
36724 #define OACC_HOST_DATA_CLAUSE_MASK \
36725 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36728 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36730 tree stmt
, clauses
, block
;
36733 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36734 "#pragma acc host_data", pragma_tok
);
36736 block
= begin_omp_parallel ();
36737 save
= cp_parser_begin_omp_structured_block (parser
);
36738 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36739 cp_parser_end_omp_structured_block (parser
, save
);
36740 stmt
= finish_oacc_host_data (clauses
, block
);
36745 # pragma acc declare oacc-data-clause[optseq] new-line
36748 #define OACC_DECLARE_CLAUSE_MASK \
36749 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36763 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36765 tree clauses
, stmt
;
36766 bool error
= false;
36768 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36769 "#pragma acc declare", pragma_tok
, true);
36772 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36774 error_at (pragma_tok
->location
,
36775 "no valid clauses specified in %<#pragma acc declare%>");
36779 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36781 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36782 tree decl
= OMP_CLAUSE_DECL (t
);
36783 if (!DECL_P (decl
))
36785 error_at (loc
, "array section in %<#pragma acc declare%>");
36789 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36790 switch (OMP_CLAUSE_MAP_KIND (t
))
36792 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36793 case GOMP_MAP_FORCE_ALLOC
:
36794 case GOMP_MAP_FORCE_TO
:
36795 case GOMP_MAP_FORCE_DEVICEPTR
:
36796 case GOMP_MAP_DEVICE_RESIDENT
:
36799 case GOMP_MAP_LINK
:
36800 if (!global_bindings_p ()
36801 && (TREE_STATIC (decl
)
36802 || !DECL_EXTERNAL (decl
)))
36805 "%qD must be a global variable in "
36806 "%<#pragma acc declare link%>",
36814 if (global_bindings_p ())
36816 error_at (loc
, "invalid OpenACC clause at file scope");
36820 if (DECL_EXTERNAL (decl
))
36823 "invalid use of %<extern%> variable %qD "
36824 "in %<#pragma acc declare%>", decl
);
36828 else if (TREE_PUBLIC (decl
))
36831 "invalid use of %<global%> variable %qD "
36832 "in %<#pragma acc declare%>", decl
);
36839 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36840 || lookup_attribute ("omp declare target link",
36841 DECL_ATTRIBUTES (decl
)))
36843 error_at (loc
, "variable %qD used more than once with "
36844 "%<#pragma acc declare%>", decl
);
36853 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36854 id
= get_identifier ("omp declare target link");
36856 id
= get_identifier ("omp declare target");
36858 DECL_ATTRIBUTES (decl
)
36859 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36860 if (global_bindings_p ())
36862 symtab_node
*node
= symtab_node::get (decl
);
36865 node
->offloadable
= 1;
36866 if (ENABLE_OFFLOADING
)
36868 g
->have_offload
= true;
36869 if (is_a
<varpool_node
*> (node
))
36870 vec_safe_push (offload_vars
, decl
);
36877 if (error
|| global_bindings_p ())
36880 stmt
= make_node (OACC_DECLARE
);
36881 TREE_TYPE (stmt
) = void_type_node
;
36882 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36883 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36891 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36895 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36897 LOC is the location of the #pragma token.
36900 #define OACC_ENTER_DATA_CLAUSE_MASK \
36901 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36909 #define OACC_EXIT_DATA_CLAUSE_MASK \
36910 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36917 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36920 location_t loc
= pragma_tok
->location
;
36921 tree stmt
, clauses
;
36922 const char *p
= "";
36924 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36925 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36927 if (strcmp (p
, "data") != 0)
36929 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
36930 enter
? "enter" : "exit");
36931 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36935 cp_lexer_consume_token (parser
->lexer
);
36938 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36939 "#pragma acc enter data", pragma_tok
);
36941 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36942 "#pragma acc exit data", pragma_tok
);
36944 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36946 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36947 enter
? "enter" : "exit");
36951 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36952 TREE_TYPE (stmt
) = void_type_node
;
36953 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36954 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36960 # pragma acc loop oacc-loop-clause[optseq] new-line
36961 structured-block */
36963 #define OACC_LOOP_CLAUSE_MASK \
36964 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36976 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
36977 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
36979 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
36981 strcat (p_name
, " loop");
36982 mask
|= OACC_LOOP_CLAUSE_MASK
;
36984 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36988 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
36990 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
36992 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36995 tree block
= begin_omp_structured_block ();
36996 int save
= cp_parser_begin_omp_structured_block (parser
);
36997 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
36998 cp_parser_end_omp_structured_block (parser
, save
);
36999 add_stmt (finish_omp_structured_block (block
));
37005 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37010 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37014 #define OACC_KERNELS_CLAUSE_MASK \
37015 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37033 #define OACC_PARALLEL_CLAUSE_MASK \
37034 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37056 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37057 char *p_name
, bool *if_p
)
37059 omp_clause_mask mask
;
37060 enum tree_code code
;
37061 switch (cp_parser_pragma_kind (pragma_tok
))
37063 case PRAGMA_OACC_KERNELS
:
37064 strcat (p_name
, " kernels");
37065 mask
= OACC_KERNELS_CLAUSE_MASK
;
37066 code
= OACC_KERNELS
;
37068 case PRAGMA_OACC_PARALLEL
:
37069 strcat (p_name
, " parallel");
37070 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37071 code
= OACC_PARALLEL
;
37074 gcc_unreachable ();
37077 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37080 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37081 if (strcmp (p
, "loop") == 0)
37083 cp_lexer_consume_token (parser
->lexer
);
37084 tree block
= begin_omp_parallel ();
37086 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37088 return finish_omp_construct (code
, block
, clauses
);
37092 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37094 tree block
= begin_omp_parallel ();
37095 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37096 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37097 cp_parser_end_omp_structured_block (parser
, save
);
37098 return finish_omp_construct (code
, block
, clauses
);
37102 # pragma acc update oacc-update-clause[optseq] new-line
37105 #define OACC_UPDATE_CLAUSE_MASK \
37106 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37114 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37116 tree stmt
, clauses
;
37118 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37119 "#pragma acc update", pragma_tok
);
37121 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37123 error_at (pragma_tok
->location
,
37124 "%<#pragma acc update%> must contain at least one "
37125 "%<device%> or %<host%> or %<self%> clause");
37129 stmt
= make_node (OACC_UPDATE
);
37130 TREE_TYPE (stmt
) = void_type_node
;
37131 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37132 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37138 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37140 LOC is the location of the #pragma token.
37143 #define OACC_WAIT_CLAUSE_MASK \
37144 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37147 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37149 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37150 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37152 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37153 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37155 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37156 "#pragma acc wait", pragma_tok
);
37158 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37159 stmt
= finish_expr_stmt (stmt
);
37165 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37167 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37168 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37176 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37177 enum pragma_context context
)
37179 bool first_p
= parser
->omp_declare_simd
== NULL
;
37180 cp_omp_declare_simd_data data
;
37183 data
.error_seen
= false;
37184 data
.fndecl_seen
= false;
37185 data
.tokens
= vNULL
;
37186 data
.clauses
= NULL_TREE
;
37187 /* It is safe to take the address of a local variable; it will only be
37188 used while this scope is live. */
37189 parser
->omp_declare_simd
= &data
;
37192 /* Store away all pragma tokens. */
37193 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37194 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37195 cp_lexer_consume_token (parser
->lexer
);
37196 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37197 parser
->omp_declare_simd
->error_seen
= true;
37198 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37199 struct cp_token_cache
*cp
37200 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37201 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37205 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37206 cp_parser_pragma (parser
, context
, NULL
);
37209 case pragma_external
:
37210 cp_parser_declaration (parser
);
37212 case pragma_member
:
37213 cp_parser_member_declaration (parser
);
37215 case pragma_objc_icode
:
37216 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37219 cp_parser_declaration_statement (parser
);
37222 if (parser
->omp_declare_simd
37223 && !parser
->omp_declare_simd
->error_seen
37224 && !parser
->omp_declare_simd
->fndecl_seen
)
37225 error_at (pragma_tok
->location
,
37226 "%<#pragma omp declare simd%> not immediately followed by "
37227 "function declaration or definition");
37228 data
.tokens
.release ();
37229 parser
->omp_declare_simd
= NULL
;
37233 /* Finalize #pragma omp declare simd clauses after direct declarator has
37234 been parsed, and put that into "omp declare simd" attribute. */
37237 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37239 struct cp_token_cache
*ce
;
37240 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37243 if (!data
->error_seen
&& data
->fndecl_seen
)
37245 error ("%<#pragma omp declare simd%> not immediately followed by "
37246 "a single function declaration or definition");
37247 data
->error_seen
= true;
37249 if (data
->error_seen
)
37252 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37256 cp_parser_push_lexer_for_tokens (parser
, ce
);
37257 parser
->lexer
->in_pragma
= true;
37258 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37259 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37260 cp_lexer_consume_token (parser
->lexer
);
37261 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37262 "#pragma omp declare simd", pragma_tok
);
37263 cp_parser_pop_lexer (parser
);
37265 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37266 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37267 TREE_CHAIN (c
) = attrs
;
37268 if (processing_template_decl
)
37269 ATTR_IS_DEPENDENT (c
) = 1;
37273 data
->fndecl_seen
= true;
37279 # pragma omp declare target new-line
37280 declarations and definitions
37281 # pragma omp end declare target new-line
37284 # pragma omp declare target ( extended-list ) new-line
37286 # pragma omp declare target declare-target-clauses[seq] new-line */
37288 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37289 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37293 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37295 tree clauses
= NULL_TREE
;
37296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37298 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37299 "#pragma omp declare target", pragma_tok
);
37300 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37302 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37304 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37305 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37309 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37310 scope_chain
->omp_declare_target_attribute
++;
37313 if (scope_chain
->omp_declare_target_attribute
)
37314 error_at (pragma_tok
->location
,
37315 "%<#pragma omp declare target%> with clauses in between "
37316 "%<#pragma omp declare target%> without clauses and "
37317 "%<#pragma omp end declare target%>");
37318 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37320 tree t
= OMP_CLAUSE_DECL (c
), id
;
37321 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37322 tree at2
= lookup_attribute ("omp declare target link",
37323 DECL_ATTRIBUTES (t
));
37324 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37326 id
= get_identifier ("omp declare target link");
37327 std::swap (at1
, at2
);
37330 id
= get_identifier ("omp declare target");
37333 error_at (OMP_CLAUSE_LOCATION (c
),
37334 "%qD specified both in declare target %<link%> and %<to%>"
37340 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37341 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37344 symtab_node
*node
= symtab_node::get (t
);
37347 node
->offloadable
= 1;
37348 if (ENABLE_OFFLOADING
)
37350 g
->have_offload
= true;
37351 if (is_a
<varpool_node
*> (node
))
37352 vec_safe_push (offload_vars
, t
);
37360 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37362 const char *p
= "";
37363 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37365 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37366 p
= IDENTIFIER_POINTER (id
);
37368 if (strcmp (p
, "declare") == 0)
37370 cp_lexer_consume_token (parser
->lexer
);
37372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37374 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37375 p
= IDENTIFIER_POINTER (id
);
37377 if (strcmp (p
, "target") == 0)
37378 cp_lexer_consume_token (parser
->lexer
);
37381 cp_parser_error (parser
, "expected %<target%>");
37382 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37388 cp_parser_error (parser
, "expected %<declare%>");
37389 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37392 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37393 if (!scope_chain
->omp_declare_target_attribute
)
37394 error_at (pragma_tok
->location
,
37395 "%<#pragma omp end declare target%> without corresponding "
37396 "%<#pragma omp declare target%>");
37398 scope_chain
->omp_declare_target_attribute
--;
37401 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37402 expression and optional initializer clause of
37403 #pragma omp declare reduction. We store the expression(s) as
37404 either 3, 6 or 7 special statements inside of the artificial function's
37405 body. The first two statements are DECL_EXPRs for the artificial
37406 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37407 expression that uses those variables.
37408 If there was any INITIALIZER clause, this is followed by further statements,
37409 the fourth and fifth statements are DECL_EXPRs for the artificial
37410 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37411 constructor variant (first token after open paren is not omp_priv),
37412 then the sixth statement is a statement with the function call expression
37413 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37414 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37415 to initialize the OMP_PRIV artificial variable and there is seventh
37416 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37419 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37421 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37422 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
37423 type
= TREE_TYPE (type
);
37424 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37425 DECL_ARTIFICIAL (omp_out
) = 1;
37426 pushdecl (omp_out
);
37427 add_decl_expr (omp_out
);
37428 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37429 DECL_ARTIFICIAL (omp_in
) = 1;
37431 add_decl_expr (omp_in
);
37433 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37435 keep_next_level (true);
37436 tree block
= begin_omp_structured_block ();
37437 combiner
= cp_parser_expression (parser
);
37438 finish_expr_stmt (combiner
);
37439 block
= finish_omp_structured_block (block
);
37442 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37445 const char *p
= "";
37446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37448 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37449 p
= IDENTIFIER_POINTER (id
);
37452 if (strcmp (p
, "initializer") == 0)
37454 cp_lexer_consume_token (parser
->lexer
);
37455 matching_parens parens
;
37456 if (!parens
.require_open (parser
))
37460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37462 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37463 p
= IDENTIFIER_POINTER (id
);
37466 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37467 DECL_ARTIFICIAL (omp_priv
) = 1;
37468 pushdecl (omp_priv
);
37469 add_decl_expr (omp_priv
);
37470 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37471 DECL_ARTIFICIAL (omp_orig
) = 1;
37472 pushdecl (omp_orig
);
37473 add_decl_expr (omp_orig
);
37475 keep_next_level (true);
37476 block
= begin_omp_structured_block ();
37479 if (strcmp (p
, "omp_priv") == 0)
37481 bool is_direct_init
, is_non_constant_init
;
37483 cp_lexer_consume_token (parser
->lexer
);
37484 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37485 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37486 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37487 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37489 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37490 == CPP_CLOSE_PAREN
))
37492 finish_omp_structured_block (block
);
37493 error ("invalid initializer clause");
37496 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37497 &is_non_constant_init
);
37498 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37499 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37503 cp_parser_parse_tentatively (parser
);
37504 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37505 /*check_dependency_p=*/true,
37506 /*template_p=*/NULL
,
37507 /*declarator_p=*/false,
37508 /*optional_p=*/false);
37509 vec
<tree
, va_gc
> *args
;
37510 if (fn_name
== error_mark_node
37511 || cp_parser_error_occurred (parser
)
37512 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37513 || ((args
= cp_parser_parenthesized_expression_list
37514 (parser
, non_attr
, /*cast_p=*/false,
37515 /*allow_expansion_p=*/true,
37516 /*non_constant_p=*/NULL
)),
37517 cp_parser_error_occurred (parser
)))
37519 finish_omp_structured_block (block
);
37520 cp_parser_abort_tentative_parse (parser
);
37521 cp_parser_error (parser
, "expected id-expression (arguments)");
37526 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37527 if (arg
== omp_priv
37528 || (TREE_CODE (arg
) == ADDR_EXPR
37529 && TREE_OPERAND (arg
, 0) == omp_priv
))
37531 cp_parser_abort_tentative_parse (parser
);
37532 if (arg
== NULL_TREE
)
37533 error ("one of the initializer call arguments should be %<omp_priv%>"
37534 " or %<&omp_priv%>");
37535 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37537 finish_expr_stmt (initializer
);
37540 block
= finish_omp_structured_block (block
);
37541 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37545 add_decl_expr (omp_orig
);
37547 if (!parens
.require_close (parser
))
37551 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37552 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37559 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37560 initializer-clause[opt] new-line
37562 initializer-clause:
37563 initializer (omp_priv initializer)
37564 initializer (function-name (argument-list)) */
37567 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37568 enum pragma_context
)
37570 auto_vec
<tree
> types
;
37571 enum tree_code reduc_code
= ERROR_MARK
;
37572 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37574 cp_token
*first_token
;
37575 cp_token_cache
*cp
;
37579 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37580 p
= obstack_alloc (&declarator_obstack
, 0);
37582 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37585 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37588 reduc_code
= PLUS_EXPR
;
37591 reduc_code
= MULT_EXPR
;
37594 reduc_code
= MINUS_EXPR
;
37597 reduc_code
= BIT_AND_EXPR
;
37600 reduc_code
= BIT_XOR_EXPR
;
37603 reduc_code
= BIT_IOR_EXPR
;
37606 reduc_code
= TRUTH_ANDIF_EXPR
;
37609 reduc_code
= TRUTH_ORIF_EXPR
;
37612 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37615 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37616 "%<|%>, %<&&%>, %<||%> or identifier");
37620 if (reduc_code
!= ERROR_MARK
)
37621 cp_lexer_consume_token (parser
->lexer
);
37623 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37624 if (reduc_id
== error_mark_node
)
37627 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37630 /* Types may not be defined in declare reduction type list. */
37631 const char *saved_message
;
37632 saved_message
= parser
->type_definition_forbidden_message
;
37633 parser
->type_definition_forbidden_message
37634 = G_("types may not be defined in declare reduction type list");
37635 bool saved_colon_corrects_to_scope_p
;
37636 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37637 parser
->colon_corrects_to_scope_p
= false;
37638 bool saved_colon_doesnt_start_class_def_p
;
37639 saved_colon_doesnt_start_class_def_p
37640 = parser
->colon_doesnt_start_class_def_p
;
37641 parser
->colon_doesnt_start_class_def_p
= true;
37645 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37646 type
= cp_parser_type_id (parser
);
37647 if (type
== error_mark_node
)
37649 else if (ARITHMETIC_TYPE_P (type
)
37650 && (orig_reduc_id
== NULL_TREE
37651 || (TREE_CODE (type
) != COMPLEX_TYPE
37652 && (id_equal (orig_reduc_id
, "min")
37653 || id_equal (orig_reduc_id
, "max")))))
37654 error_at (loc
, "predeclared arithmetic type %qT in "
37655 "%<#pragma omp declare reduction%>", type
);
37656 else if (TREE_CODE (type
) == FUNCTION_TYPE
37657 || TREE_CODE (type
) == METHOD_TYPE
37658 || TREE_CODE (type
) == ARRAY_TYPE
)
37659 error_at (loc
, "function or array type %qT in "
37660 "%<#pragma omp declare reduction%>", type
);
37661 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37662 error_at (loc
, "reference type %qT in "
37663 "%<#pragma omp declare reduction%>", type
);
37664 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37665 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37666 "%<#pragma omp declare reduction%>", type
);
37668 types
.safe_push (type
);
37670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37671 cp_lexer_consume_token (parser
->lexer
);
37676 /* Restore the saved message. */
37677 parser
->type_definition_forbidden_message
= saved_message
;
37678 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37679 parser
->colon_doesnt_start_class_def_p
37680 = saved_colon_doesnt_start_class_def_p
;
37682 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37683 || types
.is_empty ())
37686 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37690 first_token
= cp_lexer_peek_token (parser
->lexer
);
37693 FOR_EACH_VEC_ELT (types
, i
, type
)
37696 = build_function_type_list (void_type_node
,
37697 cp_build_reference_type (type
, false),
37699 tree this_reduc_id
= reduc_id
;
37700 if (!dependent_type_p (type
))
37701 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37702 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37703 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37704 DECL_ARTIFICIAL (fndecl
) = 1;
37705 DECL_EXTERNAL (fndecl
) = 1;
37706 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37707 DECL_IGNORED_P (fndecl
) = 1;
37708 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37709 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37710 DECL_ATTRIBUTES (fndecl
)
37711 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37712 DECL_ATTRIBUTES (fndecl
));
37713 if (processing_template_decl
)
37714 fndecl
= push_template_decl (fndecl
);
37715 bool block_scope
= false;
37716 tree block
= NULL_TREE
;
37717 if (current_function_decl
)
37719 block_scope
= true;
37720 DECL_CONTEXT (fndecl
) = global_namespace
;
37721 if (!processing_template_decl
)
37724 else if (current_class_type
)
37728 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37729 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37730 cp_lexer_consume_token (parser
->lexer
);
37731 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37733 cp
= cp_token_cache_new (first_token
,
37734 cp_lexer_peek_nth_token (parser
->lexer
,
37737 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37738 finish_member_declaration (fndecl
);
37739 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37740 DECL_PENDING_INLINE_P (fndecl
) = 1;
37741 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37746 DECL_CONTEXT (fndecl
) = current_namespace
;
37750 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37752 block
= begin_omp_structured_block ();
37755 cp_parser_push_lexer_for_tokens (parser
, cp
);
37756 parser
->lexer
->in_pragma
= true;
37758 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37761 finish_function (/*inline_p=*/false);
37763 DECL_CONTEXT (fndecl
) = current_function_decl
;
37765 cp_parser_pop_lexer (parser
);
37769 cp_parser_pop_lexer (parser
);
37771 finish_function (/*inline_p=*/false);
37774 DECL_CONTEXT (fndecl
) = current_function_decl
;
37775 block
= finish_omp_structured_block (block
);
37776 if (TREE_CODE (block
) == BIND_EXPR
)
37777 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37778 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37779 DECL_SAVED_TREE (fndecl
) = block
;
37780 if (processing_template_decl
)
37781 add_decl_expr (fndecl
);
37783 cp_check_omp_declare_reduction (fndecl
);
37784 if (cp
== NULL
&& types
.length () > 1)
37785 cp
= cp_token_cache_new (first_token
,
37786 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37787 if (errs
!= errorcount
)
37791 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37794 /* Free any declarators allocated. */
37795 obstack_free (&declarator_obstack
, p
);
37799 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37800 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37801 initializer-clause[opt] new-line
37802 #pragma omp declare target new-line */
37805 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37806 enum pragma_context context
)
37808 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37810 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37811 const char *p
= IDENTIFIER_POINTER (id
);
37813 if (strcmp (p
, "simd") == 0)
37815 cp_lexer_consume_token (parser
->lexer
);
37816 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37820 cp_ensure_no_omp_declare_simd (parser
);
37821 if (strcmp (p
, "reduction") == 0)
37823 cp_lexer_consume_token (parser
->lexer
);
37824 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37828 if (!flag_openmp
) /* flag_openmp_simd */
37830 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37833 if (strcmp (p
, "target") == 0)
37835 cp_lexer_consume_token (parser
->lexer
);
37836 cp_parser_omp_declare_target (parser
, pragma_tok
);
37840 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37842 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37847 #pragma omp taskloop taskloop-clause[optseq] new-line
37850 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37853 #define OMP_TASKLOOP_CLAUSE_MASK \
37854 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37870 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37871 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37874 tree clauses
, sb
, ret
;
37876 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37878 strcat (p_name
, " taskloop");
37879 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37881 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37883 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37884 const char *p
= IDENTIFIER_POINTER (id
);
37886 if (strcmp (p
, "simd") == 0)
37888 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37889 if (cclauses
== NULL
)
37890 cclauses
= cclauses_buf
;
37892 cp_lexer_consume_token (parser
->lexer
);
37893 if (!flag_openmp
) /* flag_openmp_simd */
37894 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37896 sb
= begin_omp_structured_block ();
37897 save
= cp_parser_begin_omp_structured_block (parser
);
37898 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37900 cp_parser_end_omp_structured_block (parser
, save
);
37901 tree body
= finish_omp_structured_block (sb
);
37904 ret
= make_node (OMP_TASKLOOP
);
37905 TREE_TYPE (ret
) = void_type_node
;
37906 OMP_FOR_BODY (ret
) = body
;
37907 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37908 SET_EXPR_LOCATION (ret
, loc
);
37913 if (!flag_openmp
) /* flag_openmp_simd */
37915 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37919 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37923 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37924 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37927 sb
= begin_omp_structured_block ();
37928 save
= cp_parser_begin_omp_structured_block (parser
);
37930 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37933 cp_parser_end_omp_structured_block (parser
, save
);
37934 add_stmt (finish_omp_structured_block (sb
));
37941 # pragma acc routine oacc-routine-clause[optseq] new-line
37942 function-definition
37944 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37947 #define OACC_ROUTINE_CLAUSE_MASK \
37948 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37954 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37955 component, which must resolve to a declared namespace-scope
37956 function. The clauses are either processed directly (for a named
37957 function), or defered until the immediatley following declaration
37961 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
37962 enum pragma_context context
)
37964 gcc_checking_assert (context
== pragma_external
);
37965 /* The checking for "another pragma following this one" in the "no optional
37966 '( name )'" case makes sure that we dont re-enter. */
37967 gcc_checking_assert (parser
->oacc_routine
== NULL
);
37969 cp_oacc_routine_data data
;
37970 data
.error_seen
= false;
37971 data
.fndecl_seen
= false;
37972 data
.tokens
= vNULL
;
37973 data
.clauses
= NULL_TREE
;
37974 data
.loc
= pragma_tok
->location
;
37975 /* It is safe to take the address of a local variable; it will only be
37976 used while this scope is live. */
37977 parser
->oacc_routine
= &data
;
37979 /* Look for optional '( name )'. */
37980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37982 matching_parens parens
;
37983 parens
.consume_open (parser
); /* '(' */
37985 /* We parse the name as an id-expression. If it resolves to
37986 anything other than a non-overloaded function at namespace
37987 scope, it's an error. */
37988 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37989 tree name
= cp_parser_id_expression (parser
,
37990 /*template_keyword_p=*/false,
37991 /*check_dependency_p=*/false,
37992 /*template_p=*/NULL
,
37993 /*declarator_p=*/false,
37994 /*optional_p=*/false);
37995 tree decl
= (identifier_p (name
)
37996 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
37998 if (name
!= error_mark_node
&& decl
== error_mark_node
)
37999 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38001 if (decl
== error_mark_node
38002 || !parens
.require_close (parser
))
38004 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38005 parser
->oacc_routine
= NULL
;
38010 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38011 "#pragma acc routine",
38012 cp_lexer_peek_token (parser
->lexer
));
38014 if (decl
&& is_overloaded_fn (decl
)
38015 && (TREE_CODE (decl
) != FUNCTION_DECL
38016 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38018 error_at (name_loc
,
38019 "%<#pragma acc routine%> names a set of overloads");
38020 parser
->oacc_routine
= NULL
;
38024 /* Perhaps we should use the same rule as declarations in different
38026 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38028 error_at (name_loc
,
38029 "%qD does not refer to a namespace scope function", decl
);
38030 parser
->oacc_routine
= NULL
;
38034 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38036 error_at (name_loc
, "%qD does not refer to a function", decl
);
38037 parser
->oacc_routine
= NULL
;
38041 cp_finalize_oacc_routine (parser
, decl
, false);
38042 parser
->oacc_routine
= NULL
;
38044 else /* No optional '( name )'. */
38046 /* Store away all pragma tokens. */
38047 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38048 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38049 cp_lexer_consume_token (parser
->lexer
);
38050 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38051 parser
->oacc_routine
->error_seen
= true;
38052 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38053 struct cp_token_cache
*cp
38054 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38055 parser
->oacc_routine
->tokens
.safe_push (cp
);
38057 /* Emit a helpful diagnostic if there's another pragma following this
38059 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38061 cp_ensure_no_oacc_routine (parser
);
38062 data
.tokens
.release ();
38063 /* ..., and then just keep going. */
38067 /* We only have to consider the pragma_external case here. */
38068 cp_parser_declaration (parser
);
38069 if (parser
->oacc_routine
38070 && !parser
->oacc_routine
->fndecl_seen
)
38071 cp_ensure_no_oacc_routine (parser
);
38073 parser
->oacc_routine
= NULL
;
38074 data
.tokens
.release ();
38078 /* Finalize #pragma acc routine clauses after direct declarator has
38082 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38084 struct cp_token_cache
*ce
;
38085 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38087 if (!data
->error_seen
&& data
->fndecl_seen
)
38089 error_at (data
->loc
,
38090 "%<#pragma acc routine%> not immediately followed by "
38091 "a single function declaration or definition");
38092 data
->error_seen
= true;
38094 if (data
->error_seen
)
38097 gcc_checking_assert (data
->tokens
.length () == 1);
38098 ce
= data
->tokens
[0];
38100 cp_parser_push_lexer_for_tokens (parser
, ce
);
38101 parser
->lexer
->in_pragma
= true;
38102 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38104 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38105 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38106 parser
->oacc_routine
->clauses
38107 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38108 "#pragma acc routine", pragma_tok
);
38109 cp_parser_pop_lexer (parser
);
38110 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38116 /* Apply any saved OpenACC routine clauses to a just-parsed
38120 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38122 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38124 /* Keep going if we're in error reporting mode. */
38125 if (parser
->oacc_routine
->error_seen
38126 || fndecl
== error_mark_node
)
38129 if (parser
->oacc_routine
->fndecl_seen
)
38131 error_at (parser
->oacc_routine
->loc
,
38132 "%<#pragma acc routine%> not immediately followed by"
38133 " a single function declaration or definition");
38134 parser
->oacc_routine
= NULL
;
38137 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38139 cp_ensure_no_oacc_routine (parser
);
38143 if (oacc_get_fn_attrib (fndecl
))
38145 error_at (parser
->oacc_routine
->loc
,
38146 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38147 parser
->oacc_routine
= NULL
;
38151 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38153 error_at (parser
->oacc_routine
->loc
,
38155 ? G_("%<#pragma acc routine%> must be applied before use")
38156 : G_("%<#pragma acc routine%> must be applied before "
38158 parser
->oacc_routine
= NULL
;
38162 /* Process the routine's dimension clauses. */
38163 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38164 oacc_replace_fn_attrib (fndecl
, dims
);
38166 /* Add an "omp declare target" attribute. */
38167 DECL_ATTRIBUTES (fndecl
)
38168 = tree_cons (get_identifier ("omp declare target"),
38169 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38171 /* Don't unset parser->oacc_routine here: we may still need it to
38172 diagnose wrong usage. But, remember that we've used this "#pragma acc
38174 parser
->oacc_routine
->fndecl_seen
= true;
38178 /* Main entry point to OpenMP statement pragmas. */
38181 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38184 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38185 omp_clause_mask
mask (0);
38187 switch (cp_parser_pragma_kind (pragma_tok
))
38189 case PRAGMA_OACC_ATOMIC
:
38190 cp_parser_omp_atomic (parser
, pragma_tok
);
38192 case PRAGMA_OACC_CACHE
:
38193 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38195 case PRAGMA_OACC_DATA
:
38196 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38198 case PRAGMA_OACC_ENTER_DATA
:
38199 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38201 case PRAGMA_OACC_EXIT_DATA
:
38202 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38204 case PRAGMA_OACC_HOST_DATA
:
38205 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38207 case PRAGMA_OACC_KERNELS
:
38208 case PRAGMA_OACC_PARALLEL
:
38209 strcpy (p_name
, "#pragma acc");
38210 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38213 case PRAGMA_OACC_LOOP
:
38214 strcpy (p_name
, "#pragma acc");
38215 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38218 case PRAGMA_OACC_UPDATE
:
38219 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38221 case PRAGMA_OACC_WAIT
:
38222 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38224 case PRAGMA_OMP_ATOMIC
:
38225 cp_parser_omp_atomic (parser
, pragma_tok
);
38227 case PRAGMA_OMP_CRITICAL
:
38228 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38230 case PRAGMA_OMP_DISTRIBUTE
:
38231 strcpy (p_name
, "#pragma omp");
38232 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38235 case PRAGMA_OMP_FOR
:
38236 strcpy (p_name
, "#pragma omp");
38237 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38240 case PRAGMA_OMP_MASTER
:
38241 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38243 case PRAGMA_OMP_PARALLEL
:
38244 strcpy (p_name
, "#pragma omp");
38245 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38248 case PRAGMA_OMP_SECTIONS
:
38249 strcpy (p_name
, "#pragma omp");
38250 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38252 case PRAGMA_OMP_SIMD
:
38253 strcpy (p_name
, "#pragma omp");
38254 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38257 case PRAGMA_OMP_SINGLE
:
38258 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38260 case PRAGMA_OMP_TASK
:
38261 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38263 case PRAGMA_OMP_TASKGROUP
:
38264 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38266 case PRAGMA_OMP_TASKLOOP
:
38267 strcpy (p_name
, "#pragma omp");
38268 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38271 case PRAGMA_OMP_TEAMS
:
38272 strcpy (p_name
, "#pragma omp");
38273 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38277 gcc_unreachable ();
38280 protected_set_expr_location (stmt
, pragma_tok
->location
);
38283 /* Transactional Memory parsing routines. */
38285 /* Parse a transaction attribute.
38291 We use this instead of cp_parser_attributes_opt for transactions to avoid
38292 the pedwarn in C++98 mode. */
38295 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38298 tree attr_name
, attr
= NULL
;
38300 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38301 return cp_parser_attributes_opt (parser
);
38303 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38305 cp_lexer_consume_token (parser
->lexer
);
38306 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38309 token
= cp_lexer_peek_token (parser
->lexer
);
38310 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38312 token
= cp_lexer_consume_token (parser
->lexer
);
38314 attr_name
= (token
->type
== CPP_KEYWORD
38315 /* For keywords, use the canonical spelling,
38316 not the parsed identifier. */
38317 ? ridpointers
[(int) token
->keyword
]
38319 attr
= build_tree_list (attr_name
, NULL_TREE
);
38322 cp_parser_error (parser
, "expected identifier");
38324 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38326 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38330 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38332 transaction-statement:
38333 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38335 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38339 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38341 unsigned char old_in
= parser
->in_transaction
;
38342 unsigned char this_in
= 1, new_in
;
38343 enum rid keyword
= token
->keyword
;
38344 tree stmt
, attrs
, noex
;
38346 cp_lexer_consume_token (parser
->lexer
);
38348 if (keyword
== RID_TRANSACTION_RELAXED
38349 || keyword
== RID_SYNCHRONIZED
)
38350 this_in
|= TM_STMT_ATTR_RELAXED
;
38353 attrs
= cp_parser_txn_attribute_opt (parser
);
38355 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38358 /* Parse a noexcept specification. */
38359 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38360 noex
= boolean_true_node
;
38361 else if (keyword
== RID_ATOMIC_CANCEL
)
38363 /* cancel-and-throw is unimplemented. */
38364 sorry ("atomic_cancel");
38368 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38370 /* Keep track if we're in the lexical scope of an outer transaction. */
38371 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38373 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38375 parser
->in_transaction
= new_in
;
38376 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38377 parser
->in_transaction
= old_in
;
38379 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38384 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38386 transaction-expression:
38387 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38388 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38392 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38394 unsigned char old_in
= parser
->in_transaction
;
38395 unsigned char this_in
= 1;
38399 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38401 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38402 || keyword
== RID_TRANSACTION_RELAXED
);
38406 keyword
== RID_TRANSACTION_RELAXED
38407 ? G_("%<__transaction_relaxed%> without transactional memory "
38409 : G_("%<__transaction_atomic%> without transactional memory "
38410 "support enabled"));
38412 token
= cp_parser_require_keyword (parser
, keyword
,
38413 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38414 : RT_TRANSACTION_RELAXED
));
38415 gcc_assert (token
!= NULL
);
38417 if (keyword
== RID_TRANSACTION_RELAXED
)
38418 this_in
|= TM_STMT_ATTR_RELAXED
;
38420 /* Set this early. This might mean that we allow transaction_cancel in
38421 an expression that we find out later actually has to be a constexpr.
38422 However, we expect that cxx_constant_value will be able to deal with
38423 this; also, if the noexcept has no constexpr, then what we parse next
38424 really is a transaction's body. */
38425 parser
->in_transaction
= this_in
;
38427 /* Parse a noexcept specification. */
38428 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38431 if (!noex
|| !noex_expr
38432 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38434 matching_parens parens
;
38435 parens
.require_open (parser
);
38437 expr
= cp_parser_expression (parser
);
38438 expr
= finish_parenthesized_expr (expr
);
38440 parens
.require_close (parser
);
38444 /* The only expression that is available got parsed for the noexcept
38445 already. noexcept is true then. */
38447 noex
= boolean_true_node
;
38450 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38451 parser
->in_transaction
= old_in
;
38453 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38454 return error_mark_node
;
38456 return (flag_tm
? expr
: error_mark_node
);
38459 /* Parse a function-transaction-block.
38461 function-transaction-block:
38462 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38464 __transaction_atomic txn-attribute[opt] function-try-block
38465 __transaction_relaxed ctor-initializer[opt] function-body
38466 __transaction_relaxed function-try-block
38470 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38472 unsigned char old_in
= parser
->in_transaction
;
38473 unsigned char new_in
= 1;
38474 tree compound_stmt
, stmt
, attrs
;
38477 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38478 || keyword
== RID_TRANSACTION_RELAXED
);
38479 token
= cp_parser_require_keyword (parser
, keyword
,
38480 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38481 : RT_TRANSACTION_RELAXED
));
38482 gcc_assert (token
!= NULL
);
38484 if (keyword
== RID_TRANSACTION_RELAXED
)
38485 new_in
|= TM_STMT_ATTR_RELAXED
;
38488 attrs
= cp_parser_txn_attribute_opt (parser
);
38490 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38493 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38495 parser
->in_transaction
= new_in
;
38497 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38498 cp_parser_function_try_block (parser
);
38500 cp_parser_ctor_initializer_opt_and_function_body
38501 (parser
, /*in_function_try_block=*/false);
38503 parser
->in_transaction
= old_in
;
38505 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38508 /* Parse a __transaction_cancel statement.
38511 __transaction_cancel txn-attribute[opt] ;
38512 __transaction_cancel txn-attribute[opt] throw-expression ;
38514 ??? Cancel and throw is not yet implemented. */
38517 cp_parser_transaction_cancel (cp_parser
*parser
)
38520 bool is_outer
= false;
38523 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38524 RT_TRANSACTION_CANCEL
);
38525 gcc_assert (token
!= NULL
);
38527 attrs
= cp_parser_txn_attribute_opt (parser
);
38529 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38531 /* ??? Parse cancel-and-throw here. */
38533 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38537 error_at (token
->location
, "%<__transaction_cancel%> without "
38538 "transactional memory support enabled");
38539 return error_mark_node
;
38541 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38543 error_at (token
->location
, "%<__transaction_cancel%> within a "
38544 "%<__transaction_relaxed%>");
38545 return error_mark_node
;
38549 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38550 && !is_tm_may_cancel_outer (current_function_decl
))
38552 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38553 "within outer %<__transaction_atomic%>");
38554 error_at (token
->location
,
38555 " or a %<transaction_may_cancel_outer%> function");
38556 return error_mark_node
;
38559 else if (parser
->in_transaction
== 0)
38561 error_at (token
->location
, "%<__transaction_cancel%> not within "
38562 "%<__transaction_atomic%>");
38563 return error_mark_node
;
38566 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38574 static GTY (()) cp_parser
*the_parser
;
38577 /* Special handling for the first token or line in the file. The first
38578 thing in the file might be #pragma GCC pch_preprocess, which loads a
38579 PCH file, which is a GC collection point. So we need to handle this
38580 first pragma without benefit of an existing lexer structure.
38582 Always returns one token to the caller in *FIRST_TOKEN. This is
38583 either the true first token of the file, or the first token after
38584 the initial pragma. */
38587 cp_parser_initial_pragma (cp_token
*first_token
)
38591 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38592 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38595 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38596 if (first_token
->type
== CPP_STRING
)
38598 name
= first_token
->u
.value
;
38600 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38601 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38602 error_at (first_token
->location
,
38603 "junk at end of %<#pragma GCC pch_preprocess%>");
38606 error_at (first_token
->location
, "expected string literal");
38608 /* Skip to the end of the pragma. */
38609 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38610 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38612 /* Now actually load the PCH file. */
38614 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38616 /* Read one more token to return to our caller. We have to do this
38617 after reading the PCH file in, since its pointers have to be
38619 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38622 /* Parse a pragma GCC ivdep. */
38625 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38627 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38631 /* Parse a pragma GCC unroll. */
38633 static unsigned short
38634 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38636 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38637 tree expr
= cp_parser_constant_expression (parser
);
38638 unsigned short unroll
;
38639 expr
= maybe_constant_value (expr
);
38640 HOST_WIDE_INT lunroll
= 0;
38641 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38642 || TREE_CODE (expr
) != INTEGER_CST
38643 || (lunroll
= tree_to_shwi (expr
)) < 0
38644 || lunroll
>= USHRT_MAX
)
38646 error_at (location
, "%<#pragma GCC unroll%> requires an"
38647 " assignment-expression that evaluates to a non-negative"
38648 " integral constant less than %u", USHRT_MAX
);
38653 unroll
= (unsigned short)lunroll
;
38657 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38661 /* Normal parsing of a pragma token. Here we can (and must) use the
38665 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38667 cp_token
*pragma_tok
;
38672 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38673 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38674 parser
->lexer
->in_pragma
= true;
38676 id
= cp_parser_pragma_kind (pragma_tok
);
38677 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38678 cp_ensure_no_omp_declare_simd (parser
);
38681 case PRAGMA_GCC_PCH_PREPROCESS
:
38682 error_at (pragma_tok
->location
,
38683 "%<#pragma GCC pch_preprocess%> must be first");
38686 case PRAGMA_OMP_BARRIER
:
38689 case pragma_compound
:
38690 cp_parser_omp_barrier (parser
, pragma_tok
);
38693 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38694 "used in compound statements", "omp barrier");
38701 case PRAGMA_OMP_FLUSH
:
38704 case pragma_compound
:
38705 cp_parser_omp_flush (parser
, pragma_tok
);
38708 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38709 "used in compound statements", "omp flush");
38716 case PRAGMA_OMP_TASKWAIT
:
38719 case pragma_compound
:
38720 cp_parser_omp_taskwait (parser
, pragma_tok
);
38723 error_at (pragma_tok
->location
,
38724 "%<#pragma %s%> may only be used in compound statements",
38732 case PRAGMA_OMP_TASKYIELD
:
38735 case pragma_compound
:
38736 cp_parser_omp_taskyield (parser
, pragma_tok
);
38739 error_at (pragma_tok
->location
,
38740 "%<#pragma %s%> may only be used in compound statements",
38748 case PRAGMA_OMP_CANCEL
:
38751 case pragma_compound
:
38752 cp_parser_omp_cancel (parser
, pragma_tok
);
38755 error_at (pragma_tok
->location
,
38756 "%<#pragma %s%> may only be used in compound statements",
38764 case PRAGMA_OMP_CANCELLATION_POINT
:
38765 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38768 case PRAGMA_OMP_THREADPRIVATE
:
38769 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38772 case PRAGMA_OMP_DECLARE
:
38773 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38775 case PRAGMA_OACC_DECLARE
:
38776 cp_parser_oacc_declare (parser
, pragma_tok
);
38779 case PRAGMA_OACC_ENTER_DATA
:
38780 if (context
== pragma_stmt
)
38782 error_at (pragma_tok
->location
,
38783 "%<#pragma %s%> may only be used in compound statements",
38787 else if (context
!= pragma_compound
)
38789 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38792 case PRAGMA_OACC_EXIT_DATA
:
38793 if (context
== pragma_stmt
)
38795 error_at (pragma_tok
->location
,
38796 "%<#pragma %s%> may only be used in compound statements",
38800 else if (context
!= pragma_compound
)
38802 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38805 case PRAGMA_OACC_ROUTINE
:
38806 if (context
!= pragma_external
)
38808 error_at (pragma_tok
->location
,
38809 "%<#pragma acc routine%> must be at file scope");
38812 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38815 case PRAGMA_OACC_UPDATE
:
38816 if (context
== pragma_stmt
)
38818 error_at (pragma_tok
->location
,
38819 "%<#pragma %s%> may only be used in compound statements",
38823 else if (context
!= pragma_compound
)
38825 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38828 case PRAGMA_OACC_WAIT
:
38829 if (context
== pragma_stmt
)
38831 error_at (pragma_tok
->location
,
38832 "%<#pragma %s%> may only be used in compound statements",
38836 else if (context
!= pragma_compound
)
38838 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38841 case PRAGMA_OACC_ATOMIC
:
38842 case PRAGMA_OACC_CACHE
:
38843 case PRAGMA_OACC_DATA
:
38844 case PRAGMA_OACC_HOST_DATA
:
38845 case PRAGMA_OACC_KERNELS
:
38846 case PRAGMA_OACC_PARALLEL
:
38847 case PRAGMA_OACC_LOOP
:
38848 case PRAGMA_OMP_ATOMIC
:
38849 case PRAGMA_OMP_CRITICAL
:
38850 case PRAGMA_OMP_DISTRIBUTE
:
38851 case PRAGMA_OMP_FOR
:
38852 case PRAGMA_OMP_MASTER
:
38853 case PRAGMA_OMP_PARALLEL
:
38854 case PRAGMA_OMP_SECTIONS
:
38855 case PRAGMA_OMP_SIMD
:
38856 case PRAGMA_OMP_SINGLE
:
38857 case PRAGMA_OMP_TASK
:
38858 case PRAGMA_OMP_TASKGROUP
:
38859 case PRAGMA_OMP_TASKLOOP
:
38860 case PRAGMA_OMP_TEAMS
:
38861 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38863 stmt
= push_omp_privatization_clauses (false);
38864 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38865 pop_omp_privatization_clauses (stmt
);
38868 case PRAGMA_OMP_ORDERED
:
38869 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38871 stmt
= push_omp_privatization_clauses (false);
38872 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38873 pop_omp_privatization_clauses (stmt
);
38876 case PRAGMA_OMP_TARGET
:
38877 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38879 stmt
= push_omp_privatization_clauses (false);
38880 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38881 pop_omp_privatization_clauses (stmt
);
38884 case PRAGMA_OMP_END_DECLARE_TARGET
:
38885 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38888 case PRAGMA_OMP_SECTION
:
38889 error_at (pragma_tok
->location
,
38890 "%<#pragma omp section%> may only be used in "
38891 "%<#pragma omp sections%> construct");
38896 if (context
== pragma_external
)
38898 error_at (pragma_tok
->location
,
38899 "%<#pragma GCC ivdep%> must be inside a function");
38902 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
38903 unsigned short unroll
;
38904 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38905 if (tok
->type
== CPP_PRAGMA
38906 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
38908 tok
= cp_lexer_consume_token (parser
->lexer
);
38909 unroll
= cp_parser_pragma_unroll (parser
, tok
);
38910 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38914 if (tok
->type
!= CPP_KEYWORD
38915 || (tok
->keyword
!= RID_FOR
38916 && tok
->keyword
!= RID_WHILE
38917 && tok
->keyword
!= RID_DO
))
38919 cp_parser_error (parser
, "for, while or do statement expected");
38922 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38926 case PRAGMA_UNROLL
:
38928 if (context
== pragma_external
)
38930 error_at (pragma_tok
->location
,
38931 "%<#pragma GCC unroll%> must be inside a function");
38934 const unsigned short unroll
38935 = cp_parser_pragma_unroll (parser
, pragma_tok
);
38937 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38938 if (tok
->type
== CPP_PRAGMA
38939 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
38941 tok
= cp_lexer_consume_token (parser
->lexer
);
38942 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
38943 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38947 if (tok
->type
!= CPP_KEYWORD
38948 || (tok
->keyword
!= RID_FOR
38949 && tok
->keyword
!= RID_WHILE
38950 && tok
->keyword
!= RID_DO
))
38952 cp_parser_error (parser
, "for, while or do statement expected");
38955 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38960 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
38961 c_invoke_pragma_handler (id
);
38965 cp_parser_error (parser
, "expected declaration specifiers");
38969 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38973 /* The interface the pragma parsers have to the lexer. */
38976 pragma_lex (tree
*value
, location_t
*loc
)
38978 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38979 enum cpp_ttype ret
= tok
->type
;
38981 *value
= tok
->u
.value
;
38983 *loc
= tok
->location
;
38985 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
38987 else if (ret
== CPP_STRING
)
38988 *value
= cp_parser_string_literal (the_parser
, false, false);
38991 if (ret
== CPP_KEYWORD
)
38993 cp_lexer_consume_token (the_parser
->lexer
);
39000 /* External interface. */
39002 /* Parse one entire translation unit. */
39005 c_parse_file (void)
39007 static bool already_called
= false;
39009 if (already_called
)
39010 fatal_error (input_location
,
39011 "inter-module optimizations not implemented for C++");
39012 already_called
= true;
39014 the_parser
= cp_parser_new ();
39015 push_deferring_access_checks (flag_access_control
39016 ? dk_no_deferred
: dk_no_check
);
39017 cp_parser_translation_unit (the_parser
);
39021 /* Create an identifier for a generic parameter type (a synthesized
39022 template parameter implied by `auto' or a concept identifier). */
39024 static GTY(()) int generic_parm_count
;
39026 make_generic_type_name ()
39029 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39030 return get_identifier (buf
);
39033 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39034 (creating a new template parameter list if necessary). Returns the newly
39035 created template type parm. */
39038 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39040 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39042 /* Before committing to modifying any scope, if we're in an
39043 implicit template scope, and we're trying to synthesize a
39044 constrained parameter, try to find a previous parameter with
39045 the same name. This is the same-type rule for abbreviated
39046 function templates.
39048 NOTE: We can generate implicit parameters when tentatively
39049 parsing a nested name specifier, only to reject that parse
39050 later. However, matching the same template-id as part of a
39051 direct-declarator should generate an identical template
39052 parameter, so this rule will merge them. */
39053 if (parser
->implicit_template_scope
&& constr
)
39055 tree t
= parser
->implicit_template_parms
;
39058 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39060 tree d
= TREE_VALUE (t
);
39061 if (TREE_CODE (d
) == PARM_DECL
)
39062 /* Return the TEMPLATE_PARM_INDEX. */
39063 d
= DECL_INITIAL (d
);
39066 t
= TREE_CHAIN (t
);
39070 /* We are either continuing a function template that already contains implicit
39071 template parameters, creating a new fully-implicit function template, or
39072 extending an existing explicit function template with implicit template
39075 cp_binding_level
*const entry_scope
= current_binding_level
;
39077 bool become_template
= false;
39078 cp_binding_level
*parent_scope
= 0;
39080 if (parser
->implicit_template_scope
)
39082 gcc_assert (parser
->implicit_template_parms
);
39084 current_binding_level
= parser
->implicit_template_scope
;
39088 /* Roll back to the existing template parameter scope (in the case of
39089 extending an explicit function template) or introduce a new template
39090 parameter scope ahead of the function parameter scope (or class scope
39091 in the case of out-of-line member definitions). The function scope is
39092 added back after template parameter synthesis below. */
39094 cp_binding_level
*scope
= entry_scope
;
39096 while (scope
->kind
== sk_function_parms
)
39098 parent_scope
= scope
;
39099 scope
= scope
->level_chain
;
39101 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39103 /* If not defining a class, then any class scope is a scope level in
39104 an out-of-line member definition. In this case simply wind back
39105 beyond the first such scope to inject the template parameter list.
39106 Otherwise wind back to the class being defined. The latter can
39107 occur in class member friend declarations such as:
39113 friend void A::foo (auto);
39116 The template parameter list synthesized for the friend declaration
39117 must be injected in the scope of 'B'. This can also occur in
39118 erroneous cases such as:
39124 void B::foo (auto) {}
39127 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39128 but, nevertheless, the template parameter list synthesized for the
39129 declarator should be injected into the scope of 'A' as if the
39130 ill-formed template was specified explicitly. */
39132 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39134 parent_scope
= scope
;
39135 scope
= scope
->level_chain
;
39139 current_binding_level
= scope
;
39141 if (scope
->kind
!= sk_template_parms
39142 || !function_being_declared_is_template_p (parser
))
39144 /* Introduce a new template parameter list for implicit template
39147 become_template
= true;
39149 parser
->implicit_template_scope
39150 = begin_scope (sk_template_parms
, NULL
);
39152 ++processing_template_decl
;
39154 parser
->fully_implicit_function_template_p
= true;
39155 ++parser
->num_template_parameter_lists
;
39159 /* Synthesize implicit template parameters at the end of the explicit
39160 template parameter list. */
39162 gcc_assert (current_template_parms
);
39164 parser
->implicit_template_scope
= scope
;
39166 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39167 parser
->implicit_template_parms
39168 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39172 /* Synthesize a new template parameter and track the current template
39173 parameter chain with implicit_template_parms. */
39175 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39176 tree synth_id
= make_generic_type_name ();
39177 tree synth_tmpl_parm
;
39178 bool non_type
= false;
39180 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39182 = finish_template_type_parm (class_type_node
, synth_id
);
39183 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39185 = finish_constrained_template_template_parm (proto
, synth_id
);
39188 synth_tmpl_parm
= copy_decl (proto
);
39189 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39193 // Attach the constraint to the parm before processing.
39194 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39195 TREE_TYPE (node
) = constr
;
39197 = process_template_parm (parser
->implicit_template_parms
,
39200 /*non_type=*/non_type
,
39201 /*param_pack=*/false);
39203 // Chain the new parameter to the list of implicit parameters.
39204 if (parser
->implicit_template_parms
)
39205 parser
->implicit_template_parms
39206 = TREE_CHAIN (parser
->implicit_template_parms
);
39208 parser
->implicit_template_parms
= new_parm
;
39210 tree new_decl
= get_local_decls ();
39212 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39213 new_decl
= DECL_INITIAL (new_decl
);
39215 /* If creating a fully implicit function template, start the new implicit
39216 template parameter list with this synthesized type, otherwise grow the
39217 current template parameter list. */
39219 if (become_template
)
39221 parent_scope
->level_chain
= current_binding_level
;
39223 tree new_parms
= make_tree_vec (1);
39224 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39225 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39226 new_parms
, current_template_parms
);
39230 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39231 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39232 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39233 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39236 // If the new parameter was constrained, we need to add that to the
39237 // constraints in the template parameter list.
39238 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39240 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39241 reqs
= conjoin_constraints (reqs
, req
);
39242 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39245 current_binding_level
= entry_scope
;
39250 /* Finish the declaration of a fully implicit function template. Such a
39251 template has no explicit template parameter list so has not been through the
39252 normal template head and tail processing. synthesize_implicit_template_parm
39253 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39254 provided if the declaration is a class member such that its template
39255 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39256 form is returned. Otherwise NULL_TREE is returned. */
39259 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39261 gcc_assert (parser
->fully_implicit_function_template_p
);
39263 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39264 && DECL_VIRTUAL_P (member_decl_opt
))
39266 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39267 "implicit templates may not be %<virtual%>");
39268 DECL_VIRTUAL_P (member_decl_opt
) = false;
39271 if (member_decl_opt
)
39272 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39273 end_template_decl ();
39275 parser
->fully_implicit_function_template_p
= false;
39276 --parser
->num_template_parameter_lists
;
39278 return member_decl_opt
;
39281 /* Helper function for diagnostics that have complained about things
39282 being used with 'extern "C"' linkage.
39284 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39287 maybe_show_extern_c_location (void)
39289 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39290 inform (the_parser
->innermost_linkage_specification_location
,
39291 "%<extern \"C\"%> linkage started here");
39294 #include "gt-cp-parser.h"